public void SetValue(Vector4 value, Rect rect)
 {
     Graphics.Blit(this.READ, this.WRITE);
     setFloatValueMat.SetVector("_Value", value);
     RTUtility.Blit(this.READ, this.WRITE, setFloatValueMat, rect, 0, false);
     this.Swap();
 }
 public void ChangeValue(Vector4 value, Rect rect)
 {
     Graphics.Blit(this.READ, this.WRITE); // don't know why but need it
     changeValueMat.SetVector("_Value", value);
     RTUtility.Blit(this.READ, this.WRITE, changeValueMat, rect, 0, false);
     this.Swap();
 }
Exemple #3
0
        private void ApplySlippage()
        {
            for (int i = 0; i < TERRAIN_LAYERS; i++)
            {
                if (m_talusAngle[i] < 90.0f)
                {
                    float talusAngle   = (Mathf.PI * m_talusAngle[i]) / 180.0f;
                    float maxHeightDif = Mathf.Tan(talusAngle) * CELL_LENGTH;

                    m_slippageHeightMat.SetFloat("_TexSize", (float)TEX_SIZE);
                    m_slippageHeightMat.SetFloat("_Layers", (float)(i + 1));
                    m_slippageHeightMat.SetFloat("_MaxHeightDif", maxHeightDif);
                    m_slippageHeightMat.SetTexture("_TerrainField", m_terrainField[READ]);

                    Graphics.Blit(null, m_slippageHeight, m_slippageHeightMat);

                    m_slippageOutflowMat.SetFloat("_TexSize", (float)TEX_SIZE);
                    m_slippageOutflowMat.SetFloat("_Layers", (float)(i + 1));
                    m_slippageOutflowMat.SetFloat("T", TIME_STEP);
                    m_slippageOutflowMat.SetTexture("_MaxSlippageHeights", m_slippageHeight);
                    m_slippageOutflowMat.SetTexture("_TerrainField", m_terrainField[READ]);

                    Graphics.Blit(null, m_slippageOutflow, m_slippageOutflowMat);

                    m_slippageUpdateMat.SetFloat("T", TIME_STEP);
                    m_slippageUpdateMat.SetFloat("_TexSize", (float)TEX_SIZE);
                    m_slippageUpdateMat.SetFloat("_Layers", (float)(i + 1));
                    m_slippageUpdateMat.SetTexture("_SlippageOutflow", m_slippageOutflow);

                    Graphics.Blit(m_terrainField[READ], m_terrainField[WRITE], m_slippageUpdateMat);
                    RTUtility.Swap(m_terrainField);
                }
            }
        }
Exemple #4
0
        /// <summary>
        ///  Calculates how much ground should go in sediment flow aka force-based erosion
        ///  Transfers m_terrainField to m_sedimentField basing on
        ///  m_waterVelocity, m_sedimentCapacity, m_dissolvingConstant,
        ///  m_depositionConstant, m_tiltAngle, m_minTiltAngle
        /// Also calculates m_tiltAngle
        /// </summary>
        private void DissolveAndDeposition(DoubleDataTexture terrainField, Vector4 dissolvingConstant, float minTiltAngle, int TERRAIN_LAYERS)
        {
            link.m_tiltAngleMat.SetFloat("_TexSize", size);
            link.m_tiltAngleMat.SetFloat("_Layers", TERRAIN_LAYERS);
            link.m_tiltAngleMat.SetTexture("_TerrainField", terrainField.READ);

            Graphics.Blit(null, tiltAngle, link.m_tiltAngleMat);

            link.dissolutionAndDepositionMat.SetTexture("_TerrainField", terrainField.READ);
            link.dissolutionAndDepositionMat.SetTexture("_SedimentField", sedimentField.READ);
            link.dissolutionAndDepositionMat.SetTexture("_VelocityField", velocity.READ);
            link.dissolutionAndDepositionMat.SetTexture("_WaterField", main.READ);
            link.dissolutionAndDepositionMat.SetTexture("_TiltAngle", tiltAngle);
            link.dissolutionAndDepositionMat.SetFloat("_MinTiltAngle", minTiltAngle);
            link.dissolutionAndDepositionMat.SetFloat("_SedimentCapacity", sedimentCapacity);
            link.dissolutionAndDepositionMat.SetVector("_DissolvingConstant", dissolvingConstant);
            link.dissolutionAndDepositionMat.SetFloat("_DepositionConstant", depositionConstant);
            link.dissolutionAndDepositionMat.SetFloat("_Layers", (float)TERRAIN_LAYERS);
            link.dissolutionAndDepositionMat.SetFloat("_DissolveLimit", dissolveLimit); //nash added it

            RenderTexture[] terrainAndSediment = new RenderTexture[3] {
                terrainField.WRITE, sedimentField.WRITE, sedimentDeposition.WRITE
            };

            RTUtility.MultiTargetBlit(terrainAndSediment, link.dissolutionAndDepositionMat);
            terrainField.Swap();
            sedimentField.Swap();
            sedimentDeposition.Swap();
        }
Exemple #5
0
        private void ErosionAndDeposition()
        {
            m_tiltAngleMat.SetFloat("_TexSize", (float)TEX_SIZE);
            m_tiltAngleMat.SetFloat("_Layers", TERRAIN_LAYERS);
            m_tiltAngleMat.SetTexture("_TerrainField", m_terrainField[READ]);

            Graphics.Blit(null, m_tiltAngle, m_tiltAngleMat);

            m_erosionAndDepositionMat.SetTexture("_TerrainField", m_terrainField[READ]);
            m_erosionAndDepositionMat.SetTexture("_SedimentField", m_sedimentField[READ]);
            m_erosionAndDepositionMat.SetTexture("_VelocityField", m_waterVelocity[READ]);
            m_erosionAndDepositionMat.SetTexture("_TiltAngle", m_tiltAngle);
            m_erosionAndDepositionMat.SetFloat("_MinTiltAngle", m_minTiltAngle);
            m_erosionAndDepositionMat.SetFloat("_SedimentCapacity", m_sedimentCapacity);
            m_erosionAndDepositionMat.SetVector("_DissolvingConstant", m_dissolvingConstant);
            m_erosionAndDepositionMat.SetFloat("_DepositionConstant", m_depositionConstant);
            m_erosionAndDepositionMat.SetFloat("_Layers", (float)TERRAIN_LAYERS);

            RenderTexture[] terrainAndSediment = new RenderTexture[2] {
                m_terrainField[WRITE], m_sedimentField[WRITE]
            };

            RTUtility.MultiTargetBlit(terrainAndSediment, m_erosionAndDepositionMat);
            RTUtility.Swap(m_terrainField);
            RTUtility.Swap(m_sedimentField);
        }
Exemple #6
0
        private void ApplyFreeSlip(RenderTexture[] field)
        {
            float   u = 1.0f / (float)TEX_SIZE;
            Vector2 offset;

            Graphics.Blit(field[READ], field[WRITE]);

            offset = new Vector2(u, 0.0f);
            m_applyFreeSlipMat.SetVector("_Offset", offset);
            RTUtility.Blit(field[READ], field[WRITE], m_applyFreeSlipMat, m_rectLeft, 0, false);

            offset = new Vector2(0.0f, u);
            m_applyFreeSlipMat.SetVector("_Offset", offset);
            RTUtility.Blit(field[READ], field[WRITE], m_applyFreeSlipMat, m_rectBottom, 0, false);

            offset = new Vector2(-u, 0.0f);
            m_applyFreeSlipMat.SetVector("_Offset", offset);
            RTUtility.Blit(field[READ], field[WRITE], m_applyFreeSlipMat, m_rectRight, 0, false);

            offset = new Vector2(0.0f, -u);
            m_applyFreeSlipMat.SetVector("_Offset", offset);
            RTUtility.Blit(field[READ], field[WRITE], m_applyFreeSlipMat, m_rectTop, 0, false);

            RTUtility.Swap(field);
        }
Exemple #7
0
        private void Update()
        {
            RTUtility.SetToPoint(m_terrainField);
            RTUtility.SetToPoint(m_waterField);

            WaterInput();

            ApplyFreeSlip(m_terrainField);
            ApplyFreeSlip(m_sedimentField);
            ApplyFreeSlip(m_waterField);
            ApplyFreeSlip(m_regolithField);

            OutFlow(m_waterField, m_waterOutFlow, m_waterDamping);

            WaterVelocity();

            ErosionAndDeposition();
            ApplyFreeSlip(m_terrainField);
            ApplyFreeSlip(m_sedimentField);

            AdvectSediment();

            DisintegrateAndDeposit();
            ApplyFreeSlip(m_terrainField);
            ApplyFreeSlip(m_regolithField);

            OutFlow(m_regolithField, m_regolithOutFlow, m_regolithDamping);

            ApplySlippage();

            RTUtility.SetToBilinear(m_terrainField);
            RTUtility.SetToBilinear(m_waterField);

            //if the size of the mesh does not match the size of the teture
            //the y axis needs to be scaled
            float scaleY = (float)TOTAL_GRID_SIZE / (float)TEX_SIZE;

            m_landMat.SetFloat("_ScaleY", scaleY);
            m_landMat.SetFloat("_TexSize", (float)TEX_SIZE);
            m_landMat.SetTexture("_MainTex", m_terrainField[READ]);
            m_landMat.SetFloat("_Layers", (float)TERRAIN_LAYERS);

            m_waterMat.SetTexture("_SedimentField", m_sedimentField[READ]);
            m_waterMat.SetTexture("_VelocityField", m_waterVelocity[READ]);
            m_waterMat.SetFloat("_ScaleY", scaleY);
            m_waterMat.SetFloat("_TexSize", (float)TEX_SIZE);
            m_waterMat.SetTexture("_WaterField", m_waterField[READ]);
            m_waterMat.SetTexture("_MainTex", m_terrainField[READ]);
            m_waterMat.SetFloat("_Layers", (float)TERRAIN_LAYERS);
            m_waterMat.SetVector("_SunDir", m_sun.transform.forward * -1.0f);
        }
        /// <summary>
        /// Transfers ground to regolith basing on water level, regolith level, max_regolith
        /// aka dissolution based erosion
        /// </summary>
        private void DisintegrateAndDeposit()
        {
            m_disintegrateAndDepositMat.SetFloat("_Layers", (float)TERRAIN_LAYERS);
            m_disintegrateAndDepositMat.SetTexture("_TerrainField", terrainField.READ);
            m_disintegrateAndDepositMat.SetTexture("_WaterField", water.main.READ);
            m_disintegrateAndDepositMat.SetTexture("_RegolithField", regolithField.READ);
            m_disintegrateAndDepositMat.SetFloat("_MaxRegolith", maxRegolith);

            RenderTexture[] terrainAndRegolith = new RenderTexture[2] {
                terrainField.WRITE, regolithField.WRITE
            };

            RTUtility.MultiTargetBlit(terrainAndRegolith, m_disintegrateAndDepositMat);
            terrainField.Swap();
            regolithField.Swap();
        }
Exemple #9
0
        private void DisintegrateAndDeposit()
        {
            m_disintegrateAndDepositMat.SetFloat("_Layers", (float)TERRAIN_LAYERS);
            m_disintegrateAndDepositMat.SetTexture("_TerrainField", m_terrainField[READ]);
            m_disintegrateAndDepositMat.SetTexture("_WaterField", m_waterField[READ]);
            m_disintegrateAndDepositMat.SetTexture("_RegolithField", m_regolithField[READ]);
            m_disintegrateAndDepositMat.SetFloat("_MaxRegolith", m_maxRegolith);

            RenderTexture[] terrainAndRegolith = new RenderTexture[2] {
                m_terrainField[WRITE], m_regolithField[WRITE]
            };

            RTUtility.MultiTargetBlit(terrainAndRegolith, m_disintegrateAndDepositMat);
            RTUtility.Swap(m_terrainField);
            RTUtility.Swap(m_regolithField);
        }
Exemple #10
0
        public void onLoadTerrain()
        {
            // Open file with filter
            var extensions = new[] {
                new ExtensionFilter("Image Files", "png", "jpg", "jpeg")
            };
            var selected = StandaloneFileBrowser.OpenFilePanel("Open File", "", extensions, false);

            if (selected[0] != "")
            {
                //var tex = RTUtility.Load(Path.GetFileNameWithoutExtension(selected));
                var tex = RTUtility.Load(selected[0]);
                if (tex != null)
                {
                    sim.SetTerrain(tex);
                }
            }
        }
Exemple #11
0
        private void AdvectSediment()
        {
            m_advectSedimentMat.SetFloat("_TexSize", (float)TEX_SIZE);
            m_advectSedimentMat.SetFloat("T", TIME_STEP);
            m_advectSedimentMat.SetFloat("_VelocityFactor", 1.0f);
            m_advectSedimentMat.SetTexture("_VelocityField", m_waterVelocity[READ]);

            Graphics.Blit(m_sedimentField[READ], m_advectSediment[0], m_advectSedimentMat);

            m_advectSedimentMat.SetFloat("_VelocityFactor", -1.0f);
            Graphics.Blit(m_advectSediment[0], m_advectSediment[1], m_advectSedimentMat);

            m_processMacCormackMat.SetFloat("_TexSize", (float)TEX_SIZE);
            m_processMacCormackMat.SetFloat("T", TIME_STEP);
            m_processMacCormackMat.SetTexture("_VelocityField", m_waterVelocity[READ]);
            m_processMacCormackMat.SetTexture("_InterField1", m_advectSediment[0]);
            m_processMacCormackMat.SetTexture("_InterField2", m_advectSediment[1]);

            Graphics.Blit(m_sedimentField[READ], m_sedimentField[WRITE], m_processMacCormackMat);
            RTUtility.Swap(m_sedimentField);
        }
Exemple #12
0
        private void WaterInput()
        {
            if (Input.GetKey(KeyCode.DownArrow))
            {
                m_waterInputPoint.y -= m_waterInputSpeed * Time.deltaTime;
            }

            if (Input.GetKey(KeyCode.UpArrow))
            {
                m_waterInputPoint.y += m_waterInputSpeed * Time.deltaTime;
            }

            if (Input.GetKey(KeyCode.LeftArrow))
            {
                m_waterInputPoint.x -= m_waterInputSpeed * Time.deltaTime;
            }

            if (Input.GetKey(KeyCode.RightArrow))
            {
                m_waterInputPoint.x += m_waterInputSpeed * Time.deltaTime;
            }

            if (m_waterInputAmount > 0.0f)
            {
                m_waterInputMat.SetVector("_Point", m_waterInputPoint);
                m_waterInputMat.SetFloat("_Radius", m_waterInputRadius);
                m_waterInputMat.SetFloat("_Amount", m_waterInputAmount);

                Graphics.Blit(m_waterField[READ], m_waterField[WRITE], m_waterInputMat);
                RTUtility.Swap(m_waterField);
            }

            if (m_evaporationConstant > 0.0f)
            {
                m_evaprationMat.SetFloat("_EvaporationConstant", m_evaporationConstant);

                Graphics.Blit(m_waterField[READ], m_waterField[WRITE], m_evaprationMat);
                RTUtility.Swap(m_waterField);
            }
        }
Exemple #13
0
        private void WaterVelocity()
        {
            m_waterVelocityMat.SetFloat("_TexSize", (float)TEX_SIZE);
            m_waterVelocityMat.SetFloat("L", CELL_LENGTH);
            m_waterVelocityMat.SetTexture("_WaterField", m_waterField[READ]);
            m_waterVelocityMat.SetTexture("_WaterFieldOld", m_waterField[WRITE]);
            m_waterVelocityMat.SetTexture("_OutFlowField", m_waterOutFlow[READ]);

            Graphics.Blit(null, m_waterVelocity[READ], m_waterVelocityMat);

            const float viscosity  = 10.5f;
            const int   iterations = 2;

            m_diffuseVelocityMat.SetFloat("_TexSize", (float)TEX_SIZE);
            m_diffuseVelocityMat.SetFloat("_Alpha", CELL_AREA / (viscosity * TIME_STEP));

            for (int i = 0; i < iterations; i++)
            {
                Graphics.Blit(m_waterVelocity[READ], m_waterVelocity[WRITE], m_diffuseVelocityMat);
                RTUtility.Swap(m_waterVelocity);
            }
        }
Exemple #14
0
        private void OutFlow(RenderTexture[] field, RenderTexture[] outFlow, float damping)
        {
            m_outFlowMat.SetFloat("_TexSize", (float)TEX_SIZE);
            m_outFlowMat.SetFloat("T", TIME_STEP);
            m_outFlowMat.SetFloat("L", PIPE_LENGTH);
            m_outFlowMat.SetFloat("A", CELL_AREA);
            m_outFlowMat.SetFloat("G", GRAVITY);
            m_outFlowMat.SetFloat("_Layers", TERRAIN_LAYERS);
            m_outFlowMat.SetFloat("_Damping", 1.0f - damping);
            m_outFlowMat.SetTexture("_TerrainField", m_terrainField[READ]);
            m_outFlowMat.SetTexture("_Field", field[READ]);

            Graphics.Blit(outFlow[READ], outFlow[WRITE], m_outFlowMat);
            RTUtility.Swap(outFlow);

            m_fieldUpdateMat.SetFloat("_TexSize", (float)TEX_SIZE);
            m_fieldUpdateMat.SetFloat("T", TIME_STEP);
            m_fieldUpdateMat.SetFloat("L", PIPE_LENGTH);
            m_fieldUpdateMat.SetTexture("_OutFlowField", outFlow[READ]);

            Graphics.Blit(field[READ], field[WRITE], m_fieldUpdateMat);
            RTUtility.Swap(field);
        }
Exemple #15
0
        private void InitMaps()
        {
            RTUtility.ClearColor(m_terrainField);
            RTUtility.ClearColor(m_waterOutFlow);
            RTUtility.ClearColor(m_waterVelocity);
            RTUtility.ClearColor(m_advectSediment);
            RTUtility.ClearColor(m_waterField);
            RTUtility.ClearColor(m_sedimentField);
            RTUtility.ClearColor(m_regolithField);
            RTUtility.ClearColor(m_regolithOutFlow);

            RenderTexture[] noiseTex = new RenderTexture[2];

            noiseTex[0]            = new RenderTexture(TEX_SIZE, TEX_SIZE, 0, RenderTextureFormat.RFloat);
            noiseTex[0].wrapMode   = TextureWrapMode.Clamp;
            noiseTex[0].filterMode = FilterMode.Bilinear;

            noiseTex[1]            = new RenderTexture(TEX_SIZE, TEX_SIZE, 0, RenderTextureFormat.RFloat);
            noiseTex[1].wrapMode   = TextureWrapMode.Clamp;
            noiseTex[1].filterMode = FilterMode.Bilinear;

            GPUPerlinNoise perlin = new GPUPerlinNoise(m_seed);

            perlin.LoadResourcesFor2DNoise();

            m_noiseMat.SetTexture("_PermTable1D", perlin.PermutationTable1D);
            m_noiseMat.SetTexture("_Gradient2D", perlin.Gradient2D);

            for (int j = 0; j < TERRAIN_LAYERS; j++)
            {
                m_noiseMat.SetFloat("_Offset", m_offset[j]);

                float amp  = 0.5f;
                float freq = m_frequency[j];

                //Must clear noise from last pass
                RTUtility.ClearColor(noiseTex);

                //write noise into texture with the settings for this layer
                for (int i = 0; i < m_octaves[j]; i++)
                {
                    m_noiseMat.SetFloat("_Frequency", freq);
                    m_noiseMat.SetFloat("_Amp", amp);
                    m_noiseMat.SetFloat("_Pass", (float)i);

                    Graphics.Blit(noiseTex[READ], noiseTex[WRITE], m_noiseMat, (int)m_layerStyle[j]);
                    RTUtility.Swap(noiseTex);

                    freq *= m_lacunarity[j];
                    amp  *= m_gain[j];
                }

                float useAbs = 0.0f;
                if (m_finalNosieIsAbs[j])
                {
                    useAbs = 1.0f;
                }

                //Mask the layers that we dont want to write into
                Vector4 mask = new Vector4(0.0f, 0.0f, 0.0f, 0.0f);
                mask[j] = 1.0f;

                m_initTerrainMat.SetFloat("_Amp", m_amp[j]);
                m_initTerrainMat.SetFloat("_UseAbs", useAbs);
                m_initTerrainMat.SetVector("_Mask", mask);
                m_initTerrainMat.SetTexture("_NoiseTex", noiseTex[READ]);
                m_initTerrainMat.SetFloat("_Height", TERRAIN_HEIGHT);

                //Apply the noise for this layer to the terrain field
                Graphics.Blit(m_terrainField[READ], m_terrainField[WRITE], m_initTerrainMat);
                RTUtility.Swap(m_terrainField);
            }

            //dont need this tex anymore
            noiseTex[0] = null;
            noiseTex[1] = null;
        }