public static GUIStyle[] getPlayerSprites(Texture2D playersTexture) { GUIStyle[] styles = new GUIStyle[4]; styles[0] = new GUIStyle (); Color[] button1Colors = playersTexture.GetPixels (0, 0, IMAGE_HEIGHT, IMAGE_HEIGHT); Texture2D button1Texture = new Texture2D(IMAGE_HEIGHT, IMAGE_HEIGHT); button1Texture.SetPixels(button1Colors); button1Texture.Apply (); styles[0].normal.background = button1Texture; styles[1] = new GUIStyle (); Color[] button2Colors = playersTexture.GetPixels (320, 0, IMAGE_HEIGHT, IMAGE_HEIGHT); Texture2D button2Texture = new Texture2D(IMAGE_HEIGHT, IMAGE_HEIGHT); button2Texture.SetPixels(button2Colors); button2Texture.Apply (); styles[1].normal.background = button2Texture; styles[2] = new GUIStyle (); Color[] button3Colors = playersTexture.GetPixels (640, 0, IMAGE_HEIGHT, IMAGE_HEIGHT); Texture2D button3Texture = new Texture2D(IMAGE_HEIGHT, IMAGE_HEIGHT); button3Texture.SetPixels(button3Colors); button3Texture.Apply (); styles[2].normal.background = button3Texture; styles[3] = new GUIStyle (); Color[] button4Colors = playersTexture.GetPixels (960, 0, IMAGE_HEIGHT, IMAGE_HEIGHT); Texture2D button4Texture = new Texture2D(IMAGE_HEIGHT, IMAGE_HEIGHT); button4Texture.SetPixels(button4Colors); button4Texture.Apply (); styles[3].normal.background = button4Texture; return styles; }
void Start() { Renderer rend = GetComponent<Renderer>(); // duplicate the original texture and assign to the material tex = Instantiate(rend.material.mainTexture) as Texture2D; tex.Resize (200, 100); rend.material.mainTexture = tex; // colors used to tint the first 3 mip levels Color[] colors = new Color[3]; colors[0] = Color.red; colors[1] = Color.green; colors[2] = Color.blue; mipCount = Mathf.Min(3, tex.mipmapCount); // tint each mip level for( var mip = 0; mip < mipCount; ++mip ) { var cols = tex.GetPixels( mip ); for( var i = 0; i < cols.Length; ++i ) { cols[i] = new Color(Random.value,Random.value,Random.value); } tex.SetPixels( cols, mip ); } // actually apply all SetPixels, don't recalculate mip levels tex.Apply(false); int length = tex.GetPixels ().Length; wat = new float[length]; for (int i =0; i< wat.Length; i++) wat [i] = Random.value; Debug.Log ("n minmaps: " + mipCount); //for(int i =0; i< wa }
public static void FlattenSSBump(Texture2D Normal, bool flipXZ) { if (!flipXZ) { Texture2D SSBumpTex = new Texture2D(Normal.width, Normal.height, TextureFormat.RGB24, true); Color[] SSBumpColor; Color[] NormalColor; Vector4[] Basis = new Vector4[3]; Basis[0] = new Vector4(0.816496580927726f, 0.0f, 0.5773502691896258f, 0.0f); Basis[1] = new Vector4(-0.408248290463863f, 0.7071067811865475f, 0.5773502691896258f, 0.0f); Basis[2] = new Vector4(-0.408248290463863f, -0.7071067811865475f, 0.5773502691896258f, 0.0f); //Make our Normal Texture readible temporarily TextureImporter tempRNMImport = TextureImporter.GetAtPath(AssetDatabase.GetAssetPath(Normal.GetInstanceID())) as TextureImporter; tempRNMImport.textureType = TextureImporterType.Image; tempRNMImport.isReadable = true; AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(Normal.GetInstanceID()), ImportAssetOptions.ForceUpdate); SSBumpColor = Normal.GetPixels(); NormalColor = Normal.GetPixels(); for (int i = 0; i < SSBumpColor.Length; i++) { SSBumpColor[i].r = (DotBasis(UnpackNormal(NormalColor[i]), Basis[0]) + 1) / 2; SSBumpColor[i].g = (DotBasis(UnpackNormal(NormalColor[i]), Basis[1]) + 1) / 2; SSBumpColor[i].b = (DotBasis(UnpackNormal(NormalColor[i]), Basis[2]) + 1) / 2; } SSBumpTex.SetPixels(SSBumpColor); GzCRNMMergeUtil.TextureImportHelper(SSBumpTex, AssetDatabase.GetAssetPath(Normal.GetInstanceID()) + "_SSBump.png"); tempRNMImport.textureType = TextureImporterType.Bump; tempRNMImport.isReadable = false; AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(Normal.GetInstanceID()), ImportAssetOptions.ForceUpdate); } else { Color[] SSBumpColor; TextureImporter tempRNMImport = TextureImporter.GetAtPath(AssetDatabase.GetAssetPath(Normal.GetInstanceID())) as TextureImporter; tempRNMImport.isReadable = true; tempRNMImport.textureFormat = TextureImporterFormat.ARGB32; AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(Normal.GetInstanceID()), ImportAssetOptions.ForceUpdate); SSBumpColor = Normal.GetPixels(); for (int i = 0; i < SSBumpColor.Length; i++) { SSBumpColor[i].a = SSBumpColor[i].b; SSBumpColor[i].b = SSBumpColor[i].g; SSBumpColor[i].g = SSBumpColor[i].a; SSBumpColor[i].a = 0.0f; } Normal.SetPixels(SSBumpColor); GzCRNMMergeUtil.TextureImportHelper(Normal, AssetDatabase.GetAssetPath(Normal.GetInstanceID()) + "_ConvertedSSBump.png"); tempRNMImport.isReadable = false; AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(Normal.GetInstanceID()), ImportAssetOptions.ForceUpdate); } }
protected void GenerateLevelFromBitmap(Texture2D bitmap) { Color[] pixels = bitmap.GetPixels(); int width = bitmap.width; int height = bitmap.height; NodeGraph = new Graph(this.transform.position,width,height); for (int i = 0; i < width; i++) { for (int j = 0; j < height; j++) { GameObject o = null; int idx = FindBlockIndex(pixels[i*width+j]); if (idx >= 0) { o = (GameObject)Instantiate(blocks[idx].LevelTile.gameObject,new Vector3(i,0,j),Quaternion.identity); } else { o = (GameObject)Instantiate(floorTile,new Vector3(i,0,j),Quaternion.identity); } o.transform.parent = m_levelParent.transform; Tile t = o.GetComponent<Tile>(); NodeGraph.AddNode(new Vector2(i,j),t); } } NodeGraph.SetEightWayNeighbors(width,height); m_ready = true; }
private static Texture2D CreatePreviewTexture(Texture2D page) { if (page != null) { Texture2D preview = new Texture2D(page.width, page.height, TextureFormat.ARGB32, true); preview.filterMode = FilterMode.Bilinear; Color[] contents = page.GetPixels(); var magenta = Color.magenta; var s = contents.Length; for (var i = 0; i < s; ++i) { var newCol = contents[i]; var sa = newCol.a; var da = 1 - sa; contents[i].r = newCol.r * sa + magenta.r * da; contents[i].g = newCol.g * sa + magenta.g * da; contents[i].b = newCol.b * sa + magenta.b * da; contents[i].a = 1; } preview.SetPixels(contents); preview.Apply(true, true); return preview; } else { return null; } }
public static Texture2D BlendMono(Texture2D a, Texture2D b, int mode) { IntPairClass resA = new IntPairClass(a); IntPairClass resB = new IntPairClass(b); IntPairClass res = new IntPairClass(); res.SetFromMinimum(resA, resB); Color[] originalColorsA = a.GetPixels(0, 0, res.value01, res.value02); Color[] originalColorsB = b.GetPixels(0, 0, res.value01, res.value02); Color[] resultColors = new Color[originalColorsA.Length]; for (int i = 0; i < resultColors.Length; i++) { float aValue = originalColorsA[i].a; float bValue = originalColorsB[i].a; Color r = new Color(); if (mode == 0 || mode == 1) { r = new Color(1f, 1f, 1f, aValue); } else if(mode == 2) { r = new Color(1f, 1f, 1f, aValue * bValue); } resultColors[i] = r; } Texture2D toReturn = new Texture2D(a.width, a.height, TextureFormat.Alpha8, true); toReturn.SetPixels(0, 0, a.width, a.height, resultColors); toReturn.Apply(true); return toReturn; }
//0 - A over B, 1 - B over A, 2 - Multiply public static Texture2D Blend(Texture2D a, Texture2D b, int mode) { IntPairClass resA = new IntPairClass(a); IntPairClass resB = new IntPairClass(b); IntPairClass res = new IntPairClass(); res.SetFromMinimum(resA, resB); Color[] originalColorsA = a.GetPixels(0, 0, res.value01, res.value02); Color[] originalColorsB = b.GetPixels(0, 0, res.value01, res.value02); Color[] resultColors = new Color[originalColorsA.Length]; for(int i = 0; i < resultColors.Length; i++) { Color aColor = originalColorsA[i]; Color bColor = originalColorsB[i]; Color r = new Color(); if(mode == 0 || mode == 1) { float restForB = GetMin(bColor.a, 1 - aColor.a); r = new Color(GetMiddleValue(aColor.r, bColor.r, aColor.a, restForB), GetMiddleValue(aColor.g, bColor.g, aColor.a, restForB), GetMiddleValue(aColor.b, bColor.b, aColor.a, restForB), aColor.a + restForB); } else if(mode == 2) { r = new Color(aColor.r * bColor.r, aColor.g * bColor.g, aColor.b * bColor.b, aColor.a * bColor.a); } resultColors[i] = r; } Texture2D toReturn = new Texture2D(a.width, a.height, TextureFormat.RGBA32, true); toReturn.SetPixels(0, 0, a.width, a.height, resultColors); toReturn.Apply(true); return toReturn; }
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); }
//------------------------------------------------------------------------- // TEXTURE METHODS //------------------------------------------------------------------------- public void Apply(Texture2D t) { HSB transformer = new HSB(); hue %= 360.0f; if (hue < 0.0f) { hue += 360.0f; } for (int i=0; i<t.mipmapCount; i++) { Color[] pixels = t.GetPixels(i); for (int j=0; j<pixels.Length; j++) { transformer.FromARGB(ref pixels[j]); transformer.hue += hue; transformer.brightness += brightness; transformer.saturation += saturation; transformer.hue = transformer.hue >= 360.0f ? transformer.hue-360.0f: transformer.hue; transformer.brightness = transformer.brightness < 0.0f ? 0.0f : transformer.brightness > 1.0f ? 1.0f: transformer.brightness; transformer.saturation = transformer.saturation < 0.0f ? 0.0f : transformer.saturation > 1.0f ? 1.0f : transformer.brightness; transformer.ToARGB(out pixels[j]); } t.SetPixels(pixels, i); } t.Apply(false); }
public Porta(string nome,int offset, TipoDePorta tipo, WNode nodoPai) { Nome = nome; yOffset = offset; tipoDaPorta = tipo; nodoDono = nodoPai; cor = Color.white; switch(offset) { case 0: cor = Color.green; break; case 1: cor = Color.blue; break; case 2: cor = Color.red; break; case 3: cor = Color.yellow; break; case 4: cor = Color.black; break; case 5: cor = Color.cyan; break; default: Debug.LogError("erro cor da porta não suportada"); break; } //guiskin = Resources.Load<GUISkin>("GUISkinNode"); // textura = Resources.Load<Texture2D>("CirculoBranco"); textura = (Texture2D)Object.Instantiate(Resources.Load<Texture2D>("CirculoBranco")); Color[] colors = textura.GetPixels(); for (int i = 0; i < colors.Length; i++) { if (colors[i].a != 0) colors[i] = cor; } textura.SetPixels(colors); textura.Apply(); }
private Texture2D getNewTexture2D(Texture2D copiedTexture) { Texture2D newTexture = new Texture2D(copiedTexture.width, copiedTexture.height); newTexture.filterMode = FilterMode.Point; newTexture.wrapMode = TextureWrapMode.Clamp; Color[] pixelColors = copiedTexture.GetPixels(0, 0, copiedTexture.width, copiedTexture.height); for (int pixelColorIndex = 0; pixelColorIndex < pixelColors.Length; ++pixelColorIndex) { for (int originalColorIndex = 0; originalColorIndex < OriginalColors.Length; ++originalColorIndex) { if (pixelColors[pixelColorIndex] == OriginalColors[originalColorIndex]) { pixelColors[pixelColorIndex] = TargetColors[originalColorIndex]; break; } } } newTexture.SetPixels(pixelColors); newTexture.name = (SpriteSheetName + "test"); newTexture.Apply(); return newTexture; }
/// <summary> /// Builds the texture atlas. /// </summary> public static void BuildTextureAtlas() { // Build texture array Texture2D[] atlasTextures = new Texture2D[textures.Count]; foreach (KeyValuePair<int, Texture2D> texture in textures) { atlasTextures[texture.Key] = texture.Value; } // Build atlas Texture2D _textureAtlas = new Texture2D (4096, 4096, TextureFormat.RGBA32, false); Rect[] uvRects = _textureAtlas.PackTextures (atlasTextures, 0); textureAtlas = new Texture2D(_textureAtlas.width, _textureAtlas.height, TextureFormat.RGBA32, false); textureAtlas.SetPixels (0,0,_textureAtlas.width, _textureAtlas.height, _textureAtlas.GetPixels (0,0,_textureAtlas.width, _textureAtlas.height)); // Set texture atlas properties textureAtlas.anisoLevel = 9; textureAtlas.filterMode = FilterMode.Point; textureAtlas.Apply (); // Save uvs textureCoordinates = new Dictionary<int, Vector2[]> (); foreach (KeyValuePair<int, Texture2D> texture in textures) { textureCoordinates.Add (texture.Key,new Vector2[] { new Vector2(uvRects[texture.Key].x, uvRects[texture.Key].y), new Vector2(uvRects[texture.Key].x+uvRects[texture.Key].width, uvRects[texture.Key].y), new Vector2(uvRects[texture.Key].x+uvRects[texture.Key].width, uvRects[texture.Key].y+uvRects[texture.Key].height), new Vector2(uvRects[texture.Key].x, uvRects[texture.Key].y+uvRects[texture.Key].height) }); } }
public static Texture2D DrawLine(Vector2 _from,Vector2 to, float w, Color col, Texture2D tex, bool stroke, Color strokeCol, float strokeWidth) { w = Mathf.Round (w);//It is important to round the numbers otherwise it will mess up with the texture width strokeWidth = Mathf.Round (strokeWidth); float extent = w + strokeWidth; float stY = Mathf.Clamp (Mathf.Min (_from.y,to.y)-extent,0,tex.height);//This is the topmost Y value float stX = Mathf.Clamp ((int)Mathf.Min (_from.x,to.x)-extent,0,(int)tex.width); float endY = Mathf.Clamp ((int)Mathf.Max (_from.y,to.y)+extent,0,(int)tex.height); float endX = Mathf.Clamp ((int)Mathf.Max (_from.x,to.x)+extent,0,(int)tex.width);//This is the rightmost Y value strokeWidth = strokeWidth/2; float strokeInner = (w-strokeWidth)*(w-strokeWidth); float strokeOuter = (w+strokeWidth)*(w+strokeWidth); float strokeOuter2 = (w+strokeWidth+1)*(w+strokeWidth+1); float sqrW = w*w;//It is much faster to calculate with squared values float lengthX = Mathf.Floor( endX-stX); float lengthY = Mathf.Floor( endY-stY); Vector2 start = new Vector2 (stX,stY); Color[] pixels = tex.GetPixels ((int)stX,(int)stY,(int)lengthX,(int)lengthY,0);//Get all pixels for (float y=0;y<lengthY;y++) { for (float x=0;x<lengthX;x++) {//Loop through the pixels Vector2 p = new Vector2 (x,y) + start; Vector2 center = p + new Vector2(0.5f,0.5f); float dist = (center-NearestPointStrict(_from,to,center)).sqrMagnitude;//The squared distance from the center of the pixels to the nearest point on the line if (dist<=strokeOuter2) { Vector2 [] samples = Sample (p); Color c = Color.clear; Color pc = pixels[(int)(y*lengthX+x)]; for (int i=0;i<samples.Length;i++) {//Loop through the samples dist = (samples[i]-NearestPointStrict(_from,to,samples[i])).sqrMagnitude;//The squared distance from the sample to the line if (stroke) { if (dist<=strokeOuter && dist >= strokeInner) { c+=strokeCol; } else if (dist<sqrW) { c+=col; } else { c+=pc; } } else { if (dist<sqrW) {//Is the distance smaller than the width of the line c+=col; } else { c+=pc;//No it wasn't, set it to be the original colour } } } c /= samples.Length;//Get the avarage colour pixels[(int)(y*lengthX+x)]=c; } } } tex.SetPixels ((int)stX,(int)stY,(int)lengthX,(int)lengthY,pixels,0); tex.Apply (); return tex; }
public BitmapData(Texture2D texture) { height = texture.height; width = texture.width; pixels = texture.GetPixels(); }
private static void FloodFill(Color oldColor, Color color, Texture2D tex, int fX, int fY, int minX, int minY, int maxX, int maxY) { if (Utility.SameColor (oldColor, color)) //just in case. return; int width = maxX - minX; int height = maxY - minY; Color[] colors = tex.GetPixels (minX, minY, width, height); //store the colors into a temporary buffer Stack<Texel> stack = new Stack<Texel> (); //non-recursive stack stack.Push (new Texel (fX, fY)); //original target while (stack.Count > 0) { Texel n = stack.Pop (); int index = (n.Y - minY)*width + (n.X - minX); //index into temporary buffer bool pixelIsInTheSprite = n.X >= minX && n.X < maxX && n.Y >= minY && n.Y < maxY; if (pixelIsInTheSprite) { bool colorMatches = Utility.SameColor (colors[index], oldColor); if (colorMatches) { colors[index] = color; stack.Push (n + new Texel (-1, 0)); // stack.Push (n + new Texel (1, 0)); // add to stack in all 4 directions stack.Push (n + new Texel (0, 1)); // stack.Push (n + new Texel (0, -1)); // } } } tex.SetPixels (minX, minY, width, height, colors); //put the temporary buffer back into the texture }
//Constructor public AnimatedSprite(Texture2D spriteSheet, int subimageCount, int cellWidth, int cellHeight, float defaultPlaybackRate = 1, bool doesLoop = false, int paddingWidth = 0, int paddingHeight = 0, int offsetX = 0, int offsetY = 0) { this.defaultPlaybackRate = defaultPlaybackRate; this.doesLoop = doesLoop; int cellPaddedWidth = cellWidth + paddingWidth * 2; int cellPaddedHeight = cellHeight + paddingHeight * 2; int cellsH = (spriteSheet.width - offsetX) / cellPaddedWidth; int cellsV = (spriteSheet.height - offsetY) / cellPaddedHeight; count = Mathf.Min(subimageCount, cellsH * cellsV); subimages = new Texture2D[count]; for(int i = 0; i < count; i++) subimages[i] = new Texture2D(cellWidth, cellHeight); int index = 0; for (int y = cellsV - 1; y >= 0; y--) { for (int x = 0; x < cellsH; x++) { if (index >= count) break; Texture2D tex = subimages[index++]; tex.SetPixels(spriteSheet.GetPixels(offsetX + paddingWidth + x * cellPaddedWidth, offsetY + paddingHeight + y * cellPaddedHeight, cellWidth, cellHeight)); tex.Apply(); if (index >= count) break; } } }
void OnPostprocessTexture(Texture2D texture) { if (!assetPath.EndsWith (suffix)) { return; } // Make a mask texture temporary in true color. var mask = new Texture2D (texture.width, texture.height, TextureFormat.RGB24, false); mask.wrapMode = TextureWrapMode.Clamp; // Convert the source image into a mask. var pixels = texture.GetPixels (); for (int i = 0; i < pixels.Length; i++) { var a = pixels [i].a; pixels [i] = new Color (a, a, a); } mask.SetPixels (pixels); // Compress the mask in the proper compression format. EditorUtility.CompressTexture (mask, CompressionFormat, TextureCompressionQuality.Best); // Replace the asset which already exists, or create a new asset. var maskPath = assetPath.Replace (suffix, "mask.asset"); var maskAsset = AssetDatabase.LoadAssetAtPath (maskPath, typeof(Texture2D)) as Texture2D; if (maskAsset == null) { AssetDatabase.CreateAsset (mask, maskPath); } else { EditorUtility.CopySerialized (mask, maskAsset); } // Compress the source texture. EditorUtility.CompressTexture (texture, CompressionFormat, TextureCompressionQuality.Best); }
public static void CropTexture(Texture2D tex, int x, int y, int width, int height) { var newPixels = tex.GetPixels(x, y, width, height); tex.Resize(width, height); tex.SetPixels(newPixels); tex.Apply(); }
public static string Decode (Texture2D image){ //Get the pixels for the image... Color[] imagePixels = image.GetPixels(); //Go Through the First 32 Pixels and create a 4 byte array. //This array should give us the message's length. BitArray newBits = new BitArray(32); for (int i=0;i<32;i++){ if(imagePixels[i].a == 1){ newBits[i] = true; } else { newBits[i] = false; } } int total = System.BitConverter.ToInt32(ToByteArray(newBits), 0); BitArray messageBits = new BitArray(total); for (int j=32;j<total + 32;j++){ if(imagePixels[j].a == 1){ messageBits[j-32] = true; } else { messageBits[j-32] = false; } } return System.Text.Encoding.ASCII.GetString(ToByteArray(messageBits)); }
public static void MakeBasis(string name, Vector4 basisx, Vector4 basisy, Vector4 basisz) { Color[] cBasisx; Color[] cBasisy; Color[] cBasisz; Texture2D texBasisx = new Texture2D(16, 16); Texture2D texBasisy = new Texture2D(16, 16); Texture2D texBasisz = new Texture2D(16, 16); cBasisx = texBasisx.GetPixels(); cBasisy = texBasisy.GetPixels(); cBasisz = texBasisz.GetPixels(); for (int i = 0; i < cBasisx.Length; i++) { cBasisx[i] = CompressedBasis(basisx); cBasisy[i] = CompressedBasis(basisy); cBasisz[i] = CompressedBasis(basisz); } texBasisx.SetPixels(cBasisx); texBasisy.SetPixels(cBasisy); texBasisz.SetPixels(cBasisz); if (!System.IO.Directory.Exists("Assets/GzRNM/BasisMaps")) System.IO.Directory.CreateDirectory("Assets/GzRNM/BasisMaps"); GzCRNMMergeUtil.TextureImportHelper(texBasisx, "Assets/GzRNM/BasisMaps/Basis X.png"); GzCRNMMergeUtil.TextureImportHelper(texBasisy, "Assets/GzRNM/BasisMaps/Basis Y.png"); GzCRNMMergeUtil.TextureImportHelper(texBasisz, "Assets/GzRNM/BasisMaps/Basis Z.png"); }
public static void Generate(ref Texture2D WaterTex, ScmapEditor Map){ Color[] AllColors = WaterTex.GetPixels (); float WaterHeight = Map.map.Water.Elevation * 0.1f; if (WaterHeight == 0) WaterHeight = 1; float WaterDeep = Map.map.Water.ElevationAbyss * 0.1f; float DeepDifference = (WaterHeight - WaterDeep) / WaterHeight; float Width = WaterTex.width; float Height = WaterTex.height; int i = 0; int x = 0; int y = 0; float WaterDepth = 0; for (x = 0; x < WaterTex.width; x++) { for (y = 0; y < WaterTex.height; y++) { //int i = x + y * WaterTex.width; i = x + y * WaterTex.width; //i++; WaterDepth = Map.Data.GetInterpolatedHeight (x / Width, 1f - y / Height); WaterDepth = (WaterHeight - WaterDepth) / WaterHeight; WaterDepth /= DeepDifference; AllColors [i] = new Color (AllColors [i].r, Mathf.Clamp01 (WaterDepth), (1f - Mathf.Clamp01(WaterDepth * 100f)) , 0); } } WaterTex.SetPixels(AllColors) ; WaterTex.Apply (); }
public static bool LoadTexture2DLutFromImage( Texture2D texture, ToolSettings settings, out Texture2D lutTexture ) { var width = settings.Resolution.TargetWidth; var height = settings.Resolution.TargetHeight; var size = settings.LUT.Size; var cols = settings.LUT.Columns; var rows = settings.LUT.Rows; var imageData = texture.GetPixels(); var lutText = new Texture2D( size * size, size, TextureFormat.ARGB32, false ); var lutData = new Color[ size * size * size ]; for ( int h = 0, i = 0; h < size; h++ ) { for ( int r = 0; r < rows; r++ ) { for ( int w = 0; w < size * cols; w++ ) { var x = w; var y = h + r * size; y = height - 1 - y; lutData[ i++ ] = imageData[ x + y * width ]; } } } lutText.SetPixels( lutData ); lutText.Apply(); lutTexture = lutText; return true; }
// Initialization void Start() { // Create texture that will be updated in the plugin m_Texture = new Texture2D (width, height, TextureFormat.ARGB32, false); renderer.material.mainTexture = m_Texture; // Create the pixel array for the plugin to write into at startup m_Pixels = m_Texture.GetPixels (0); // "pin" the array in memory, so we can pass direct pointer to it's data to the plugin, // without costly marshaling of array of structures. ZPAY.iOSBarCodeExpert.m_PixelsHandle = GCHandle.Alloc(m_Pixels, GCHandleType.Pinned); // takeScreenShot(m_Texture.GetNativeTextureID()); // Assign texture to the renderer if (renderer) renderer.material.mainTexture = m_Texture; // or gui texture else if (GetComponent(typeof(GUITexture))) { GUITexture gui = GetComponent(typeof(GUITexture)) as GUITexture; gui.texture = m_Texture; } else { Debug.Log("Game object has no renderer or gui texture to assign the generated texture to!"); } ZPAY.iOSBarcodeHandler.ActionPluginError = new Action<string>(AcPluginError); ZPAY.iOSBarcodeHandler.ActionScanBarCode = new Action<string>(AcScanBarCode); }
void Awake() { m_rawImage = this.GetComponent<RawImage>(); //Draw and save the biome graph texture immediately, as it doesn't depend upon any generated data. m_biomeGraph = new Texture2D (Size, Size); Color[] pixels = m_biomeGraph.GetPixels (); for (int x = 0; x < m_biomeGraph.width; ++x) { for (int y = 0; y < m_biomeGraph.height; ++y) { float temperature = x / (float)m_biomeGraph.width; float precipitation = y / (float)m_biomeGraph.height; float[] biomeWeights = BiomeCalculator.Instance.GetBiomeWeights (temperature, precipitation, 1f); BiomeType[] allBiomes = System.Enum.GetValues(typeof(BiomeType)) as BiomeType[]; Color pixel = new Color(0f, 0f, 0f, 0f); foreach (BiomeType biome in allBiomes) { pixel += BiomeDatabase.Instance.Get(biome).MinimapColor * biomeWeights[(int) biome]; } pixels [y * m_biomeGraph.width + x] = pixel; } } m_biomeGraph.SetPixels (pixels); m_biomeGraph.Apply (); //Also, write out the biome map to a png file for reference. byte[] outfile = m_biomeGraph.EncodeToPNG(); File.WriteAllBytes(Application.dataPath + "/../BiomeMap.png", outfile); }
private void UpdateFogTexture(GameObject[] visions) { var texture = new Texture2D(128, 128); var fog = fogTexture.GetPixels(0, 0, 128, 128); texture.SetPixels(fog); var texturePixels = texture.GetPixels(); for (int i = 0; i < visions.Length; i++) { var visionPos = visions[i].transform.position; var texturePos = new Vector2(visionPos.x, visionPos.z); texturePos += mapOffset; texturePos /= mapSize; texturePos *= fogSize; texturePos -= new Vector2(16, 16); int x = Mathf.FloorToInt(texturePos.x); int y = Mathf.FloorToInt(texturePos.y); AddPixels(texturePixels, visionPixels, x, y, GetVisionPoly(visions[i].transform.position)); } texture.SetPixels(texturePixels); texture.Apply(); Shader.SetGlobalTexture("_FogTex", texture); Shader.SetGlobalTexture("_LastFogTex", lastFogTexture); Shader.SetGlobalFloat("_FogBlend", 0f); lastFogTexture = texture; }
/// <summary> /// Converts vector data stored within a texture using the specified basis. Assume all calculations are performed in tangent space. /// </summary> /// <param name='basis'> /// Basis to multiply the vector values against. /// </param> /// <param name='vectorData'> /// Texture2D containing vector data. Textures are passed by reference, so make sure to copy beforehand if you don't want to overwrite your data! /// </param> public static void ConvertTangentBasis(Matrix4x4 basis, ref Texture2D vectorData, bool recomputeZ = false) { Color[] colorData = vectorData.GetPixels(); Texture2D tmpTexture = new Texture2D(vectorData.width, vectorData.height, TextureFormat.ARGB32, false); for (int i = 0; i < colorData.Length; i++) { Color vecData = new Color(colorData[i].r, colorData[i].g, colorData[i].b, 1); vecData.r = Vector3.Dot(new Vector3(basis.m00, basis.m01, basis.m02), UnpackUnitVector(new Vector3(colorData[i].r, colorData[i].g, colorData[i].b))) * 0.5f + 0.5f; vecData.g = Vector3.Dot(new Vector3(basis.m10, basis.m11, basis.m12), UnpackUnitVector(new Vector3(colorData[i].r, colorData[i].g, colorData[i].b))) * 0.5f + 0.5f; if (recomputeZ) { vecData.r = vecData.r * 2 - 1; vecData.g = vecData.g * 2 - 1; vecData.b = Mathf.Sqrt(1 - vecData.r * vecData.r - vecData.g * vecData.g) * 0.5f + 0.5f; vecData.r = vecData.r * 0.5f + 0.5f; vecData.g = vecData.g * 0.5f + 0.5f; } else { vecData.b = Vector3.Dot(new Vector3(basis.m20, basis.m21, basis.m22), UnpackUnitVector(new Vector3(colorData[i].r, colorData[i].g, colorData[i].b))) * 0.5f + 0.5f; } colorData[i] = vecData; } tmpTexture.SetPixels(colorData); tmpTexture.Apply(); vectorData = tmpTexture; }
public override Texture2D CreateSprite (Texture2D texture) { Color[] pixels = texture.GetPixels (); Color[,] pixels2D = new Color[texture.width, texture.height]; for (int i = 0; i < pixels.Length; i++) { int x = i % texture.width; int y = i / texture.height; pixels2D [x, y] = pixels [i]; } int amountOfEyes = (int)(sightAngle / 45); for (int i = 0; i < amountOfEyes; i++) { int radius = (texture.width - eyeSize) / 2; float xPos = Mathf.Sin (Mathf.Deg2Rad * (i * 45)); float yPos = Mathf.Cos (Mathf.Deg2Rad * (i * 45)); Vector2 pos = new Vector2 (xPos, yPos) * radius + new Vector2 (texture.width - eyeSize, texture.height - eyeSize) / 2; ApplyKernel (ref pixels2D, texture.width, texture.height, pos); } for (int x = 0; x < pixels2D.GetLength (0); x++) { for (int y = 0; y < pixels2D.GetLength (1); y++) { pixels [x + y * texture.width] = pixels2D [x, y]; } } texture.SetPixels (pixels); texture.Apply (); return texture; }
private Texture2D[] generateSwatches() { if (paletteSwatch == null) { paletteSwatch = new Texture2D[availablePalettes.Length]; for (int i = 0; i < availablePalettes.Length; i++) { int k = 0; int m = 120; int paletteSize = availablePalettes[i].colors.Length; if (paletteSize == 11) m = 121; else if (paletteSize == 18) m = 126; else if (paletteSize == 9) m = 117; else if (paletteSize == 7) m = 119; Texture2D t = new Texture2D(m, 1); Color[] pix = t.GetPixels(); int sW = m / paletteSize; for (int j = 0; j < m; j++) { if (j % sW == 0) k++; pix[j] = availablePalettes[i].colors[k-1]; } t.SetPixels(pix); t.Apply(); paletteSwatch[i] = t; } return paletteSwatch; } else return paletteSwatch; }
// Update is called once per frame void Update () { if (colorMyMang == null) { return; } if (Input.GetButtonDown("Fire1")) { Debug.Log(Input.mousePosition); _ColorManager = colorMyMang.GetComponent<colorMyMang>(); if(_ColorManager == null) { return; } ttt = _ColorManager.GetColorTexture(); Texture2D result = new Texture2D((int)1024, (int)1024); result.SetPixels(ttt.GetPixels(Mathf.FloorToInt(448),Mathf.FloorToInt(28),Mathf.FloorToInt(1024),Mathf.FloorToInt(1024))); //TextureScale.Bilinear(result, 32, 32); TextureScale.Bilinear(result, 32, 32); byte[] bytes = result.EncodeToPNG(); File.WriteAllBytes("SavedScreen.png", bytes); cubeRR.GetComponent<Renderer>().material.mainTexture = ttt; } }
static public int GetPixels(IntPtr l) { try{ int argc = LuaDLL.lua_gettop(l); if (argc == 1) { UnityEngine.Texture2D self = (UnityEngine.Texture2D)checkSelf(l); UnityEngine.Color[] ret = self.GetPixels(); pushValue(l, ret); return(1); } else if (argc == 2) { UnityEngine.Texture2D self = (UnityEngine.Texture2D)checkSelf(l); System.Int32 a1; checkType(l, 2, out a1); UnityEngine.Color[] ret = self.GetPixels(a1); pushValue(l, ret); return(1); } else if (argc == 6) { UnityEngine.Texture2D self = (UnityEngine.Texture2D)checkSelf(l); System.Int32 a1; checkType(l, 2, out a1); System.Int32 a2; checkType(l, 3, out a2); System.Int32 a3; checkType(l, 4, out a3); System.Int32 a4; checkType(l, 5, out a4); System.Int32 a5; checkType(l, 6, out a5); UnityEngine.Color[] ret = self.GetPixels(a1, a2, a3, a4, a5); pushValue(l, ret); return(1); } else if (argc == 5) { UnityEngine.Texture2D self = (UnityEngine.Texture2D)checkSelf(l); System.Int32 a1; checkType(l, 2, out a1); System.Int32 a2; checkType(l, 3, out a2); System.Int32 a3; checkType(l, 4, out a3); System.Int32 a4; checkType(l, 5, out a4); UnityEngine.Color[] ret = self.GetPixels(a1, a2, a3, a4); pushValue(l, ret); return(1); } LuaDLL.luaL_error(l, "No matched override function to call"); return(0); } catch (Exception e) { LuaDLL.luaL_error(l, e.ToString()); return(0); } }
public static void Encode(Texture2D src, Texture2D dst, bool gpu = false, YCgACoFormat format = YCgACoFormat.CgACoY_DontChange, int quality = 100) { var pixels = src.GetPixels(); Resize(src, dst, format); var resized = src.width != dst.width || src.height != dst.height; if (gpu) { // TODO: Force mipmap and trilinear when resized. var shader = Shader.Find("Hidden/RGBA to CgACoY"); var mat = new Material(shader); var temp = RenderTexture.GetTemporary(dst.width, dst.height); Graphics.Blit(src, temp, mat); dst.ReadPixels(new Rect(0, 0, dst.width, dst.height), 0, 0); RenderTexture.ReleaseTemporary(temp); Object.DestroyImmediate(mat); } else { if (resized) { var srcPixels = pixels; pixels = dst.GetPixels(); Shrink(srcPixels, pixels, src.width, dst.width); } RGBAToCgACoY(pixels, pixels); dst.SetPixels(pixels); } Compress(dst, format, quality); }
static int GetPixels(IntPtr L) { #if UNITY_EDITOR ToluaProfiler.AddCallRecord("UnityEngine.Texture2D.Register"); #endif try { int count = LuaDLL.lua_gettop(L); if (count == 1) { UnityEngine.Texture2D obj = (UnityEngine.Texture2D)ToLua.CheckObject(L, 1, typeof(UnityEngine.Texture2D)); UnityEngine.Color[] o = obj.GetPixels(); ToLua.Push(L, o); return(1); } else if (count == 2) { UnityEngine.Texture2D obj = (UnityEngine.Texture2D)ToLua.CheckObject(L, 1, typeof(UnityEngine.Texture2D)); int arg0 = (int)LuaDLL.luaL_checknumber(L, 2); UnityEngine.Color[] o = obj.GetPixels(arg0); ToLua.Push(L, o); return(1); } else if (count == 5) { 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); int arg3 = (int)LuaDLL.luaL_checknumber(L, 5); UnityEngine.Color[] o = obj.GetPixels(arg0, arg1, arg2, arg3); ToLua.Push(L, o); return(1); } else if (count == 6) { 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); int arg3 = (int)LuaDLL.luaL_checknumber(L, 5); int arg4 = (int)LuaDLL.luaL_checknumber(L, 6); UnityEngine.Color[] o = obj.GetPixels(arg0, arg1, arg2, arg3, arg4); ToLua.Push(L, o); return(1); } else { return(LuaDLL.luaL_throw(L, "invalid arguments to method: UnityEngine.Texture2D.GetPixels")); } } catch (Exception e) { return(LuaDLL.toluaL_exception(L, e)); } }
static int GetPixels(IntPtr L) { try { int count = LuaDLL.lua_gettop(L); if (count == 1 && TypeChecker.CheckTypes(L, 1, typeof(UnityEngine.Texture2D))) { UnityEngine.Texture2D obj = (UnityEngine.Texture2D)ToLua.ToObject(L, 1); UnityEngine.Color[] o = obj.GetPixels(); ToLua.Push(L, o); return(1); } else if (count == 2 && TypeChecker.CheckTypes(L, 1, typeof(UnityEngine.Texture2D), typeof(int))) { UnityEngine.Texture2D obj = (UnityEngine.Texture2D)ToLua.ToObject(L, 1); int arg0 = (int)LuaDLL.lua_tonumber(L, 2); UnityEngine.Color[] o = obj.GetPixels(arg0); ToLua.Push(L, o); return(1); } else if (count == 5 && TypeChecker.CheckTypes(L, 1, typeof(UnityEngine.Texture2D), typeof(int), typeof(int), typeof(int), typeof(int))) { UnityEngine.Texture2D obj = (UnityEngine.Texture2D)ToLua.ToObject(L, 1); int arg0 = (int)LuaDLL.lua_tonumber(L, 2); int arg1 = (int)LuaDLL.lua_tonumber(L, 3); int arg2 = (int)LuaDLL.lua_tonumber(L, 4); int arg3 = (int)LuaDLL.lua_tonumber(L, 5); UnityEngine.Color[] o = obj.GetPixels(arg0, arg1, arg2, arg3); ToLua.Push(L, o); return(1); } else if (count == 6 && TypeChecker.CheckTypes(L, 1, typeof(UnityEngine.Texture2D), typeof(int), typeof(int), typeof(int), typeof(int), typeof(int))) { UnityEngine.Texture2D obj = (UnityEngine.Texture2D)ToLua.ToObject(L, 1); int arg0 = (int)LuaDLL.lua_tonumber(L, 2); int arg1 = (int)LuaDLL.lua_tonumber(L, 3); int arg2 = (int)LuaDLL.lua_tonumber(L, 4); int arg3 = (int)LuaDLL.lua_tonumber(L, 5); int arg4 = (int)LuaDLL.lua_tonumber(L, 6); UnityEngine.Color[] o = obj.GetPixels(arg0, arg1, arg2, arg3, arg4); ToLua.Push(L, o); return(1); } else { return(LuaDLL.luaL_throw(L, "invalid arguments to method: UnityEngine.Texture2D.GetPixels")); } } catch (Exception e) { return(LuaDLL.toluaL_exception(L, e)); } }
static int GetPixels(IntPtr L) { try { int count = LuaDLL.lua_gettop(L); if (count == 1) { UnityEngine.Texture2D obj = (UnityEngine.Texture2D)ToLua.CheckObject <UnityEngine.Texture2D>(L, 1); UnityEngine.Color[] o = obj.GetPixels(); ToLua.Push(L, o); return(1); } else if (count == 2) { UnityEngine.Texture2D obj = (UnityEngine.Texture2D)ToLua.CheckObject <UnityEngine.Texture2D>(L, 1); int arg0 = (int)LuaDLL.luaL_checkinteger(L, 2); UnityEngine.Color[] o = obj.GetPixels(arg0); ToLua.Push(L, o); return(1); } else if (count == 5) { UnityEngine.Texture2D obj = (UnityEngine.Texture2D)ToLua.CheckObject <UnityEngine.Texture2D>(L, 1); int arg0 = (int)LuaDLL.luaL_checkinteger(L, 2); int arg1 = (int)LuaDLL.luaL_checkinteger(L, 3); int arg2 = (int)LuaDLL.luaL_checkinteger(L, 4); int arg3 = (int)LuaDLL.luaL_checkinteger(L, 5); UnityEngine.Color[] o = obj.GetPixels(arg0, arg1, arg2, arg3); ToLua.Push(L, o); return(1); } else if (count == 6) { UnityEngine.Texture2D obj = (UnityEngine.Texture2D)ToLua.CheckObject <UnityEngine.Texture2D>(L, 1); int arg0 = (int)LuaDLL.luaL_checkinteger(L, 2); int arg1 = (int)LuaDLL.luaL_checkinteger(L, 3); int arg2 = (int)LuaDLL.luaL_checkinteger(L, 4); int arg3 = (int)LuaDLL.luaL_checkinteger(L, 5); int arg4 = (int)LuaDLL.luaL_checkinteger(L, 6); UnityEngine.Color[] o = obj.GetPixels(arg0, arg1, arg2, arg3, arg4); ToLua.Push(L, o); return(1); } else { return(LuaDLL.luaL_throw(L, "invalid arguments to method: UnityEngine.Texture2D.GetPixels")); } } catch (Exception e) { return(LuaDLL.toluaL_exception(L, e)); } }
static int QPYX_GetPixels_YXQP(IntPtr L_YXQP) { try { int QPYX_count_YXQP = LuaDLL.lua_gettop(L_YXQP); if (QPYX_count_YXQP == 1) { UnityEngine.Texture2D QPYX_obj_YXQP = (UnityEngine.Texture2D)ToLua.CheckObject(L_YXQP, 1, typeof(UnityEngine.Texture2D)); UnityEngine.Color[] QPYX_o_YXQP = QPYX_obj_YXQP.GetPixels(); ToLua.Push(L_YXQP, QPYX_o_YXQP); return(1); } else if (QPYX_count_YXQP == 2) { 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); UnityEngine.Color[] QPYX_o_YXQP = QPYX_obj_YXQP.GetPixels(QPYX_arg0_YXQP); ToLua.Push(L_YXQP, QPYX_o_YXQP); return(1); } else if (QPYX_count_YXQP == 5) { 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); int QPYX_arg2_YXQP = (int)LuaDLL.luaL_checknumber(L_YXQP, 4); int QPYX_arg3_YXQP = (int)LuaDLL.luaL_checknumber(L_YXQP, 5); UnityEngine.Color[] QPYX_o_YXQP = QPYX_obj_YXQP.GetPixels(QPYX_arg0_YXQP, QPYX_arg1_YXQP, QPYX_arg2_YXQP, QPYX_arg3_YXQP); ToLua.Push(L_YXQP, QPYX_o_YXQP); return(1); } else if (QPYX_count_YXQP == 6) { 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); int QPYX_arg2_YXQP = (int)LuaDLL.luaL_checknumber(L_YXQP, 4); int QPYX_arg3_YXQP = (int)LuaDLL.luaL_checknumber(L_YXQP, 5); int QPYX_arg4_YXQP = (int)LuaDLL.luaL_checknumber(L_YXQP, 6); UnityEngine.Color[] QPYX_o_YXQP = QPYX_obj_YXQP.GetPixels(QPYX_arg0_YXQP, QPYX_arg1_YXQP, QPYX_arg2_YXQP, QPYX_arg3_YXQP, QPYX_arg4_YXQP); ToLua.Push(L_YXQP, QPYX_o_YXQP); return(1); } else { return(LuaDLL.luaL_throw(L_YXQP, "invalid arguments to method: UnityEngine.Texture2D.GetPixels")); } } catch (Exception e_YXQP) { return(LuaDLL.toluaL_exception(L_YXQP, e_YXQP)); } }
public Color[] GetPixels(int x, int y, int width, int height) { // Fix: updside down. var ucs = texture.GetPixels(x, y, width, height); var cs = new Color[ucs.Length]; for (int i = 0; i < ucs.Length; i++) { cs[i] = ucs[i].ToColor(); } return(cs); }
public static int GetPixels_wrap(long L) { try { long nThisPtr = FCLibHelper.fc_get_inport_obj_ptr(L); UnityEngine.Texture2D obj = get_obj(nThisPtr); Color[] ret = obj.GetPixels(); long ret_ptr = FCLibHelper.fc_get_return_ptr(L); FCCustomParam.ReturnArray(ret, ret_ptr); } catch (Exception e) { Debug.LogException(e); } return(0); }
public void Load(string path) { UnityEngine.Texture2D texture = Resources.Load(path) as UnityEngine.Texture2D; UnityEngine.Color[] colorsRead = texture.GetPixels(); colors = new Color[colorsRead.Length]; for (int i = 0; i < colorsRead.Length; i++) { Color color = new Color(); color.r = colorsRead[i].r; color.g = colorsRead[i].g; color.b = colorsRead[i].b; color.a = colorsRead[i].a; colors[i] = color; } width = texture.width; height = texture.height; }
public static int GetPixels2_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); int arg2 = FCLibHelper.fc_get_int(L, 2); int arg3 = FCLibHelper.fc_get_int(L, 3); int arg4 = FCLibHelper.fc_get_int(L, 4); Color[] ret = obj.GetPixels(arg0, arg1, arg2, arg3, arg4); long ret_ptr = FCLibHelper.fc_get_return_ptr(L); FCCustomParam.ReturnArray(ret, ret_ptr); } catch (Exception e) { Debug.LogException(e); } return(0); }
static public int GetPixels(IntPtr l) { try { int argc = LuaDLL.lua_gettop(l); if (argc == 7) { UnityEngine.Texture2D self = (UnityEngine.Texture2D)checkSelf(l); System.Int32 a1; checkType(l, 3, out a1); System.Int32 a2; checkType(l, 4, out a2); System.Int32 a3; checkType(l, 5, out a3); System.Int32 a4; checkType(l, 6, out a4); System.Int32 a5; checkType(l, 7, out a5); var ret = self.GetPixels(a1, a2, a3, a4, a5); pushValue(l, true); pushValue(l, ret); return(2); } else if (argc == 6) { UnityEngine.Texture2D self = (UnityEngine.Texture2D)checkSelf(l); System.Int32 a1; checkType(l, 3, out a1); System.Int32 a2; checkType(l, 4, out a2); System.Int32 a3; checkType(l, 5, out a3); System.Int32 a4; checkType(l, 6, out a4); var ret = self.GetPixels(a1, a2, a3, a4); pushValue(l, true); pushValue(l, ret); return(2); } else if (argc == 3) { UnityEngine.Texture2D self = (UnityEngine.Texture2D)checkSelf(l); System.Int32 a1; checkType(l, 3, out a1); var ret = self.GetPixels(a1); pushValue(l, true); pushValue(l, ret); return(2); } else if (argc == 2) { UnityEngine.Texture2D self = (UnityEngine.Texture2D)checkSelf(l); var ret = self.GetPixels(); pushValue(l, true); pushValue(l, ret); return(2); } pushValue(l, false); LuaDLL.lua_pushstring(l, "No matched override function to call"); return(2); } catch (Exception e) { return(error(l, e)); } }
internal static UnityEngine.Texture2D BlitTexture(UnityEngine.Texture2D texture, UnityEngine.TextureFormat format, bool alphaOnly = false) { var texPath = AssetDatabase.GetAssetPath(texture); var asset = AssetDatabase.LoadMainAssetAtPath(texPath); RenderTextureReadWrite rtReadWrite = UnityEngine.RenderTextureReadWrite.sRGB; if (asset is Texture2D) { var importer = (TextureImporter)AssetImporter.GetAtPath(texPath); if (importer != null) { rtReadWrite = importer.sRGBTexture ? UnityEngine.RenderTextureReadWrite.sRGB : UnityEngine.RenderTextureReadWrite.Linear; } } // hack to support text mesh pro var fontAssetType = Type.GetType("TMPro.TMP_FontAsset, Unity.TextMeshPro, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null"); if (fontAssetType != null && fontAssetType.IsInstanceOfType(asset)) { // TMPro texture atlases are always Linear space rtReadWrite = UnityEngine.RenderTextureReadWrite.Linear; } // Create a temporary RenderTexture of the same size as the texture var tmp = UnityEngine.RenderTexture.GetTemporary( texture.width, texture.height, 0, UnityEngine.RenderTextureFormat.Default, rtReadWrite); // Blit the pixels on texture to the RenderTexture UnityEngine.Graphics.Blit(texture, tmp); // Backup the currently set RenderTexture var previous = UnityEngine.RenderTexture.active; // Set the current RenderTexture to the temporary one we created UnityEngine.RenderTexture.active = tmp; // Create a new readable Texture2D to copy the pixels to it var result = new UnityEngine.Texture2D(texture.width, texture.height, format, false); // Copy the pixels from the RenderTexture to the new Texture result.ReadPixels(new UnityEngine.Rect(0, 0, tmp.width, tmp.height), 0, 0); result.Apply(); // Broadcast alpha to color if (alphaOnly || !HasColor(texture)) { var pixels = result.GetPixels(); for (var i = 0; i < pixels.Length; i++) { pixels[i].r = pixels[i].a; pixels[i].g = pixels[i].a; pixels[i].b = pixels[i].a; } result.SetPixels(pixels); result.Apply(); } // Reset the active RenderTexture UnityEngine.RenderTexture.active = previous; // Release the temporary RenderTexture UnityEngine.RenderTexture.ReleaseTemporary(tmp); return(result); }
// Update is called once per frame void Update() { //Added by Yuqi Ding MetaCoreInterop.meta_get_point_cloud(ref _metaPointCloud, _translation, _rotation); // obtain the rgb data MetaCoreInterop.meta_get_rgb_frame(RawPixelBuffer, _translation_rgb, _new_rotation_rgb); // The buffer is pre-allocated by constructor. // obtain the rgb data parameter MetaCoreInterop.meta_get_rgb_intrinsics(ref _camera_params); // Check for a difference bool isEqual = true; // Check for a difference in pose (should change with each new RGB frame). for (int i = 0; i < _new_rotation_rgb.Length; ++i) { isEqual = _rotation_rgb[i] == _new_rotation_rgb[i]; if (!isEqual) { break; } } // If the two rotations are not equal, we have a new rgb frame. if (!isEqual) { // Copy new rotation if it's different. for (int i = 0; i < _new_rotation_rgb.Length; ++i) { _rotation_rgb[i] = _new_rotation_rgb[i]; } _rgbTexture.LoadRawTextureData(RawPixelBuffer, _totalBufferSize); _rgbTexture.Apply(); } SetDepthToWorldTransform(); if (SavePointCloud && (Time.frameCount % 48 == 0)) { MarshalMetaPointCloud(); int num = _metaPointCloud.num_points; if (num != 0) { //save the point cloud string PointCloudName = string.Format("{0}/{1:D04} shot.ply", folder, Time.frameCount); SavePointCloudToPly(PointCloudName, _pointCloud); string PointCloudIntrName = string.Format("{0}/{1:D04} pointcloud_Intr.txt", folder, Time.frameCount); SavePointCloudPara(PointCloudIntrName, _translation, _rotation); //save the rgb frame Color[] color2dtemp = _rgbTexture.GetPixels(); for (int i = 0; i < color2dtemp.Length; i++) { float temp = 0.0f; temp = color2dtemp[i].r; color2dtemp[i].r = color2dtemp[i].b; color2dtemp[i].b = temp; } _rgbTexture.SetPixels(color2dtemp); //Debug.Log("Swap r and b"); byte[] bytes = _rgbTexture.EncodeToJPG(); string rgbName = string.Format("{0}/{1:D04} shot.jpg", folder, Time.frameCount); File.WriteAllBytes(rgbName, bytes); string rgbIntrName = string.Format("{0}/{1:D04} shot_Intr.txt", folder, Time.frameCount); SaveRGBIntrinsics(rgbIntrName, _camera_params); string rgbParaName = string.Format("{0}/{1:D04} shot_Para.txt", folder, Time.frameCount); SaveRGBPara(rgbParaName, _translation_rgb, _rotation_rgb); } // Added end } }
static public int GetPixels(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 int argc = LuaDLL.lua_gettop(l); if (argc == 1) { UnityEngine.Texture2D self = (UnityEngine.Texture2D)checkSelf(l); var ret = self.GetPixels(); pushValue(l, true); pushValue(l, ret); return(2); } else if (argc == 2) { UnityEngine.Texture2D self = (UnityEngine.Texture2D)checkSelf(l); System.Int32 a1; checkType(l, 2, out a1); var ret = self.GetPixels(a1); pushValue(l, true); pushValue(l, ret); return(2); } else if (argc == 5) { UnityEngine.Texture2D self = (UnityEngine.Texture2D)checkSelf(l); System.Int32 a1; checkType(l, 2, out a1); System.Int32 a2; checkType(l, 3, out a2); System.Int32 a3; checkType(l, 4, out a3); System.Int32 a4; checkType(l, 5, out a4); var ret = self.GetPixels(a1, a2, a3, a4); pushValue(l, true); pushValue(l, ret); return(2); } else if (argc == 6) { UnityEngine.Texture2D self = (UnityEngine.Texture2D)checkSelf(l); System.Int32 a1; checkType(l, 2, out a1); System.Int32 a2; checkType(l, 3, out a2); System.Int32 a3; checkType(l, 4, out a3); System.Int32 a4; checkType(l, 5, out a4); System.Int32 a5; checkType(l, 6, out a5); var ret = self.GetPixels(a1, a2, a3, a4, a5); pushValue(l, true); pushValue(l, ret); return(2); } pushValue(l, false); LuaDLL.lua_pushstring(l, "No matched override function GetPixels to call"); 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 }