Example #1
0
    private Vector3 EventPositionForLocalPosition(WaterOnTerrainSFX.IntVector v)
    {
        Rect    rect     = this.GridCellRect(this.SampleGridToWorldGrid(v));
        Vector3 position = base.transform.position;

        position.x = Mathf.Clamp(position.x, rect.xMin, rect.xMax);
        position.z = Mathf.Clamp(position.z, rect.yMin, rect.yMax);
        return(position);
    }
Example #2
0
 public void DrawDebug()
 {
     if (WaterOnTerrainSFX.cellTexture == null)
     {
         WaterOnTerrainSFX.CreateDebugTextures();
     }
     if (Camera.main != null)
     {
         Vector2 vector  = new Vector2(110f, (float)(Camera.main.pixelHeight - 110));
         Vector2 vector2 = vector - new Vector2(105f, 105f);
         GUI.Box(new Rect(vector2.x, vector2.y - 35f, 210f, 245f), "Water On Terrain");
         GUI.Label(new Rect(vector2.x, vector2.y - 10f, 210f, 35f), string.Format("Wetness: {0:f2}", WaterOnTerrainSFX.Wetness));
         float num  = 10f * this.gridCellSize * Mathf.Sqrt(2f);
         float d    = 100f / num;
         float num2 = 0f;
         float num3 = 0f;
         for (int i = 0; i < 21; i++)
         {
             for (int j = 0; j < 21; j++)
             {
                 num2 = Mathf.Max(num2, this.ScoreForSource(new WaterOnTerrainSFX.IntVector(i, j), null));
                 num3 = Mathf.Max(num3, this.sampleGrid[i, j].gradient);
             }
         }
         float     num4     = (num2 <= 0f) ? 0f : (1f / num2);
         float     num5     = (num3 <= 0f) ? 0f : (1f / num3);
         Matrix4x4 matrix4x = Matrix4x4.TRS(vector, Quaternion.Euler(0f, 0f, -LocalPlayer.Transform.eulerAngles.y), new Vector3(1f, -1f, 1f) * d);
         matrix4x *= Matrix4x4.TRS(new Vector3(-LocalPlayer.Transform.position.x, -LocalPlayer.Transform.position.z, 0f), Quaternion.identity, Vector3.one);
         Matrix4x4 matrix = GUI.matrix;
         Color     color  = GUI.color;
         GUI.matrix *= matrix4x;
         for (int k = 0; k < 21; k++)
         {
             for (int l = 0; l < 21; l++)
             {
                 WaterOnTerrainSFX.IntVector intVector = new WaterOnTerrainSFX.IntVector(k, l);
                 GUI.color = new Color(0f, this.sampleGrid[k, l].gradient * num5, this.ScoreForSource(intVector, null) * num4, 0.5f);
                 GUI.DrawTexture(this.GridCellRect(this.SampleGridToWorldGrid(intVector)), WaterOnTerrainSFX.cellTexture);
             }
         }
         GUI.matrix = matrix;
         GUI.color  = color;
         if (this.centreSource.IsPlaying())
         {
             this.DrawSource(this.centreSource, matrix4x);
         }
         for (int m = 0; m < this.sources.Count; m++)
         {
             this.DrawSource(this.sources[m], matrix4x);
         }
     }
 }
Example #3
0
 private void CacheGradients(WaterOnTerrainSFX.IntVector basePosition, WaterOnTerrainSFX.IntVector start, WaterOnTerrainSFX.IntVector end)
 {
     for (int i = start.x; i < end.x; i++)
     {
         for (int j = start.y; j < end.y; j++)
         {
             WaterOnTerrainSFX.IntVector b = new WaterOnTerrainSFX.IntVector(i, j) - WaterOnTerrainSFX.SAMPLE_GRID_CENTRE;
             Vector3 position = this.GridCellCentre(basePosition + b);
             position = Terrain.activeTerrain.transform.InverseTransformPoint(position);
             TerrainData terrainData = Terrain.activeTerrain.terrainData;
             position.x /= terrainData.size.x;
             position.z /= terrainData.size.z;
             this.sampleGrid[i, j].gradient = terrainData.GetSteepness(position.x, position.z);
         }
     }
 }
Example #4
0
    private float ScoreForSource(WaterOnTerrainSFX.IntVector position, WaterOnTerrainSFX.Source source)
    {
        float num = 0f;

        if (WaterOnTerrainSFX.IsWithinSampleGrid(position))
        {
            num = this.sampleGrid[position.x, position.y].score;
            for (int i = 0; i < this.sources.Count; i++)
            {
                if (this.sources[i] != source)
                {
                    float t = (float)((position - this.sources[i].localPosition).squareMagnitude / 5);
                    num *= Mathf.Lerp(0.1f, 1f, t);
                }
            }
        }
        return(num);
    }
Example #5
0
 private void MoveSampleGridEntries(WaterOnTerrainSFX.IntVector offset)
 {
     WaterOnTerrainSFX.IntVector intVector;
     WaterOnTerrainSFX.IntVector intVector2;
     WaterOnTerrainSFX.IntVector intVector3;
     if (offset.x <= 0)
     {
         intVector.x  = 0;
         intVector2.x = 21 + offset.x;
         intVector3.x = 1;
     }
     else
     {
         intVector.x  = 20;
         intVector2.x = offset.x - 1;
         intVector3.x = -1;
     }
     if (offset.y <= 0)
     {
         intVector.y  = 0;
         intVector2.y = 21 + offset.y;
         intVector3.y = 1;
     }
     else
     {
         intVector.y  = 20;
         intVector2.y = offset.y - 1;
         intVector3.y = -1;
     }
     for (int num = intVector.x; num != intVector2.x; num += intVector3.x)
     {
         for (int num2 = intVector.y; num2 != intVector2.y; num2 += intVector3.y)
         {
             this.sampleGrid[num, num2] = this.sampleGrid[num - offset.x, num2 - offset.y];
         }
     }
 }
Example #6
0
 private float GradientForLocalPosition(WaterOnTerrainSFX.IntVector v)
 {
     return(this.sampleGrid[v.x, v.y].gradient);
 }
Example #7
0
    private void UpdateSources()
    {
        float num = this.maximumScore / 5f;
        int   i   = 0;

        while (i < this.sources.Count)
        {
            WaterOnTerrainSFX.Source    source        = this.sources[i];
            WaterOnTerrainSFX.IntVector localPosition = source.localPosition;
            float num2 = this.ScoreForSource(source.localPosition, source);
            for (int j = 0; j < WaterOnTerrainSFX.NEIGHBOURS.Length; j++)
            {
                WaterOnTerrainSFX.IntVector intVector = source.localPosition + WaterOnTerrainSFX.NEIGHBOURS[j];
                float num3 = this.ScoreForSource(intVector, source);
                if (num3 > num2)
                {
                    num2          = num3;
                    localPosition = intVector;
                }
            }
            if (num2 > num)
            {
                source.localPosition = localPosition;
                source.SetGradient(this.GradientForLocalPosition(source.localPosition));
                i++;
            }
            else
            {
                this.StopSource(i);
            }
        }
        if (!this.centreSource.IsPlaying())
        {
            this.centreSource.Start(this.eventDescription, this.gradientParameterIndex, this.wetnessParameterIndex, WaterOnTerrainSFX.SAMPLE_GRID_CENTRE, base.transform.position, this.GradientForLocalPosition(WaterOnTerrainSFX.SAMPLE_GRID_CENTRE));
        }
        while (this.sourcePool.Count > 0)
        {
            WaterOnTerrainSFX.IntVector intVector2 = new WaterOnTerrainSFX.IntVector(0, 0);
            float num4 = num;
            WaterOnTerrainSFX.IntVector intVector3;
            intVector3.x = 0;
            while (intVector3.x < 21)
            {
                intVector3.y = 0;
                while (intVector3.y < 21)
                {
                    float num5 = this.ScoreForSource(intVector3, null);
                    if (num5 > num4)
                    {
                        num4       = num5;
                        intVector2 = intVector3;
                    }
                    intVector3.y++;
                }
                intVector3.x++;
            }
            if (num4 <= num)
            {
                break;
            }
            WaterOnTerrainSFX.Source source2 = this.sourcePool.Pop();
            source2.Start(this.eventDescription, this.gradientParameterIndex, this.wetnessParameterIndex, intVector2, this.EventPositionForLocalPosition(intVector2), this.GradientForLocalPosition(intVector2));
            this.sources.Add(source2);
        }
        float distance = Time.deltaTime * this.sourceSpeed;

        this.centreSource.UpdateEvent(base.transform.position, float.MaxValue, WaterOnTerrainSFX.Wetness);
        for (int k = 0; k < this.sources.Count; k++)
        {
            WaterOnTerrainSFX.Source source3 = this.sources[k];
            source3.UpdateEvent(this.EventPositionForLocalPosition(source3.localPosition), distance, WaterOnTerrainSFX.Wetness);
        }
    }
Example #8
0
 public void Start(EventDescription eventDescription, int gradientParameterIndex, int wetnessParameterIndex, WaterOnTerrainSFX.IntVector localPosition, Vector3 eventPosition, float gradient)
 {
     this.localPosition          = localPosition;
     this.eventPosition          = eventPosition;
     this.gradientParameterIndex = gradientParameterIndex;
     this.wetnessParameterIndex  = wetnessParameterIndex;
     UnityUtil.ERRCHECK(eventDescription.createInstance(out this.eventInstance));
     UnityUtil.ERRCHECK(this.eventInstance.set3DAttributes(eventPosition.to3DAttributes()));
     this.SetGradient(gradient);
     UnityUtil.ERRCHECK(this.eventInstance.start());
 }
Example #9
0
 private Rect GridCellRect(WaterOnTerrainSFX.IntVector v)
 {
     return(new Rect((float)v.x * this.gridCellSize, (float)v.y * this.gridCellSize, this.gridCellSize, this.gridCellSize));
 }
Example #10
0
 private Vector3 GridCellCentre(WaterOnTerrainSFX.IntVector v)
 {
     return(new Vector3(((float)v.x + 0.5f) * this.gridCellSize, 0f, ((float)v.y + 0.5f) * this.gridCellSize));
 }
Example #11
0
 private void UpdateSampleGrid()
 {
     WaterOnTerrainSFX.IntVector intVector = new WaterOnTerrainSFX.IntVector(Mathf.FloorToInt(base.transform.position.x / this.gridCellSize), Mathf.FloorToInt(base.transform.position.z / this.gridCellSize));
     if (intVector != this.gridPosition)
     {
         WaterOnTerrainSFX.IntVector intVector2 = intVector - this.gridPosition;
         if (Math.Abs(intVector2.x) < 21 && Math.Abs(intVector2.y) < 21)
         {
             this.MoveSampleGridEntries(-intVector2);
             WaterOnTerrainSFX.IntVector intVector3;
             WaterOnTerrainSFX.IntVector intVector4;
             if (intVector2.x < 0)
             {
                 intVector3.x = 0;
                 intVector4.x = -intVector2.x;
             }
             else
             {
                 intVector3.x = 21 - intVector2.x;
                 intVector4.x = 21;
             }
             if (intVector2.y < 0)
             {
                 intVector3.y = 0;
                 intVector4.y = -intVector2.y;
             }
             else
             {
                 intVector3.y = 21 - intVector2.y;
                 intVector4.y = 21;
             }
             this.CacheGradients(intVector, new WaterOnTerrainSFX.IntVector(0, intVector3.y), new WaterOnTerrainSFX.IntVector(21, intVector4.y));
             if (intVector3.y > 0)
             {
                 this.CacheGradients(intVector, new WaterOnTerrainSFX.IntVector(intVector3.x, 0), new WaterOnTerrainSFX.IntVector(intVector4.x, intVector3.y));
             }
             else
             {
                 this.CacheGradients(intVector, new WaterOnTerrainSFX.IntVector(intVector3.x, intVector4.y), new WaterOnTerrainSFX.IntVector(intVector4.x, 21));
             }
         }
         else
         {
             this.CacheGradients(intVector, new WaterOnTerrainSFX.IntVector(0, 0), new WaterOnTerrainSFX.IntVector(21, 21));
         }
         this.CalculateSampleGridScores();
         int i = 0;
         while (i < this.sources.Count)
         {
             WaterOnTerrainSFX.Source source = this.sources[i];
             this.sources[i].localPosition -= intVector2;
             int squareMagnitude = (source.localPosition - WaterOnTerrainSFX.SAMPLE_GRID_CENTRE).squareMagnitude;
             if (!WaterOnTerrainSFX.IsWithinSampleGrid(source.localPosition) || squareMagnitude > 100)
             {
                 this.StopSource(i);
             }
             else
             {
                 i++;
             }
         }
         this.gridPosition = intVector;
     }
 }
Example #12
0
 private WaterOnTerrainSFX.IntVector SampleGridToWorldGrid(WaterOnTerrainSFX.IntVector v)
 {
     return(v + this.gridPosition - WaterOnTerrainSFX.SAMPLE_GRID_CENTRE);
 }
Example #13
0
 private static bool IsWithinSampleGrid(WaterOnTerrainSFX.IntVector v)
 {
     return(v.x >= 0 && v.x < 21 && v.y >= 0 && v.y < 21);
 }