Browse Source

Editor: Add body and joint UIs

Panagiotis Christopoulos Charitos 2 weeks ago
parent
commit
9343e99d7c

+ 97 - 0
AnKi/Editor/EditorUi.cpp

@@ -774,6 +774,12 @@ void EditorUi::sceneNodePropertiesWindow()
 					case SceneComponentType::kLight:
 						lightComponent(static_cast<LightComponent&>(comp));
 						break;
+					case SceneComponentType::kBody:
+						bodyComponent(static_cast<BodyComponent&>(comp));
+						break;
+					case SceneComponentType::kJoint:
+						jointComponent(static_cast<JointComponent&>(comp));
+						break;
 					default:
 						ImGui::Text("TODO");
 					}
@@ -957,6 +963,13 @@ void EditorUi::meshComponent(MeshComponent& comp)
 
 void EditorUi::skinComponent(SkinComponent& comp)
 {
+	if(!comp.isValid())
+	{
+		ImGui::SameLine();
+		ImGui::TextUnformatted(ICON_MDI_ALERT);
+		ImGui::SetItemTooltip("Component not valid");
+	}
+
 	// Locate button
 	{
 		ImGui::BeginDisabled(!comp.hasSkeletonResource());
@@ -1149,6 +1162,90 @@ void EditorUi::lightComponent(LightComponent& comp)
 	}
 }
 
+void EditorUi::jointComponent(JointComponent& comp)
+{
+	if(!comp.isValid())
+	{
+		ImGui::SameLine();
+		ImGui::TextUnformatted(ICON_MDI_ALERT);
+		ImGui::SetItemTooltip("Component not valid");
+	}
+
+	// Joint type
+	if(ImGui::BeginCombo("Type", kJointComponentTypeName[comp.getJointType()]))
+	{
+		for(JointComponentyType type : EnumIterable<JointComponentyType>())
+		{
+			const Bool selected = type == comp.getJointType();
+			if(ImGui::Selectable(kBodyComponentCollisionShapeTypeNames[type], selected))
+			{
+				comp.setJointType(type);
+			}
+
+			// Set the initial focus when opening the combo (scrolling + keyboard navigation focus)
+			if(selected)
+			{
+				ImGui::SetItemDefaultFocus();
+			}
+		}
+		ImGui::EndCombo();
+	}
+}
+
+void EditorUi::bodyComponent(BodyComponent& comp)
+{
+	if(!comp.isValid())
+	{
+		ImGui::SameLine();
+		ImGui::TextUnformatted(ICON_MDI_ALERT);
+		ImGui::SetItemTooltip("Component not valid");
+	}
+
+	// Shape type
+	if(ImGui::BeginCombo("Type", kBodyComponentCollisionShapeTypeNames[comp.getCollisionShapeType()]))
+	{
+		for(BodyComponentCollisionShapeType type : EnumIterable<BodyComponentCollisionShapeType>())
+		{
+			const Bool selected = type == comp.getCollisionShapeType();
+			if(ImGui::Selectable(kBodyComponentCollisionShapeTypeNames[type], selected))
+			{
+				comp.setCollisionShapeType(type);
+			}
+
+			// Set the initial focus when opening the combo (scrolling + keyboard navigation focus)
+			if(selected)
+			{
+				ImGui::SetItemDefaultFocus();
+			}
+		}
+		ImGui::EndCombo();
+	}
+
+	// Mass
+	F32 mass = comp.getMass();
+	if(ImGui::SliderFloat("Mass", &mass, 0.0f, 100.0f))
+	{
+		comp.setMass(mass);
+	}
+
+	if(comp.getCollisionShapeType() == BodyComponentCollisionShapeType::kAabb)
+	{
+		Vec3 extend = comp.getBoxExtend();
+		if(ImGui::SliderFloat3("Box Extend", &extend[0], 0.01f, 100.0f))
+		{
+			comp.setBoxExtend(extend);
+		}
+	}
+	else if(comp.getCollisionShapeType() == BodyComponentCollisionShapeType::kSphere)
+	{
+		F32 radius = comp.getSphereRadius();
+		if(ImGui::SliderFloat("Radius", &radius, 0.01f, 100.0f))
+		{
+			comp.setSphereRadius(radius);
+		}
+	}
+}
+
 void EditorUi::cVarsWindow()
 {
 	if(!m_showCVarEditorWindow)

+ 2 - 0
AnKi/Editor/EditorUi.h

@@ -177,6 +177,8 @@ private:
 	void skinComponent(SkinComponent& comp);
 	void particleEmitterComponent(ParticleEmitter2Component& comp);
 	void lightComponent(LightComponent& comp);
+	void jointComponent(JointComponent& comp);
+	void bodyComponent(BodyComponent& comp);
 	void dirTree(const AssetPath& path);
 
 	// Widget/UI utils

+ 6 - 2
AnKi/Scene/Components/BodyComponent.cpp

@@ -36,10 +36,14 @@ void BodyComponent::teleportTo(Vec3 position, const Mat3& rotation)
 	m_node->setLocalRotation(rotation);
 }
 
+Bool BodyComponent::isValid() const
+{
+	return m_shapeType != BodyComponentCollisionShapeType::kFromMeshComponent || (m_mesh.m_meshc && m_mesh.m_meshc->hasMeshResource());
+}
+
 void BodyComponent::update(SceneComponentUpdateInfo& info, Bool& updated)
 {
-	const Bool meshIsValid = m_mesh.m_meshc && m_mesh.m_meshc->hasMeshResource();
-	if(m_shapeType == BodyComponentCollisionShapeType::kCount || (m_shapeType == BodyComponentCollisionShapeType::kFromMeshComponent && !meshIsValid))
+	if(!isValid())
 	{
 		// It's invalid, return
 		ANKI_ASSERT(!m_body);

+ 23 - 11
AnKi/Scene/Components/BodyComponent.h

@@ -21,8 +21,13 @@ enum class BodyComponentCollisionShapeType : U8
 	kAabb,
 	kSphere,
 
-	kCount
+	kCount,
+	kFirst = 0
 };
+ANKI_ENUM_ALLOW_NUMERIC_OPERATIONS(BodyComponentCollisionShapeType)
+
+inline constexpr Array<const Char*, U32(BodyComponentCollisionShapeType::kCount)> kBodyComponentCollisionShapeTypeNames = {"Mesh Component", "AABB",
+																														   "Sphere"};
 
 /// Rigid body component.
 class BodyComponent : public SceneComponent
@@ -34,6 +39,20 @@ public:
 
 	~BodyComponent();
 
+	void setCollisionShapeType(BodyComponentCollisionShapeType type)
+	{
+		if(ANKI_EXPECT(type <= BodyComponentCollisionShapeType::kCount) && m_shapeType != type)
+		{
+			m_shapeType = type;
+			cleanup(); // Force recreate
+		}
+	}
+
+	BodyComponentCollisionShapeType getCollisionShapeType() const
+	{
+		return m_shapeType;
+	}
+
 	void setBoxExtend(Vec3 extend)
 	{
 		if(ANKI_EXPECT(extend > 0.01f) && extend != m_box.m_extend)
@@ -68,15 +87,6 @@ public:
 		return m_sphere.m_radius;
 	}
 
-	void setCollisionShapeType(BodyComponentCollisionShapeType type)
-	{
-		if(ANKI_EXPECT(type <= BodyComponentCollisionShapeType::kCount) && m_shapeType != type)
-		{
-			m_shapeType = type;
-			cleanup(); // Force recreate
-		}
-	}
-
 	void setMass(F32 mass)
 	{
 		if(ANKI_EXPECT(mass >= 0.0f) && m_mass != mass)
@@ -109,6 +119,8 @@ public:
 		return *m_node;
 	}
 
+	Bool isValid() const;
+
 private:
 	SceneNode* m_node = nullptr;
 	PhysicsBodyPtr m_body;
@@ -146,7 +158,7 @@ private:
 
 	Bool m_teleported = false;
 
-	BodyComponentCollisionShapeType m_shapeType = BodyComponentCollisionShapeType::kCount;
+	BodyComponentCollisionShapeType m_shapeType = BodyComponentCollisionShapeType::kAabb;
 
 	void update(SceneComponentUpdateInfo& info, Bool& updated) override;
 

+ 7 - 7
AnKi/Scene/Components/GlobalIlluminationProbeComponent.cpp

@@ -120,22 +120,21 @@ void GlobalIlluminationProbeComponent::update(SceneComponentUpdateInfo& info, Bo
 	// Upload to GPU scene
 	if(moved || shapeDirty || m_dirty)
 	{
-		// Change the UUID
-		U32 uuid;
+		Bool cpuFeedback = false;
 		if(m_cellsRefreshedCount == 0)
 		{
-			// Refresh starts over, get a new UUID
-			uuid = regenerateUuid();
+			// Refresh starts over
+			cpuFeedback = true;
 		}
 		else if(m_cellsRefreshedCount < m_totalCellCount)
 		{
 			// In the middle of the refresh process
-			uuid = getUuid();
+			cpuFeedback = true;
 		}
 		else
 		{
 			// Refresh it done
-			uuid = 0;
+			cpuFeedback = false;
 		}
 
 		// Upload to the GPU scene
@@ -148,8 +147,9 @@ void GlobalIlluminationProbeComponent::update(SceneComponentUpdateInfo& info, Bo
 		gpuProbe.m_volumeTexture = m_volTexBindlessIdx;
 		gpuProbe.m_halfTexelSizeU = 1.0f / (F32(m_cellCounts.y()) * 6.0f) / 2.0f;
 		gpuProbe.m_fadeDistance = m_fadeDistance;
-		gpuProbe.m_uuid = uuid;
+		gpuProbe.m_uuid = getUuid();
 		gpuProbe.m_componentArrayIndex = getArrayIndex();
+		gpuProbe.m_cpuFeedback = cpuFeedback;
 		m_gpuSceneProbe.uploadToGpuScene(gpuProbe);
 	}
 

+ 23 - 8
AnKi/Scene/Components/JointComponent.cpp

@@ -11,6 +11,7 @@ namespace anki {
 
 JointComponent::JointComponent(SceneNode* node)
 	: SceneComponent(node, kClassType)
+	, m_node(node)
 {
 	node->setIgnoreParentTransform(true);
 }
@@ -19,12 +20,10 @@ JointComponent::~JointComponent()
 {
 }
 
-void JointComponent::update(SceneComponentUpdateInfo& info, Bool& updated)
+Bool JointComponent::isValid() const
 {
-	SceneNode& node = *info.m_node;
-
-	SceneNode* parent = node.getParent();
-	SceneNode* child = node.hasChildren() ? &node.getChild(0) : nullptr;
+	SceneNode* parent = m_node->getParent();
+	SceneNode* child = m_node->hasChildren() ? &m_node->getChild(0) : nullptr;
 
 	BodyComponent* bodyc1 = (parent) ? parent->tryGetFirstComponentOfType<BodyComponent>() : nullptr;
 	BodyComponent* bodyc2 = (child) ? child->tryGetFirstComponentOfType<BodyComponent>() : nullptr;
@@ -32,12 +31,28 @@ void JointComponent::update(SceneComponentUpdateInfo& info, Bool& updated)
 	PhysicsBody* body1 = (bodyc1) ? bodyc1->getPhysicsBody().tryGet() : nullptr;
 	PhysicsBody* body2 = (bodyc2) ? bodyc2->getPhysicsBody().tryGet() : nullptr;
 
-	if(!body1 || !body2 || m_type == JointType::kCount)
+	return body1 && body2 && m_type < JointComponentyType::kCount;
+}
+
+void JointComponent::update(SceneComponentUpdateInfo& info, Bool& updated)
+{
+	SceneNode& node = *info.m_node;
+
+	if(!isValid())
 	{
 		m_joint.reset(nullptr);
 		return;
 	}
 
+	SceneNode* parent = node.getParent();
+	SceneNode* child = &node.getChild(0);
+
+	BodyComponent* bodyc1 = parent->tryGetFirstComponentOfType<BodyComponent>();
+	BodyComponent* bodyc2 = child->tryGetFirstComponentOfType<BodyComponent>();
+
+	PhysicsBody* body1 = bodyc1->getPhysicsBody().tryGet();
+	PhysicsBody* body2 = bodyc2->getPhysicsBody().tryGet();
+
 	const Bool parentChanged = parent && m_parentNodeUuid != parent->getUuid();
 	const Bool childChanged = child && m_childNodeUuid != child->getUuid();
 	if(parentChanged || childChanged || node.movedThisFrame())
@@ -56,10 +71,10 @@ void JointComponent::update(SceneComponentUpdateInfo& info, Bool& updated)
 
 		switch(m_type)
 		{
-		case JointType::kPoint:
+		case JointComponentyType::kPoint:
 			m_joint = PhysicsWorld::getSingleton().newPointJoint(body1, body2, node.getWorldTransform().getOrigin().xyz());
 			break;
-		case JointType::kHinge:
+		case JointComponentyType::kHinge:
 			m_joint = PhysicsWorld::getSingleton().newHingeJoint(body1, body2, node.getWorldTransform());
 			break;
 		default:

+ 21 - 11
AnKi/Scene/Components/JointComponent.h

@@ -10,20 +10,19 @@
 
 namespace anki {
 
-/// @addtogroup scene
-/// @{
-
-/// @memberof JointComponent
-enum class JointType : U8
+enum class JointComponentyType : U8
 {
 	kPoint,
 	kHinge,
 
 	kCount,
-	kFirst
+	kFirst = 0
 };
+ANKI_ENUM_ALLOW_NUMERIC_OPERATIONS(JointComponentyType)
+
+inline constexpr Array<const Char*, U32(JointComponentyType::kCount)> kJointComponentTypeName = {"Point", "Hinge"};
 
-/// Contains a single joint that connects the parent node with the 1st child node of the node that has this component.
+// Contains a single joint that connects the parent node with the 1st child node of the node that has this component.
 class JointComponent : public SceneComponent
 {
 	ANKI_SCENE_COMPONENT(JointComponent)
@@ -33,21 +32,32 @@ public:
 
 	~JointComponent();
 
-	void setType(JointType type)
+	void setJointType(JointComponentyType type)
 	{
-		m_type = type;
+		if(ANKI_EXPECT(type < JointComponentyType::kCount))
+		{
+			m_type = type;
+		}
 	}
 
+	JointComponentyType getJointType() const
+	{
+		return m_type;
+	}
+
+	Bool isValid() const;
+
 private:
 	PhysicsJointPtr m_joint;
 
+	SceneNode* m_node = nullptr;
+
 	U32 m_parentNodeUuid = 0;
 	U32 m_childNodeUuid = 0;
 
-	JointType m_type = JointType::kCount;
+	JointComponentyType m_type = JointComponentyType::kPoint;
 
 	void update(SceneComponentUpdateInfo& info, Bool& updated) override;
 };
-/// @}
 
 } // end namespace anki

+ 1 - 1
AnKi/Scene/Components/MaterialComponent.cpp

@@ -191,7 +191,7 @@ void MaterialComponent::update(SceneComponentUpdateInfo& info, Bool& updated)
 		}
 
 		// Update the GPU scene AABBs
-		if(prioritizeEmitter || m_skinComponent)
+		if(prioritizeEmitter || m_skinComponent || moved)
 		{
 			const Aabb aabbWorld = computeAabb(*info.m_node);
 			for(RenderingTechnique t : EnumBitsIterable<RenderingTechnique, RenderingTechniqueBit>(mtl.getRenderingTechniques()))

+ 2 - 4
AnKi/Scene/Components/ReflectionProbeComponent.cpp

@@ -52,9 +52,6 @@ void ReflectionProbeComponent::update(SceneComponentUpdateInfo& info, Bool& upda
 		m_worldPos = info.m_node->getWorldTransform().getOrigin().xyz();
 		m_halfSize = info.m_node->getWorldTransform().getScale().xyz();
 
-		// Update the UUID
-		const U32 uuid = (m_reflectionNeedsRefresh) ? regenerateUuid() : 0;
-
 		// Upload to the GPU scene
 		GpuSceneReflectionProbe gpuProbe;
 		gpuProbe.m_position = m_worldPos;
@@ -64,8 +61,9 @@ void ReflectionProbeComponent::update(SceneComponentUpdateInfo& info, Bool& upda
 		gpuProbe.m_aabbMin = aabbWorld.getMin().xyz();
 		gpuProbe.m_aabbMax = aabbWorld.getMax().xyz();
 
-		gpuProbe.m_uuid = uuid;
+		gpuProbe.m_uuid = getUuid();
 		gpuProbe.m_componentArrayIndex = getArrayIndex();
+		gpuProbe.m_cpuFeedback = m_reflectionNeedsRefresh;
 		m_gpuSceneProbe.uploadToGpuScene(gpuProbe);
 	}
 }

+ 0 - 4
AnKi/Shaders/GpuVisibilityNonRenderables.ankiprog

@@ -133,11 +133,7 @@ Vec4 getSphere(GpuSceneGlobalIlluminationProbe l)
 	// Give feedback to the CPU
 	//
 #if CPU_FEEDBACK
-#	if OBJECT_TYPE == ANKI_GPU_SCENE_NON_RENDERABLE_OBJECT_TYPE_LIGHT
 	const Bool doFeedback = g_objects[svDispatchThreadId].m_cpuFeedback;
-#	else
-	const Bool doFeedback = true;
-#	endif
 
 	if(!skip && doFeedback)
 	{

+ 5 - 4
AnKi/Shaders/Include/GpuSceneTypes.h

@@ -161,10 +161,11 @@ struct GpuSceneLight
 struct GpuSceneReflectionProbe
 {
 	Vec3 m_position; // Position of the probe in world space.
-	U32 m_cubeTexture; // Bindless index of the reflection texture.
+	U32 m_cubeTexture : 30; // Bindless index of the reflection texture
+	U32 m_cpuFeedback : 1;
 
 	Vec3 m_aabbMin ANKI_CPP_CODE(= Vec3(kSomeFarDistance));
-	U32 m_uuid; // The UUID of that probe. If it's zero the GPU will not inform the CPU about it.
+	U32 m_uuid; // The UUID of that probe
 
 	Vec3 m_aabbMax ANKI_CPP_CODE(= Vec3(kSomeFarDistance));
 	U32 m_componentArrayIndex; // Array in the CPU scene.
@@ -176,7 +177,7 @@ static_assert(sizeof(GpuSceneReflectionProbe) == kSizeof_GpuSceneReflectionProbe
 struct GpuSceneGlobalIlluminationProbe
 {
 	Vec3 m_aabbMin ANKI_CPP_CODE(= Vec3(kSomeFarDistance));
-	U32 m_uuid; // The UUID of that probe. If it's zero the GPU will not inform the CPU about it.
+	U32 m_uuid; // The UUID of that probe
 
 	Vec3 m_aabbMax ANKI_CPP_CODE(= Vec3(kSomeFarDistance));
 	U32 m_componentArrayIndex; // Array in the CPU scene.
@@ -184,7 +185,7 @@ struct GpuSceneGlobalIlluminationProbe
 	U32 m_volumeTexture; // Bindless index of the irradiance volume texture.
 	F32 m_halfTexelSizeU; // (1.0 / textureSize(texArr[textureIndex]).x) / 2.0
 	F32 m_fadeDistance; // Used to calculate a factor that is zero when fragPos is close to AABB bounds and 1.0 at fadeDistance and less
-	F32 m_padding2;
+	U32 m_cpuFeedback;
 };
 constexpr U32 kSizeof_GpuSceneGlobalIlluminationProbe = 3u * sizeof(Vec4);
 static_assert(sizeof(GpuSceneGlobalIlluminationProbe) == kSizeof_GpuSceneGlobalIlluminationProbe);

+ 2 - 2
Samples/PhysicsPlayground/Main.cpp

@@ -57,7 +57,7 @@ Error MyApp::sampleExtraInit()
 
 		SceneNode* joint = SceneGraph::getSingleton().newSceneNode<SceneNode>("hinge");
 		JointComponent* jointc = joint->newComponent<JointComponent>();
-		jointc->setType(JointType::kHinge);
+		jointc->setJointType(JointComponentyType::kHinge);
 		joint->setLocalOrigin(Vec3(-0.0f, 4.8f, -3.0f));
 		base->addChild(joint);
 
@@ -97,7 +97,7 @@ Error MyApp::sampleExtraInit()
 		{
 			SceneNode* joint = SceneGraph::getSingleton().newSceneNode<SceneNode>(String().sprintf("joint_chain%u", i));
 			JointComponent* jointc = joint->newComponent<JointComponent>();
-			jointc->setType(JointType::kPoint);
+			jointc->setJointType(JointComponentyType::kPoint);
 			joint->setLocalOrigin(trf.getOrigin().xyz());
 			joint->setParent(prevNode);