From 307090bfd5e266575c7d6938da03474e1ed35820 Mon Sep 17 00:00:00 2001 From: turanszkij Date: Mon, 10 Sep 2018 15:19:32 +0100 Subject: [PATCH] physics tmp commit --- Editor/ObjectWindow.cpp | 241 +++++++++------------------------ Editor/ObjectWindow.h | 3 +- WickedEngine/wiBULLET.cpp | 4 +- WickedEngine/wiRenderer.cpp | 92 +++++-------- WickedEngine/wiSceneSystem.cpp | 80 +++++++---- WickedEngine/wiSceneSystem.h | 31 ++--- 6 files changed, 172 insertions(+), 279 deletions(-) diff --git a/Editor/ObjectWindow.cpp b/Editor/ObjectWindow.cpp index e9eff77bf..3cd9e4151 100644 --- a/Editor/ObjectWindow.cpp +++ b/Editor/ObjectWindow.cpp @@ -85,114 +85,58 @@ ObjectWindow::ObjectWindow(wiGUI* gui) : GUI(gui) physicsLabel->SetSize(XMFLOAT2(150, 20)); objectWindow->AddWidget(physicsLabel); - simulationTypeComboBox = new wiComboBox("Simulation Type:"); - simulationTypeComboBox->SetSize(XMFLOAT2(100, 20)); - simulationTypeComboBox->SetPos(XMFLOAT2(x, y += 30)); - simulationTypeComboBox->AddItem("None"); - simulationTypeComboBox->AddItem("Rigid Body"); - //simulationTypeComboBox->AddItem("Soft Body"); - simulationTypeComboBox->OnSelect([&](wiEventArgs args) { - //PhysicsComponent* physicscomponent = wiRenderer::GetScene().physicscomponents.GetComponent(entity); - //if (physicscomponent != nullptr) - //{ - // wiRenderer::physicsEngine->removeObject(*physicscomponent); - // switch (args.iValue) - // { - // case 0: - // physicscomponent->rigidBody = false; - // //if (object->mesh != nullptr) - // //{ - // // object->mesh->softBody = false; - // //} - // kinematicCheckBox->SetEnabled(false); - // physicsTypeComboBox->SetEnabled(false); - // collisionShapeComboBox->SetEnabled(false); - // break; - // case 1: - // physicscomponent->rigidBody = true; - // //if (object->mesh != nullptr) - // //{ - // // object->mesh->softBody = false; - // //} + rigidBodyCheckBox = new wiCheckBox("Rigid Body Physics: "); + rigidBodyCheckBox->SetTooltip("Enable rigid body physics simulation."); + rigidBodyCheckBox->SetPos(XMFLOAT2(x, y += 30)); + rigidBodyCheckBox->SetCheck(false); + rigidBodyCheckBox->OnClick([&](wiEventArgs args) + { + Scene& scene = wiRenderer::GetScene(); + if (args.bValue) + { + TransformComponent* transform = scene.transforms.GetComponent(entity); + ObjectComponent* object = scene.objects.GetComponent(entity); + if (transform != nullptr && object != nullptr) + { + MeshComponent* mesh = scene.meshes.GetComponent(object->meshID); - // kinematicCheckBox->SetEnabled(true); - // physicsTypeComboBox->SetEnabled(true); - // collisionShapeComboBox->SetEnabled(true); - // break; - // //case 2: - // // object->rigidBody = false; - // // if (object->mesh != nullptr) - // // { - // // object->mesh->softBody = true; - // // } - - // // kinematicCheckBox->SetEnabled(false); - // // physicsTypeComboBox->SetEnabled(true); - // // collisionShapeComboBox->SetEnabled(false); - // // break; - // default: - // assert(0); - // break; - // } - - // wiRenderer::physicsEngine->registerObject(*physicscomponent); - - //} + RigidBodyPhysicsComponent* physicscomponent = scene.rigidbodies.GetComponent(entity); + if (physicscomponent == nullptr) + { + RigidBodyPhysicsComponent& rigidbody = scene.rigidbodies.Create(entity); + rigidbody.kinematic = kinematicCheckBox->GetCheck(); + rigidbody.shape = (RigidBodyPhysicsComponent::CollisionShape)collisionShapeComboBox->GetSelected(); + wiRenderer::physicsEngine->addRigidBody(rigidbody, *mesh, *transform); + } + } + } + else + { + RigidBodyPhysicsComponent* physicscomponent = wiRenderer::GetScene().rigidbodies.GetComponent(entity); + if (physicscomponent != nullptr) + { + wiRenderer::physicsEngine->removeRigidBody(*physicscomponent); + scene.rigidbodies.Remove(entity); + } + } }); - simulationTypeComboBox->SetSelected(0); - simulationTypeComboBox->SetEnabled(true); - simulationTypeComboBox->SetTooltip("Set simulation type."); - objectWindow->AddWidget(simulationTypeComboBox); + objectWindow->AddWidget(rigidBodyCheckBox); kinematicCheckBox = new wiCheckBox("Kinematic: "); kinematicCheckBox->SetTooltip("Toggle kinematic behaviour."); kinematicCheckBox->SetPos(XMFLOAT2(x, y += 30)); kinematicCheckBox->SetCheck(false); kinematicCheckBox->OnClick([&](wiEventArgs args) { - //PhysicsComponent* physicscomponent = wiRenderer::GetScene().physicscomponents.GetComponent(entity); - //if (physicscomponent != nullptr) - //{ - // wiRenderer::physicsEngine->removeObject(object); - - // object->kinematic = args.bValue; - - // wiRenderer::physicsEngine->registerObject(object); - //} + RigidBodyPhysicsComponent* physicscomponent = wiRenderer::GetScene().rigidbodies.GetComponent(entity); + if (physicscomponent != nullptr) + { + physicscomponent->kinematic = args.bValue; + } }); objectWindow->AddWidget(kinematicCheckBox); - physicsTypeComboBox = new wiComboBox("Contribution Type:"); - physicsTypeComboBox->SetSize(XMFLOAT2(100, 20)); - physicsTypeComboBox->SetPos(XMFLOAT2(x, y += 30)); - physicsTypeComboBox->AddItem("Active"); - physicsTypeComboBox->AddItem("Passive"); - physicsTypeComboBox->OnSelect([&](wiEventArgs args) { - //if (object != nullptr) - //{ - // wiRenderer::physicsEngine->removeObject(object); - - // switch (args.iValue) - // { - // case 0: - // object->physicsType = "ACTIVE"; - // break; - // case 1: - // object->physicsType = "PASSIVE"; - // break; - // default: - // break; - // } - - // wiRenderer::physicsEngine->registerObject(object); - //} - }); - physicsTypeComboBox->SetSelected(0); - physicsTypeComboBox->SetEnabled(true); - physicsTypeComboBox->SetTooltip("Set physics type."); - objectWindow->AddWidget(physicsTypeComboBox); - collisionShapeComboBox = new wiComboBox("Collision Shape:"); collisionShapeComboBox->SetSize(XMFLOAT2(100, 20)); collisionShapeComboBox->SetPos(XMFLOAT2(x, y += 30)); @@ -201,34 +145,32 @@ ObjectWindow::ObjectWindow(wiGUI* gui) : GUI(gui) collisionShapeComboBox->AddItem("Capsule"); collisionShapeComboBox->AddItem("Convex Hull"); collisionShapeComboBox->AddItem("Triangle Mesh"); - collisionShapeComboBox->OnSelect([&](wiEventArgs args) { - //if (object != nullptr) - //{ - // wiRenderer::physicsEngine->removeObject(object); - - // switch (args.iValue) - // { - // case 0: - // object->collisionShape = "BOX"; - // break; - // case 1: - // object->collisionShape = "SPHERE"; - // break; - // case 2: - // object->collisionShape = "CAPSULE"; - // break; - // case 3: - // object->collisionShape = "CONVEX_HULL"; - // break; - // case 4: - // object->collisionShape = "MESH"; - // break; - // default: - // break; - // } - - // wiRenderer::physicsEngine->registerObject(object); - //} + collisionShapeComboBox->OnSelect([&](wiEventArgs args) + { + RigidBodyPhysicsComponent* physicscomponent = wiRenderer::GetScene().rigidbodies.GetComponent(entity); + if (physicscomponent != nullptr) + { + switch (args.iValue) + { + case 0: + physicscomponent->shape = RigidBodyPhysicsComponent::CollisionShape::BOX; + break; + case 1: + physicscomponent->shape = RigidBodyPhysicsComponent::CollisionShape::SPHERE; + break; + case 2: + physicscomponent->shape = RigidBodyPhysicsComponent::CollisionShape::CAPSULE; + break; + case 3: + physicscomponent->shape = RigidBodyPhysicsComponent::CollisionShape::CONVEX_HULL; + break; + case 4: + physicscomponent->shape = RigidBodyPhysicsComponent::CollisionShape::TRIANGLE_MESH; + break; + default: + break; + } + } }); collisionShapeComboBox->SetSelected(0); collisionShapeComboBox->SetEnabled(true); @@ -272,42 +214,12 @@ void ObjectWindow::SetEntity(Entity entity) const RigidBodyPhysicsComponent* physicsComponent = scene.rigidbodies.GetComponent(entity); + rigidBodyCheckBox->SetCheck(physicsComponent != nullptr); + if (physicsComponent != nullptr) { - //if (physicsComponent->rigidBody) - //{ - // simulationTypeComboBox->SetSelected(1); - //} - //else - //{ - // if (object->mesh != nullptr) - // { - // if (object->mesh->softBody) - // { - // simulationTypeComboBox->SetSelected(2); - // } - // else - // { - // simulationTypeComboBox->SetSelected(0); - // } - // } - // else - // { - // simulationTypeComboBox->SetSelected(0); - // } - //} - kinematicCheckBox->SetCheck(physicsComponent->kinematic); - //if (!physicsComponent->physicsType.compare("ACTIVE")) - //{ - // physicsTypeComboBox->SetSelected(0); - //} - //else if (!physicsComponent->physicsType.compare("PASSIVE")) - //{ - // physicsTypeComboBox->SetSelected(1); - //} - if (physicsComponent->shape == RigidBodyPhysicsComponent::CollisionShape::BOX) { collisionShapeComboBox->SetSelected(0); @@ -332,25 +244,6 @@ void ObjectWindow::SetEntity(Entity entity) objectWindow->SetEnabled(true); - - switch (simulationTypeComboBox->GetSelected()) - { - case 1: - kinematicCheckBox->SetEnabled(true); - physicsTypeComboBox->SetEnabled(true); - collisionShapeComboBox->SetEnabled(true); - break; - case 2: - kinematicCheckBox->SetEnabled(false); - physicsTypeComboBox->SetEnabled(true); - collisionShapeComboBox->SetEnabled(false); - break; - default: - kinematicCheckBox->SetEnabled(false); - physicsTypeComboBox->SetEnabled(false); - collisionShapeComboBox->SetEnabled(false); - break; - } } else { diff --git a/Editor/ObjectWindow.h b/Editor/ObjectWindow.h index f5895fa26..8c734d699 100644 --- a/Editor/ObjectWindow.h +++ b/Editor/ObjectWindow.h @@ -27,9 +27,8 @@ public: wiColorPicker* colorPicker; wiLabel* physicsLabel; - wiComboBox* simulationTypeComboBox; + wiCheckBox* rigidBodyCheckBox; wiCheckBox* kinematicCheckBox; - wiComboBox* physicsTypeComboBox; wiComboBox* collisionShapeComboBox; }; diff --git a/WickedEngine/wiBULLET.cpp b/WickedEngine/wiBULLET.cpp index f3e106098..541ec1a28 100644 --- a/WickedEngine/wiBULLET.cpp +++ b/WickedEngine/wiBULLET.cpp @@ -66,8 +66,8 @@ wiBULLET::wiBULLET() bulletPhysics->dynamicsWorld->getSolverInfo().m_splitImpulse=true; bulletPhysics->dynamicsWorld->setGravity(btVector3(0,-11,0)); - ((btSoftRigidDynamicsWorld*)bulletPhysics->dynamicsWorld)->getWorldInfo().air_density = (btScalar)1.0; - ((btSoftRigidDynamicsWorld*)bulletPhysics->dynamicsWorld)->getWorldInfo().water_density = 0; + ((btSoftRigidDynamicsWorld*)bulletPhysics->dynamicsWorld)->getWorldInfo().air_density = (btScalar)0.0; + ((btSoftRigidDynamicsWorld*)bulletPhysics->dynamicsWorld)->getWorldInfo().water_density = 0; ((btSoftRigidDynamicsWorld*)bulletPhysics->dynamicsWorld)->getWorldInfo().water_offset = 0; ((btSoftRigidDynamicsWorld*)bulletPhysics->dynamicsWorld)->getWorldInfo().water_normal = btVector3(0,0,0); ((btSoftRigidDynamicsWorld*)bulletPhysics->dynamicsWorld)->getWorldInfo().m_gravity.setValue(0,-11,0); diff --git a/WickedEngine/wiRenderer.cpp b/WickedEngine/wiRenderer.cpp index 7933ada53..0794dc811 100644 --- a/WickedEngine/wiRenderer.cpp +++ b/WickedEngine/wiRenderer.cpp @@ -3386,16 +3386,16 @@ void wiRenderer::UpdateRenderData(GRAPHICSTHREAD threadID) } entityArrayCount_ForceFields = entityCounter - entityArrayOffset_ForceFields; - GetDevice()->UpdateBuffer(resourceBuffers[RBTYPE_ENTITYARRAY], entityArray, threadID, sizeof(ShaderEntityType)*entityCounter); - GetDevice()->UpdateBuffer(resourceBuffers[RBTYPE_MATRIXARRAY], matrixArray, threadID, sizeof(XMMATRIX)*matrixCounter); + device->UpdateBuffer(resourceBuffers[RBTYPE_ENTITYARRAY], entityArray, threadID, sizeof(ShaderEntityType)*entityCounter); + device->UpdateBuffer(resourceBuffers[RBTYPE_MATRIXARRAY], matrixArray, threadID, sizeof(XMMATRIX)*matrixCounter); GPUResource* resources[] = { resourceBuffers[RBTYPE_ENTITYARRAY], resourceBuffers[RBTYPE_MATRIXARRAY], }; - GetDevice()->BindResources(VS, resources, SBSLOT_ENTITYARRAY, ARRAYSIZE(resources), threadID); - GetDevice()->BindResources(PS, resources, SBSLOT_ENTITYARRAY, ARRAYSIZE(resources), threadID); - GetDevice()->BindResources(CS, resources, SBSLOT_ENTITYARRAY, ARRAYSIZE(resources), threadID); + device->BindResources(VS, resources, SBSLOT_ENTITYARRAY, ARRAYSIZE(resources), threadID); + device->BindResources(PS, resources, SBSLOT_ENTITYARRAY, ARRAYSIZE(resources), threadID); + device->BindResources(CS, resources, SBSLOT_ENTITYARRAY, ARRAYSIZE(resources), threadID); } @@ -3406,7 +3406,7 @@ void wiRenderer::UpdateRenderData(GRAPHICSTHREAD threadID) ManageDecalAtlas(threadID); wiProfiler::GetInstance().BeginRange("Skinning", wiProfiler::DOMAIN_GPU, threadID); - GetDevice()->EventBegin("Skinning", threadID); + device->EventBegin("Skinning", threadID); { bool streamOutSetUp = false; CSTYPES lastCS = CSTYPE_SKINNING_LDS; @@ -3433,7 +3433,7 @@ void wiRenderer::UpdateRenderData(GRAPHICSTHREAD threadID) bd.StructureByteStride = sizeof(ArmatureComponent::ShaderBoneType); armature.boneBuffer.reset(new GPUBuffer); - HRESULT hr = wiRenderer::GetDevice()->CreateBuffer(&bd, nullptr, armature.boneBuffer.get()); + HRESULT hr = device->CreateBuffer(&bd, nullptr, armature.boneBuffer.get()); assert(SUCCEEDED(hr)); } @@ -3447,8 +3447,8 @@ void wiRenderer::UpdateRenderData(GRAPHICSTHREAD threadID) const UINT strides[] = { 0,0,0,0,0,0,0,0 }; - GetDevice()->BindVertexBuffers(vbs, 0, ARRAYSIZE(vbs), strides, nullptr, threadID); - GetDevice()->BindComputePSO(CPSO[CSTYPE_SKINNING_LDS], threadID); + device->BindVertexBuffers(vbs, 0, ARRAYSIZE(vbs), strides, nullptr, threadID); + device->BindComputePSO(CPSO[CSTYPE_SKINNING_LDS], threadID); } CSTYPES targetCS = CSTYPE_SKINNING_LDS; @@ -3462,7 +3462,7 @@ void wiRenderer::UpdateRenderData(GRAPHICSTHREAD threadID) if (targetCS != lastCS) { lastCS = targetCS; - GetDevice()->BindComputePSO(CPSO[targetCS], threadID); + device->BindComputePSO(CPSO[targetCS], threadID); } // Upload bones for skinning to shader @@ -3470,8 +3470,8 @@ void wiRenderer::UpdateRenderData(GRAPHICSTHREAD threadID) { armature.boneData[k].Create(armature.skinningMatrices[k]); } - GetDevice()->UpdateBuffer(armature.boneBuffer.get(), armature.boneData.data(), threadID, (int)(sizeof(ArmatureComponent::ShaderBoneType) * armature.boneData.size())); - GetDevice()->BindResource(CS, armature.boneBuffer.get(), SKINNINGSLOT_IN_BONEBUFFER, threadID); + device->UpdateBuffer(armature.boneBuffer.get(), armature.boneData.data(), threadID, (int)(sizeof(ArmatureComponent::ShaderBoneType) * armature.boneData.size())); + device->BindResource(CS, armature.boneBuffer.get(), SKINNINGSLOT_IN_BONEBUFFER, threadID); // Do the skinning GPUResource* vbs[] = { @@ -3483,37 +3483,28 @@ void wiRenderer::UpdateRenderData(GRAPHICSTHREAD threadID) mesh.streamoutBuffer_PRE.get(), }; - GetDevice()->BindResources(CS, vbs, SKINNINGSLOT_IN_VERTEX_POS, ARRAYSIZE(vbs), threadID); - GetDevice()->BindUAVs(CS, sos, 0, ARRAYSIZE(sos), threadID); + device->BindResources(CS, vbs, SKINNINGSLOT_IN_VERTEX_POS, ARRAYSIZE(vbs), threadID); + device->BindUAVs(CS, sos, 0, ARRAYSIZE(sos), threadID); - GetDevice()->Dispatch((UINT)ceilf((float)mesh.vertex_positions.size() / SKINNING_COMPUTE_THREADCOUNT), 1, 1, threadID); - GetDevice()->UAVBarrier(sos, ARRAYSIZE(sos), threadID); // todo: defer - //GetDevice()->TransitionBarrier(sos, ARRAYSIZE(sos), RESOURCE_STATE_UNORDERED_ACCESS, RESOURCE_STATE_VERTEX_AND_CONSTANT_BUFFER, threadID); + device->Dispatch((UINT)ceilf((float)mesh.vertex_positions.size() / SKINNING_COMPUTE_THREADCOUNT), 1, 1, threadID); + device->UAVBarrier(sos, ARRAYSIZE(sos), threadID); // todo: defer + //device->TransitionBarrier(sos, ARRAYSIZE(sos), RESOURCE_STATE_UNORDERED_ACCESS, RESOURCE_STATE_VERTEX_AND_CONSTANT_BUFFER, threadID); + } + else if (mesh.IsDynamicVB()) + { + } - //else if (mesh.IsDynamicVB()) - //{ - // // Upload CPU skinned vertex buffer (Soft body VB) - // size_t size_pos = sizeof(MeshComponent::Vertex_POS) * mesh.vertices_Transformed_POS.size(); - // size_t size_pre = sizeof(MeshComponent::Vertex_POS) * mesh.vertices_Transformed_PRE.size(); - // UINT offset; - // void* vertexData = GetDevice()->AllocateFromRingBuffer(dynamicVertexBufferPool, size_pos + size_pre, offset, threadID); - // mesh.bufferOffset_POS = offset; - // mesh.bufferOffset_PRE = offset + (UINT)size_pos; - // memcpy(vertexData, mesh.vertices_Transformed_POS.data(), size_pos); - // memcpy(reinterpret_cast(reinterpret_cast(vertexData) + size_pos), mesh.vertices_Transformed_PRE.data(), size_pre); - // GetDevice()->InvalidateBufferAccess(dynamicVertexBufferPool, threadID); - //} } if (streamOutSetUp) { - GetDevice()->UnbindUAVs(0, 2, threadID); - GetDevice()->UnbindResources(SKINNINGSLOT_IN_VERTEX_POS, 2, threadID); + device->UnbindUAVs(0, 2, threadID); + device->UnbindResources(SKINNINGSLOT_IN_VERTEX_POS, 2, threadID); } } - GetDevice()->EventEnd(threadID); + device->EventEnd(threadID); wiProfiler::GetInstance().EndRange(threadID); // skinning //// Particle system simulation/sorting/culling: @@ -3548,7 +3539,7 @@ void wiRenderer::UpdateRenderData(GRAPHICSTHREAD threadID) desc.MiscFlags = 0; desc.Usage = USAGE_DEFAULT; - GetDevice()->CreateTexture2D(&desc, nullptr, (Texture2D**)&textures[TEXTYPE_2D_CLOUDS]); + device->CreateTexture2D(&desc, nullptr, (Texture2D**)&textures[TEXTYPE_2D_CLOUDS]); } float cloudPhase = renderTime * GetScene().cloudSpeed; @@ -5147,8 +5138,6 @@ void wiRenderer::RenderMeshes(const XMFLOAT3& eye, const CulledCollection& culle continue; } - const bool dynamicVB = mesh.IsDynamicVB(); - const auto& visibleInstances = iter->second; const float tessF = mesh.GetTessellationFactor(); @@ -5200,21 +5189,14 @@ void wiRenderer::RenderMeshes(const XMFLOAT3& eye, const CulledCollection& culle if (advancedVBRequest || tessellatorRequested) { - ((volatile InstBuf*)instances)[k].instance.Create(dynamicVB ? __identityMat : transform.world, instance.color); + ((volatile InstBuf*)instances)[k].instance.Create(transform.world, instance.color); - if (dynamicVB) - { - ((volatile InstBuf*)instances)[k].instancePrev.Create(__identityMat); - } - else - { - const PreviousFrameTransformComponent& prev_transform = *scene.prev_transforms.GetComponent(objectEntity); - ((volatile InstBuf*)instances)[k].instancePrev.Create(prev_transform.world_prev); - } + const PreviousFrameTransformComponent& prev_transform = *scene.prev_transforms.GetComponent(objectEntity); + ((volatile InstBuf*)instances)[k].instancePrev.Create(prev_transform.world_prev); } else { - ((volatile Instance*)instances)[k].Create(dynamicVB ? __identityMat : transform.world, instance.color, dither); + ((volatile Instance*)instances)[k].Create(transform.world, instance.color, dither); } ++k; @@ -5320,7 +5302,7 @@ void wiRenderer::RenderMeshes(const XMFLOAT3& eye, const CulledCollection& culle case BOUNDVERTEXBUFFERTYPE::POSITION: { GPUBuffer* vbs[] = { - dynamicVB ? dynamicVertexBufferPool : (mesh.streamoutBuffer_POS.get() != nullptr ? mesh.streamoutBuffer_POS.get() : mesh.vertexBuffer_POS.get()), + mesh.streamoutBuffer_POS.get() != nullptr ? mesh.streamoutBuffer_POS.get() : mesh.vertexBuffer_POS.get(), dynamicVertexBufferPool }; UINT strides[] = { @@ -5328,7 +5310,7 @@ void wiRenderer::RenderMeshes(const XMFLOAT3& eye, const CulledCollection& culle sizeof(Instance) }; UINT offsets[] = { - dynamicVB ? mesh.bufferOffset_POS : 0, + 0, instancesOffset }; device->BindVertexBuffers(vbs, 0, ARRAYSIZE(vbs), strides, offsets, threadID); @@ -5337,7 +5319,7 @@ void wiRenderer::RenderMeshes(const XMFLOAT3& eye, const CulledCollection& culle case BOUNDVERTEXBUFFERTYPE::POSITION_TEXCOORD: { GPUBuffer* vbs[] = { - dynamicVB ? dynamicVertexBufferPool : (mesh.streamoutBuffer_POS.get() != nullptr ? mesh.streamoutBuffer_POS.get() : mesh.vertexBuffer_POS.get()), + mesh.streamoutBuffer_POS.get() != nullptr ? mesh.streamoutBuffer_POS.get() : mesh.vertexBuffer_POS.get(), mesh.vertexBuffer_TEX.get(), dynamicVertexBufferPool }; @@ -5347,7 +5329,7 @@ void wiRenderer::RenderMeshes(const XMFLOAT3& eye, const CulledCollection& culle sizeof(Instance) }; UINT offsets[] = { - dynamicVB ? mesh.bufferOffset_POS : 0, + 0, 0, instancesOffset }; @@ -5357,9 +5339,9 @@ void wiRenderer::RenderMeshes(const XMFLOAT3& eye, const CulledCollection& culle case BOUNDVERTEXBUFFERTYPE::EVERYTHING: { GPUBuffer* vbs[] = { - dynamicVB ? dynamicVertexBufferPool : (mesh.streamoutBuffer_POS.get() != nullptr ? mesh.streamoutBuffer_POS.get() : mesh.vertexBuffer_POS.get()), + mesh.streamoutBuffer_POS.get() != nullptr ? mesh.streamoutBuffer_POS.get() : mesh.vertexBuffer_POS.get(), mesh.vertexBuffer_TEX.get(), - dynamicVB ? dynamicVertexBufferPool : (mesh.streamoutBuffer_PRE.get() != nullptr ? mesh.streamoutBuffer_PRE.get() : mesh.vertexBuffer_POS.get()), + mesh.streamoutBuffer_PRE.get() != nullptr ? mesh.streamoutBuffer_PRE.get() : mesh.vertexBuffer_POS.get(), dynamicVertexBufferPool }; UINT strides[] = { @@ -5369,9 +5351,9 @@ void wiRenderer::RenderMeshes(const XMFLOAT3& eye, const CulledCollection& culle sizeof(InstBuf) }; UINT offsets[] = { - dynamicVB ? mesh.bufferOffset_POS : 0, 0, - dynamicVB ? mesh.bufferOffset_PRE : 0, + 0, + 0, instancesOffset }; device->BindVertexBuffers(vbs, 0, ARRAYSIZE(vbs), strides, offsets, threadID); diff --git a/WickedEngine/wiSceneSystem.cpp b/WickedEngine/wiSceneSystem.cpp index c00e52e65..9ac67bb59 100644 --- a/WickedEngine/wiSceneSystem.cpp +++ b/WickedEngine/wiSceneSystem.cpp @@ -2,6 +2,7 @@ #include "wiMath.h" #include "wiTextureHelper.h" #include "wiResourceManager.h" +#include "wiPHYSICS.h" using namespace wiECS; using namespace wiGraphicsTypes; @@ -740,6 +741,8 @@ namespace wiSceneSystem RunPreviousFrameTransformUpdateSystem(transforms, prev_transforms); + RunPhysicsUpdateSystem(transforms, meshes, objects, rigidbodies, softbodies, dt); + RunTransformUpdateSystem(transforms); RunAnimationUpdateSystem(animations, transforms, dt); @@ -748,8 +751,6 @@ namespace wiSceneSystem RunArmatureUpdateSystem(transforms, armatures); - RunPhysicsUpdateSystem(transforms, meshes, objects, rigidbodies, softbodies); - RunMaterialUpdateSystem(materials, dt); RunObjectUpdateSystem(transforms, meshes, materials, objects, cullables, bounds, waterPlane); @@ -1127,6 +1128,57 @@ namespace wiSceneSystem prev_transform.world_prev = transform.world; } } + void RunPhysicsUpdateSystem( + ComponentManager& transforms, + ComponentManager& meshes, + ComponentManager& objects, + ComponentManager& rigidbodies, + ComponentManager& softbodies, + float dt + ) + { + PHYSICS* physicsEngine = wiRenderer::physicsEngine; + + if (physicsEngine != nullptr) + { + physicsEngine->Update(dt); + + // Rigid bodies - Objects: + for (size_t i = 0; i < rigidbodies.GetCount(); ++i) + { + RigidBodyPhysicsComponent& rigidbody = rigidbodies[i]; + Entity entity = rigidbodies.GetEntity(i); + TransformComponent& transform = *transforms.GetComponent(entity); + + if (rigidbody.kinematic) + { + physicsEngine->transformBody(transform.rotation, transform.translation, rigidbody.physicsObjectID); + } + else + { + PHYSICS::PhysicsTransform* pt = physicsEngine->getObject(rigidbody.physicsObjectID); + transform.translation_local = pt->position; + transform.rotation_local = pt->rotation; + transform.dirty = true; + } + + } + + // Soft bodies - Meshes: + for (size_t i = 0; i < softbodies.GetCount(); ++i) + { + SoftBodyPhysicsComponent& softbody = softbodies[i]; + Entity entity = softbodies.GetEntity(i); + + MeshComponent& mesh = *meshes.GetComponent(entity); + mesh.dynamicVB = true; + + } + + physicsEngine->NextRunWorld(); + } + + } void RunTransformUpdateSystem(ComponentManager& transforms) { for (size_t i = 0; i < transforms.GetCount(); ++i) @@ -1328,30 +1380,6 @@ namespace wiSceneSystem } } - void RunPhysicsUpdateSystem( - ComponentManager& transforms, - ComponentManager& meshes, - ComponentManager& objects, - ComponentManager& rigidbodies, - ComponentManager& softbodies - ) - { - for (size_t i = 0; i < rigidbodies.GetCount(); ++i) - { - RigidBodyPhysicsComponent& rigidbody = rigidbodies[i]; - Entity entity = rigidbodies.GetEntity(i); - - } - for (size_t i = 0; i < softbodies.GetCount(); ++i) - { - SoftBodyPhysicsComponent& softbody = softbodies[i]; - Entity entity = softbodies.GetEntity(i); - - MeshComponent& mesh = *meshes.GetComponent(entity); - mesh.dynamicVB = true; - - } - } void RunMaterialUpdateSystem(ComponentManager& materials, float dt) { for (size_t i = 0; i < materials.GetCount(); ++i) diff --git a/WickedEngine/wiSceneSystem.h b/WickedEngine/wiSceneSystem.h index f6c5112aa..7949ade74 100644 --- a/WickedEngine/wiSceneSystem.h +++ b/WickedEngine/wiSceneSystem.h @@ -287,10 +287,7 @@ namespace wiSceneSystem std::unique_ptr streamoutBuffer_POS; std::unique_ptr streamoutBuffer_PRE; - // Dynamic vertexbuffers write into a global pool, these will be the offsets into that: - bool dynamicVB = false; - uint32_t bufferOffset_POS; - uint32_t bufferOffset_PRE; + bool dynamicVB = false; // soft body will update the vertex buffer from cpu wiGraphicsTypes::INDEXBUFFER_FORMAT indexFormat = wiGraphicsTypes::INDEXFORMAT_16BIT; @@ -299,14 +296,7 @@ namespace wiSceneSystem AABB aabb; - wiECS::Entity armatureID; - - struct VertexGroup - { - std::string name; - std::unordered_map vertex_weights; // maps vertex index to weight value... - }; - std::vector vertexGroups; + wiECS::Entity armatureID = wiECS::INVALID_ENTITY; wiRenderTarget impostorTarget; float impostorDistance = 100.0f; @@ -364,8 +354,8 @@ namespace wiSceneSystem { enum class CollisionShape { - SPHERE, BOX, + SPHERE, CAPSULE, CONVEX_HULL, TRIANGLE_MESH, @@ -784,6 +774,14 @@ namespace wiSceneSystem const wiECS::ComponentManager& transforms, wiECS::ComponentManager& prev_transforms ); + void RunPhysicsUpdateSystem( + wiECS::ComponentManager& transforms, + wiECS::ComponentManager& meshes, + wiECS::ComponentManager& objects, + wiECS::ComponentManager& rigidbodies, + wiECS::ComponentManager& softbodies, + float dt + ); void RunTransformUpdateSystem(wiECS::ComponentManager& transforms); void RunAnimationUpdateSystem( wiECS::ComponentManager& animations, @@ -795,13 +793,6 @@ namespace wiSceneSystem wiECS::ComponentManager& transforms, wiECS::ComponentManager& layers ); - void RunPhysicsUpdateSystem( - wiECS::ComponentManager& transforms, - wiECS::ComponentManager& meshes, - wiECS::ComponentManager& objects, - wiECS::ComponentManager& rigidbodies, - wiECS::ComponentManager& softbodies - ); void RunArmatureUpdateSystem( const wiECS::ComponentManager& transforms, wiECS::ComponentManager& armatures