- Home /
Bend a Rectangle plane using unity3d scripting
This is my college project...A image attached here..I want to flip the ears using unity3D scripting in Java??? I tried my level.but,i can't do it..Please send me a correct code..I attached flip code given below... Example Image:
Flip Code:
var twist = 1.0;
var inputSensitivity = 1.5;
private var baseVertices : Vector3[]; private var baseNormals : Vector3[];
function Update() { if(Input.anyKey)
//twist -= 10.0 *Input.GetAxis("Horizontal") * Time.deltaTime;
twist -= 10.0 *Input.GetAxis("Horizontal") * inputSensitivity * Time.deltaTime;
var mesh : Mesh = GetComponent(MeshFilter).mesh;
if (baseVertices == null)
baseVertices = mesh.vertices;
if (baseNormals == null)
baseNormals = mesh.normals;
var vertices = new Vector3[baseVertices.Length];
var normals = new Vector3[baseVertices.Length];
//var i : float =0.0;
for (var i=0;i<vertices.Length;i+=1)
//if(i<vertices.Length)
{
//i+=1;
vertices[i] = DoTwist(baseVertices[i], baseVertices[i].x * twist );
normals[i] = DoTwist(baseNormals[i], baseVertices[i].y * twist );
}
mesh.vertices = vertices;
mesh.normals = vertices;
mesh.RecalculateNormals();
mesh.RecalculateBounds();
}
function DoTwist( pos : Vector3, t : float ) {
var st = Mathf.Sin(t);
var ct = Mathf.Cos(t);
new_pos = Vector3.zero;
new_pos.x = pos.x*ct - pos.z*st;
new_pos.z = pos.x*st + pos.z*ct;
new_pos.y = pos.y;
return new_pos;
}
face.jpg
(23.9 kB)
Comment
Answer by Gasimo · Feb 01, 2018 at 04:54 PM
public class Bend : MonoBehaviour { public enum BendAxis {X, Y, Z};
public float rotate = 90;
public float fromPosition = 0.5F; //from 0 to 1
public BendAxis axis = BendAxis.X;
Mesh mesh;
Vector3[] vertices;
void Start()
{
mesh = GetComponent<MeshFilter>().mesh;
vertices = mesh.vertices;
if (axis == BendAxis.X)
{
float meshWidth = mesh.bounds.size.z;
for (var i = 0; i < vertices.Length; i++)
{
float formPos = Mathf.Lerp(meshWidth / 2, -meshWidth / 2, fromPosition);
float zeroPos = vertices[i].z + formPos;
float rotateValue = (-rotate / 2) * (zeroPos / meshWidth);
zeroPos -= 2 * vertices[i].x * Mathf.Cos ((90 - rotateValue) * Mathf.Deg2Rad);
vertices[i].x += zeroPos * Mathf.Sin(rotateValue * Mathf.Deg2Rad);
vertices[i].z = zeroPos * Mathf.Cos(rotateValue * Mathf.Deg2Rad) - formPos;
}
}
else if (axis == BendAxis.Y)
{
float meshWidth = mesh.bounds.size.z;
for (var i = 0; i < vertices.Length; i++)
{
float formPos = Mathf.Lerp(meshWidth / 2, -meshWidth / 2, fromPosition);
float zeroPos = vertices[i].z + formPos;
float rotateValue = (-rotate / 2) * (zeroPos / meshWidth);
zeroPos -= 2 * vertices[i].y * Mathf.Cos ((90 - rotateValue) * Mathf.Deg2Rad);
vertices[i].y += zeroPos * Mathf.Sin(rotateValue * Mathf.Deg2Rad);
vertices[i].z = zeroPos * Mathf.Cos(rotateValue * Mathf.Deg2Rad) - formPos;
}
}
else if (axis == BendAxis.Z)
{
float meshWidth = mesh.bounds.size.x;
for (var i = 0; i < vertices.Length; i++)
{
float formPos = Mathf.Lerp(meshWidth / 2, -meshWidth / 2, fromPosition);
float zeroPos = vertices[i].x + formPos;
float rotateValue = (-rotate / 2) * (zeroPos / meshWidth);
zeroPos -= 2 * vertices[i].y * Mathf.Cos ((90 - rotateValue) * Mathf.Deg2Rad);
vertices[i].y += zeroPos * Mathf.Sin(rotateValue * Mathf.Deg2Rad);
vertices[i].x = zeroPos * Mathf.Cos(rotateValue * Mathf.Deg2Rad) - formPos;
}
}
mesh.vertices = vertices;
mesh.RecalculateBounds ();
}
}
Your answer
Follow this Question
Related Questions
Simple mesh height alteration. 1 Answer
Is Mesh Collider possible on an animated plane ? 0 Answers
Animate one side of a plane 2 Answers