Returns pixel color at coordinates (x, y).
public static Texture2D CalculateNormalMap(Texture2D source, float strength) { Texture2D result; float xLeft, xRight; float yUp, yDown; float yDelta, xDelta; var pixels = new Color[source.width * source.height]; strength = Mathf.Clamp(strength, 0.0F, 10.0F); result = new Texture2D(source.width, source.height, TextureFormat.ARGB32, true); for (int by = 0; by < result.height; by++) { for (int bx = 0; bx < result.width; bx++) { xLeft = source.GetPixel(bx - 1, by).grayscale * strength; xRight = source.GetPixel(bx + 1, by).grayscale * strength; yUp = source.GetPixel(bx, by - 1).grayscale * strength; yDown = source.GetPixel(bx, by + 1).grayscale * strength; xDelta = ((xLeft - xRight) + 1) * 0.5f; yDelta = ((yUp - yDown) + 1) * 0.5f; pixels[bx + by * source.width] = new Color(xDelta, yDelta, 1.0f, yDelta); } } result.SetPixels(pixels); result.wrapMode = TextureWrapMode.Clamp; result.Apply(); return result; }
void OnRenderImage (RenderTexture source, RenderTexture destination){ Graphics.Blit(source,destination,mat); //mat is the material which contains the shader //we are passing the destination RenderTexture to int width = Screen.width; int height = Screen.height; Debug.Log (width.ToString ()); Debug.Log (height.ToString ()); Texture2D tex = new Texture2D(width, height); tex.ReadPixels(new Rect(0, 0, width, height), 0, 0); if (flag == true) { string[] blue = new string[width * height]; for (int i = 0; i < height; i++) { for (int j=0; j < width; j++) { blue[(i * width) + j] = tex.GetPixel (j, i).b.ToString (); } } string output = string.Join(",", blue); File.WriteAllText ("/Users/mettinger/Desktop/temp/output.txt", output); flag = false; } Debug.Log (tex.GetPixel(x,y).b.ToString()); }
public void changeSpritesColour() { texWidth = 32; texHeight = 32; newTexture = new Texture2D(texWidth, texHeight, TextureFormat.ARGB32, false); newTexture.SetPixels(spriteParent.sprite.texture.GetPixels()); cArray = newTexture.GetPixels(); int y = 0; while(y < texHeight) { int x = 0; while(x < texWidth) { if(newTexture.GetPixel(x, y) == skinColourBase) newTexture.SetPixel(x, y, skinColour); if(newTexture.GetPixel(x, y) == hairColourBase) newTexture.SetPixel(x, y, hairColour); if(newTexture.GetPixel(x, y) == eyeColourBase) newTexture.SetPixel(x, y, eyeColour); if(newTexture.GetPixel(x, y) == shoeColourBase) newTexture.SetPixel(x, y, shoeColour); if(newTexture.GetPixel(x, y) == shirtColourBase) newTexture.SetPixel(x, y, shirtColour); if(newTexture.GetPixel(x, y) == pantsColourBase) newTexture.SetPixel(x, y, pantsColour); x++; } y++; } newTexture.wrapMode = TextureWrapMode.Clamp; newTexture.filterMode = FilterMode.Point; newTexture.Apply(); spriteParent.sprite = Sprite.Create(newTexture, new Rect(0, 0, texWidth, texHeight), new Vector2(0.5f, 0.5f), 16); }
public static Texture2D GreyscaleToNormal(Texture2D greyscale) { int width = greyscale.width; int height = greyscale.height; int lenght = width * height; Color[] pixelsNormal = new Color[lenght]; for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { float currValue = greyscale.GetPixel(x, y).grayscale; float x1n = greyscale.GetPixel(x - 1, y).grayscale; float x1 = greyscale.GetPixel(x + 1, y).grayscale; float y1n = greyscale.GetPixel(x, y - 1).grayscale; float y1 = greyscale.GetPixel(x, y + 1).grayscale; float normalXValue = (currValue - x1n) + (x1 - currValue) * 10 + 0.5f; float normalYValue = (currValue - y1n) + (y1 - currValue) * 10 + 0.5f; pixelsNormal[x + y * width] = new Color(1, 1-normalYValue, 1, 1-normalXValue); } } Texture2D normalmap = new Texture2D(width, height); normalmap.SetPixels(pixelsNormal); normalmap.Apply(); return normalmap; }
public Texture2D GenerateNormalMap(Texture2D heightMap) { var dx = 0f; var dy = 0f; var strength = 0.5f; var left = 0f; var right = 0f; var up = 0f; var down = 0f; var result = new Texture2D(heightMap.width, heightMap.height, TextureFormat.ARGB32, true); for (var i = 0; i < result.height; i++) { for (var j = 0; j < result.width; j++) { left = heightMap.GetPixel(j - 1, i).grayscale * strength; right = heightMap.GetPixel(j + 1, i).grayscale * strength; up = heightMap.GetPixel(j, i - 1).grayscale * strength; down = heightMap.GetPixel(j, i + 1).grayscale * strength; dx = (left - right) * 0.5f; dy = (down - up) * 0.5f; result.SetPixel(j, i, new Color(dx, dy, 1.0f, dx)); } } result.Apply(); return result; }
Vector4 Count(Texture2D texture, Edge edge) { Vector4 result = new Vector4(); int depth = 0; int until, position, step, size; until = position = step = size = 0; MatchDirection dir = 0; switch(edge) { case Edge.Right: until = texture.height; position = texture.width; size = texture.width; step = -1; dir = MatchDirection.Horizontal; break; case Edge.Left: until = texture.height; position = 0; size = texture.width; step = 1; dir = MatchDirection.Horizontal; break; case Edge.Bottom: until = texture.width; position = 0; size = texture.height; step = 1; dir = MatchDirection.Vertical; break; case Edge.Top: until = texture.width; position = texture.height; size = texture.height; step = -1; dir = MatchDirection.Vertical; break; } for(int i = _buffer; i < until-_buffer; i++) { int counter = 0; int pos = position; Color pixel; do { if(dir == MatchDirection.Horizontal) pixel = texture.GetPixel(pos, i); else pixel = texture.GetPixel(i, pos); counter++; pos += step; } while(pixel.a == 0 && counter < size); depth = Mathf.Max(depth, counter); result += (Vector4)pixel; } if(depth < _buffer) return Vector4.zero; else return result; }
/// <summary> /// Destructive dithering of texture. /// Texture is 8888, will be written out as 8888 too /// </summary> public static void DitherTexture(Texture2D texture, TextureFormat targetTextureFormat, int x0, int y0, int w, int h) { int quantShiftR = 0, quantShiftG = 0, quantShiftB = 0, quantShiftA = 0; switch (targetTextureFormat) { case TextureFormat.ARGB4444: quantShiftR = quantShiftG = quantShiftB = quantShiftA = 4; break; case TextureFormat.RGB565: quantShiftR = 0; quantShiftB = 6; quantShiftG = 5; quantShiftA = 0; break; } int x1 = x0 + w; int y1 = y0 + h; for (int y = y0; y < y1; ++y) { for (int x = x0; x < x1; ++x) { Color oldPixel = texture.GetPixel(x, y); Color newPixel = new Color( (((int)(oldPixel.r * 255.0f + 0.5f) >> quantShiftR) << quantShiftR) / 255.0f, (((int)(oldPixel.g * 255.0f + 0.5f) >> quantShiftG) << quantShiftG) / 255.0f, (((int)(oldPixel.b * 255.0f + 0.5f) >> quantShiftB) << quantShiftB) / 255.0f, (((int)(oldPixel.a * 255.0f + 0.5f) >> quantShiftA) << quantShiftA) / 255.0f ); Color quantizationError = oldPixel - newPixel; // write out color, but "fix up" whites Color targetColor = new Color((oldPixel.r == 1.0f)?1.0f:newPixel.r, (oldPixel.g == 1.0f)?1.0f:newPixel.g, (oldPixel.b == 1.0f)?1.0f:newPixel.b, (oldPixel.a == 1.0f)?1.0f:newPixel.a); texture.SetPixel(x, y, targetColor); if (x < x1 - 1) texture.SetPixel(x + 1, y, texture.GetPixel(x + 1, y) + (quantizationError * 7.0f / 16.0f)); if (y < y1 - 1) { if (x > x0) texture.SetPixel(x - 1, y + 1, texture.GetPixel(x - 1, y + 1) + (quantizationError * 3.0f / 16.0f)); if (x < x1 - 1) texture.SetPixel(x + 1, y + 1, texture.GetPixel(x + 1, y + 1) + (quantizationError / 16.0f)); texture.SetPixel(x, y + 1, texture.GetPixel(x, y + 1) + (quantizationError * 5.0f / 16.0f)); } } } }
public void OnImageLoad(string imgPath, Texture2D tex) { Data.Instance.SetRoomFromLocalFiles(true); float currAspect = Screen.currentResolution.width * 0.8f / Screen.currentResolution.height; float texAspect = tex.width / tex.height; if (texAspect > currAspect) { Texture2D result = new Texture2D ((int)(tex.width * 1.2f), (int)(tex.height * 1.2f), tex.format, true); for (int y = 0; y < result.height; y++) { for (int x = 0; x < result.width; x++) { if (y > (result.height * 0.1f) && y < (result.height * 0.9f) && x > (result.width * 0.1f) && x < (result.width * 0.9f)) { result.SetPixel (x, y, tex.GetPixel (x - (int)(tex.width * 0.1f), y - (int)(tex.height * 0.1f))); } else { result.SetPixel (x, y, Color.black); } } } result.Apply (); Data.Instance.lastPhotoTexture = result; } else { Data.Instance.lastPhotoTexture = tex; } Data.Instance.LoadLevel("ConfirmPhoto"); }
public static void convert(Texture2D texture) { int size = texture.width * texture.height; Color[] color = new Color[size]; for(int i = 0; i < texture.height; ++i) { for(int j = 0; j < texture.width; ++j) { Color c = texture.GetPixel(j, i); int index = i * texture.width + j; color[index].r = c.r; color[index].g = c.g; color[index].b = c.b; color[index].a = c.a; } } process(color, texture.width, texture.height); for(int i = 0; i < texture.height; ++i) { for(int j = 0; j < texture.width; ++j) { int index = i * texture.width + j; float a = Mathf.Clamp01(color[index].a); float r = Mathf.Clamp01(color[index].r); float g = Mathf.Clamp01(color[index].g); float b = Mathf.Clamp01(color[index].b); texture.SetPixel(j, i, new Color(r, g, b, a)); } } texture.Apply(); }
void CreateStencil(int x, int y, Texture2D texture) { this.AonMaterial.mainTexture = tex; for (int xPix = 0; xPix < texture.width; xPix++) { for (int yPix = 0; yPix < texture.height; yPix++) { stencilUV[i] = texture.GetPixel(xPix, yPix) * texture.GetPixel(xPix, yPix).a + tex.GetPixel((x - texture.width / 2) + xPix, (y - texture.height / 2) + yPix) * (1 - texture.GetPixel(xPix, yPix).a); i++; } } //Debug.Log("x = "+ x + ", y = " + y); i = 0; tex.SetPixels(x - texture.width / 2, y - texture.height / 2, texture.width, texture.height, stencilUV); tex.Apply(); }
static int GetPixel(IntPtr L) { try { int count = LuaDLL.lua_gettop(L); if (count == 3) { UnityEngine.Texture2D obj = (UnityEngine.Texture2D)ToLua.CheckObject(L, 1, typeof(UnityEngine.Texture2D)); int arg0 = (int)LuaDLL.luaL_checknumber(L, 2); int arg1 = (int)LuaDLL.luaL_checknumber(L, 3); UnityEngine.Color o = obj.GetPixel(arg0, arg1); ToLua.Push(L, o); return(1); } else if (count == 4) { UnityEngine.Texture2D obj = (UnityEngine.Texture2D)ToLua.CheckObject(L, 1, typeof(UnityEngine.Texture2D)); int arg0 = (int)LuaDLL.luaL_checknumber(L, 2); int arg1 = (int)LuaDLL.luaL_checknumber(L, 3); int arg2 = (int)LuaDLL.luaL_checknumber(L, 4); UnityEngine.Color o = obj.GetPixel(arg0, arg1, arg2); ToLua.Push(L, o); return(1); } else { return(LuaDLL.luaL_throw(L, "invalid arguments to method: UnityEngine.Texture2D.GetPixel")); } } catch (Exception e) { return(LuaDLL.toluaL_exception(L, e)); } }
// Use this for initialization void Start() { SpriteTexture = spriterender.sprite.texture; Color32[] pix = spriterender.sprite.texture.GetPixels32(); // System.Array.Reverse(pix); SpriteTexture = new Texture2D(spriterender.sprite.texture.width, spriterender.sprite.texture.height); SpriteTexture.SetPixels32(pix); for (int x = 0; x < SpriteTexture.width; x++) { for (int y = 0; y < SpriteTexture.height; y++) { Color col = SpriteTexture.GetPixel(x,y); SpriteTexture.SetPixel(x, y, new Color(col.r,col.g, col.b, 1)); } } SpriteTexture.Apply(); Rect rec = new Rect(0,0,SpriteTexture.width,SpriteTexture.height); spriterender.sprite = Sprite.Create(SpriteTexture, rec, new Vector2(0.5f, 0.5f)); }
/// <summary> /// Scans through the pixels in the given texture and loads breakout block /// prefabs for each one. Returns a GameObject containing all these blocks, /// appropriately scaled and positioned in the center of the screen. /// </summary> public GameObject Load(Texture2D levelTex) { if (levelTex == null) throw new ArgumentNullException("level", "Level texture cannot be null."); GameObject levelContainer = new GameObject("LevelContainer"); for (int x = 0; x < levelTex.width; x++) { for (int y = 0; y < levelTex.height; y++) { Color color = levelTex.GetPixel(x, y); if (color.a == 0) continue; //ignore transparent pixels var block = Instantiate(BlockPrefab, new Vector2(x, y), Quaternion.identity) as GameObject; block.GetComponent<SpriteRenderer>().color = color; block.transform.parent = levelContainer.transform; BlockCount++; } } //scale the level to be SIZE world units large const float SIZE = 5.0f; float scaledWidth = SIZE / levelTex.width; float scaledHeight = SIZE / levelTex.height; levelContainer.transform.localScale = new Vector2(scaledWidth, scaledHeight); //center the level levelContainer.transform.position = new Vector2(-(scaledWidth * (levelTex.width - 1)) / 2, -(scaledHeight * (levelTex.height - 1)) / 2); return levelContainer; }
private static Texture2D Multiply(Texture2D lowerLayer, float lowerOpacity, Texture2D upperLayer, float upperOpacity) { Texture2D result = new Texture2D (lowerLayer.width, lowerLayer.height); result.filterMode = FilterMode.Point; for (int x = 0; x < lowerLayer.width; x++) { for (int y = 0; y < lowerLayer.height; y++) { Color lower = lowerLayer.GetPixel (x, y); Color upper = upperLayer.GetPixel (x, y); float srca = upper.a * upperOpacity; float lowa = lower.a * lowerOpacity; Color c = Color.clear; if (srca == 0 && lowa != 0) { c = new Color(lower.r, lower.g, lower.b, lowa); } else if (lowa == 0 && srca != 0){ c = new Color(upper.r, upper.g, upper.b, srca); } else if (lowa != 0 && srca != 0) { float outa = srca + lowa * (1 - srca); c = new Color ((upper.r * upper.a * lower.r * lower.a) / outa, (upper.g * upper.a * lower.g * lower.a) / outa, (upper.b * upper.a * lower.b * lower.a) / outa, outa); } result.SetPixel (x, y, c); } } result.Apply (); return result; }
static public int GetPixel(IntPtr l) { try { #if DEBUG var method = System.Reflection.MethodBase.GetCurrentMethod(); string methodName = GetMethodName(method); #if UNITY_5_5_OR_NEWER UnityEngine.Profiling.Profiler.BeginSample(methodName); #else Profiler.BeginSample(methodName); #endif #endif UnityEngine.Texture2D self = (UnityEngine.Texture2D)checkSelf(l); System.Int32 a1; checkType(l, 2, out a1); System.Int32 a2; checkType(l, 3, out a2); var ret = self.GetPixel(a1, a2); pushValue(l, true); pushValue(l, ret); return(2); } catch (Exception e) { return(error(l, e)); } #if DEBUG finally { #if UNITY_5_5_OR_NEWER UnityEngine.Profiling.Profiler.EndSample(); #else Profiler.EndSample(); #endif } #endif }
public void OnRenderImage(RenderTexture source, RenderTexture destination) { CheckResources(); originalTex = new Texture2D(source.width, source.height); // change texture. every white pixel should be transparent after this int y = 0; while (y < originalTex.height) { int x = 0; while (x < originalTex.width) { Color colorAtPixel = new Color(); colorAtPixel = originalTex.GetPixel(x, y); if (colorAtPixel[0] == 1 && colorAtPixel[1] == 1 && colorAtPixel[2] == 1 && colorAtPixel[3] == 1) { originalTex.SetPixel(x, y, Color.clear); } else { originalTex.SetPixel(x, y, new Color(colorAtPixel[0], colorAtPixel[1], colorAtPixel[2], colorAtPixel[3] )); } ++x; } ++y; } originalTex.Apply(); transparentMaterial.SetTexture("_MainTex", originalTex); Graphics.Blit(source, destination, transparentMaterial, 0); }
public static Texture2D pick(int[,] pPatternMark,int pPickPatternID, Texture2D pSource, zzPointBounds pBounds, zzPoint pOutSize) { Texture2D lOut = new Texture2D(pOutSize.x, pOutSize.y, TextureFormat.ARGB32, false); var lMin = pBounds.min; var lMax = pBounds.max; var lDrawOffset = -lMin; for (int lY = lMin.y; lY < lMax.y; ++lY) { var lDrawedPointY = lY + lDrawOffset.y; for (int lX = lMin.x; lX < lMax.x; ++lX) { var lColor = pPatternMark[lX, lY] == pPickPatternID ? pSource.GetPixel(lX, lY) : Color.clear; lOut.SetPixel(lX + lDrawOffset.x, lDrawedPointY, lColor); } for (int i = lMax.x+lDrawOffset.x; i < lOut.width; ++i) { lOut.SetPixel(i, lDrawedPointY, Color.clear); } } for (int lY = lMax.y + lDrawOffset.y; lY < lOut.height; ++lY) { for (int lX = 0; lX < lOut.width; ++lX) lOut.SetPixel(lX, lY, Color.clear); } lOut.Apply(); return lOut; }
// ------------------------------------------------------------------------------------------- void JustNoise(Texture2D texture) { const int brickSize = 10; for (int x = 0; x < m_textureSize; x+=brickSize) { for (int y = 0; y < m_textureSize; y+=brickSize) { float p = m_noise.BrownianValue (new Vector2 ((float)x / (float)m_textureSize, (float)y / (float)m_textureSize), new Vector2 (7, 30)); if (p > 0.46) { for (int bx = 0; bx < brickSize; bx++) { for (int by = 0; by < brickSize; by++) { Color c = texture.GetPixel (x+bx, y+by); c.g = 1.0f; texture.SetPixel (x+bx, y+by, c); } } } } } }
public static Texture2D ProgressUpdate(Texture2D tex, float progress, Color overlayColor) { progress = 1-progress; Texture2D thisTex = new Texture2D(tex.width, tex.height); Vector2 centre = new Vector2(Mathf.Ceil(thisTex.width/2), Mathf.Ceil(thisTex.height/2)); //find the centre pixel for(int y = 0; y < thisTex.height; y++){ for(int x = 0; x < thisTex.width; x++){ float angle = Mathf.Atan2(x-centre.x, y-centre.y)*Mathf.Rad2Deg*-1; //find the angle between the centre and this pixel (between -180 and 180) if(angle < 0){ angle += 360; //change angles to go from 0 to 360 } Color pixColor = tex.GetPixel(x, y); if(angle <= progress*360.0){ //if the angle is less than the progress angle blend the overlay colour pixColor = new Color( (pixColor.r*pixColor.a*(1-overlayColor.a))+(overlayColor.r*overlayColor.a), (pixColor.g*pixColor.a*(1-overlayColor.a))+(overlayColor.g*overlayColor.a), (pixColor.b*pixColor.a*(1-overlayColor.a))+(overlayColor.b*overlayColor.a) ); thisTex.SetPixel(x, y, pixColor); }else{ thisTex.SetPixel(x, y, pixColor); } } } thisTex.Apply(); //apply the cahnges we made to the texture return thisTex; }
private Texture2D DownSample(Texture2D src) { int width = 1024, height = 512; Texture2D dst = new Texture2D(width, height, TextureFormat.ARGB32, false); Color[] cs = new Color[width * height]; for (int x = 0; x < width; ++x) for (int y = 0; y < height; ++y) { int dstIndex = x + y * width; Color dstColor = Color.black; for (int subX = 0; subX < 4; ++subX) for (int subY = 0; subY < 4; ++subY) { Color srcColor = src.GetPixel(x * 4 + subX, y * 4 + subY); dstColor += srcColor; } dstColor /= 16.0f; dstColor.a = 1.0f; cs[dstIndex] = dstColor; } dst.SetPixels(cs); dst.Apply(); return dst; }
public static Texture2D CropSquare(Texture2D texture) { bool landscape = texture.width > texture.height; int size = landscape ? texture.height : texture.width; Texture2D newTexture = new Texture2D (size, size); int minY = 0; int minX = 0; if (landscape) { minX = (texture.width / 2) - (size / 2); } else { minY = (texture.height / 2) - (size / 2); } int newY = 0; int newX = 0; for (int y = minY; y < (minY+size); y++) { newX = 0; for (int x = minX; x < (minX+size); x++) { newTexture.SetPixel(newX, newY, texture.GetPixel(x,y)); newX++; } newY++; } newTexture.Apply (); return newTexture; }
// Probably a terrible idea, but here we do a one-time scan through the map // and turn all of the pixel's grayscale values into a 2d array public void convertImageTo2DArray(Texture2D mapImage){ Debug.Log ("Converting image to array"); for (int i = 0; i < mapDimension; i++) for (int j = 0; j < mapDimension; j++) mapAsArray[i, j] = mapImage.GetPixel(i, j).grayscale; }
public static Color[] BuildPaletteFromTexture(Texture2D paletteTexture) { Color[] result=new Color[paletteTexture.width]; for (int i = 0; i < paletteTexture.width; i++) { result[i]=paletteTexture.GetPixel(i,0); } return result; }
public void initiate(PuzzlePieceInfo piece, Texture2D outLine, Texture2D theImage) { if(!initiated) { //set object's texture to puzzle-piece shaped highlight Color mainImageColor; //Set Up myPuzzlePiece = new PuzzlePieceInfo(); puzzleOutline = outLine; mainImageTexture = theImage; myPuzzlePiece = piece; // Debug.Log(pictureTexture.width); // Debug.Log(pictureTexture.height); // // Debug.Log("w "+outLine.width); // Debug.Log("h "+outLine.height); //Where does the texture start.. int initX = myPuzzlePiece.uvX; int initY = myPuzzlePiece.uvY; //How much of the texture will be shown (uv rect) float textureWidth = myPuzzlePiece.uvWidth; float textureHeight = myPuzzlePiece.uvHeight; myColor = myPuzzlePiece.myColor; //New "Canvas" to paint on myTexture = new Texture2D((int)textureWidth, (int)textureHeight); //Clears any other color that it is not its own for(int y = initY; y < initY + textureHeight; y++){ for(int x = initX; x < initX + textureWidth; x++){ //if pixel is not the piece's color, clear it if(puzzleOutline.GetPixel(x,y) != myColor) { myTexture.SetPixel(x-initX,y-initY,Color.clear); } else{ mainImageColor = mainImageTexture.GetPixel(x,y); myTexture.SetPixel(x-initX, y-initY, mainImageColor); } } } //Commit changes to texture and apply it myTexture.wrapMode = TextureWrapMode.Clamp; myTexture.Apply(); myUITexture.mainTexture = myTexture; myUITexture.enabled = false; initiated =true; } myUITexture.mainTexture = myTexture; myUITexture.enabled = false; }
public static Texture2D GetClosestSplatTexture(TerrainMeshBlend comp, Texture2D splatAlphaTexture, out Texture2D normalMap) { normalMap = null; Vector3 terrainLocalPos = comp.Terrain.transform.TransformPoint(comp.transform.position); float blendValue = 0f; int splatIndex = -1; int xCoord = (int)Mathf.Clamp((terrainLocalPos.x / comp.Terrain.terrainData.size.x) * splatAlphaTexture.width, 0, splatAlphaTexture.width); int yCoord = (int)Mathf.Clamp((terrainLocalPos.z / comp.Terrain.terrainData.size.y) * splatAlphaTexture.height, 0, splatAlphaTexture.height); Color color = splatAlphaTexture.GetPixel(xCoord, yCoord); if (color.r > blendValue) { splatIndex = 0; blendValue = color.r; } if (color.g > blendValue) { splatIndex = 1; blendValue = color.g; } if (color.b > blendValue) { splatIndex = 2; blendValue = color.b; } if (color.a > blendValue) { splatIndex = 3; blendValue = color.a; } if (splatIndex != -1) { TerrainSettings settings = comp.Terrain.GetComponent<TerrainSettings>(); if (settings != null) { switch (splatIndex) { case 0: normalMap = settings.Bump0; break; case 1: normalMap = settings.Bump1; break; case 2: normalMap = settings.Bump2; break; case 3: normalMap = settings.Bump3; break; } } return comp.Terrain.terrainData.splatPrototypes[splatIndex].texture; } return null; }
// Use this for initialization void Start() { Texture2D tx = new Texture2D(100,100); tx.name = "TEXTURE"; Rect R = new Rect(0,0,100,100); UnityEngine.Color[] c = new UnityEngine.Color[100*100]; for (int i = 0; i < c.Length; i++) { switch(i % 5) { case 1: c[i] = new UnityEngine.Color(255,0,0,1); break; case 2: c[i] = new UnityEngine.Color(0,255,0,1); break; case 3: c[i] = new UnityEngine.Color(0,0,255,0.8f); break; case 4: c[i] = new UnityEngine.Color(120, 10, 37, 0.5f); break; default: c[i] = new UnityEngine.Color(100,231,120,0); break; } } Sprite sp = Sprite.Create(tx, R, new Vector2(0,0)); tx.SetPixels(c); disp (tx.GetPixel(1,5), tx.GetPixel (90,1)); var pl = GameObject.CreatePrimitive(PrimitiveType.Plane); pl.transform.position = new Vector3(0, 30, 0); pl.transform.rotation = Quaternion.Euler(180,0,0); disp(pl); Renderer r = GetComponent<Renderer>(); disp (r.material); disp (r.material.name); disp (r.material.mainTexture.height, tx); r.material.mainTexture = tx; //Instantiate(pl, new Vector3(0,30,0), Quaternion.Euler(new Vector3(180,0,0))); }
//Recolors a texture with the main and secondary colors given in Inspector Texture2D ChangeTexture(Texture2D theTexture) { //Creates new texture to override old one Texture2D newTexture = new Texture2D (theTexture.width, theTexture.height); newTexture.filterMode = FilterMode.Point; newTexture.wrapMode = TextureWrapMode.Clamp; int x = 0; //Iterating through pixel by pixel while (x < newTexture.width) { int y = 0; while (y < newTexture.height) { //Debug.Log(theTexture.GetPixel(x,y)); if (theTexture.GetPixel (x, y) == new Color32 (255, 0, 255, 255)) { //Inner color newTexture.SetPixel (x, y, MainColor); } else if (theTexture.GetPixel (x, y) == new Color32 (200, 0, 200, 255)) { //Outer Color newTexture.SetPixel (x, y,SecColor); } else { //Non Changed color newTexture.SetPixel(x, y, theTexture.GetPixel(x,y)); } y++; } x++; } //Finalize Texture newTexture.Apply (); //Creates new Sprite with the texture, this is currently useless due to how Animator works //SelectedColor.GetComponent<SpriteRenderer> ().sprite = Sprite.Create (newTexture, SelectedColor.GetComponent<SpriteRenderer> ().sprite.rect, new Vector2(.5f,.5f)); //SelectedColor.GetComponent<SpriteRenderer> ().material.mainTexture = newTexture; return newTexture; }
// Credit goes to Kragrathea. public static Texture2D BumpToNormalMap(Texture2D source, float strength) { strength = Mathf.Clamp(strength, 0.0F, 10.0F); var result = new Texture2D(source.width, source.height, TextureFormat.ARGB32, true); for (int by = 0; by < result.height; by++) { for (var bx = 0; bx < result.width; bx++) { var xLeft = source.GetPixel(bx - 1, by).grayscale * strength; var xRight = source.GetPixel(bx + 1, by).grayscale * strength; var yUp = source.GetPixel(bx, by - 1).grayscale * strength; var yDown = source.GetPixel(bx, by + 1).grayscale * strength; var xDelta = ((xLeft - xRight) + 1) * 0.5f; var yDelta = ((yUp - yDown) + 1) * 0.5f; result.SetPixel(bx, by, new Color(xDelta, yDelta, 1.0f, xDelta)); } } result.Apply(); return result; }
private void SetHeightFromImage(Texture2D image) { _heights = new float[image.height, image.width]; for (var i = 0; i < image.height; i++) { for (var j = 0; j < image.width; j++) { _heights[i, j] = image.GetPixel(i, j).grayscale; } } }
IEnumerator ConvertToImage() { //grab the main camera and mess with it for rendering the object - make sure orthographic Camera cam = Camera.main; cam.orthographic = true; //render to screen rect area equal to out image size float rw = imageWidth; rw /= Screen.width; float rh = imageHeight; rh /= Screen.height; cam.rect = new Rect(0,0,rw,rh); //grab size of object to render - place/size camera to fit Bounds bb = objectToRender.GetComponent<Renderer>().bounds; //place camera looking at centre of object - and backwards down the z-axis from it cam.transform.position = bb.center; cam.transform.position.Set(cam.transform.position.x, cam.transform.position.y, -1.0f + (bb.min.z * 2.0f)); //make clip planes fairly optimal and enclose whole mesh cam.nearClipPlane = 0.5f; cam.farClipPlane = -cam.transform.position.z + 10.0f + bb.max.z; //set camera size to just cover entire mesh cam.orthographicSize = 1.01f * Mathf.Max( (bb.max.y - bb.min.y)/2.0f, (bb.max.x - bb.min.x)/2.0f); cam.transform.position.Set(cam.transform.position.x, cam.orthographicSize * 0.05f, cam.transform.position.y); //render yield return new WaitForEndOfFrame(); var tex = new Texture2D( imageWidth, imageHeight, TextureFormat.ARGB32, false ); // Read screen contents into the texture tex.ReadPixels(new Rect(0, 0, imageWidth, imageHeight), 0, 0 ); tex.Apply(); //turn all pixels == background-color to transparent Color bCol = cam.backgroundColor; Color alpha = bCol; alpha.a = 0.0f; for(int y = 0; y < imageHeight; y++) { for(int x = 0; x < imageWidth; x++) { Color c = tex.GetPixel(x,y); if (c.r == bCol.r) tex.SetPixel(x,y,alpha); } } tex.Apply(); // Encode texture into PNG byte[] bytes = tex.EncodeToPNG(); Destroy( tex ); System.IO.File.WriteAllBytes(Application.dataPath + "/../billboard.png", bytes); }
public bool IsGroundColorWhite(Vector3 woldpos) { Vector2 sccenPos = mainCam.WorldToScreenPoint(woldpos); Texture2D tex = new Texture2D(1, 1, TextureFormat.RGB24, true); tex.ReadPixels(new Rect(sccenPos.x, sccenPos.y, tex.width, tex.height), 0, 0, false); //Debug.DrawLine(woldpos, woldpos + Vector3.up, tex.GetPixel(0, 0), 0.1f); if (tex.GetPixel(0, 0).r > 0.5f) return true; return false; }
static void PadTexture(Texture2D tex, int pad, bool stretchPad) { Color bgColor = new Color(0,0,0,0); for (int y = 0; y < pad; ++y) { for (int x = 0; x < tex.width; ++x) { tex.SetPixel(x, y, stretchPad?tex.GetPixel(x, pad):bgColor); tex.SetPixel(x, tex.height - 1 - y, stretchPad?tex.GetPixel(x, tex.height - 1 - pad):bgColor); } } for (int x = 0; x < pad; ++x) { for (int y = 0; y < tex.height; ++y) { tex.SetPixel(x, y, stretchPad?tex.GetPixel(pad, y):bgColor); tex.SetPixel(tex.width - 1 - x, y, stretchPad?tex.GetPixel(tex.width - 1 - pad, y):bgColor); } } }
public override System.Drawing.Color PixelColor(Texture texture, uint x, uint y, System.Drawing.Color defaultColor) { int x1 = (int)x; int y1 = (int)y; UnityEngine.Texture2D tex = texture.RendererData as UnityEngine.Texture2D; if (tex == null) { return(defaultColor); } UnityEngine.Color clr = tex.GetPixel(x1, tex.height - y1); return(new Color((byte)(clr.a * 255), (byte)(clr.r * 255), (byte)(clr.g * 255), (byte)(clr.b * 255))); }
//////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////// /// <summary> /// Blur a Texture2D /// </summary> /// <param name="p_Image">Source</param> /// <param name="p_BlurSize">Blur size</param> /// <returns></returns> public static UnityEngine.Texture2D Blur(UnityEngine.Texture2D p_Image, int p_BlurSize) { UnityEngine.Texture2D l_Blurred = new UnityEngine.Texture2D(p_Image.width, p_Image.height); /// Look at every pixel in the blur rectangle for (int l_XX = 0; l_XX < p_Image.width; l_XX++) { for (int l_YY = 0; l_YY < p_Image.height; l_YY++) { float l_AvgR = 0, l_AvgG = 0, l_AvgB = 0, l_AvgA = 0; int l_BlurPixelCount = 0; /// Average the color of the red, green and blue for each pixel in the /// blur size while making sure you don't go outside the image bounds for (int l_X = l_XX; (l_X < l_XX + p_BlurSize && l_X < p_Image.width); l_X++) { for (int l_Y = l_YY; (l_Y < l_YY + p_BlurSize && l_Y < p_Image.height); l_Y++) { Color l_Pixel = p_Image.GetPixel(l_X, l_Y); l_AvgR += l_Pixel.r; l_AvgG += l_Pixel.g; l_AvgB += l_Pixel.b; l_AvgA += l_Pixel.a; l_BlurPixelCount++; } } l_AvgR /= l_BlurPixelCount; l_AvgG /= l_BlurPixelCount; l_AvgB /= l_BlurPixelCount; l_AvgA /= l_BlurPixelCount; /// Now that we know the average for the blur size, set each pixel to that color for (int l_X = l_XX; l_X < l_XX + p_BlurSize && l_X < p_Image.width; l_X++) { for (int l_Y = l_YY; l_Y < l_YY + p_BlurSize && l_Y < p_Image.height; l_Y++) { l_Blurred.SetPixel(l_X, l_Y, new Color(l_AvgR, l_AvgG, l_AvgB, l_AvgA)); } } } } l_Blurred.Apply(); return(l_Blurred); }
static public int GetPixel(IntPtr l) { try { UnityEngine.Texture2D self = (UnityEngine.Texture2D)checkSelf(l); System.Int32 a1; checkType(l, 2, out a1); System.Int32 a2; checkType(l, 3, out a2); var ret = self.GetPixel(a1, a2); pushValue(l, ret); return(1); } catch (Exception e) { return(error(l, e)); } }
static int QPYX_GetPixel_YXQP(IntPtr L_YXQP) { try { ToLua.CheckArgsCount(L_YXQP, 3); UnityEngine.Texture2D QPYX_obj_YXQP = (UnityEngine.Texture2D)ToLua.CheckObject(L_YXQP, 1, typeof(UnityEngine.Texture2D)); int QPYX_arg0_YXQP = (int)LuaDLL.luaL_checknumber(L_YXQP, 2); int QPYX_arg1_YXQP = (int)LuaDLL.luaL_checknumber(L_YXQP, 3); UnityEngine.Color QPYX_o_YXQP = QPYX_obj_YXQP.GetPixel(QPYX_arg0_YXQP, QPYX_arg1_YXQP); ToLua.Push(L_YXQP, QPYX_o_YXQP); return(1); } catch (Exception e_YXQP) { return(LuaDLL.toluaL_exception(L_YXQP, e_YXQP)); } }
public bool LoadSharedObject() { var sectorName = GetSectorName(this); var colorPath = Path.Combine(Application.persistentDataPath, "MiniMap/Colors_" + sectorName + ".png"); var waypointPath = Path.Combine(Application.persistentDataPath, "MiniMap/Waypoints_" + sectorName + ".png"); if (!File.Exists(colorPath) || !File.Exists(waypointPath)) { return(false); } var colorBytes = File.ReadAllBytes(colorPath); var waypointBytes = File.ReadAllBytes(waypointPath); if (colorBytes == null || waypointBytes == null) { return(false); } if (!Texture2D.LoadImage(colorBytes)) { return(false); } if (!m_WaypointsTexture2D.LoadImage(waypointBytes)) { Texture2D.SetPixels(Enumerable.Repeat(Color.black, Constants.MiniMapSectorSize * Constants.MiniMapSectorSize).ToArray()); Texture2D.Apply(); UncommittedPixelChanges = false; return(false); } for (int x = 0; x < Constants.MiniMapSectorSize; x++) { for (int y = 0; y < Constants.MiniMapSectorSize; y++) { var cost = Colors.EightBitFromColor(m_WaypointsTexture2D.GetPixel(x, y)); MinCost = Mathf.Min(MinCost, cost); m_Cost[(Constants.MiniMapSectorSize - y - 1) * Constants.MiniMapSectorSize + x] = cost; } } Texture2D.Apply(); UncommittedPixelChanges = false; return(true); }
static int GetPixel(IntPtr L) { try { ToLua.CheckArgsCount(L, 3); UnityEngine.Texture2D obj = (UnityEngine.Texture2D)ToLua.CheckObject(L, 1, typeof(UnityEngine.Texture2D)); int arg0 = (int)LuaDLL.luaL_checknumber(L, 2); int arg1 = (int)LuaDLL.luaL_checknumber(L, 3); UnityEngine.Color o = obj.GetPixel(arg0, arg1); ToLua.Push(L, o); return(1); } catch (Exception e) { return(LuaDLL.toluaL_exception(L, e)); } }
static public int GetPixel(IntPtr l) { try{ UnityEngine.Texture2D self = (UnityEngine.Texture2D)checkSelf(l); System.Int32 a1; checkType(l, 2, out a1); System.Int32 a2; checkType(l, 3, out a2); UnityEngine.Color ret = self.GetPixel(a1, a2); pushValue(l, ret); return(1); } catch (Exception e) { LuaDLL.luaL_error(l, e.ToString()); return(0); } }
public static int GetPixel_wrap(long L) { try { long nThisPtr = FCLibHelper.fc_get_inport_obj_ptr(L); UnityEngine.Texture2D obj = get_obj(nThisPtr); int arg0 = FCLibHelper.fc_get_int(L, 0); int arg1 = FCLibHelper.fc_get_int(L, 1); Color ret = obj.GetPixel(arg0, arg1); long ret_ptr = FCLibHelper.fc_get_return_ptr(L); Color temp_ret = ret; FCLibHelper.fc_set_value_color(ret_ptr, ref temp_ret); } catch (Exception e) { Debug.LogException(e); } return(0); }
static int GetPixel(IntPtr L) { #if UNITY_EDITOR ToluaProfiler.AddCallRecord("UnityEngine.Texture2D.GetPixel"); #endif try { ToLua.CheckArgsCount(L, 3); UnityEngine.Texture2D obj = (UnityEngine.Texture2D)ToLua.CheckObject(L, 1, typeof(UnityEngine.Texture2D)); int arg0 = (int)LuaDLL.luaL_checknumber(L, 2); int arg1 = (int)LuaDLL.luaL_checknumber(L, 3); UnityEngine.Color o = obj.GetPixel(arg0, arg1); ToLua.Push(L, o); return(1); } catch (Exception e) { return(LuaDLL.toluaL_exception(L, e)); } }
public static void RaycastForColor(Vector3 position, float radius, Vector3 direction, LayerMask mask, out List <UnityEngine.Color> colors) { colors = new List <UnityEngine.Color>(); RaycastHit hit; Ray ray = new Ray(position + direction * radius, -direction); bool isHitting = Physics.Raycast(ray, out hit, 2 * radius, mask); if (isHitting) { UnityEngine.Texture2D text = hit.collider.gameObject.GetComponent <Renderer>().material.mainTexture as UnityEngine.Texture2D; if (text != null) { int x = (int)(hit.textureCoord.x * text.width); int y = (int)(hit.textureCoord.y * text.height); UnityEngine.Color color = text.GetPixel(x, y); colors.Add(color); } } Debug.DrawRay(ray.origin, ray.direction * radius, isHitting ? UnityEngine.Color.green : UnityEngine.Color.red, 0.1f); }
public Color GetPixel(int x, int y) { return(texture.GetPixel(x, texture.height - y - 1).ToColor()); }