From 9f98c9ff9df27cfd3f0122a571a0b0c3bd4b543e Mon Sep 17 00:00:00 2001 From: turanszkij Date: Wed, 3 Oct 2018 18:48:21 +0100 Subject: [PATCH] system updates; gltf importer updates; editor updates --- Editor/CameraWindow.cpp | 2 +- Editor/Editor.cpp | 25 +++--- Editor/MeshWindow.cpp | 30 ++++++- Editor/MeshWindow.h | 2 + Editor/ModelImporter_GLTF.cpp | 33 +++++++ WickedEngine/wiRenderer.cpp | 3 +- WickedEngine/wiSceneSystem.cpp | 115 ++++++++++++++----------- WickedEngine/wiSceneSystem.h | 5 +- WickedEngine/wiSceneSystem_BindLua.cpp | 15 +++- WickedEngine/wiSceneSystem_BindLua.h | 1 + WickedEngine/wiVersion.cpp | 2 +- scripts/camera_animation_clamped.lua | 2 +- scripts/camera_animation_repeat.lua | 2 +- 13 files changed, 164 insertions(+), 73 deletions(-) diff --git a/Editor/CameraWindow.cpp b/Editor/CameraWindow.cpp index dae70689a..917acbf19 100644 --- a/Editor/CameraWindow.cpp +++ b/Editor/CameraWindow.cpp @@ -11,7 +11,7 @@ void CameraWindow::ResetCam() camera_transform.ClearTransform(); camera_transform.Translate(XMFLOAT3(0, 2, -10)); camera_transform.UpdateTransform(); - wiRenderer::GetCamera().UpdateCamera(&camera_transform); + wiRenderer::GetCamera().TransformCamera(camera_transform); camera_target.ClearTransform(); camera_target.UpdateTransform(); diff --git a/Editor/Editor.cpp b/Editor/Editor.cpp index 7a720ff1e..51543c288 100644 --- a/Editor/Editor.cpp +++ b/Editor/Editor.cpp @@ -56,7 +56,6 @@ void Editor::Initialize() infoDisplay.resolution = true; wiRenderer::GetDevice()->SetVSyncEnabled(true); - //wiRenderer::physicsEngine = new wiBULLET(); wiRenderer::SetOcclusionCullingEnabled(true); wiInputManager::GetInstance()->addXInput(new wiXInput()); @@ -753,16 +752,6 @@ void EditorComponent::Update(float dt) Scene& scene = wiRenderer::GetScene(); CameraComponent& camera = wiRenderer::GetCamera(); - // Follow camera proxy: - if (cameraWnd->followCheckBox->IsEnabled() && cameraWnd->followCheckBox->GetCheck()) - { - TransformComponent* proxy = scene.transforms.GetComponent(cameraWnd->proxy); - if (proxy != nullptr) - { - cameraWnd->camera_transform.Lerp(cameraWnd->camera_transform, *proxy, 1.0f - cameraWnd->followSlider->GetValue()); - } - } - animWnd->Update(); // Exit cinema mode: @@ -1322,7 +1311,19 @@ void EditorComponent::Update(float dt) renderPath->Update(dt); - camera.UpdateCamera(&cameraWnd->camera_transform); + // Follow camera proxy: + if (cameraWnd->followCheckBox->IsEnabled() && cameraWnd->followCheckBox->GetCheck()) + { + TransformComponent* proxy = scene.transforms.GetComponent(cameraWnd->proxy); + if (proxy != nullptr) + { + cameraWnd->camera_transform.Lerp(cameraWnd->camera_transform, *proxy, 1.0f - cameraWnd->followSlider->GetValue()); + cameraWnd->camera_transform.UpdateTransform(); + } + } + + camera.TransformCamera(cameraWnd->camera_transform); + camera.UpdateCamera(); } void EditorComponent::Render() { diff --git a/Editor/MeshWindow.cpp b/Editor/MeshWindow.cpp index 77d586a2d..59f1b7ef7 100644 --- a/Editor/MeshWindow.cpp +++ b/Editor/MeshWindow.cpp @@ -16,7 +16,7 @@ MeshWindow::MeshWindow(wiGUI* gui) : GUI(gui) meshWindow = new wiWindow(GUI, "Mesh Window"); - meshWindow->SetSize(XMFLOAT2(800, 640)); + meshWindow->SetSize(XMFLOAT2(800, 700)); meshWindow->SetEnabled(false); GUI->AddWidget(meshWindow); @@ -193,6 +193,34 @@ MeshWindow::MeshWindow(wiGUI* gui) : GUI(gui) }); meshWindow->AddWidget(computeNormalsHardButton); + recenterButton = new wiButton("Recenter"); + recenterButton->SetTooltip("Recenter mesh to AABB center."); + recenterButton->SetSize(XMFLOAT2(240, 30)); + recenterButton->SetPos(XMFLOAT2(x - 50, y += step)); + recenterButton->OnClick([&](wiEventArgs args) { + MeshComponent* mesh = wiRenderer::GetScene().meshes.GetComponent(entity); + if (mesh != nullptr) + { + mesh->Recenter(); + SetEntity(entity); + } + }); + meshWindow->AddWidget(recenterButton); + + recenterToBottomButton = new wiButton("RecenterToBottom"); + recenterToBottomButton->SetTooltip("Recenter mesh to AABB bottom."); + recenterToBottomButton->SetSize(XMFLOAT2(240, 30)); + recenterToBottomButton->SetPos(XMFLOAT2(x - 50, y += step)); + recenterToBottomButton->OnClick([&](wiEventArgs args) { + MeshComponent* mesh = wiRenderer::GetScene().meshes.GetComponent(entity); + if (mesh != nullptr) + { + mesh->RecenterToBottom(); + SetEntity(entity); + } + }); + meshWindow->AddWidget(recenterToBottomButton); + diff --git a/Editor/MeshWindow.h b/Editor/MeshWindow.h index 8d1eb5fc6..c364f57c5 100644 --- a/Editor/MeshWindow.h +++ b/Editor/MeshWindow.h @@ -31,5 +31,7 @@ public: wiButton* flipNormalsButton; wiButton* computeNormalsSmoothButton; wiButton* computeNormalsHardButton; + wiButton* recenterButton; + wiButton* recenterToBottomButton; }; diff --git a/Editor/ModelImporter_GLTF.cpp b/Editor/ModelImporter_GLTF.cpp index c9a297d0c..b5a86428d 100644 --- a/Editor/ModelImporter_GLTF.cpp +++ b/Editor/ModelImporter_GLTF.cpp @@ -222,10 +222,31 @@ void LoadNode(tinygltf::Node* node, Entity parent, LoaderState& state) { transform.translation_local = XMFLOAT3((float)node->translation[0], (float)node->translation[1], (float)node->translation[2]); } + if (!node->matrix.empty()) + { + transform.world._11 = (float)node->matrix[0]; + transform.world._12 = (float)node->matrix[1]; + transform.world._13 = (float)node->matrix[2]; + transform.world._14 = (float)node->matrix[3]; + transform.world._21 = (float)node->matrix[4]; + transform.world._22 = (float)node->matrix[5]; + transform.world._23 = (float)node->matrix[6]; + transform.world._24 = (float)node->matrix[7]; + transform.world._31 = (float)node->matrix[8]; + transform.world._32 = (float)node->matrix[9]; + transform.world._33 = (float)node->matrix[10]; + transform.world._34 = (float)node->matrix[11]; + transform.world._41 = (float)node->matrix[12]; + transform.world._42 = (float)node->matrix[13]; + transform.world._43 = (float)node->matrix[14]; + transform.world._44 = (float)node->matrix[15]; + transform.ApplyTransform(); // this creates S, R, T vectors from world matrix + } // 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 (relative to parent) already, so it must be IDENTITY! + transform.world = IDENTITYMATRIX; if (parent != INVALID_ENTITY) { @@ -610,6 +631,18 @@ void ImportModel_GLTF(const std::string& fileName) mesh.vertex_texcoords[vertexOffset + i].y = tex.y; } } + else if (!attr_name.compare("TEXCOORD_1")) + { + mesh.vertex_atlas.resize(vertexOffset + vertexCount); + assert(stride == 8); + for (size_t i = 0; i < vertexCount; ++i) + { + const XMFLOAT2& tex = ((XMFLOAT2*)data)[i]; + + mesh.vertex_atlas[vertexOffset + i].x = tex.x; + mesh.vertex_atlas[vertexOffset + i].y = tex.y; + } + } else if (!attr_name.compare("JOINTS_0")) { mesh.vertex_boneindices.resize(vertexOffset + vertexCount); diff --git a/WickedEngine/wiRenderer.cpp b/WickedEngine/wiRenderer.cpp index ded2b2225..e42367ccd 100644 --- a/WickedEngine/wiRenderer.cpp +++ b/WickedEngine/wiRenderer.cpp @@ -6066,8 +6066,9 @@ void RefreshImpostors(GRAPHICSTHREAD threadID) camera_transform.RotateRollPitchYaw(XMFLOAT3(0, XM_2PI * (float)i / (float)impostorCaptureAngles, 0)); camera_transform.UpdateTransform(); - impostorcamera.UpdateCamera(&camera_transform); + impostorcamera.TransformCamera(camera_transform); impostorcamera.UpdateProjection(); + impostorcamera.UpdateCamera(); UpdateCameraCB(impostorcamera, threadID); for (auto& subset : mesh.subsets) diff --git a/WickedEngine/wiSceneSystem.cpp b/WickedEngine/wiSceneSystem.cpp index debe395c4..90f5c1808 100644 --- a/WickedEngine/wiSceneSystem.cpp +++ b/WickedEngine/wiSceneSystem.cpp @@ -716,6 +716,33 @@ namespace wiSceneSystem CreateRenderData(); } + void MeshComponent::Recenter() + { + XMFLOAT3 center = aabb.getCenter(); + + for (auto& pos : vertex_positions) + { + pos.x -= center.x; + pos.y -= center.y; + pos.z -= center.z; + } + + CreateRenderData(); + } + void MeshComponent::RecenterToBottom() + { + XMFLOAT3 center = aabb.getCenter(); + center.y -= aabb.getHalfWidth().y; + + for (auto& pos : vertex_positions) + { + pos.x -= center.x; + pos.y -= center.y; + pos.z -= center.z; + } + + CreateRenderData(); + } void CameraComponent::CreatePerspective(float newWidth, float newHeight, float newNear, float newFar, float newFOV) { @@ -736,40 +763,15 @@ namespace wiSceneSystem XMStoreFloat4x4(&Projection, XMMatrixPerspectiveFovLH(fov, width / height, zFarP, zNearP)); // reverse zbuffer! XMStoreFloat4x4(&realProjection, XMMatrixPerspectiveFovLH(fov, width / height, zNearP, zFarP)); // normal zbuffer! } - void CameraComponent::UpdateCamera(const TransformComponent* transform) + void CameraComponent::UpdateCamera() { SetDirty(false); - XMVECTOR _Eye; - XMVECTOR _At; - XMVECTOR _Up; - - if (transform != nullptr) - { - XMVECTOR S, R, T; - XMMatrixDecompose(&S, &R, &T, XMLoadFloat4x4(&transform->world)); - - _Eye = T; - _At = XMVectorSet(0, 0, 1, 0); - _Up = XMVectorSet(0, 1, 0, 0); - - XMMATRIX _Rot = XMMatrixRotationQuaternion(R); - _At = XMVector3TransformNormal(_At, _Rot); - _Up = XMVector3TransformNormal(_Up, _Rot); - XMStoreFloat3x3(&rotationMatrix, _Rot); - } - else - { - _Eye = XMLoadFloat3(&Eye); - _At = XMLoadFloat3(&At); - _Up = XMLoadFloat3(&Up); - } - XMMATRIX _P = XMLoadFloat4x4(&Projection); XMMATRIX _InvP = XMMatrixInverse(nullptr, _P); XMStoreFloat4x4(&InvProjection, _InvP); - XMMATRIX _V = XMMatrixLookToLH(_Eye, _At, _Up); + XMMATRIX _V = XMLoadFloat4x4(&View); XMMATRIX _VP = XMMatrixMultiply(_V, _P); XMStoreFloat4x4(&View, _V); XMStoreFloat4x4(&VP, _VP); @@ -778,11 +780,30 @@ namespace wiSceneSystem XMStoreFloat4x4(&Projection, _P); XMStoreFloat4x4(&InvProjection, XMMatrixInverse(nullptr, _P)); + frustum.ConstructFrustum(zFarP, realProjection, View); + } + void CameraComponent::TransformCamera(const TransformComponent& transform) + { + SetDirty(); + + XMVECTOR S, R, T; + XMMatrixDecompose(&S, &R, &T, XMLoadFloat4x4(&transform.world)); + + XMVECTOR _Eye = T; + XMVECTOR _At = XMVectorSet(0, 0, 1, 0); + XMVECTOR _Up = XMVectorSet(0, 1, 0, 0); + + XMMATRIX _Rot = XMMatrixRotationQuaternion(R); + _At = XMVector3TransformNormal(_At, _Rot); + _Up = XMVector3TransformNormal(_Up, _Rot); + XMStoreFloat3x3(&rotationMatrix, _Rot); + + XMMATRIX _V = XMMatrixLookToLH(_Eye, _At, _Up); + XMStoreFloat4x4(&View, _V); + XMStoreFloat3(&Eye, _Eye); XMStoreFloat3(&At, _At); XMStoreFloat3(&Up, _Up); - - frustum.ConstructFrustum(zFarP, realProjection, View); } void CameraComponent::Reflect(const XMFLOAT4& plane) { @@ -1620,7 +1641,11 @@ namespace wiSceneSystem CameraComponent& camera = cameras[i]; Entity entity = cameras.GetEntity(i); const TransformComponent* transform = transforms.GetComponent(entity); - camera.UpdateCamera(transform); + if (transform != nullptr) + { + camera.TransformCamera(*transform); + } + camera.UpdateCamera(); } } void RunDecalUpdateSystem( @@ -1733,40 +1758,28 @@ namespace wiSceneSystem switch (light.type) { case LightComponent::DIRECTIONAL: - { if (weather != nullptr) { weather->sunColor = light.color; weather->sunDirection = light.direction; } - aabb.createFromHalfWidth(wiRenderer::GetCamera().Eye, XMFLOAT3(10000, 10000, 10000)); - } - break; + break; case LightComponent::SPOT: - { aabb.createFromHalfWidth(light.position, XMFLOAT3(light.range, light.range, light.range)); - } - break; + break; case LightComponent::POINT: + aabb.createFromHalfWidth(light.position, XMFLOAT3(light.range, light.range, light.range)); + break; case LightComponent::SPHERE: case LightComponent::DISC: case LightComponent::RECTANGLE: case LightComponent::TUBE: - { - if (light.type == LightComponent::POINT) - { - aabb.createFromHalfWidth(light.position, XMFLOAT3(light.range, light.range, light.range)); - } - else - { - XMStoreFloat3(&light.right, XMVector3TransformNormal(XMVectorSet(-1, 0, 0, 0), W)); - XMStoreFloat3(&light.front, XMVector3TransformNormal(XMVectorSet(0, 0, -1, 0), W)); - // area lights have no bounds, just like directional lights (maybe todo) - aabb.createFromHalfWidth(wiRenderer::GetCamera().Eye, XMFLOAT3(10000, 10000, 10000)); - } - } - break; + XMStoreFloat3(&light.right, XMVector3TransformNormal(XMVectorSet(-1, 0, 0, 0), W)); + XMStoreFloat3(&light.front, XMVector3TransformNormal(XMVectorSet(0, 0, -1, 0), W)); + // area lights have no bounds, just like directional lights (todo: but they should have real bounds) + aabb.createFromHalfWidth(wiRenderer::GetCamera().Eye, XMFLOAT3(10000, 10000, 10000)); + break; } } diff --git a/WickedEngine/wiSceneSystem.h b/WickedEngine/wiSceneSystem.h index ea77ed30d..9fe4a4721 100644 --- a/WickedEngine/wiSceneSystem.h +++ b/WickedEngine/wiSceneSystem.h @@ -247,6 +247,8 @@ namespace wiSceneSystem void ComputeNormals(bool smooth); void FlipCulling(); void FlipNormals(); + void Recenter(); + void RecenterToBottom(); void Serialize(wiArchive& archive, uint32_t seed = 0); @@ -646,7 +648,8 @@ namespace wiSceneSystem void CreatePerspective(float newWidth, float newHeight, float newNear, float newFar, float newFOV = XM_PI / 3.0f); void UpdateProjection(); - void UpdateCamera(const TransformComponent* transform = nullptr); + void UpdateCamera(); + void TransformCamera(const TransformComponent& transform); void Reflect(const XMFLOAT4& plane = XMFLOAT4(0, 1, 0, 0)); inline XMVECTOR GetEye() const { return XMLoadFloat3(&Eye); } diff --git a/WickedEngine/wiSceneSystem_BindLua.cpp b/WickedEngine/wiSceneSystem_BindLua.cpp index 900a75c42..a5d6c4316 100644 --- a/WickedEngine/wiSceneSystem_BindLua.cpp +++ b/WickedEngine/wiSceneSystem_BindLua.cpp @@ -672,6 +672,7 @@ const char CameraComponent_BindLua::className[] = "CameraComponent"; Luna::FunctionType CameraComponent_BindLua::methods[] = { lunamethod(CameraComponent_BindLua, UpdateCamera), + lunamethod(CameraComponent_BindLua, TransformCamera), { NULL, NULL } }; Luna::PropertyType CameraComponent_BindLua::properties[] = { @@ -692,6 +693,11 @@ CameraComponent_BindLua::~CameraComponent_BindLua() } int CameraComponent_BindLua::UpdateCamera(lua_State* L) +{ + component->UpdateCamera(); + return 0; +} +int CameraComponent_BindLua::TransformCamera(lua_State* L) { int argc = wiLua::SGetArgCount(L); if (argc > 0) @@ -699,15 +705,18 @@ int CameraComponent_BindLua::UpdateCamera(lua_State* L) TransformComponent_BindLua* transform = Luna::lightcheck(L, 1); if (transform != nullptr) { - component->UpdateCamera(transform->component); + component->TransformCamera(*transform->component); return 0; } else { - wiLua::SError(L, "UpdateCamera(opt TransformComponent transform) invalid argument!"); + wiLua::SError(L, "TransformCamera(TransformComponent transform) invalid argument!"); } } - component->UpdateCamera(); + else + { + wiLua::SError(L, "TransformCamera(TransformComponent transform) not enough arguments!"); + } return 0; } diff --git a/WickedEngine/wiSceneSystem_BindLua.h b/WickedEngine/wiSceneSystem_BindLua.h index 841803373..804bf539b 100644 --- a/WickedEngine/wiSceneSystem_BindLua.h +++ b/WickedEngine/wiSceneSystem_BindLua.h @@ -120,6 +120,7 @@ namespace wiSceneSystem_BindLua ~CameraComponent_BindLua(); int UpdateCamera(lua_State* L); + int TransformCamera(lua_State* L); }; class AnimationComponent_BindLua diff --git a/WickedEngine/wiVersion.cpp b/WickedEngine/wiVersion.cpp index b72bde27a..2c16387bf 100644 --- a/WickedEngine/wiVersion.cpp +++ b/WickedEngine/wiVersion.cpp @@ -9,7 +9,7 @@ namespace wiVersion // minor features, major updates const int minor = 21; // minor bug fixes, alterations, refactors, updates - const int revision = 1; + const int revision = 2; long GetVersion() diff --git a/scripts/camera_animation_clamped.lua b/scripts/camera_animation_clamped.lua index fa7138900..a99032c95 100644 --- a/scripts/camera_animation_clamped.lua +++ b/scripts/camera_animation_clamped.lua @@ -51,7 +51,7 @@ runProcess(function() local d = scene.Component_GetTransform(proxies[math.clamp(rot + 2, 0, count - 1) + 1]) target.CatmullRom(a, b, c, d, tt) target.UpdateTransform() - cam.UpdateCamera(target) + cam.TransformCamera(target) -- Advance animation state: tt = tt + scriptableCameraSpeed * getDeltaTime() diff --git a/scripts/camera_animation_repeat.lua b/scripts/camera_animation_repeat.lua index f842f6f7a..db4b7b0ea 100644 --- a/scripts/camera_animation_repeat.lua +++ b/scripts/camera_animation_repeat.lua @@ -51,7 +51,7 @@ runProcess(function() local d = scene.Component_GetTransform(proxies[(rot + 2) % count + 1]) target.CatmullRom(a, b, c, d, tt) target.UpdateTransform() - cam.UpdateCamera(target) + cam.TransformCamera(target) -- Advance animation state: tt = tt + scriptableCameraSpeed * getDeltaTime()