diff --git a/Editor/MeshWindow.cpp b/Editor/MeshWindow.cpp index c75ea7516..96fa79e75 100644 --- a/Editor/MeshWindow.cpp +++ b/Editor/MeshWindow.cpp @@ -48,7 +48,7 @@ MeshWindow::MeshWindow(wiGUI* gui) : GUI(gui) massSlider->SetSize(XMFLOAT2(100, 30)); massSlider->SetPos(XMFLOAT2(x, y += step)); massSlider->OnSlide([&](wiEventArgs args) { - PhysicsComponent* physicscomponent = wiRenderer::GetScene().physicscomponents.GetComponent(entity); + SoftBodyPhysicsComponent* physicscomponent = wiRenderer::GetScene().softbodies.GetComponent(entity); if (physicscomponent != nullptr) { physicscomponent->mass = args.fValue; @@ -61,7 +61,7 @@ MeshWindow::MeshWindow(wiGUI* gui) : GUI(gui) frictionSlider->SetSize(XMFLOAT2(100, 30)); frictionSlider->SetPos(XMFLOAT2(x, y += step)); frictionSlider->OnSlide([&](wiEventArgs args) { - PhysicsComponent* physicscomponent = wiRenderer::GetScene().physicscomponents.GetComponent(entity); + SoftBodyPhysicsComponent* physicscomponent = wiRenderer::GetScene().softbodies.GetComponent(entity); if (physicscomponent != nullptr) { physicscomponent->friction = args.fValue; @@ -204,7 +204,7 @@ void MeshWindow::SetEntity(Entity entity) impostorDistanceSlider->SetValue(mesh->impostorDistance); tessellationFactorSlider->SetValue(mesh->GetTessellationFactor()); - const PhysicsComponent* physicscomponent = scene.physicscomponents.GetComponent(entity); + SoftBodyPhysicsComponent* physicscomponent = wiRenderer::GetScene().softbodies.GetComponent(entity); if (physicscomponent != nullptr) { massSlider->SetValue(physicscomponent->mass); diff --git a/Editor/ModelImporter_GLTF.cpp b/Editor/ModelImporter_GLTF.cpp index c09fe8fb9..e63b07634 100644 --- a/Editor/ModelImporter_GLTF.cpp +++ b/Editor/ModelImporter_GLTF.cpp @@ -513,14 +513,10 @@ Entity ImportModel_GLTF(const std::string& fileName) state.meshArray.push_back(meshEntity); MeshComponent& mesh = *scene.meshes.GetComponent(meshEntity); - mesh.renderable = true; - int matIndex = -1; for (auto& prim : x.primitives) { - matIndex++; - assert(prim.indices >= 0); // Fill indices: @@ -574,10 +570,6 @@ Entity ImportModel_GLTF(const std::string& fileName) assert(0 && "unsupported index stride!"); } - - bool hasBoneWeights = false; - bool hasBoneIndices = false; - for (auto& attr : prim.attributes) { const string& attr_name = attr.first; @@ -594,6 +586,7 @@ Entity ImportModel_GLTF(const std::string& fileName) if (!attr_name.compare("POSITION")) { + size_t offset = mesh.vertex_positions.size(); mesh.vertex_positions.resize(mesh.vertex_positions.size() + count); assert(stride == 12); for (size_t i = 0; i < count; ++i) @@ -603,6 +596,7 @@ Entity ImportModel_GLTF(const std::string& fileName) } else if (!attr_name.compare("NORMAL")) { + size_t offset = mesh.vertex_normals.size(); mesh.vertex_normals.resize(mesh.vertex_normals.size() + count); assert(stride == 12); for (size_t i = 0; i < count; ++i) @@ -612,6 +606,7 @@ Entity ImportModel_GLTF(const std::string& fileName) } else if (!attr_name.compare("TEXCOORD_0")) { + size_t offset = mesh.vertex_texcoords.size(); mesh.vertex_texcoords.resize(mesh.vertex_texcoords.size() + count); assert(stride == 8); for (size_t i = 0; i < count; ++i) @@ -624,10 +619,10 @@ Entity ImportModel_GLTF(const std::string& fileName) } else if (!attr_name.compare("JOINTS_0")) { + size_t offset = mesh.vertex_boneindices.size(); mesh.vertex_boneindices.resize(mesh.vertex_boneindices.size() + count); if (stride == 4) { - hasBoneIndices = true; struct JointTmp { uint8_t ind[4]; @@ -645,7 +640,6 @@ Entity ImportModel_GLTF(const std::string& fileName) } else if (stride == 8) { - hasBoneIndices = true; struct JointTmp { uint16_t ind[4]; @@ -668,8 +662,8 @@ Entity ImportModel_GLTF(const std::string& fileName) } else if (!attr_name.compare("WEIGHTS_0")) { + size_t offset = mesh.vertex_boneweights.size(); mesh.vertex_boneweights.resize(mesh.vertex_boneweights.size() + count); - hasBoneWeights = true; assert(stride == 16); for (size_t i = 0; i < count; ++i) { diff --git a/Editor/ObjectWindow.cpp b/Editor/ObjectWindow.cpp index 535018645..e9eff77bf 100644 --- a/Editor/ObjectWindow.cpp +++ b/Editor/ObjectWindow.cpp @@ -270,14 +270,14 @@ void ObjectWindow::SetEntity(Entity entity) cascadeMaskSlider->SetValue((float)object->cascadeMask); ditherSlider->SetValue(object->GetTransparency()); - const PhysicsComponent* physicsComponent = scene.physicscomponents.GetComponent(entity); + const RigidBodyPhysicsComponent* physicsComponent = scene.rigidbodies.GetComponent(entity); if (physicsComponent != nullptr) { - if (physicsComponent->rigidBody) - { - simulationTypeComboBox->SetSelected(1); - } + //if (physicsComponent->rigidBody) + //{ + // simulationTypeComboBox->SetSelected(1); + //} //else //{ // if (object->mesh != nullptr) @@ -299,32 +299,32 @@ void ObjectWindow::SetEntity(Entity entity) kinematicCheckBox->SetCheck(physicsComponent->kinematic); - if (!physicsComponent->physicsType.compare("ACTIVE")) - { - physicsTypeComboBox->SetSelected(0); - } - else if (!physicsComponent->physicsType.compare("PASSIVE")) - { - physicsTypeComboBox->SetSelected(1); - } + //if (!physicsComponent->physicsType.compare("ACTIVE")) + //{ + // physicsTypeComboBox->SetSelected(0); + //} + //else if (!physicsComponent->physicsType.compare("PASSIVE")) + //{ + // physicsTypeComboBox->SetSelected(1); + //} - if (!physicsComponent->collisionShape.compare("BOX")) + if (physicsComponent->shape == RigidBodyPhysicsComponent::CollisionShape::BOX) { collisionShapeComboBox->SetSelected(0); } - else if (!physicsComponent->collisionShape.compare("SPHERE")) + else if (physicsComponent->shape == RigidBodyPhysicsComponent::CollisionShape::SPHERE) { collisionShapeComboBox->SetSelected(1); } - else if (!physicsComponent->collisionShape.compare("CAPSULE")) + else if (physicsComponent->shape == RigidBodyPhysicsComponent::CollisionShape::CAPSULE) { collisionShapeComboBox->SetSelected(2); } - else if (!physicsComponent->collisionShape.compare("CONVEX_HULL")) + else if (physicsComponent->shape == RigidBodyPhysicsComponent::CollisionShape::CONVEX_HULL) { collisionShapeComboBox->SetSelected(3); } - else if (!physicsComponent->collisionShape.compare("MESH")) + else if (physicsComponent->shape == RigidBodyPhysicsComponent::CollisionShape::TRIANGLE_MESH) { collisionShapeComboBox->SetSelected(4); } diff --git a/WickedEngine/bvh_classificationCS.hlsl b/WickedEngine/bvh_classificationCS.hlsl index 6ab5ce03a..3e3fac0c8 100644 --- a/WickedEngine/bvh_classificationCS.hlsl +++ b/WickedEngine/bvh_classificationCS.hlsl @@ -97,7 +97,7 @@ void main(uint3 DTid : SV_DispatchThreadID, uint groupIndex : SV_GroupIndex) nor0.x = (float)((nor_u >> 0) & 0x000000FF) / 255.0f * 2.0f - 1.0f; nor0.y = (float)((nor_u >> 8) & 0x000000FF) / 255.0f * 2.0f - 1.0f; nor0.z = (float)((nor_u >> 16) & 0x000000FF) / 255.0f * 2.0f - 1.0f; - materialIndex = (nor_u >> 28) & 0x0000000F; + materialIndex = (nor_u >> 24) & 0x000000FF; } nor_u = asuint(pos_nor1.w); float3 nor1; diff --git a/WickedEngine/wiBULLET.cpp b/WickedEngine/wiBULLET.cpp index d1d9690b7..f3e106098 100644 --- a/WickedEngine/wiBULLET.cpp +++ b/WickedEngine/wiBULLET.cpp @@ -121,7 +121,7 @@ void wiBULLET::setWind(const XMFLOAT3& wind){ } -void wiBULLET::connectVerticesToSoftBody(PhysicsComponent& physicscomponent, MeshComponent& mesh) +void wiBULLET::connectVerticesToSoftBody(wiSceneSystem::SoftBodyPhysicsComponent& physicscomponent, wiSceneSystem::MeshComponent& mesh) { //if (!softBodyPhysicsEnabled) // return; @@ -155,34 +155,34 @@ void wiBULLET::connectVerticesToSoftBody(PhysicsComponent& physicscomponent, Mes // } //} } -void wiBULLET::connectSoftBodyToVertices(PhysicsComponent& physicscomponent, MeshComponent& mesh) +void wiBULLET::connectSoftBodyToVertices(wiSceneSystem::SoftBodyPhysicsComponent& physicscomponent, wiSceneSystem::MeshComponent& mesh) { - if (!softBodyPhysicsEnabled) - return; + //if (!softBodyPhysicsEnabled) + // return; - if (!firstRunWorld) - { - btCollisionObject* obj = bulletPhysics->dynamicsWorld->getCollisionObjectArray()[physicscomponent.physicsObjectID]; - btSoftBody* softBody = btSoftBody::upcast(obj); + //if (!firstRunWorld) + //{ + // btCollisionObject* obj = bulletPhysics->dynamicsWorld->getCollisionObjectArray()[physicscomponent.physicsObjectID]; + // btSoftBody* softBody = btSoftBody::upcast(obj); - if (softBody) { - btSoftBody::tNodeArray& nodes(softBody->m_nodes); + // if (softBody) { + // btSoftBody::tNodeArray& nodes(softBody->m_nodes); - int gvg = physicscomponent.goalVG; - if (gvg >= 0) - { - int j = 0; - for (auto it = mesh.vertexGroups[gvg].vertex_weights.begin(); it != mesh.vertexGroups[gvg].vertex_weights.end(); ++it) - { - int vi = (*it).first; - int index = physicscomponent.physicalmapGP[vi]; - float weight = (*it).second; - nodes[index].m_x = nodes[index].m_x.lerp(btVector3(physicscomponent.goalPositions[j].x, physicscomponent.goalPositions[j].y, physicscomponent.goalPositions[j].z), weight); - ++j; - } - } - } - } + // int gvg = physicscomponent.goalVG; + // if (gvg >= 0) + // { + // int j = 0; + // for (auto it = mesh.vertexGroups[gvg].vertex_weights.begin(); it != mesh.vertexGroups[gvg].vertex_weights.end(); ++it) + // { + // int vi = (*it).first; + // int index = physicscomponent.physicalmapGP[vi]; + // float weight = (*it).second; + // nodes[index].m_x = nodes[index].m_x.lerp(btVector3(physicscomponent.goalPositions[j].x, physicscomponent.goalPositions[j].y, physicscomponent.goalPositions[j].z), weight); + // ++j; + // } + // } + // } + //} } void wiBULLET::transformBody(const XMFLOAT4& rot, const XMFLOAT3& pos, int objectI) { @@ -228,15 +228,20 @@ PHYSICS::PhysicsTransform* wiBULLET::getObject(int index) return transforms[index]; } -void wiBULLET::registerObject(PhysicsComponent& physicscomponent, MeshComponent& mesh, TransformComponent& transform) +void wiBULLET::addRigidBody(wiSceneSystem::RigidBodyPhysicsComponent& physicscomponent, wiSceneSystem::MeshComponent& mesh, wiSceneSystem::TransformComponent& transform) { + if (!rigidBodyPhysicsEnabled) + { + return; + } + btVector3 S = btVector3(transform.scale_local.x, transform.scale_local.y, transform.scale_local.z); btQuaternion R = btQuaternion(transform.rotation_local.x, transform.rotation_local.y, transform.rotation_local.z, transform.rotation_local.z); btVector3 T = btVector3(transform.translation_local.x, transform.translation_local.y, transform.translation_local.z); - if(physicscomponent.rigidBody&& rigidBodyPhysicsEnabled){ - - if(!physicscomponent.collisionShape.compare("BOX")) + switch(physicscomponent.shape) + { + case RigidBodyPhysicsComponent::CollisionShape::BOX: { btCollisionShape* shape = new btBoxShape(S); shape->setMargin(btScalar(0.05)); @@ -286,7 +291,9 @@ void wiBULLET::registerObject(PhysicsComponent& physicscomponent, MeshComponent& } } } - if(!physicscomponent.collisionShape.compare("SPHERE")) + break; + + case RigidBodyPhysicsComponent::CollisionShape::SPHERE: { btCollisionShape* shape = new btSphereShape(btScalar(S.x())); shape->setMargin(btScalar(0.05)); @@ -336,7 +343,9 @@ void wiBULLET::registerObject(PhysicsComponent& physicscomponent, MeshComponent& } } - if(!physicscomponent.collisionShape.compare("CAPSULE")) + break; + + case RigidBodyPhysicsComponent::CollisionShape::CAPSULE: { btCollisionShape* shape = new btCapsuleShape(btScalar(S.x()), btScalar(S.y())); shape->setMargin(btScalar(0.05)); @@ -386,7 +395,9 @@ void wiBULLET::registerObject(PhysicsComponent& physicscomponent, MeshComponent& } } } - if(!physicscomponent.collisionShape.compare("CONVEX_HULL")) + break; + + case RigidBodyPhysicsComponent::CollisionShape::CONVEX_HULL: { btCollisionShape* shape = new btConvexHullShape(); for (auto& pos : mesh.vertex_positions) @@ -442,7 +453,9 @@ void wiBULLET::registerObject(PhysicsComponent& physicscomponent, MeshComponent& } } } - if(!physicscomponent.collisionShape.compare("MESH")) + break; + + case RigidBodyPhysicsComponent::CollisionShape::TRIANGLE_MESH: { int totalVerts = (int)mesh.vertex_positions.size(); int totalTriangles = (int)mesh.indices.size() / 3; @@ -455,9 +468,10 @@ void wiBULLET::registerObject(PhysicsComponent& physicscomponent, MeshComponent& } int* btInd = new int[mesh.indices.size()]; - for (unsigned int i = 0; i < mesh.indices.size(); ++i) + i = 0; + for (auto& ind : mesh.indices) { - btInd[i] = mesh.indices[i]; + btInd[i++] = ind; } int vertStride = sizeof(btVector3); @@ -526,45 +540,60 @@ void wiBULLET::registerObject(PhysicsComponent& physicscomponent, MeshComponent& } } - if(physicscomponent.softBody && softBodyPhysicsEnabled) + + + physicscomponent.physicsObjectID = ++registeredObjects; +} +void wiBULLET::addSoftBody(wiSceneSystem::SoftBodyPhysicsComponent& physicscomponent, wiSceneSystem::MeshComponent& mesh, wiSceneSystem::TransformComponent& transform) +{ + if (!softBodyPhysicsEnabled) { - const int vCount = (int)physicscomponent.physicsverts.size(); - btScalar* btVerts = new btScalar[vCount*3]; - for(int i=0;idynamicsWorld)->getWorldInfo() - ,&btVerts[0] - ,&btInd[0] - ,tCount - ,false + ((btSoftRigidDynamicsWorld*)bulletPhysics->dynamicsWorld)->getWorldInfo() + , &btVerts[0] + , &btInd[0] + , tCount + , false ); - + delete[] btVerts; delete[] btInd; - - if(softBody){ - btSoftBody::Material* pm=softBody->appendMaterial(); + + if (softBody) + { + btSoftBody::Material* pm = softBody->appendMaterial(); pm->m_kLST = 0.5; pm->m_kVST = 0.5; pm->m_kAST = 0.5; pm->m_flags = 0; - softBody->generateBendingConstraints(2,pm); + softBody->generateBendingConstraints(2, pm); softBody->randomizeConstraints(); - + btTransform shapeTransform; shapeTransform.setIdentity(); shapeTransform.setOrigin(T); @@ -573,60 +602,60 @@ void wiBULLET::registerObject(PhysicsComponent& physicscomponent, MeshComponent& softBody->transform(shapeTransform); - softBody->m_cfg.piterations = softBodyIterationCount; - softBody->m_cfg.aeromodel=btSoftBody::eAeroModel::F_TwoSidedLiftDrag; + softBody->m_cfg.piterations = softBodyIterationCount; + softBody->m_cfg.aeromodel = btSoftBody::eAeroModel::F_TwoSidedLiftDrag; - softBody->m_cfg.kAHR =btScalar(.69); //0.69 Anchor hardness [0,1] - softBody->m_cfg.kCHR =btScalar(1.0); //1 Rigid contact hardness [0,1] - softBody->m_cfg.kDF =btScalar(0.2); //0.2 Dynamic friction coefficient [0,1] - softBody->m_cfg.kDG =btScalar(0.01); //0 Drag coefficient [0,+inf] - softBody->m_cfg.kDP =btScalar(0.0); //0 Damping coefficient [0,1] - softBody->m_cfg.kKHR =btScalar(0.1); //0.1 Kinetic contact hardness [0,1] - softBody->m_cfg.kLF =btScalar(0.1); //0 Lift coefficient [0,+inf] - softBody->m_cfg.kMT =btScalar(0.0); //0 Pose matching coefficient [0,1] - softBody->m_cfg.kPR =btScalar(0.0); //0 Pressure coefficient [-1,1] - softBody->m_cfg.kSHR =btScalar(1.0); //1 Soft contacts hardness [0,1] - softBody->m_cfg.kVC =btScalar(0.0); //0 Volume conseration coefficient [0,+inf] - softBody->m_cfg.kVCF =btScalar(1.0); //1 Velocities correction factor (Baumgarte) + softBody->m_cfg.kAHR = btScalar(.69); //0.69 Anchor hardness [0,1] + softBody->m_cfg.kCHR = btScalar(1.0); //1 Rigid contact hardness [0,1] + softBody->m_cfg.kDF = btScalar(physicscomponent.friction); //0.2 Dynamic friction coefficient [0,1] + softBody->m_cfg.kDG = btScalar(0.01); //0 Drag coefficient [0,+inf] + softBody->m_cfg.kDP = btScalar(0.0); //0 Damping coefficient [0,1] + softBody->m_cfg.kKHR = btScalar(0.1); //0.1 Kinetic contact hardness [0,1] + softBody->m_cfg.kLF = btScalar(0.1); //0 Lift coefficient [0,+inf] + softBody->m_cfg.kMT = btScalar(0.0); //0 Pose matching coefficient [0,1] + softBody->m_cfg.kPR = btScalar(0.0); //0 Pressure coefficient [-1,1] + softBody->m_cfg.kSHR = btScalar(1.0); //1 Soft contacts hardness [0,1] + softBody->m_cfg.kVC = btScalar(0.0); //0 Volume conseration coefficient [0,+inf] + softBody->m_cfg.kVCF = btScalar(1.0); //1 Velocities correction factor (Baumgarte) + + softBody->m_cfg.kSKHR_CL = btScalar(1.0); //1 Soft vs. kinetic hardness [0,1] + softBody->m_cfg.kSK_SPLT_CL = btScalar(0.5); //0.5 Soft vs. rigid impulse split [0,1] + softBody->m_cfg.kSRHR_CL = btScalar(0.1); //0.1 Soft vs. rigid hardness [0,1] + softBody->m_cfg.kSR_SPLT_CL = btScalar(0.5); //0.5 Soft vs. rigid impulse split [0,1] + softBody->m_cfg.kSSHR_CL = btScalar(0.5); //0.5 Soft vs. soft hardness [0,1] + softBody->m_cfg.kSS_SPLT_CL = btScalar(0.5); //0.5 Soft vs. rigid impulse split [0,1] - softBody->m_cfg.kSKHR_CL =btScalar(1.0); //1 Soft vs. kinetic hardness [0,1] - softBody->m_cfg.kSK_SPLT_CL =btScalar(0.5); //0.5 Soft vs. rigid impulse split [0,1] - softBody->m_cfg.kSRHR_CL =btScalar(0.1); //0.1 Soft vs. rigid hardness [0,1] - softBody->m_cfg.kSR_SPLT_CL =btScalar(0.5); //0.5 Soft vs. rigid impulse split [0,1] - softBody->m_cfg.kSSHR_CL =btScalar(0.5); //0.5 Soft vs. soft hardness [0,1] - softBody->m_cfg.kSS_SPLT_CL =btScalar(0.5); //0.5 Soft vs. rigid impulse split [0,1] - btScalar mass = btScalar(physicscomponent.mass); softBody->setTotalMass(mass); - - int mvg = physicscomponent.massVG; - if(mvg>=0){ - for(auto it=mesh.vertexGroups[mvg].vertex_weights.begin();it!=mesh.vertexGroups[mvg].vertex_weights.end();++it){ - int vi = (*it).first; - float wei = (*it).second; - int index= physicscomponent.physicalmapGP[vi]; - softBody->setMass(index,softBody->getMass(index)*btScalar(wei)); - } - } - - int gvg = physicscomponent.goalVG; - if(gvg>=0){ - for(auto it=mesh.vertexGroups[gvg].vertex_weights.begin();it!=mesh.vertexGroups[gvg].vertex_weights.end();++it){ - int vi = (*it).first; - int index= physicscomponent.physicalmapGP[vi]; - float weight = (*it).second; - if(weight==1) - softBody->setMass(index,0); - } - } + //int mvg = physicscomponent.massVG; + //if (mvg >= 0) { + // for (auto it = mesh.vertexGroups[mvg].vertex_weights.begin(); it != mesh.vertexGroups[mvg].vertex_weights.end(); ++it) { + // int vi = (*it).first; + // float wei = (*it).second; + // int index = physicscomponent.physicalmapGP[vi]; + // softBody->setMass(index, softBody->getMass(index)*btScalar(wei)); + // } + //} + + + //int gvg = physicscomponent.goalVG; + //if (gvg >= 0) { + // for (auto it = mesh.vertexGroups[gvg].vertex_weights.begin(); it != mesh.vertexGroups[gvg].vertex_weights.end(); ++it) { + // int vi = (*it).first; + // int index = physicscomponent.physicalmapGP[vi]; + // float weight = (*it).second; + // if (weight == 1) + // softBody->setMass(index, 0); + // } + //} softBody->getCollisionShape()->setMargin(btScalar(0.2)); softBody->setWindVelocity(bulletPhysics->wind); - - softBody->setPose(true,true); + + softBody->setPose(true, true); softBody->setActivationState(DISABLE_DEACTIVATION); @@ -638,15 +667,26 @@ void wiBULLET::registerObject(PhysicsComponent& physicscomponent, MeshComponent& physicscomponent.physicsObjectID = ++registeredObjects; } -void wiBULLET::removeObject(PhysicsComponent& physicscomponent) +void wiBULLET::removeRigidBody(wiSceneSystem::RigidBodyPhysicsComponent& physicscomponent) { - if (physicscomponent.physicsObjectID < 0) { return; } deleteObject(physicscomponent.physicsObjectID); + + physicscomponent.physicsObjectID = -1; +} +void wiBULLET::removeSoftBody(wiSceneSystem::SoftBodyPhysicsComponent& physicscomponent) +{ + if (physicscomponent.physicsObjectID < 0) + { + return; + } + + deleteObject(physicscomponent.physicsObjectID); + physicscomponent.physicsObjectID = -1; } diff --git a/WickedEngine/wiBULLET.h b/WickedEngine/wiBULLET.h index 0b4061f99..9e588ca72 100644 --- a/WickedEngine/wiBULLET.h +++ b/WickedEngine/wiBULLET.h @@ -17,16 +17,17 @@ public: virtual void setWind(const XMFLOAT3& wind) override; - virtual void connectVerticesToSoftBody(wiSceneSystem::PhysicsComponent& physicscomponent, wiSceneSystem::MeshComponent& mesh) override; - virtual void connectSoftBodyToVertices(wiSceneSystem::PhysicsComponent& physicscomponent, wiSceneSystem::MeshComponent& mesh) override; + virtual void connectVerticesToSoftBody(wiSceneSystem::SoftBodyPhysicsComponent& physicscomponent, wiSceneSystem::MeshComponent& mesh) override; + virtual void connectSoftBodyToVertices(wiSceneSystem::SoftBodyPhysicsComponent& physicscomponent, wiSceneSystem::MeshComponent& mesh) override; virtual void transformBody(const XMFLOAT4& rot, const XMFLOAT3& pos, int objectI) override; virtual PhysicsTransform* getObject(int index) override; - // add object to the simulation - virtual void registerObject(wiSceneSystem::PhysicsComponent& physicscomponent, wiSceneSystem::MeshComponent& mesh, wiSceneSystem::TransformComponent& transform) override; - // remove object from simulation - virtual void removeObject(wiSceneSystem::PhysicsComponent& physicscomponent) override; + virtual void addRigidBody(wiSceneSystem::RigidBodyPhysicsComponent& physicscomponent, wiSceneSystem::MeshComponent& mesh, wiSceneSystem::TransformComponent& transform) override; + virtual void addSoftBody(wiSceneSystem::SoftBodyPhysicsComponent& physicscomponent, wiSceneSystem::MeshComponent& mesh, wiSceneSystem::TransformComponent& transform) override; + + virtual void removeRigidBody(wiSceneSystem::RigidBodyPhysicsComponent& physicscomponent) override; + virtual void removeSoftBody(wiSceneSystem::SoftBodyPhysicsComponent& physicscomponent) override; virtual void Update(float dt) override; virtual void ClearWorld() override; diff --git a/WickedEngine/wiPHYSICS.h b/WickedEngine/wiPHYSICS.h index ebfed0307..2d053a3a3 100644 --- a/WickedEngine/wiPHYSICS.h +++ b/WickedEngine/wiPHYSICS.h @@ -37,14 +37,15 @@ public: virtual void setWind(const XMFLOAT3& wind)=0; - virtual void connectVerticesToSoftBody(wiSceneSystem::PhysicsComponent& physicscomponent, wiSceneSystem::MeshComponent& mesh) = 0; - virtual void connectSoftBodyToVertices(wiSceneSystem::PhysicsComponent& physicscomponent, wiSceneSystem::MeshComponent& mesh) = 0; + virtual void connectVerticesToSoftBody(wiSceneSystem::SoftBodyPhysicsComponent& physicscomponent, wiSceneSystem::MeshComponent& mesh) = 0; + virtual void connectSoftBodyToVertices(wiSceneSystem::SoftBodyPhysicsComponent& physicscomponent, wiSceneSystem::MeshComponent& mesh) = 0; virtual void transformBody(const XMFLOAT4& rot, const XMFLOAT3& pos, int objectI) = 0; virtual PhysicsTransform* getObject(int index)=0; - // add object to the simulation - virtual void registerObject(wiSceneSystem::PhysicsComponent& physicscomponent, wiSceneSystem::MeshComponent& mesh, wiSceneSystem::TransformComponent& transform) = 0; - // remove object from simulation - virtual void removeObject(wiSceneSystem::PhysicsComponent& physicscomponent) = 0; + virtual void addRigidBody(wiSceneSystem::RigidBodyPhysicsComponent& physicscomponent, wiSceneSystem::MeshComponent& mesh, wiSceneSystem::TransformComponent& transform) = 0; + virtual void addSoftBody(wiSceneSystem::SoftBodyPhysicsComponent& physicscomponent, wiSceneSystem::MeshComponent& mesh, wiSceneSystem::TransformComponent& transform) = 0; + + virtual void removeRigidBody(wiSceneSystem::RigidBodyPhysicsComponent& physicscomponent) = 0; + virtual void removeSoftBody(wiSceneSystem::SoftBodyPhysicsComponent& physicscomponent) = 0; }; diff --git a/WickedEngine/wiSceneSystem.cpp b/WickedEngine/wiSceneSystem.cpp index 35027f91f..2912c11f7 100644 --- a/WickedEngine/wiSceneSystem.cpp +++ b/WickedEngine/wiSceneSystem.cpp @@ -284,7 +284,8 @@ namespace wiSceneSystem for (size_t i = 0; i < vertices.size(); ++i) { const XMFLOAT3& pos = vertex_positions[i]; - const XMFLOAT3& nor = vertex_normals[i]; + XMFLOAT3& nor = vertex_normals[i]; + XMStoreFloat3(&nor, XMVector3Normalize(XMLoadFloat3(&nor))); uint32_t subsetIndex = subsetIndicesLUT[(uint32_t)i]; vertices[i].FromFULL(pos, nor, subsetIndex); @@ -356,7 +357,7 @@ namespace wiSceneSystem assert(SUCCEEDED(hr)); } - // texture coordinate buffers: + // vertexBuffer - TEXCOORDS { std::vector vertices(vertex_texcoords.size()); for (size_t i = 0; i < vertices.size(); ++i) @@ -381,176 +382,232 @@ namespace wiSceneSystem } } - void MeshComponent::RemoveVertex(size_t index) - { - for (size_t ind = 0; ind < indices.size(); ++ind) - { - if (indices[ind] == index) - { - indices[ind] = static_cast(index); - } - else if (indices[ind] > index && indices[ind] > 0) - { - indices[ind]--; - } - } - - vertex_positions.erase(vertex_positions.begin() + index); - vertex_normals.erase(vertex_normals.begin() + index); - vertex_texcoords.erase(vertex_texcoords.begin() + index); - vertex_boneindices.erase(vertex_boneindices.begin() + index); - vertex_boneweights.erase(vertex_boneweights.begin() + index); - } void MeshComponent::ComputeNormals(bool smooth) { - // Start recalculating normals: + assert(0); // todo: fix - if (smooth) - { - // Compute smooth surface normals: + //std::vector vertex_subsetindices(vertex_positions.size()); + //uint32_t subsetCounter = 0; + //for (auto& subset : subsets) + //{ + // for (uint32_t i = 0; i < subset.indexCount; ++i) + // { + // uint32_t index = indices[subset.indexOffset + i]; + // vertex_subsetindices[index] = subsetCounter; + // } + // subsetCounter++; + //} - // 1.) Zero normals, they will be averaged later - for (size_t i = 0; i < vertex_normals.size() - 1; i++) - { - vertex_normals[i] = XMFLOAT3(0, 0, 0); - } + //// Start recalculating normals: - // 2.) Find identical vertices by POSITION, accumulate face normals - for (size_t i = 0; i < vertex_positions.size() - 1; i++) - { - XMFLOAT3& v_search = vertex_positions[i]; - XMFLOAT3& v_search_nor = vertex_normals[i]; + //if (smooth) + //{ + // // Compute smooth surface normals: - for (size_t ind = 0; ind < indices.size() / 3; ++ind) - { - uint32_t i0 = indices[ind * 3 + 0]; - uint32_t i1 = indices[ind * 3 + 1]; - uint32_t i2 = indices[ind * 3 + 2]; + // // 1.) Zero normals, they will be averaged later + // for (size_t i = 0; i < vertex_normals.size(); i++) + // { + // vertex_normals[i] = XMFLOAT3(0, 0, 0); + // } - XMFLOAT3& v0 = vertex_positions[i0]; - XMFLOAT3& v1 = vertex_positions[i1]; - XMFLOAT3& v2 = vertex_positions[i2]; + // // 2.) Find identical vertices by POSITION, accumulate face normals + // for (size_t i = 0; i < vertex_positions.size(); i++) + // { + // XMFLOAT3& v_search_pos = vertex_positions[i]; - bool match_pos0 = - fabs(v_search.x - v0.x) < FLT_EPSILON && - fabs(v_search.y - v0.y) < FLT_EPSILON && - fabs(v_search.z - v0.z) < FLT_EPSILON; + // for (size_t ind = 0; ind < indices.size() / 3; ++ind) + // { + // uint32_t i0 = indices[ind * 3 + 0]; + // uint32_t i1 = indices[ind * 3 + 1]; + // uint32_t i2 = indices[ind * 3 + 2]; - bool match_pos1 = - fabs(v_search.x - v1.x) < FLT_EPSILON && - fabs(v_search.y - v1.y) < FLT_EPSILON && - fabs(v_search.z - v1.z) < FLT_EPSILON; + // XMFLOAT3& v0 = vertex_positions[i0]; + // XMFLOAT3& v1 = vertex_positions[i1]; + // XMFLOAT3& v2 = vertex_positions[i2]; - bool match_pos2 = - fabs(v_search.x - v2.x) < FLT_EPSILON && - fabs(v_search.y - v2.y) < FLT_EPSILON && - fabs(v_search.z - v2.z) < FLT_EPSILON; + // bool match_pos0 = + // fabs(v_search_pos.x - v0.x) < FLT_EPSILON && + // fabs(v_search_pos.y - v0.y) < FLT_EPSILON && + // fabs(v_search_pos.z - v0.z) < FLT_EPSILON; - if (match_pos0 || match_pos1 || match_pos2) - { - XMVECTOR U = XMLoadFloat3(&v2) - XMLoadFloat3(&v0); - XMVECTOR V = XMLoadFloat3(&v1) - XMLoadFloat3(&v0); + // bool match_pos1 = + // fabs(v_search_pos.x - v1.x) < FLT_EPSILON && + // fabs(v_search_pos.y - v1.y) < FLT_EPSILON && + // fabs(v_search_pos.z - v1.z) < FLT_EPSILON; - XMVECTOR N = XMVector3Cross(U, V); - N = XMVector3Normalize(N); + // bool match_pos2 = + // fabs(v_search_pos.x - v2.x) < FLT_EPSILON && + // fabs(v_search_pos.y - v2.y) < FLT_EPSILON && + // fabs(v_search_pos.z - v2.z) < FLT_EPSILON; - XMFLOAT3 normal; - XMStoreFloat3(&normal, N); + // if (match_pos0 || match_pos1 || match_pos2) + // { + // XMVECTOR U = XMLoadFloat3(&v2) - XMLoadFloat3(&v0); + // XMVECTOR V = XMLoadFloat3(&v1) - XMLoadFloat3(&v0); - v_search_nor.x += normal.x; - v_search_nor.y += normal.y; - v_search_nor.z += normal.z; - } + // XMVECTOR N = XMVector3Cross(U, V); + // N = XMVector3Normalize(N); - } - } + // XMFLOAT3 normal; + // XMStoreFloat3(&normal, N); - std::unordered_map subsetIndicesLUT; - uint32_t subsetCounter = 0; - for (auto& subset : subsets) - { - for (uint32_t i = 0; i < subset.indexCount; ++i) - { - uint32_t index = indices[subset.indexOffset + i]; - subsetIndicesLUT[index] = subsetCounter; - } - subsetCounter++; - } + // vertex_normals[i].x += normal.x; + // vertex_normals[i].y += normal.y; + // vertex_normals[i].z += normal.z; + // } - // 3.) Find unique vertices by POSITION and TEXCOORD and MATERIAL and remove duplicates - for (size_t i = 0; i < vertex_positions.size() - 1; i++) - { - const XMFLOAT3& p0 = vertex_positions[i]; - const XMFLOAT3& n0 = vertex_normals[i]; - const XMFLOAT2& t0 = vertex_texcoords[i]; - const uint32_t s0 = subsetIndicesLUT[(uint32_t)i]; + // } + // } - for (size_t j = i + 1; j < vertex_positions.size(); j++) - { - const XMFLOAT3& p1 = vertex_positions[j]; - const XMFLOAT3& n1 = vertex_normals[j]; - const XMFLOAT2& t1 = vertex_texcoords[j]; - const uint32_t s1 = subsetIndicesLUT[(uint32_t)j]; + // // 3.) Find unique vertices by POSITION and TEXCOORD and MATERIAL and remove duplicates + // for (size_t i = 0; i < vertex_positions.size() - 1; i++) + // { + // const XMFLOAT3& p0 = vertex_positions[i]; + // const XMFLOAT3& n0 = vertex_normals[i]; + // const XMFLOAT2& t0 = vertex_texcoords[i]; + // const uint32_t s0 = vertex_subsetindices[i]; - bool unique_pos = - fabs(p0.x - p1.x) < FLT_EPSILON && - fabs(p0.y - p1.y) < FLT_EPSILON && - fabs(p0.z - p1.z) < FLT_EPSILON; + // for (size_t j = i + 1; j < vertex_positions.size(); j++) + // { + // const XMFLOAT3& p1 = vertex_positions[j]; + // const XMFLOAT3& n1 = vertex_normals[j]; + // const XMFLOAT2& t1 = vertex_texcoords[j]; + // const uint32_t s1 = vertex_subsetindices[j]; - bool unique_tex = - fabs(t0.x - t1.x) < FLT_EPSILON && - fabs(t0.y - t1.y) < FLT_EPSILON && - s0 == s1; + // bool unique_pos = + // fabs(p0.x - p1.x) < FLT_EPSILON && + // fabs(p0.y - p1.y) < FLT_EPSILON && + // fabs(p0.z - p1.z) < FLT_EPSILON; - if (unique_pos && unique_tex) - { - RemoveVertex(j); - } + // bool unique_tex = + // fabs(t0.x - t1.x) < FLT_EPSILON && + // fabs(t0.y - t1.y) < FLT_EPSILON && + // s0 == s1; - } - } - } - else - { - // Compute hard surface normals: + // if (unique_pos && unique_tex) + // { + // for (size_t ind = 0; ind < indices.size(); ++ind) + // { + // if (indices[ind] == j) + // { + // indices[ind] = static_cast(i); + // } + // else if (indices[ind] > j && indices[ind] > 0) + // { + // indices[ind]--; + // } + // } - std::vector newIndexBuffer; - std::vector newVertexBuffer; + // if (j < vertex_subsetindices.size()) + // { + // vertex_subsetindices.erase(vertex_subsetindices.begin() + j); + // } - for (size_t face = 0; face < indices.size() / 3; face++) - { - uint32_t i0 = indices[face * 3 + 0]; - uint32_t i1 = indices[face * 3 + 1]; - uint32_t i2 = indices[face * 3 + 2]; + // if (j < vertex_positions.size()) + // { + // vertex_positions.erase(vertex_positions.begin() + j); + // } + // if (j < vertex_normals.size()) + // { + // vertex_normals.erase(vertex_normals.begin() + j); + // } + // if (j < vertex_texcoords.size()) + // { + // vertex_texcoords.erase(vertex_texcoords.begin() + j); + // } + // if (j < vertex_boneindices.size()) + // { + // vertex_boneindices.erase(vertex_boneindices.begin() + j); + // } + // if (j < vertex_boneweights.size()) + // { + // vertex_boneweights.erase(vertex_boneweights.begin() + j); + // } + // } - XMFLOAT3& p0 = vertex_positions[i0]; - XMFLOAT3& p1 = vertex_positions[i1]; - XMFLOAT3& p2 = vertex_positions[i2]; + // } + // } + //} + //else + //{ + // // Compute hard surface normals: - XMVECTOR U = XMLoadFloat3(&p2) - XMLoadFloat3(&p0); - XMVECTOR V = XMLoadFloat3(&p1) - XMLoadFloat3(&p0); + // std::vector newIndexBuffer; + // std::vector newPositionsBuffer; + // std::vector newNormalsBuffer; + // std::vector newTexcoordsBuffer; + // std::vector newSubsetIndexBuffer; - XMVECTOR N = XMVector3Cross(U, V); - N = XMVector3Normalize(N); + // for (size_t face = 0; face < indices.size() / 3; face++) + // { + // uint32_t i0 = indices[face * 3 + 0]; + // uint32_t i1 = indices[face * 3 + 1]; + // uint32_t i2 = indices[face * 3 + 2]; - XMFLOAT3 normal; - XMStoreFloat3(&normal, N); + // XMFLOAT3& p0 = vertex_positions[i0]; + // XMFLOAT3& p1 = vertex_positions[i1]; + // XMFLOAT3& p2 = vertex_positions[i2]; - newVertexBuffer.push_back(normal); - newVertexBuffer.push_back(normal); - newVertexBuffer.push_back(normal); + // XMVECTOR U = XMLoadFloat3(&p2) - XMLoadFloat3(&p0); + // XMVECTOR V = XMLoadFloat3(&p1) - XMLoadFloat3(&p0); - newIndexBuffer.push_back(static_cast(newIndexBuffer.size())); - newIndexBuffer.push_back(static_cast(newIndexBuffer.size())); - newIndexBuffer.push_back(static_cast(newIndexBuffer.size())); - } + // XMVECTOR N = XMVector3Cross(U, V); + // N = XMVector3Normalize(N); - // For hard surface normals, we created a new mesh in the previous loop through faces, so swap data: - vertex_normals = newVertexBuffer; - indices = newIndexBuffer; - } + // XMFLOAT3 normal; + // XMStoreFloat3(&normal, N); - CreateRenderData(); + // newPositionsBuffer.push_back(p0); + // newPositionsBuffer.push_back(p0); + // newPositionsBuffer.push_back(p0); + + // newNormalsBuffer.push_back(normal); + // newNormalsBuffer.push_back(normal); + // newNormalsBuffer.push_back(normal); + + // newTexcoordsBuffer.push_back(vertex_texcoords[i0]); + // newTexcoordsBuffer.push_back(vertex_texcoords[i0]); + // newTexcoordsBuffer.push_back(vertex_texcoords[i0]); + + // newSubsetIndexBuffer.push_back(vertex_subsetindices[i0]); + // newSubsetIndexBuffer.push_back(vertex_subsetindices[i0]); + // newSubsetIndexBuffer.push_back(vertex_subsetindices[i0]); + + // newIndexBuffer.push_back(static_cast(newIndexBuffer.size())); + // newIndexBuffer.push_back(static_cast(newIndexBuffer.size())); + // newIndexBuffer.push_back(static_cast(newIndexBuffer.size())); + // } + + // // For hard surface normals, we created a new mesh in the previous loop through faces, so swap data: + // vertex_positions = newPositionsBuffer; + // vertex_normals = newNormalsBuffer; + // vertex_texcoords = newTexcoordsBuffer; + // indices = newIndexBuffer; + + // for (auto& subset : subsets) + // { + // subset.indexCount = 0; + // subset.indexOffset = 0; + // } + // for (size_t i = 0; i < indices.size(); ++i) + // { + // uint32_t index = indices[i]; + // uint32_t subsetIndex = vertex_subsetindices[index]; + // subsets[subsetIndex].indexCount++; + // } + // if (subsets.size() > 1) + // { + // for (size_t i = 0; i < subsets.size() - 1; ++i) + // { + // subsets[i + 1].indexOffset = subsets[i].indexOffset + subsets[i].indexCount; + // } + // } + //} + + //// Restore subsets: + + + //CreateRenderData(); } void MeshComponent::FlipCulling() { @@ -675,7 +732,7 @@ namespace wiSceneSystem RunArmatureUpdateSystem(transforms, armatures); - RunPhysicsUpdateSystem(transforms, meshes, objects, physicscomponents); + RunPhysicsUpdateSystem(transforms, meshes, objects, rigidbodies, softbodies); RunMaterialUpdateSystem(materials, dt); @@ -702,7 +759,8 @@ namespace wiSceneSystem materials.Remove(entity); meshes.Remove(entity); objects.Remove(entity); - physicscomponents.Remove(entity); + rigidbodies.Remove(entity); + softbodies.Remove(entity); cullables.Remove(entity); armatures.Remove(entity); lights.Remove(entity); @@ -729,7 +787,8 @@ namespace wiSceneSystem materials.Remove(entity); meshes.Remove(entity); objects.Remove(entity); - physicscomponents.Remove(entity); + rigidbodies.Remove(entity); + softbodies.Remove(entity); cullables.Remove(entity); armatures.Remove(entity); lights.Remove(entity); @@ -744,7 +803,7 @@ namespace wiSceneSystem { for (size_t i = 0; i < names.GetCount(); ++i) { - if (strcmp(names[i].name, name.c_str()) == 0) + if (names[i] == name) { return names.GetEntity(i); } @@ -1257,19 +1316,24 @@ namespace wiSceneSystem ComponentManager& transforms, ComponentManager& meshes, ComponentManager& objects, - ComponentManager& physicscomponents + ComponentManager& rigidbodies, + ComponentManager& softbodies ) { - for (size_t i = 0; i < physicscomponents.GetCount(); ++i) + for (size_t i = 0; i < rigidbodies.GetCount(); ++i) { - PhysicsComponent& physicscomponent = physicscomponents[i]; - Entity entity = physicscomponents.GetEntity(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; - if (physicscomponent.softBody) - { - MeshComponent& mesh = *meshes.GetComponent(entity); - mesh.dynamicVB = true; - } } } void RunMaterialUpdateSystem(ComponentManager& materials, float dt) diff --git a/WickedEngine/wiSceneSystem.h b/WickedEngine/wiSceneSystem.h index 8bf2678f8..f6c5112aa 100644 --- a/WickedEngine/wiSceneSystem.h +++ b/WickedEngine/wiSceneSystem.h @@ -24,6 +24,7 @@ namespace wiSceneSystem char name[128]; inline void operator=(const std::string& str) { strcpy_s(name, str.c_str()); } + inline bool operator==(const std::string& str) const { return strcmp(name, str.c_str()) == 0; } }; struct LayerComponent @@ -319,7 +320,6 @@ namespace wiSceneSystem inline bool IsDynamicVB() const { return dynamicVB; } void CreateRenderData(); - void RemoveVertex(size_t index); void ComputeNormals(bool smooth); void FlipCulling(); void FlipNormals(); @@ -360,30 +360,32 @@ namespace wiSceneSystem inline uint32_t GetRenderTypes() const { return rendertypeMask; } }; - struct PhysicsComponent + struct RigidBodyPhysicsComponent { - std::string collisionShape; - std::string physicsType; - + enum class CollisionShape + { + SPHERE, + BOX, + CAPSULE, + CONVEX_HULL, + TRIANGLE_MESH, + }; + CollisionShape shape; int physicsObjectID = -1; - bool rigidBody = false; - bool softBody = false; bool kinematic = false; float mass = 1.0f; float friction = 1.0f; float restitution = 1.0f; float damping = 1.0f; + }; - int massVG; // vertex group ID for masses - int goalVG; // vertex group ID for soft body goals (how much the vertex is driven by anim/other than physics systems) - int softVG; // vertex group ID for soft body softness (how much to blend in soft body sim to a vertex) - std::vector goalPositions; - std::vector goalNormals; - - std::vector physicsverts; - std::vector physicsverts_prev; + struct SoftBodyPhysicsComponent + { + int physicsObjectID = -1; + float mass = 1.0f; + float friction = 1.0f; + std::vector physicsvertices; std::vector physicsindices; - std::vector physicalmapGP; }; struct ArmatureComponent @@ -686,7 +688,8 @@ namespace wiSceneSystem wiECS::ComponentManager materials; wiECS::ComponentManager meshes; wiECS::ComponentManager objects; - wiECS::ComponentManager physicscomponents; + wiECS::ComponentManager rigidbodies; + wiECS::ComponentManager softbodies; wiECS::ComponentManager cullables; wiECS::ComponentManager armatures; wiECS::ComponentManager lights; @@ -796,7 +799,8 @@ namespace wiSceneSystem wiECS::ComponentManager& transforms, wiECS::ComponentManager& meshes, wiECS::ComponentManager& objects, - wiECS::ComponentManager& physicscomponents + wiECS::ComponentManager& rigidbodies, + wiECS::ComponentManager& softbodies ); void RunArmatureUpdateSystem( const wiECS::ComponentManager& transforms, diff --git a/WickedEngine/wiSceneSystem_Decl.h b/WickedEngine/wiSceneSystem_Decl.h index a0a4d8a75..9944d4a1b 100644 --- a/WickedEngine/wiSceneSystem_Decl.h +++ b/WickedEngine/wiSceneSystem_Decl.h @@ -11,7 +11,8 @@ namespace wiSceneSystem struct MaterialComponent; struct MeshComponent; struct ObjectComponent; - struct PhysicsComponent; + struct RigidBodyPhysicsComponent; + struct SoftBodyPhysicsComponent; struct CullableComponent; struct ArmatureComponent; struct LightComponent;