コード例 #1
0
        public void EditorThread_CreateScene(IRenderSurface renderSurface, EInputClass cameraInputClass)
        {
            IsCreated = true;
            OnEngineThread(() =>
            {
                Input.RegisterListener(EngineThread_OnInputEvent, cameraInputClass);
                // Create a new renderer scene with our viewport
                SSceneViewInfo viewInfo       = new SSceneViewInfo();
                viewInfo.Fov                  = FieldOfView;
                viewInfo.ScreenFar            = ScreenFar;
                viewInfo.ScreenNear           = ScreenNear;
                viewInfo.FitProjectionToScene = true;
                viewInfo.ViewMatrix           = Matrix.Invert(m_cameraTransform.WorldMatrix);
                viewInfo.ViewLocation         = m_cameraTransform.Position;
                viewInfo.CreateBoundingFrustum();

                m_renderScene = CRenderer.Instance.CreateRenderScene(renderSurface);
                if (m_renderScene != null)
                {
                    m_renderScene.OnDoFrame += EngineThread_Update;
                    m_renderScene.UpdateViewInfo(in viewInfo);
                    m_ambientLight                    = new CAmbientLight();
                    m_ambientLight.LightColor         = Vector4.One * 0.2f;
                    m_directionalLight                = new CDirectionalLight();
                    m_directionalLight.LightDirection = new Vector3(0.3f, -0.7f, 0.0f);
                    m_directionalLight.LightColor     = Vector4.One * 0.8f;
                    m_renderScene.LightManager.AddLight(m_ambientLight);
                    m_renderScene.LightManager.AddLight(m_directionalLight);
                }
            });
        }
コード例 #2
0
        public override void GenerateShadowMaps(Device device, DeviceContext deviceContext, CRenderScene renderScene)
        {
            UserDefinedAnnotation annotation = deviceContext.QueryInterface <UserDefinedAnnotation>();

            annotation.BeginEvent("SpotLightShadowMap");

            deviceContext.Rasterizer.SetViewport(0.0f, 0.0f, CStaticRendererCvars.ShadowMapSize, CStaticRendererCvars.ShadowMapSize);

            deviceContext.ClearDepthStencilView(ShadowMapTexture.GetRenderTarget(), DepthStencilClearFlags.Depth, 1.0f, 0);
            deviceContext.OutputMerger.SetRenderTargets(ShadowMapTexture.GetRenderTarget());

            DepthStencilStateDescription depthStateDesc = new DepthStencilStateDescription()
            {
                IsDepthEnabled  = true,
                DepthWriteMask  = DepthWriteMask.All,
                DepthComparison = Comparison.Less,

                IsStencilEnabled = false,
                StencilReadMask  = 0xFF,
                StencilWriteMask = 0xFF,
            };

            DepthStencilState depthState = new DepthStencilState(device, depthStateDesc);

            deviceContext.OutputMerger.SetDepthStencilState(depthState);

            SSceneViewInfo viewInfo = new SSceneViewInfo()
            {
                FitProjectionToScene = false,
                Fov          = MathUtil.Pi / 2.0f,
                ScreenFar    = Range,
                ScreenNear   = 0.1f,
                ScreenHeight = CStaticRendererCvars.ShadowMapSize,
                ScreenWidth  = CStaticRendererCvars.ShadowMapSize,
                ScreenLeft   = 0.0f,
                ScreenTop    = 0.0f,
                ViewLocation = Transform.WorldPosition,
                ViewMatrix   = Matrix.Invert(Transform.WorldMatrix)
            };

            m_projectionMatrix        = Matrix.PerspectiveFovLH(viewInfo.Fov, 1.0f, viewInfo.ScreenNear, viewInfo.ScreenFar);
            viewInfo.ProjectionMatrix = m_projectionMatrix;
            viewInfo.CreateBoundingFrustum();

            depthState.Dispose();
            renderScene.RenderSceneDepth(device, deviceContext, in viewInfo);

            DepthStencilView nullDepthView = null;

            deviceContext.OutputMerger.SetRenderTargets(nullDepthView);

            annotation.EndEvent();
            annotation.Dispose();
        }
コード例 #3
0
        public void EngineThread_ZoomCameraToFitAsset()
        {
            if (m_currentMeshAsset != null)
            {
                // Zoom camera
                float distance = (Vector3.Distance(m_currentMeshAsset.AABBMin, m_currentMeshAsset.AABBMax) / 2f) / MathUtilities.Tanf(FieldOfView / 2);
                m_cameraTransform.Position = Axis.Forward * -distance;

                SSceneViewInfo viewInfo = new SSceneViewInfo();
                viewInfo.Fov                  = FieldOfView;
                viewInfo.ScreenFar            = ScreenFar;
                viewInfo.ScreenNear           = ScreenNear;
                viewInfo.FitProjectionToScene = true;
                viewInfo.ViewMatrix           = Matrix.Invert(m_cameraTransform.WorldMatrix);
                viewInfo.ViewLocation         = m_cameraTransform.WorldPosition;
                viewInfo.CreateBoundingFrustum();
                m_renderScene?.UpdateViewInfo(in viewInfo);
            }
        }
コード例 #4
0
        public void GetViewInfo(out SSceneViewInfo viewInfo)
        {
            viewInfo = new SSceneViewInfo {
                ScreenTop = ScreenTop, ScreenLeft = ScreenLeft, ScreenWidth = ScreenWidth, ScreenHeight = ScreenHeight
            };

            if (m_viewProviders.Count > 0)
            {
                IViewProvider activeViewProvider = m_viewProviders[0];
                for (int i = 1; i < m_viewProviders.Count; i++)
                {
                    if (activeViewProvider.GetPriority() <= 0)
                    {
                        break;
                    }

                    if (m_viewProviders[i].GetPriority() < activeViewProvider.GetPriority())
                    {
                        activeViewProvider = m_viewProviders[i];
                    }
                }

                viewInfo.ViewLocation     = activeViewProvider.GetViewLocation();
                viewInfo.ViewMatrix       = activeViewProvider.GetViewMatrix();
                viewInfo.ProjectionMatrix = activeViewProvider.GetProjectionMatrix();
                viewInfo.ScreenNear       = activeViewProvider.GetScreenNear();
                viewInfo.ScreenFar        = activeViewProvider.GetScreenFar();
                viewInfo.Fov = activeViewProvider.GetFov();
                viewInfo.FitProjectionToScene = false;
            }
            else
            {
                viewInfo.ViewLocation     = Vector3.Zero;
                viewInfo.ViewMatrix       = Matrix.Identity;
                viewInfo.ProjectionMatrix = Matrix.Identity;
                viewInfo.ScreenNear       = 0.2f;
                viewInfo.ScreenFar        = 100000.0f;
                viewInfo.Fov = MathUtil.PiOverFour;
                viewInfo.FitProjectionToScene = false;
            }

            viewInfo.CreateBoundingFrustum();
        }
コード例 #5
0
        private void EngineThread_Update(float deltaTime)
        {
            m_renderScene.DebugRenderer.DrawArrow(Vector3.Zero, Axis.Forward, 1.0f, Color.Blue.ToColor4(), 0.0f);
            m_renderScene.DebugRenderer.DrawArrow(Vector3.Zero, Axis.Up, 1.0f, Color.Green.ToColor4(), 0.0f);
            m_renderScene.DebugRenderer.DrawArrow(Vector3.Zero, Axis.Right, 1.0f, Color.Red.ToColor4(), 0.0f);
            bool bUpdateViewInfo = false;

            if (m_bIsRightMouseDown)
            {
                float   forwardDelta  = Input.GetNativeAxisValue(EInputAxis.MouseX);
                Vector3 positionDelta = Axis.Forward * forwardDelta * ZoomSpeed;
                m_cameraTransform.Position += positionDelta;
                bUpdateViewInfo             = true;
            }

            if (m_bIsLeftMouseDown)
            {
                float upAxisRotationAngle    = Input.GetNativeAxisValue(EInputAxis.MouseX) * OrbitSpeed;
                float rightAxisRotationAngle = Input.GetNativeAxisValue(EInputAxis.MouseY) * OrbitSpeed;

                Quaternion xRotation = Quaternion.RotationAxis(KlaxMath.Axis.Up, upAxisRotationAngle);
                m_orbitTransform.RotateLocal(xRotation);
                Quaternion yRotation = Quaternion.RotationAxis(m_orbitTransform.Right, rightAxisRotationAngle);
                m_orbitTransform.RotateLocal(yRotation);
                bUpdateViewInfo = true;
            }

            if (bUpdateViewInfo)
            {
                SSceneViewInfo viewInfo = new SSceneViewInfo();
                viewInfo.Fov                  = FieldOfView;
                viewInfo.ScreenFar            = ScreenFar;
                viewInfo.ScreenNear           = ScreenNear;
                viewInfo.FitProjectionToScene = true;
                viewInfo.ViewMatrix           = Matrix.Invert(m_cameraTransform.WorldMatrix);
                viewInfo.ViewLocation         = m_cameraTransform.WorldPosition;
                viewInfo.CreateBoundingFrustum();
                m_renderScene.UpdateViewInfo(in viewInfo);
            }

            if (ShowMeshSelection)
            {
                CWindowRenderer window = m_renderScene.SceneRenderer;
                ImGui.SetNextWindowPos(new System.Numerics.Vector2(0, window.Height - 40));
                ImGui.SetNextWindowSize(new System.Numerics.Vector2(window.Width, 40));
                ImGui.SetNextWindowBgAlpha(0);
                ImGui.Begin("MeshSelection", ImGuiWindowFlags.NoMove | ImGuiWindowFlags.NoTitleBar | ImGuiWindowFlags.NoResize | ImGuiWindowFlags.NoScrollbar);
                if (ImGui.Button("Cu"))
                {
                    EngineThread_SetPreviewMesh(EngineBaseContentLoader.DefaultCube, true, false);
                    m_lastSelectedDefaultMesh = EngineBaseContentLoader.DefaultCube;
                }
                ImGui.SameLine();
                if (ImGui.Button("Sp"))
                {
                    EngineThread_SetPreviewMesh(EngineBaseContentLoader.DefaultSphere, true, false);
                    m_lastSelectedDefaultMesh = EngineBaseContentLoader.DefaultSphere;
                }
                ImGui.SameLine();
                if (ImGui.Button("Cy"))
                {
                    EngineThread_SetPreviewMesh(EngineBaseContentLoader.DefaultCylinder, true, false);
                    m_lastSelectedDefaultMesh = EngineBaseContentLoader.DefaultCylinder;
                }
                ImGui.End();
            }
        }