Ejemplo n.º 1
0
        public void OnPreRender()
        {
            if (material == null)
            {
                return;
            }
            else if (Camera.current.actualRenderingPath != RenderingPath.DeferredShading)
            {
                return;
            }

            componentCreateReflectionLookupTextures.createReflectionTextureCoordinatesAndIndexTextures();

            int downsampleAmount = 1; // 2;

            var rtW = camera_.pixelWidth / downsampleAmount;
            var rtH = camera_.pixelHeight / downsampleAmount;

            float sWidth  = camera_.pixelWidth;
            float sHeight = camera_.pixelHeight;

            Matrix4x4 P        = GetComponent <Camera>().projectionMatrix;
            Vector4   projInfo = new Vector4
                                     ((-2.0f / (sWidth * P[0])),
                                     (-2.0f / (sHeight * P[5])),
                                     ((1.0f - P[2]) / P[0]),
                                     ((1.0f + P[6]) / P[5]));

            RenderTextureFormat intermediateFormat = camera_.allowHDR ? RenderTextureFormat.ARGBHalf : RenderTextureFormat.ARGB32;

            Matrix4x4 cameraToWorldMatrix = GetComponent <Camera>().worldToCameraMatrix.inverse;

            material.SetVector("_ProjInfo", projInfo); // used for unprojection
            material.SetMatrix("_CameraToWorldMatrix", cameraToWorldMatrix);
            material.SetMatrix("_InverseViewProject", (GetComponent <Camera>().projectionMatrix *GetComponent <Camera>().worldToCameraMatrix).inverse);

            reflectionGroundTexture     = componentUpdateReflectionTextures.getGroundReflectionRenderTexture();
            reflectionLowerLevelTexture = componentUpdateReflectionTextures.getSeaReflectionRenderTexture();
            material.SetTexture("_ReflectionGroundTex", reflectionGroundTexture);
            material.SetTexture("_ReflectionLowerLevelTex", reflectionLowerLevelTexture);

            material.SetTexture("_ReflectionsTextureIndexTex", componentCreateReflectionLookupTextures.renderTextureReflectionTextureIndex);
            material.SetTexture("_ReflectionsTextureCoordinatesTex", componentCreateReflectionLookupTextures.renderTextureReflectionTextureCoordinates);

            material.SetFloat("_GroundLevelHeight", componentUpdateReflectionTextures.ReflectionPlaneGroundLevelY);
            material.SetFloat("_LowerLevelHeight", componentUpdateReflectionTextures.ReflectionPlaneLowerLevelY);

            int minDimValue     = Math.Min(componentUpdateReflectionTextures.FloorReflectionTextureWidth, componentUpdateReflectionTextures.FloorReflectionTextureHeight);
            int numMipMapLevels = (int)(Math.Log(minDimValue) / Math.Log(2));

            material.SetInt("_NumMipMapLevelsReflectionGroundTex", numMipMapLevels);
            minDimValue     = Math.Min(componentUpdateReflectionTextures.LowerLevelReflectionTextureWidth, componentUpdateReflectionTextures.LowerLevelReflectionTextureHeight);
            numMipMapLevels = (int)(Math.Log(minDimValue) / Math.Log(2));
            material.SetInt("_NumMipMapLevelsReflectionLowerLevelTex", numMipMapLevels);

            material.SetFloat("_RoughnessMultiplier", componentUpdateReflectionTextures.RoughnessMultiplier);

            if (m_CommandBuffer == null)
            {
                m_CommandBuffer      = new CommandBuffer();
                m_CommandBuffer.name = "Deferred Planar Reflections";


                m_CommandBuffer.GetTemporaryRT(kFinalReflectionTexture, rtW, rtH, 0, FilterMode.Point, intermediateFormat);

                m_CommandBuffer.GetTemporaryRT(kTempTexture, camera_.pixelWidth, camera_.pixelHeight, 0, FilterMode.Bilinear, intermediateFormat);
                m_CommandBuffer.Blit(BuiltinRenderTextureType.CameraTarget, kFinalReflectionTexture, material, (int)PassIndex.ReflectionStep);

                m_CommandBuffer.Blit(BuiltinRenderTextureType.CameraTarget, kTempTexture, material, (int)PassIndex.CompositeFinal);
                m_CommandBuffer.Blit(kTempTexture, BuiltinRenderTextureType.CameraTarget);

                m_CommandBuffer.ReleaseTemporaryRT(kTempTexture);
                camera_.AddCommandBuffer(CameraEvent.AfterFinalPass, m_CommandBuffer);
            }
        }
        //public void Update()
        //{
        //    componentCreateReflectionLookupTextures.createReflectionTextureCoordinatesAndIndexTextures();
        //}

        public void OnPreRender()
        {
            if (material == null)
            {
                return;
            }
            else if (Camera.current.actualRenderingPath != RenderingPath.DeferredShading)
            {
                return;
            }

            componentCreateReflectionLookupTextures.createReflectionTextureCoordinatesAndIndexTextures();

            int downsampleAmount = 1; // 2;

            var rtW = camera_.pixelWidth / downsampleAmount;
            var rtH = camera_.pixelHeight / downsampleAmount;

            float sWidth  = camera_.pixelWidth;
            float sHeight = camera_.pixelHeight;

            Matrix4x4 P        = GetComponent <Camera>().projectionMatrix;
            Vector4   projInfo = new Vector4
                                     ((-2.0f / (sWidth * P[0])),
                                     (-2.0f / (sHeight * P[5])),
                                     ((1.0f - P[2]) / P[0]),
                                     ((1.0f + P[6]) / P[5]));

            RenderTextureFormat intermediateFormat = camera_.allowHDR ? RenderTextureFormat.ARGBHalf : RenderTextureFormat.ARGB32;

            Matrix4x4 cameraToWorldMatrix = GetComponent <Camera>().worldToCameraMatrix.inverse;

            material.SetVector("_ProjInfo", projInfo); // used for unprojection
            material.SetMatrix("_CameraToWorldMatrix", cameraToWorldMatrix);
            material.SetMatrix("_InverseViewProject", (GetComponent <Camera>().projectionMatrix *GetComponent <Camera>().worldToCameraMatrix).inverse);

            reflectionGroundTexture     = componentUpdateReflectionTextures.getGroundReflectionRenderTexture();
            reflectionLowerLevelTexture = componentUpdateReflectionTextures.getSeaReflectionRenderTexture();
            material.SetTexture("_ReflectionGroundTex", reflectionGroundTexture);
            material.SetTexture("_ReflectionLowerLevelTex", reflectionLowerLevelTexture);

            material.SetTexture("_ReflectionsTextureIndexTex", componentCreateReflectionLookupTextures.renderTextureReflectionTextureIndex);
            material.SetTexture("_ReflectionsTextureCoordinatesTex", componentCreateReflectionLookupTextures.renderTextureReflectionTextureCoordinates);

            material.SetFloat("_GroundLevelHeight", componentUpdateReflectionTextures.ReflectionPlaneGroundLevelY); // + GameManager.Instance.MainCamera.transform.localPosition.y);
            material.SetFloat("_LowerLevelHeight", componentUpdateReflectionTextures.ReflectionPlaneLowerLevelY);   // + GameManager.Instance.MainCamera.transform.localPosition.y);
            //material.SetFloat("_CameraHeightFromGround", GameManager.Instance.MainCamera.transform.localPosition.y);

            if ((componentUpdateReflectionTextures.IsEnabledOutdoorGroundReflections && componentUpdateReflectionTextures.CurrentPlayerEnvironment == UpdateReflectionTextures.PlayerEnvironment.Outdoors) ||
                (componentUpdateReflectionTextures.IsEnabledIndoorBuildingFloorReflections && componentUpdateReflectionTextures.CurrentPlayerEnvironment == UpdateReflectionTextures.PlayerEnvironment.Building) ||
                (componentUpdateReflectionTextures.IsEnabledDungeonGroundReflections && componentUpdateReflectionTextures.CurrentPlayerEnvironment == UpdateReflectionTextures.PlayerEnvironment.DungeonOrCastle))
            {
                material.EnableKeyword("_GROUND_LEVEL_REFLECTIONS");
            }
            else
            {
                material.DisableKeyword("_GROUND_LEVEL_REFLECTIONS");
            }

            if ((componentUpdateReflectionTextures.IsEnabledOutdoorSeaReflections && componentUpdateReflectionTextures.CurrentPlayerEnvironment == UpdateReflectionTextures.PlayerEnvironment.Outdoors) ||
                (componentUpdateReflectionTextures.IsEnabledIndoorBuildingLowerLevelReflection && componentUpdateReflectionTextures.CurrentPlayerEnvironment == UpdateReflectionTextures.PlayerEnvironment.Building) ||
                (componentUpdateReflectionTextures.IsEnabledDungeonWaterReflections && componentUpdateReflectionTextures.CurrentPlayerEnvironment == UpdateReflectionTextures.PlayerEnvironment.DungeonOrCastle))
            {
                material.EnableKeyword("_LOWER_LEVEL_REFLECTIONS");
            }
            else
            {
                material.DisableKeyword("_LOWER_LEVEL_REFLECTIONS");
            }

            if (componentUpdateReflectionTextures.IsFeatureEnabledFakeParallaxReflections && componentUpdateReflectionTextures.CurrentPlayerEnvironment == UpdateReflectionTextures.PlayerEnvironment.Outdoors)
            {
                material.EnableKeyword("_PARALLAX_REFLECTIONS");
            }
            else
            {
                material.DisableKeyword("_PARALLAX_REFLECTIONS");
            }


            int minDimValue     = Math.Min(componentUpdateReflectionTextures.FloorReflectionTextureWidth, componentUpdateReflectionTextures.FloorReflectionTextureHeight);
            int numMipMapLevels = (int)(Math.Log(minDimValue) / Math.Log(2));

            material.SetInt("_NumMipMapLevelsReflectionGroundTex", numMipMapLevels);
            minDimValue     = Math.Min(componentUpdateReflectionTextures.LowerLevelReflectionTextureWidth, componentUpdateReflectionTextures.LowerLevelReflectionTextureHeight);
            numMipMapLevels = (int)(Math.Log(minDimValue) / Math.Log(2));
            material.SetInt("_NumMipMapLevelsReflectionLowerLevelTex", numMipMapLevels);

            material.SetFloat("_RoughnessMultiplier", componentUpdateReflectionTextures.RoughnessMultiplier);

            if (m_CommandBuffer == null)
            {
                m_CommandBuffer      = new CommandBuffer();
                m_CommandBuffer.name = "Deferred Planar Reflections";


                m_CommandBuffer.GetTemporaryRT(kFinalReflectionTexture, rtW, rtH, 0, FilterMode.Point, intermediateFormat);

                m_CommandBuffer.GetTemporaryRT(kTempTexture, camera_.pixelWidth, camera_.pixelHeight, 0, FilterMode.Bilinear, intermediateFormat);
                m_CommandBuffer.Blit(BuiltinRenderTextureType.CameraTarget, kFinalReflectionTexture, material, (int)PassIndex.ReflectionStep);

                m_CommandBuffer.Blit(BuiltinRenderTextureType.CameraTarget, kTempTexture, material, (int)PassIndex.CompositeFinal);
                m_CommandBuffer.Blit(kTempTexture, BuiltinRenderTextureType.CameraTarget);

                m_CommandBuffer.ReleaseTemporaryRT(kTempTexture);
                camera_.AddCommandBuffer(CameraEvent.AfterFinalPass, m_CommandBuffer);
                AddedCommandBuffer = true;
            }
        }