From 6ac0a29cbfd4b884272cf644225fe12efd736acb Mon Sep 17 00:00:00 2001 From: turanszkij Date: Fri, 7 Sep 2018 21:04:40 +0100 Subject: [PATCH] big mesh refactor --- Editor/MeshWindow.cpp | 2 +- Editor/ModelImporter_GLTF.cpp | 92 ++-- Editor/ModelImporter_OBJ.cpp | 46 +- WickedEngine/WickedEngine_SHADERS.vcxproj | 1 - .../WickedEngine_SHADERS.vcxproj.filters | 3 - WickedEngine/cubeShadowVS.hlsl | 2 - WickedEngine/cubeShadowVS_alphatest.hlsl | 3 - WickedEngine/objectHF.hlsli | 1 - WickedEngine/objectInputLayoutHF.hlsli | 16 +- WickedEngine/objectVS_common.hlsl | 3 - .../objectVS_common_tessellation.hlsl | 4 - WickedEngine/objectVS_positionstream.hlsl | 6 +- WickedEngine/objectVS_simple.hlsl | 2 - .../objectVS_simple_tessellation.hlsl | 2 - WickedEngine/shadowVS.hlsl | 3 - WickedEngine/shadowVS_alphatest.hlsl | 4 +- WickedEngine/shadowVS_transparent.hlsl | 2 - WickedEngine/skinningCS.hlsl | 4 +- WickedEngine/wiBULLET.cpp | 59 +-- WickedEngine/wiRenderer.cpp | 376 +++++++------- WickedEngine/wiRenderer.h | 2 +- WickedEngine/wiSceneSystem.cpp | 471 ++++++++---------- WickedEngine/wiSceneSystem.h | 113 ++--- WickedEngine/windHF.hlsli | 11 - 24 files changed, 543 insertions(+), 685 deletions(-) delete mode 100644 WickedEngine/windHF.hlsli diff --git a/Editor/MeshWindow.cpp b/Editor/MeshWindow.cpp index 06418b427..c75ea7516 100644 --- a/Editor/MeshWindow.cpp +++ b/Editor/MeshWindow.cpp @@ -195,7 +195,7 @@ void MeshWindow::SetEntity(Entity entity) stringstream ss(""); ss << "Mesh name: " << name.name << endl; - ss << "Vertex count: " << mesh->vertices_POS.size() << endl; + ss << "Vertex count: " << mesh->vertex_positions.size() << endl; ss << "Index count: " << mesh->indices.size() << endl; ss << "Subset count: " << mesh->subsets.size() << endl; meshInfoLabel->SetText(ss.str()); diff --git a/Editor/ModelImporter_GLTF.cpp b/Editor/ModelImporter_GLTF.cpp index 1dd31664b..c09fe8fb9 100644 --- a/Editor/ModelImporter_GLTF.cpp +++ b/Editor/ModelImporter_GLTF.cpp @@ -18,7 +18,7 @@ using namespace wiECS; // Transform the data from glTF space to engine-space: -const bool transform_to_LH = true; +static const bool transform_to_LH = true; namespace tinygltf @@ -181,7 +181,7 @@ void LoadNode(tinygltf::Node* node, Entity parent, LoaderState& state) if (node->skin >= 0) { MeshComponent& mesh = *scene.meshes.GetComponent(object.meshID); - assert(!mesh.vertices_BON.empty()); + assert(!mesh.vertex_boneindices.empty()); mesh.armatureID = state.armatureArray[node->skin]; } } @@ -229,7 +229,7 @@ void LoadNode(tinygltf::Node* node, Entity parent, LoaderState& state) // Important: // Do NOT call UpdateTransform, because Attach will query parent world matrix, and invert it for bind matrix - // But here we load everything in bind space already, so it must be IDENTITY! + // But here we load everything in bind space (relative to parent) already, so it must be IDENTITY! if (parent != INVALID_ENTITY) { @@ -516,11 +516,11 @@ Entity ImportModel_GLTF(const std::string& fileName) mesh.renderable = true; - XMFLOAT3 min = XMFLOAT3(FLT_MAX, FLT_MAX, FLT_MAX); - XMFLOAT3 max = XMFLOAT3(-FLT_MAX, -FLT_MAX, -FLT_MAX); - + int matIndex = -1; for (auto& prim : x.primitives) { + matIndex++; + assert(prim.indices >= 0); // Fill indices: @@ -530,10 +530,16 @@ Entity ImportModel_GLTF(const std::string& fileName) int stride = accessor.ByteStride(bufferView); size_t count = accessor.count; - size_t offset = mesh.indices.size(); mesh.indices.resize(offset + count); + assert(prim.material >= 0); + + mesh.subsets.push_back(MeshComponent::MeshSubset()); + mesh.subsets.back().materialID = state.materialArray[prim.material]; + mesh.subsets.back().indexOffset = (uint32_t)offset; + mesh.subsets.back().indexCount = (uint32_t)count; + const unsigned char* data = buffer.data.data() + accessor.byteOffset + bufferView.byteOffset; if (stride == 1) @@ -569,29 +575,8 @@ Entity ImportModel_GLTF(const std::string& fileName) } - // Create mesh subset: - MeshComponent::MeshSubset subset; - - if (prim.material >= 0) - { - subset.materialID = state.materialArray[prim.material]; - } - else - { - assert(0); - } - - mesh.subsets.push_back(subset); - } - - bool hasBoneWeights = false; - bool hasBoneIndices = false; - - int matIndex = -1; - for (auto& prim : x.primitives) - { - matIndex++; - size_t offset = mesh.vertices_FULL.size(); + bool hasBoneWeights = false; + bool hasBoneIndices = false; for (auto& attr : prim.attributes) { @@ -605,52 +590,41 @@ Entity ImportModel_GLTF(const std::string& fileName) int stride = accessor.ByteStride(bufferView); size_t count = accessor.count; - if (mesh.vertices_FULL.size() == offset) - { - mesh.vertices_FULL.resize(offset + count); - } - const unsigned char* data = buffer.data.data() + accessor.byteOffset + bufferView.byteOffset; if (!attr_name.compare("POSITION")) { + mesh.vertex_positions.resize(mesh.vertex_positions.size() + count); assert(stride == 12); for (size_t i = 0; i < count; ++i) { - XMFLOAT3 pos = ((XMFLOAT3*)data)[i]; - - mesh.vertices_FULL[offset + i].pos = XMFLOAT4(pos.x, pos.y, pos.z, 0); - - min = wiMath::Min(min, pos); - max = wiMath::Max(max, pos); + mesh.vertex_positions[offset + i] = ((XMFLOAT3*)data)[i]; } } else if (!attr_name.compare("NORMAL")) { + mesh.vertex_normals.resize(mesh.vertex_normals.size() + count); assert(stride == 12); for (size_t i = 0; i < count; ++i) { - XMFLOAT3 nor = ((XMFLOAT3*)data)[i]; - - mesh.vertices_FULL[offset + i].nor.x = nor.x; - mesh.vertices_FULL[offset + i].nor.y = nor.y; - mesh.vertices_FULL[offset + i].nor.z = nor.z; + mesh.vertex_normals[offset + i] = ((XMFLOAT3*)data)[i]; } } else if (!attr_name.compare("TEXCOORD_0")) { + mesh.vertex_texcoords.resize(mesh.vertex_texcoords.size() + count); assert(stride == 8); for (size_t i = 0; i < count; ++i) { const XMFLOAT2& tex = ((XMFLOAT2*)data)[i]; - mesh.vertices_FULL[offset + i].tex.x = tex.x; - mesh.vertices_FULL[offset + i].tex.y = tex.y; - mesh.vertices_FULL[offset + i].tex.z = (float)matIndex /*prim.material*/; + mesh.vertex_texcoords[offset + i].x = tex.x; + mesh.vertex_texcoords[offset + i].y = tex.y; } } else if (!attr_name.compare("JOINTS_0")) { + mesh.vertex_boneindices.resize(mesh.vertex_boneindices.size() + count); if (stride == 4) { hasBoneIndices = true; @@ -663,10 +637,10 @@ Entity ImportModel_GLTF(const std::string& fileName) { const JointTmp& joint = ((JointTmp*)data)[i]; - mesh.vertices_FULL[offset + i].ind.x = (float)joint.ind[0]; - mesh.vertices_FULL[offset + i].ind.y = (float)joint.ind[1]; - mesh.vertices_FULL[offset + i].ind.z = (float)joint.ind[2]; - mesh.vertices_FULL[offset + i].ind.w = (float)joint.ind[3]; + mesh.vertex_boneindices[offset + i].x = joint.ind[0]; + mesh.vertex_boneindices[offset + i].y = joint.ind[1]; + mesh.vertex_boneindices[offset + i].z = joint.ind[2]; + mesh.vertex_boneindices[offset + i].w = joint.ind[3]; } } else if (stride == 8) @@ -681,10 +655,10 @@ Entity ImportModel_GLTF(const std::string& fileName) { const JointTmp& joint = ((JointTmp*)data)[i]; - mesh.vertices_FULL[offset + i].ind.x = (float)joint.ind[0]; - mesh.vertices_FULL[offset + i].ind.y = (float)joint.ind[1]; - mesh.vertices_FULL[offset + i].ind.z = (float)joint.ind[2]; - mesh.vertices_FULL[offset + i].ind.w = (float)joint.ind[3]; + mesh.vertex_boneindices[offset + i].x = joint.ind[0]; + mesh.vertex_boneindices[offset + i].y = joint.ind[1]; + mesh.vertex_boneindices[offset + i].z = joint.ind[2]; + mesh.vertex_boneindices[offset + i].w = joint.ind[3]; } } else @@ -694,11 +668,12 @@ Entity ImportModel_GLTF(const std::string& fileName) } else if (!attr_name.compare("WEIGHTS_0")) { + mesh.vertex_boneweights.resize(mesh.vertex_boneweights.size() + count); hasBoneWeights = true; assert(stride == 16); for (size_t i = 0; i < count; ++i) { - mesh.vertices_FULL[offset + i].wei = ((XMFLOAT4*)data)[i]; + mesh.vertex_boneweights[offset + i] = ((XMFLOAT4*)data)[i]; } } @@ -706,7 +681,6 @@ Entity ImportModel_GLTF(const std::string& fileName) } - mesh.aabb.create(min, max); mesh.CreateRenderData(); model.meshes.insert(meshEntity); diff --git a/Editor/ModelImporter_OBJ.cpp b/Editor/ModelImporter_OBJ.cpp index ed64b63ac..114d8c1e8 100644 --- a/Editor/ModelImporter_OBJ.cpp +++ b/Editor/ModelImporter_OBJ.cpp @@ -11,6 +11,9 @@ using namespace wiGraphicsTypes; using namespace wiSceneSystem; using namespace wiECS; +// Transform the data from OBJ space to engine-space: +static const bool transform_to_LH = true; + Entity ImportModel_OBJ(const std::string& fileName) { string directory, name; @@ -104,9 +107,6 @@ Entity ImportModel_OBJ(const std::string& fileName) object.meshID = meshEntity; mesh.renderable = true; - XMFLOAT3 min = XMFLOAT3(FLT_MAX, FLT_MAX, FLT_MAX); - XMFLOAT3 max = XMFLOAT3(-FLT_MAX, -FLT_MAX, -FLT_MAX); - unordered_map registered_materialIndices = {}; unordered_map uniqueVertices = {}; @@ -128,31 +128,30 @@ Entity ImportModel_OBJ(const std::string& fileName) for (auto& index : reordered_indices) { - MeshComponent::Vertex_FULL vert; + XMFLOAT3 pos; - vert.pos = XMFLOAT4( + pos = XMFLOAT3( obj_attrib.vertices[index.vertex_index * 3 + 0], obj_attrib.vertices[index.vertex_index * 3 + 1], - obj_attrib.vertices[index.vertex_index * 3 + 2], - 0 + obj_attrib.vertices[index.vertex_index * 3 + 2] ); + XMFLOAT3 nor = XMFLOAT3(0, 0, 0); if (!obj_attrib.normals.empty()) { - vert.nor = XMFLOAT4( + nor = XMFLOAT3( obj_attrib.normals[index.normal_index * 3 + 0], obj_attrib.normals[index.normal_index * 3 + 1], - obj_attrib.normals[index.normal_index * 3 + 2], - 0 + obj_attrib.normals[index.normal_index * 3 + 2] ); } + XMFLOAT2 tex = XMFLOAT2(0, 0); if (index.texcoord_index >= 0 && !obj_attrib.texcoords.empty()) { - vert.tex = XMFLOAT4( + tex = XMFLOAT2( obj_attrib.texcoords[index.texcoord_index * 2 + 0], - 1 - obj_attrib.texcoords[index.texcoord_index * 2 + 1], - 0, 0 + 1 - obj_attrib.texcoords[index.texcoord_index * 2 + 1] ); } @@ -162,15 +161,13 @@ Entity ImportModel_OBJ(const std::string& fileName) registered_materialIndices[materialIndex] = (int)mesh.subsets.size(); mesh.subsets.push_back(MeshComponent::MeshSubset()); mesh.subsets.back().materialID = materialLibrary[materialIndex]; + mesh.subsets.back().indexOffset = (uint32_t)mesh.indices.size(); } - vert.tex.z = (float)registered_materialIndices[materialIndex]; // this indexes a mesh subset - // todo: option parameter would be better - const bool flipZ = true; - if (flipZ) + if (transform_to_LH) { - vert.pos.z *= -1; - vert.nor.z *= -1; + pos.z *= -1; + nor.z *= -1; } // eliminate duplicate vertices by means of hashing: @@ -184,16 +181,15 @@ Entity ImportModel_OBJ(const std::string& fileName) if (uniqueVertices.count(vertexHash) == 0) { - uniqueVertices[vertexHash] = (uint32_t)mesh.vertices_FULL.size(); - mesh.vertices_FULL.push_back(vert); + uniqueVertices[vertexHash] = (uint32_t)mesh.vertex_positions.size(); + mesh.vertex_positions.push_back(pos); + mesh.vertex_normals.push_back(nor); + mesh.vertex_texcoords.push_back(tex); } mesh.indices.push_back(uniqueVertices[vertexHash]); - - min = wiMath::Min(min, XMFLOAT3(vert.pos.x, vert.pos.y, vert.pos.z)); - max = wiMath::Max(max, XMFLOAT3(vert.pos.x, vert.pos.y, vert.pos.z)); + mesh.subsets.back().indexCount++; } } - mesh.aabb.create(min, max); mesh.CreateRenderData(); model.objects.insert(objectEntity); diff --git a/WickedEngine/WickedEngine_SHADERS.vcxproj b/WickedEngine/WickedEngine_SHADERS.vcxproj index 5d9d19cb2..2344f1c92 100644 --- a/WickedEngine/WickedEngine_SHADERS.vcxproj +++ b/WickedEngine/WickedEngine_SHADERS.vcxproj @@ -45,7 +45,6 @@ - diff --git a/WickedEngine/WickedEngine_SHADERS.vcxproj.filters b/WickedEngine/WickedEngine_SHADERS.vcxproj.filters index b568f47bf..1de3bf8f2 100644 --- a/WickedEngine/WickedEngine_SHADERS.vcxproj.filters +++ b/WickedEngine/WickedEngine_SHADERS.vcxproj.filters @@ -67,9 +67,6 @@ HF - - HF - HF diff --git a/WickedEngine/cubeShadowVS.hlsl b/WickedEngine/cubeShadowVS.hlsl index 81bcf41b1..023b0eaad 100644 --- a/WickedEngine/cubeShadowVS.hlsl +++ b/WickedEngine/cubeShadowVS.hlsl @@ -1,6 +1,5 @@ #include "globals.hlsli" #include "objectInputLayoutHF.hlsli" -#include "windHF.hlsli" struct GS_CUBEMAP_IN @@ -16,7 +15,6 @@ GS_CUBEMAP_IN main(Input_Object_POS input) VertexSurface surface = MakeVertexSurfaceFromInput(input); Out.Pos = mul(surface.position, WORLD); - affectWind(Out.Pos.xyz, surface.wind, g_xFrame_Time); return Out; } \ No newline at end of file diff --git a/WickedEngine/cubeShadowVS_alphatest.hlsl b/WickedEngine/cubeShadowVS_alphatest.hlsl index 28c0aa1b8..e1e013556 100644 --- a/WickedEngine/cubeShadowVS_alphatest.hlsl +++ b/WickedEngine/cubeShadowVS_alphatest.hlsl @@ -1,6 +1,5 @@ #include "globals.hlsli" #include "objectInputLayoutHF.hlsli" -#include "windHF.hlsli" struct GS_CUBEMAP_IN @@ -17,9 +16,7 @@ GS_CUBEMAP_IN main(Input_Object_POS_TEX input) VertexSurface surface = MakeVertexSurfaceFromInput(input); Out.Pos = mul(surface.position, WORLD); - affectWind(Out.Pos.xyz, surface.wind, g_xFrame_Time); Out.Tex = surface.uv; - return Out; } \ No newline at end of file diff --git a/WickedEngine/objectHF.hlsli b/WickedEngine/objectHF.hlsli index 9cee9478f..a45c98f4f 100644 --- a/WickedEngine/objectHF.hlsli +++ b/WickedEngine/objectHF.hlsli @@ -18,7 +18,6 @@ #include "globals.hlsli" #include "objectInputLayoutHF.hlsli" -#include "windHF.hlsli" #include "ditherHF.hlsli" #include "tangentComputeHF.hlsli" #include "depthConvertHF.hlsli" diff --git a/WickedEngine/objectInputLayoutHF.hlsli b/WickedEngine/objectInputLayoutHF.hlsli index d988599b7..3513ac91b 100644 --- a/WickedEngine/objectInputLayoutHF.hlsli +++ b/WickedEngine/objectInputLayoutHF.hlsli @@ -17,18 +17,18 @@ struct Input_InstancePrev struct Input_Object_POS { - float4 pos : POSITION_NORMAL_WIND_MATID; + float4 pos : POSITION_NORMAL_SUBSETINDEX; Input_Instance instance; }; struct Input_Object_POS_TEX { - float4 pos : POSITION_NORMAL_WIND_MATID; + float4 pos : POSITION_NORMAL_SUBSETINDEX; float2 tex : TEXCOORD0; Input_Instance instance; }; struct Input_Object_ALL { - float4 pos : POSITION_NORMAL_WIND_MATID; + float4 pos : POSITION_NORMAL_SUBSETINDEX; float2 tex : TEXCOORD; float4 pre : PREVPOS; Input_Instance instance; @@ -58,7 +58,6 @@ struct VertexSurface { float4 position; float3 normal; - float wind; uint materialIndex; float2 uv; float4 prevPos; @@ -73,8 +72,7 @@ inline VertexSurface MakeVertexSurfaceFromInput(Input_Object_POS input) surface.normal.x = (float)((normal_wind_matID >> 0) & 0x000000FF) / 255.0f * 2.0f - 1.0f; surface.normal.y = (float)((normal_wind_matID >> 8) & 0x000000FF) / 255.0f * 2.0f - 1.0f; surface.normal.z = (float)((normal_wind_matID >> 16) & 0x000000FF) / 255.0f * 2.0f - 1.0f; - surface.wind = (float)((normal_wind_matID >> 24) & 0x0000000F) / 15.0f; - surface.materialIndex = (normal_wind_matID >> 28) & 0x0000000F; + surface.materialIndex = (normal_wind_matID >> 24) & 0x000000FF; return surface; } @@ -88,8 +86,7 @@ inline VertexSurface MakeVertexSurfaceFromInput(Input_Object_POS_TEX input) surface.normal.x = (float)((normal_wind_matID >> 0) & 0x000000FF) / 255.0f * 2.0f - 1.0f; surface.normal.y = (float)((normal_wind_matID >> 8) & 0x000000FF) / 255.0f * 2.0f - 1.0f; surface.normal.z = (float)((normal_wind_matID >> 16) & 0x000000FF) / 255.0f * 2.0f - 1.0f; - surface.wind = (float)((normal_wind_matID >> 24) & 0x0000000F) / 15.0f; - surface.materialIndex = (normal_wind_matID >> 28) & 0x0000000F; + surface.materialIndex = (normal_wind_matID >> 24) & 0x000000FF; surface.uv = input.tex.xy; @@ -105,8 +102,7 @@ inline VertexSurface MakeVertexSurfaceFromInput(Input_Object_ALL input) surface.normal.x = (float)((normal_wind_matID >> 0) & 0x000000FF) / 255.0f * 2.0f - 1.0f; surface.normal.y = (float)((normal_wind_matID >> 8) & 0x000000FF) / 255.0f * 2.0f - 1.0f; surface.normal.z = (float)((normal_wind_matID >> 16) & 0x000000FF) / 255.0f * 2.0f - 1.0f; - surface.wind = (float)((normal_wind_matID >> 24) & 0x0000000F) / 15.0f; - surface.materialIndex = (normal_wind_matID >> 28) & 0x0000000F; + surface.materialIndex = (normal_wind_matID >> 24) & 0x000000FF; surface.uv = input.tex.xy; diff --git a/WickedEngine/objectVS_common.hlsl b/WickedEngine/objectVS_common.hlsl index f55f089b5..d227840b7 100644 --- a/WickedEngine/objectVS_common.hlsl +++ b/WickedEngine/objectVS_common.hlsl @@ -19,9 +19,6 @@ PixelInputType main(Input_Object_ALL input) surface.normal = normalize(mul(surface.normal, (float3x3)WORLD)); Out.clip = dot(surface.position, g_xClipPlane); - - affectWind(surface.position.xyz, surface.wind, g_xFrame_Time); - affectWind(surface.prevPos.xyz, surface.wind, g_xFrame_TimePrev); Out.pos = Out.pos2D = mul(surface.position, g_xCamera_VP); Out.pos2DPrev = mul(surface.prevPos, g_xFrame_MainCamera_PrevVP); diff --git a/WickedEngine/objectVS_common_tessellation.hlsl b/WickedEngine/objectVS_common_tessellation.hlsl index 4aa329335..36e4b6287 100644 --- a/WickedEngine/objectVS_common_tessellation.hlsl +++ b/WickedEngine/objectVS_common_tessellation.hlsl @@ -27,10 +27,6 @@ HullInputType main(Input_Object_ALL input) surface.normal = normalize(mul(surface.normal, (float3x3)WORLD)); - affectWind(surface.position.xyz, surface.wind, g_xFrame_Time); - affectWind(surface.prevPos.xyz, surface.wind, g_xFrame_TimePrev); - - Out.pos = surface.position.xyz; Out.posPrev = surface.prevPos.xyz; Out.tex = surface.uv.xyxy; diff --git a/WickedEngine/objectVS_positionstream.hlsl b/WickedEngine/objectVS_positionstream.hlsl index dd932df67..6b1de6934 100644 --- a/WickedEngine/objectVS_positionstream.hlsl +++ b/WickedEngine/objectVS_positionstream.hlsl @@ -7,9 +7,5 @@ float4 main(Input_Object_POS input) : SV_POSITION surface.position = mul(surface.position, WORLD); - affectWind(surface.position.xyz, surface.wind, g_xFrame_Time); - - float4 pos = mul(surface.position, g_xCamera_VP); - - return pos; + return mul(surface.position, g_xCamera_VP); } diff --git a/WickedEngine/objectVS_simple.hlsl b/WickedEngine/objectVS_simple.hlsl index c5f9034b4..818e86fb7 100644 --- a/WickedEngine/objectVS_simple.hlsl +++ b/WickedEngine/objectVS_simple.hlsl @@ -14,8 +14,6 @@ PixelInputType_Simple main(Input_Object_POS_TEX input) Out.clip = dot(surface.position, g_xClipPlane); - affectWind(surface.position.xyz, surface.wind, g_xFrame_Time); - Out.pos = mul(surface.position, g_xCamera_VP); Out.tex = surface.uv; diff --git a/WickedEngine/objectVS_simple_tessellation.hlsl b/WickedEngine/objectVS_simple_tessellation.hlsl index 18d577103..60cbeac44 100644 --- a/WickedEngine/objectVS_simple_tessellation.hlsl +++ b/WickedEngine/objectVS_simple_tessellation.hlsl @@ -22,8 +22,6 @@ HullInputType main(Input_Object_ALL input) surface.position = mul(surface.position, WORLD); surface.normal = normalize(mul(surface.normal, (float3x3)WORLD)); - affectWind(surface.position.xyz, surface.wind, g_xFrame_Time); - Out.pos = surface.position.xyz; Out.tex = surface.uv.xyxy; diff --git a/WickedEngine/shadowVS.hlsl b/WickedEngine/shadowVS.hlsl index bef38b779..f4f36a4f7 100644 --- a/WickedEngine/shadowVS.hlsl +++ b/WickedEngine/shadowVS.hlsl @@ -1,6 +1,5 @@ #include "globals.hlsli" #include "objectInputLayoutHF.hlsli" -#include "windHF.hlsli" struct VertexOut @@ -16,8 +15,6 @@ VertexOut main(Input_Object_POS input) VertexSurface surface = MakeVertexSurfaceFromInput(input); Out.pos = mul(surface.position, WORLD); - affectWind(Out.pos.xyz, surface.wind, g_xFrame_Time); - Out.pos = mul(Out.pos, g_xCamera_VP); return Out; diff --git a/WickedEngine/shadowVS_alphatest.hlsl b/WickedEngine/shadowVS_alphatest.hlsl index a51b74cb2..ecb6e5aba 100644 --- a/WickedEngine/shadowVS_alphatest.hlsl +++ b/WickedEngine/shadowVS_alphatest.hlsl @@ -1,6 +1,5 @@ #include "globals.hlsli" #include "objectInputLayoutHF.hlsli" -#include "windHF.hlsli" struct VertexOut @@ -11,13 +10,12 @@ struct VertexOut VertexOut main(Input_Object_POS_TEX input) { - VertexOut Out = (VertexOut)0; + VertexOut Out; float4x4 WORLD = MakeWorldMatrixFromInstance(input.instance); VertexSurface surface = MakeVertexSurfaceFromInput(input); Out.pos = mul(surface.position, WORLD); - affectWind(Out.pos.xyz, surface.wind, g_xFrame_Time); Out.pos = mul(Out.pos, g_xCamera_VP); Out.tex = surface.uv; diff --git a/WickedEngine/shadowVS_transparent.hlsl b/WickedEngine/shadowVS_transparent.hlsl index 37484ae37..618f64727 100644 --- a/WickedEngine/shadowVS_transparent.hlsl +++ b/WickedEngine/shadowVS_transparent.hlsl @@ -20,8 +20,6 @@ VertexOut main(Input_Object_POS_TEX input) surface.position = mul(surface.position, WORLD); - affectWind(surface.position.xyz, surface.wind, g_xFrame_Time); - Out.pos = mul(surface.position, g_xCamera_VP); Out.tex = surface.uv; diff --git a/WickedEngine/skinningCS.hlsl b/WickedEngine/skinningCS.hlsl index e2ef26d25..26df8861d 100644 --- a/WickedEngine/skinningCS.hlsl +++ b/WickedEngine/skinningCS.hlsl @@ -87,7 +87,7 @@ void main(uint3 DTid : SV_DispatchThreadID, uint3 GTid : SV_GroupThreadID) nor.x = (float)((pos_nor_u.w >> 0) & 0x000000FF) / 255.0f * 2.0f - 1.0f; nor.y = (float)((pos_nor_u.w >> 8) & 0x000000FF) / 255.0f * 2.0f - 1.0f; nor.z = (float)((pos_nor_u.w >> 16) & 0x000000FF) / 255.0f * 2.0f - 1.0f; - nor.w = (float)((pos_nor_u.w >> 24) & 0x000000FF) / 255.0f; // wind + nor.w = (float)((pos_nor_u.w >> 24) & 0x000000FF) / 255.0f; // subsetindex } @@ -127,7 +127,7 @@ void main(uint3 DTid : SV_DispatchThreadID, uint3 GTid : SV_GroupThreadID) pos_nor_u.w |= (uint)((nor.x * 0.5f + 0.5f) * 255.0f) << 0; pos_nor_u.w |= (uint)((nor.y * 0.5f + 0.5f) * 255.0f) << 8; pos_nor_u.w |= (uint)((nor.z * 0.5f + 0.5f) * 255.0f) << 16; - pos_nor_u.w |= (uint)(nor.w * 255.0f) << 24; // wind + pos_nor_u.w |= (uint)(nor.w * 255.0f) << 24; // subsetindex } // Store data: diff --git a/WickedEngine/wiBULLET.cpp b/WickedEngine/wiBULLET.cpp index 14a6e9156..d1d9690b7 100644 --- a/WickedEngine/wiBULLET.cpp +++ b/WickedEngine/wiBULLET.cpp @@ -123,36 +123,37 @@ void wiBULLET::setWind(const XMFLOAT3& wind){ void wiBULLET::connectVerticesToSoftBody(PhysicsComponent& physicscomponent, MeshComponent& mesh) { - if (!softBodyPhysicsEnabled) - return; + //if (!softBodyPhysicsEnabled) + // return; - btCollisionObject* obj = bulletPhysics->dynamicsWorld->getCollisionObjectArray()[physicscomponent.physicsObjectID]; - btSoftBody* softBody = btSoftBody::upcast(obj); + //btCollisionObject* obj = bulletPhysics->dynamicsWorld->getCollisionObjectArray()[physicscomponent.physicsObjectID]; + //btSoftBody* softBody = btSoftBody::upcast(obj); - if(softBody){ - btVector3 min, max; - softBody->getAabb(min, max); - mesh.aabb.create(XMFLOAT3(min.x(), min.y(), min.z()), XMFLOAT3(max.x(), max.y(), max.z())); + //if (softBody) + //{ + // btVector3 min, max; + // softBody->getAabb(min, max); + // mesh.aabb.create(XMFLOAT3(min.x(), min.y(), min.z()), XMFLOAT3(max.x(), max.y(), max.z())); - softBody->setWindVelocity(bulletPhysics->wind); + // softBody->setWindVelocity(bulletPhysics->wind); - btSoftBody::tNodeArray& nodes(softBody->m_nodes); - - int gvg = physicscomponent.goalVG; - for (unsigned int i = 0; im_nodes); - MeshComponent::Vertex_POS& vert = mesh.vertices_Transformed_POS[i]; + // int gvg = physicscomponent.goalVG; + // for (size_t i = 0; i < mesh.vertex_positions.size(); ++i) + // { + // int indexP = physicscomponent.physicalmapGP[i]; + // float weight = mesh.vertexGroups[gvg].vertex_weights[indexP]; - mesh.vertices_Transformed_PRE[i] = vert; - vert.pos.x = nodes[indexP].m_x.getX(); - vert.pos.y = nodes[indexP].m_x.getY(); - vert.pos.z = nodes[indexP].m_x.getZ(); - mesh.vertices_Transformed_POS[i].MakeFromParams(XMFLOAT3(-nodes[indexP].m_n.getX(), -nodes[indexP].m_n.getY(), -nodes[indexP].m_n.getZ())/*, vert.GetWind(), vert.GetMaterialIndex()*/); - } - } + // MeshComponent::Vertex_POS& vert = mesh.vertices_Transformed_POS[i]; + + // mesh.vertices_Transformed_PRE[i] = vert; + // vert.pos.x = nodes[indexP].m_x.getX(); + // vert.pos.y = nodes[indexP].m_x.getY(); + // vert.pos.z = nodes[indexP].m_x.getZ(); + // mesh.vertices_Transformed_POS[i].MakeFromParams(XMFLOAT3(-nodes[indexP].m_n.getX(), -nodes[indexP].m_n.getY(), -nodes[indexP].m_n.getZ())/*, vert.GetWind(), vert.GetMaterialIndex()*/); + // } + //} } void wiBULLET::connectSoftBodyToVertices(PhysicsComponent& physicscomponent, MeshComponent& mesh) { @@ -388,9 +389,9 @@ void wiBULLET::registerObject(PhysicsComponent& physicscomponent, MeshComponent& if(!physicscomponent.collisionShape.compare("CONVEX_HULL")) { btCollisionShape* shape = new btConvexHullShape(); - for (auto& x : mesh.vertices_POS) + for (auto& pos : mesh.vertex_positions) { - ((btConvexHullShape*)shape)->addPoint(btVector3(x.pos.x, x.pos.y, x.pos.z)); + ((btConvexHullShape*)shape)->addPoint(btVector3(pos.x, pos.y, pos.z)); } shape->setLocalScaling(S); shape->setMargin(btScalar(0.05)); @@ -443,14 +444,14 @@ void wiBULLET::registerObject(PhysicsComponent& physicscomponent, MeshComponent& } if(!physicscomponent.collisionShape.compare("MESH")) { - int totalVerts = (int)mesh.vertices_POS.size(); + int totalVerts = (int)mesh.vertex_positions.size(); int totalTriangles = (int)mesh.indices.size() / 3; btVector3* btVerts = new btVector3[totalVerts]; size_t i = 0; - for (auto& x : mesh.vertices_POS) + for (auto& pos : mesh.vertex_positions) { - btVerts[i++] = btVector3(x.pos.x, x.pos.y, x.pos.z); + btVerts[i++] = btVector3(pos.x, pos.y, pos.z); } int* btInd = new int[mesh.indices.size()]; diff --git a/WickedEngine/wiRenderer.cpp b/WickedEngine/wiRenderer.cpp index 9a9647996..b8a735aac 100644 --- a/WickedEngine/wiRenderer.cpp +++ b/WickedEngine/wiRenderer.cpp @@ -362,167 +362,171 @@ void wiRenderer::SetUpStaticComponents() } if (!impostorVB_POS.IsValid()) { - MeshComponent::Vertex_FULL impostorVertices[6 * 6]; + const int count = 6 * 6; + + XMFLOAT3 impostorVertices_pos[count]; + XMFLOAT3 impostorVertices_nor[count]; + XMFLOAT2 impostorVertices_tex[count]; float stepX = 1.f / 6.f; // front - impostorVertices[0].pos = XMFLOAT4(-1, 1, 0, 0); - impostorVertices[0].nor = XMFLOAT4(0, 0, -1, 1); - impostorVertices[0].tex = XMFLOAT4(0, 0, 0, 0); + impostorVertices_pos[0] = XMFLOAT3(-1, 1, 0); + impostorVertices_nor[0] = XMFLOAT3(0, 0, -1); + impostorVertices_tex[0] = XMFLOAT2(0, 0); - impostorVertices[1].pos = XMFLOAT4(-1, -1, 0, 0); - impostorVertices[1].nor = XMFLOAT4(0, 0, -1, 1); - impostorVertices[1].tex = XMFLOAT4(0, 1, 0, 0); + impostorVertices_pos[1] = XMFLOAT3(-1, -1, 0); + impostorVertices_nor[1] = XMFLOAT3(0, 0, -1); + impostorVertices_tex[1] = XMFLOAT2(0, 1); - impostorVertices[2].pos = XMFLOAT4(1, 1, 0, 0); - impostorVertices[2].nor = XMFLOAT4(0, 0, -1, 1); - impostorVertices[2].tex = XMFLOAT4(stepX, 0, 0, 0); + impostorVertices_pos[2] = XMFLOAT3(1, 1, 0); + impostorVertices_nor[2] = XMFLOAT3(0, 0, -1); + impostorVertices_tex[2] = XMFLOAT2(stepX, 0); - impostorVertices[3].pos = XMFLOAT4(-1, -1, 0, 0); - impostorVertices[3].nor = XMFLOAT4(0, 0, -1, 1); - impostorVertices[3].tex = XMFLOAT4(0, 1, 0, 0); + impostorVertices_pos[3] = XMFLOAT3(-1, -1, 0); + impostorVertices_nor[3] = XMFLOAT3(0, 0, -1); + impostorVertices_tex[3] = XMFLOAT2(0, 1); - impostorVertices[4].pos = XMFLOAT4(1, -1, 0, 0); - impostorVertices[4].nor = XMFLOAT4(0, 0, -1, 1); - impostorVertices[4].tex = XMFLOAT4(stepX, 1, 0, 0); + impostorVertices_pos[4] = XMFLOAT3(1, -1, 0); + impostorVertices_nor[4] = XMFLOAT3(0, 0, -1); + impostorVertices_tex[4] = XMFLOAT2(stepX, 1); - impostorVertices[5].pos = XMFLOAT4(1, 1, 0, 0); - impostorVertices[5].nor = XMFLOAT4(0, 0, -1, 1); - impostorVertices[5].tex = XMFLOAT4(stepX, 0, 0, 0); + impostorVertices_pos[5] = XMFLOAT3(1, 1, 0); + impostorVertices_nor[5] = XMFLOAT3(0, 0, -1); + impostorVertices_tex[5] = XMFLOAT2(stepX, 0); // right - impostorVertices[6].pos = XMFLOAT4(0, 1, -1, 0); - impostorVertices[6].nor = XMFLOAT4(1, 0, 0, 1); - impostorVertices[6].tex = XMFLOAT4(stepX, 0, 0, 0); + impostorVertices_pos[6] = XMFLOAT3(0, 1, -1); + impostorVertices_nor[6] = XMFLOAT3(1, 0, 0); + impostorVertices_tex[6] = XMFLOAT2(stepX, 0); - impostorVertices[7].pos = XMFLOAT4(0, -1, -1, 0); - impostorVertices[7].nor = XMFLOAT4(1, 0, 0, 1); - impostorVertices[7].tex = XMFLOAT4(stepX, 1, 0, 0); + impostorVertices_pos[7] = XMFLOAT3(0, -1, -1); + impostorVertices_nor[7] = XMFLOAT3(1, 0, 0); + impostorVertices_tex[7] = XMFLOAT2(stepX, 1); - impostorVertices[8].pos = XMFLOAT4(0, 1, 1, 0); - impostorVertices[8].nor = XMFLOAT4(1, 0, 0, 1); - impostorVertices[8].tex = XMFLOAT4(stepX * 2, 0, 0, 0); + impostorVertices_pos[8] = XMFLOAT3(0, 1, 1); + impostorVertices_nor[8] = XMFLOAT3(1, 0, 0); + impostorVertices_tex[8] = XMFLOAT2(stepX * 2, 0); - impostorVertices[9].pos = XMFLOAT4(0, -1, -1, 0); - impostorVertices[9].nor = XMFLOAT4(1, 0, 0, 1); - impostorVertices[9].tex = XMFLOAT4(stepX, 1, 0, 0); + impostorVertices_pos[9] = XMFLOAT3(0, -1, -1); + impostorVertices_nor[9] = XMFLOAT3(1, 0, 0); + impostorVertices_tex[9] = XMFLOAT2(stepX, 1); - impostorVertices[10].pos = XMFLOAT4(0, -1, 1, 0); - impostorVertices[10].nor = XMFLOAT4(1, 0, 0, 1); - impostorVertices[10].tex = XMFLOAT4(stepX * 2, 1, 0, 0); + impostorVertices_pos[10] = XMFLOAT3(0, -1, 1); + impostorVertices_nor[10] = XMFLOAT3(1, 0, 0); + impostorVertices_tex[10] = XMFLOAT2(stepX * 2, 1); - impostorVertices[11].pos = XMFLOAT4(0, 1, 1, 0); - impostorVertices[11].nor = XMFLOAT4(1, 0, 0, 1); - impostorVertices[11].tex = XMFLOAT4(stepX * 2, 0, 0, 0); + impostorVertices_pos[11] = XMFLOAT3(0, 1, 1); + impostorVertices_nor[11] = XMFLOAT3(1, 0, 0); + impostorVertices_tex[11] = XMFLOAT2(stepX * 2, 0); // back - impostorVertices[12].pos = XMFLOAT4(-1, 1, 0, 0); - impostorVertices[12].nor = XMFLOAT4(0, 0, -1, 1); - impostorVertices[12].tex = XMFLOAT4(stepX * 3, 0, 0, 0); + impostorVertices_pos[12] = XMFLOAT3(-1, 1, 0); + impostorVertices_nor[12] = XMFLOAT3(0, 0, -1); + impostorVertices_tex[12] = XMFLOAT2(stepX * 3, 0); - impostorVertices[13].pos = XMFLOAT4(1, 1, 0, 0); - impostorVertices[13].nor = XMFLOAT4(0, 0, -1, 1); - impostorVertices[13].tex = XMFLOAT4(stepX * 2, 0, 0, 0); + impostorVertices_pos[13] = XMFLOAT3(1, 1, 0); + impostorVertices_nor[13] = XMFLOAT3(0, 0, -1); + impostorVertices_tex[13] = XMFLOAT2(stepX * 2, 0); - impostorVertices[14].pos = XMFLOAT4(-1, -1, 0, 0); - impostorVertices[14].nor = XMFLOAT4(0, 0, -1, 1); - impostorVertices[14].tex = XMFLOAT4(stepX * 3, 1, 0, 0); + impostorVertices_pos[14] = XMFLOAT3(-1, -1, 0); + impostorVertices_nor[14] = XMFLOAT3(0, 0, -1); + impostorVertices_tex[14] = XMFLOAT2(stepX * 3, 1); - impostorVertices[15].pos = XMFLOAT4(-1, -1, 0, 0); - impostorVertices[15].nor = XMFLOAT4(0, 0, -1, 1); - impostorVertices[15].tex = XMFLOAT4(stepX * 3, 1, 0, 0); + impostorVertices_pos[15] = XMFLOAT3(-1, -1, 0); + impostorVertices_nor[15] = XMFLOAT3(0, 0, -1); + impostorVertices_tex[15] = XMFLOAT2(stepX * 3, 1); - impostorVertices[16].pos = XMFLOAT4(1, 1, 0, 0); - impostorVertices[16].nor = XMFLOAT4(0, 0, -1, 1); - impostorVertices[16].tex = XMFLOAT4(stepX * 2, 0, 0, 0); + impostorVertices_pos[16] = XMFLOAT3(1, 1, 0); + impostorVertices_nor[16] = XMFLOAT3(0, 0, -1); + impostorVertices_tex[16] = XMFLOAT2(stepX * 2, 0); - impostorVertices[17].pos = XMFLOAT4(1, -1, 0, 0); - impostorVertices[17].nor = XMFLOAT4(0, 0, -1, 1); - impostorVertices[17].tex = XMFLOAT4(stepX * 2, 1, 0, 0); + impostorVertices_pos[17] = XMFLOAT3(1, -1, 0); + impostorVertices_nor[17] = XMFLOAT3(0, 0, -1); + impostorVertices_tex[17] = XMFLOAT2(stepX * 2, 1); // left - impostorVertices[18].pos = XMFLOAT4(0, 1, -1, 0); - impostorVertices[18].nor = XMFLOAT4(1, 0, 0, 1); - impostorVertices[18].tex = XMFLOAT4(stepX * 4, 0, 0, 0); + impostorVertices_pos[18] = XMFLOAT3(0, 1, -1); + impostorVertices_nor[18] = XMFLOAT3(1, 0, 0); + impostorVertices_tex[18] = XMFLOAT2(stepX * 4, 0); - impostorVertices[19].pos = XMFLOAT4(0, 1, 1, 0); - impostorVertices[19].nor = XMFLOAT4(1, 0, 0, 1); - impostorVertices[19].tex = XMFLOAT4(stepX * 3, 0, 0, 0); + impostorVertices_pos[19] = XMFLOAT3(0, 1, 1); + impostorVertices_nor[19] = XMFLOAT3(1, 0, 0); + impostorVertices_tex[19] = XMFLOAT2(stepX * 3, 0); - impostorVertices[20].pos = XMFLOAT4(0, -1, -1, 0); - impostorVertices[20].nor = XMFLOAT4(1, 0, 0, 1); - impostorVertices[20].tex = XMFLOAT4(stepX * 4, 1, 0, 0); + impostorVertices_pos[20] = XMFLOAT3(0, -1, -1); + impostorVertices_nor[20] = XMFLOAT3(1, 0, 0); + impostorVertices_tex[20] = XMFLOAT2(stepX * 4, 1); - impostorVertices[21].pos = XMFLOAT4(0, -1, -1, 0); - impostorVertices[21].nor = XMFLOAT4(1, 0, 0, 1); - impostorVertices[21].tex = XMFLOAT4(stepX * 4, 1, 0, 0); + impostorVertices_pos[21] = XMFLOAT3(0, -1, -1); + impostorVertices_nor[21] = XMFLOAT3(1, 0, 0); + impostorVertices_tex[21] = XMFLOAT2(stepX * 4, 1); - impostorVertices[22].pos = XMFLOAT4(0, 1, 1, 0); - impostorVertices[22].nor = XMFLOAT4(1, 0, 0, 1); - impostorVertices[22].tex = XMFLOAT4(stepX * 3, 0, 0, 0); + impostorVertices_pos[22] = XMFLOAT3(0, 1, 1); + impostorVertices_nor[22] = XMFLOAT3(1, 0, 0); + impostorVertices_tex[22] = XMFLOAT2(stepX * 3, 0); - impostorVertices[23].pos = XMFLOAT4(0, -1, 1, 0); - impostorVertices[23].nor = XMFLOAT4(1, 0, 0, 1); - impostorVertices[23].tex = XMFLOAT4(stepX * 3, 1, 0, 0); + impostorVertices_pos[23] = XMFLOAT3(0, -1, 1); + impostorVertices_nor[23] = XMFLOAT3(1, 0, 0); + impostorVertices_tex[23] = XMFLOAT2(stepX * 3, 1); // bottom - impostorVertices[24].pos = XMFLOAT4(-1, 0, 1, 0); - impostorVertices[24].nor = XMFLOAT4(0, 1, 0, 1); - impostorVertices[24].tex = XMFLOAT4(stepX * 4, 0, 0, 0); + impostorVertices_pos[24] = XMFLOAT3(-1, 0, 1); + impostorVertices_nor[24] = XMFLOAT3(0, 1, 0); + impostorVertices_tex[24] = XMFLOAT2(stepX * 4, 0); - impostorVertices[25].pos = XMFLOAT4(1, 0, 1, 0); - impostorVertices[25].nor = XMFLOAT4(0, 1, 0, 1); - impostorVertices[25].tex = XMFLOAT4(stepX * 5, 0, 0, 0); + impostorVertices_pos[25] = XMFLOAT3(1, 0, 1); + impostorVertices_nor[25] = XMFLOAT3(0, 1, 0); + impostorVertices_tex[25] = XMFLOAT2(stepX * 5, 0); - impostorVertices[26].pos = XMFLOAT4(-1, 0, -1, 0); - impostorVertices[26].nor = XMFLOAT4(0, 1, 0, 1); - impostorVertices[26].tex = XMFLOAT4(stepX * 4, 1, 0, 0); + impostorVertices_pos[26] = XMFLOAT3(-1, 0, -1); + impostorVertices_nor[26] = XMFLOAT3(0, 1, 0); + impostorVertices_tex[26] = XMFLOAT2(stepX * 4, 1); - impostorVertices[27].pos = XMFLOAT4(-1, 0, -1, 0); - impostorVertices[27].nor = XMFLOAT4(0, 1, 0, 1); - impostorVertices[27].tex = XMFLOAT4(stepX * 4, 1, 0, 0); + impostorVertices_pos[27] = XMFLOAT3(-1, 0, -1); + impostorVertices_nor[27] = XMFLOAT3(0, 1, 0); + impostorVertices_tex[27] = XMFLOAT2(stepX * 4, 1); - impostorVertices[28].pos = XMFLOAT4(1, 0, 1, 0); - impostorVertices[28].nor = XMFLOAT4(0, 1, 0, 1); - impostorVertices[28].tex = XMFLOAT4(stepX * 5, 0, 0, 0); + impostorVertices_pos[28] = XMFLOAT3(1, 0, 1); + impostorVertices_nor[28] = XMFLOAT3(0, 1, 0); + impostorVertices_tex[28] = XMFLOAT2(stepX * 5, 0); - impostorVertices[29].pos = XMFLOAT4(1, 0, -1, 0); - impostorVertices[29].nor = XMFLOAT4(0, 1, 0, 1); - impostorVertices[29].tex = XMFLOAT4(stepX * 5, 1, 0, 0); + impostorVertices_pos[29] = XMFLOAT3(1, 0, -1); + impostorVertices_nor[29] = XMFLOAT3(0, 1, 0); + impostorVertices_tex[29] = XMFLOAT2(stepX * 5, 1); // top - impostorVertices[30].pos = XMFLOAT4(-1, 0, 1, 0); - impostorVertices[30].nor = XMFLOAT4(0, 1, 0, 1); - impostorVertices[30].tex = XMFLOAT4(stepX * 5, 0, 0, 0); + impostorVertices_pos[30] = XMFLOAT3(-1, 0, 1); + impostorVertices_nor[30] = XMFLOAT3(0, 1, 0); + impostorVertices_tex[30] = XMFLOAT2(stepX * 5, 0); - impostorVertices[31].pos = XMFLOAT4(-1, 0, -1, 0); - impostorVertices[31].nor = XMFLOAT4(0, 1, 0, 1); - impostorVertices[31].tex = XMFLOAT4(stepX * 5, 1, 0, 0); + impostorVertices_pos[31] = XMFLOAT3(-1, 0, -1); + impostorVertices_nor[31] = XMFLOAT3(0, 1, 0); + impostorVertices_tex[31] = XMFLOAT2(stepX * 5, 1); - impostorVertices[32].pos = XMFLOAT4(1, 0, 1, 0); - impostorVertices[32].nor = XMFLOAT4(0, 1, 0, 1); - impostorVertices[32].tex = XMFLOAT4(stepX * 6, 0, 0, 0); + impostorVertices_pos[32] = XMFLOAT3(1, 0, 1); + impostorVertices_nor[32] = XMFLOAT3(0, 1, 0); + impostorVertices_tex[32] = XMFLOAT2(stepX * 6, 0); - impostorVertices[33].pos = XMFLOAT4(-1, 0, -1, 0); - impostorVertices[33].nor = XMFLOAT4(0, 1, 0, 1); - impostorVertices[33].tex = XMFLOAT4(stepX * 5, 1, 0, 0); + impostorVertices_pos[33] = XMFLOAT3(-1, 0, -1); + impostorVertices_nor[33] = XMFLOAT3(0, 1, 0); + impostorVertices_tex[33] = XMFLOAT2(stepX * 5, 1); - impostorVertices[34].pos = XMFLOAT4(1, 0, -1, 0); - impostorVertices[34].nor = XMFLOAT4(0, 1, 0, 1); - impostorVertices[34].tex = XMFLOAT4(stepX * 6, 1, 0, 0); + impostorVertices_pos[34] = XMFLOAT3(1, 0, -1); + impostorVertices_nor[34] = XMFLOAT3(0, 1, 0); + impostorVertices_tex[34] = XMFLOAT2(stepX * 6, 1); - impostorVertices[35].pos = XMFLOAT4(1, 0, 1, 0); - impostorVertices[35].nor = XMFLOAT4(0, 1, 0, 1); - impostorVertices[35].tex = XMFLOAT4(stepX * 6, 0, 0, 0); + impostorVertices_pos[35] = XMFLOAT3(1, 0, 1); + impostorVertices_nor[35] = XMFLOAT3(0, 1, 0); + impostorVertices_tex[35] = XMFLOAT2(stepX * 6, 0); - MeshComponent::Vertex_POS impostorVertices_POS[ARRAYSIZE(impostorVertices)]; - MeshComponent::Vertex_TEX impostorVertices_TEX[ARRAYSIZE(impostorVertices)]; - for (int i = 0; i < ARRAYSIZE(impostorVertices); ++i) + MeshComponent::Vertex_POS impostorVertices_POS[count]; + MeshComponent::Vertex_TEX impostorVertices_TEX[count]; + for (int i = 0; i < count; ++i) { - impostorVertices_POS[i].FromFULL(impostorVertices[i]); - impostorVertices_TEX[i].FromFULL(impostorVertices[i]); + impostorVertices_POS[i].FromFULL(impostorVertices_pos[i], impostorVertices_nor[i], 0); + impostorVertices_TEX[i].FromFULL(impostorVertices_tex[i]); } @@ -1374,7 +1378,7 @@ void wiRenderer::LoadShaders() { VertexLayoutDesc layout[] = { - { "POSITION_NORMAL_WIND_MATID", 0, MeshComponent::Vertex_POS::FORMAT, 0, APPEND_ALIGNED_ELEMENT, INPUT_PER_VERTEX_DATA, 0 }, + { "POSITION_NORMAL_SUBSETINDEX", 0, MeshComponent::Vertex_POS::FORMAT, 0, APPEND_ALIGNED_ELEMENT, INPUT_PER_VERTEX_DATA, 0 }, }; vertexShaders[VSTYPE_OBJECT_DEBUG] = static_cast(wiResourceManager::GetShaderManager()->add(SHADERPATH + "objectVS_debug.cso", wiResourceManager::VERTEXSHADER)); device->CreateInputLayout(layout, ARRAYSIZE(layout), vertexShaders[VSTYPE_OBJECT_DEBUG]->code.data, vertexShaders[VSTYPE_OBJECT_DEBUG]->code.size, vertexLayouts[VLTYPE_OBJECT_DEBUG]); @@ -1382,9 +1386,9 @@ void wiRenderer::LoadShaders() { VertexLayoutDesc layout[] = { - { "POSITION_NORMAL_WIND_MATID", 0, MeshComponent::Vertex_POS::FORMAT, 0, APPEND_ALIGNED_ELEMENT, INPUT_PER_VERTEX_DATA, 0 }, - { "TEXCOORD", 0, MeshComponent::Vertex_TEX::FORMAT, 1, APPEND_ALIGNED_ELEMENT, INPUT_PER_VERTEX_DATA, 0 }, - { "PREVPOS", 0, MeshComponent::Vertex_POS::FORMAT, 2, APPEND_ALIGNED_ELEMENT, INPUT_PER_VERTEX_DATA, 0 }, + { "POSITION_NORMAL_SUBSETINDEX", 0, MeshComponent::Vertex_POS::FORMAT, 0, APPEND_ALIGNED_ELEMENT, INPUT_PER_VERTEX_DATA, 0 }, + { "TEXCOORD", 0, MeshComponent::Vertex_TEX::FORMAT, 1, APPEND_ALIGNED_ELEMENT, INPUT_PER_VERTEX_DATA, 0 }, + { "PREVPOS", 0, MeshComponent::Vertex_POS::FORMAT, 2, APPEND_ALIGNED_ELEMENT, INPUT_PER_VERTEX_DATA, 0 }, { "MATI", 0, FORMAT_R32G32B32A32_FLOAT, 3, APPEND_ALIGNED_ELEMENT, INPUT_PER_INSTANCE_DATA, 1 }, { "MATI", 1, FORMAT_R32G32B32A32_FLOAT, 3, APPEND_ALIGNED_ELEMENT, INPUT_PER_INSTANCE_DATA, 1 }, @@ -1401,7 +1405,7 @@ void wiRenderer::LoadShaders() { VertexLayoutDesc layout[] = { - { "POSITION_NORMAL_WIND_MATID", 0, MeshComponent::Vertex_POS::FORMAT, 0, APPEND_ALIGNED_ELEMENT, INPUT_PER_VERTEX_DATA, 0 }, + { "POSITION_NORMAL_SUBSETINDEX", 0, MeshComponent::Vertex_POS::FORMAT, 0, APPEND_ALIGNED_ELEMENT, INPUT_PER_VERTEX_DATA, 0 }, { "MATI", 0, FORMAT_R32G32B32A32_FLOAT, 1, APPEND_ALIGNED_ELEMENT, INPUT_PER_INSTANCE_DATA, 1 }, { "MATI", 1, FORMAT_R32G32B32A32_FLOAT, 1, APPEND_ALIGNED_ELEMENT, INPUT_PER_INSTANCE_DATA, 1 }, @@ -1415,8 +1419,8 @@ void wiRenderer::LoadShaders() { VertexLayoutDesc layout[] = { - { "POSITION_NORMAL_WIND_MATID", 0, MeshComponent::Vertex_POS::FORMAT, 0, APPEND_ALIGNED_ELEMENT, INPUT_PER_VERTEX_DATA, 0 }, - { "TEXCOORD", 0, MeshComponent::Vertex_TEX::FORMAT, 1, APPEND_ALIGNED_ELEMENT, INPUT_PER_VERTEX_DATA, 0 }, + { "POSITION_NORMAL_SUBSETINDEX", 0, MeshComponent::Vertex_POS::FORMAT, 0, APPEND_ALIGNED_ELEMENT, INPUT_PER_VERTEX_DATA, 0 }, + { "TEXCOORD", 0, MeshComponent::Vertex_TEX::FORMAT, 1, APPEND_ALIGNED_ELEMENT, INPUT_PER_VERTEX_DATA, 0 }, { "MATI", 0, FORMAT_R32G32B32A32_FLOAT, 2, APPEND_ALIGNED_ELEMENT, INPUT_PER_INSTANCE_DATA, 1 }, { "MATI", 1, FORMAT_R32G32B32A32_FLOAT, 2, APPEND_ALIGNED_ELEMENT, INPUT_PER_INSTANCE_DATA, 1 }, @@ -1430,7 +1434,7 @@ void wiRenderer::LoadShaders() { VertexLayoutDesc layout[] = { - { "POSITION_NORMAL_WIND_MATID", 0, MeshComponent::Vertex_POS::FORMAT, 0, APPEND_ALIGNED_ELEMENT, INPUT_PER_VERTEX_DATA, 0 }, + { "POSITION_NORMAL_SUBSETINDEX", 0, MeshComponent::Vertex_POS::FORMAT, 0, APPEND_ALIGNED_ELEMENT, INPUT_PER_VERTEX_DATA, 0 }, { "MATI", 0, FORMAT_R32G32B32A32_FLOAT, 1, APPEND_ALIGNED_ELEMENT, INPUT_PER_INSTANCE_DATA, 1 }, { "MATI", 1, FORMAT_R32G32B32A32_FLOAT, 1, APPEND_ALIGNED_ELEMENT, INPUT_PER_INSTANCE_DATA, 1 }, @@ -1444,7 +1448,7 @@ void wiRenderer::LoadShaders() { VertexLayoutDesc layout[] = { - { "POSITION_NORMAL_WIND_MATID", 0, MeshComponent::Vertex_POS::FORMAT, 0, APPEND_ALIGNED_ELEMENT, INPUT_PER_VERTEX_DATA, 0 }, + { "POSITION_NORMAL_SUBSETINDEX", 0, MeshComponent::Vertex_POS::FORMAT, 0, APPEND_ALIGNED_ELEMENT, INPUT_PER_VERTEX_DATA, 0 }, { "TEXCOORD", 0, MeshComponent::Vertex_TEX::FORMAT, 1, APPEND_ALIGNED_ELEMENT, INPUT_PER_VERTEX_DATA, 0 }, { "MATI", 0, FORMAT_R32G32B32A32_FLOAT, 2, APPEND_ALIGNED_ELEMENT, INPUT_PER_INSTANCE_DATA, 1 }, @@ -3482,23 +3486,23 @@ void wiRenderer::UpdateRenderData(GRAPHICSTHREAD threadID) GetDevice()->BindResources(CS, vbs, SKINNINGSLOT_IN_VERTEX_POS, ARRAYSIZE(vbs), threadID); GetDevice()->BindUAVs(CS, sos, 0, ARRAYSIZE(sos), threadID); - GetDevice()->Dispatch((UINT)ceilf((float)mesh.vertices_POS.size() / SKINNING_COMPUTE_THREADCOUNT), 1, 1, 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); } - 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); - } + //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); + //} } @@ -5235,7 +5239,7 @@ void wiRenderer::RenderMeshes(const XMFLOAT3& eye, const CulledCollection& culle for (MeshComponent::MeshSubset& subset : mesh.subsets) { - if (subset.subsetIndices.empty()) + if (subset.indexCount == 0) { continue; } @@ -5396,7 +5400,7 @@ void wiRenderer::RenderMeshes(const XMFLOAT3& eye, const CulledCollection& culle SetAlphaRef(material.alphaRef, threadID); - device->DrawIndexedInstanced((int)subset.subsetIndices.size(), k, subset.indexBufferOffset, 0, 0, threadID); + device->DrawIndexedInstanced((int)subset.indexCount, k, subset.indexOffset, 0, 0, threadID); } } @@ -7828,22 +7832,14 @@ RAY wiRenderer::getPickRay(long cursorX, long cursorY) { return RAY(lineStart, rayDirection); } -wiRenderer::RayIntersectWorldResult wiRenderer::RayIntersectWorld(const RAY& ray, UINT renderTypeMask, uint32_t layerMask, bool dynamicObjects, bool onlyVisible) +wiRenderer::RayIntersectWorldResult wiRenderer::RayIntersectWorld(const RAY& ray, UINT renderTypeMask, uint32_t layerMask) { Scene& scene = GetScene(); RayIntersectWorldResult result; - //if (spTree != nullptr) + if (scene.objects.GetCount() > 0) { - //CulledList culledObjects; - //spTree->getVisible(ray, culledObjects); - - //if (culledObjects.empty()) - //{ - // return result; - //} - const XMVECTOR rayOrigin = XMLoadFloat3(&ray.origin); const XMVECTOR rayDirection = XMVector3Normalize(XMLoadFloat3(&ray.direction)); @@ -7869,14 +7865,6 @@ wiRenderer::RayIntersectWorldResult wiRenderer::RayIntersectWorld(const RAY& ray { continue; } - if (!dynamicObjects && object.IsDynamic()) - { - continue; - } - if (onlyVisible && object.IsOccluded() && GetOcclusionCullingEnabled()) - { - continue; - } const CullableComponent& cullable = *scene.cullables.GetComponent(entity); if (!ray.intersects(cullable.aabb)) @@ -7885,11 +7873,11 @@ wiRenderer::RayIntersectWorldResult wiRenderer::RayIntersectWorld(const RAY& ray } const MeshComponent& mesh = *scene.meshes.GetComponent(object.meshID); - if (mesh.vertices_POS.size() >= _arraySize) + if (mesh.vertex_positions.size() >= _arraySize) { // grow preallocated vector helper array _mm_free(_vertices); - _arraySize = (mesh.vertices_POS.size() + 1) * 2; + _arraySize = (mesh.vertex_positions.size() + 1) * 2; _vertices = (XMVECTOR*)_mm_malloc(sizeof(XMVECTOR)*_arraySize, 16); } @@ -7901,62 +7889,68 @@ wiRenderer::RayIntersectWorldResult wiRenderer::RayIntersectWorld(const RAY& ray const XMVECTOR rayOrigin_local = XMVector3Transform(rayOrigin, objectMat_Inverse); const XMVECTOR rayDirection_local = XMVector3Normalize(XMVector3TransformNormal(rayDirection, objectMat_Inverse)); - MeshComponent::Vertex_FULL _tmpvert; - if (mesh.IsSkinned()) { const ArmatureComponent& armature = *scene.armatures.GetComponent(mesh.armatureID); - for (size_t vertexI = 0; vertexI < mesh.vertices_POS.size(); ++vertexI) + for (size_t vertexI = 0; vertexI < mesh.vertex_positions.size(); ++vertexI) { - XMVECTOR pos = mesh.vertices_POS[vertexI].LoadPOS(); + XMVECTOR pos = XMLoadFloat3(&mesh.vertex_positions[vertexI]); - const XMFLOAT4& ind = mesh.vertices_BON[vertexI].GetInd_FULL(); - const XMFLOAT4& wei = mesh.vertices_BON[vertexI].GetWei_FULL(); + const XMUINT4& ind = mesh.vertex_boneindices[vertexI]; + const XMFLOAT4& wei = mesh.vertex_boneweights[vertexI]; - XMMATRIX sump = XMLoadFloat4x4(&armature.skinningMatrices[(int)ind.x]) * wei.x; - sump += XMLoadFloat4x4(&armature.skinningMatrices[(int)ind.y]) * wei.y; - sump += XMLoadFloat4x4(&armature.skinningMatrices[(int)ind.z]) * wei.z; - sump += XMLoadFloat4x4(&armature.skinningMatrices[(int)ind.w]) * wei.w; + XMMATRIX sump = XMLoadFloat4x4(&armature.skinningMatrices[ind.x]) * wei.x; + sump += XMLoadFloat4x4(&armature.skinningMatrices[ind.y]) * wei.y; + sump += XMLoadFloat4x4(&armature.skinningMatrices[ind.z]) * wei.z; + sump += XMLoadFloat4x4(&armature.skinningMatrices[ind.w]) * wei.w; _vertices[i] = XMVector3Transform(pos, sump); } } else if (mesh.IsDynamicVB()) { - for (size_t i = 0; i < mesh.vertices_Transformed_POS.size(); ++i) - { - _vertices[i] = mesh.vertices_Transformed_POS[i].LoadPOS(); - } + //for (size_t i = 0; i < mesh.vertices_Transformed_POS.size(); ++i) + //{ + // _vertices[i] = mesh.vertices_Transformed_POS[i].LoadPOS(); + //} + assert(0); } else { - for (size_t i = 0; i < mesh.vertices_POS.size(); ++i) + for (size_t i = 0; i < mesh.vertex_positions.size(); ++i) { - _vertices[i] = mesh.vertices_POS[i].LoadPOS(); + _vertices[i] = XMLoadFloat3(&mesh.vertex_positions[i]); } } - for (size_t i = 0; i < mesh.indices.size(); i += 3) + int subsetCounter = 0; + for (auto& subset : mesh.subsets) { - int i0 = mesh.indices[i], i1 = mesh.indices[i + 1], i2 = mesh.indices[i + 2]; - float distance; - if (TriangleTests::Intersects(rayOrigin_local, rayDirection_local, _vertices[i0], _vertices[i1], _vertices[i2], distance)) + for (size_t i = 0; i < subset.indexCount; i += 3) { - XMVECTOR pos = XMVector3Transform(XMVectorAdd(rayOrigin_local, rayDirection_local*distance), objectMat); - distance = wiMath::Distance(pos, rayOrigin); - - if (distance < result.distance) + uint32_t i0 = mesh.indices[subset.indexOffset + i + 0]; + uint32_t i1 = mesh.indices[subset.indexOffset + i + 1]; + uint32_t i2 = mesh.indices[subset.indexOffset + i + 2]; + float distance; + if (TriangleTests::Intersects(rayOrigin_local, rayDirection_local, _vertices[i0], _vertices[i1], _vertices[i2], distance)) { - XMVECTOR nor = XMVector3Normalize(XMVector3TransformNormal(XMVector3Normalize(XMVector3Cross(XMVectorSubtract(_vertices[i2], _vertices[i1]), XMVectorSubtract(_vertices[i1], _vertices[i0]))), objectMat)); + XMVECTOR pos = XMVector3Transform(XMVectorAdd(rayOrigin_local, rayDirection_local*distance), objectMat); + distance = wiMath::Distance(pos, rayOrigin); - result.entity = entity; - XMStoreFloat3(&result.position, pos); - XMStoreFloat3(&result.normal, nor); - result.distance = distance; - result.subsetIndex = (int)mesh.vertices_POS[i0].GetMaterialIndex(); + if (distance < result.distance) + { + XMVECTOR nor = XMVector3Normalize(XMVector3TransformNormal(XMVector3Normalize(XMVector3Cross(XMVectorSubtract(_vertices[i2], _vertices[i1]), XMVectorSubtract(_vertices[i1], _vertices[i0]))), objectMat)); + + result.entity = entity; + XMStoreFloat3(&result.position, pos); + XMStoreFloat3(&result.normal, nor); + result.distance = distance; + result.subsetIndex = subsetCounter; + } } } + subsetCounter++; } } @@ -8282,7 +8276,7 @@ void wiRenderer::CreateImpostor(Entity entity, GRAPHICSTHREAD threadID) for (MeshComponent::MeshSubset& subset : mesh.subsets) { - if (subset.subsetIndices.empty()) + if (subset.indexCount == 0) { continue; } @@ -8294,7 +8288,7 @@ void wiRenderer::CreateImpostor(Entity entity, GRAPHICSTHREAD threadID) GetDevice()->BindResource(PS, material.GetNormalMap(), TEXSLOT_ONDEMAND1, threadID); GetDevice()->BindResource(PS, material.GetSurfaceMap(), TEXSLOT_ONDEMAND2, threadID); - GetDevice()->DrawIndexedInstanced((int)subset.subsetIndices.size(), 1, subset.indexBufferOffset, 0, 0, threadID); + GetDevice()->DrawIndexedInstanced((int)subset.indexCount, 1, subset.indexOffset, 0, 0, threadID); } diff --git a/WickedEngine/wiRenderer.h b/WickedEngine/wiRenderer.h index fa52d64c4..c87d2286e 100644 --- a/WickedEngine/wiRenderer.h +++ b/WickedEngine/wiRenderer.h @@ -465,7 +465,7 @@ public: float distance = FLT_MAX; int subsetIndex = -1; }; - static RayIntersectWorldResult RayIntersectWorld(const RAY& ray, UINT renderTypeMask = RENDERTYPE_OPAQUE, uint32_t layerMask = 0xFFFFFFFF, bool dynamicObjects = true, bool onlyVisible = false); + static RayIntersectWorldResult RayIntersectWorld(const RAY& ray, UINT renderTypeMask = RENDERTYPE_OPAQUE, uint32_t layerMask = ~0); static PHYSICS* physicsEngine; diff --git a/WickedEngine/wiSceneSystem.cpp b/WickedEngine/wiSceneSystem.cpp index 23c5b3d6a..35027f91f 100644 --- a/WickedEngine/wiSceneSystem.cpp +++ b/WickedEngine/wiSceneSystem.cpp @@ -210,236 +210,196 @@ namespace wiSceneSystem void MeshComponent::CreateRenderData() { - // First, assemble vertex, index arrays: - - // In case of recreate, delete data first: - vertices_POS.clear(); - vertices_TEX.clear(); - vertices_BON.clear(); - - // De-interleave vertex arrays: - vertices_POS.resize(vertices_FULL.size()); - vertices_TEX.resize(vertices_FULL.size()); - // do not resize vertices_BON just yet, not every mesh will need bone vertex data! - for (size_t i = 0; i < vertices_FULL.size(); ++i) - { - // Normalize normals: - float alpha = vertices_FULL[i].nor.w; - XMVECTOR nor = XMLoadFloat4(&vertices_FULL[i].nor); - nor = XMVector3Normalize(nor); - XMStoreFloat4(&vertices_FULL[i].nor, nor); - vertices_FULL[i].nor.w = alpha; - - // Normalize bone weights: - XMFLOAT4& wei = vertices_FULL[i].wei; - float len = wei.x + wei.y + wei.z + wei.w; - if (len > 0) - { - wei.x /= len; - wei.y /= len; - wei.z /= len; - wei.w /= len; - - if (vertices_BON.empty()) - { - // Allocate full bone vertex data when we find a correct bone weight. - vertices_BON.resize(vertices_FULL.size()); - } - vertices_BON[i].FromFULL(vertices_FULL[i]); - } - - // Split and type conversion: - vertices_POS[i].FromFULL(vertices_FULL[i]); - vertices_TEX[i].FromFULL(vertices_FULL[i]); - } - - // Save original vertices. This will be input for CPU skinning / soft bodies - vertices_Transformed_POS = vertices_POS; - vertices_Transformed_PRE = vertices_POS; // pre <- pos!! (previous positions will have the current positions initially) - - // Map subset indices: - for (auto& subset : subsets) - { - subset.subsetIndices.clear(); - } - for (size_t i = 0; i < indices.size(); ++i) - { - uint32_t index = indices[i]; - const XMFLOAT4& tex = vertices_FULL[index].tex; - unsigned int materialIndex = (unsigned int)floor(tex.z); - - assert((materialIndex < (unsigned int)subsets.size()) && "Bad subset index!"); - - MeshSubset& subset = subsets[materialIndex]; - subset.subsetIndices.push_back(index); - - if (index >= 65536) - { - indexFormat = INDEXFORMAT_32BIT; - } - } - - - //// Goal positions, normals are controlling blending between animation and physics states for soft body rendering: - //goalPositions.clear(); - //goalNormals.clear(); - //if (goalVG >= 0) - //{ - // goalPositions.resize(vertexGroups[goalVG].vertices.size()); - // goalNormals.resize(vertexGroups[goalVG].vertices.size()); - //} - - - //// Mapping render vertices to physics vertex representation: - //// the physics vertices contain unique position, not duplicated by texcoord or normals - //// this way we can map several renderable vertices to one physics vertex - //// but the mapping function will actually be indexed by renderable vertex index for efficient retrieval. - //if (!physicsverts.empty() && physicalmapGP.empty()) - //{ - // for (size_t i = 0; i < vertices_POS.size(); ++i) - // { - // for (size_t j = 0; j < physicsverts.size(); ++j) - // { - // if (fabs(vertices_POS[i].pos.x - physicsverts[j].x) < FLT_EPSILON - // && fabs(vertices_POS[i].pos.y - physicsverts[j].y) < FLT_EPSILON - // && fabs(vertices_POS[i].pos.z - physicsverts[j].z) < FLT_EPSILON - // ) - // { - // physicalmapGP.push_back(static_cast(j)); - // break; - // } - // } - // } - //} - - - - // Create actual GPU data: - - GPUBufferDesc bd; - SubresourceData InitData; - HRESULT hr; - //if (!IsDynamicVB()) + // Create index buffer GPU data: { - ZeroMemory(&bd, sizeof(bd)); + uint32_t counter = 0; + uint8_t stride; + void* gpuIndexData; + if (vertex_positions.size() > 65535) + { + indexFormat = INDEXFORMAT_32BIT; + gpuIndexData = new uint32_t[indices.size()]; + stride = sizeof(uint32_t); + + for (auto& x : indices) + { + static_cast(gpuIndexData)[counter++] = static_cast(x); + } + + } + else + { + indexFormat = INDEXFORMAT_16BIT; + gpuIndexData = new uint16_t[indices.size()]; + stride = sizeof(uint16_t); + + for (auto& x : indices) + { + static_cast(gpuIndexData)[counter++] = static_cast(x); + } + + } + + + GPUBufferDesc bd; + bd.Usage = USAGE_IMMUTABLE; + bd.CPUAccessFlags = 0; + bd.BindFlags = BIND_INDEX_BUFFER | BIND_SHADER_RESOURCE; + bd.MiscFlags = 0; + bd.StructureByteStride = stride; + bd.Format = GetIndexFormat() == INDEXFORMAT_16BIT ? FORMAT_R16_UINT : FORMAT_R32_UINT; + + SubresourceData InitData; + InitData.pSysMem = gpuIndexData; + bd.ByteWidth = (UINT)(stride * indices.size()); + indexBuffer.reset(new GPUBuffer); + hr = wiRenderer::GetDevice()->CreateBuffer(&bd, &InitData, indexBuffer.get()); + assert(SUCCEEDED(hr)); + + SAFE_DELETE_ARRAY(gpuIndexData); + } + + + XMFLOAT3 _min = XMFLOAT3(FLT_MAX, FLT_MAX, FLT_MAX); + XMFLOAT3 _max = XMFLOAT3(-FLT_MAX, -FLT_MAX, -FLT_MAX); + + // vertexBuffer - POSITION + NORMAL + SUBSETINDEX: + { + + 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++; + } + + std::vector vertices(vertex_positions.size()); + for (size_t i = 0; i < vertices.size(); ++i) + { + const XMFLOAT3& pos = vertex_positions[i]; + const XMFLOAT3& nor = vertex_normals[i]; + uint32_t subsetIndex = subsetIndicesLUT[(uint32_t)i]; + vertices[i].FromFULL(pos, nor, subsetIndex); + + _min = wiMath::Min(_min, pos); + _max = wiMath::Max(_max, pos); + } + + GPUBufferDesc bd; bd.Usage = USAGE_IMMUTABLE; bd.CPUAccessFlags = 0; bd.BindFlags = BIND_VERTEX_BUFFER | BIND_SHADER_RESOURCE; bd.MiscFlags = RESOURCE_MISC_BUFFER_ALLOW_RAW_VIEWS; - ZeroMemory(&InitData, sizeof(InitData)); + bd.ByteWidth = (UINT)(sizeof(Vertex_POS) * vertices.size()); - InitData.pSysMem = vertices_POS.data(); - bd.ByteWidth = (UINT)(sizeof(Vertex_POS) * vertices_POS.size()); + SubresourceData InitData; + InitData.pSysMem = vertices.data(); vertexBuffer_POS.reset(new GPUBuffer); hr = wiRenderer::GetDevice()->CreateBuffer(&bd, &InitData, vertexBuffer_POS.get()); assert(SUCCEEDED(hr)); } - if (!vertices_BON.empty()) + aabb.create(_min, _max); + + // skinning buffers: + if (!vertex_boneindices.empty()) { - ZeroMemory(&bd, sizeof(bd)); + std::vector vertices(vertex_boneindices.size()); + for (size_t i = 0; i < vertices.size(); ++i) + { + XMFLOAT4& wei = vertex_boneweights[i]; + // normalize bone weights + float len = wei.x + wei.y + wei.z + wei.w; + if (len > 0) + { + wei.x /= len; + wei.y /= len; + wei.z /= len; + wei.w /= len; + } + vertices[i].FromFULL(vertex_boneindices[i], wei); + } + + GPUBufferDesc bd; bd.Usage = USAGE_IMMUTABLE; bd.BindFlags = BIND_SHADER_RESOURCE; bd.CPUAccessFlags = 0; bd.MiscFlags = RESOURCE_MISC_BUFFER_ALLOW_RAW_VIEWS; + bd.ByteWidth = (UINT)(sizeof(Vertex_BON) * vertices.size()); - InitData.pSysMem = vertices_BON.data(); - bd.ByteWidth = (UINT)(sizeof(Vertex_BON) * vertices_BON.size()); + SubresourceData InitData; + InitData.pSysMem = vertices.data(); vertexBuffer_BON.reset(new GPUBuffer); hr = wiRenderer::GetDevice()->CreateBuffer(&bd, &InitData, vertexBuffer_BON.get()); assert(SUCCEEDED(hr)); - ZeroMemory(&bd, sizeof(bd)); bd.Usage = USAGE_DEFAULT; bd.BindFlags = BIND_VERTEX_BUFFER | BIND_UNORDERED_ACCESS; bd.CPUAccessFlags = 0; bd.MiscFlags = RESOURCE_MISC_BUFFER_ALLOW_RAW_VIEWS; - bd.ByteWidth = (UINT)(sizeof(Vertex_POS) * vertices_POS.size()); + bd.ByteWidth = (UINT)(sizeof(Vertex_POS) * vertex_positions.size()); streamoutBuffer_POS.reset(new GPUBuffer); hr = wiRenderer::GetDevice()->CreateBuffer(&bd, nullptr, streamoutBuffer_POS.get()); assert(SUCCEEDED(hr)); - bd.ByteWidth = (UINT)(sizeof(Vertex_POS) * vertices_POS.size()); + bd.ByteWidth = (UINT)(sizeof(Vertex_POS) * vertex_positions.size()); streamoutBuffer_PRE.reset(new GPUBuffer); hr = wiRenderer::GetDevice()->CreateBuffer(&bd, nullptr, streamoutBuffer_PRE.get()); assert(SUCCEEDED(hr)); } - // texture coordinate buffers are always static: - ZeroMemory(&bd, sizeof(bd)); - bd.Usage = USAGE_IMMUTABLE; - bd.CPUAccessFlags = 0; - bd.BindFlags = BIND_VERTEX_BUFFER | BIND_SHADER_RESOURCE; - bd.MiscFlags = 0; - bd.StructureByteStride = sizeof(Vertex_TEX); - bd.ByteWidth = (UINT)(bd.StructureByteStride * vertices_TEX.size()); - bd.Format = Vertex_TEX::FORMAT; - InitData.pSysMem = vertices_TEX.data(); - vertexBuffer_TEX.reset(new GPUBuffer); - hr = wiRenderer::GetDevice()->CreateBuffer(&bd, &InitData, vertexBuffer_TEX.get()); - assert(SUCCEEDED(hr)); - - - // Remap index buffer to be continuous across subsets and create gpu buffer data: - uint32_t counter = 0; - uint8_t stride; - void* gpuIndexData; - if (GetIndexFormat() == INDEXFORMAT_16BIT) + // texture coordinate buffers: { - gpuIndexData = new uint16_t[indices.size()]; - stride = sizeof(uint16_t); - } - else - { - gpuIndexData = new uint32_t[indices.size()]; - stride = sizeof(uint32_t); - } - - for (MeshSubset& subset : subsets) - { - if (subset.subsetIndices.empty()) + std::vector vertices(vertex_texcoords.size()); + for (size_t i = 0; i < vertices.size(); ++i) { - continue; + vertices[i].FromFULL(vertex_texcoords[i]); } - subset.indexBufferOffset = counter; - switch (GetIndexFormat()) + GPUBufferDesc bd; + bd.Usage = USAGE_IMMUTABLE; + bd.CPUAccessFlags = 0; + bd.BindFlags = BIND_VERTEX_BUFFER | BIND_SHADER_RESOURCE; + bd.MiscFlags = 0; + bd.StructureByteStride = sizeof(Vertex_TEX); + bd.ByteWidth = (UINT)(bd.StructureByteStride * vertices.size()); + bd.Format = Vertex_TEX::FORMAT; + + SubresourceData InitData; + InitData.pSysMem = vertices.data(); + vertexBuffer_TEX.reset(new GPUBuffer); + hr = wiRenderer::GetDevice()->CreateBuffer(&bd, &InitData, vertexBuffer_TEX.get()); + assert(SUCCEEDED(hr)); + } + + } + void MeshComponent::RemoveVertex(size_t index) + { + for (size_t ind = 0; ind < indices.size(); ++ind) + { + if (indices[ind] == index) { - case INDEXFORMAT_16BIT: - for (auto& x : subset.subsetIndices) - { - static_cast(gpuIndexData)[counter] = static_cast(x); - counter++; - } - break; - default: - for (auto& x : subset.subsetIndices) - { - static_cast(gpuIndexData)[counter] = static_cast(x); - counter++; - } - break; + indices[ind] = static_cast(index); + } + else if (indices[ind] > index && indices[ind] > 0) + { + indices[ind]--; } } - ZeroMemory(&bd, sizeof(bd)); - bd.Usage = USAGE_IMMUTABLE; - bd.CPUAccessFlags = 0; - bd.BindFlags = BIND_INDEX_BUFFER | BIND_SHADER_RESOURCE; - bd.MiscFlags = 0; - bd.StructureByteStride = stride; - bd.Format = GetIndexFormat() == INDEXFORMAT_16BIT ? FORMAT_R16_UINT : FORMAT_R32_UINT; - InitData.pSysMem = gpuIndexData; - bd.ByteWidth = (UINT)(stride * indices.size()); - indexBuffer.reset(new GPUBuffer); - hr = wiRenderer::GetDevice()->CreateBuffer(&bd, &InitData, indexBuffer.get()); - assert(SUCCEEDED(hr)); - - SAFE_DELETE_ARRAY(gpuIndexData); - + 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) { @@ -450,15 +410,16 @@ namespace wiSceneSystem // Compute smooth surface normals: // 1.) Zero normals, they will be averaged later - for (size_t i = 0; i < vertices_FULL.size() - 1; i++) + for (size_t i = 0; i < vertex_normals.size() - 1; i++) { - vertices_FULL[i].nor = XMFLOAT4(0, 0, 0, 0); + vertex_normals[i] = XMFLOAT3(0, 0, 0); } // 2.) Find identical vertices by POSITION, accumulate face normals - for (size_t i = 0; i < vertices_FULL.size() - 1; i++) + for (size_t i = 0; i < vertex_positions.size() - 1; i++) { - Vertex_FULL& v_search = vertices_FULL[i]; + XMFLOAT3& v_search = vertex_positions[i]; + XMFLOAT3& v_search_nor = vertex_normals[i]; for (size_t ind = 0; ind < indices.size() / 3; ++ind) { @@ -466,29 +427,29 @@ namespace wiSceneSystem uint32_t i1 = indices[ind * 3 + 1]; uint32_t i2 = indices[ind * 3 + 2]; - Vertex_FULL& v0 = vertices_FULL[i0]; - Vertex_FULL& v1 = vertices_FULL[i1]; - Vertex_FULL& v2 = vertices_FULL[i2]; + XMFLOAT3& v0 = vertex_positions[i0]; + XMFLOAT3& v1 = vertex_positions[i1]; + XMFLOAT3& v2 = vertex_positions[i2]; bool match_pos0 = - fabs(v_search.pos.x - v0.pos.x) < FLT_EPSILON && - fabs(v_search.pos.y - v0.pos.y) < FLT_EPSILON && - fabs(v_search.pos.z - v0.pos.z) < FLT_EPSILON; + fabs(v_search.x - v0.x) < FLT_EPSILON && + fabs(v_search.y - v0.y) < FLT_EPSILON && + fabs(v_search.z - v0.z) < FLT_EPSILON; bool match_pos1 = - fabs(v_search.pos.x - v1.pos.x) < FLT_EPSILON && - fabs(v_search.pos.y - v1.pos.y) < FLT_EPSILON && - fabs(v_search.pos.z - v1.pos.z) < FLT_EPSILON; + fabs(v_search.x - v1.x) < FLT_EPSILON && + fabs(v_search.y - v1.y) < FLT_EPSILON && + fabs(v_search.z - v1.z) < FLT_EPSILON; bool match_pos2 = - fabs(v_search.pos.x - v2.pos.x) < FLT_EPSILON && - fabs(v_search.pos.y - v2.pos.y) < FLT_EPSILON && - fabs(v_search.pos.z - v2.pos.z) < FLT_EPSILON; + fabs(v_search.x - v2.x) < FLT_EPSILON && + fabs(v_search.y - v2.y) < FLT_EPSILON && + fabs(v_search.z - v2.z) < FLT_EPSILON; if (match_pos0 || match_pos1 || match_pos2) { - XMVECTOR U = XMLoadFloat4(&v2.pos) - XMLoadFloat4(&v0.pos); - XMVECTOR V = XMLoadFloat4(&v1.pos) - XMLoadFloat4(&v0.pos); + XMVECTOR U = XMLoadFloat3(&v2) - XMLoadFloat3(&v0); + XMVECTOR V = XMLoadFloat3(&v1) - XMLoadFloat3(&v0); XMVECTOR N = XMVector3Cross(U, V); N = XMVector3Normalize(N); @@ -496,48 +457,54 @@ namespace wiSceneSystem XMFLOAT3 normal; XMStoreFloat3(&normal, N); - v_search.nor.x += normal.x; - v_search.nor.y += normal.y; - v_search.nor.z += normal.z; + v_search_nor.x += normal.x; + v_search_nor.y += normal.y; + v_search_nor.z += normal.z; } } } - // 3.) Find unique vertices by POSITION and TEXCOORD and MATERIAL and remove duplicates - for (size_t i = 0; i < vertices_FULL.size() - 1; i++) + std::unordered_map subsetIndicesLUT; + uint32_t subsetCounter = 0; + for (auto& subset : subsets) { - const Vertex_FULL& v0 = vertices_FULL[i]; - - for (size_t j = i + 1; j < vertices_FULL.size(); j++) + for (uint32_t i = 0; i < subset.indexCount; ++i) { - const Vertex_FULL& v1 = vertices_FULL[j]; + uint32_t index = indices[subset.indexOffset + i]; + subsetIndicesLUT[index] = subsetCounter; + } + subsetCounter++; + } + + // 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]; bool unique_pos = - fabs(v0.pos.x - v1.pos.x) < FLT_EPSILON && - fabs(v0.pos.y - v1.pos.y) < FLT_EPSILON && - fabs(v0.pos.z - v1.pos.z) < FLT_EPSILON; + fabs(p0.x - p1.x) < FLT_EPSILON && + fabs(p0.y - p1.y) < FLT_EPSILON && + fabs(p0.z - p1.z) < FLT_EPSILON; bool unique_tex = - fabs(v0.tex.x - v1.tex.x) < FLT_EPSILON && - fabs(v0.tex.y - v1.tex.y) < FLT_EPSILON && - (int)v0.tex.z == (int)v1.tex.z; + fabs(t0.x - t1.x) < FLT_EPSILON && + fabs(t0.y - t1.y) < FLT_EPSILON && + s0 == s1; 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]--; - } - } - - vertices_FULL.erase(vertices_FULL.begin() + j); + RemoveVertex(j); } } @@ -548,7 +515,7 @@ namespace wiSceneSystem // Compute hard surface normals: std::vector newIndexBuffer; - std::vector newVertexBuffer; + std::vector newVertexBuffer; for (size_t face = 0; face < indices.size() / 3; face++) { @@ -556,12 +523,12 @@ namespace wiSceneSystem uint32_t i1 = indices[face * 3 + 1]; uint32_t i2 = indices[face * 3 + 2]; - Vertex_FULL& v0 = vertices_FULL[i0]; - Vertex_FULL& v1 = vertices_FULL[i1]; - Vertex_FULL& v2 = vertices_FULL[i2]; + XMFLOAT3& p0 = vertex_positions[i0]; + XMFLOAT3& p1 = vertex_positions[i1]; + XMFLOAT3& p2 = vertex_positions[i2]; - XMVECTOR U = XMLoadFloat4(&v2.pos) - XMLoadFloat4(&v0.pos); - XMVECTOR V = XMLoadFloat4(&v1.pos) - XMLoadFloat4(&v0.pos); + XMVECTOR U = XMLoadFloat3(&p2) - XMLoadFloat3(&p0); + XMVECTOR V = XMLoadFloat3(&p1) - XMLoadFloat3(&p0); XMVECTOR N = XMVector3Cross(U, V); N = XMVector3Normalize(N); @@ -569,21 +536,9 @@ namespace wiSceneSystem XMFLOAT3 normal; XMStoreFloat3(&normal, N); - v0.nor.x = normal.x; - v0.nor.y = normal.y; - v0.nor.z = normal.z; - - v1.nor.x = normal.x; - v1.nor.y = normal.y; - v1.nor.z = normal.z; - - v2.nor.x = normal.x; - v2.nor.y = normal.y; - v2.nor.z = normal.z; - - newVertexBuffer.push_back(v0); - newVertexBuffer.push_back(v1); - newVertexBuffer.push_back(v2); + newVertexBuffer.push_back(normal); + newVertexBuffer.push_back(normal); + newVertexBuffer.push_back(normal); newIndexBuffer.push_back(static_cast(newIndexBuffer.size())); newIndexBuffer.push_back(static_cast(newIndexBuffer.size())); @@ -591,7 +546,7 @@ namespace wiSceneSystem } // For hard surface normals, we created a new mesh in the previous loop through faces, so swap data: - vertices_FULL = newVertexBuffer; + vertex_normals = newVertexBuffer; indices = newIndexBuffer; } @@ -614,13 +569,11 @@ namespace wiSceneSystem } void MeshComponent::FlipNormals() { - for (size_t i = 0; i < vertices_FULL.size() - 1; i++) + for (auto& normal : vertex_normals) { - Vertex_FULL& v0 = vertices_FULL[i]; - - v0.nor.x *= -1; - v0.nor.y *= -1; - v0.nor.z *= -1; + normal.x *= -1; + normal.y *= -1; + normal.z *= -1; } CreateRenderData(); diff --git a/WickedEngine/wiSceneSystem.h b/WickedEngine/wiSceneSystem.h index 5b5ea6fa5..8bf2678f8 100644 --- a/WickedEngine/wiSceneSystem.h +++ b/WickedEngine/wiSceneSystem.h @@ -153,25 +153,17 @@ namespace wiSceneSystem struct MeshComponent { - struct Vertex_FULL - { - XMFLOAT4 pos = XMFLOAT4(0, 0, 0, 0); //pos, wind - XMFLOAT4 nor = XMFLOAT4(0, 0, 0, 1); //normal, unused - XMFLOAT4 tex = XMFLOAT4(0, 0, 0, 0); //tex, matIndex, unused - XMFLOAT4 ind = XMFLOAT4(0, 0, 0, 0); //bone indices - XMFLOAT4 wei = XMFLOAT4(0, 0, 0, 0); //bone weights - }; struct Vertex_POS { XMFLOAT3 pos = XMFLOAT3(0.0f, 0.0f, 0.0f); - uint32_t normal_wind_matID = 0; + uint32_t normal_subsetIndex = 0; - void FromFULL(const Vertex_FULL& vert) + void FromFULL(const XMFLOAT3& _pos, const XMFLOAT3& _nor, uint32_t subsetIndex) { - pos.x = vert.pos.x; - pos.y = vert.pos.y; - pos.z = vert.pos.z; - MakeFromParams(XMFLOAT3(vert.nor.x, vert.nor.y, vert.nor.z), vert.pos.w, static_cast(vert.tex.z)); + pos.x = _pos.x; + pos.y = _pos.y; + pos.z = _pos.z; + MakeFromParams(_nor, subsetIndex); } inline XMVECTOR LoadPOS() const { @@ -183,41 +175,36 @@ namespace wiSceneSystem } inline void MakeFromParams(const XMFLOAT3& normal) { - normal_wind_matID = normal_wind_matID & 0xFF000000; // reset only the normals + normal_subsetIndex = normal_subsetIndex & 0xFF000000; // reset only the normals - normal_wind_matID |= (uint32_t)((normal.x * 0.5f + 0.5f) * 255.0f) << 0; - normal_wind_matID |= (uint32_t)((normal.y * 0.5f + 0.5f) * 255.0f) << 8; - normal_wind_matID |= (uint32_t)((normal.z * 0.5f + 0.5f) * 255.0f) << 16; + normal_subsetIndex |= (uint32_t)((normal.x * 0.5f + 0.5f) * 255.0f) << 0; + normal_subsetIndex |= (uint32_t)((normal.y * 0.5f + 0.5f) * 255.0f) << 8; + normal_subsetIndex |= (uint32_t)((normal.z * 0.5f + 0.5f) * 255.0f) << 16; } - inline void MakeFromParams(const XMFLOAT3& normal, float wind, uint32_t materialIndex) + inline void MakeFromParams(const XMFLOAT3& normal, uint32_t subsetIndex) { - assert(materialIndex < 16); // subset materialIndex is packed onto 4 bits + assert(subsetIndex < 256); // subsetIndex is packed onto 8 bits - normal_wind_matID = 0; + normal_subsetIndex = 0; - normal_wind_matID |= (uint32_t)((normal.x * 0.5f + 0.5f) * 255.0f) << 0; - normal_wind_matID |= (uint32_t)((normal.y * 0.5f + 0.5f) * 255.0f) << 8; - normal_wind_matID |= (uint32_t)((normal.z * 0.5f + 0.5f) * 255.0f) << 16; - normal_wind_matID |= ((uint32_t)(wind * 15.0f) & 0x0000000F) << 24; - normal_wind_matID |= (materialIndex & 0x0000000F) << 28; + normal_subsetIndex |= (uint32_t)((normal.x * 0.5f + 0.5f) * 255.0f) << 0; + normal_subsetIndex |= (uint32_t)((normal.y * 0.5f + 0.5f) * 255.0f) << 8; + normal_subsetIndex |= (uint32_t)((normal.z * 0.5f + 0.5f) * 255.0f) << 16; + normal_subsetIndex |= (subsetIndex & 0x000000FF) << 24; } inline XMFLOAT3 GetNor_FULL() const { XMFLOAT3 nor_FULL(0, 0, 0); - nor_FULL.x = (float)((normal_wind_matID >> 0) & 0x000000FF) / 255.0f * 2.0f - 1.0f; - nor_FULL.y = (float)((normal_wind_matID >> 8) & 0x000000FF) / 255.0f * 2.0f - 1.0f; - nor_FULL.z = (float)((normal_wind_matID >> 16) & 0x000000FF) / 255.0f * 2.0f - 1.0f; + nor_FULL.x = (float)((normal_subsetIndex >> 0) & 0x000000FF) / 255.0f * 2.0f - 1.0f; + nor_FULL.y = (float)((normal_subsetIndex >> 8) & 0x000000FF) / 255.0f * 2.0f - 1.0f; + nor_FULL.z = (float)((normal_subsetIndex >> 16) & 0x000000FF) / 255.0f * 2.0f - 1.0f; return nor_FULL; } - inline float GetWind() const - { - return (float)((normal_wind_matID >> 24) & 0x0000000F) / 15.0f; - } inline uint32_t GetMaterialIndex() const { - return (normal_wind_matID >> 28) & 0x0000000F; + return (normal_subsetIndex >> 24) & 0x000000FF; } static const wiGraphicsTypes::FORMAT FORMAT = wiGraphicsTypes::FORMAT::FORMAT_R32G32B32A32_FLOAT; @@ -226,9 +213,9 @@ namespace wiSceneSystem { XMHALF2 tex = XMHALF2(0.0f, 0.0f); - void FromFULL(const Vertex_FULL& vert) + void FromFULL(const XMFLOAT2& texcoords) { - tex = XMHALF2(vert.tex.x, vert.tex.y); + tex = XMHALF2(texcoords.x, texcoords.y); } static const wiGraphicsTypes::FORMAT FORMAT = wiGraphicsTypes::FORMAT::FORMAT_R16G16_FLOAT; @@ -238,29 +225,29 @@ namespace wiSceneSystem uint64_t ind = 0; uint64_t wei = 0; - void FromFULL(const Vertex_FULL& vert) + void FromFULL(const XMUINT4& boneIndices, const XMFLOAT4& boneWeights) { ind = 0; wei = 0; - ind |= (uint64_t)vert.ind.x << 0; - ind |= (uint64_t)vert.ind.y << 16; - ind |= (uint64_t)vert.ind.z << 32; - ind |= (uint64_t)vert.ind.w << 48; + ind |= (uint64_t)boneIndices.x << 0; + ind |= (uint64_t)boneIndices.y << 16; + ind |= (uint64_t)boneIndices.z << 32; + ind |= (uint64_t)boneIndices.w << 48; - wei |= (uint64_t)(vert.wei.x * 65535.0f) << 0; - wei |= (uint64_t)(vert.wei.y * 65535.0f) << 16; - wei |= (uint64_t)(vert.wei.z * 65535.0f) << 32; - wei |= (uint64_t)(vert.wei.w * 65535.0f) << 48; + wei |= (uint64_t)(boneWeights.x * 65535.0f) << 0; + wei |= (uint64_t)(boneWeights.y * 65535.0f) << 16; + wei |= (uint64_t)(boneWeights.z * 65535.0f) << 32; + wei |= (uint64_t)(boneWeights.w * 65535.0f) << 48; } - inline XMFLOAT4 GetInd_FULL() const + inline XMUINT4 GetInd_FULL() const { - XMFLOAT4 ind_FULL(0, 0, 0, 0); + XMUINT4 ind_FULL(0, 0, 0, 0); - ind_FULL.x = (float)((ind >> 0) & 0x0000FFFF); - ind_FULL.y = (float)((ind >> 16) & 0x0000FFFF); - ind_FULL.z = (float)((ind >> 32) & 0x0000FFFF); - ind_FULL.w = (float)((ind >> 48) & 0x0000FFFF); + ind_FULL.x = ((ind >> 0) & 0x0000FFFF); + ind_FULL.y = ((ind >> 16) & 0x0000FFFF); + ind_FULL.z = ((ind >> 32) & 0x0000FFFF); + ind_FULL.w = ((ind >> 48) & 0x0000FFFF); return ind_FULL; } @@ -277,20 +264,18 @@ namespace wiSceneSystem } }; - std::vector vertices_FULL; - std::vector vertices_POS; // position(xyz), normal+wind(w as uint) - std::vector vertices_TEX; // texcoords - std::vector vertices_BON; // bone indices, bone weights - std::vector vertices_Transformed_POS; // for soft body simulation - std::vector vertices_Transformed_PRE; // for soft body simulation + std::vector vertex_positions; + std::vector vertex_normals; + std::vector vertex_texcoords; + std::vector vertex_boneindices; + std::vector vertex_boneweights; std::vector indices; struct MeshSubset { - wiECS::Entity materialID; - UINT indexBufferOffset = 0; - - std::vector subsetIndices; + wiECS::Entity materialID = wiECS::INVALID_ENTITY; + uint32_t indexOffset = 0; + uint32_t indexCount = 0; }; std::vector subsets; @@ -303,8 +288,8 @@ namespace wiSceneSystem // Dynamic vertexbuffers write into a global pool, these will be the offsets into that: bool dynamicVB = false; - UINT bufferOffset_POS; - UINT bufferOffset_PRE; + uint32_t bufferOffset_POS; + uint32_t bufferOffset_PRE; wiGraphicsTypes::INDEXBUFFER_FORMAT indexFormat = wiGraphicsTypes::INDEXFORMAT_16BIT; @@ -334,6 +319,7 @@ namespace wiSceneSystem inline bool IsDynamicVB() const { return dynamicVB; } void CreateRenderData(); + void RemoveVertex(size_t index); void ComputeNormals(bool smooth); void FlipCulling(); void FlipNormals(); @@ -395,6 +381,7 @@ namespace wiSceneSystem std::vector goalNormals; std::vector physicsverts; + std::vector physicsverts_prev; std::vector physicsindices; std::vector physicalmapGP; }; diff --git a/WickedEngine/windHF.hlsli b/WickedEngine/windHF.hlsli deleted file mode 100644 index f42933363..000000000 --- a/WickedEngine/windHF.hlsli +++ /dev/null @@ -1,11 +0,0 @@ -#ifndef _WIND_HF_ -#define _WIND_HF_ - -#include "globals.hlsli" - -inline void affectWind(inout float3 pos, in float weight, float time) { - float3 wind = sin(time + (pos.x + pos.y + pos.z))*g_xFrame_WindDirection.xyz*0.1*weight; - pos += wind; -} - -#endif // _WIND_HF_