void Start() { Deform = G.Engine.Op <Ops.Deform>(); deformingMesh = GetComponent <MeshFilter>().mesh; originalVertices = deformingMesh.vertices; { var tmp = new Vector3[originalVertices.Length]; Array.Copy(originalVertices, tmp, originalVertices.Length); displacedVertices = new Ar <Vector3>(G.Engine, tmp); } vertexVelocities = new Ar <Vector3>(G.Engine, new Vector3[originalVertices.Length]); uniformScale = transform.localScale.x; G.Engine.Computer(cd, Dep.On(Deform, G.Tick), () => { if (Deform.Count <= 0 && isStill(this.vertexVelocities.Read())) { return; } var vertexVelocities = this.vertexVelocities.AsWrite(); var displacedVertices = this.displacedVertices.Read(); float dt = G.Tick.Reduced; for (int i = 0, n = Deform.Count; i < n; ++i) { var d = Deform[i]; AddDeformingForce(d.Point, d.Force, displacedVertices, vertexVelocities, dt); } for (int i = 0, n = displacedVertices.Length; i < n; i++) { UpdateVelocity(i, displacedVertices, vertexVelocities, dt); } }); G.Engine.Computer(cd, Dep.On(G.Tick), () => { if (isStill(this.vertexVelocities.Read())) { return; } var vertexVelocities = this.vertexVelocities.Read(); var displacedVertices = this.displacedVertices.AsWrite(); float dt = G.Tick.Reduced; for (int i = 0, n = displacedVertices.Length; i < n; i++) { UpdateVertex(i, displacedVertices, vertexVelocities, dt); } }); G.Engine.Reader(cd, Dep.On(G.Tick), () => { deformingMesh.vertices = displacedVertices.Read(); deformingMesh.RecalculateNormals(); }); }
private void CreateVertices() { int cornerVertices = 8; int edgeVertices = (gridSize + gridSize + gridSize - 3) * 4; int faceVertices = ( (gridSize - 1) * (gridSize - 1) + (gridSize - 1) * (gridSize - 1) + (gridSize - 1) * (gridSize - 1)) * 2; vertices = new Vector3[cornerVertices + edgeVertices + faceVertices]; normals = new Vector3[vertices.Length]; cubeUV = new Color32[vertices.Length]; int v = 0; for (int y = 0; y <= gridSize; y++) { for (int x = 0; x <= gridSize; x++) { SetVertex(v++, x, y, 0); } for (int z = 1; z <= gridSize; z++) { SetVertex(v++, gridSize, y, z); } for (int x = gridSize - 1; x >= 0; x--) { SetVertex(v++, x, y, gridSize); } for (int z = gridSize - 1; z > 0; z--) { SetVertex(v++, 0, y, z); } } for (int z = 1; z < gridSize; z++) { for (int x = 1; x < gridSize; x++) { SetVertex(v++, x, gridSize, z); } } for (int z = 1; z < gridSize; z++) { for (int x = 1; x < gridSize; x++) { SetVertex(v++, x, 0, z); } } Vertices = new Ar <Vector3>(G.Engine, vertices); Normals = new Ar <Vector3>(G.Engine, normals); CubeUV = new Ar <Color32>(G.Engine, cubeUV); mesh.vertices = Vertices.Read(); mesh.normals = Normals.Read(); mesh.colors32 = CubeUV.Read(); }