public ConvexPointCloudShape(Vector3Array points, int numPoints, Vector3 localScaling,
			bool computeAabb)
			: base(btConvexPointCloudShape_new3(points._native, numPoints, ref localScaling,
				computeAabb))
		{
            _unscaledPoints = points;
		}
Esempio n. 2
0
        public static void CreateConvexHull(ConvexHullShape shape, Mesh mesh)
        {
            ShapeHull hull = new ShapeHull(shape);

            hull.BuildHull(shape.Margin);

            int          vertexCount = hull.NumIndices;
            UIntArray    indices     = hull.Indices;
            Vector3Array points      = hull.Vertices;

            UnityEngine.Vector3[] vertices = new UnityEngine.Vector3[vertexCount];
            for (int i = 0; i < vertexCount; i++)
            {
                vertices[i] = points[(int)indices[i]].ToUnity();
            }
            int[] tris = new int[indices.Count];
            for (int i = 0; i < indices.Count; i++)
            {
                tris[i] = (int)indices[i];
            }
            mesh.vertices  = vertices;
            mesh.triangles = tris;
            mesh.RecalculateBounds();
            mesh.RecalculateNormals();
        }
        public static Vector3[] CreateConvexHull(ConvexHullShape shape)
        {
            var hull = new ShapeHull(shape);

            hull.BuildHull(shape.Margin);

            int          vertexCount = hull.NumIndices;
            UIntArray    indices     = hull.Indices;
            Vector3Array points      = hull.Vertices;

            var vertices = new Vector3[vertexCount * 2];

            int v = 0;

            for (int i = 0; i < vertexCount; i += 3)
            {
                Vector3 v0 = points[(int)indices[i]];
                Vector3 v1 = points[(int)indices[i + 1]];
                Vector3 v2 = points[(int)indices[i + 2]];

                Vector3 v01    = v0 - v1;
                Vector3 v02    = v0 - v2;
                Vector3 normal = Vector3.Cross(v01, v02);
                normal.Normalize();

                vertices[v++] = v0;
                vertices[v++] = normal;
                vertices[v++] = v1;
                vertices[v++] = normal;
                vertices[v++] = v2;
                vertices[v++] = normal;
            }

            return(vertices);
        }
        public static void CreateConvexHull(ConvexHullShape shape, Mesh mesh)
        {
            ShapeHull hull = new ShapeHull(shape);

            hull.BuildHull(shape.Margin);

            List <UnityEngine.Vector3> verts = new List <UnityEngine.Vector3>();
            List <int> tris = new List <int>();

            //int vertexCount = hull.NumVertices;
            UIntArray    indices = hull.Indices;
            Vector3Array points  = hull.Vertices;

            for (int i = 0; i < indices.Count; i += 3)
            {
                verts.Add(points[(int)indices[i]].ToUnity());
                verts.Add(points[(int)indices[i + 1]].ToUnity());
                verts.Add(points[(int)indices[i + 2]].ToUnity());
                tris.Add(i);
                tris.Add(i + 1);
                tris.Add(i + 2);
            }

            mesh.vertices  = verts.ToArray();
            mesh.triangles = tris.ToArray();
            mesh.RecalculateBounds();
            mesh.RecalculateNormals();
        }
Esempio n. 5
0
        Mesh CreateConvexHullShape(ConvexHullShape shape)
        {
            ShapeHull hull = new ShapeHull(shape);

            hull.BuildHull(shape.Margin);

            UIntArray    hullIndices = hull.Indices;
            Vector3Array points      = hull.Vertices;


            int vertexCount = hull.NumIndices;
            int faceCount   = hull.NumTriangles;

            bool index32 = vertexCount > 65536;

            Mesh mesh = new Mesh(device, faceCount, vertexCount,
                                 MeshFlags.SystemMemory | (index32 ? MeshFlags.Use32Bit : 0), VertexFormat.Position | VertexFormat.Normal);


            SlimDX.DataStream indices = mesh.LockIndexBuffer(LockFlags.Discard);
            int i;

            if (index32)
            {
                for (i = 0; i < vertexCount; i++)
                {
                    indices.Write(i);
                }
            }
            else
            {
                for (i = 0; i < vertexCount; i++)
                {
                    indices.Write((short)i);
                }
            }
            mesh.UnlockIndexBuffer();

            SlimDX.DataStream verts = mesh.LockVertexBuffer(LockFlags.Discard);
            Vector3           scale = Vector3.Multiply(shape.LocalScaling, 1.0f + shape.Margin);

            for (i = 0; i < vertexCount; i += 3)
            {
                verts.Write(Vector3.Modulate(points[(int)hullIndices[i]], scale));
                verts.Position += 12;
                verts.Write(Vector3.Modulate(points[(int)hullIndices[i + 1]], scale));
                verts.Position += 12;
                verts.Write(Vector3.Modulate(points[(int)hullIndices[i + 2]], scale));
                verts.Position += 12;
            }
            mesh.UnlockVertexBuffer();

            mesh.ComputeNormals();
            shapes.Add(shape, mesh);

            return(mesh);
        }
        private static Vector3[] CreateBox2DShape(Box2DShape box2DShape)
        {
            Vector3Array v = box2DShape.Vertices;

            return(new Vector3[12]
            {
                v[0], Vector3.UnitZ,
                v[1], Vector3.UnitZ,
                v[2], Vector3.UnitZ,
                v[0], Vector3.UnitZ,
                v[2], Vector3.UnitZ,
                v[3], Vector3.UnitZ,
            });
        }
Esempio n. 7
0
        private static UnityEngine.Vector3[] CreateBox2DShape(Box2DShape box2DShape)
        {
            Vector3Array v = box2DShape.Vertices;

            return(new UnityEngine.Vector3[12]
            {
                v[0].ToUnity(), UnityEngine.Vector3.forward,
                v[1].ToUnity(), UnityEngine.Vector3.forward,
                v[2].ToUnity(), UnityEngine.Vector3.forward,
                v[0].ToUnity(), UnityEngine.Vector3.forward,
                v[2].ToUnity(), UnityEngine.Vector3.forward,
                v[3].ToUnity(), UnityEngine.Vector3.forward,
            });
        }
Esempio n. 8
0
        ShapeData CreateConvexHullShape(ConvexHullShape shape)
        {
            ConvexPolyhedron poly = shape.ConvexPolyhedron;

            if (poly != null)
            {
                throw new NotImplementedException();
            }

            ShapeHull hull = new ShapeHull(shape);

            hull.BuildHull(shape.Margin);

            int          indexCount = hull.NumIndices;
            UIntArray    indices    = hull.Indices;
            Vector3Array points     = hull.Vertices;

            ShapeData shapeData = new ShapeData();

            shapeData.VertexCount = indexCount;

            Vector3[] vertices = new Vector3[indexCount * 2];

            int v = 0, i;

            for (i = 0; i < indexCount; i += 3)
            {
                Vector3 v0 = points[(int)indices[i]];
                Vector3 v1 = points[(int)indices[i + 1]];
                Vector3 v2 = points[(int)indices[i + 2]];

                Vector3 v01    = v0 - v1;
                Vector3 v02    = v0 - v2;
                Vector3 normal = Vector3.Cross(v01, v02);
                normal.Normalize();

                vertices[v++] = v0;
                vertices[v++] = normal;
                vertices[v++] = v1;
                vertices[v++] = normal;
                vertices[v++] = v2;
                vertices[v++] = normal;
            }

            shapeData.SetVertexBuffer(device, vertices);

            return(shapeData);
        }
Esempio n. 9
0
        public static (Vector3, Vector3) GetBounds(Vector3Array accesor)
        {
            var min = new Vector3(Single.MaxValue);
            var max = new Vector3(Single.MinValue);

            int c = accesor.Count;

            for (int i = 0; i < c; ++i)
            {
                var v = accesor[i];
                min = Vector3.Min(min, v);
                max = Vector3.Max(max, v);
            }

            return(min, max);
        }
Esempio n. 10
0
        public static MeshPrimitive WithVertexAccessor(this MeshPrimitive primitive, string attribute, IReadOnlyList <Vector3> values)
        {
            var root = primitive.LogicalParent.LogicalParent;

            // create a vertex buffer and fill it
            var view  = root.UseBufferView(new Byte[12 * values.Count], 0, null, 0, BufferMode.ARRAY_BUFFER);
            var array = new Vector3Array(view.Content);

            array.FillFrom(0, values);

            var accessor = root.CreateAccessor();

            accessor.SetVertexData(view, 0, values.Count, DimensionType.VEC3, EncodingType.FLOAT, false);

            primitive.SetVertexAccessor(attribute, accessor);

            return(primitive);
        }
        protected override AbstractSoftShapeDefinition GetShapeDefinition(int slice)
        {
            Vector3Array p = new Vector3Array(FPosition.SliceCount);
            ScalarArray  m = new ScalarArray(FPosition.SliceCount);

            for (int i = 0; i < FPosition.SliceCount; i++)
            {
                p[i] = FPosition[i].ToBulletVector();
            }

            for (int i = 0; i < FPosition.SliceCount; i++)
            {
                m[i] = FMass[i];
            }

            int[] indices = FIndices.ToArray();

            return(new GenericSoftShapeDefinition(p, indices, m));
        }
Esempio n. 12
0
        SoftBody CreateFromIndexedMesh(Vector3Array vertexArray, IntArray triangleVertexIndexArray, bool createBendLinks)
        {
            SoftBody softBody           = new SoftBody(SoftWorld.WorldInfo, vertexArray, null);
            Material structuralMaterial = softBody.AppendMaterial();
            Material bendMaterial;

            if (createBendLinks)
            {
                bendMaterial     = softBody.AppendMaterial();
                bendMaterial.Lst = 0.7f;
            }
            else
            {
                bendMaterial = null;
            }
            structuralMaterial.Lst = 1.0f;


            int numVertices  = vertexArray.Count;
            int numTriangles = triangleVertexIndexArray.Count / 3;

            // List of values for each link saying which triangle is associated with that link
            // -1 to start. Once a value is entered we know the "other" triangle
            // and can add a link across the link
            AlignedIntArray triangleForLinks = new AlignedIntArray();

            triangleForLinks.Resize(numVertices * numVertices, -1);

            for (int triangle = 0; triangle < numTriangles; ++triangle)
            {
                int[] index = new int[] { triangleVertexIndexArray[triangle * 3], triangleVertexIndexArray[triangle * 3 + 1], triangleVertexIndexArray[triangle * 3 + 2] };
                softBody.AppendFace(index[0], index[1], index[2]);

                // Generate the structural links directly from the triangles
                TestAndAddLink(triangleForLinks, softBody, triangle, triangleVertexIndexArray, numVertices, index[0], index[1], index[2], structuralMaterial, createBendLinks, bendMaterial);
                TestAndAddLink(triangleForLinks, softBody, triangle, triangleVertexIndexArray, numVertices, index[1], index[2], index[0], structuralMaterial, createBendLinks, bendMaterial);
                TestAndAddLink(triangleForLinks, softBody, triangle, triangleVertexIndexArray, numVertices, index[2], index[0], index[1], structuralMaterial, createBendLinks, bendMaterial);
            }

            return(softBody);
        }
Esempio n. 13
0
        public static void CreateBox2DShape(Box2DShape shape, Mesh mesh)
        {
            Vector3Array v = shape.Vertices;

            MakeUnityCubeMesh(v[0].ToUnity(), v[1].ToUnity(), v[2].ToUnity(), v[3].ToUnity(), v[4].ToUnity(), v[5].ToUnity(), v[6].ToUnity(), v[7].ToUnity(), mesh);
        }
        public void SetPoints(Vector3Array points, int numPoints, bool computeAabb)
		{
			btConvexPointCloudShape_setPoints2(_native, points._native, numPoints,
				computeAabb);
            _unscaledPoints = points;
		}
Esempio n. 15
0
        // Create a sequence of flag objects and add them to the world.
        void CreateFlag(int width, int height, out AlignedSoftBodyArray flags)
        {
            flags = new AlignedSoftBodyArray();

            // First create a triangle mesh to represent a flag

            // Allocate a simple mesh consisting of a vertex array and a triangle index array
            IndexedMesh mesh = new IndexedMesh();
            mesh.NumVertices = width * height;
            mesh.NumTriangles = 2 * (width - 1) * (height - 1);

            Vector3Array vertexArray = new Vector3Array(mesh.NumVertices);
            mesh.Vertices = vertexArray;
            mesh.VertexStride = Vector3.SizeInBytes;

            IntArray triangleVertexIndexArray = new IntArray(3 * mesh.NumTriangles);
            mesh.TriangleIndices = triangleVertexIndexArray;
            mesh.TriangleIndexStride = sizeof(int) * 3;

            // Generate normalised object space vertex coordinates for a rectangular flag

            Matrix defaultScale = Matrix.Scaling(5, 20, 1);
            for (int y = 0; y < height; ++y)
            {
                float yCoordinate = y * 2.0f / (float)height - 1.0f;
                for (int x = 0; x < width; ++x)
                {
                    float xCoordinate = x * 2.0f / (float)width - 1.0f;

                    Vector3 vertex = new Vector3(xCoordinate, yCoordinate, 0.0f);
                    vertexArray[y * width + x] = Vector3.TransformCoordinate(vertex, defaultScale);
                }
            }

            // Generate vertex indices for triangles
            for (int y = 0; y < (height - 1); ++y)
            {
                for (int x = 0; x < (width - 1); ++x)
                {
                    // Triangle 0
                    // Top left of square on mesh
                    {
                        int vertex0 = y * width + x;
                        int vertex1 = vertex0 + 1;
                        int vertex2 = vertex0 + width;
                        int triangleIndex = 2 * (y * (width - 1) + x);
                        triangleVertexIndexArray[(mesh.TriangleIndexStride * triangleIndex) / sizeof(int)] = vertex0;
                        triangleVertexIndexArray[(mesh.TriangleIndexStride * triangleIndex + 1) / sizeof(int) + 1] = vertex1;
                        triangleVertexIndexArray[(mesh.TriangleIndexStride * triangleIndex + 2) / sizeof(int) + 2] = vertex2;
                    }

                    // Triangle 1
                    // Bottom right of square on mesh
                    {
                        int vertex0 = y * width + x + 1;
                        int vertex1 = vertex0 + width;
                        int vertex2 = vertex1 - 1;
                        int triangleIndex = 2 * y * (width - 1) + 2 * x + 1;
                        triangleVertexIndexArray[(mesh.TriangleIndexStride * triangleIndex) / sizeof(int)] = vertex0;
                        triangleVertexIndexArray[(mesh.TriangleIndexStride * triangleIndex) / sizeof(int) + 1] = vertex1;
                        triangleVertexIndexArray[(mesh.TriangleIndexStride * triangleIndex) / sizeof(int) + 2] = vertex2;
                    }
                }
            }

            Matrix defaultRotateAndScale = Matrix.RotationX(0.5f);

            // Construct the sequence flags applying a slightly different translation to each one to arrange them
            // appropriately in the scene.
            for (int i = 0; i < numFlags; ++i)
            {
                float zTranslate = flagSpacing * (i - numFlags / 2);

                Vector3 defaultTranslate = new Vector3(0, 20, zTranslate);
                Matrix transform = defaultRotateAndScale * Matrix.Translation(defaultTranslate);

                SoftBody softBody = CreateFromIndexedMesh(vertexArray, triangleVertexIndexArray, true);

                for (int j = 0; j < mesh.NumVertices; ++j)
                {
                    softBody.SetMass(j, 10.0f / mesh.NumVertices);
                }
                softBody.SetMass((height - 1) * width, 0);
                softBody.SetMass((height - 1) * width + width - 1, 0);
                softBody.SetMass((height - 1) * width + width / 2, 0);
                softBody.Cfg.Collisions = FCollisions.CLSS | FCollisions.CLRS;

                softBody.Cfg.LF = 0.0005f;
                softBody.Cfg.VCF = 0.001f;
                softBody.Cfg.DP = 0.0f;
                softBody.Cfg.DG = 0.0f;

                flags.Add(softBody);

                softBody.Transform(transform);

                SoftWorld.AddSoftBody(softBody);
            }

            //delete [] vertexArray;
            //delete [] triangleVertexIndexArray;
        }
Esempio n. 16
0
 public GenericSoftShapeDefinition(Vector3Array vertices, int[] indices, ScalarArray mass)
 {
     this.vertices = vertices;
     this.indices  = indices;
     this.mass     = mass;
 }
Esempio n. 17
0
        SoftBody CreateFromIndexedMesh(Vector3Array vertexArray, IntArray triangleVertexIndexArray, bool createBendLinks)
        {
            SoftBody softBody = new SoftBody(SoftWorld.WorldInfo, vertexArray, null);
            Material structuralMaterial = softBody.AppendMaterial();
            Material bendMaterial;
            if (createBendLinks)
            {
                bendMaterial = softBody.AppendMaterial();
                bendMaterial.Lst = 0.7f;
            }
            else
            {
                bendMaterial = null;
            }
            structuralMaterial.Lst = 1.0f;

            int numVertices = vertexArray.Count;
            int numTriangles = triangleVertexIndexArray.Count / 3;

            // List of values for each link saying which triangle is associated with that link
            // -1 to start. Once a value is entered we know the "other" triangle
            // and can add a link across the link
            AlignedIntArray triangleForLinks = new AlignedIntArray();
            triangleForLinks.Resize(numVertices * numVertices, -1);

            for (int triangle = 0; triangle < numTriangles; ++triangle)
            {
                int[] index = new int[] { triangleVertexIndexArray[triangle * 3], triangleVertexIndexArray[triangle * 3 + 1], triangleVertexIndexArray[triangle * 3 + 2] };
                softBody.AppendFace(index[0], index[1], index[2]);

                // Generate the structural links directly from the triangles
                TestAndAddLink(triangleForLinks, softBody, triangle, triangleVertexIndexArray, numVertices, index[0], index[1], index[2], structuralMaterial, createBendLinks, bendMaterial);
                TestAndAddLink(triangleForLinks, softBody, triangle, triangleVertexIndexArray, numVertices, index[1], index[2], index[0], structuralMaterial, createBendLinks, bendMaterial);
                TestAndAddLink(triangleForLinks, softBody, triangle, triangleVertexIndexArray, numVertices, index[2], index[0], index[1], structuralMaterial, createBendLinks, bendMaterial);
            }

            return softBody;
        }
Esempio n. 18
0
 public _MapVector3ToVector4(Vector3Array source)
 {
     _Accessor = source;
 }
		public MultiSphereShape(Vector3Array positions, float[] radi)
			: base(btMultiSphereShape_new2(positions._native, radi, (radi.Length < positions.Count) ? radi.Length : positions.Count))
		{
		}
        public ConvexPointCloudShape(Vector3Array points, int numPoints, Vector3 localScaling)
			: base(btConvexPointCloudShape_new2(points._native, numPoints, ref localScaling))
		{
            _unscaledPoints = points;
		}
        public void SetPoints(Vector3Array points, int numPoints)
		{
			btConvexPointCloudShape_setPoints(_native, points._native, numPoints);
            _unscaledPoints = points;
		}
Esempio n. 22
0
        // Create a sequence of flag objects and add them to the world.
        void CreateFlag(int width, int height, out AlignedSoftBodyArray flags)
        {
            flags = new AlignedSoftBodyArray();

            // First create a triangle mesh to represent a flag

            // Allocate a simple mesh consisting of a vertex array and a triangle index array
            IndexedMesh mesh = new IndexedMesh();

            mesh.NumVertices  = width * height;
            mesh.NumTriangles = 2 * (width - 1) * (height - 1);

            Vector3Array vertexArray = new Vector3Array(mesh.NumVertices);

            mesh.Vertices     = vertexArray;
            mesh.VertexStride = Vector3.SizeInBytes;

            IntArray triangleVertexIndexArray = new IntArray(3 * mesh.NumTriangles);

            mesh.TriangleIndices     = triangleVertexIndexArray;
            mesh.TriangleIndexStride = sizeof(int) * 3;


            // Generate normalised object space vertex coordinates for a rectangular flag

            Matrix defaultScale = Matrix.Scaling(5, 20, 1);

            for (int y = 0; y < height; ++y)
            {
                float yCoordinate = y * 2.0f / (float)height - 1.0f;
                for (int x = 0; x < width; ++x)
                {
                    float xCoordinate = x * 2.0f / (float)width - 1.0f;

                    Vector3 vertex = new Vector3(xCoordinate, yCoordinate, 0.0f);
                    vertexArray[y * width + x] = Vector3.TransformCoordinate(vertex, defaultScale);
                }
            }

            // Generate vertex indices for triangles
            for (int y = 0; y < (height - 1); ++y)
            {
                for (int x = 0; x < (width - 1); ++x)
                {
                    // Triangle 0
                    // Top left of square on mesh
                    {
                        int vertex0       = y * width + x;
                        int vertex1       = vertex0 + 1;
                        int vertex2       = vertex0 + width;
                        int triangleIndex = 2 * (y * (width - 1) + x);
                        triangleVertexIndexArray[(mesh.TriangleIndexStride * triangleIndex) / sizeof(int)]         = vertex0;
                        triangleVertexIndexArray[(mesh.TriangleIndexStride * triangleIndex + 1) / sizeof(int) + 1] = vertex1;
                        triangleVertexIndexArray[(mesh.TriangleIndexStride * triangleIndex + 2) / sizeof(int) + 2] = vertex2;
                    }

                    // Triangle 1
                    // Bottom right of square on mesh
                    {
                        int vertex0       = y * width + x + 1;
                        int vertex1       = vertex0 + width;
                        int vertex2       = vertex1 - 1;
                        int triangleIndex = 2 * y * (width - 1) + 2 * x + 1;
                        triangleVertexIndexArray[(mesh.TriangleIndexStride * triangleIndex) / sizeof(int)]     = vertex0;
                        triangleVertexIndexArray[(mesh.TriangleIndexStride * triangleIndex) / sizeof(int) + 1] = vertex1;
                        triangleVertexIndexArray[(mesh.TriangleIndexStride * triangleIndex) / sizeof(int) + 2] = vertex2;
                    }
                }
            }


            Matrix defaultRotateAndScale = Matrix.RotationX(0.5f);

            // Construct the sequence flags applying a slightly different translation to each one to arrange them
            // appropriately in the scene.
            for (int i = 0; i < numFlags; ++i)
            {
                float zTranslate = flagSpacing * (i - numFlags / 2);

                Vector3 defaultTranslate = new Vector3(0, 20, zTranslate);
                Matrix  transform        = defaultRotateAndScale * Matrix.Translation(defaultTranslate);


                SoftBody softBody = CreateFromIndexedMesh(vertexArray, triangleVertexIndexArray, true);


                for (int j = 0; j < mesh.NumVertices; ++j)
                {
                    softBody.SetMass(j, 10.0f / mesh.NumVertices);
                }
                softBody.SetMass((height - 1) * width, 0);
                softBody.SetMass((height - 1) * width + width - 1, 0);
                softBody.SetMass((height - 1) * width + width / 2, 0);
                softBody.Cfg.Collisions = FCollisions.CLSS | FCollisions.CLRS;

                softBody.Cfg.LF  = 0.0005f;
                softBody.Cfg.VCF = 0.001f;
                softBody.Cfg.DP  = 0.0f;
                softBody.Cfg.DG  = 0.0f;


                flags.Add(softBody);

                softBody.Transform(transform);

                SoftWorld.AddSoftBody(softBody);
            }

            //delete [] vertexArray;
            //delete [] triangleVertexIndexArray;
        }
        public void SetPoints(Vector3Array points, int numPoints, bool computeAabb, Vector3 localScaling)
		{
			btConvexPointCloudShape_setPoints3(_native, points._native, numPoints,
				computeAabb, ref localScaling);
            _unscaledPoints = points;
		}