예제 #1
0
			/*public Vector2 GetAreaNormal (int cx, int cz, int range)
			{
				CoordRect areaRect = new CoordRect(cx-range-1, cz-range-1, cx+range, cz+range);
				CoordRect intersection = CoordRect.Intersect(rect, areaRect);

				float nx = 0; float nz = 0;

				Coord min = intersection.Min; Coord max = intersection.Max;
				for (int x=min.x; x<max.x; x++)
					for (int z=min.z; z<max.z; z++)
				{
					nx += 
				}

			}*/

		#endregion

		#region Sorting

			// ???
			public bool[] InRect (CoordRect area = new CoordRect())
			{
				Matrix2<bool> result = new Matrix2<bool>(rect);
				CoordRect intersection = CoordRect.Intersect(rect,area);
				Coord min = intersection.Min; Coord max = intersection.Max;
				for (int x=min.x; x<max.x; x++)
					for (int z=min.z; z<max.z; z++)
						result[x,z] = true;
				return result.array;
			}
예제 #2
0
            public void Add(Vector3 pos, float radius)
            {
                //adding heights
                float     pixelSize = terrain.terrainData.size.x / terrain.terrainData.heightmapResolution;
                CoordRect rect      = (pos - terrain.transform.localPosition).ToCoordRect(radius, pixelSize);

                CoordRect intersection = CoordRect.Intersect(rect, new CoordRect(0, 0, terrain.terrainData.heightmapResolution, terrain.terrainData.heightmapResolution));

                if (intersection.size.x > 0 && intersection.size.z > 0)
                {
                    int numHeightCells = Mathf.CeilToInt(1f * terrain.terrainData.heightmapResolution / cellSize);
                    if (heights == null)
                    {
                        heights = new Matrix2 <float[, ]>(numHeightCells, numHeightCells);
                    }

                    foreach (Coord cellCoord in intersection.Cells(cellSize))
                    {
                        if (heights[cellCoord] != null)
                        {
                            continue;
                        }
                        heights[cellCoord] = terrain.terrainData.SafeGetHeights(cellCoord.x * cellSize, cellCoord.z * cellSize, cellSize, cellSize);
                    }
                }

                //adding splats
                if (terrain.terrainData.alphamapLayers == 0)
                {
                    return;
                }
                pixelSize = terrain.terrainData.size.x / terrain.terrainData.alphamapResolution;
                rect      = (pos - terrain.transform.localPosition).ToCoordRect(radius, pixelSize);

                intersection = CoordRect.Intersect(rect, new CoordRect(0, 0, terrain.terrainData.alphamapResolution, terrain.terrainData.alphamapResolution));
                if (intersection.size.x > 0 && intersection.size.z > 0)
                {
                    int numSplatCells = Mathf.CeilToInt(1f * terrain.terrainData.alphamapResolution / cellSize);
                    if (splats == null)
                    {
                        splats = new Matrix2 <float[, , ]>(numSplatCells, numSplatCells);
                    }

                    foreach (Coord cellCoord in intersection.Cells(cellSize))
                    {
                        if (splats[cellCoord] != null)
                        {
                            continue;
                        }
                        splats[cellCoord] = terrain.terrainData.SafeGetAlphamaps(cellCoord.x * cellSize, cellCoord.z * cellSize, cellSize, cellSize);
                    }
                }
            }
예제 #3
0
			static public void AddSplatmaps (TerrainData data, Matrix[] matrices, int[] channels, float[] opacity, float[,,] array=null, float brushFallof=0.5f)
			{
				int numChannels = data.alphamapLayers;
				bool[] usedChannels = new bool[numChannels];
				for (int i=0; i<channels.Length; i++) usedChannels[channels[i]] = true;
				float[] slice = new float[numChannels];

				Coord dataSize = new Coord(data.alphamapResolution, data.alphamapResolution);
				CoordRect dataRect = new CoordRect(new Coord(0,0), dataSize);
				CoordRect intersection = CoordRect.Intersect(dataRect, matrices[0].rect);
				
				if (array==null) array = data.GetAlphamaps(intersection.offset.x, intersection.offset.z, intersection.size.x, intersection.size.z);

				Coord min = intersection.Min; Coord max = intersection.Max;
				for (int x=min.x; x<max.x; x++)
					for (int z=min.z; z<max.z; z++)
				{
					//calculating fallof and opacity
					float fallofFactor = matrices[0].Fallof(x,z,brushFallof);
					if (Mathf.Approximately(fallofFactor,0)) continue;

					//reading slice
					for (int c=0; c<numChannels; c++) slice[c] = array[z-min.z, x-min.x, c];

					//converting matrices to additive
					for (int i=0; i<matrices.Length; i++) matrices[i][x,z] = Mathf.Max(0, matrices[i][x,z] - slice[channels[i]]);

					//apply fallof
					for (int i=0; i<matrices.Length; i++) matrices[i][x,z] *= fallofFactor * opacity[i];

					//calculating sum of adding values
					float addedSum = 0; //the sum of adding channels
					for (int i=0; i<matrices.Length; i++) addedSum += matrices[i][x,z];
					//if (addedSum < 0.00001f) continue; //no need to do anything

					//if addedsum exceeds 1 - equalizing matrices
					if (addedSum > 1f) 
						{ for (int i=0; i<matrices.Length; i++) matrices[i][x,z] /= addedSum; addedSum=1; }

					//multiplying all values on a remaining amount
					float multiplier = 1-addedSum;
					for (int c=0; c<numChannels; c++) slice[c] *= multiplier;

					//adding matrices
					for (int i=0; i<matrices.Length; i++) slice[channels[i]] += matrices[i][x,z];

					//saving slice
					for (int c=0; c<numChannels; c++) array[z-min.z, x-min.x, c] = slice[c];
				}

				data.SetAlphamaps(intersection.offset.x, intersection.offset.z, array);
			}
예제 #4
0
        public void Fill(Matrix2 <T> m, bool removeBorders = false)
        {
            CoordRect intersection = CoordRect.Intersect(rect, m.rect);
            Coord     min = intersection.Min; Coord max = intersection.Max;

            for (int x = min.x; x < max.x; x++)
            {
                for (int z = min.z; z < max.z; z++)
                {
                    this[x, z] = m[x, z];
                }
            }
            if (removeBorders)
            {
                RemoveBorders(intersection);
            }
        }
예제 #5
0
			/*public void FromTexture (Texture2D texture, Coord textureOffset=new Coord(), bool fillBorders=false)
			{
				Coord textureSize = new Coord(texture.width, texture.height);
				CoordRect textureRect = new CoordRect(textureOffset, textureSize);
				CoordRect intersection = CoordRect.Intersect(textureRect, rect);

				Color[] colors = texture.GetPixels(intersection.offset.x - textureOffset.x, intersection.offset.z - textureOffset.z, intersection.size.x, intersection.size.z);

				Coord min = intersection.Min; Coord max = intersection.Max;
				for (int x=min.x; x<max.x; x++)
					for (int z=min.z; z<max.z; z++)
				{
					int tx = x-min.x; int tz = z-min.z;
					Color col = colors[tz*(max.x-min.x) + tx];

					this[x,z] = (col.r+col.g+col.b)/3;
				}

				if (fillBorders) RemoveBorders(intersection);
			}*/

			public void FromTexture (Texture2D texture)
			{
				CoordRect textureRect = new CoordRect(0,0, texture.width, texture.height);
				CoordRect intersection = CoordRect.Intersect(textureRect, rect);

				Color[] colors = texture.GetPixels(intersection.offset.x, intersection.offset.z, intersection.size.x, intersection.size.z);

				Coord min = intersection.Min; Coord max = intersection.Max;
				for (int x=min.x; x<max.x; x++)
					for (int z=min.z; z<max.z; z++)
				{
					int tx = x-min.x; int tz = z-min.z;
					Color col = colors[tz*(max.x-min.x) + tx];

					this[x,z] = (col.r+col.g+col.b)/3;
				}
			}
예제 #6
0
			public float[,,] ReadSplatmap (TerrainData data, int channel, float[,,] array=null)
			{
				CoordRect intersection = CoordRect.Intersect(rect, new CoordRect(0,0,data.alphamapResolution, data.alphamapResolution));
				
				//get heights
				if (array==null) array = data.GetAlphamaps(intersection.offset.x, intersection.offset.z, intersection.size.x, intersection.size.z); //returns x and z swapped

				//reading array
				Coord min = intersection.Min; Coord max = intersection.Max;
				for (int x=min.x; x<max.x; x++)
					for (int z=min.z; z<max.z; z++)
						this[x,z] = array[z-min.z, x-min.x, channel];

				//removing borders
				RemoveBorders(intersection);

				return array;
			}
예제 #7
0
			public float[,] ReadHeighmap (TerrainData data, float height=1)
			{
				CoordRect intersection = CoordRect.Intersect(rect, new CoordRect(0,0,data.heightmapResolution, data.heightmapResolution));
				
				//get heights
				float[,] array = data.GetHeights(intersection.offset.x, intersection.offset.z, intersection.size.x, intersection.size.z); //returns x and z swapped

				//reading 2d array
				Coord min = intersection.Min; Coord max = intersection.Max;
				for (int x=min.x; x<max.x; x++)
					for (int z=min.z; z<max.z; z++)
						this[x,z] = array[z-min.z, x-min.x] * height;

				//removing borders
				RemoveBorders(intersection);

				return array;
			}
예제 #8
0
			public void WriteHeightmap (TerrainData data, float[,] array=null, float brushFallof=0.5f)
			{
				CoordRect intersection = CoordRect.Intersect(rect, new CoordRect(0,0,data.heightmapResolution, data.heightmapResolution));
				
				//checking ref array
				if (array == null || array.Length != intersection.size.x*intersection.size.z) array = new float[intersection.size.z,intersection.size.x]; //x and z swapped

				//write to 2d array
				Coord min = intersection.Min; Coord max = intersection.Max;
				for (int x=min.x; x<max.x; x++)
					for (int z=min.z; z<max.z; z++)
				{
					float fallofFactor = Fallof(x,z,brushFallof);
					if (Mathf.Approximately(fallofFactor,0)) continue;
					array[z-min.z, x-min.x] = this[x,z]*fallofFactor + array[z-min.z, x-min.x]*(1-fallofFactor);
					//array[z-min.z, x-min.x] += this[x,z];
				}

				data.SetHeights(intersection.offset.x, intersection.offset.z, array);
			}
예제 #9
0
			public void ToTexture (Texture2D texture=null, Color[] colors=null, float rangeMin=0, float rangeMax=1, bool resizeTexture=false)
			{
				//creating or resizing texture
				if (texture == null) texture = new Texture2D(rect.size.x, rect.size.z);
				if (resizeTexture) texture.Resize(rect.size.x, rect.size.z);
				
				//finding matrix-texture intersection
				Coord textureSize = new Coord(texture.width, texture.height);
				CoordRect textureRect = new CoordRect(new Coord(0,0), textureSize);
				CoordRect intersection = CoordRect.Intersect(textureRect, rect);
				
				//checking ref color array
				if (colors == null || colors.Length != intersection.size.x*intersection.size.z) colors = new Color[intersection.size.x*intersection.size.z];

				//filling texture
				Coord min = intersection.Min; Coord max = intersection.Max;
				for (int x=min.x; x<max.x; x++)
					for (int z=min.z; z<max.z; z++)
				{
					float val = this[x,z];

					//adjusting value to range
					val -= rangeMin;
					val /= rangeMax-rangeMin;

					//making color gradient
					float byteVal = val * 256;
					int flooredByteVal = (int)byteVal;
					float remainder = byteVal - flooredByteVal;

					float flooredVal = flooredByteVal/256f;
					float ceiledVal = (flooredByteVal+1)/256f;
					
					//saving to colors
					int tx = x-min.x; int tz = z-min.z;
					colors[tz*(max.x-min.x) + tx] = new Color(flooredVal, remainder>0.333f ? ceiledVal : flooredVal, remainder>0.666f ? ceiledVal : flooredVal);
				}
			
				texture.SetPixels(intersection.offset.x, intersection.offset.z, intersection.size.x, intersection.size.z, colors);
				texture.Apply();
			}
예제 #10
0
        public void GetHeight(Matrix matrix, Terrain terrain)
        {
            CoordRect terrainRect  = terrain.GetHeightRect();
            CoordRect intersection = CoordRect.Intersect(terrainRect, matrix.rect);

            if (intersection.size.x <= 0 || intersection.size.z <= 0)
            {
                return;
            }

            float[,] heights = terrain.terrainData.GetHeights(intersection.offset.x - terrainRect.offset.x, intersection.offset.z - terrainRect.offset.z, intersection.size.x, intersection.size.z);

            Coord min = intersection.Min; Coord max = intersection.Max;

            for (int x = min.x; x < max.x; x++)
            {
                for (int z = min.z; z < max.z; z++)
                {
                    matrix[x, z] = heights[z - min.z, x - min.x];                //x and z switched
                }
            }
        }
예제 #11
0
        public void AddSplat(Matrix matrix, Terrain terrain, int channel)
        {
            CoordRect terrainRect  = terrain.GetSplatRect();
            CoordRect intersection = CoordRect.Intersect(terrainRect, matrix.rect);

            if (intersection.size.x <= 0 || intersection.size.z <= 0)
            {
                return;
            }

            float[,,] splats = terrain.terrainData.GetAlphamaps(intersection.offset.x - terrainRect.offset.x, intersection.offset.z - terrainRect.offset.z, intersection.size.x, intersection.size.z);
            int numSplats = splats.GetLength(2);

            Coord min = intersection.Min; Coord max = intersection.Max;

            for (int x = min.x; x < max.x; x++)
            {
                for (int z = min.z; z < max.z; z++)
                {
                    int   sx = z - min.z;  int sz = x - min.x;               //x and z switched
                    float val    = matrix[x, z];
                    float invVal = 1 - matrix[x, z];

                    //multiplying all splats on inverse value
                    for (int s = 0; s < numSplats; s++)
                    {
                        splats[sx, sz, s] *= invVal;
                    }

                    //adding val
                    splats[sx, sz, channel] += val;
                }
            }

            terrain.terrainData.SetAlphamaps(intersection.offset.x - terrainRect.offset.x, intersection.offset.z - terrainRect.offset.z, splats);
        }