public void Execute()
 {
     if (isOrtho)
     {
         OrthoCam cam = new OrthoCam
         {
             forward  = forward,
             up       = up,
             right    = right,
             position = 0
         };
         cam.UpdateTRSMatrix();
         viewProj = mul(proj, cam.worldToCameraMatrix);
     }
     else
     {
         PerspCam cam = new PerspCam
         {
             forward  = forward,
             up       = up,
             right    = right,
             position = 0
         };
         cam.UpdateTRSMatrix();
         viewProj = mul(proj, cam.worldToCameraMatrix);
     }
     invViewProj = inverse(viewProj);
 }
        public override void FrameUpdate(PipelineCamera camera, ref PipelineCommandData data)
        {
            float4x4 proj = GL.GetGPUProjectionMatrix(camera.cam.nonJitteredProjectionMatrix, false);
            float4x4 viewProj;

            if (camera.cam.orthographic)
            {
                OrthoCam cam = new OrthoCam
                {
                    forward  = camera.cam.transform.forward,
                    up       = camera.cam.transform.up,
                    right    = camera.cam.transform.right,
                    position = 0
                };
                cam.UpdateTRSMatrix();
                viewProj = mul(proj, cam.worldToCameraMatrix);
            }
            else
            {
                PerspCam cam = new PerspCam
                {
                    forward  = camera.cam.transform.forward,
                    up       = camera.cam.transform.up,
                    right    = camera.cam.transform.right,
                    position = 0
                };
                cam.UpdateTRSMatrix();
                viewProj = mul(proj, cam.worldToCameraMatrix);
            }
            CommandBuffer buffer = data.buffer;

            buffer.SetGlobalMatrix(_InvSkyVP, inverse(viewProj));
            buffer.SetRenderTarget(color: camera.targets.renderTargetIdentifier, depth: ShaderIDs._DepthBufferTexture);
            buffer.DrawMesh(GraphicsUtility.mesh, Matrix4x4.identity, skyboxMaterial, 0, 0);
        }
Exemple #3
0
        private static void CalculateCubemapMatrix(float3 position, float farClipPlane, float nearClipPlane, out NativeList <float4x4> viewMatrices, out NativeList <float4x4> projMatrices)
        {
            viewMatrices = new NativeList <float4x4>(6, 6, Allocator.Temp);
            projMatrices = new NativeList <float4x4>(6, 6, Allocator.Temp);
            PerspCam cam = new PerspCam();

            cam.aspect        = 1;
            cam.farClipPlane  = farClipPlane;
            cam.nearClipPlane = nearClipPlane;
            cam.position      = position;
            cam.fov           = 90f;
            //Forward
            cam.right   = float3(1, 0, 0);
            cam.up      = float3(0, 1, 0);
            cam.forward = float3(0, 0, 1);
            cam.UpdateTRSMatrix();
            cam.UpdateProjectionMatrix();
            for (int i = 0; i < 6; ++i)
            {
                projMatrices[i] = cam.projectionMatrix;
            }
            viewMatrices[5] = cam.worldToCameraMatrix;
            //Back
            cam.right   = float3(-1, 0, 0);
            cam.up      = float3(0, 1, 0);
            cam.forward = float3(0, 0, -1);
            cam.UpdateTRSMatrix();

            viewMatrices[4] = cam.worldToCameraMatrix;
            //Up
            cam.right   = float3(-1, 0, 0);
            cam.up      = float3(0, 0, 1);
            cam.forward = float3(0, 1, 0);
            cam.UpdateTRSMatrix();
            viewMatrices[3] = cam.worldToCameraMatrix;
            //Down
            cam.right   = float3(-1, 0, 0);
            cam.up      = float3(0, 0, -1);
            cam.forward = float3(0, -1, 0);
            cam.UpdateTRSMatrix();
            viewMatrices[2] = cam.worldToCameraMatrix;
            //Right
            cam.up      = float3(0, 1, 0);
            cam.right   = float3(0, 0, -1);
            cam.forward = float3(1, 0, 0);
            cam.UpdateTRSMatrix();
            viewMatrices[1] = cam.worldToCameraMatrix;
            //Left
            cam.up      = float3(0, 1, 0);
            cam.right   = float3(0, 0, 1);
            cam.forward = float3(-1, 0, 0);
            cam.UpdateTRSMatrix();
            viewMatrices[0] = cam.worldToCameraMatrix;
        }
        public override void FrameUpdate(PipelineCamera camera, ref PipelineCommandData data)
        {
            SkyboxMatrixData skyData = IPerCameraData.GetProperty(camera, () => new SkyboxMatrixData(), this);
            float4x4         proj    = GL.GetGPUProjectionMatrix(camera.cam.nonJitteredProjectionMatrix, false);
            float4x4         viewProj;

            if (camera.cam.orthographic)
            {
                OrthoCam cam = new OrthoCam
                {
                    forward  = camera.cam.transform.forward,
                    up       = camera.cam.transform.up,
                    right    = camera.cam.transform.right,
                    position = 0
                };
                cam.UpdateTRSMatrix();
                viewProj = mul(proj, cam.worldToCameraMatrix);
            }
            else
            {
                PerspCam cam = new PerspCam
                {
                    forward  = camera.cam.transform.forward,
                    up       = camera.cam.transform.up,
                    right    = camera.cam.transform.right,
                    position = 0
                };
                cam.UpdateTRSMatrix();
                viewProj = mul(proj, cam.worldToCameraMatrix);
            }
            CommandBuffer buffer = data.buffer;

            buffer.SetGlobalMatrix(_InvSkyVP, inverse(viewProj));
            buffer.SetGlobalMatrix(_LastSkyVP, skyData.lastVP);
            targets[0] = camera.targets.renderTargetIdentifier;
            targets[1] = camera.targets.motionVectorTexture;
            buffer.SetRenderTarget(colors: targets, depth: camera.targets.depthBuffer);
            buffer.DrawMesh(GraphicsUtility.mesh, Matrix4x4.identity, skyboxMaterial, 0, 0);
            skyData.lastVP = viewProj;
        }
 private static void GetMatrix(float4x4 *allmat, ref PerspCam persp, float3 position)
 {
     persp.position = position;
     //X
     persp.up      = float3(0, 1, 0);
     persp.right   = float3(0, 0, -1);
     persp.forward = float3(1, 0, 0);
     persp.UpdateTRSMatrix();
     allmat[1] = persp.worldToCameraMatrix;
     //-X
     persp.up      = float3(0, 1, 0);
     persp.right   = float3(0, 0, 1);
     persp.forward = float3(-1, 0, 0);
     persp.UpdateTRSMatrix();
     allmat[0] = persp.worldToCameraMatrix;
     //Y
     persp.right   = float3(-1, 0, 0);
     persp.up      = float3(0, 0, 1);
     persp.forward = float3(0, 1, 0);
     persp.UpdateTRSMatrix();
     allmat[2] = persp.worldToCameraMatrix;
     //-Y
     persp.right   = float3(-1, 0, 0);
     persp.up      = float3(0, 0, -1);
     persp.forward = float3(0, -1, 0);
     persp.UpdateTRSMatrix();
     allmat[3] = persp.worldToCameraMatrix;
     //Z
     persp.right   = float3(1, 0, 0);
     persp.up      = float3(0, 1, 0);
     persp.forward = float3(0, 0, 1);
     persp.UpdateTRSMatrix();
     allmat[5] = persp.worldToCameraMatrix;
     //-Z
     persp.right   = float3(-1, 0, 0);
     persp.up      = float3(0, 1, 0);
     persp.forward = float3(0, 0, -1);
     persp.UpdateTRSMatrix();
     allmat[4] = persp.worldToCameraMatrix;
 }
        public override void DrawCubeMap(MPointLight lit, ref RenderClusterOptions opts, ref CubeCullingBuffer buffer, int offset)
        {
            CommandBuffer cb            = opts.command;
            ComputeShader shader        = opts.cullingShader;
            Material      depthMaterial = opts.proceduralMaterial;

            cb.SetComputeIntParam(shader, ShaderIDs._LightOffset, offset);
            ComputeShaderUtility.Dispatch(shader, cb, CubeFunction.RunFrustumCull, baseBuffer.clusterCount, 256);
            PerspCam cam = new PerspCam();

            cam.aspect        = 1;
            cam.farClipPlane  = lit.range;
            cam.nearClipPlane = 0.3f;
            cam.position      = lit.position;
            cam.fov           = 90f;
            Matrix4x4 vpMatrix;

            cb.SetGlobalVector(ShaderIDs._LightPos, new Vector4(lit.position.x, lit.position.y, lit.position.z, lit.range));
            cb.SetGlobalBuffer(ShaderIDs.verticesBuffer, baseBuffer.verticesBuffer);
            cb.SetGlobalBuffer(ShaderIDs.resultBuffer, baseBuffer.resultBuffer);
            //Forward
            cam.forward  = Vector3.forward;
            cam.up       = Vector3.down;
            cam.right    = Vector3.left;
            cam.position = lit.position;
            cam.UpdateTRSMatrix();
            cam.UpdateProjectionMatrix();
            vpMatrix = GL.GetGPUProjectionMatrix(cam.projectionMatrix, true) * cam.worldToCameraMatrix;
            cb.SetRenderTarget(lit.shadowmapTexture, 0, CubemapFace.NegativeZ);
            cb.ClearRenderTarget(true, true, Color.white);
            cb.SetGlobalMatrix(ShaderIDs._VP, vpMatrix);
            offset = offset * 20;
            cb.DrawProceduralIndirect(Matrix4x4.identity, depthMaterial, 0, MeshTopology.Triangles, buffer.indirectDrawBuffer, offset);
            //Back
            cam.forward = Vector3.back;
            cam.up      = Vector3.down;
            cam.right   = Vector3.right;
            cam.UpdateTRSMatrix();
            cam.UpdateProjectionMatrix();
            vpMatrix = GL.GetGPUProjectionMatrix(cam.projectionMatrix, true) * cam.worldToCameraMatrix;
            cb.SetRenderTarget(lit.shadowmapTexture, 0, CubemapFace.PositiveZ);
            cb.ClearRenderTarget(true, true, Color.white);
            cb.SetGlobalMatrix(ShaderIDs._VP, vpMatrix);
            cb.DrawProceduralIndirect(Matrix4x4.identity, depthMaterial, 0, MeshTopology.Triangles, buffer.indirectDrawBuffer, offset);
            //Up
            cam.forward = Vector3.up;
            cam.up      = Vector3.back;
            cam.right   = Vector3.right;
            cam.UpdateTRSMatrix();
            cam.UpdateProjectionMatrix();
            vpMatrix = GL.GetGPUProjectionMatrix(cam.projectionMatrix, true) * cam.worldToCameraMatrix;
            cb.SetRenderTarget(lit.shadowmapTexture, 0, CubemapFace.PositiveY);
            cb.ClearRenderTarget(true, true, Color.white);
            cb.SetGlobalMatrix(ShaderIDs._VP, vpMatrix);
            cb.DrawProceduralIndirect(Matrix4x4.identity, depthMaterial, 0, MeshTopology.Triangles, buffer.indirectDrawBuffer, offset);
            //Down
            cam.forward = Vector3.down;
            cam.up      = Vector3.forward;
            cam.right   = Vector3.right;
            cam.UpdateTRSMatrix();
            cam.UpdateProjectionMatrix();
            vpMatrix = GL.GetGPUProjectionMatrix(cam.projectionMatrix, true) * cam.worldToCameraMatrix;
            cb.SetRenderTarget(lit.shadowmapTexture, 0, CubemapFace.NegativeY);
            cb.ClearRenderTarget(true, true, Color.white);
            cb.SetGlobalMatrix(ShaderIDs._VP, vpMatrix);
            cb.DrawProceduralIndirect(Matrix4x4.identity, depthMaterial, 0, MeshTopology.Triangles, buffer.indirectDrawBuffer, offset);
            //Right
            cam.forward = Vector3.right;
            cam.up      = Vector3.down;
            cam.right   = Vector3.forward;
            cam.UpdateTRSMatrix();
            cam.UpdateProjectionMatrix();
            vpMatrix = GL.GetGPUProjectionMatrix(cam.projectionMatrix, true) * cam.worldToCameraMatrix;
            cb.SetRenderTarget(lit.shadowmapTexture, 0, CubemapFace.PositiveX);
            cb.ClearRenderTarget(true, true, Color.white);
            cb.SetGlobalMatrix(ShaderIDs._VP, vpMatrix);
            cb.DrawProceduralIndirect(Matrix4x4.identity, depthMaterial, 0, MeshTopology.Triangles, buffer.indirectDrawBuffer, offset);
            //Left
            cam.forward = Vector3.left;
            cam.up      = Vector3.down;
            cam.right   = Vector3.back;
            cam.UpdateTRSMatrix();
            cam.UpdateProjectionMatrix();
            vpMatrix = GL.GetGPUProjectionMatrix(cam.projectionMatrix, true) * cam.worldToCameraMatrix;
            cb.SetRenderTarget(lit.shadowmapTexture, 0, CubemapFace.NegativeX);
            cb.ClearRenderTarget(true, true, Color.white);
            cb.SetGlobalMatrix(ShaderIDs._VP, vpMatrix);
            cb.DrawProceduralIndirect(Matrix4x4.identity, depthMaterial, 0, MeshTopology.Triangles, buffer.indirectDrawBuffer, offset);
        }
        public static void DrawShadow(MPointLight lit, CommandBuffer cb, MaterialPropertyBlock block, ref CubeCullingBuffer buffer, ref PipelineBaseBuffer baseBuffer, ComputeShader shader, int offset, Material depthMaterial)
        {
            cb.SetComputeIntParam(shader, ShaderIDs._LightOffset, offset);
            ComputeShaderUtility.Dispatch(shader, cb, RunFrustumCull, baseBuffer.clusterCount, 64);
            PerspCam cam = new PerspCam();

            cam.aspect        = 1;
            cam.farClipPlane  = lit.range;
            cam.nearClipPlane = 0.3f;
            cam.position      = lit.position;
            cam.fov           = 90f;
            Matrix4x4 vpMatrix;

            block.SetVector(ShaderIDs._LightPos, new Vector4(lit.position.x, lit.position.y, lit.position.z, lit.range));
            PipelineFunctions.SetShaderBuffer(ref baseBuffer, block);
            //Forward
            cam.forward  = Vector3.forward;
            cam.up       = Vector3.down;
            cam.right    = Vector3.left;
            cam.position = lit.position;
            cam.UpdateTRSMatrix();
            cam.UpdateProjectionMatrix();
            vpMatrix = GL.GetGPUProjectionMatrix(cam.projectionMatrix, true) * cam.worldToCameraMatrix;
            cb.SetRenderTarget(lit.shadowmapTexture, 0, CubemapFace.NegativeZ);
            cb.ClearRenderTarget(true, true, Color.white);
            block.SetMatrix(ShaderIDs._VP, vpMatrix);
            offset = offset * 20;
            cb.DrawProceduralIndirect(Matrix4x4.identity, depthMaterial, 0, MeshTopology.Triangles, buffer.indirectDrawBuffer, offset, block);
            //Back
            cam.forward = Vector3.back;
            cam.up      = Vector3.down;
            cam.right   = Vector3.right;
            cam.UpdateTRSMatrix();
            cam.UpdateProjectionMatrix();
            vpMatrix = GL.GetGPUProjectionMatrix(cam.projectionMatrix, true) * cam.worldToCameraMatrix;
            cb.SetRenderTarget(lit.shadowmapTexture, 0, CubemapFace.PositiveZ);
            cb.ClearRenderTarget(true, true, Color.white);
            block.SetMatrix(ShaderIDs._VP, vpMatrix);
            cb.DrawProceduralIndirect(Matrix4x4.identity, depthMaterial, 0, MeshTopology.Triangles, buffer.indirectDrawBuffer, offset, block);
            //Up
            cam.forward = Vector3.up;
            cam.up      = Vector3.back;
            cam.right   = Vector3.right;
            cam.UpdateTRSMatrix();
            cam.UpdateProjectionMatrix();
            vpMatrix = GL.GetGPUProjectionMatrix(cam.projectionMatrix, true) * cam.worldToCameraMatrix;
            cb.SetRenderTarget(lit.shadowmapTexture, 0, CubemapFace.PositiveY);
            cb.ClearRenderTarget(true, true, Color.white);
            block.SetMatrix(ShaderIDs._VP, vpMatrix);
            cb.DrawProceduralIndirect(Matrix4x4.identity, depthMaterial, 0, MeshTopology.Triangles, buffer.indirectDrawBuffer, offset, block);
            //Down
            cam.forward = Vector3.down;
            cam.up      = Vector3.forward;
            cam.right   = Vector3.right;
            cam.UpdateTRSMatrix();
            cam.UpdateProjectionMatrix();
            vpMatrix = GL.GetGPUProjectionMatrix(cam.projectionMatrix, true) * cam.worldToCameraMatrix;
            cb.SetRenderTarget(lit.shadowmapTexture, 0, CubemapFace.NegativeY);
            cb.ClearRenderTarget(true, true, Color.white);
            block.SetMatrix(ShaderIDs._VP, vpMatrix);
            cb.DrawProceduralIndirect(Matrix4x4.identity, depthMaterial, 0, MeshTopology.Triangles, buffer.indirectDrawBuffer, offset, block);
            //Right
            cam.forward = Vector3.right;
            cam.up      = Vector3.down;
            cam.right   = Vector3.forward;
            cam.UpdateTRSMatrix();
            cam.UpdateProjectionMatrix();
            vpMatrix = GL.GetGPUProjectionMatrix(cam.projectionMatrix, true) * cam.worldToCameraMatrix;
            cb.SetRenderTarget(lit.shadowmapTexture, 0, CubemapFace.PositiveX);
            cb.ClearRenderTarget(true, true, Color.white);
            block.SetMatrix(ShaderIDs._VP, vpMatrix);
            cb.DrawProceduralIndirect(Matrix4x4.identity, depthMaterial, 0, MeshTopology.Triangles, buffer.indirectDrawBuffer, offset, block);
            //Left
            cam.forward = Vector3.left;
            cam.up      = Vector3.down;
            cam.right   = Vector3.back;
            cam.UpdateTRSMatrix();
            cam.UpdateProjectionMatrix();
            vpMatrix = GL.GetGPUProjectionMatrix(cam.projectionMatrix, true) * cam.worldToCameraMatrix;
            cb.SetRenderTarget(lit.shadowmapTexture, 0, CubemapFace.NegativeX);
            cb.ClearRenderTarget(true, true, Color.white);
            block.SetMatrix(ShaderIDs._VP, vpMatrix);
            cb.DrawProceduralIndirect(Matrix4x4.identity, depthMaterial, 0, MeshTopology.Triangles, buffer.indirectDrawBuffer, offset, block);
        }