Browse Source

Added GetMaterialIndex and GetMaterialList functions (#117)

This can be used to create a decorator shape that overrides materials of a mesh
Jorrit Rouwe 3 years ago
parent
commit
8981a34518

+ 11 - 6
Jolt/Physics/Collision/Shape/MeshShape.cpp

@@ -332,12 +332,8 @@ void MeshShape::DecodeSubShapeID(const SubShapeID &inSubShapeID, const void *&ou
 	JPH_ASSERT(remainder.IsEmpty(), "Invalid subshape ID");
 }
 
-const PhysicsMaterial *MeshShape::GetMaterial(const SubShapeID &inSubShapeID) const
+uint MeshShape::GetMaterialIndex(const SubShapeID &inSubShapeID) const
 {
-	// Return the default material if there are no materials on this shape
-	if (mMaterials.empty())
-		return PhysicsMaterial::sDefault;
-
 	// Decode ID
 	const void *block_start;
 	uint32 triangle_idx;
@@ -345,7 +341,16 @@ const PhysicsMaterial *MeshShape::GetMaterial(const SubShapeID &inSubShapeID) co
 		
 	// Fetch the flags
 	uint8 flags = TriangleCodec::DecodingContext::sGetFlags(block_start, triangle_idx);
-	return mMaterials[flags & FLAGS_MATERIAL_MASK];
+	return flags & FLAGS_MATERIAL_MASK;
+}
+
+const PhysicsMaterial *MeshShape::GetMaterial(const SubShapeID &inSubShapeID) const
+{
+	// Return the default material if there are no materials on this shape
+	if (mMaterials.empty())
+		return PhysicsMaterial::sDefault;
+
+	return mMaterials[GetMaterialIndex(inSubShapeID)];
 }
 
 Vec3 MeshShape::GetSurfaceNormal(const SubShapeID &inSubShapeID, Vec3Arg inLocalSurfacePosition) const 

+ 7 - 0
Jolt/Physics/Collision/Shape/MeshShape.h

@@ -74,6 +74,13 @@ public:
 	// See Shape::GetMaterial
 	virtual const PhysicsMaterial *	GetMaterial(const SubShapeID &inSubShapeID) const override;
 
+	/// Get the list of all materials
+	const PhysicsMaterialList &		GetMaterialList() const										{ return mMaterials; }
+
+	/// Determine which material index a particular sub shape uses (note that if there are no materials this function will return 0 so check the array size)
+	/// Note: This could for example be used to create a decorator shape around a mesh shape that overrides the GetMaterial call to replace a material with another material.
+	uint							GetMaterialIndex(const SubShapeID &inSubShapeID) const;
+
 	// See Shape::GetSurfaceNormal
 	virtual Vec3					GetSurfaceNormal(const SubShapeID &inSubShapeID, Vec3Arg inLocalSurfacePosition) const override;