コード例 #1
0
        public void Shutdown()
        {
            // Release the light object.
            Light  = null;
            Light2 = null;
            // Release the camera object.
            Camera = null;

            // Release the second render to texture object.
            RenderTexture2?.Shutdown();
            RenderTexture2 = null;
            // Release the shadow shader object.
            ShadowShader?.ShutDown();
            ShadowShader = null;
            // Release the depth shader object.
            DepthShader?.ShutDown();
            DepthShader = null;
            /// Release the render to texture object.
            RenderTexture?.Shutdown();
            RenderTexture = null;
            // Release the ground model object.
            GroundModel?.Shutdown();
            GroundModel = null;
            // Release the sphere model object.
            SphereModel?.Shutdown();
            SphereModel = null;
            // Release the cube model object.
            CubeModel?.Shutdown();
            CubeModel = null;
            // Release the Direct3D object.
            D3D?.ShutDown();
            D3D = null;
        }
コード例 #2
0
        private bool RenderSceneToTexture()
        {
            // Set the render target to be the render to texture.
            RenderTexture.SetRenderTarget(D3D.DeviceContext);

            // Clear the render to texture.
            RenderTexture.ClearRenderTarget(D3D.DeviceContext, 0.0f, 0.0f, 0.0f, 1.0f);

            // Get the world matrix from the d3d object.
            Matrix worldMatrix = D3D.WorldMatrix;

            // Generate the light view matrix based on the light's position.
            Light.GenerateViewMatrix();

            // Get the view and orthographic matrices from the light object.
            Matrix lightViewMatrix  = Light.ViewMatrix;
            Matrix lightOrthoMatrix = Light.OrthoMatrix;

            // Translate to the position of the tree.
            Vector3 treePosition = TreeModel.GetPosition();

            Matrix.Translation(treePosition.X, treePosition.Y, treePosition.Z, out worldMatrix);

            // Render the tree trunk with the depth shader.
            TreeModel.RenderTrunk(D3D.DeviceContext);
            if (!DepthShader.Render(D3D.DeviceContext, TreeModel.TrunkIndexCount, worldMatrix, lightViewMatrix, lightOrthoMatrix))
            {
                return(false);
            }

            // Render the tree leaves using the depth transparency shader.
            TreeModel.RenderLeaves(D3D.DeviceContext);
            if (!TransparentDepthShader.Render(D3D.DeviceContext, TreeModel.LeafIndexCount, worldMatrix, lightViewMatrix, lightOrthoMatrix, TreeModel.LeafTexture.TextureResource))
            {
                return(false);
            }

            // Reset the world matrix.
            worldMatrix = D3D.WorldMatrix;

            // Translate to the position of the ground model.
            Vector3 groundPosition = GroundModel.GetPosition();

            Matrix.Translation(groundPosition.X, groundPosition.Y, groundPosition.Z, out worldMatrix);

            // Render the ground model with the depth shader.
            GroundModel.Render(D3D.DeviceContext);
            if (!DepthShader.Render(D3D.DeviceContext, GroundModel.IndexCount, worldMatrix, lightViewMatrix, lightOrthoMatrix))
            {
                return(false);
            }

            // Reset the render target back to the original back buffer and not the render to texture anymore.
            D3D.SetBackBufferRenderTarget();

            // Reset the viewport back to the original.
            D3D.ResetViewPort();

            return(true);
        }
コード例 #3
0
        private bool RenderSceneToTexture()
        {
            // Set the render target to be the render to texture.
            RenderTexture.SetRenderTarget(D3D.DeviceContext);

            // Clear the render to texture.
            RenderTexture.ClearRenderTarget(D3D.DeviceContext, 0.0f, 0.0f, 0.0f, 1.0f);

            // Generate the view matrix based on the camera's position.
            Camera.Render();

            // Get the world, view, projection, ortho, and base view matrices from the camera and Direct3D objects.
            Matrix worldMatrix      = D3D.WorldMatrix;
            Matrix viewMatrix       = Camera.ViewMatrix;
            Matrix projectionMatrix = D3D.ProjectionMatrix;

            // Render the terrain using the depth shader.
            TerrainModel.Render(D3D.DeviceContext);
            if (!DepthShader.Render(D3D.DeviceContext, TerrainModel.IndexCount, worldMatrix, viewMatrix, projectionMatrix))
            {
                return(false);
            }

            // Reset the render target back to the original back buffer and not the render to texture anymore.
            D3D.SetBackBufferRenderTarget();

            // Reset the viewport back to the original.
            D3D.ResetViewPort();

            return(true);
        }
コード例 #4
0
        public void Dispose()
        {
            DefaultShader.Dispose();
            DepthShader.Dispose();
            DepthCubeShader.Dispose();
            DefaultTexture.Dispose();
            FallbackTexture.Dispose();
            WhiteTexture.Dispose();
            BlackTexture.Dispose();
            DefaultMaterial.Dispose();
            DefaultTextureMaterial.Dispose();
            MissingTextureMaterial.Dispose();
            m_shaderRegistry.Dispose();

            foreach (CResource waitingResource in m_waitingResources)
            {
                waitingResource.Dispose();
            }

            m_waitingResources.Clear();

            foreach (var registeredResource in m_registeredResources)
            {
                registeredResource.Value.Dispose();
            }

            m_registeredResources.Clear();
        }
コード例 #5
0
    // add camera
    private unsafe void AddCamera()
    {
        if (enable_rendering == false)
        {
            Destroy(GameObject.Find("PreviewCamera"));
        }
        else
        {
            Destroy(GameObject.Find("DummyCamera"));
        }


        // add camera under root
        GameObject camobj = new GameObject("camera");

        camobj.layer = LayerMask.NameToLayer("PostProcessing");

        camobj.transform.parent = root.transform;
        Camera thecamera = camobj.AddComponent <Camera>();

        // For Furniture Assembly Environment: remove SITE from the culling mask
        thecamera.cullingMask     = 1 + (1 << 1) + (1 << 2) + (1 << 4) + (1 << 8);
        thecamera.backgroundColor = new Color(1f, 1f, 1f);
        thecamera.clearFlags      = CameraClearFlags.SolidColor;

        Shader             segshader = Shader.Find("Unlit/SegmentationColor");
        SegmentationShader shadersub = camobj.AddComponent <SegmentationShader>();

        DepthShader shaderdepth = camobj.AddComponent <DepthShader>();


        // For Furniture Assembly Environment: no post process
        //GameObject pp_obj = GameObject.Find("PostPocessing");
        //PostProcessLayer pp_layer = camobj.AddComponent<PostProcessLayer>();
        //var resources= Resources.FindObjectsOfTypeAll<PostProcessResources>();
        //pp_layer.Init(resources[0]);
        //pp_layer.volumeTrigger = camobj.transform;
        //pp_layer.volumeLayer = 1 << LayerMask.NameToLayer("PostProcessing");

        // set field of view, near, far
        MJP.TCamera cam;
        MJP.GetCamera(-1, &cam);
        thecamera.fieldOfView = cam.fov;

        // For Furniture Assembly Environment: set znear and zfar independent to model extent.
        thecamera.nearClipPlane = 0.01f;
        thecamera.farClipPlane  = 10f;

        //thecamera.nearClipPlane = cam.znear * this.transform.localScale.x;
        //thecamera.farClipPlane = cam.zfar * this.transform.localScale.x;

        // thecamera.enabled = false;
        //camobj.SetActive(enable_rendering);
        // set transform
        MJP.TTransform transform;
        MJP.GetCameraState(-1, &transform);
        SetCamera(thecamera, transform);
    }
コード例 #6
0
ファイル: Camera.cs プロジェクト: yoship1639/MikuMikuWorld
        protected internal override void OnLoad()
        {
            base.OnLoad();

            CreateShadowMap();

            whiteMap    = MMW.GetAsset <Texture2D>("WhiteMap");
            depthShader = (DepthShader)MMW.GetAsset <Shader>("Depth");
            colorShader = (ColorShader)MMW.GetAsset <Shader>("Color");

            depthRT = new RenderTexture(MMW.RenderResolution);
            depthRT.ColorFormat0 = PixelInternalFormat.Rgba16f;
            depthRT.Load();

            colorRT = new RenderTexture(MMW.RenderResolution);
            colorRT.ColorFormat0 = MMW.Configuration.DefaultPixelFormat;
            colorRT.Load();
            randColors = new Color4[ushort.MaxValue + 1];
            for (var i = 0; i < randColors.Length; i++)
            {
                randColors[i] = new Color4(
                    RandomHelper.NextFloat() * 0.5f + 0.5f,
                    RandomHelper.NextFloat() * 0.5f + 0.5f,
                    RandomHelper.NextFloat() * 0.5f + 0.5f,
                    1.0f);
            }

            shadowDepthBias = Matrix4.CreateScale(0.5f) * Matrix4.CreateTranslation(0.5f, 0.5f, 0.5f);

            velocityShader          = (VelocityShader)MMW.GetAsset <Shader>("Velocity");
            velocityRT              = new RenderTexture(MMW.RenderResolution);
            velocityRT.ColorFormat0 = PixelInternalFormat.Rgba16f;
            velocityRT.Load();

            sp = new ShaderUniqueParameter()
            {
                camera = this,
            };

            getter.Add("Orthographic", obj => Orthographic);
            getter.Add("Up", obj => Up);
            getter.Add("Width", obj => Width);
            getter.Add("Height", obj => Height);
            getter.Add("Aspect", obj => Aspect);
            getter.Add("FoV", obj => FoV);
            getter.Add("Near", obj => Near);
            getter.Add("Far", obj => Far);
            getter.Add("Depth", obj => Depth);
            getter.Add("ClearColor", obj => ClearColor);
        }
コード例 #7
0
        public void Shutdown()
        {
            // Release the position object.
            Position = null;
            // Release the light object.
            Light = null;
            // Release the fps object.
            FPS = null;
            // Release the camera object.
            Camera = null;

            // Release the depth shader object.
            DepthShader?.ShutDown();
            DepthShader = null;
            // Release the render to texture object.
            RenderTexture?.Shutdown();
            RenderTexture = null;
            // Release the texture shader object.
            TextureShader?.ShutDown();
            TextureShader = null;
            // Release the debug window bitmap object.
            DebugWindow?.Shutdown();
            DebugWindow = null;
            // Release the text object.
            Text?.Shutdown();
            Text = null;
            // Release the cpu object.
            CPU?.Shutdown();
            CPU = null;
            // Release the terrain shader object.
            TerrainShader?.ShutDown();
            TerrainShader = null;
            // Release the tree object.
            TerrainModel?.ShutDown();
            TerrainModel = null;
            // Release the input object.
            Input?.Shutdown();
            Input = null;
            // Release the Direct3D object.
            D3D?.ShutDown();
            D3D = null;
        }
コード例 #8
0
        public virtual object Clone()
        {
            GeometryModel NewModel = new GeometryModel(this.Name);

            List <Vector3> NewVertexList = new List <Vector3>();

            for (int i = 0; i < VertexList.Count; i++)
            {
                NewVertexList.Add(new Vector3(VertexList[i].X, VertexList[i].Y, VertexList[i].Z));
            }
            List <Vector2> NewTexcoordList = new List <Vector2>();

            for (int i = 0; i < TexcoordList.Count; i++)
            {
                NewTexcoordList.Add(new Vector2(TexcoordList[i].X, TexcoordList[i].Y));
            }
            List <Vector3> NewNormalList = new List <Vector3>();

            for (int i = 0; i < NormalList.Count; i++)
            {
                NewNormalList.Add(new Vector3(NormalList[i].X, NormalList[i].Y, NormalList[i].Z));
            }
            Dictionary <string, string> NewTextureReferences = new Dictionary <string, string>();

            foreach (KeyValuePair <string, string> entry in Textures)
            {
                NewTextureReferences.Add(entry.Key, entry.Value);
            }

            NewModel.Name         = (string)Name.Clone();
            NewModel.VertexList   = NewVertexList;
            NewModel.TexcoordList = NewTexcoordList;
            NewModel.NormalList   = NewNormalList;

            NewModel.Textures    = NewTextureReferences;
            NewModel.Shader      = Shader.Clone();
            NewModel.DepthShader = DepthShader.Clone();
            return(NewModel);
        }
コード例 #9
0
        public void ShutDown()
        {
            DPerfLogger.ShutDown();

            // Release the light object.
            Light = null;
            // Release the position object.
            Position = null;
            // Release the timer object.
            Timer = null;
            // Release the camera object.
            Camera = null;

            // Release the shadow shader object.
            ShadowShader?.ShutDown();
            ShadowShader = null;
            // Release the transparent depth shader object.
            TransparentDepthShader?.ShutDown();
            TransparentDepthShader = null;
            // Release the depth shader object.
            DepthShader?.ShutDown();
            DepthShader = null;
            // Release the render to texture object.
            RenderTexture?.Shutdown();
            RenderTexture = null;
            // Release the tree object.
            TreeModel?.Shutdown();
            TreeModel = null;
            // Release the model object.
            GroundModel?.Shutdown();
            GroundModel = null;
            // Release the input object.
            Input?.Shutdown();
            Input = null;
            // Release the Direct3D object.
            D3D?.ShutDown();
            D3D = null;
        }
コード例 #10
0
        private bool RenderSceneToTexture2()
        {
            // Set the render target to be the render to texture.
            RenderTexture2.SetRenderTarget(D3D.DeviceContext);

            // Clear the render to texture.
            RenderTexture2.ClearRenderTarget(D3D.DeviceContext, 0.0f, 0.0f, 0.0f, 1.0f);

            // Generate the light view matrix based on the light's position.
            Light2.GenerateViewMatrix();

            // Get the world matrix from the d3d object.
            Matrix worldMareix = D3D.WorldMatrix;

            // Get the view and orthographic matrices from the light object.
            Matrix lightViewMatrix  = Light2.ViewMatrix;
            Matrix lightOrthoMatrix = Light2.ProjectionMatrix;

            // Setup the translation matrix for the cube model.
            Vector3 cubePosition = CubeModel.GetPosition();

            Matrix.Translation(cubePosition.X, cubePosition.Y, cubePosition.Z, out worldMareix);

            // Render the cube model with the depth shader.
            CubeModel.Render(D3D.DeviceContext);
            if (!DepthShader.Render(D3D.DeviceContext, CubeModel.IndexCount, worldMareix, lightViewMatrix, lightOrthoMatrix))
            {
                return(false);
            }

            // Reset the world matrix.
            worldMareix = D3D.WorldMatrix;

            // Setup the translation matrix for the sphere model.
            Vector3 spherePosition = SphereModel.GetPosition();

            Matrix.Translation(spherePosition.X, spherePosition.Y, spherePosition.Z, out worldMareix);

            // Render the sphere model with the depth shader.
            SphereModel.Render(D3D.DeviceContext);
            if (!DepthShader.Render(D3D.DeviceContext, SphereModel.IndexCount, worldMareix, lightViewMatrix, lightOrthoMatrix))
            {
                return(false);
            }

            // Reset the world matrix.
            worldMareix = D3D.WorldMatrix;

            // Setup the translation matrix for the ground model.
            Vector3 groundPosition = GroundModel.GetPosition();

            Matrix.Translation(groundPosition.X, groundPosition.Y, groundPosition.Z, out worldMareix);

            // Render the ground model with the depth shader.
            GroundModel.Render(D3D.DeviceContext);
            if (!DepthShader.Render(D3D.DeviceContext, GroundModel.IndexCount, worldMareix, lightViewMatrix, lightOrthoMatrix))
            {
                return(false);
            }

            // Reset the render target back to the original back buffer and not the render to texture anymore.
            D3D.SetBackBufferRenderTarget();

            // Reset the viewport back to the original.
            D3D.ResetViewPort();

            return(true);
        }
コード例 #11
0
        public void Render()
        {
            Stopwatch sw = new Stopwatch();

            sw.Start();

            DirectBitmap bitmap = new DirectBitmap(width, height);

            Rasterizer rasterizer = new Rasterizer();

            //// start of shadow buffer rendering
            Matrix4x4 SB_ModelView  = Matrix4x4Extensions.LookAt(light_dir, center, up);
            Matrix4x4 SB_Projection = Matrix4x4.Identity;
            Matrix4x4 ViewPort      = Matrix4x4Extensions.Viewport(width / 8, height / 8, width * 3 / 4, height * 3 / 4);

            SB_Projection.M43 = 0;

            var shader = new DepthShader(model, ViewPort, SB_Projection, SB_ModelView);

            for (int i = 0; i < model.FacesV.Count; i++)
            {
                Vector4[] screen_coords = new Vector4[3];
                for (int j = 0; j < model.FacesV[i].Length; j++)
                {
                    screen_coords[j] = shader.Vertex(i, j);
                }

                rasterizer.Triangle(screen_coords, shader, bitmap, pictureBox, shadow_buffer, true);
            }
            //// end of shadow buffer rendering

            //// start of picture render by shadow shader ////
            Matrix4x4 M = ViewPort * SB_Projection * SB_ModelView;

            Matrix4x4 ModelView  = Matrix4x4Extensions.LookAt(eye, center, up);
            Matrix4x4 Projection = Matrix4x4.Identity;

            Projection.M43 = -1.0f / (eye - center).Norm();

            var sh_shader = new ShadowShader(model, ViewPort, Projection, ModelView, light_dir, shadow_buffer, width);

            sh_shader.uniform_m        = Projection * ModelView;
            sh_shader.uniform_mit      = sh_shader.uniform_m.Inverse().Transpose();
            sh_shader.uniform_m_shadow = M * (ViewPort * Projection * ModelView).Inverse();

            for (int i = 0; i < model.FacesV.Count; i++)
            {
                Vector4[] screen_coords = new Vector4[3];
                for (int j = 0; j < model.FacesV[i].Length; j++)
                {
                    screen_coords[j] = sh_shader.Vertex(i, j);
                }

                rasterizer.Triangle(screen_coords, sh_shader, bitmap, pictureBox, z_buffer);
            }
            //// end of picture render by shadow shader ////

            sw.Stop();

            model.Texture.Dispose();
            bitmap.Bitmap.RotateFlip(RotateFlipType.Rotate180FlipNone);

            InfoViewer.ShowDiagnosticInfo
                ($"Render Time: {sw.Elapsed.TotalMilliseconds:f2} ms", new PointF(10, 30), Graphics.FromImage(bitmap.Bitmap));

            // output final render
            pictureBox.Image = bitmap.Bitmap;
        }
コード例 #12
0
        public void Shutdown()
        {
            // Release the light object.
            Light = null;
            // Release the camera object.
            Camera = null;

            // Release the soft shadow shader object.
            SoftShadowShader?.ShutDown();
            SoftShadowShader = null;
            // Release the full screen ortho window object.
            FullScreenWindow?.Shutdown();
            FullScreenWindow = null;
            // Release the up sample render to texture object.
            UpSampleTexure?.Shutdown();
            UpSampleTexure = null;
            // Release the vertical blur shader object.
            VerticalBlurShader?.ShutDown();
            VerticalBlurShader = null;
            // Release the vertical blur render to texture object.
            VerticalBlurTexture?.Shutdown();
            VerticalBlurTexture = null;
            // Release the horizontal blur shader object.
            HorizontalBlurShader?.ShutDown();
            HorizontalBlurShader = null;
            // Release the horizontal blur render to texture object.
            HorizontalBlurTexture?.Shutdown();
            HorizontalBlurTexture = null;
            // Release the texture shader object.
            TextureShader?.ShutDown();
            TextureShader = null;
            // Release the small ortho window object.
            SmallWindow?.Shutdown();
            SmallWindow = null;
            // Release the down sample render to texture object.
            DownSampleTexure?.Shutdown();
            DownSampleTexure = null;
            // Release the shadow shader object.
            ShadowShader?.ShutDown();
            ShadowShader = null;
            // Release the black and white render to texture.
            BlackWhiteRenderTexture?.Shutdown();
            BlackWhiteRenderTexture = null;
            // Release the depth shader object.
            DepthShader?.ShutDown();
            DepthShader = null;
            /// Release the render to texture object.
            RenderTexture?.Shutdown();
            RenderTexture = null;
            // Release the ground model object.
            GroundModel?.Shutdown();
            GroundModel = null;
            // Release the sphere model object.
            SphereModel?.Shutdown();
            SphereModel = null;
            // Release the cube model object.
            CubeModel?.Shutdown();
            CubeModel = null;
            // Release the Direct3D object.
            D3D?.ShutDown();
            D3D = null;
        }