void BuildFromMesh() { if (m_boundaryMesh) { Vector3[] vertices = m_boundaryMesh.vertices; if (vertices != null && vertices.Length > 0) { for (int i = 0; i < vertices.Length; ++i) { Vector3 v = vertices[i]; vertices[i] = new Vector3(v.x * m_meshLocalScale.x, v.y * m_meshLocalScale.y, v.z * m_meshLocalScale.z); } int[] indices = m_boundaryMesh.triangles; if (indices != null && indices.Length > 0) { FlexExt.Asset.Handle assetHandle = FlexExt.CreateRigidFromMesh(ref vertices[0], vertices.Length, ref indices[0], indices.Length, m_particleSpacing, m_meshExpansion); if (assetHandle) { FlexExt.Asset asset = assetHandle.asset; FlexExt.Asset particlesOnly = new FlexExt.Asset(); particlesOnly.numParticles = asset.numParticles; particlesOnly.maxParticles = asset.numParticles; particlesOnly.particles = asset.particles; StoreAsset(particlesOnly); FlexExt.DestroyAsset(assetHandle); } } } } }
protected void StoreAsset(FlexExt.Asset _asset) { m_particles = new Vector4[_asset.numParticles]; if (_asset.numParticles > 0) { FlexUtils.FastCopy(_asset.particles, m_particles); } m_maxParticles = _asset.maxParticles; m_springIndices = new int[_asset.numSprings * 2]; if (_asset.numSprings > 0) { FlexUtils.FastCopy(_asset.springIndices, m_springIndices); } m_springCoefficients = new float[_asset.numSprings]; if (_asset.numSprings > 0) { FlexUtils.FastCopy(_asset.springCoefficients, m_springCoefficients); } m_springRestLengths = new float[_asset.numSprings]; if (_asset.numSprings > 0) { FlexUtils.FastCopy(_asset.springRestLengths, m_springRestLengths); } m_shapeIndices = new int[_asset.numShapeIndices]; if (_asset.numShapeIndices > 0) { FlexUtils.FastCopy(_asset.shapeIndices, m_shapeIndices); } m_shapeOffsets = new int[_asset.numShapes]; if (_asset.numShapes > 0) { FlexUtils.FastCopy(_asset.shapeOffsets, m_shapeOffsets); } m_shapeCoefficients = new float[_asset.numShapes]; if (_asset.numShapes > 0) { FlexUtils.FastCopy(_asset.shapeCoefficients, m_shapeCoefficients); } m_shapeCenters = new Vector3[_asset.numShapes]; if (_asset.numShapes > 0) { FlexUtils.FastCopy(_asset.shapeCenters, m_shapeCenters); } m_triangleIndices = new int[_asset.numTriangles * 3]; if (_asset.numTriangles > 0) { FlexUtils.FastCopy(_asset.triangleIndices, m_triangleIndices); } m_inflatable = _asset.inflatable; m_inflatableVolume = _asset.inflatableVolume; m_inflatablePressure = _asset.inflatablePressure; m_inflatableStiffness = _asset.inflatableStiffness; }
public void TestCreateRigidFromMesh() { Vector3[] vertices = { new Vector3(1, 1, 1), new Vector3(-1, -1, 1), new Vector3(-1, 1, -1), new Vector3(1, -1, -1) }; int[] indices = { 0, 1, 2, 0, 3, 1, 0, 2, 3, 1, 3, 2 }; float radius = 0.1f; float expand = 0.0f; FlexExt.Asset.Handle handle = FlexExt.CreateRigidFromMesh(ref vertices[0], vertices.Length, ref indices[0], indices.Length, radius, expand); FlexExt.Asset asset = handle.asset; Assert.AreEqual(2680, asset.numParticles); Assert.AreEqual(1, asset.numShapes); FlexExt.DestroyAsset(handle); }
public void TestCreateTearingClothFromMesh() { Vector4[] particles = { new Vector4(1, 1, 0, 1), new Vector4(0, 1, 0, 1), new Vector4(-1, 1, 0, 1), new Vector4(1, 0, 0, 1), new Vector4(0, 0, 0, 1), new Vector4(-1, 0, 0, 1), new Vector4(1, -1, 0, 1), new Vector4(0, -1, 0, 1), new Vector4(-1, -1, 0, 1) }; int[] indices = { 0, 1, 4, 0, 4, 3, 1, 2, 5, 1, 5, 4, 3, 4, 7, 3, 7, 6, 4, 5, 8, 4, 8, 7 }; float stretchStiffness = 1.0f; float bendStiffness = 1.0f; float pressure = 0.0f; FlexExt.Asset.Handle handle = FlexExt.CreateTearingClothFromMesh(ref particles[0], particles.Length, particles.Length * 2, ref indices[0], indices.Length / 3, stretchStiffness, bendStiffness, pressure); FlexExt.Asset asset = handle.asset; Assert.AreEqual(particles.Length, asset.numParticles); Assert.AreEqual(indices.Length / 3, asset.numTriangles); FlexExt.DestroyTearingCloth(handle); }
public void TestCreateSoftFromMesh() { Vector3[] vertices = { new Vector3(1, 1, 1), new Vector3(-1, -1, 1), new Vector3(-1, 1, -1), new Vector3(1, -1, -1) }; int[] indices = { 0, 1, 2, 0, 3, 1, 0, 2, 3, 1, 3, 2 }; float particleSpacing = 0.1f; float volumeSampling = 0.1f; float surfaceSampling = 0.1f; float clusterSpacing = 0.1f; float clusterRadius = 0.3f; float clusterStiffness = 0.5f; float linkRadius = 0.2f; float linkStiffness = 0.5f; FlexExt.Asset.Handle handle = FlexExt.CreateSoftFromMesh(ref vertices[0], vertices.Length, ref indices[0], indices.Length, particleSpacing, volumeSampling, surfaceSampling, clusterSpacing, clusterRadius, clusterStiffness, linkRadius, linkStiffness); FlexExt.Asset asset = handle.asset; Assert.AreEqual(668, asset.numParticles); Assert.AreEqual(502, asset.numShapes); Assert.AreEqual(3856, asset.numSprings); FlexExt.DestroyAsset(handle); }
void BuildFromMesh() { FlexExt.Asset reserveSpace = new FlexExt.Asset(); reserveSpace.maxParticles = maxParticles; StoreAsset(reserveSpace); if (m_surfaceMesh) { Vector3[] vertices = m_surfaceMesh.vertices; int[] triangles = m_surfaceMesh.triangles; if (vertices != null && vertices.Length > 0 && triangles != null && triangles.Length > 0) { List <Vector3> surface = new List <Vector3>(); for (int i = 0; i < triangles.Length / 3; ++i) { surface.Add(Scale(vertices[triangles[i * 3 + 0]], m_meshLocalScale)); surface.Add(Scale(vertices[triangles[i * 3 + 1]], m_meshLocalScale)); surface.Add(Scale(vertices[triangles[i * 3 + 2]], m_meshLocalScale)); } for (int i = 0; i < m_meshTesselation; ++i) { List <Vector3> newSurface = new List <Vector3>(); for (int j = 0; j < surface.Count / 3; ++j) { Vector3 v0 = surface[j * 3 + 0], v1 = surface[j * 3 + 1], v2 = surface[j * 3 + 2]; Vector3 v3 = (v0 + v1) * 0.5f, v4 = (v1 + v2) * 0.5f, v5 = (v2 + v0) * 0.5f; newSurface.Add(v0); newSurface.Add(v3); newSurface.Add(v5); newSurface.Add(v3); newSurface.Add(v1); newSurface.Add(v4); newSurface.Add(v5); newSurface.Add(v4); newSurface.Add(v2); newSurface.Add(v3); newSurface.Add(v4); newSurface.Add(v5); } surface = newSurface; } List <Vector3> pointList = new List <Vector3>(); List <Vector3> normalList = new List <Vector3>(); for (int i = 0; i < surface.Count / 3; ++i) { Vector3[] v = new Vector3[] { surface[i * 3 + 0], surface[i * 3 + 1], surface[i * 3 + 2] }; Vector3 n = Vector3.Cross(v[1] - v[0], v[2] - v[0]); for (int j = 0; j < 3; ++j) { bool newPoint = true; for (int k = 0; k < pointList.Count; ++k) { if ((v[j] - pointList[k]).sqrMagnitude <= float.Epsilon) { normalList[k] += n; newPoint = false; break; } } if (newPoint) { pointList.Add(v[j]); normalList.Add(n); } } } for (int i = 0; i < normalList.Count; ++i) { normalList[i] = normalList[i].normalized; } m_nozzlePositions = pointList.ToArray(); m_nozzleDirections = normalList.ToArray(); } } }
public static extern void FastCopy(IntPtr src, int srcOfs, ref FlexExt.Asset dst, int dstOfs, int size);
public static void FastCopy(IntPtr source, ref FlexExt.Asset destination) { FastCopy(source, 0, ref destination, 0, assetSize); }
public static void FastCopy(ref FlexExt.Asset source, IntPtr destination) { FastCopy(ref source, 0, destination, 0, assetSize); }