public static void CreateRenderMeshForCollider( EntityManager entityManager, Entity entity, BlobAssetReference <Collider> collider, Material material ) { var mesh = SceneCreationUtilities.CreateMeshFromCollider(collider); entityManager.AddSharedComponentData(entity, new RenderMesh { mesh = mesh, material = material }); entityManager.AddComponentData(entity, new RenderBounds { Value = mesh.bounds.ToAABB() }); }
private BlobAssetReference <Collider> CreateCollider(ColliderType type) { int numMeshes = type == ColliderType.Compound ? 2 : 1; ColliderMeshes = new Mesh[numMeshes]; BlobAssetReference <Collider> collider = default; switch (type) { case ColliderType.Sphere: collider = SphereCollider.Create(new SphereGeometry { Center = float3.zero, Radius = 0.5f }); break; case ColliderType.Triangle: collider = PolygonCollider.CreateTriangle(k_TriangleVertices[0], k_TriangleVertices[1], k_TriangleVertices[2]); break; case ColliderType.Quad: collider = PolygonCollider.CreateQuad(k_QuadVertices[0], k_QuadVertices[1], k_QuadVertices[2], k_QuadVertices[3]); break; case ColliderType.Box: collider = BoxCollider.Create(new BoxGeometry { Center = float3.zero, Orientation = quaternion.identity, Size = new float3(1.0f), BevelRadius = 0.0f }); break; case ColliderType.Capsule: collider = CapsuleCollider.Create(new CapsuleGeometry { Vertex0 = new float3(0, -0.5f, 0), Vertex1 = new float3(0, 0.5f, 0), Radius = 0.5f }); break; case ColliderType.Cylinder: // TODO: need someone to add throw new NotImplementedException(); case ColliderType.Convex: // Tetrahedron NativeArray <float3> points = new NativeArray <float3>(k_TetraherdonVertices, Allocator.TempJob); collider = ConvexCollider.Create(points, ConvexHullGenerationParameters.Default, CollisionFilter.Default); points.Dispose(); break; case ColliderType.Compound: var child1 = SphereCollider.Create(new SphereGeometry { Center = float3.zero, Radius = 0.5f }); ChildrenColliders.Add(child1); var child2 = BoxCollider.Create(new BoxGeometry { Center = float3.zero, Orientation = quaternion.identity, Size = new float3(1.0f), BevelRadius = 0.0f }); ChildrenColliders.Add(child2); NativeArray <ColliderBlobInstance> childrenBlobs = new NativeArray <ColliderBlobInstance>(2, Allocator.TempJob); childrenBlobs[0] = new ColliderBlobInstance { Collider = child1, CompoundFromChild = new RigidTransform { pos = new float3(0.5f, 0, 0), rot = quaternion.identity } }; childrenBlobs[1] = new ColliderBlobInstance { Collider = child2, CompoundFromChild = new RigidTransform { pos = new float3(-0.5f, 0, 0), rot = quaternion.identity } }; ColliderMeshes[0] = SceneCreationUtilities.CreateMeshFromCollider(child1); ColliderMeshes[1] = SceneCreationUtilities.CreateMeshFromCollider(child2); collider = CompoundCollider.Create(childrenBlobs); childrenBlobs.Dispose(); break; case ColliderType.Mesh: // Tetrahedron mesh NativeArray <float3> meshVertices = new NativeArray <float3>(k_TetraherdonVertices, Allocator.TempJob); NativeArray <int3> meshTriangles = new NativeArray <int3>(k_TetrahedronMeshTriangles, Allocator.TempJob); collider = MeshCollider.Create(meshVertices, meshTriangles); meshVertices.Dispose(); meshTriangles.Dispose(); break; case ColliderType.Terrain: int2 size = 2; float3 scale = 1; Random rand = new Random(0x9739); int numSamples = size.x * size.y; var heights = new NativeArray <float>(numSamples, Allocator.TempJob, NativeArrayOptions.UninitializedMemory); for (int i = 0; i < numSamples; i++) { heights[i] = rand.NextFloat(0, 1); } collider = TerrainCollider.Create(heights, size, scale, TerrainCollider.CollisionMethod.VertexSamples); heights.Dispose(); break; default: throw new System.NotImplementedException(); } if (ColliderType != ColliderType.Compound) { ColliderMeshes[0] = SceneCreationUtilities.CreateMeshFromCollider(collider); } return(collider); }