public void SetPixels(Color[] colors) { // Fix: updside down. var ucs = new UnityEngine.Color32[colors.Length]; for (int i = 0; i < ucs.Length; i++) { ucs[i] = colors[i].ToUnityColor(); } texture.SetPixels32(ucs); }
void Start() { _block = new CRoomCellTypeManager( _cellSize, 10, new Color32(0, 0, 200, 200), new Color32(250, 250, 250, 255), new Color32(250, 0, 0, 255)); _map._AreaList.Add(new CMapCreator.CArea()); _map._AreaList[0].ResetArea(50, 50, 50); _map._AreaList[0].Produce(); _prefab = (GameObject)Resources.Load("Prefabs/Room"); foreach (CMapCreator.CArea.CCrawler cc in _map._AreaList[0]._evaCrawler.GetCells()) { int[,] __room = cc.GetRoomData(); Texture2D __t2d = new Texture2D(__room.GetLength(0) * (_cellSize - _borderSize) + _borderSize, __room.GetLength(1) * (_cellSize - _borderSize) + _borderSize, TextureFormat.RGBA32, false); for (int x = 0; x < __t2d.width; x++) { for (int y = 0; y < __t2d.height; y++) __t2d.SetPixel(x, y, _transparent); } for (int x = 0; x < __room.GetLength(0); x++) { for (int y = 0; y < __room.GetLength(1); y++) { if (__room[x, y] > 0 && (__room[x, y] & (int)ERoomFlag.UpWall) == 0 && (__room[x, y] & (int)ERoomFlag.LeftWall) == 0) __t2d.SetPixels32(x * (_cellSize - _borderSize), y * (_cellSize - _borderSize), _block.GetSize(), _block.GetSize(), _block.GetColorBlock((ERoomFlag)__room[x, y]), 0); } for (int y = 0; y < __room.GetLength(1); y++) { if (__room[x, y] > 0 && ((__room[x, y] & (int)ERoomFlag.UpWall) != 0 || (__room[x, y] & (int)ERoomFlag.LeftWall) != 0)) __t2d.SetPixels32(x * (_cellSize - _borderSize), y * (_cellSize - _borderSize), _block.GetSize(), _block.GetSize(), _block.GetColorBlock((ERoomFlag)__room[x, y]), 0); } } __t2d.Apply(); GameObject g = Instantiate(_prefab) as GameObject; CRoomRender obj = g.GetComponent<CRoomRender>(); obj.SetTexture(__t2d); obj.SetData(cc); } Debug.Log("Rooms:" + _map._AreaList[0]._evaCrawler.GetCrawlerCount()); }
public Texture2D generateTexture(MapProvider provider, BBox bbox, int i, int j, string OSMFileName) { string[] proj = OSMFileName.Split(new char[] {'/', '\\'}); string projectName = proj[proj.Length - 1]; if (File.Exists(tileFolder + "/final/" + provider.ToString("G") + "_" + projectName + "_" + i + "_" + j + ".png")) { byte[] fileData; fileData = File.ReadAllBytes(tileFolder + "/final/" + provider.ToString("G") + "_" + projectName + "_" + i + "_" + j + ".png"); Texture2D texture = new Texture2D(2, 2); texture.LoadImage(fileData); return texture; } Texture2D uncroppedTexture = generateUncroppedTexture(bbox, provider, i, j); Rect cropWindow = generateCroppingRect(bbox); Texture2D finalTexture = CropTexture(uncroppedTexture, cropWindow); if (!Directory.Exists(tileFolder + "/final/")) Directory.CreateDirectory(tileFolder + "/final/"); var tex = new Texture2D(finalTexture.width, finalTexture.height); tex.SetPixels32(finalTexture.GetPixels32()); tex.Apply(false); File.WriteAllBytes(tileFolder + "/final/" + provider.ToString("G") + "_" + projectName + "_" + i + "_" + j + ".png", tex.EncodeToPNG()); return finalTexture; }
static void Convert(string origpath, string filename, string destpath) { FSNEditorUtils.MakeTargetDirectory(destpath); // 타겟 경로 확보 var assetpath = destpath + "/" + filename; var absolutepath = Application.dataPath + "/../" + assetpath; //Debug.LogFormat("asset path : {0}, absolute target path : {1}", assetpath, absolutepath); if (AssetDatabase.AssetPathToGUID(assetpath) != null) // 복사하려는 위치에 해당 어셋이 이미 존재한다면 기존 것은 삭제 AssetDatabase.DeleteAsset(assetpath); AssetDatabase.CopyAsset(origpath, assetpath); // 변경하려는 어셋을 복제하여 타겟 경로에 넣는다. AssetDatabase.Refresh(); var texture = AssetDatabase.LoadAssetAtPath<Texture2D>(assetpath); var converted = new Texture2D(texture.width, texture.height, TextureFormat.ARGB32, false); var origcolors = texture.GetPixels32(); var len = origcolors.Length; for (int i = 0; i < len; i++) // 픽셀마다 알파곱 계산 { var color = origcolors[i]; var alpha = (int)color.a; color.r = (byte)(color.r * alpha / 255); color.g = (byte)(color.g * alpha / 255); color.b = (byte)(color.b * alpha / 255); origcolors[i] = color; } converted.SetPixels32(origcolors); System.IO.File.WriteAllBytes(absolutepath, converted.EncodeToPNG()); // 실제 파일로 write AssetDatabase.ImportAsset(assetpath); var importer = AssetImporter.GetAtPath(assetpath) as TextureImporter; // 텍스쳐 옵션 설정 importer.textureType= TextureImporterType.Advanced; importer.alphaIsTransparency = false; // premultiplied alpha texture는 이 옵션을 꺼줘야 한다. importer.SaveAndReimport(); }
void Start() { Texture2D mip = new Texture2D(tileTex.width, tileTex.height, TextureFormat.ARGB32, true, false); { tileTex.filterMode = FilterMode.Point; var pdata = tileTex.GetPixels32(0); mip.SetPixels32(pdata, 0); mip.Apply(true); } int width = tileTex.width; int layer = 0; while (width > tileSplit) { width /= 2; layer++; Debug.Log("p layer:" + layer); Texture2D m = new Texture2D(width, width, TextureFormat.ARGB32, false, true); m.filterMode = FilterMode.Point; var d = mip.GetPixels32(layer); m.SetPixels32(d, 0); m.Apply(); mipTileTex.Add(m); } GameObject.Destroy(mip); wordData = new worldData[srcTex.width * srcTex.height]; scale = srcTex.width; }
void WriteImageToTexture(Texture2D tex) { // NOTE: This method only works when unity is rendering with OpenGL ("unity.exe -force-opengl"). This is *much* faster // then Texture2D::SetPixels32 which we would have to use otherwise // NOTE: The native texture id needs a +1 if we are rendering to GUI if (openGl) { //Gl.glBindTexture(Gl.GL_TEXTURE_2D, (null == target) ? tex.GetNativeTextureID() + 1 : tex.GetNativeTextureID()); //Gl.glTexSubImage2D(Gl.GL_TEXTURE_2D, 0, 0, 0, (int)inputSize.x, (int)inputSize.y, Gl.GL_RGB, Gl.GL_UNSIGNED_BYTE, Image.ImageMapPtr); GL.BindTexture(GL.TEXTURE_2D, (null == target) ? tex.GetNativeTextureID() + 1 : tex.GetNativeTextureID()); GL.TexSubImage2D(GL.TEXTURE_2D, 0, 0, 0, (int)inputSize.x, (int)inputSize.y, GL.RGB, GL.UNSIGNED_BYTE, Image.ImageMapPtr); return; } // The slow method: copy image map data to a manager buffer, and then create a Color32 // for each pixel Marshal.Copy(Image.ImageMapPtr, rawImageMap, 0, rawImageMap.Length); int src = 0; int dst = 0; for (int row = 0; row < (int)inputSize.y; row++) { for (int col = 0; col < (int)inputSize.x; col++) { imageMapPixels[dst] = new Color32(rawImageMap[src], rawImageMap[src + 1], rawImageMap[src + 2], 255); src += 3; dst++; } dst += (int)(outputSize.x - inputSize.x); } tex.SetPixels32(imageMapPixels); tex.Apply(); }
// public Texture2D DoDithering(Texture2D input) { this.width = input.width; this.height = input.height; // Copy all pixels of input Texture2D to new array, which we are going to edit this.pixels = input.GetPixels32(); Color32 originalPixel = Color.white; // Default value isn't used Color32 newPixel = Color.white; // Default value isn't used short[] quantError = null; // Default values aren't used for (int y = 0; y < this.height; y++) { for (int x = 0; x < this.width; x++) { originalPixel = this.pixels[GetIndexWith(x, y)]; newPixel = this.colorFunction(originalPixel); this.pixels[GetIndexWith(x, y)] = newPixel; quantError = GetQuantError(originalPixel, newPixel); this.PushError(x, y, quantError); } } // Create the texture we are going to return from pixels array Texture2D returnTexture = new Texture2D(width, height, TextureFormat.ARGB32, false); returnTexture.SetPixels32(this.pixels); returnTexture.Apply(); return returnTexture; }
void LoadTexDistanceField(string name) { #if UNITY_STANDALONE string filename = System.IO.Path.Combine(Application.streamingAssetsPath, name + ".png"); string filename_df = System.IO.Path.Combine(Application.streamingAssetsPath, name + "_df.png"); //if (System.IO.File.Exists(filename_df)) //{ // Texture2D tex = new Texture2D(1, 1); // tex.LoadImage(System.IO.File.ReadAllBytes(filename_df)); // texs[name] = tex; //} //else { Texture2D tex = new Texture2D(1, 1); tex.LoadImage(System.IO.File.ReadAllBytes(filename));//加载原始图片 Color32[] _bsdata = tex.GetPixels32(0); KDTree2D tree = new KDTree2D(); List<KDTree2D.Train> treedata = new List<KDTree2D.Train>(); FindBorder(tex.width,tex.height,_bsdata, treedata);//四次采样寻找边界,并把在边界上的点填入点集 var node = tree.CreatKDTree(treedata);//用KDTree来查找最近点 int w = tex.width; int h = tex.height; DateTime t1 = DateTime.Now; float maxlen = (float)Mathf.Sqrt(w * w + h * h) / 4; for (int y = 0; y < h; y++) { for (int x = 0; x < w; x++) { var near = tree.KDTreeFindNearest(node, new KDTree2D.Train() { positionX = x, positionY = y }); float d = (float)Mathf.Sqrt((near.point.positionX - x) * (near.point.positionX - x) + (near.point.positionY - y) * (near.point.positionY - y)); if (_bsdata[y * w + x].a < 128) { d *= -1; _bsdata[y * w + x]= _bsdata[(int)near.point.positionY * w + (int)near.point.positionX]; } float dist = d / maxlen; if (dist < -1) dist = -1; if (dist > 1) dist = 1; var b = (byte)(128 + 127.0f * dist); _bsdata[y * w + x].a = b;//替换原alpha值为距离值,形状内>128,形状外<128 } } DateTime t2 = DateTime.Now; Debug.Log("t=" + (t2 - t1).TotalSeconds); tex.SetPixels32(_bsdata); tex.Apply(); System.IO.File.WriteAllBytes(filename_df, tex.EncodeToPNG());//保存为新文件 texs[name] = tex; } #endif }
public static Texture2D CutTextureBottomBorder(this Texture2D texture, int cutSize) { var newTexture = new Texture2D(texture.width, texture.height, TextureFormat.RGBA32, false, true); var pixels = texture.GetPixels32(); var markerRow = cutSize; Color32[] newPixels = new Color32[texture.width * texture.height]; for (var row = 0; row < texture.height; row++) { for (int pix = 0; pix < texture.width; pix++) { if (row > markerRow) { newPixels[row * texture.width + pix] = pixels[row * texture.width + pix]; } else { newPixels[row * texture.width + pix] = pixels[markerRow * texture.width + pix]; } } } newTexture.SetPixels32(newPixels); newTexture.Apply(); return newTexture; }
public static Texture2D createFrame(int width, int height, int borderSize, Color32 borderColor, Color32 innerColor) { int size=width * height; Texture2D texture = new Texture2D(width,height); Color32[] pixels = new Color32[size]; int firstRowEnd = width * borderSize; int lastRowStart = size - width * borderSize; int leftBound = borderSize; int rightBound = width - borderSize; int remainder; for (int i = 0; i < pixels.Length; i++) { if (i<firstRowEnd || i > lastRowStart) pixels[i]=borderColor; else { remainder = i % width; if (remainder < leftBound || remainder >= rightBound) pixels[i]=borderColor; else pixels[i]=innerColor; } } texture.SetPixels32(pixels); texture.Apply(); return texture; }
// Texture Function -------------------------------------------------------------- public static Texture2D CopyTexture(Texture2D srcTex, Texture2D tarTex) { Color32[] colBuf = srcTex.GetPixels32(); tarTex.SetPixels32(colBuf); tarTex.Apply(false); return tarTex; }
public Texture2D GetCurrentTexture() { Texture2D t = new Texture2D(320,240,TextureFormat.ARGB32,false); t.SetPixels32(mipmapImg(kinect.getColor(),640,480)); t.Apply(false); return t; }
static public int SetPixels32(IntPtr l) { try { int argc = LuaDLL.lua_gettop(l); if (argc == 2) { UnityEngine.Texture2D self = (UnityEngine.Texture2D)checkSelf(l); UnityEngine.Color32[] a1; checkArray(l, 2, out a1); self.SetPixels32(a1); pushValue(l, true); return(1); } else if (argc == 3) { UnityEngine.Texture2D self = (UnityEngine.Texture2D)checkSelf(l); UnityEngine.Color32[] a1; checkArray(l, 2, out a1); System.Int32 a2; checkType(l, 3, out a2); self.SetPixels32(a1, a2); pushValue(l, true); return(1); } pushValue(l, false); LuaDLL.lua_pushstring(l, "No matched override function to call"); return(2); } catch (Exception e) { return(error(l, e)); } }
public Texture2D GetImage() { Texture2D texture = new Texture2D (deviceCamera.width, deviceCamera.height); texture.SetPixels32 (deviceCamera.GetPixels32 ()); texture.Apply (); return texture; }
public static Texture2D MirrorTexture(this Texture2D _inputTex, bool _mirrorHorizontally, bool _mirrorVertically) { int _texWidth = _inputTex.width; int _texWidthMinus1 = _texWidth - 1; int _texHeight = _inputTex.height; int _texHeightMinus1 = _texHeight - 1; // Pixels Color32[] _inputTexPixels = _inputTex.GetPixels32(0); Color32[] _mirrorTexPixels = new Color32[_inputTexPixels.Length]; for (int _yCord = 0; _yCord < _texHeight; _yCord++) { int _sourceIndex = _yCord * _texWidth; int _destYCord = _mirrorVertically ? (_texHeightMinus1 - _yCord) : _yCord; int _destIndex = _destYCord * _texWidth; for (int _xCord = 0; _xCord < _texWidth; _xCord++) { int _destXCord = _mirrorHorizontally ? (_texWidthMinus1 - _xCord) : _xCord; _mirrorTexPixels[_destIndex + _destXCord] = _inputTexPixels[_sourceIndex++]; } } // Create mirrored texture Texture2D _mirrorTex = new Texture2D(_texWidth, _texHeight, _inputTex.format, false); _mirrorTex.SetPixels32(_mirrorTexPixels, 0); _mirrorTex.Apply(); Debug.Log("[TextureExtensions:Mirror] Output W=" + _mirrorTex.width + " H=" + _mirrorTex.height); return _mirrorTex; }
public static Texture2D createTexture(int width, int height, Color32[] colors) { Texture2D tex = new Texture2D(width,height,TextureFormat.ARGB32,false); tex.SetPixels32(colors); tex.Apply(false); return tex; }
static public int SetPixels32(IntPtr l) { try{ int argc = LuaDLL.lua_gettop(l); if (argc == 3) { UnityEngine.Texture2D self = (UnityEngine.Texture2D)checkSelf(l); UnityEngine.Color32[] a1; checkType(l, 2, out a1); System.Int32 a2; checkType(l, 3, out a2); self.SetPixels32(a1, a2); return(0); } else if (argc == 2) { UnityEngine.Texture2D self = (UnityEngine.Texture2D)checkSelf(l); UnityEngine.Color32[] a1; checkType(l, 2, out a1); self.SetPixels32(a1); return(0); } LuaDLL.luaL_error(l, "No matched override function to call"); return(0); } catch (Exception e) { LuaDLL.luaL_error(l, e.ToString()); return(0); } }
void NoiseTexture(int size, bool mono) { var path = EditorUtility.SaveFilePanel("Save Noise Texture", "Assets", "noise" + size, "png"); if (path != "") { var tex = new Texture2D(size, size, TextureFormat.ARGB32, false); var s2 = size * size; var cols = new Color32[s2]; for (int i = 0; i < s2; ++i) { if (mono) { var r = (byte)Random.Range(0, 256); cols[i] = new Color32(r, r, r, 255); } else { cols[i] = new Color32((byte)Random.Range(0, 256), (byte)Random.Range(0, 256), (byte)Random.Range(0, 256), 255); } } tex.SetPixels32(cols); tex.Apply(); System.IO.File.WriteAllBytes(path, tex.EncodeToPNG()); AssetDatabase.Refresh(); Object.DestroyImmediate(tex); path = "Assets" + path.Remove(0, Application.dataPath.Length); // tex = (Texture2D)AssetDatabase.LoadAssetAtPath(path, typeof(Texture2D)); TextureImporter textureImporter = AssetImporter.GetAtPath(path) as TextureImporter; textureImporter.textureFormat = TextureImporterFormat.ARGB32; textureImporter.anisoLevel = 0; AssetDatabase.ImportAsset(path); // EditorUtility.CompressTexture(tex, TextureFormat.ARGB32, 0); //tex.format = TextureFormat.ARGB32; } }
public TilesetPropertiesWindow (TilesetWindow parent) { _parent = parent; _overlay = new Texture2D(OverlaySize, OverlaySize, TextureFormat.ARGB32, false); _highlight1 = new Texture2D(OverlaySize, OverlaySize, TextureFormat.ARGB32, false); _highlight2 = new Texture2D(OverlaySize, OverlaySize, TextureFormat.ARGB32, false); var colorO = new Color32(255, 255, 0, 127); var colorsO = Util.InitilizeArray(OverlaySize, colorO); _overlay.SetPixels32(colorsO); _overlay.Apply(); var colorH1 = new Color32(255, 0, 255, 127); var colorsH1 = Util.InitilizeArray(OverlaySize, colorH1); _highlight1.SetPixels32(colorsH1); _highlight1.Apply(); var colorH2 = new Color32(0, 255, 255, 127); var colorsH2 = Util.InitilizeArray(OverlaySize, colorH2); _highlight2.SetPixels32(colorsH2); _highlight2.Apply(); }
static int SetPixels32(IntPtr L) { try { int count = LuaDLL.lua_gettop(L); if (count == 2 && TypeChecker.CheckTypes(L, 1, typeof(UnityEngine.Texture2D), typeof(UnityEngine.Color32[]))) { UnityEngine.Texture2D obj = (UnityEngine.Texture2D)ToLua.ToObject(L, 1); UnityEngine.Color32[] arg0 = ToLua.CheckObjectArray <UnityEngine.Color32>(L, 2); obj.SetPixels32(arg0); return(0); } else if (count == 3 && TypeChecker.CheckTypes(L, 1, typeof(UnityEngine.Texture2D), typeof(UnityEngine.Color32[]), typeof(int))) { UnityEngine.Texture2D obj = (UnityEngine.Texture2D)ToLua.ToObject(L, 1); UnityEngine.Color32[] arg0 = ToLua.CheckObjectArray <UnityEngine.Color32>(L, 2); int arg1 = (int)LuaDLL.lua_tonumber(L, 3); obj.SetPixels32(arg0, arg1); return(0); } else { return(LuaDLL.luaL_throw(L, "invalid arguments to method: UnityEngine.Texture2D.SetPixels32")); } } catch (Exception e) { return(LuaDLL.toluaL_exception(L, e)); } }
//========================================================================= // Methods created by petrucio -> http://answers.unity3d.com/questions/238922/png-transparency-has-white-borderhalo.html // // Copy the values of adjacent pixels to transparent pixels color info, to // remove the white border artifact when importing transparent .PNGs. public static void FixTransparency(Texture2D texture) { Color32[] pixels = texture.GetPixels32(); int w = texture.width; int h = texture.height; for (int y = 0; y < h; y++) { for (int x = 0; x < w; x++) { int idx = y * w + x; Color32 pixel = pixels[idx]; if (pixel.a == 0) { bool done = false; if (!done && x > 0) done = TryAdjacent(ref pixel, pixels[idx - 1]); // Left pixel if (!done && x < w - 1) done = TryAdjacent(ref pixel, pixels[idx + 1]); // Right pixel if (!done && y > 0) done = TryAdjacent(ref pixel, pixels[idx - w]); // Top pixel if (!done && y < h - 1) done = TryAdjacent(ref pixel, pixels[idx + w]); // Bottom pixel pixels[idx] = pixel; } } } texture.SetPixels32(pixels); texture.Apply(); }
// 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)); }
// Update is called once per frame void Update() { img = camTex.GetPixels32(); Texture2D texture = new Texture2D(camTex.width, camTex.height); texture.SetPixels32(img); //send screenshot if (Input.GetKeyDown(KeyCode.Space)){ //convert Texture2D tex2d = new Texture2D(camTex.width, camTex.height); tex2d.SetPixels32(img); byte[] bytes = tex2d.EncodeToPNG(); //send data StartCoroutine("accessToServer", bytes); //accessToServer(bytes); print("finish capture and send"); } //debug else if (Input.GetKeyDown(KeyCode.Q)) { result_txt = "test_txt"; camTex.Stop(); CameraFade.StartAlphaFade(Color.white, false, 1f, 0f, () => { Application.LoadLevel("SummonResultScene"); }); } }
/*public static Texture2D LoadTGA(string fileName) { using (var imageFile = File.OpenRead(fileName)) { return LoadTGA(imageFile); } }*/ public static Texture2D LoadTGA(Stream TGAStream) { using (BinaryReader r = new BinaryReader(TGAStream)) { // Skip some header info we don't care about. // Even if we did care, we have to move the stream seek point to the beginning, // as the previous method in the workflow left it at the end. r.BaseStream.Seek(12, SeekOrigin.Begin); short width = r.ReadInt16(); short height = r.ReadInt16(); int bitDepth = r.ReadByte(); // Skip a byte of header information we don't care about. r.BaseStream.Seek(1, SeekOrigin.Current); Texture2D tex = new Texture2D(width, height); Color32[] pulledColors = new Color32[width * height]; int length = width * height; if (bitDepth == 32) { for (int row = 1; row <= height; row++) { for (int col = 0; col < width; col++) { byte red = r.ReadByte(); byte green = r.ReadByte(); byte blue = r.ReadByte(); byte alpha = r.ReadByte(); // pulledColors [i] = new Color32(blue, green, red, alpha); pulledColors [length - (row * width) + col] = new Color32(blue, green, red, alpha); } } } else if (bitDepth == 24) { for (int row = 1; row <= height; row++) { for (int col = 0; col < width; col++) { byte red = r.ReadByte(); byte green = r.ReadByte(); byte blue = r.ReadByte(); pulledColors [length - (row * width) + col] = new Color32(blue, green, red, 1); } } } else { throw new Exception("TGA texture had non 32/24 bit depth."); } tex.SetPixels32(pulledColors); tex.Apply(); return tex; } }
void OnGUI() { if (finalize) { // select created texture Selection.activeObject=AssetDatabase.LoadAssetAtPath(save_path, typeof(Texture2D)); finalize=false; } EditorGUILayout.Space(); source_tex0 = EditorGUILayout.ObjectField("Source Texture 0", source_tex0, typeof(Texture2D), false) as Texture2D; EditorGUILayout.Space(); source_tex1 = EditorGUILayout.ObjectField("Source Texture 1", source_tex1, typeof(Texture2D), false) as Texture2D; EditorGUILayout.Space(); source_tex2 = EditorGUILayout.ObjectField("Source Texture 2", source_tex2, typeof(Texture2D), false) as Texture2D; EditorGUILayout.Space(); source_tex3 = EditorGUILayout.ObjectField("Source Texture 3", source_tex3, typeof(Texture2D), false) as Texture2D; EditorGUILayout.Space(); if (source_tex0) { int sources_ready=0; if (check_texture(source_tex0, 0, source_tex0.width, source_tex0.height)) { sources_ready++; sourceChannel0 = (RTPColorChannels)EditorGUILayout.EnumPopup("Target R from source 0", sourceChannel0); } if (check_texture(source_tex1, 1, source_tex0.width, source_tex0.height)) { sources_ready++; sourceChannel1 = (RTPColorChannels)EditorGUILayout.EnumPopup("Target G from source 1", sourceChannel1); } if (check_texture(source_tex2, 2, source_tex0.width, source_tex0.height)) { sources_ready++; sourceChannel2 = (RTPColorChannels)EditorGUILayout.EnumPopup("Target B from source 2", sourceChannel2); } if (check_texture(source_tex3, 3, source_tex0.width, source_tex0.height)) { sources_ready++; sourceChannel3 = (RTPColorChannels)EditorGUILayout.EnumPopup("Target A from source 3", sourceChannel3); } if (sources_ready==4) { if (GUILayout.Button("Render mixed texture")) { rendered_tex=new Texture2D(source_tex0.width, source_tex0.height, TextureFormat.ARGB32, true); byte[] colsR=get_color_channel(source_tex0, sourceChannel0); byte[] colsG=get_color_channel(source_tex1, sourceChannel1); byte[] colsB=get_color_channel(source_tex2, sourceChannel2); byte[] colsA=get_color_channel(source_tex3, sourceChannel3); Color32[] cols=rendered_tex.GetPixels32(); for(int i=0; i<cols.Length; i++) { cols[i].r=colsR[i]; cols[i].g=colsG[i]; cols[i].b=colsB[i]; cols[i].a=colsA[i]; } rendered_tex.SetPixels32(cols); if (Selection.activeObject is Texture2D) { save_path=AssetDatabase.GetAssetPath(Selection.activeObject as Texture2D); directory=Path.GetDirectoryName(save_path); file=Path.GetFileNameWithoutExtension(save_path)+".png"; } else { if (save_path=="") { directory=Path.GetDirectoryName(AssetDatabase.GetAssetPath(source_tex0)); file=Path.GetFileNameWithoutExtension(AssetDatabase.GetAssetPath(source_tex0))+"(mixed).png"; } } } } } if (rendered_tex) { linearTexture=GUILayout.Toggle(linearTexture, "Linear texture (Bypass sRGB Sampling)"); if (GUILayout.Button("Save texture")) { SaveTexture(directory, file); } } }
void Setup() { RectTransform rt = image.GetComponent<RectTransform>(); txW = (int)rt.sizeDelta.x; txH = (int)rt.sizeDelta.y; easeH = (int)(txH * 0.35f); txBorder = (int)((txH - easeH) * 0.5f); Color32[] colors = new Color32[txW * txH]; for (int c = 0; c < colors.Length; ++c) colors[c] = new Color(0.1f, 0.1f, 0.1f, 1); int lineP = txBorder * txW; for (int c = lineP; c < lineP + txW; ++c) { colors[c] = new Color(0.25f, 0.25f, 0.25f, 1); colors[c + txW * easeH] = new Color(0.25f, 0.25f, 0.25f, 1); } // Create a tween for each easeType int totTypes = Enum.GetNames(typeof(Ease)).Length; int distX = txW; int distY = txH; int totCols = Screen.width / txW - 1; float startX = image.transform.position.x; float startY = image.transform.position.y; Vector2 gridCount = Vector2.zero; for (int i = 0; i < totTypes + 1; ++i) { // Instantiate and position new Images Transform t = ((GameObject)Instantiate(image.gameObject)).transform; t.SetParent(image.transform.parent); t.position = new Vector3(startX + distX * gridCount.x + txDistance * gridCount.x, startY - distY * gridCount.y - txDistance * gridCount.y, 0); gridCount.x++; if (gridCount.x > totCols) { gridCount.y++; gridCount.x = 0; } // Set textures Texture2D tx = new Texture2D(txW, txH, TextureFormat.ARGB32, false); tx.filterMode = FilterMode.Point; tx.SetPixels32(colors); tx.Apply(); RawImage img = t.GetComponent<RawImage>(); img.texture = tx; // Set tween and text Ease easeType = (Ease)i; float val = txBorder; Tween tween = DOTween.To(() => val, x => val = x, txBorder + easeH, tweenDuration).SetDelay(1); tween.OnUpdate(() => SetTextureEase(easeType, tx, tween.Elapsed(), (int)val)); if (i == totTypes) { img.GetComponentInChildren<Text>().text = "custom"; tween.SetEase((time, duration, overshootOrAmplitude, period)=> { return (float)Math.Sin(time /= duration) / (float)Math.Cos(time /= duration); }); } else { img.GetComponentInChildren<Text>().text = easeType.ToString(); if (easeType == Ease.INTERNAL_Custom) tween.SetEase(easeCurve); else tween.SetEase(easeType); } } // Disable original image image.gameObject.SetActive(false); }
/// <summary> /// Converts a <see cref="GenericImage{T}"/> to a <see cref="Texture2D"/> type. /// </summary> /// <param name="image">The image to be converted.</param> /// <returns>Returns a new <see cref="Texture2D"/> type.</returns> public static Texture2D ToTexture2D(this GenericImage<UnityEngine.Color> image) { var texture = new Texture2D(image.Width, image.Height, TextureFormat.ARGB32, false); var flippedImage = image.Clone(); flippedImage.FlipVertically(); texture.SetPixels32(flippedImage.ToUnityColor32Array()); texture.Apply(); return texture; }
static int SetPixels32(IntPtr L) { #if UNITY_EDITOR ToluaProfiler.AddCallRecord("UnityEngine.Texture2D.Register"); #endif try { int count = LuaDLL.lua_gettop(L); if (count == 2) { UnityEngine.Texture2D obj = (UnityEngine.Texture2D)ToLua.CheckObject(L, 1, typeof(UnityEngine.Texture2D)); UnityEngine.Color32[] arg0 = ToLua.CheckStructArray <UnityEngine.Color32>(L, 2); obj.SetPixels32(arg0); return(0); } else if (count == 3) { UnityEngine.Texture2D obj = (UnityEngine.Texture2D)ToLua.CheckObject(L, 1, typeof(UnityEngine.Texture2D)); UnityEngine.Color32[] arg0 = ToLua.CheckStructArray <UnityEngine.Color32>(L, 2); int arg1 = (int)LuaDLL.luaL_checknumber(L, 3); obj.SetPixels32(arg0, arg1); return(0); } 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); UnityEngine.Color32[] arg4 = ToLua.CheckStructArray <UnityEngine.Color32>(L, 6); obj.SetPixels32(arg0, arg1, arg2, arg3, arg4); return(0); } else if (count == 7) { 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.Color32[] arg4 = ToLua.CheckStructArray <UnityEngine.Color32>(L, 6); int arg5 = (int)LuaDLL.luaL_checknumber(L, 7); obj.SetPixels32(arg0, arg1, arg2, arg3, arg4, arg5); return(0); } else { return(LuaDLL.luaL_throw(L, "invalid arguments to method: UnityEngine.Texture2D.SetPixels32")); } } catch (Exception e) { return(LuaDLL.toluaL_exception(L, e)); } }
public static void FlipTexture2DVertically(Texture2D texture2D) { var pixels = texture2D.GetPixels32(); Utils.Flip2DArrayVertically(pixels, texture2D.height, texture2D.width); texture2D.SetPixels32(pixels); texture2D.Apply(); }
public Texture2D getWebCamTexture() { //テクスチャに変換 Color32[] color32 = webcamTexture.GetPixels32(); Texture2D texture = new Texture2D(webcamTexture.width, webcamTexture.height); texture.SetPixels32(color32); texture.Apply(); return texture; }
IEnumerator Start() { if (signal == null) signal = Enumerable.Repeat(originalSignal, 3).SelectMany(b => b).ToArray(); tex = new Texture2D(1024, 512, TextureFormat.ARGB32, false); rend.material.mainTexture = tex; Color32[] cols = tex.GetPixels32(); //byte[] send = new byte[cols.Length * 4]; Write("230.0.0.1", 9876); int dataSize = sendAlpha ? 4 : 3; while(true) { yield return null; if (Input.GetKeyDown(KeyCode.H)) { Debug.Log("Sent so far: " + sent); } if (Input.GetKeyDown(KeyCode.G)) { for(int i = 0; i < cols.Length; ++i) { cols[i]= new Color32((byte)Random.Range(0,255), (byte)Random.Range(0,255), (byte)Random.Range(0,255), (byte)255); } tex.SetPixels32(cols); tex.Apply(); /* for(int i = 0; i < cols.Length; ++i) { for(int j = 0; j < dataSize; ++j) { var col = cols[i]; switch(j) { case 0: default: send[i*dataSize + j] = col.r; break; case 1: send[i*dataSize + j] = col.g; break; case 2: send[i*dataSize + j] = col.b; break; case 3: send[i*dataSize + j] = col.a; break; } } } */ AddData(tex.EncodeToPNG()); //AddData(send); } } }
public static Texture2D createTexture(int width, int height, Color color){ Texture2D tex = new Texture2D(width,height,TextureFormat.ARGB32,false); Color32[] pixels=tex.GetPixels32(); for (int i=0;i<pixels.Length;i++){ pixels[i]=color; } tex.SetPixels32(pixels); tex.Apply(false); return tex; }
public static Texture2D EncodeImage(string data) { BitMatrix bitMat = CodeWriter.encode(data, BarcodeFormat.QR_CODE, QR_DIMENSION, QR_DIMENSION); Color32[] cols = ColorRenderer.Render(bitMat, BarcodeFormat.QR_CODE,""); Texture2D tex = new Texture2D(bitMat.Width,bitMat.Height); //we set pixels to the new image tex.SetPixels32(cols); return tex; }
public Texture2D Create3x3Tex() { Texture2D t = new Texture2D(3, 3, TextureFormat.ARGB32, false); Color32[] cs = new Color32[t.width * t.height]; for (int i =0; i < cs.Length; i++) { cs[i] = color; } t.SetPixels32(cs); t.Apply(); return t; }
public static Texture2D InverseTexture32(Texture2D srcTex, Texture2D tarTex) { Color32[] colBuf = srcTex.GetPixels32(); for (int n = 0; n < colBuf.Length; n++) colBuf[n].a = (byte)(255 - colBuf[n].a); tarTex.SetPixels32(colBuf); tarTex.Apply(false); return tarTex; }
static int SetPixels32(IntPtr L) { try { int count = LuaDLL.lua_gettop(L); if (count == 2 && TypeChecker.CheckTypes <UnityEngine.Texture2D, UnityEngine.Color32[]>(L, 1)) { UnityEngine.Texture2D obj = (UnityEngine.Texture2D)ToLua.ToObject(L, 1); UnityEngine.Color32[] arg0 = ToLua.ToStructArray <UnityEngine.Color32>(L, 2); obj.SetPixels32(arg0); return(0); } else if (count == 3 && TypeChecker.CheckTypes <UnityEngine.Texture2D, UnityEngine.Color32[], int>(L, 1)) { UnityEngine.Texture2D obj = (UnityEngine.Texture2D)ToLua.ToObject(L, 1); UnityEngine.Color32[] arg0 = ToLua.ToStructArray <UnityEngine.Color32>(L, 2); int arg1 = (int)LuaDLL.lua_tonumber(L, 3); obj.SetPixels32(arg0, arg1); return(0); } else if (count == 6 && TypeChecker.CheckTypes <UnityEngine.Texture2D, int, int, int, int, UnityEngine.Color32[]>(L, 1)) { 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.Color32[] arg4 = ToLua.ToStructArray <UnityEngine.Color32>(L, 6); obj.SetPixels32(arg0, arg1, arg2, arg3, arg4); return(0); } else if (count == 7 && TypeChecker.CheckTypes <UnityEngine.Texture2D, int, int, int, int, UnityEngine.Color32[], int>(L, 1)) { 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.Color32[] arg4 = ToLua.ToStructArray <UnityEngine.Color32>(L, 6); int arg5 = (int)LuaDLL.lua_tonumber(L, 7); obj.SetPixels32(arg0, arg1, arg2, arg3, arg4, arg5); return(0); } else { return(LuaDLL.luaL_throw(L, "invalid arguments to method: UnityEngine.Texture2D.SetPixels32")); } } catch (Exception e) { return(LuaDLL.toluaL_exception(L, e)); } }
static int SetPixels32(IntPtr L) { try { int count = LuaDLL.lua_gettop(L); if (count == 2) { UnityEngine.Texture2D obj = (UnityEngine.Texture2D)ToLua.CheckObject <UnityEngine.Texture2D>(L, 1); UnityEngine.Color32[] arg0 = ToLua.CheckStructArray <UnityEngine.Color32>(L, 2); obj.SetPixels32(arg0); return(0); } else if (count == 3) { UnityEngine.Texture2D obj = (UnityEngine.Texture2D)ToLua.CheckObject <UnityEngine.Texture2D>(L, 1); UnityEngine.Color32[] arg0 = ToLua.CheckStructArray <UnityEngine.Color32>(L, 2); int arg1 = (int)LuaDLL.luaL_checkinteger(L, 3); obj.SetPixels32(arg0, arg1); return(0); } 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); UnityEngine.Color32[] arg4 = ToLua.CheckStructArray <UnityEngine.Color32>(L, 6); obj.SetPixels32(arg0, arg1, arg2, arg3, arg4); return(0); } else if (count == 7) { 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.Color32[] arg4 = ToLua.CheckStructArray <UnityEngine.Color32>(L, 6); int arg5 = (int)LuaDLL.luaL_checkinteger(L, 7); obj.SetPixels32(arg0, arg1, arg2, arg3, arg4, arg5); return(0); } else { return(LuaDLL.luaL_throw(L, "invalid arguments to method: UnityEngine.Texture2D.SetPixels32")); } } catch (Exception e) { return(LuaDLL.toluaL_exception(L, e)); } }
/// <summary> /// Call this to capture a custom, screenshot /// </summary> /// <param name="width"></param> /// <param name="height"></param> /// <returns></returns> public static Texture2D CaptureCustomScreenshot(int width, int height) { UnityEngine.Texture2D textured = new UnityEngine.Texture2D(width, height, UnityEngine.TextureFormat.RGB24, true, false); textured.ReadPixels(new UnityEngine.Rect(0f, 0f, (float)width, (float)height), 0, 0); int miplevel = UnityEngine.Screen.width / 800; UnityEngine.Texture2D textured2 = new UnityEngine.Texture2D(width >> miplevel, height >> miplevel, UnityEngine.TextureFormat.RGB24, false, false); textured2.SetPixels32(textured.GetPixels32(miplevel)); textured2.Apply(); return(textured2); }
//////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////// /// <summary> /// Re sample and crop a texture /// </summary> /// <param name="p_Source">Source texture</param> /// <param name="p_TargetWidth">Target width</param> /// <param name="p_TargetHeight">Target height</param> /// <returns></returns> public static UnityEngine.Texture2D ResampleAndCrop(UnityEngine.Texture2D p_Source, int p_TargetWidth, int p_TargetHeight, float p_YOffsetRel = 0.5f) { int l_SourceWidth = p_Source.width; int l_SourceHeight = p_Source.height; float l_SourceAspect = (float)l_SourceWidth / l_SourceHeight; float l_TargetAspect = (float)p_TargetWidth / p_TargetHeight; int l_XOffset = 0; int l_YOffset = 0; float l_Factor; /// Crop width if (l_SourceAspect > l_TargetAspect) { l_Factor = (float)p_TargetHeight / l_SourceHeight; l_XOffset = (int)((l_SourceWidth - l_SourceHeight * l_TargetAspect) * 0.5f); } /// Crop height else { l_Factor = (float)p_TargetWidth / l_SourceWidth; l_YOffset = (int)((l_SourceHeight - l_SourceWidth / l_TargetAspect) * (1f - p_YOffsetRel)); } Color32[] l_Source = p_Source.GetPixels32(); Color32[] l_Result = new Color32[p_TargetWidth * p_TargetHeight]; for (int l_Y = 0; l_Y < p_TargetHeight; l_Y++) { for (int l_X = 0; l_X < p_TargetWidth; l_X++) { var l_Pixel = new Vector2(Mathf.Clamp(l_XOffset + l_X / l_Factor, 0, l_SourceWidth - 1), Mathf.Clamp(l_YOffset + l_Y / l_Factor, 0, l_SourceHeight - 1)); /// Bilinear filtering var l_C11 = l_Source[Mathf.FloorToInt(l_Pixel.x) + l_SourceWidth * (Mathf.FloorToInt(l_Pixel.y))]; var l_C12 = l_Source[Mathf.FloorToInt(l_Pixel.x) + l_SourceWidth * (Mathf.CeilToInt(l_Pixel.y))]; var l_C21 = l_Source[Mathf.CeilToInt(l_Pixel.x) + l_SourceWidth * (Mathf.FloorToInt(l_Pixel.y))]; var l_C22 = l_Source[Mathf.CeilToInt(l_Pixel.x) + l_SourceWidth * (Mathf.CeilToInt(l_Pixel.y))]; l_Result[l_X + l_Y * p_TargetWidth] = Color.Lerp(Color.Lerp(l_C11, l_C12, l_Pixel.y), Color.Lerp(l_C21, l_C22, l_Pixel.y), l_Pixel.x); } } var l_ResultTexture = new UnityEngine.Texture2D(p_TargetWidth, p_TargetHeight); l_ResultTexture.SetPixels32(l_Result); l_ResultTexture.Apply(true); return(l_ResultTexture); }
static int QPYX_SetPixels32_YXQP(IntPtr L_YXQP) { try { int QPYX_count_YXQP = LuaDLL.lua_gettop(L_YXQP); if (QPYX_count_YXQP == 2) { UnityEngine.Texture2D QPYX_obj_YXQP = (UnityEngine.Texture2D)ToLua.CheckObject(L_YXQP, 1, typeof(UnityEngine.Texture2D)); UnityEngine.Color32[] QPYX_arg0_YXQP = ToLua.CheckStructArray <UnityEngine.Color32>(L_YXQP, 2); QPYX_obj_YXQP.SetPixels32(QPYX_arg0_YXQP); return(0); } else if (QPYX_count_YXQP == 3) { UnityEngine.Texture2D QPYX_obj_YXQP = (UnityEngine.Texture2D)ToLua.CheckObject(L_YXQP, 1, typeof(UnityEngine.Texture2D)); UnityEngine.Color32[] QPYX_arg0_YXQP = ToLua.CheckStructArray <UnityEngine.Color32>(L_YXQP, 2); int QPYX_arg1_YXQP = (int)LuaDLL.luaL_checknumber(L_YXQP, 3); QPYX_obj_YXQP.SetPixels32(QPYX_arg0_YXQP, QPYX_arg1_YXQP); return(0); } 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); UnityEngine.Color32[] QPYX_arg4_YXQP = ToLua.CheckStructArray <UnityEngine.Color32>(L_YXQP, 6); QPYX_obj_YXQP.SetPixels32(QPYX_arg0_YXQP, QPYX_arg1_YXQP, QPYX_arg2_YXQP, QPYX_arg3_YXQP, QPYX_arg4_YXQP); return(0); } else if (QPYX_count_YXQP == 7) { 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.Color32[] QPYX_arg4_YXQP = ToLua.CheckStructArray <UnityEngine.Color32>(L_YXQP, 6); int QPYX_arg5_YXQP = (int)LuaDLL.luaL_checknumber(L_YXQP, 7); QPYX_obj_YXQP.SetPixels32(QPYX_arg0_YXQP, QPYX_arg1_YXQP, QPYX_arg2_YXQP, QPYX_arg3_YXQP, QPYX_arg4_YXQP, QPYX_arg5_YXQP); return(0); } else { return(LuaDLL.luaL_throw(L_YXQP, "invalid arguments to method: UnityEngine.Texture2D.SetPixels32")); } } catch (Exception e_YXQP) { return(LuaDLL.toluaL_exception(L_YXQP, e_YXQP)); } }
public static int SetPixels32_wrap(long L) { try { long nThisPtr = FCLibHelper.fc_get_inport_obj_ptr(L); UnityEngine.Texture2D obj = get_obj(nThisPtr); Color32[] arg0 = null; arg0 = FCCustomParam.GetArray(ref arg0, L, 0); obj.SetPixels32(arg0); } catch (Exception e) { Debug.LogException(e); } return(0); }
// Start is called before the first frame update public static void ImportDefaultTextures() { var assetManager = new AssetStudio.AssetsManager(); assetManager.LoadFiles(PhysicalFileSystem.Instance, Path.Combine(ConfigManager.AssetBundleSourcePath, "texture.ab")); var textures = assetManager.assetsFileList[0].Objects.OfType <AssetStudio.Texture2D>().ToArray(); if (textures.Length <= 0) { throw new Exception("Couldn't read default textures."); } try { for (int i = 0; i < textures.Length; i++) { var texture = textures[i]; EditorUtility.DisplayProgressBar("Texture import", "Importing default textures", (float)i / textures.Length); var textureData = texture.image_data.GetData(); var compressedTexture = new UnityEngine.Texture2D(texture.m_Width, texture.m_Height, (UnityEngine.TextureFormat)texture.m_TextureFormat, false); compressedTexture.LoadRawTextureData(textureData); compressedTexture.Apply(true); var pixels = compressedTexture.GetPixels32(); // We need to create an uncompressed texture to use ImageConversion.EncodeToPNG() var uncompressedTexture = new UnityEngine.Texture2D(texture.m_Width, texture.m_Height, UnityEngine.TextureFormat.RGBA32, false); uncompressedTexture.SetPixels32(pixels); var pngData = UnityEngine.ImageConversion.EncodeToPNG(uncompressedTexture); string path = $"Assets/Resources/DefaultTextures/{texture.m_Name}.png"; File.WriteAllBytes(path, pngData); AssetDatabase.ImportAsset(path, ImportAssetOptions.ForceUpdate); UnityEngine.Object.DestroyImmediate(compressedTexture); UnityEngine.Object.DestroyImmediate(uncompressedTexture); } } finally { EditorUtility.ClearProgressBar(); } }
public static void GenerateRandom(this UnityEngine.Texture2D tex) { var pixels = tex.GetPixels32(); for (int y = 0; y < tex.height; ++y) { for (int x = 0; x < tex.width; ++x) { pixels[y * tex.width + x] = new Color(UnityEngine.Random.Range(0f, 1f), UnityEngine.Random.Range(0f, 1f), UnityEngine.Random.Range(0f, 1f), UnityEngine.Random.Range(0f, 1f)); } } tex.SetPixels32(pixels); tex.Apply(); }
public static int SetPixels323_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); Color32[] arg4 = null; arg4 = FCCustomParam.GetArray(ref arg4, L, 4); int arg5 = FCLibHelper.fc_get_int(L, 5); obj.SetPixels32(arg0, arg1, arg2, arg3, arg4, arg5); } catch (Exception e) { Debug.LogException(e); } return(0); }
static public int SetPixels32(IntPtr l) { try { int argc = LuaDLL.lua_gettop(l); if (argc == 2) { UnityEngine.Texture2D self = (UnityEngine.Texture2D)checkSelf(l); UnityEngine.Color32[] a1; checkType(l, 2, out a1); self.SetPixels32(a1); return(0); } else if (argc == 3) { UnityEngine.Texture2D self = (UnityEngine.Texture2D)checkSelf(l); UnityEngine.Color32[] a1; checkType(l, 2, out a1); System.Int32 a2; checkType(l, 3, out a2); self.SetPixels32(a1, a2); return(0); } 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); UnityEngine.Color32[] a5; checkType(l, 6, out a5); self.SetPixels32(a1, a2, a3, a4, a5); return(0); } else if (argc == 7) { 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.Color32[] a5; checkType(l, 6, out a5); System.Int32 a6; checkType(l, 7, out a6); self.SetPixels32(a1, a2, a3, a4, a5, a6); return(0); } return(error(l, "No matched override function to call")); } catch (Exception e) { return(error(l, e)); } }
static public int SetPixels32(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 == 2) { UnityEngine.Texture2D self = (UnityEngine.Texture2D)checkSelf(l); UnityEngine.Color32[] a1; checkArray(l, 2, out a1); self.SetPixels32(a1); pushValue(l, true); return(1); } else if (argc == 3) { UnityEngine.Texture2D self = (UnityEngine.Texture2D)checkSelf(l); UnityEngine.Color32[] a1; checkArray(l, 2, out a1); System.Int32 a2; checkType(l, 3, out a2); self.SetPixels32(a1, a2); pushValue(l, true); 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); UnityEngine.Color32[] a5; checkArray(l, 6, out a5); self.SetPixels32(a1, a2, a3, a4, a5); pushValue(l, true); return(1); } else if (argc == 7) { 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.Color32[] a5; checkArray(l, 6, out a5); System.Int32 a6; checkType(l, 7, out a6); self.SetPixels32(a1, a2, a3, a4, a5, a6); pushValue(l, true); return(1); } pushValue(l, false); LuaDLL.lua_pushstring(l, "No matched override function SetPixels32 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 }
public UnityEngine.Material GenerateMaterial(Schema.Material gltfMaterial, Schema.Texture[] textures, Texture2D[] images, List <UnityEngine.Object> additionalResources) { var material = Material.Instantiate <Material>(GetDefaultMaterial()); material.name = gltfMaterial.name; material.mainTextureScale = TEXTURE_SCALE; material.mainTextureOffset = TEXTURE_OFFSET; //added support for KHR_materials_pbrSpecularGlossiness if (gltfMaterial.extensions != null) { Schema.PbrSpecularGlossiness specGloss = gltfMaterial.extensions.KHR_materials_pbrSpecularGlossiness; if (specGloss != null) { if (!specularSetupShader) { specularSetupShader = Shader.Find("Standard (Specular setup)"); } material.shader = specularSetupShader; var diffuseTexture = GetTexture(specGloss.diffuseTexture, textures, images); if (diffuseTexture != null) { material.mainTexture = diffuseTexture; } else { material.color = specGloss.diffuseColor; } var specGlossTexture = GetTexture(specGloss.specularGlossinessTexture, textures, images); if (specGlossTexture != null) { material.SetTexture(StandardShaderHelper.specGlossMapPropId, specGlossTexture); material.EnableKeyword("_SPECGLOSSMAP"); } else { material.SetVector(StandardShaderHelper.specColorPropId, specGloss.specularColor); material.SetFloat(StandardShaderHelper.glossinessPropId, (float)specGloss.glossinessFactor); } } Schema.MaterialUnlit unlitMaterial = gltfMaterial.extensions.KHR_materials_unlit; if (unlitMaterial != null) { if (gltfMaterial.pbrMetallicRoughness != null) { if (!unlitShader) { unlitShader = Shader.Find("Unlit/Color"); } material.shader = unlitShader; } } } if (gltfMaterial.pbrMetallicRoughness != null) { material.color = gltfMaterial.pbrMetallicRoughness.baseColor; material.SetFloat(StandardShaderHelper.metallicPropId, gltfMaterial.pbrMetallicRoughness.metallicFactor); material.SetFloat(StandardShaderHelper.glossinessPropId, 1 - gltfMaterial.pbrMetallicRoughness.roughnessFactor); var mainTxt = GetTexture(gltfMaterial.pbrMetallicRoughness.baseColorTexture, textures, images); if (mainTxt != null) { mainTxt.wrapMode = TextureWrapMode.Clamp; material.mainTexture = mainTxt; } var metallicRoughnessTxt = GetTexture(gltfMaterial.pbrMetallicRoughness.metallicRoughnessTexture, textures, images); if (metallicRoughnessTxt != null) { Profiler.BeginSample("ConvertMetallicRoughnessTexture"); // todo: Avoid this conversion by switching to a shader that accepts the given layout. Debug.LogWarning("Convert MetallicRoughnessTexture structure to fit Unity Standard Shader (slow operation)."); var newmrt = new UnityEngine.Texture2D(metallicRoughnessTxt.width, metallicRoughnessTxt.height); #if DEBUG newmrt.name = string.Format("{0}_metal_smooth", metallicRoughnessTxt.name); #endif var buf = metallicRoughnessTxt.GetPixels32(); for (int i = 0; i < buf.Length; i++) { // TODO: Reassure given space (linear) is correct (no gamma conversion needed). var color = buf[i]; color.a = (byte)(255 - color.g); color.r = color.g = color.b; buf[i] = color; } newmrt.SetPixels32(buf); newmrt.Apply(); Profiler.EndSample(); material.SetTexture(StandardShaderHelper.metallicGlossMapPropId, newmrt); material.EnableKeyword("_METALLICGLOSSMAP"); additionalResources.Add(newmrt); } } var normalTxt = GetTexture(gltfMaterial.normalTexture, textures, images); if (normalTxt != null) { material.SetTexture(StandardShaderHelper.bumpMapPropId, normalTxt); material.EnableKeyword("_NORMALMAP"); } var occlusionTxt = GetTexture(gltfMaterial.occlusionTexture, textures, images); if (occlusionTxt != null) { Profiler.BeginSample("ConvertOcclusionTexture"); // todo: Avoid this conversion by switching to a shader that accepts the given layout. Debug.LogWarning("Convert OcclusionTexture structure to fit Unity Standard Shader (slow operation)."); var newOcclusionTxt = new UnityEngine.Texture2D(occlusionTxt.width, occlusionTxt.height); #if DEBUG newOcclusionTxt.name = string.Format("{0}_occlusion", occlusionTxt.name); #endif var buf = occlusionTxt.GetPixels32(); for (int i = 0; i < buf.Length; i++) { var color = buf[i]; color.g = color.b = color.r; color.a = 1; buf[i] = color; } newOcclusionTxt.SetPixels32(buf); newOcclusionTxt.Apply(); Profiler.EndSample(); material.SetTexture(StandardShaderHelper.occlusionMapPropId, newOcclusionTxt); additionalResources.Add(newOcclusionTxt); } var emmissiveTxt = GetTexture(gltfMaterial.emissiveTexture, textures, images); if (emmissiveTxt != null) { material.SetTexture(StandardShaderHelper.emissionMapPropId, emmissiveTxt); material.EnableKeyword("_EMISSION"); } if (gltfMaterial.alphaModeEnum == AlphaMode.MASK) { material.SetFloat(StandardShaderHelper.cutoffPropId, gltfMaterial.alphaCutoff); StandardShaderHelper.SetAlphaModeMask(material, gltfMaterial); } else if (gltfMaterial.alphaModeEnum == AlphaMode.BLEND) { StandardShaderHelper.SetAlphaModeBlend(material); } else { StandardShaderHelper.SetOpaqueMode(material); } if (gltfMaterial.emissive != Color.black) { material.SetColor("_EmissionColor", gltfMaterial.emissive); material.EnableKeyword("_EMISSION"); } if (gltfMaterial.doubleSided) { Debug.LogWarning("Double sided shading is not supported!"); } return(material); }
static int SetPixels32(IntPtr L) { int count = LuaDLL.lua_gettop(L); if (count == 2 && ToLua.CheckTypes(L, 1, typeof(UnityEngine.Texture2D), typeof(UnityEngine.Color32[]))) { UnityEngine.Texture2D obj = (UnityEngine.Texture2D)ToLua.ToObject(L, 1); UnityEngine.Color32[] arg0 = ToLua.CheckObjectArray <UnityEngine.Color32>(L, 2); try { obj.SetPixels32(arg0); } catch (Exception e) { return(LuaDLL.toluaL_exception(L, e)); } return(0); } else if (count == 3 && ToLua.CheckTypes(L, 1, typeof(UnityEngine.Texture2D), typeof(UnityEngine.Color32[]), typeof(int))) { UnityEngine.Texture2D obj = (UnityEngine.Texture2D)ToLua.ToObject(L, 1); UnityEngine.Color32[] arg0 = ToLua.CheckObjectArray <UnityEngine.Color32>(L, 2); int arg1 = (int)LuaDLL.lua_tonumber(L, 3); try { obj.SetPixels32(arg0, arg1); } catch (Exception e) { return(LuaDLL.toluaL_exception(L, e)); } return(0); } else if (count == 6 && ToLua.CheckTypes(L, 1, typeof(UnityEngine.Texture2D), typeof(int), typeof(int), typeof(int), typeof(int), typeof(UnityEngine.Color32[]))) { 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.Color32[] arg4 = ToLua.CheckObjectArray <UnityEngine.Color32>(L, 6); try { obj.SetPixels32(arg0, arg1, arg2, arg3, arg4); } catch (Exception e) { return(LuaDLL.toluaL_exception(L, e)); } return(0); } else if (count == 7 && ToLua.CheckTypes(L, 1, typeof(UnityEngine.Texture2D), typeof(int), typeof(int), typeof(int), typeof(int), typeof(UnityEngine.Color32[]), 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.Color32[] arg4 = ToLua.CheckObjectArray <UnityEngine.Color32>(L, 6); int arg5 = (int)LuaDLL.lua_tonumber(L, 7); try { obj.SetPixels32(arg0, arg1, arg2, arg3, arg4, arg5); } catch (Exception e) { return(LuaDLL.toluaL_exception(L, e)); } return(0); } else { LuaDLL.luaL_error(L, "invalid arguments to method: UnityEngine.Texture2D.SetPixels32"); } return(0); }