/// <summary> /// Load subtexture from pixel array /// Warning: this method doesn't support automatic texture reload after restoring graphics context /// </summary> public void LoadSubImage(Color4[] pixels, int x, int y, int width, int height) { // This mindblowing code: // 1) Rearranges pixels in bottom to top layout; // 2) Clips pixels outside the texture rectangle. y = unityTexture.height - y - height; var x0 = Math.Max(0, x); var y0 = Math.Max(0, y); var x1 = Math.Min(unityTexture.width, x + width); var y1 = Math.Min(unityTexture.height, y + height); if (x1 <= x0 || y1 <= y0) { return; } var stride = width; width = x1 - x0; height = y1 - y0; var c = new UnityEngine.Color32[width * height]; int k = 0; for (var i = y1 - 1; i >= y0; i--) { var p = (i - y) * stride + (x0 - x); for (var j = x0; j < x1; j++) { c[k].a = pixels[p].A; c[k].r = pixels[p].R; c[k].g = pixels[p].G; c[k].b = pixels[p].B; k++; p++; } } unityTexture.SetPixels32(x0, y0, width, height, c); unityTexture.Apply(); }
/// <summary> /// Render the current slide of pptview.exe into a Unity Texture2D /// NOTE: this method will resize the texture if it's not the correct size/format /// </summary> /// <param name="texture">texture to receive pptview.exe's window pixels</param> public void Render(ref UnityEngine.Texture2D texture) { int width = 0, height = 0; if (!Render(ref lastRenderedPixels, ref width, ref height)) { return; } if (texture.width != width || texture.height != height || texture.format != UnityEngine.TextureFormat.BGRA32) { texture.Resize(width, height, UnityEngine.TextureFormat.BGRA32, false); } // NOTE: this method is ONLY for debugging purposes. If you actually // need to render to a Texture2D faster you should use the new shiny // LoadRawTextureData(IntPtr data, int size) overload! texture.LoadRawTextureData(lastRenderedPixels); texture.Apply(); }
/// <summary> /// Draws the object on the specified <see cref="UnityEngine.Texture2D"/>. /// </summary> /// <param name="texture">The texture on which to draw the object.</param> /// <param name="xLeft">The X axis lower bound.</param> /// <param name="xRight">The X axis upper bound.</param> /// <param name="yBottom">The Y axis lower bound.</param> /// <param name="yTop">The Y axis upper bound.</param> public override void Draw(ref UnityEngine.Texture2D texture, float xLeft, float xRight, float yBottom, float yTop) { if (!Visible) { return; } float xRange = xRight - xLeft; float yRange = yTop - yBottom; int width = texture.width; int height = texture.height; int[] xPix, yPix; // TODO: Add robustness for NaNs and Infinities. if (!Transpose) { xPix = _values.Select(vect => UnityEngine.Mathf.RoundToInt((vect.x - xLeft) / xRange * width)).ToArray(); yPix = _values.Select(vect => UnityEngine.Mathf.RoundToInt((vect.y - yBottom) / yRange * height)).ToArray(); } else { xPix = _values.Select(vect => UnityEngine.Mathf.RoundToInt((vect.y - yBottom) / yRange * width)).ToArray(); yPix = _values.Select(vect => UnityEngine.Mathf.RoundToInt((vect.x - xLeft) / xRange * height)).ToArray(); } for (int i = _values.Length - 2; i >= 0; i--) { DrawingHelper.DrawLine(ref texture, xPix[i], yPix[i], xPix[i + 1], yPix[i + 1], this.Color[ColorFunc(xPix[i], yPix[i], _values[i].z)], this.Color[ColorFunc(xPix[i + 1], yPix[i + 1], _values[i + 1].z)]); for (int w = 2; w <= LineWidth; w++) { int l = w % 2 == 0 ? (-w) >> 1 : (w - 1) >> 1; DrawingHelper.DrawLine(ref texture, xPix[i] + l, yPix[i], xPix[i + 1] + l, yPix[i + 1], this.Color[ColorFunc(xPix[i], yPix[i], _values[i].z)], this.Color[ColorFunc(xPix[i + 1], yPix[i + 1], _values[i + 1].z)]); DrawingHelper.DrawLine(ref texture, xPix[i], yPix[i] + l, xPix[i + 1], yPix[i + 1] + l, this.Color[ColorFunc(xPix[i], yPix[i], _values[i].z)], this.Color[ColorFunc(xPix[i + 1], yPix[i + 1], _values[i + 1].z)]); } } texture.Apply(); }
public override void Draw(ref UnityEngine.Texture2D texture, float xLeft, float xRight, float yBottom, float yTop) { int width = texture.width - 1; int height = texture.height - 1; float graphStepX = (xRight - xLeft) / width; float graphStepY = (yTop - yBottom) / height; for (int x = 0; x <= width; x++) { for (int y = 0; y <= height; y++) { float xF = x * graphStepX + xLeft; float yF = y * graphStepY + yBottom; if (xF < XMin || xF > XMax || yF < YMin || yF > YMax) { continue; } texture.SetPixel(x, y, this.Color[ColorFunc(xF, yF, ValueAt(xF, yF) * ZAxisScaler)]); } } texture.Apply(); }
public override void Apply() { unityTexture.Apply(); }
private void FeelingMonitorPanel(int id) { Dictionary<string, float> feelingValueMap = _connector.FeelingValueMap; if(feelingValueMap.Count == 0) { return; } _scrollPosition = UnityEngine.GUILayout.BeginScrollView(_scrollPosition); float feelingBarWidth = UnityEngine.Screen.width * 0.3f; _boxStyle = _panelSkin.box; lock(feelingValueMap) { int topOffset = 5; foreach(string feeling in feelingValueMap.Keys) { if(!_isFeelingTextureMapInitialized) { float r = UnityEngine.Random.value; float g = UnityEngine.Random.value; float b = UnityEngine.Random.value; UnityEngine.Color c = new UnityEngine.Color(r, g, b, 0.6f); UnityEngine.Texture2D t = new UnityEngine.Texture2D(1, 1); t.SetPixel(0, 0, c); t.Apply(); _feelingTextureMap[feeling] = t; } float value = feelingValueMap[feeling]; // Set the texture of background. _boxStyle.normal.background = _feelingTextureMap[feeling]; UnityEngine.GUILayout.BeginHorizontal(); UnityEngine.GUILayout.Label(feeling + ": ", _panelSkin.label, UnityEngine.GUILayout.MaxWidth(_panel.width * 0.3f)); UnityEngine.GUILayout.Box("", _boxStyle, UnityEngine.GUILayout.Width(feelingBarWidth * value), UnityEngine.GUILayout.Height(16)); UnityEngine.GUILayout.EndHorizontal(); topOffset += 15; } // We only need to initialize the map at the first time. if(!_isFeelingTextureMapInitialized) { _isFeelingTextureMapInitialized = true; } } UnityEngine.GUILayout.EndScrollView(); }
public void Bake() { var tSize = TextureSize; _bakedTexture = new Bitmap(tSize.Width, tSize.Height); // Clear baked texture with transparent color. for (int y = 0; y < _bakedTexture.Height; y++) { for (int x = 0; x < _bakedTexture.Width; x++) { _bakedTexture.SetPixel(x, y, Color.Transparent); } } int xOffset = 0; for (int i = 0; i < Text.Length; i++) { if (_font.textureList.ContainsKey(Text[i]) == false) { continue; } var textC = _font.textureList[Text[i]]; if (textC == null) { continue; } float _scale = _charSettings[i].Scale; Color _foreColor = _charSettings[i].ForeColor; float cX = xOffset + textC.OffsetX * _scale; float cY = GetCursor() - textC.OffsetY * _scale; float cW = textC.Texture.Width * _scale; float cH = textC.Texture.Height * _scale; float cA = textC.Advance * _scale; if (cW <= 0 || cH <= 0) { // Skip } else { var origPixels = textC.Texture.uTexture.GetPixels32(); var newTexture = new UnityEngine.Texture2D(textC.Texture.Width, textC.Texture.Height); newTexture.name = "backedGlyphTexture"; newTexture.SetPixels32(origPixels); newTexture.Apply(); // Scale texture if needed. if ((int)cW != newTexture.width || (int)cH != newTexture.height) { TextureScaler.scale(newTexture, (int)cW, (int)cH); //TextureScale.Bilinear(newTexture, (int)cW, (int)cH); newTexture.Apply(); } var newImagePixels = newTexture.GetPixels32(); for (int p = 0; p < newImagePixels.Length; p++) { // BlendMode: Multiply var origColor = Color.FromUColor(newImagePixels[p]); var blendColorA = (float)(origColor.A * _foreColor.A) / 255; var blendColorR = (float)(origColor.R * _foreColor.R) / 255; var blendColorG = (float)(origColor.G * _foreColor.G) / 255; var blendColorB = (float)(origColor.B * _foreColor.B) / 255; var blendColor = Color.FromArgb((int)blendColorA, (int)blendColorR, (int)blendColorG, (int)blendColorB); newImagePixels[p] = blendColor.ToUColor(); } _bakedTexture.uTexture.SetPixels32((int)cX, (int)cY, newTexture.width, newTexture.height, newImagePixels); } xOffset += (int)cA; } _bakedTexture.Apply(); if (AutoSize) { Size = tSize; } }
public unsafe UnityEngine.Texture2D RenderIntoTexture(char c) { int code; FT.FT_FaceRec facerec = FT.HandleToRecord<FT.FT_FaceRec>(face_); IntPtr slot = facerec.glyph; code = FT.FT_Load_Char(face_, c, FT.FT_LOAD_RENDER/*|FT.FT_LOAD_TARGET_NORMAL*/); if (code != 0) { return null; } FT.FT_GlyphSlotRec slotrec = FT.HandleToRecord<FT.FT_GlyphSlotRec>(slot); FTSharp.FT.FT_Bitmap ftbm=slotrec.bitmap; UnityEngine.Texture2D texbuffer=new UnityEngine.Texture2D(ftbm.width,ftbm.rows,UnityEngine.TextureFormat.ARGB32,false); if (((int)ftbm.pixel_mode)!=2) { UnityEngine.Debug.LogError("Unsupported bitmap depth :"+((int)ftbm.pixel_mode).ToString()); return null; } // FT_PIXEL_MODE_MONO, //FT_PIXEL_MODE_GRAY, UnityEngine.Color32 [] clrs=texbuffer.GetPixels32(); int i=0; // UNSAFE DOES NOT WORK IN THE WEB PLAYER ! for (int y=0;y<ftbm.rows;y++) { int j=(((ftbm.rows-(1+y))*ftbm.pitch)); byte * bo=((byte *)ftbm.buffer); for (int x=0;x<ftbm.width;x++) { clrs[i].a=clrs[i].r=clrs[i].g=clrs[i].b=(bo[j+x]); i++; } } texbuffer.SetPixels32(clrs); texbuffer.Apply(); return texbuffer; }
public void Bake() { var tSize = TextureSize; _bakedTexture = new Bitmap(tSize.Width, tSize.Height); // Clear baked texture with transparent color. for (int y = 0; y < _bakedTexture.Height; y++) for (int x = 0; x < _bakedTexture.Width; x++) _bakedTexture.SetPixel(x, y, Color.Transparent); int xOffset = 0; for (int i = 0; i < Text.Length; i++) { if (_font.textureList.ContainsKey(Text[i]) == false) continue; var textC = _font.textureList[Text[i]]; if (textC == null) continue; float _scale = _charSettings[i].Scale; Color _foreColor = _charSettings[i].ForeColor; float cX = xOffset + textC.OffsetX * _scale; float cY = GetCursor() - textC.OffsetY * _scale; float cW = textC.Texture.Width * _scale; float cH = textC.Texture.Height * _scale; float cA = textC.Advance * _scale; if (cW <= 0 || cH <= 0) { // Skip } else { var origPixels = textC.Texture.uTexture.GetPixels32(); var newTexture = new UnityEngine.Texture2D(textC.Texture.Width, textC.Texture.Height); newTexture.name = "backedGlyphTexture"; newTexture.SetPixels32(origPixels); newTexture.Apply(); // Scale texture if needed. if ((int)cW != newTexture.width || (int)cH != newTexture.height) { TextureScaler.scale(newTexture, (int)cW, (int)cH); //TextureScale.Bilinear(newTexture, (int)cW, (int)cH); newTexture.Apply(); } var newImagePixels = newTexture.GetPixels32(); for (int p = 0; p < newImagePixels.Length; p++) { // BlendMode: Multiply var origColor = Color.FromUColor(newImagePixels[p]); var blendColorA = (float)(origColor.A * _foreColor.A) / 255; var blendColorR = (float)(origColor.R * _foreColor.R) / 255; var blendColorG = (float)(origColor.G * _foreColor.G) / 255; var blendColorB = (float)(origColor.B * _foreColor.B) / 255; var blendColor = Color.FromArgb((int)blendColorA, (int)blendColorR, (int)blendColorG, (int)blendColorB); newImagePixels[p] = blendColor.ToUColor(); } _bakedTexture.uTexture.SetPixels32((int)cX, (int)cY, newTexture.width, newTexture.height, newImagePixels); } xOffset += (int)cA; } _bakedTexture.Apply(); if (AutoSize) Size = tSize; }
protected override void OnUpdate() { Entities.WithAll <ChunkMap>().ForEach((Entity e, ref ChunkMap chunkMap) => { if (chunkMap.dirty == 2) { // create texture UnityEngine.Texture2D mapTexture = new UnityEngine.Texture2D( chunkMap.width, chunkMap.height, UnityEngine.Experimental.Rendering.DefaultFormat.LDR, //UnityEngine.Experimental.Rendering.DefaultFormat.HDR, UnityEngine.Experimental.Rendering.TextureCreationFlags.None); if (mapTexture == null) { World.EntityManager.RemoveComponent <ChunkMap>(e); //UnityEngine.Debug.LogError("Map Texture [" + chunkMap.chunkPosition + "] created for chunk was null."); return; } mapTexture.filterMode = UnityEngine.FilterMode.Point; UnityEngine.Color[] pixels = new UnityEngine.Color[chunkMap.width * chunkMap.height]; mapTexture.name = "ChunkMap_" + chunkMap.chunkPosition.x + "_" + chunkMap.chunkPosition.y + "_" + chunkMap.chunkPosition.z; int xzIndex = 0; for (int i = 0; i < chunkMap.width; i++) { for (int j = 0; j < chunkMap.height; j++) { //int xzIndex = i + j * chunkMap.height; byte voxel = chunkMap.topVoxels[xzIndex]; if (voxel == 0) { pixels[xzIndex] = UnityEngine.Color.black; } else if (voxel == 1) { pixels[xzIndex] = UnityEngine.Color.green; } else if (voxel == 2) { pixels[xzIndex] = UnityEngine.Color.red; } else if (voxel == 3) { pixels[xzIndex] = UnityEngine.Color.blue; } else if (voxel == 4) { pixels[xzIndex] = UnityEngine.Color.yellow; } else { pixels[xzIndex] = UnityEngine.Color.magenta; } int height = (int)chunkMap.heights[xzIndex]; //float colorBrightness = 3 * ((float)height / (float)chunkMap.highestHeight); pixels[xzIndex] *= ((float)height / 64); //math.min(1, colorBrightness); xzIndex++; } } mapTexture.SetPixels(pixels); mapTexture.Apply(); //Bootstrap.instance.debugMaps.Add(mapTexture); float2 mapPosition = new float2(chunkMap.chunkPosition.x, chunkMap.chunkPosition.z); if (maps.ContainsKey(mapPosition)) { if (maps[mapPosition] != null) { UnityEngine.GameObject.Destroy(maps[mapPosition]); } maps.Remove(mapPosition); } maps.Add(mapPosition, mapTexture); World.EntityManager.RemoveComponent <ChunkMap>(e); } }); }
protected internal override Texture2D Read(ContentReader reader, Texture2D existingInstance) { Texture2D texture = null; SurfaceFormat surfaceFormat; if (reader.version < 5) { SurfaceFormatLegacy legacyFormat = (SurfaceFormatLegacy)reader.ReadInt32(); switch (legacyFormat) { case SurfaceFormatLegacy.Dxt1: surfaceFormat = SurfaceFormat.Dxt1; break; case SurfaceFormatLegacy.Dxt3: surfaceFormat = SurfaceFormat.Dxt3; break; case SurfaceFormatLegacy.Dxt5: surfaceFormat = SurfaceFormat.Dxt5; break; case SurfaceFormatLegacy.Color: surfaceFormat = SurfaceFormat.Color; break; default: throw new NotSupportedException("Unsupported legacy surface format."); } } else { surfaceFormat = (SurfaceFormat)reader.ReadInt32(); } int width = (reader.ReadInt32()); int height = (reader.ReadInt32()); int levelCount = (reader.ReadInt32()); int levelCountOutput = levelCount; SurfaceFormat convertedFormat = surfaceFormat; switch (surfaceFormat) { case SurfaceFormat.Dxt1: case SurfaceFormat.Dxt1a: convertedFormat = SurfaceFormat.Color; break; case SurfaceFormat.Dxt3: case SurfaceFormat.Dxt5: convertedFormat = SurfaceFormat.Color; break; case SurfaceFormat.NormalizedByte4: convertedFormat = SurfaceFormat.Color; break; } UnityEngine.Texture2D unityTexture = new UnityEngine.Texture2D(width, height, XnaToUnity.TextureFormat(convertedFormat), levelCountOutput > 1); for (int level = 0; level < levelCount; level++) { int levelDataSizeInBytes = (reader.ReadInt32()); byte[] levelData = reader.ReadBytes(levelDataSizeInBytes); int levelWidth = width >> level; int levelHeight = height >> level; if (level >= levelCountOutput) { continue; } //Convert the image data if required switch (surfaceFormat) { case SurfaceFormat.Dxt1: case SurfaceFormat.Dxt1a: levelData = DxtUtil.DecompressDxt1(levelData, levelWidth, levelHeight); break; case SurfaceFormat.Dxt3: levelData = DxtUtil.DecompressDxt3(levelData, levelWidth, levelHeight); break; case SurfaceFormat.Dxt5: levelData = DxtUtil.DecompressDxt5(levelData, levelWidth, levelHeight); break; } // un-premultiply alpha (instead do it in the shader) for (int i = 0; i < levelData.Length; i += 4) { float r = levelData[i + 0] / 255.0f; float g = levelData[i + 1] / 255.0f; float b = levelData[i + 2] / 255.0f; float a = levelData[i + 3] / 255.0f; levelData[i + 0] = (byte)(r / a * 255.0f); levelData[i + 1] = (byte)(g / a * 255.0f); levelData[i + 2] = (byte)(b / a * 255.0f); //levelData[i + 0] = 0; //levelData[i + 1] = 255; //levelData[i + 2] = 0; //levelData[i + 3] = 255; } // swap rows because unity textures are laid out bottom-top instead of top-bottom int rowSize = width * 4; byte[] temp = new byte[rowSize]; for (int i = 0; i < levelData.Length / 2; i += rowSize) { for (int j = 0; j < rowSize; j++) { temp[j] = levelData[i + j]; } int p = levelData.Length - (i + rowSize); for (int j = 0; j < rowSize; j++) { levelData[i + j] = levelData[p + j]; } for (int j = 0; j < rowSize; j++) { levelData[p + j] = temp[j]; } } UnityEngine.Color[] unityColors = new UnityEngine.Color[levelData.Length * 4]; unityTexture.SetPixels(XnaToUnity.Color(levelData, ref unityColors), level); unityTexture.Apply(); texture = new Texture2D(unityTexture); } return(texture); }
/** [シングルトン]constructor */ private Instantiate() { //Sprite のグラフィックスとして適用させるテクスチャ。 UnityEngine.Texture2D t_texture = UnityEngine.Texture2D.whiteTexture; { t_texture = new UnityEngine.Texture2D(64, 64); for (int xx = 0; xx < t_texture.width; xx++) { for (int yy = 0; yy < t_texture.height; yy++) { float t_value_x; if (xx < (t_texture.width / 2)) { t_value_x = xx; } else { t_value_x = t_texture.width - 1 - xx; } float t_value_y; if (yy < (t_texture.height / 2)) { t_value_y = yy; } else { t_value_y = t_texture.width - 1 - yy; } float t_value = UnityEngine.Mathf.Min(t_value_x, t_value_y); t_value = UnityEngine.Mathf.Clamp01(t_value * 0.8f); t_texture.SetPixel(xx, yy, new UnityEngine.Color(1.0f, 1.0f, 1.0f, t_value)); } } t_texture.filterMode = UnityEngine.FilterMode.Bilinear; t_texture.wrapMode = UnityEngine.TextureWrapMode.Clamp; t_texture.Apply(); } //Sprite に適用させるテクスチャの Rect 領域。 UnityEngine.Rect t_rect = new UnityEngine.Rect(0.0f, 0.0f, t_texture.width, t_texture.height); //グラフィックスの Rect に対するピボット地点の相対位置。 UnityEngine.Vector2 t_pivot = new UnityEngine.Vector2(t_texture.width / 2, t_texture.height / 2); //ワールド空間座標の 1 単位分に相当する、スプライトのピクセル数。 const float t_pixels_per_unit = 100.0f; //スプライトメッシュが外側に拡張する量。 const uint t_extrude = 0; //スプライトのために生成されるメッシュタイプの制御。 //FullRect : 矩形はメッシュと等しいユーザーが指定したスプライトサイズ。 //Tight : ピクセルのアルファ値を基にしたタイトなメッシュ。多くの余分なピクセルは可能な限りクロップされます。 UnityEngine.SpriteMeshType t_mesh_type = UnityEngine.SpriteMeshType.FullRect; //スプライトのサイズ (X=左、Y=下、Z=右、W=上)。 UnityEngine.Vector4 t_border = new UnityEngine.Vector4(0.0f, 0.0f, 0.0f, 0.0f); //Generates a default physics shape for the sprite. bool t_generate_fallback_physicsshape = true; //UnityEngine.Sprite.Create this.inputfield_sprite = UnityEngine.Sprite.Create(t_texture, t_rect, t_pivot, t_pixels_per_unit, t_extrude, t_mesh_type, t_border, t_generate_fallback_physicsshape); }
public AlphaPicker(int w, int h) { Alpha = 1; Size = new Size(w, h); _image = new UnityEngine.Texture2D(w, h); for (int i = 0; i < w; i++) { int rgb = (int)(((float)i / w) * 255); Color currentColor = Color.FromArgb(rgb, rgb, rgb); for (int k = 0; k < h; k++) { _image.SetPixel(i, k, currentColor.ToUColor()); } } _image.Apply(); Owner.UpClick += Application_UpClick; }
/// <summary> /// Initializes the components of the image to the values specified by the strings. /// </summary> /// <param name="ID">The unique id (if any) of this object.</param> /// <param name="Src">The path (if any) to the image file to load.</param> /// <param name="Colour">The colour tint to apply to the image or the colour fill to use as the image. /// <list type="table"> /// <listheader> /// <term>Src Value</term> /// <term>Colour Value</term> /// <term>Result</term> /// </listheader> /// <item> /// <term>Specified</term> /// <term>Specified</term> /// <term>Image is rendered with Colour tint</term> /// </item> /// <item> /// <term>Specified</term> /// <term>null or empty</term> /// <term>Image is rendered without tint</term> /// </item> /// <item> /// <term>null or empty</term> /// <term>Specified</term> /// <term>Colour is used as fill colour for image</term> /// </item> /// <item> /// <term>null or empty</term> /// <term>null or empty</term> /// <term>Invalid, exception thrown</term> /// </item> /// </list> /// </param> /// <param name="Scale">The scale mode used for rendering the image to the specicified unit rect dimensions: /// <list type="table"> /// <listheader> /// <term>String Value</term> /// <term>Description</term> /// </listheader> /// <item> /// <term>null or empty</term> /// <term>Stretches the texture to fill the complete rectangle passed in to GUI.DrawTexture</term> /// </item> /// <item> /// <term>stretchToFill</term> /// <term>Stretches the texture to fill the complete rectangle passed in to GUI.DrawTexture</term> /// </item> /// <item> /// <term>scaleAndCrop</term> /// <term>Scales the texture, maintaining aspect ratio, so it completely covers the position rectangle passed to GUI.DrawTexture. If the texture is being draw to a rectangle with a different aspect ratio than the original, the image is cropped</term> /// </item> /// <item> /// <term>scaleToFit</term> /// <term>Scales the texture, maintaining aspect ratio, so it completely fits withing the position rectangle passed to GUI.DrawTexture</term> /// </item> /// </list> /// </param> /// <param name="X">The x positioning of the image relative to the parent as governed by the horizontal alignment as follows: /// <list type="table"> /// <listheader> /// <term>Alignment</term> /// <term>X Position</term> /// </listheader> /// <item> /// <term>null or empty</term> /// <term>x units from parent's left edge</term> /// </item> /// <item> /// <term>Left</term> /// <term>x units from parent's left edge</term> /// </item> /// <item> /// <term>Center</term> /// <term>x position is ignored</term> /// </item> /// <item> /// <term>Right</term> /// <term>x units from parent's right edge</term> /// </item> /// </list> /// </param> /// <param name="Y">The y positioning of the image relative to the parent as governed by the vertical alignment as follows: /// <list type="table"> /// <listheader> /// <term>Alignment</term> /// <term>Y Position</term> /// </listheader> /// <item> /// <term>null or empty</term> /// <term>y units from parent's top edge</term> /// </item> /// <item> /// <term>Top</term> /// <term>y units from parent's top edge</term> /// </item> /// <item> /// <term>Center</term> /// <term>y position is ignored</term> /// </item> /// <item> /// <term>Bottom</term> /// <term>y units from parent's bottom edge</term> /// </item> /// </list> /// </param> /// <param name="W">The width of the image as follows: /// <list type="table"> /// <listheader> /// <term>W</term> /// <term>H</term> /// <term>Description</term> /// </listheader> /// <item> /// <term>null or empty</term> /// <term>null or empty</term> /// <term>The image's dimensions (in pixels) will be used</term> /// </item> /// <item> /// <term>null or empty</term> /// <term>Specified</term> /// <term>The width will be calculated according to the specified height and aspect ration of the image</term> /// </item> /// <item> /// <term>Specified</term> /// <term>-n/a-</term> /// <term>The specified width will be used</term> /// </item> /// </list> /// </param> /// <param name="H">THe height of the image as follows: /// <list type="table"> /// <listheader> /// <term>H</term> /// <term>W</term> /// <term>Description</term> /// </listheader> /// <item> /// <term>null or empty</term> /// <term>null or empty</term> /// <term>The image's dimensions (in pixels) will be used</term> /// </item> /// <item> /// <term>null or empty</term> /// <term>Specified</term> /// <term>The height will be calculated according to the specified width and aspect ration of the image</term> /// </item> /// <item> /// <term>Specified</term> /// <term>-n/a-</term> /// <term>The specified height will be used</term> /// </item> /// </list> /// </param> /// <param name="HAlign">The horizontal alignemnt of the image within the parent rect as specified by the following values: /// <list type="table"> /// <listheader> /// <term>String Value</term> /// <term>Alignment.Type</term> /// </listheader> /// <item> /// <term>null or empty</term> /// <term>null</term> /// </item> /// <item> /// <term>left</term> /// <term>Alignment.Type.Left</term> /// </item> /// <item> /// <term>center</term> /// <term>Alignment.Type.Center</term> /// </item> /// <item> /// <term>right</term> /// <term>Alignment.Type.Right</term> /// </item> /// </list> /// </param> /// <param name="VAlign">The horizontal alignemnt of the image within the parent rect as specified by the following values: /// <list type="table"> /// <listheader> /// <term>String Value</term> /// <term>Alignment.Type</term> /// </listheader> /// <item> /// <term>null or empty</term> /// <term>null</term> /// </item> /// <item> /// <term>top</term> /// <term>Alignment.Type.Top</term> /// </item> /// <item> /// <term>center</term> /// <term>Alignment.Type.Center</term> /// </item> /// <item> /// <term>bottom</term> /// <term>Alignment.Type.Bottom</term> /// </item> /// </list> /// </param> /// <example> /// Instantiate an image object with the id "myImage", the path "somePic", no colour tint, default scale, a width of 300 virtual pixels, a height calculated automatically, positioned 10 virtual units down and centered horizontally: /// <code>Image img = new Image("myImage", "somePic", null, 300, null, null, "10", "center", null);</code> /// Instantiate an image object no id, a solid colour fill, default scale, a width of 300 virtual pixels, a height calculated automatically, positioned 10 virtual units down and centered horizontally: /// <code>Image img = new Image(null, null, "rgb(255,0,255)", 300, null, null, "10", "center", null);</code> /// </example> public Image(string ID, string Src, string Colour, string Scale, string X, string Y, string W, string H, string HAlign, string VAlign, string Visible) : base(ID, X, Y, W, H, HAlign, VAlign, Visible) { switch (Scale) { case "": scale = UnityEngine.ScaleMode.StretchToFill; break; case null: scale = UnityEngine.ScaleMode.StretchToFill; break; case "stretchToFill": scale = UnityEngine.ScaleMode.StretchToFill; break; case "scaleAndCrop": scale = UnityEngine.ScaleMode.ScaleAndCrop; break; case "scaleToFit": scale = UnityEngine.ScaleMode.ScaleToFit; break; default: throw new Exception("Image widget has unknow scale mode"); } if ((Colour == null || Colour == "") && (Src == null || Src == "")) { throw new Exception("Image widget has empty or null src string and colour string"); } rgba = TouchGUI.Colour.Parse(Colour); // Build texture from colour if ((Src == null || Src == "") && rgba != null) { img = new UnityEngine.Texture2D(1, 1); img.SetPixel(0, 0, (UnityEngine.Color)rgba); img.Apply(); } // Load texture from file else { img = UnityEngine.Resources.Load(Src) as UnityEngine.Texture2D; } if (img == null) { throw new Exception("Image widget couldn't load " + Src); } // If width and height are null, use the image dimensions if (rect.dim.x.value == null && rect.dim.y.value == null) { rect.dim.x.type = Unit.Type.Pixel; rect.dim.x.value = img.width; rect.dim.y.type = Unit.Type.Pixel; rect.dim.y.value = img.height; } // If width is null, set the width with the image aspect ratio according to the specified height else if (rect.dim.x.value == null) { float aspect = (float)img.width / (float)img.height; rect.dim.x.type = rect.dim.y.type; rect.dim.x.value = (float)rect.dim.y.value * aspect; } // If height is null, set the height with the image aspect ratio according to the specified width else if (rect.dim.y.value == null) { float aspect = (float)img.height / (float)img.width; rect.dim.y.type = rect.dim.x.type; rect.dim.y.value = (float)rect.dim.x.value * aspect; } }
private static void ThreadedScale(UnityEngine.Texture2D tex, int newWidth, int newHeight, bool useBilinear) { texColors = tex.GetPixels(); newColors = new UnityEngine.Color[newWidth * newHeight]; if (useBilinear) { ratioX = 1.0f / ((float)newWidth / (tex.width - 1)); ratioY = 1.0f / ((float)newHeight / (tex.height - 1)); } else { ratioX = ((float)tex.width) / newWidth; ratioY = ((float)tex.height) / newHeight; } w = tex.width; w2 = newWidth; var cores = UnityEngine.Mathf.Min(UnityEngine.SystemInfo.processorCount, newHeight); var slice = newHeight / cores; finishCount = 0; if (mutex == null) { mutex = new System.Threading.Mutex(false); } if (cores > 1) { int i = 0; ThreadData threadData; for (i = 0; i < cores - 1; i++) { threadData = new ThreadData(slice * i, slice * (i + 1)); System.Threading.ParameterizedThreadStart ts = useBilinear ? new System.Threading.ParameterizedThreadStart(BilinearScale) : new System.Threading.ParameterizedThreadStart(PointScale); System.Threading.Thread thread = new System.Threading.Thread(ts); thread.Start(threadData); } threadData = new ThreadData(slice * i, newHeight); if (useBilinear) { BilinearScale(threadData); } else { PointScale(threadData); } while (finishCount < cores) { System.Threading.Thread.Sleep(1); } } else { ThreadData threadData = new ThreadData(0, newHeight); if (useBilinear) { BilinearScale(threadData); } else { PointScale(threadData); } } tex.Resize(newWidth, newHeight); tex.SetPixels(newColors); tex.Apply(); texColors = null; newColors = null; }