Example #1
0
 private void OnBecameInvisible()
 {
     IsVisible = false;
     if (!Application.isPlaying)
     {
         return;
     }
     if (OnBecameInVisibleAction != null)
     {
         OnBecameInVisibleAction(getHash, lastPlanarReflectionSize, this);
     }
     FastWaterModel20FrameBuffer.DeRequestorRuntime(this);
 }
Example #2
0
        private void OnPreRenderMy(Camera camera)

        /* { }
         * void OnWillRenderObject()*/
        //private void OnPreRender()
        { //m_renderCamera = Camera.current;
          //if (!MainRender(camera)) return;
          // if (Application.isPlaying && !visible) return;
            if (!camera || camera.gameObject.layer == 7)
            {
                return;
            }
            if (!__this || !enabled)
            {
                Camera.onPreRender -= OnPreRenderMy;
                return;
            }
            if (!IsVisible)
            {
                return;
            }



            m_renderCamera = camera;
            if (cameraCounterPreRender)
            {
                cameraCounterPreRender = false;
                ++CameraIndexPreRender;
            }

            /*int count = 0;
             * foreach (var item in cache_GET_RENDER_TEXTURE) {
             * foreach (var v2 in item.Value) {
             *  count += v2.Valu;
             * }
             * }
             * Debug.Log( count );*/



        #if UNITY_EDITOR
            if (!compiler)
            {
                return;
            }
            if (!compiler.material)
            {
                return;
            }
            if (!RENDERER)
            {
                return;
            }
            if (!RENDERER.sharedMaterial)
            {
                return;
            }

            //RestoreMat();

            TryCreateTempMaterials();


            if (CameraIndexPreRender == -1)
            {
                return;
            }
            //CopyMat( material, editor_cache_materials[this] );
            //EditorJsonUtility.FromJsonOverwrite( EditorJsonUtility.ToJson( material ), editor_cache_materials[this] );

            /* if (restoreMat != RE_NDERER.sharedMaterial && !string.IsNullOrEmpty(AssetDatabase.GetAssetPath(RE_NDERER.sharedMaterial)))
             * {
             *   restoreMat = RE_NDERER.sharedMaterial;
             *   SetDirty(this);
             * }*/
            //__compiler.OverrideMaterial = RE_NDERER.sharedMaterial = editor_cache_materials[gameObject];
        #endif


            // var M = RENDERER.sharedMaterial;

            if (!setter_cache.ContainsKey(RENDERER.sharedMaterial))
            {
                setter_cache.Add(RENDERER.sharedMaterial, new MaterialSetter()
                {
                    RENDERER = RENDERER, material = RENDERER.sharedMaterial, compiler = compiler
                });
            }
            var M = setter_cache[RENDERER.sharedMaterial];

            do
            {
                if (!USE_FAKE_LIGHTING)
                {
                    if (!DirectionLight)
                    {
                        break;
                    }
                    if (Application.isPlaying && m_OldDirection == DirectionLight.forward)
                    {
                        break;
                    }

                    // if (!compiler.HasProperty(_LightDir)) break;

                    m_OldDirection = DirectionLight.forward;
                    // m_v3 = -DirectionLight.TransformDirection( 0, 0, 1 ).normalized;
                    m_v3 = DirectionLight.forward;
                    m_v4.Set(m_v3.x, m_v3.y, m_v3.z, 0);
                    M.SetVector(_LightDir, m_v4);
                }
                else
                {
                    if (!FakeLight)
                    {
                        break;
                    }
                    if (!__FakeLightComponent)
                    {
                        __FakeLightComponent = FakeLight.GetComponent <FastWaterModel20FakeLight>();
                    }
                    if (!__FakeLightComponent)
                    {
                        break;
                    }
                    if (Application.isPlaying && m_OldDirection == FakeLight.forward && m_OldColor == __FakeLightComponent.color)
                    {
                        break;
                    }


                    // if (!compiler.HasProperty(_LightDir)) break;
                    // if (!compiler.HasProperty(_LightColor0Fake)) break;

                    m_OldDirection = FakeLight.forward;
                    m_v3           = FakeLight.TransformDirection(0, 0, 1).normalized;
                    m_v4.Set(m_v3.x, m_v3.y, m_v3.z, 0);
                    M.SetVector(_LightDir, m_v4);

                    m_OldColor = __FakeLightComponent.color;
                    M.SetColor(_LightColor0Fake, __FakeLightComponent.color);
                }
            } while (false);



            // if (!lastOrto.ContainsKey(M)) lastOrto.Add(M, null);
            // if (!needOrto.ContainsKey(camera)) needOrto.Add(camera, false);

            bool CHECKORTO = M.lastOrto != m_renderCamera.orthographic;
        #if UNITY_EDITOR
            if (!Application.isPlaying)
            {
                CHECKORTO = true;
            }
        #endif


            if (CHECKORTO)
            {
                M.lastOrto = m_renderCamera.orthographic;
            }


            //


            /*   if (IsKeywordEnabled(ULTRA_FAST_MODE))
             *   {
             *       var res = bakedRotation ?? (bakedRotation = transform.rotation.eulerAngles.y * Mathf.Deg2Rad).Value;
             *       if (!Application.isPlaying) res = transform.rotation.eulerAngles.y / 180 * Mathf.PI;
             *       M.SetFloat(_ObjecAngle, 0);
             *   }*/
            // Debug.Log((Application.isPlaying ? (Time.time / 16) : ((float)((EditorApplication.timeSinceStartup / 16) % (512 / 16)))) % (512 / 16));


            DO_FRACTIME(M);



            if (Application.isPlaying)
            {
                M.SetVector(_ObjectScale, localScale ?? (localScale = transform.localScale).Value);
            }
            else
            {
                M.SetVector(_ObjectScale, transform.localScale);
            }

            if (M.lastOrto == true)
            { //Debug.Log(camera.gameObject.layer + " " + camera);
                M.SetFloat(_MyNearClipPlane, m_renderCamera.nearClipPlane);
                M.SetFloat(_MyFarClipPlane, m_renderCamera.farClipPlane);
            }
            // return;
            if (IsKeywordEnabled(ALLOW_MANUAL_DEPTH) && (m_renderCamera.depthTextureMode & DepthTextureMode.Depth) == 0)
            { //lastDepth = camera;
                m_renderCamera.depthTextureMode |= DepthTextureMode.Depth;
                //  Debug.Log(gameObject.name);
            }

            //if (!IsKeywordEnabled(SKIP_3DVERTEX_ANIMATION))
            { /* VertexMin.Set(VertexSize.x, 0, VertexSize.y);
               * VertexMax.Set(VertexSize.z, 0, VertexSize.w);
               * VertexMin = transform.TransformPoint(VertexMin);
               * VertexMax = transform.TransformPoint(VertexMax);
               * VertexSizeResult.Set( VertexMin.x, VertexMin.z, -VertexMin.x + VertexMax.x, -VertexMin.z + VertexMax.z);*/
                M.SetVector(_VertexSize, VertexSize);
            }

            ///UpdateMaterials();
            ///UpdateMaterials();
            if (IsKeywordEnabled(REFLECTION_PLANAR))
            {
                DoPlanarReflection(Mathf.RoundToInt(compiler.GetFloat(_ReflectionTex_temp_size)));
            }

            //refraction
            if (IsKeywordEnabled(REFRACTION_BAKED_FROM_TEXTURE))
            { //external
                M.SetTexture(_RefractionTex, backed_refraction);
            }
            if (IsKeywordEnabled(REFRACTION_BAKED_VIA_SCRIPT) || IsKeywordEnabled(REFRACTION_BAKED_ONAWAKE))
            { //internal
                var t = GET_RENDER_TEXTURE(BakeOrUpdateType.Refraction) ?? emptyTexture;
                // var gett = GetComponent<FastWaterModel20ManualBaking>().t1;
                // Shader.SetGlobalTexture( _RefractionTex_temp, gett );
                // material.SetTexture( _RefractionTex_temp, gett );

                /*#if !TEST_GLES20
                 *              if (SystemInfo.graphicsDeviceType == UnityEngine.Rendering.GraphicsDeviceType.OpenGLES2 && Application.isPlaying)
                 #else
                 *              if (true)
                 #endif*/
                //#if USE_TEX2D_CONVERTER
                if (UseTextureConverter)
                {
                    M.SetTexture(_RefractionTex_temp, t.texture_gles20);
                }
                //#else
                else
                {
                    M.SetTexture(_RefractionTex_temp, t.texture);
                }
                //#endif
            }
            //zdepth
            if (IsKeywordEnabled(BAKED_DEPTH_EXTERNAL_TEXTURE))
            { //external
                M.SetTexture(_BakedData, backed_zdepth);
            }


            if (IsKeywordEnabled(BAKED_DEPTH_VIASCRIPT) || IsKeywordEnabled(BAKED_DEPTH_ONAWAKE) || SECOND_DEPTH_NEED())
            { //internalAS
                var t = GET_RENDER_TEXTURE(BakeOrUpdateType.ZDepth) ?? emptyTexture;

                /*#if !TEST_GLES20
                 *              if (SystemInfo.graphicsDeviceType == UnityEngine.Rendering.GraphicsDeviceType.OpenGLES2 && Application.isPlaying)
                 #else
                 *              if (true)
                 #endif*/
                //#if USE_TEX2D_CONVERTER
                if (UseTextureConverter)
                {
                    M.SetTexture(_BakedData_temp, t.texture_gles20);
                }
                //#else
                else
                {
                    M.SetTexture(_BakedData_temp, t.texture);
                }
                //#endif
            }

            // camera.clearFlags = CameraClearFlags.Nothing;
            // GameObject.FindObjectsOfType<Text>().First(t => t.gameObject.name == "LOG").text = M.lastDownSamling.ToString();
            // M. material.EnableKeyword("WATER_DOWNSAMPLING");
            // M.SetFloat(DOWNSAMPLING_SAMPLE_SIZE, 0.5f);

            if (M.lastDownSamling /* && CameraIndexPreRender != -1 && camera.targetTexture*/ != 0)
            {
                var buffer = FastWaterModel20FrameBuffer.AddRequestorRuntime(this, camera, IsKeywordEnabled(FORCE_OPAQUE));

                /* if (buffer.LastTexture)
                 * {   M.SetTexture(_LastFrame, buffer.LastTexture);
                 * }*/
                if (buffer.LastTexture2)
                {
                    Shader.SetGlobalTexture(_LastFrame2, buffer.LastTexture2);
                    // M.SetTexture(_LastFrame2, buffer.LastTexture2);
                }

                switch (M.lastDownSamling)
                { //case 1:  M.SetFloat(DOWNSAMPLING_SAMPLE_SIZE, 0.5f); break;
                case 1:  M.SetFloat(DOWNSAMPLING_SAMPLE_SIZE, 0.25f); break;

                case 2:  M.SetFloat(DOWNSAMPLING_SAMPLE_SIZE, 0.5f); break;

                case 3:  M.SetFloat(DOWNSAMPLING_SAMPLE_SIZE, 0.75f); break;
                }
            }

            /* var trt = GET_RENDER_TEXTURE(BakeOrUpdateType.LastFrame, camera.targetTexture.width, heightSize: camera.targetTexture.height, camera: CameraIndexPreRender).texture;
             *   M.SetTexture(_LastFrame, trt);
             *
             *   Debug.Log(camera.targetTexture);*/
            //Debug.Log(camera.targetTexture);
            //
            ///UpdateMaterials();
            ///UpdateMaterials();

            // __compiler.OverrideMaterial = null;
        }
Example #3
0
#pragma warning restore
        void OnDisable()
        {
            if (!this || !enabled)
            {
                return;
            }
            if (OnDisableAction != null)
            {
                OnDisableAction(getHash, lastPlanarReflectionSize, this);
            }
            FastWaterModel20FrameBuffer.DestroyAllScript();



        #if UNITY_EDITOR
            // if (!mainThread.Equals(System.Threading.Thread.CurrentThread)) return;

            /* if (!Applicati   on.isPlaying)
             * {   if (!initFlag)
             *   {   initFlag = true;*/
            // if (!Application.isPlaying) DestroyAllEditorDatasDeep();

            /*  }
             * }*/
        #endif
            // if (gameObject.activeSelf) return;
            _was_enables = false;

            if (__compiler)
            {
                __compiler.onShaderUpdate -= onShaderUpdate;
            }

        #if FASTWATER20_LOG
            Debug.Log("OnDisable " + gameObject.name);
        #endif
        #if UNITY_EDITOR
            OnDisable2();


            EditorApplication.update -= EditorUpdate;
            // RestoreMat();
        #endif
            Camera.onPostRender -= OnPostRenderMy;
            Camera.onPreRender  -= OnPreRenderMy;



            if (RenderDepthOrRefractionCamera)
            {
                DESTROY_CAMERA(RenderDepthOrRefractionCamera);
            }
            if (RenderReflectionCamera)
            {
                DESTROY_CAMERA(RenderReflectionCamera);
            }


            destroyHelper.Push(goID);

            if (__compiler)
            { /* if (cache_GET_RENDER_TEXTURE.ContainsKey(goID))
               * {
               *
               *   foreach (var itemList in cache_GET_RENDER_TEXTURE[goID])
               *   {   foreach (var item in itemList.Value)
               *       {   if (item == null)
               *               continue;
               *           if (item.lastCamera)
               *               item.lastCamera.targetTexture = null;
               *           if (item.texture_swap)
               *               DESTORY(item.texture_swap);
               *           if (item.texture_gles20)
               *               DESTORY(item.texture_gles20);
               *       }
               *       itemList.Value.Clear();
               *       // item.Value.lastCamera
               *   }
               * }*/
              /* #if UNITY_EDITOR
               * if (editor_cache_materials.ContainsKey(goID))
               * {   //Debug.Log("ASD");
               *   if (editor_cache_materials[goID])
               *   {   Destroy(editor_cache_materials[goID].GetTexture(_ReflectionTex_temp), 5);
               *       Destroy(editor_cache_materials[goID].GetTexture(_RefractionTex_temp), 5);
               *       Destroy(editor_cache_materials[goID].GetTexture(_BakedData_temp), 5);
               *       // DESTORY(editor_cache_materials[goID].GetTexture(_ReflectionTex_temp));
               *       // DESTORY(editor_cache_materials[goID].GetTexture(_RefractionTex_temp));
               *       // DESTORY(editor_cache_materials[goID].GetTexture(_BakedData_temp));
               *       / * var t1 = editor_cache_materials[goID].GetTexture(_ReflectionTex_temp) as RenderTexture;
               *        if (t1) t1.DiscardContents(true, true);
               *        if (t1) t1.Release();
               *        var t2 = editor_cache_materials[goID].GetTexture(_RefractionTex_temp) as RenderTexture;
               *        if (t2) t2.DiscardContents(true, true);
               *        if (t2) t1.Release();
               *        var t3 = editor_cache_materials[goID].GetTexture(_BakedData_temp) as RenderTexture;
               *        if (t3)  t3.DiscardContents(true, true);
               *        if (t3) t1.Release();* /
               *
               *       //Unity 2018.2f14 crashed when scene was changing and had paticles emmiter
               *       // if (!Application.isPlaying) DESTORY(editor_cache_materials[goID]);
               *       //Unity 2018.2f14 crashed when scene was changing and had paticles emmiter
               *   }
               *   editor_cache_materials.Remove(goID);
               * }
               #else
               * DESTORY(__compiler.material.GetTexture(_ReflectionTex_temp));
               * DESTORY(__compiler.material.GetTexture(_RefractionTex_temp));
               * DESTORY(__compiler.material.GetTexture(_BakedData_temp));
               #endif*/
            }

            //Unity 2018.2f14 crashed when scene was changing and had paticles emmiter

            /* DESTORY(DepthRenderShader_SecondPass_Matertial);
             * if (__renderer && __renderer.sharedMaterial && (__renderer.sharedMaterial.hideFlags & HideFlags.HideAndDontSave) != 0)
             *   DESTORY(__renderer.sharedMaterial);*/
            //Unity 2018.2f14 crashed when scene was changing and had paticles emmiter
        }