1313#include < OvCore/ECS/Components/CDirectionalLight.h>
1414#include < OvCore/ECS/Components/CSpotLight.h>
1515
16+ #include < OvDebug/Utils/Assertion.h>
17+
1618#include " OvEditor/Core/EditorRenderer.h"
1719#include " OvEditor/Core/EditorResources.h"
1820#include " OvEditor/Panels/AView.h"
@@ -105,6 +107,32 @@ void OvEditor::Core::EditorRenderer::InitMaterials()
105107 m_guizmoBallMaterial.SetShader (m_context.editorResources ->GetShader (" Guizmo" ));
106108 m_guizmoBallMaterial.SetShader (m_context.editorResources ->GetShader (" Guizmo" ));
107109 m_guizmoBallMaterial.Set (" u_IsBall" , true );
110+
111+ /* Picking Material */
112+ m_actorPickingMaterial.SetShader (m_context.shaderManager [" :Shaders\\ Unlit.glsl" ]);
113+ m_actorPickingMaterial.Set (" u_Diffuse" , FVector4 (1 .f , 1 .f , 1 .f , 1 .0f ));
114+ m_actorPickingMaterial.Set <OvRendering::Resources::Texture*>(" u_DiffuseMap" , nullptr );
115+ m_actorPickingMaterial.SetFrontfaceCulling (false );
116+ m_actorPickingMaterial.SetBackfaceCulling (false );
117+ }
118+
119+ void OvEditor::Core::EditorRenderer::PreparePickingMaterial (OvCore::ECS::Actor& p_actor)
120+ {
121+ uint32_t actorID = static_cast <uint32_t >(p_actor.GetID ());
122+
123+ auto bytes = reinterpret_cast <uint8_t *>(&actorID);
124+ auto color = FVector4{ bytes[0 ] / 255 .0f , bytes[1 ] / 255 .0f , bytes[2 ] / 255 .0f , 1 .0f };
125+
126+ m_actorPickingMaterial.Set (" u_Diffuse" , color);
127+ }
128+
129+ OvMaths::FMatrix4 OvEditor::Core::EditorRenderer::CalculateCameraModelMatrix (OvCore::ECS::Actor& p_actor)
130+ {
131+ auto translation = FMatrix4::Translation (p_actor.transform .GetWorldPosition ());
132+ auto rotation = FQuaternion::ToMatrix4 (p_actor.transform .GetWorldRotation ());
133+ auto scale = FMatrix4::Scaling ({ 0 .4f , 0 .4f , 0 .4f });
134+
135+ return translation * rotation * scale;
108136}
109137
110138void OvEditor::Core::EditorRenderer::RenderScene (const OvMaths::FVector3& p_cameraPosition)
@@ -115,6 +143,69 @@ void OvEditor::Core::EditorRenderer::RenderScene(const OvMaths::FVector3& p_came
115143 m_context.lightSSBO ->Unbind ();
116144}
117145
146+ void OvEditor::Core::EditorRenderer::RenderSceneForActorPicking ()
147+ {
148+ auto & scene = *m_context.sceneManager .GetCurrentScene ();
149+
150+ /* Render models */
151+ for (auto modelRenderer : scene.GetFastAccessComponents ().modelRenderers )
152+ {
153+ auto & actor = modelRenderer->owner ;
154+
155+ if (actor.IsActive ())
156+ {
157+ if (auto model = modelRenderer->GetModel ())
158+ {
159+
160+ if (auto materialRenderer = modelRenderer->owner .GetComponent <OvCore::ECS::Components::CMaterialRenderer>())
161+ {
162+ const OvCore::ECS::Components::CMaterialRenderer::MaterialList& materials = materialRenderer->GetMaterials ();
163+ auto modelMatrix = actor.transform .GetWorldMatrix ();
164+ PreparePickingMaterial (actor);
165+
166+ for (auto mesh : model->GetMeshes ())
167+ {
168+ OvCore::Resources::Material* material = nullptr ;
169+
170+ if (mesh->GetMaterialIndex () < MAX_MATERIAL_COUNT)
171+ {
172+ material = materials.at (mesh->GetMaterialIndex ());
173+ if (!material || !material->GetShader ())
174+ material = &m_emptyMaterial;
175+ }
176+
177+ if (material)
178+ {
179+ m_actorPickingMaterial.SetBackfaceCulling (material->HasBackfaceCulling ());
180+ m_actorPickingMaterial.SetFrontfaceCulling (material->HasFrontfaceCulling ());
181+ m_actorPickingMaterial.SetColorWriting (material->HasColorWriting ());
182+ m_actorPickingMaterial.SetDepthTest (material->HasDepthTest ());
183+ m_actorPickingMaterial.SetDepthWriting (material->HasDepthWriting ());
184+
185+ m_context.renderer ->DrawMesh (*mesh, m_actorPickingMaterial, &modelMatrix);
186+ }
187+ }
188+ }
189+ }
190+ }
191+ }
192+
193+ /* Render cameras */
194+ for (auto camera : m_context.sceneManager .GetCurrentScene ()->GetFastAccessComponents ().cameras )
195+ {
196+ auto & actor = camera->owner ;
197+
198+ if (actor.IsActive ())
199+ {
200+ PreparePickingMaterial (actor);
201+ auto & model = *m_context.editorResources ->GetModel (" Camera" );
202+ auto modelMatrix = CalculateCameraModelMatrix (actor);
203+
204+ m_context.renderer ->DrawModelWithSingleMaterial (model, m_actorPickingMaterial, &modelMatrix);
205+ }
206+ }
207+ }
208+
118209void OvEditor::Core::EditorRenderer::RenderUI ()
119210{
120211 m_context.uiManager ->Render ();
@@ -124,10 +215,14 @@ void OvEditor::Core::EditorRenderer::RenderCameras()
124215{
125216 for (auto camera : m_context.sceneManager .GetCurrentScene ()->GetFastAccessComponents ().cameras )
126217 {
127- if (camera->owner .IsActive ())
218+ auto & actor = camera->owner ;
219+
220+ if (actor.IsActive ())
128221 {
129- auto modelMatrix = FMatrix4::Translation (camera->owner .transform .GetWorldPosition ()) * FQuaternion::ToMatrix4 (camera->owner .transform .GetWorldRotation ()) * FMatrix4::Scaling ({ 0 .4f , 0 .4f , 0 .4f });
130- m_context.renderer ->DrawModelWithSingleMaterial (*m_context.editorResources ->GetModel (" Camera" ), m_cameraMaterial, &modelMatrix);
222+ auto & model = *m_context.editorResources ->GetModel (" Camera" );
223+ auto modelMatrix = CalculateCameraModelMatrix (actor);
224+
225+ m_context.renderer ->DrawModelWithSingleMaterial (model, m_cameraMaterial, &modelMatrix);
131226 }
132227 }
133228}
0 commit comments