Exemplo n.º 1
0
 public vertexConstraints Add(Verlet.DistConstraint addThis)
 {
     Verlet.DistConstraint dC = new Verlet.DistConstraint();
     if (c1 == dC)
     {
         c1 = addThis; numValid = 1; return(this);
     }
     if (c2 == dC)
     {
         c2 = addThis; numValid = 2; return(this);
     }
     if (c3 == dC)
     {
         c3 = addThis; numValid = 3; return(this);
     }
     if (c4 == dC)
     {
         c4 = addThis; numValid = 4; return(this);
     }
     if (c5 == dC)
     {
         c5 = addThis; numValid = 5; return(this);
     }
     if (c6 == dC)
     {
         c6 = addThis; numValid = 6; return(this);
     }
     return(this);
 }
Exemplo n.º 2
0
        public SoftbodyData(Mesh bodyMesh, Transform transform, List <Verlet.DistConstraint> constraintsList, Vector3 scaledGravity, Transform ground, float radius = 1f, float friction = 0.3f)
        {
            bodyVerts = new NativeArray <Vector3>(bodyMesh.vertices, Allocator.Persistent);
            for (int i = 0; i < bodyVerts.Length; i++)
            {
                bodyVerts[i] *= radius;
            }
            preCollisionVerts = new NativeArray <Vector3>(bodyVerts, Allocator.Persistent);
            kabschVerts       = new NativeArray <Vector4>(Array.ConvertAll(bodyVerts.ToArray(), (p => new Vector4(p.x, p.y, p.z, 1f))), Allocator.Persistent);
            int[] triangles = bodyMesh.triangles; Vector3Int[] tris = new Vector3Int[triangles.Length / 3];
            for (int i = 0; i < tris.Length; i++)
            {
                tris[i] = new Vector3Int(triangles[i * 3], triangles[(i * 3) + 1], triangles[(i * 3) + 2]);
            }
            bodyTriangles            = new NativeArray <Vector3Int>(tris, Allocator.Persistent);
            bodyNormals              = new NativeArray <Vector3>(bodyMesh.normals, Allocator.Persistent);
            renderNormals            = new NativeArray <Vector3>(bodyMesh.normals, Allocator.Persistent);
            prevBodyVerts            = new NativeArray <Vector3>(new Vector3[bodyVerts.Length], Allocator.Persistent);
            accumulatedDisplacements = new NativeArray <Vector4>(new Vector4[bodyVerts.Length], Allocator.Persistent);
            for (int i = 0; i < bodyVerts.Length; i++)
            {
                prevBodyVerts[i] = transform.TransformPoint(bodyVerts[i]);
            }
            raycasts    = new NativeArray <RaycastCommand>(new RaycastCommand[bodyVerts.Length], Allocator.Persistent);
            raycastHits = new NativeArray <RaycastHit>(new RaycastHit[bodyVerts.Length], Allocator.Persistent);

            constraintsArray     = new NativeArray <Verlet.DistConstraint>(constraintsList.ToArray(), Allocator.Persistent);
            dilationDistance     = new NativeArray <float>(new float[1], Allocator.Persistent);
            triangleVolumes      = new NativeArray <float>(new float[tris.Length], Allocator.Persistent);
            triangleSurfaceAreas = new NativeArray <float>(new float[tris.Length], Allocator.Persistent);

            volumeAccumulator = new NativeAccumulator <float, Addition>(Allocator.Persistent);
            areaAccumulator   = new NativeAccumulator <float, Addition>(Allocator.Persistent);

            initialVolume       = Verlet.VolumeOfMesh(bodyVerts, bodyTriangles);
            initialSurfaceArea  = Verlet.SurfaceAreaOfMesh(bodyVerts, bodyTriangles);
            previousDeltaTime   = 1f;
            dilationDistance[0] = 0f;
            this.scaledGravity  = scaledGravity;
            localToWorld        = transform.localToWorldMatrix;
            worldToLocal        = transform.worldToLocalMatrix;
            groundPlanePos      = ground.position;
            groundPlaneNormal   = -ground.forward;
            this.radius         = radius;
            this.friction       = friction;

            //Set up parallel normal accumulation
            triangleGraph = new NativeArray <vertexTriangles>(new vertexTriangles[bodyVerts.Length], Allocator.Persistent);
            for (int i = 0; i < triangleGraph.Length; i++)
            {
                triangleGraph[i] = new vertexTriangles(-1);
            }
            for (int i = 0; i < tris.Length; i++)
            {
                triangleGraph[tris[i].x] = triangleGraph[tris[i].x].Add(tris[i]);
                triangleGraph[tris[i].y] = triangleGraph[tris[i].y].Add(tris[i]);
                triangleGraph[tris[i].z] = triangleGraph[tris[i].z].Add(tris[i]);
            }

            //Set up parallel constraint satisfaction
            connectionGraph = new NativeArray <vertexConstraints>(new vertexConstraints[bodyVerts.Length], Allocator.Persistent);
            for (int i = 0; i < connectionGraph.Length; i++)
            {
                connectionGraph[i] = new vertexConstraints();
            }
            for (int i = 0; i < constraintsArray.Length; i++)
            {
                Verlet.DistConstraint constraint = constraintsArray[i];
                connectionGraph[constraint.index1] = connectionGraph[constraint.index1].Add(constraint);
                int temp = constraint.index1; constraint.index1 = constraint.index2; constraint.index2 = temp;
                connectionGraph[constraint.index1] = connectionGraph[constraint.index1].Add(constraint);
            }
        }