Exemple #1
0
        void Start()
        {
            InjectReflectiveMaterialProperty scriptInjectReflectiveMaterialProperty = this.gameObject.AddComponent <InjectReflectiveMaterialProperty>();

            reflectionPlaneGround       = new GameObject("ReflectionPlaneGroundLevel");
            reflectionPlaneGround.layer = LayerMask.NameToLayer("Water");
            MeshFilter meshFilter = (MeshFilter)reflectionPlaneGround.AddComponent(typeof(MeshFilter));

            meshFilter.mesh = CreateMesh(100000.0f, 100000.0f); // create quad with normal facing into negative y-direction (so it is not visible but it will trigger OnWillRenderObject() in MirrorReflection.cs) - should be big enough to be "visible" even when looking parallel to the x/z-plane
            MeshRenderer renderer = reflectionPlaneGround.AddComponent(typeof(MeshRenderer)) as MeshRenderer;

            renderer.material = new Material(ShaderInvisible);
            // switch to this material for reflection plane height debugging
            //renderer.material.shader = Shader.Find("Standard");
            //Texture2D tex = new Texture2D(1, 1);
            //tex.SetPixel(0, 0, Color.green);
            //tex.Apply();
            //renderer.material.mainTexture = tex;
            //renderer.material.color = Color.green;
            renderer.shadowCastingMode = ShadowCastingMode.Off;
            renderer.enabled           = true; // if this is set to false OnWillRenderObject() in MirrorReflection.cs will not work (workaround would be to change OnWillRenderObject() to Update()

            mirrorRefl = reflectionPlaneGround.AddComponent <MirrorReflection>();
            mirrorRefl.m_TextureWidth  = floorReflectionTextureWidth;
            mirrorRefl.m_TextureHeight = floorReflectionTextureHeight;

            reflectionPlaneGround.transform.SetParent(this.transform);

            reflectionPlaneLowerLevel       = new GameObject("ReflectionPlaneLowerLevel");
            reflectionPlaneLowerLevel.layer = LayerMask.NameToLayer("Water");
            MeshFilter meshFilterSeaLevel = (MeshFilter)reflectionPlaneLowerLevel.AddComponent(typeof(MeshFilter));

            meshFilterSeaLevel.mesh = CreateMesh(1000000.0f, 1000000.0f); // create quad facing into negative y-direction (so it is not visible but it will trigger OnWillRenderObject() in MirrorReflection.cs) - should be big enough to be "visible" even when looking parallel to the x/z-plane
            MeshRenderer rendererSeaLevel = reflectionPlaneLowerLevel.AddComponent(typeof(MeshRenderer)) as MeshRenderer;

            rendererSeaLevel.material = new Material(ShaderInvisible);
            // switch to this material for reflection plane height debugging
            //rendererSeaLevel.material.shader = Shader.Find("Standard");
            //Texture2D texSeaLevel = new Texture2D(1, 1);
            //texSeaLevel.SetPixel(0, 0, Color.green);
            //texSeaLevel.Apply();
            //rendererSeaLevel.material.mainTexture = texSeaLevel;
            //rendererSeaLevel.material.color = Color.green;
            rendererSeaLevel.shadowCastingMode = ShadowCastingMode.Off;
            rendererSeaLevel.enabled           = true; // if this is set to false OnWillRenderObject() in MirrorReflection.cs will not work (workaround would be to change OnWillRenderObject() to Update()

            mirrorReflSeaLevel = reflectionPlaneLowerLevel.AddComponent <MirrorReflection>();
            mirrorReflSeaLevel.m_TextureWidth  = lowerLevelReflectionTextureWidth;
            mirrorReflSeaLevel.m_TextureHeight = lowerLevelReflectionTextureHeight;

            reflectionPlaneLowerLevel.transform.SetParent(this.transform);

            LayerMask layerIndexWorldTerrain = LayerMask.NameToLayer("WorldTerrain");

            if (layerIndexWorldTerrain != -1)
            {
                mirrorRefl.m_ReflectLayers.value   = (1 << LayerMask.NameToLayer("Default")) + (1 << LayerMask.NameToLayer("WorldTerrain"));
                mirrorReflSeaLevel.m_ReflectLayers = (1 << LayerMask.NameToLayer("Default")) + (1 << LayerMask.NameToLayer("WorldTerrain"));

                //mirrorRefl.layerCullDistances[LayerMask.NameToLayer("Default")] = 15000;
                //mirrorRefl.layerCullDistances[LayerMask.NameToLayer("WorldTerrain")] = 200000;

                //mirrorReflSeaLevel.layerCullDistances[LayerMask.NameToLayer("Default")] = 0;
                //mirrorReflSeaLevel.layerCullDistances[LayerMask.NameToLayer("WorldTerrain")] = 200000;
            }
            else
            {
                mirrorRefl.m_ReflectLayers.value   = 1 << LayerMask.NameToLayer("Default");
                mirrorReflSeaLevel.m_ReflectLayers = 1 << LayerMask.NameToLayer("Default");

                //mirrorRefl.layerCullDistances[LayerMask.NameToLayer("Default")] = 15000;
                //mirrorReflSeaLevel.layerCullDistances[LayerMask.NameToLayer("Default")] = 0;
            }

            useDeferredReflections = (GameManager.Instance.MainCamera.renderingPath == RenderingPath.DeferredShading);

            if (useDeferredReflections)
            {
                componentDeferredPlanarReflections = GameManager.Instance.MainCameraObject.AddComponent <RealtimeReflections.DeferredPlanarReflections>();
            }

            if (!GameManager.Instance.IsPlayerInside)
            {
                playerEnvironment = PlayerEnvironment.Outdoors;
                UpdateBackgroundSettingsOutdoor();
            }
            else if (GameManager.Instance.IsPlayerInsideBuilding)
            {
                playerEnvironment = PlayerEnvironment.Building;
                UpdateBackgroundSettingsIndoor();
            }
            else
            {
                playerEnvironment = PlayerEnvironment.DungeonOrCastle;
                UpdateBackgroundSettingsIndoor();
            }
        }
        void Awake()
        {
            reflectionPlaneBottom       = new GameObject("ReflectionPlaneBottom");
            reflectionPlaneBottom.layer = LayerMask.NameToLayer("Water");
            MeshFilter meshFilter = (MeshFilter)reflectionPlaneBottom.AddComponent(typeof(MeshFilter));

            meshFilter.mesh = CreateMesh(100000.0f, 100000.0f); // create quad with normal facing into negative y-direction (so it is not visible but it will trigger OnWillRenderObject() in MirrorReflection.cs) - should be big enough to be "visible" even when looking parallel to the x/z-plane
            MeshRenderer renderer = reflectionPlaneBottom.AddComponent(typeof(MeshRenderer)) as MeshRenderer;

            renderer.material.shader = Shader.Find("Standard");
            Texture2D tex = new Texture2D(1, 1);

            tex.SetPixel(0, 0, Color.green);
            tex.Apply();
            renderer.material.mainTexture = tex;
            renderer.material.color       = Color.green;
            renderer.shadowCastingMode    = ShadowCastingMode.Off;
            renderer.enabled = true; // if this is set to false OnWillRenderObject() in MirrorReflection.cs will not work (workaround would be to change OnWillRenderObject() to Update()

            mirrorRefl = reflectionPlaneBottom.AddComponent <MirrorReflection>();
            mirrorRefl.m_TextureWidth  = floorReflectionTextureWidth;
            mirrorRefl.m_TextureHeight = floorReflectionTextureHeight;

            reflectionPlaneBottom.transform.SetParent(this.transform);

            InjectReflectiveMaterialProperty scriptInjectReflectiveMaterialProperty = reflectionPlaneBottom.AddComponent <InjectReflectiveMaterialProperty>(); // the inject script is parented to this plane so that the OnWillRenderObject() method of the inject script will work - this is important since update() function resulted in slightly delayed update which could be seen when ground level height changed

            reflectionPlaneSeaLevel       = new GameObject("ReflectionPlaneSeaLevel");
            reflectionPlaneSeaLevel.layer = LayerMask.NameToLayer("Water");
            MeshFilter meshFilterSeaLevel = (MeshFilter)reflectionPlaneSeaLevel.AddComponent(typeof(MeshFilter));

            meshFilterSeaLevel.mesh = CreateMesh(1000000.0f, 1000000.0f); // create quad facing into negative y-direction (so it is not visible but it will trigger OnWillRenderObject() in MirrorReflection.cs) - should be big enough to be "visible" even when looking parallel to the x/z-plane
            MeshRenderer rendererSeaLevel = reflectionPlaneSeaLevel.AddComponent(typeof(MeshRenderer)) as MeshRenderer;


            rendererSeaLevel.material.shader = Shader.Find("Standard");
            Texture2D texSeaLevel = new Texture2D(1, 1);

            texSeaLevel.SetPixel(0, 0, Color.green);
            texSeaLevel.Apply();
            rendererSeaLevel.material.mainTexture = texSeaLevel;
            rendererSeaLevel.material.color       = Color.green;
            rendererSeaLevel.shadowCastingMode    = ShadowCastingMode.Off;
            rendererSeaLevel.enabled = true; // if this is set to false OnWillRenderObject() in MirrorReflection.cs will not work (workaround would be to change OnWillRenderObject() to Update()

            mirrorReflSeaLevel = reflectionPlaneSeaLevel.AddComponent <MirrorReflection>();
            mirrorReflSeaLevel.m_TextureWidth  = lowerLevelReflectionTextureWidth;
            mirrorReflSeaLevel.m_TextureHeight = lowerLevelReflectionTextureHeight;

            reflectionPlaneSeaLevel.transform.SetParent(this.transform);

            LayerMask layerIndexWorldTerrain = LayerMask.NameToLayer("WorldTerrain");

            if (layerIndexWorldTerrain != -1)
            {
                mirrorRefl.m_ReflectLayers.value   = (1 << LayerMask.NameToLayer("Default")) + (1 << LayerMask.NameToLayer("WorldTerrain"));
                mirrorReflSeaLevel.m_ReflectLayers = (1 << LayerMask.NameToLayer("Default")) + (1 << LayerMask.NameToLayer("WorldTerrain"));

                //mirrorRefl.layerCullDistances[LayerMask.NameToLayer("Default")] = 15000;
                //mirrorRefl.layerCullDistances[LayerMask.NameToLayer("WorldTerrain")] = 200000;

                //mirrorReflSeaLevel.layerCullDistances[LayerMask.NameToLayer("Default")] = 0;
                //mirrorReflSeaLevel.layerCullDistances[LayerMask.NameToLayer("WorldTerrain")] = 200000;
            }
            else
            {
                mirrorRefl.m_ReflectLayers.value   = 1 << LayerMask.NameToLayer("Default");
                mirrorReflSeaLevel.m_ReflectLayers = 1 << LayerMask.NameToLayer("Default");

                //mirrorRefl.layerCullDistances[LayerMask.NameToLayer("Default")] = 15000;
                //mirrorReflSeaLevel.layerCullDistances[LayerMask.NameToLayer("Default")] = 0;
            }

            useDeferredReflections = (GameManager.Instance.MainCamera.renderingPath == RenderingPath.DeferredShading);

            if (useDeferredReflections)
            {
                componentDeferredPlanarReflections = GameManager.Instance.MainCameraObject.AddComponent <RealtimeReflections.DeferredPlanarReflections>();
            }

            playerInside = GameManager.Instance.IsPlayerInside;

            if (playerInside == true)
            {
                updateBackgroundSettingsIndoor();
            }
            else
            {
                updateBackgroundSettingsOutdoor();
            }

            PlayerEnterExit.OnTransitionInterior        += OnTransitionToInterior;
            PlayerEnterExit.OnTransitionExterior        += OnTransitionToExterior;
            PlayerEnterExit.OnTransitionDungeonInterior += OnTransitionToInterior;
            PlayerEnterExit.OnTransitionDungeonExterior += OnTransitionToExterior;
        }