/** ** Default Destroy function, please override **/ public virtual void destroy(AnimatedMeshState state) { if (this.autoDestroy && state.mesh != null) { if (!this.createAsset) { Destroy(state.mesh); } state.mesh = null; } }
public void update(AnimatedMeshState state) { if (!(state.finished = (state.lifeRatio = (Time.time - state.startTime) / this.duration) > 1)) { this.updateMesh(state); } else { this.destroy(state); } }
/** ** Method to call before launching the animation **/ public AnimatedMeshState start(MeshFilter mf, MeshRenderer mr = null) { AnimatedMeshState state = new AnimatedMeshState(); state.startTime = Time.time; state.endTime = state.startTime + duration; state.finished = false; state.data = this.initialize(mf, mr); state.mesh = this.generateMesh(state); return(state); }
// Update is called once per frame void Update() { if (this.startTime <= Time.time && !this.start) { this.state = test.start(mf, mr); this.mf.mesh = this.state.mesh; this.start = true; } if (this.start) { test.update(state); } }
public override void updateMesh(AnimatedMeshState state) { Vector3[] vertices = state.mesh.vertices; AnimatedConeState acState = state.data as AnimatedConeState; ConeState current = acState.currentState; float lifeRatio = state.lifeRatio; int numVertices = this.data.numVertices; int i; current.length = Mathf.Lerp(this.data.initialState.length, this.data.finalState.length, this.data.interpolationCurve.Evaluate(lifeRatio)); current.radiusBottom = Mathf.Lerp(this.data.initialState.radiusBottom, this.data.finalState.radiusBottom, this.data.interpolationCurve.Evaluate(lifeRatio)); current.radiusTop = Mathf.Lerp(this.data.initialState.radiusTop, this.data.finalState.radiusTop, this.data.interpolationCurve.Evaluate(lifeRatio)); for (i = 0; i < numVertices; ++i) { float angle = 2 * Mathf.PI * i / numVertices; float angleSin = Mathf.Sin(angle); float angleCos = Mathf.Cos(angle); vertices[i] = new Vector3(current.radiusTop * angleCos, current.radiusTop * angleSin, -current.length); vertices[i + numVertices + 1] = new Vector3(current.radiusBottom * angleCos, current.radiusBottom * angleSin, 0); } // connecting edges ++numVertices; vertices[i] = new Vector3(vertices[0].x, vertices[0].y, vertices[0].z); vertices[i + numVertices] = new Vector3(vertices[numVertices].x, vertices[numVertices].y, vertices[numVertices].z); --numVertices; state.mesh.vertices = vertices; if (this.data.materialModifier && acState.mr) { this.data.materialModifier.update(acState.materialModifierState, this.data.interpolationCurve.Evaluate(lifeRatio)); this.data.materialModifier.updateMaterial(acState.materialModifierState, acState.mr.material); } }
public override Mesh generateMesh(AnimatedMeshState state) { AnimatedConeState acState = state.data as AnimatedConeState; Vector3[] vertices; Mesh mesh; int numVertices = this.data.numVertices; ConeState init = this.data.initialState; string path = meshPrefabPath + "Cone" + this.data.numVertices + "v" + init.radiusTop + "t" + init.radiusBottom + "b" + init.length + "l" + init.length + "o" + ".asset"; if (!this.createAsset || !(mesh = (Mesh)AssetDatabase.LoadAssetAtPath(path, typeof(Mesh)))) { mesh = new Mesh(); mesh.Clear(); numVertices += 1; // for correct uv mapping (to join correctly the faces) vertices = new Vector3[2 * numVertices]; // 0..n-1: top, n..2n-1: bottom Vector3[] normals = new Vector3[2 * numVertices]; Vector2[] uvs = new Vector2[2 * numVertices]; int[] tris; float slope = Mathf.Atan((init.radiusBottom - init.radiusTop) / init.length); // (rad difference)/height float slopeSin = Mathf.Sin(slope); float slopeCos = Mathf.Cos(slope); int i; --numVertices; for (i = 0; i < numVertices; ++i) { float angle = 2 * Mathf.PI * i / numVertices; float angleSin = Mathf.Sin(angle); float angleCos = Mathf.Cos(angle); float angleHalf = 2 * Mathf.PI * (i + 0.5f) / numVertices; // for degenerated normals at cone tips float angleHalfSin = Mathf.Sin(angleHalf); float angleHalfCos = Mathf.Cos(angleHalf); vertices[i] = new Vector3(init.radiusTop * angleCos, init.radiusTop * angleSin, -init.length); vertices[i + numVertices + 1] = new Vector3(init.radiusBottom * angleCos, init.radiusBottom * angleSin, 0); normals[i] = new Vector3(angleCos * slopeCos, angleSin * slopeCos, -slopeSin); normals[i + numVertices + 1] = new Vector3(angleCos * slopeCos, angleSin * slopeCos, -slopeSin); uvs[i] = new Vector2(1.0f * i / numVertices, 1); uvs[i + numVertices + 1] = new Vector2(1.0f * i / numVertices, 0); } // connecting edges ++numVertices; vertices[i] = new Vector3(vertices[0].x, vertices[0].y, vertices[0].z); vertices[i + numVertices] = new Vector3(vertices[numVertices].x, vertices[numVertices].y, vertices[numVertices].z); uvs[i] = new Vector2(1, 1); uvs[i + numVertices] = new Vector2(1, 0); normals[i] = new Vector3(normals[0].x, normals[0].y, normals[0].z); normals[i + numVertices] = new Vector3(normals[numVertices].x, normals[numVertices].y, normals[numVertices].z); mesh.vertices = vertices; mesh.normals = normals; mesh.uv = uvs; // creating triangles int cnt = 0; tris = new int[numVertices * 6]; for (i = 0; i < numVertices; i++) { int ip1 = i + 1; if (ip1 == numVertices) { ip1 = 0; } tris[cnt++] = i; tris[cnt++] = ip1; tris[cnt++] = i + numVertices; tris[cnt++] = ip1 + numVertices; tris[cnt++] = i + numVertices; tris[cnt++] = ip1; } --numVertices; mesh.triangles = tris; if (this.createAsset) { AssetDatabase.CreateAsset(mesh, path); AssetDatabase.SaveAssets(); } } return(mesh); }
/** ** Use this.lifeRatio to update the data **/ public abstract void updateMesh(AnimatedMeshState data);
/** ** Generate the mesh for the data given as parameter (that could contain a meshFilter for example) **/ public abstract Mesh generateMesh(AnimatedMeshState state);