Beispiel #1
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);
        }
Beispiel #2
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;
			}
Beispiel #3
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);
        }
Beispiel #4
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;
				}
			}
Beispiel #5
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];
     }
 }
Beispiel #6
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];
     }
 }
Beispiel #7
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);
            }
        }
Beispiel #8
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;
			}
Beispiel #9
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;
			}
Beispiel #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;
			}
Beispiel #11
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);
                    }
                }
            }
        }
Beispiel #12
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;
     }
 }
Beispiel #13
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
					}
				}
Beispiel #14
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();
			}
Beispiel #15
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);
			}
Beispiel #16
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;
					}
				}
			}
Beispiel #17
0
			public Matrix () { array = new float[0]; rect = new CoordRect(0,0,0,0); count = 0; } //for serializer
Beispiel #18
0
 public static CoordRect Intersect(CoordRect c1, CoordRect c2)
 {
     c1.Clamp(c2.Min, c2.Max); return(c1);
 }