Ejemplo n.º 1
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);
			}
Ejemplo n.º 2
0
        public static bool IsIntersecting(CoordRect c1, CoordRect c2)
        {
            if (c2.Contains(c1.offset.x, c1.offset.z) || c2.Contains(c1.offset.x + c1.size.x, c1.offset.z) || c2.Contains(c1.offset.x, c1.offset.z + c1.size.z) || c2.Contains(c1.offset.x + c1.size.x, c1.offset.z + c1.size.z))
            {
                return(true);
            }
            if (c1.Contains(c2.offset.x, c2.offset.z) || c1.Contains(c2.offset.x + c2.size.x, c2.offset.z) || c1.Contains(c2.offset.x, c2.offset.z + c1.size.z) || c1.Contains(c2.offset.x + c2.size.x, c2.offset.z + c2.size.z))
            {
                return(true);
            }

            return(false);
        }
Ejemplo n.º 3
0
        //TODO: test
        public static int DistanceAxisAligned(Coord c, CoordRect rect)          //NOT manhattan dist. offset and size are instead of UnityEngine.Rect
        {
            //finding x distance
            int distPosX = rect.offset.x - c.x;
            int distNegX = c.x - rect.offset.x - rect.size.x;

            int distX;

            if (distPosX >= 0)
            {
                distX = distPosX;
            }
            else if (distNegX >= 0)
            {
                distX = distNegX;
            }
            else
            {
                distX = 0;
            }

            //finding z distance
            int distPosZ = rect.offset.z - c.z;
            int distNegZ = c.z - rect.offset.z - rect.size.z;

            int distZ;

            if (distPosZ >= 0)
            {
                distZ = distPosZ;
            }
            else if (distNegZ >= 0)
            {
                distZ = distNegZ;
            }
            else
            {
                distZ = 0;
            }

            //returning the maximum(!) distance
            if (distX > distZ)
            {
                return(distX);
            }
            else
            {
                return(distZ);
            }
        }
Ejemplo n.º 4
0
        public CoordRect Approximate(int val)
        {
            CoordRect approx = new CoordRect();

            approx.size.x = (size.x / val + 1) * val;
            approx.size.z = (size.z / val + 1) * val;

            approx.offset.x = offset.x - (approx.size.x - size.x) / 2;
            approx.offset.z = offset.z - (approx.size.z - size.z) / 2;

            approx.offset.x = (int)(approx.offset.x / val + 0.5f) * val;
            approx.offset.z = (int)(approx.offset.z / val + 0.5f) * val;

            return(approx);
        }
Ejemplo n.º 5
0
			public Matrix TestResize (CoordRect newRect)
			{
				Matrix result = new Matrix(newRect);

				Coord min = result.rect.Min; Coord max = result.rect.Max;
				for (int x=min.x; x<max.x; x++)
					for (int z=min.z; z<max.z; z++)
				{
					float percentX = 1f*(x-result.rect.offset.x)/result.rect.size.x; float origX = percentX*this.rect.size.x + this.rect.offset.x; 
					float percentZ = 1f*(z-result.rect.offset.z)/result.rect.size.z; float origZ = percentZ*this.rect.size.z + this.rect.offset.z; 
					result[x,z] = this.OutdatedGetInterpolated(origX, origZ);
				}

				return result;
			}
Ejemplo n.º 6
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);
            }
        }
Ejemplo n.º 7
0
 public Matrix2(int x, int z, T[] array = null)
 {
     rect  = new CoordRect(0, 0, x, z);
     count = x * z;
     if (array != null && array.Length < count)
     {
         Debug.LogError("Array length: " + array.Length + " is lower then matrix capacity: " + count);
     }
     if (array != null && array.Length >= count)
     {
         this.array = array;
     }
     else
     {
         this.array = new T[count];
     }
 }
Ejemplo n.º 8
0
			public void FromTextureAlpha (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+col.a)/4;
				}
			}
Ejemplo n.º 9
0
 public Matrix2(Coord offset, Coord size, T[] array = null)
 {
     rect  = new CoordRect(offset, size);
     count = rect.size.x * rect.size.z;
     if (array != null && array.Length < count)
     {
         Debug.Log("Array length: " + array.Length + " is lower then matrix capacity: " + count);
     }
     if (array != null && array.Length >= count)
     {
         this.array = array;
     }
     else
     {
         this.array = new T[count];
     }
 }
Ejemplo n.º 10
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;
			}
Ejemplo n.º 11
0
			public Matrix OutdatedResize (CoordRect newRect, float smoothness=1, Matrix result=null)
			{
				//calculating ratio
				int upscaleRatio = newRect.size.x / rect.size.x;
				int downscaleRatio = rect.size.x / newRect.size.x;

				//checking if rect could be rescaled
				if (upscaleRatio > 1 && !newRect.Divisible(upscaleRatio)) Debug.LogError("Matrix rect " + rect + " could not be upscaled to " + newRect + " with factor " + upscaleRatio);
				if (downscaleRatio > 1 && !rect.Divisible(downscaleRatio)) Debug.LogError("Matrix rect " + rect + " could not be downscaled to " + newRect + " with factor " + downscaleRatio);

				//scaling
				if (upscaleRatio > 1) result = OutdatedUpscale(upscaleRatio, result:result);
				if (downscaleRatio > 1) result = OutdatedDownscale(downscaleRatio, smoothness:smoothness, result:result);

				//returning clone if all ratios are 1
				if (upscaleRatio <= 1 && downscaleRatio <= 1) return Copy(result);
				else return result;
			}
Ejemplo n.º 12
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;
			}
Ejemplo n.º 13
0
        public IEnumerable <Coord> DistanceArea(CoordRect rect)         //same as distance are, but clamped by rect
        {
            int maxDist = Mathf.Max(x - rect.offset.x, rect.Max.x - x, z - rect.offset.z, rect.Max.z - z) + 1;

            if (rect.CheckInRange(this))
            {
                yield return(this);
            }
            for (int i = 0; i < maxDist; i++)
            {
                foreach (Coord c in DistancePerimeter(i))
                {
                    if (rect.CheckInRange(c))
                    {
                        yield return(c);
                    }
                }
            }
        }
Ejemplo n.º 14
0
 public void ClampByRect(CoordRect rect)
 {
     if (x < rect.offset.x)
     {
         x = rect.offset.x;
     }
     if (x >= rect.offset.x + rect.size.x)
     {
         x = rect.offset.x + rect.size.x - 1;
     }
     if (z < rect.offset.z)
     {
         z = rect.offset.z;
     }
     if (z >= rect.offset.z + rect.size.z)
     {
         z = rect.offset.z + rect.size.z - 1;
     }
 }
Ejemplo n.º 15
0
				public Stacker (CoordRect smallRect, CoordRect bigRect)
				{
					this.smallRect = smallRect; this.bigRect = bigRect;
					isDownscaled = false;

					//do not create additional matrices if rect sizes are the same
					if (bigRect==smallRect)
					{
						upscaled = downscaled = new Matrix(bigRect);
					}

					else
					{
						downscaled = new Matrix(smallRect);
						upscaled = new Matrix(bigRect);
						difference = new Matrix(bigRect);
						//once arrays created they should not be resized
					}
				}
Ejemplo n.º 16
0
			public void WriteTextureInterpolated (Texture2D texture, CoordRect textureRect, WrapMode wrap=WrapMode.Once, float rangeMin=0, float rangeMax=1)
			{
				float pixelSizeX = 1f * textureRect.size.x / texture.width;
				float pixelSizeZ = 1f * textureRect.size.z / texture.height;

				Rect pixelTextureRect = new Rect(0, 0, texture.width, texture.height);
				Rect pixelMatrixRect = new Rect(
					(textureRect.offset.x - rect.offset.x) / pixelSizeX,
					(textureRect.offset.z - rect.offset.z) / pixelSizeZ,
					rect.size.x/pixelSizeX, 
					rect.size.z/pixelSizeZ);

				Rect pixelIntersection = CoordinatesExtensions.Intersect(pixelTextureRect, pixelMatrixRect);

				CoordRect intersect = new CoordRect(
					Mathf.CeilToInt(pixelIntersection.x),
					Mathf.CeilToInt(pixelIntersection.y),
					Mathf.FloorToInt(pixelIntersection.width),
					Mathf.FloorToInt(pixelIntersection.height) );

				Color[] colors = new Color[intersect.size.x*intersect.size.z];

				Coord min = intersect.Min; Coord max = intersect.Max;
				for (int x=min.x; x<max.x; x++)
					for (int z=min.z; z<max.z; z++)
				{
					float wx = x*pixelSizeX - textureRect.offset.x + rect.offset.x*2;
					float wz = z*pixelSizeZ - textureRect.offset.z + rect.offset.z*2;

					//float val = this[x,z]; //TODO: direct
					float val = GetInterpolated(wx, wz);
					val -= rangeMin;
					val /= rangeMax-rangeMin;

					//val = 1;

					colors[(z-min.z)*(max.x-min.x) + (x-min.x)] = new Color(val, val, val); //TODO: r should not be == r and ==b, there should be 1 byte diff
				}

				texture.SetPixels(intersect.offset.x, intersect.offset.z, intersect.size.x, intersect.size.z, colors);
				texture.Apply();
			}
Ejemplo n.º 17
0
			public void FillTexture (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();
			}
Ejemplo n.º 18
0
			public void WriteHeightmap (TerrainData data, float[,] array=null, float brushFallof=0.5f, bool delayLod=false)
			{
				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];
				}

				if (delayLod) data.SetHeightsDelayLOD(intersection.offset.x, intersection.offset.z, array);
				else data.SetHeights(intersection.offset.x, intersection.offset.z, array);
			}
Ejemplo n.º 19
0
        public static Rect Intersect(Rect r1, CoordRect r2)
        {
            Rect result = new Rect(0, 0, 0, 0);

            result.x = Mathf.Max(r1.x, r2.offset.x);
            result.y = Mathf.Max(r1.y, r2.offset.z);

            result.max = new Vector2(
                Mathf.Min(r1.max.x, r2.offset.x + r2.size.x),
                Mathf.Min(r1.max.y, r2.offset.z + r2.size.z));

            if (result.size.x < 0)
            {
                result.size = new Vector2(0, result.size.y);
            }
            if (result.size.y < 0)
            {
                result.size = new Vector2(result.size.y, 0);
            }

            return(result);
        }
Ejemplo n.º 20
0
		/*public void OverBlur (int iterations=20)
		{
			Matrix blurred = this.Clone(null);

			for (int i=1; i<=iterations; i++)
			{
				if (i==1 || i==2) blurred.Blur(step:1);
				else if (i==3) { blurred.Blur(step:1); blurred.Blur(step:1); }
				else blurred.Blur(step:i-2); //i:4, step:2

				for (int p=0; p<count; p++) 
				{
					float b = blurred.array[p] * i;
					float a = array[p];

					array[p] = a + b + a*b;
				}
			}
		}*/

		/*public void LossBlur (System.Func<float,float,float,float> blurFn=null, //prev, curr, next = output
			float intensity=0.666f, int step=1, Matrix reference=null, bool horizontal=true, bool vertical=true)
		{
			Coord min = rect.Min; Coord max = rect.Max;

			if (reference==null) reference = this;
			int lastX = max.x-1;
			int lastZ = max.z-1;

			if (horizontal)
			for (int z=min.z; z<=lastZ; z++)
			{
				float next = reference[min.x,z];
				float curr = next;
				float prev = next;

				float blurred = next;
				float lastBlurred = next;

				for (int x=min.x+step; x<=lastX; x+=step) 
				{
					//blurring
					if (blurFn==null) blurred = (prev+next)/2f;
					else blurred = blurFn(prev, curr, next);
					blurred = curr*(1-intensity) + blurred*intensity;

					//shifting values
					prev = curr; //this[x,z];
					curr = next; //this[x+step,z];
					try { next = reference[x+step*2,z]; } //this[x+step*2,z];
					catch { next = reference[lastX,z]; }

					//filling between-steps distance
					if (step==1) this[x,z] = blurred;
					else for (int i=0; i<step; i++) 
					{
						float percent = 1f * i / step;
						this[x-step+i,z] = blurred*percent + lastBlurred*(1-percent);
					}
					lastBlurred = blurred;
				}
			}

			if (vertical)
			for (int x=min.x; x<=lastX; x++)
			{
				float next = reference[x,min.z];
				float curr = next;
				float prev = next;

				float blurred = next;
				float lastBlurred = next;

				for (int z=min.z+step; z<=lastZ; z+=step) 
				{
					//blurring
					if (blurFn==null) blurred = (prev+next)/2f;
					else blurred = blurFn(prev, curr, next);
					blurred = curr*(1-intensity) + blurred*intensity;

					//shifting values
					prev = curr;
					curr = next;
					try { next = reference[x,z+step*2]; }
					catch { next = reference[x,lastZ]; }

					//filling between-steps distance
					if (step==1) this[x,z] = blurred;
					else for (int i=0; i<step; i++) 
					{
						float percent = 1f * i / step;
						this[x,z-step+i] = blurred*percent + lastBlurred*(1-percent);
					}
					lastBlurred = blurred;
				}
			}
		}*/
		#endregion

		#endregion

		#region Other

		/*public float GetOnWorldRect (Vector2 worldPos, Rect worldRect)
			{
				float relativeX = (worldPos.x - worldRect.x) / worldRect.width;
				float relativeZ = (worldPos.y - worldRect.y) / worldRect.height;
				int posX = Mathf.RoundToInt( relativeX*rect.size.x + rect.offset.x );
				int posZ = Mathf.RoundToInt( relativeZ*rect.size.z + rect.offset.z );
				posX = Mathf.Clamp(posX,rect.Min.x+1,rect.Max.x-1); posZ = Mathf.Clamp(posZ,rect.Min.z+1,rect.Max.z-1);

				return this[posX,posZ];
			}*/

			static public void BlendLayers (Matrix[] matrices, float[] opacity=null) //changes splatmaps in photoshop layered style so their summary value does not exceed 1
			{
				//finding any existing matrix
				int anyMatrixNum = -1;
				for (int i=0; i<matrices.Length; i++)
					if (matrices[i]!=null) { anyMatrixNum = i; break; }
				if (anyMatrixNum == -1) { Debug.LogError("No matrices were found to blend " + matrices.Length); return; }

				//finding rect
				CoordRect rect = matrices[anyMatrixNum].rect;

				//checking rect size
				#if WDEBUG
				for (int i=0; i<matrices.Length; i++)
					if (matrices[i]!=null && matrices[i].rect!=rect) { Debug.LogError("Matrix rect mismatch " + rect + " " + matrices[i].rect); return; }
				#endif

				int rectCount = rect.Count;
				for (int pos=0; pos<rectCount; pos++)
				{
					float sum = 0;
					for (int i=matrices.Length-1; i>=0; i--) //layer 0 is background, layer Length-1 is the top one
					{
						if (matrices[i] == null) continue;
						
						float val = matrices[i].array[pos];

						if (opacity != null) val *= opacity[i];
						
						float overly = sum + val - 1; 
						if (overly < 0) overly = 0; //faster then calling Math.Clamp
						if (overly > 1) overly = 1;

						matrices[i].array[pos] = val - overly;
						sum += val - overly;
					}
				}
			}
Ejemplo n.º 21
0
 public static CoordRect Intersect(CoordRect c1, CoordRect c2)
 {
     c1.Clamp(c2.Min, c2.Max); return(c1);
 }
Ejemplo n.º 22
0
 public bool Contains(CoordRect r)          //tested
 {
     return(r.offset.x >= offset.x && r.offset.x + r.size.x <= offset.x + size.x &&
            r.offset.z >= offset.z && r.offset.z + r.size.z <= offset.z + size.z);
 }
Ejemplo n.º 23
0
 public bool ContainsOrIntersects(CoordRect r)          //tested
 {
     return(r.offset.x > offset.x - r.size.x && r.offset.x + r.size.x < offset.x + size.x + r.size.x &&
            r.offset.z > offset.z - r.size.z && r.offset.z + r.size.z < offset.z + size.z + r.size.z);
 }
Ejemplo n.º 24
0
			public Matrix () { array = new float[0]; rect = new CoordRect(0,0,0,0); count = 0; } //for serializer