Esempio n. 1
0
        /// <summary>
        /// Perform the image effect during <see cref="OnRenderImage(RenderTexture, RenderTexture)"/>
        /// </summary>
        /// <param name="material">The material for the effect</param>
        /// <param name="camera">The Camera</param>
        /// <param name="source">The source RenderTexture</param>
        /// <param name="destination">The destination RenderTexture</param>
        protected override void PerformEffect(Material material, Camera camera, RenderTexture source, RenderTexture destination)
        {
            int width  = source.width >> _downscaling;
            int height = source.height >> _downscaling;

            if (_historyDoubleBuffers == null)
            {
                _historyDoubleBuffers = new RenderTexture[2];
            }
            _isFirstFrame |= EnsureRenderTexture(ref _historyDoubleBuffers[0], width, height, RenderTextureFormat.ARGBFloat, FilterMode.Bilinear);
            _isFirstFrame |= EnsureRenderTexture(ref _historyDoubleBuffers[1], width, height, RenderTextureFormat.ARGBFloat, FilterMode.Bilinear);
            _isFirstFrame |= EnsureRenderTexture(ref _raymarchedBuffer, width, height, RenderTextureFormat.ARGBFloat, FilterMode.Bilinear);
            _isFirstFrame |= EnsureRenderTexture(ref _raymarchAvgDepthBuffer, width, height, RenderTextureFormat.RFloat, FilterMode.Bilinear);
            if (_raymarchColorBuffers == null || _raymarchColorBuffers.Length != 2)
            {
                _raymarchColorBuffers = new RenderBuffer[] { _raymarchedBuffer.colorBuffer, _raymarchAvgDepthBuffer.colorBuffer };
            }
            else
            {
                _raymarchColorBuffers[0] = _raymarchedBuffer.colorBuffer;
                _raymarchColorBuffers[1] = _raymarchAvgDepthBuffer.colorBuffer;
            }

            _historyIndex = (_historyIndex + 1) % 2;

            RenderTargetSetup raymarchRenderTargetSetup = new RenderTargetSetup(_raymarchColorBuffers, _raymarchedBuffer.depthBuffer);

            //Pass 0 into raymarched buffer, with regular sampling quality.
            material.SetVector("_ProjectionExtents", GetProjectionExtents(camera));
            material.SetFloat("_RaymarchOffset", _lowDiscrepancySequence.GetNextValue());
            material.SetVector("_RaymarchedBuffer_TexelSize", _raymarchedBuffer.texelSize);
            BlitMRT(raymarchRenderTargetSetup, false, material, 0);

            //Pass 1: blending into active history buffer
            if (_isFirstFrame)
            {
                Graphics.Blit(_raymarchedBuffer, _historyDoubleBuffers[_historyIndex]);
                _previousViewMatrix = camera.worldToCameraMatrix;
            }
            material.SetTexture("_RaymarchedBuffer", _raymarchedBuffer);
            material.SetTexture("_RaymarchedAvgDepthBuffer", _raymarchAvgDepthBuffer);
            material.SetMatrix("_PrevVP", GL.GetGPUProjectionMatrix(camera.projectionMatrix, false) * _previousViewMatrix);
            material.SetFloat("_Ln2OverBlendHalfLife", Mathf.Log(2) / _blendHalfLife);

            int historyOtherIndex = (_historyIndex + 1) % 2;

            Graphics.Blit(_historyDoubleBuffers[_historyIndex], _historyDoubleBuffers[historyOtherIndex], material, 1);

            //Pass 2: apply lighting and blend with scene
            material.SetTexture("_CloudDensityTexture", _historyDoubleBuffers[historyOtherIndex]);
            Graphics.Blit(source, destination, material, 2);

            //Cleanup
            _previousViewMatrix = camera.worldToCameraMatrix;
            _isFirstFrame       = false;
        }
 public RenderTargetBinding(RenderTargetSetup setup)
 {
     this.m_ColorRenderTargets = new RenderTargetIdentifier[setup.color.Length];
     for (int i = 0; i < this.m_ColorRenderTargets.Length; i++)
     {
         this.m_ColorRenderTargets[i] = new RenderTargetIdentifier(setup.color[i], setup.mipLevel, setup.cubemapFace, setup.depthSlice);
     }
     this.m_DepthRenderTarget = setup.depth;
     this.m_ColorLoadActions  = (RenderBufferLoadAction[])setup.colorLoad.Clone();
     this.m_ColorStoreActions = (RenderBufferStoreAction[])setup.colorStore.Clone();
     this.m_DepthLoadAction   = setup.depthLoad;
     this.m_DepthStoreAction  = setup.depthStore;
     this.m_Flags             = RenderTargetFlags.None;
 }
Esempio n. 3
0
        public RenderTargetBinding(RenderTargetSetup setup)
        {
            m_ColorRenderTargets = new RenderTargetIdentifier[setup.color.Length];
            for (int i = 0; i < m_ColorRenderTargets.Length; ++i)
            {
                m_ColorRenderTargets[i] = new RenderTargetIdentifier(setup.color[i], setup.mipLevel, setup.cubemapFace, setup.depthSlice);
            }

            m_DepthRenderTarget = setup.depth;

            m_ColorLoadActions  = (RenderBufferLoadAction[])setup.colorLoad.Clone();
            m_ColorStoreActions = (RenderBufferStoreAction[])setup.colorStore.Clone();

            m_DepthLoadAction  = setup.depthLoad;
            m_DepthStoreAction = setup.depthStore;
        }
Esempio n. 4
0
        public void GeneratePage()
        {
            if (TilesToGenerate.Count == 0)
            {
                return;
            }
            //print("generating page" + Time.frameCount + "  " + TilesToGenerate.Count);

            List <int> TilesForMesh = new List <int>();

            // 优先处理mipmap等级高的tile
            TilesToGenerate.Sort((x, y) => { return(MortonUtility.getMip(x).CompareTo(MortonUtility.getMip(y))); });

            //一次最多贴5个
            for (int i = 0; TilesToGenerate.Count > 0; i++)
            {
                int quadKey = TilesToGenerate[TilesToGenerate.Count - 1];
                TilesToGenerate.RemoveAt(TilesToGenerate.Count - 1);
                TilesForMesh.Add(quadKey);
            }

            SetUpMesh(TilesForMesh);

            RenderBuffer[] colorBuffers = new RenderBuffer[1];

            for (int textureType = 0; textureType < physicalTexture.NumTextureType; textureType++)
            {
                colorBuffers[0] = physicalTexture.PhysicalTextures[textureType].colorBuffer;
                RenderBuffer depthBuffer = physicalTexture.PhysicalTextures[textureType].depthBuffer;
                for (int i = 0; i < 10; i++)
                {
                    SetLod(TilesForMesh, i);
                    RenderTargetSetup rtsMip = new RenderTargetSetup(colorBuffers, depthBuffer, i, CubemapFace.Unknown);
                    Graphics.SetRenderTarget(rtsMip);
                    CommandBuffer tempCB = new CommandBuffer();
                    tempCB.DrawMesh(mQuads, Matrix4x4.identity, TileGeneratorMat, 0, textureType);
                    //tempCB.DrawMesh(mQuads, Matrix4x4.identity, TileGeneratorMat);

                    Graphics.ExecuteCommandBuffer(tempCB);
                }
            }



            OnTileGenerationComplete?.Invoke(TilesForMesh);
        }
        /// <summary>
        /// Sets a multiple RenderTarget <paramref name="setup"/>, and
        /// performs a full-screen pass indicated by the <paramref name="material"/> and <paramref name="pass"/>.
        /// </summary>
        /// <param name="setup">The <see cref="RenderBuffer"/> setup.</param>
        /// <param name="clearDepth">If depth buffer should be cleared.</param>
        /// <param name="material">The material to be used for drawing.</param>
        /// <param name="pass">The material's pass to be used for drawing.</param>
        protected static void BlitMRT(RenderTargetSetup setup, bool clearDepth, Material material, int pass)
        {
            Graphics.SetRenderTarget(setup);

            GL.Clear(clearDepth, true, Color.clear);

            GL.PushMatrix();
            GL.LoadOrtho();

            material.SetPass(pass);

            //Render the full screen quad manually.
            GL.Begin(GL.QUADS);
            GL.TexCoord2(0.0f, 0.0f); GL.Vertex3(0.0f, 0.0f, 0.1f);
            GL.TexCoord2(1.0f, 0.0f); GL.Vertex3(1.0f, 0.0f, 0.1f);
            GL.TexCoord2(1.0f, 1.0f); GL.Vertex3(1.0f, 1.0f, 0.1f);
            GL.TexCoord2(0.0f, 1.0f); GL.Vertex3(0.0f, 1.0f, 0.1f);
            GL.End();

            GL.PopMatrix();
        }
Esempio n. 6
0
        void GetReflectionProbePositions(out Vector3[] positions, out Vector3[] normals, out Vector4[] tangents, Shader ExtractShader, Matrix4x4 localToWorld, Mesh mesh, int sliceCount)
        {
            // setup extraction shader
            Material extractMaterial = new Material(ExtractShader);
            extractMaterial.SetPass(0);

            // create and setup renderbuffers
            RenderTexture worldPositionBuffer = new RenderTexture(slicesPerAxis, slicesPerAxis, 0, RenderTextureFormat.ARGBFloat);
            worldPositionBuffer.Create();
            RenderTexture worldNormalBuffer = new RenderTexture(slicesPerAxis, slicesPerAxis, 0, RenderTextureFormat.ARGBFloat);
            worldNormalBuffer.Create();
            RenderTexture worldTangentBuffer = new RenderTexture(slicesPerAxis, slicesPerAxis, 0, RenderTextureFormat.ARGBFloat);
            worldTangentBuffer.Create();

            RenderBuffer[] renderBuffers = new RenderBuffer[3];
            renderBuffers[0] = worldPositionBuffer.colorBuffer;
            renderBuffers[1] = worldNormalBuffer.colorBuffer;
            renderBuffers[2] = worldTangentBuffer.colorBuffer;

            RenderTexture depth = new RenderTexture(slicesPerAxis, slicesPerAxis, 32, RenderTextureFormat.Depth);
            RenderTargetSetup renderTargetSetup = new RenderTargetSetup(renderBuffers, depth.depthBuffer);
            RenderTexture prevRenderTarget = RenderTexture.active;
            Graphics.SetRenderTarget(renderTargetSetup);
    
            GL.Clear(true, true, Color.clear);

            // render the mesh
            Graphics.DrawMeshNow(mesh, localToWorld);

            // copy the result back from the GPU
            Texture2D samplePositionsCPU = new Texture2D(slicesPerAxis, slicesPerAxis, TextureFormat.RGBAFloat, false, true);
            RenderTexture.active = worldPositionBuffer;
            samplePositionsCPU.ReadPixels(new Rect(0, 0, samplePositionsCPU.width, samplePositionsCPU.height), 0, 0);
            samplePositionsCPU.Apply();

            Texture2D sampleNormalsCPU = new Texture2D(slicesPerAxis, slicesPerAxis, TextureFormat.RGBAFloat, false, true);
            RenderTexture.active = worldNormalBuffer;
            sampleNormalsCPU.ReadPixels(new Rect(0, 0, sampleNormalsCPU.width, sampleNormalsCPU.height), 0, 0);
            sampleNormalsCPU.Apply();

            Texture2D sampleTangentsCPU = new Texture2D(slicesPerAxis, slicesPerAxis, TextureFormat.RGBAFloat, false, true);
            RenderTexture.active = worldTangentBuffer;
            sampleTangentsCPU.ReadPixels(new Rect(0, 0, sampleTangentsCPU.width, sampleTangentsCPU.height), 0, 0);
            sampleTangentsCPU.Apply();

            RenderTexture.active = prevRenderTarget;
            positions = new Vector3[sliceCount];
            Color[] samplesPositionsColors = samplePositionsCPU.GetPixels();

            normals = new Vector3[sliceCount];
            Color[] samplesNormalColors = sampleNormalsCPU.GetPixels();

            tangents = new Vector4[sliceCount];
            Color[] samplesTangentColors = sampleTangentsCPU.GetPixels();

            // copy the results into usable arrays
            for (int i = 0; i < sliceCount; i++)
            {
                Color c = samplesPositionsColors[i];
                positions[i] = new Vector3(c.r, c.g, c.b);

                Color n = samplesNormalColors[i];
                normals[i] = new Vector3(n.r, n.g, n.b);

                Color t = samplesTangentColors[i];
                tangents[i] = new Vector4(t.r, t.g, t.b, t.a);
            }

            // cleanup
            worldPositionBuffer.Release();
            worldNormalBuffer.Release();
            worldTangentBuffer.Release();

            depth.Release();
            DestroyImmediate(samplePositionsCPU);
            DestroyImmediate(sampleNormalsCPU);
            DestroyImmediate(sampleTangentsCPU);
        }
Esempio n. 7
0
    public static int constructor(IntPtr l)
    {
        int result;

        try
        {
            int num = LuaDLL.lua_gettop(l);
            if (num == 9)
            {
                RenderBuffer[] color;
                LuaObject.checkArray <RenderBuffer>(l, 2, out color);
                RenderBuffer depth;
                LuaObject.checkValueType <RenderBuffer>(l, 3, out depth);
                int mip;
                LuaObject.checkType(l, 4, out mip);
                CubemapFace face;
                LuaObject.checkEnum <CubemapFace>(l, 5, out face);
                RenderBufferLoadAction[] colorLoad;
                LuaObject.checkArray <RenderBufferLoadAction>(l, 6, out colorLoad);
                RenderBufferStoreAction[] colorStore;
                LuaObject.checkArray <RenderBufferStoreAction>(l, 7, out colorStore);
                RenderBufferLoadAction depthLoad;
                LuaObject.checkEnum <RenderBufferLoadAction>(l, 8, out depthLoad);
                RenderBufferStoreAction depthStore;
                LuaObject.checkEnum <RenderBufferStoreAction>(l, 9, out depthStore);
                RenderTargetSetup renderTargetSetup = new RenderTargetSetup(color, depth, mip, face, colorLoad, colorStore, depthLoad, depthStore);
                LuaObject.pushValue(l, true);
                LuaObject.pushValue(l, renderTargetSetup);
                result = 2;
            }
            else if (LuaObject.matchType(l, num, 2, typeof(RenderBuffer), typeof(RenderBuffer)))
            {
                RenderBuffer color2;
                LuaObject.checkValueType <RenderBuffer>(l, 2, out color2);
                RenderBuffer depth2;
                LuaObject.checkValueType <RenderBuffer>(l, 3, out depth2);
                RenderTargetSetup renderTargetSetup = new RenderTargetSetup(color2, depth2);
                LuaObject.pushValue(l, true);
                LuaObject.pushValue(l, renderTargetSetup);
                result = 2;
            }
            else if (LuaObject.matchType(l, num, 2, typeof(RenderBuffer), typeof(RenderBuffer), typeof(int)))
            {
                RenderBuffer color3;
                LuaObject.checkValueType <RenderBuffer>(l, 2, out color3);
                RenderBuffer depth3;
                LuaObject.checkValueType <RenderBuffer>(l, 3, out depth3);
                int mipLevel;
                LuaObject.checkType(l, 4, out mipLevel);
                RenderTargetSetup renderTargetSetup = new RenderTargetSetup(color3, depth3, mipLevel);
                LuaObject.pushValue(l, true);
                LuaObject.pushValue(l, renderTargetSetup);
                result = 2;
            }
            else if (LuaObject.matchType(l, num, 2, typeof(RenderBuffer), typeof(RenderBuffer), typeof(int), typeof(CubemapFace)))
            {
                RenderBuffer color4;
                LuaObject.checkValueType <RenderBuffer>(l, 2, out color4);
                RenderBuffer depth4;
                LuaObject.checkValueType <RenderBuffer>(l, 3, out depth4);
                int mipLevel2;
                LuaObject.checkType(l, 4, out mipLevel2);
                CubemapFace face2;
                LuaObject.checkEnum <CubemapFace>(l, 5, out face2);
                RenderTargetSetup renderTargetSetup = new RenderTargetSetup(color4, depth4, mipLevel2, face2);
                LuaObject.pushValue(l, true);
                LuaObject.pushValue(l, renderTargetSetup);
                result = 2;
            }
            else if (num == 6)
            {
                RenderBuffer color5;
                LuaObject.checkValueType <RenderBuffer>(l, 2, out color5);
                RenderBuffer depth5;
                LuaObject.checkValueType <RenderBuffer>(l, 3, out depth5);
                int mipLevel3;
                LuaObject.checkType(l, 4, out mipLevel3);
                CubemapFace face3;
                LuaObject.checkEnum <CubemapFace>(l, 5, out face3);
                int depthSlice;
                LuaObject.checkType(l, 6, out depthSlice);
                RenderTargetSetup renderTargetSetup = new RenderTargetSetup(color5, depth5, mipLevel3, face3, depthSlice);
                LuaObject.pushValue(l, true);
                LuaObject.pushValue(l, renderTargetSetup);
                result = 2;
            }
            else if (LuaObject.matchType(l, num, 2, typeof(RenderBuffer[]), typeof(RenderBuffer)))
            {
                RenderBuffer[] color6;
                LuaObject.checkArray <RenderBuffer>(l, 2, out color6);
                RenderBuffer depth6;
                LuaObject.checkValueType <RenderBuffer>(l, 3, out depth6);
                RenderTargetSetup renderTargetSetup = new RenderTargetSetup(color6, depth6);
                LuaObject.pushValue(l, true);
                LuaObject.pushValue(l, renderTargetSetup);
                result = 2;
            }
            else if (LuaObject.matchType(l, num, 2, typeof(RenderBuffer[]), typeof(RenderBuffer), typeof(int)))
            {
                RenderBuffer[] color7;
                LuaObject.checkArray <RenderBuffer>(l, 2, out color7);
                RenderBuffer depth7;
                LuaObject.checkValueType <RenderBuffer>(l, 3, out depth7);
                int mipLevel4;
                LuaObject.checkType(l, 4, out mipLevel4);
                RenderTargetSetup renderTargetSetup = new RenderTargetSetup(color7, depth7, mipLevel4);
                LuaObject.pushValue(l, true);
                LuaObject.pushValue(l, renderTargetSetup);
                result = 2;
            }
            else if (LuaObject.matchType(l, num, 2, typeof(RenderBuffer[]), typeof(RenderBuffer), typeof(int), typeof(CubemapFace)))
            {
                RenderBuffer[] color8;
                LuaObject.checkArray <RenderBuffer>(l, 2, out color8);
                RenderBuffer depth8;
                LuaObject.checkValueType <RenderBuffer>(l, 3, out depth8);
                int mip2;
                LuaObject.checkType(l, 4, out mip2);
                CubemapFace face4;
                LuaObject.checkEnum <CubemapFace>(l, 5, out face4);
                RenderTargetSetup renderTargetSetup = new RenderTargetSetup(color8, depth8, mip2, face4);
                LuaObject.pushValue(l, true);
                LuaObject.pushValue(l, renderTargetSetup);
                result = 2;
            }
            else if (num == 0)
            {
                RenderTargetSetup renderTargetSetup = default(RenderTargetSetup);
                LuaObject.pushValue(l, true);
                LuaObject.pushObject(l, renderTargetSetup);
                result = 2;
            }
            else
            {
                result = LuaObject.error(l, "New object failed.");
            }
        }
        catch (Exception e)
        {
            result = LuaObject.error(l, e);
        }
        return(result);
    }
Esempio n. 8
0
 protected void Blit(RenderTargetSetup renderTargetSetup, Material material, int pass = -1)
 {
     Blit(null, renderTargetSetup, material, pass);
 }
Esempio n. 9
0
 protected void Blit(Texture mainTex, RenderTargetSetup renderTargetSetup, Material material, int pass = -1)
 {
     Graphics.SetRenderTarget(renderTargetSetup);
     Blit(mainTex, material, pass);
 }