public GetPixelBilinear ( float u, float v ) : |
||
u | float | |
v | float | |
return |
private static Vector4 GetChannels(Texture2D tex, float u, float v, int miplevel, int w, int h, float rangeX, float rangeY) { if (tex == null) return Vector4.zero; if (miplevel == 0) { return tex.GetPixelBilinear(u, v); } // Averages the result over the area within the 'pixel' for this mip level // this is similar, but not quite exactly the same as trilinear filtering. Vector4 value = Vector4.zero; for (int x = -miplevel; x < miplevel; x++) { for (int y = -miplevel; y < miplevel; y++) { float um = u + (x * rangeX); float vm = v - (y * rangeY); value += (Vector4) tex.GetPixelBilinear(um, vm); } } int t = miplevel * 2; value /= t * t; return value; }
private static float GetChannel(Texture2D tex, float val, bool linear, float u, float v, int mipLevel, float rangeX, float rangeY, int channel) { float value = 0.0f; bool isAlpha = channel == 3; //TODO: Check format to ensure alpha? if (tex == null) { value = val; } else if (mipLevel == 0) { value = tex.GetPixelBilinear (u, v) [channel]; } else { // Averages the result over the area within the 'pixel' for this mip level // this is similar, but not quite exactly the same as trilinear filtering. for (int x = -mipLevel; x < mipLevel; ++x) { for (int y = -mipLevel; y < mipLevel; ++y) { float um = u + (x * rangeX); float vm = v - (y * rangeY); value += tex.GetPixelBilinear (um, vm) [channel]; } } int t = mipLevel * 2; value /= t * t; } // Gamma correct here // If editor is in linear, texture was not bypassing srgb if (linear && !isAlpha) { value = Mathf.LinearToGammaSpace(value); } return value; }
static public int GetPixelBilinear(IntPtr l) { try { #if DEBUG var method = System.Reflection.MethodBase.GetCurrentMethod(); string methodName = GetMethodName(method); #if UNITY_5_5_OR_NEWER UnityEngine.Profiling.Profiler.BeginSample(methodName); #else Profiler.BeginSample(methodName); #endif #endif UnityEngine.Texture2D self = (UnityEngine.Texture2D)checkSelf(l); System.Single a1; checkType(l, 2, out a1); System.Single a2; checkType(l, 3, out a2); var ret = self.GetPixelBilinear(a1, a2); pushValue(l, true); pushValue(l, ret); return(2); } catch (Exception e) { return(error(l, e)); } #if DEBUG finally { #if UNITY_5_5_OR_NEWER UnityEngine.Profiling.Profiler.EndSample(); #else Profiler.EndSample(); #endif } #endif }
static int GetPixelBilinear(IntPtr L) { try { int count = LuaDLL.lua_gettop(L); if (count == 3) { UnityEngine.Texture2D obj = (UnityEngine.Texture2D)ToLua.CheckObject(L, 1, typeof(UnityEngine.Texture2D)); float arg0 = (float)LuaDLL.luaL_checknumber(L, 2); float arg1 = (float)LuaDLL.luaL_checknumber(L, 3); UnityEngine.Color o = obj.GetPixelBilinear(arg0, arg1); ToLua.Push(L, o); return(1); } else if (count == 4) { UnityEngine.Texture2D obj = (UnityEngine.Texture2D)ToLua.CheckObject(L, 1, typeof(UnityEngine.Texture2D)); float arg0 = (float)LuaDLL.luaL_checknumber(L, 2); float arg1 = (float)LuaDLL.luaL_checknumber(L, 3); int arg2 = (int)LuaDLL.luaL_checknumber(L, 4); UnityEngine.Color o = obj.GetPixelBilinear(arg0, arg1, arg2); ToLua.Push(L, o); return(1); } else { return(LuaDLL.luaL_throw(L, "invalid arguments to method: UnityEngine.Texture2D.GetPixelBilinear")); } } catch (Exception e) { return(LuaDLL.toluaL_exception(L, e)); } }
private void DrawBar(float value, Rect square, Texture2D image) { GUI.DrawTexture(square, backgroundTexture); square.width *= value; GUI.color = image.GetPixelBilinear(value, 0.5f); GUI.DrawTexture(square, foregroundTexture); GUI.color = Color.white; }
public bool Load(Texture2D brushTex, int size) { if (this.m_Brush == brushTex && size == this.m_Size && this.m_Strength != null) { return true; } if (brushTex != null) { float num = (float)size; this.m_Size = size; this.m_Strength = new float[this.m_Size * this.m_Size]; if (this.m_Size > 3) { for (int i = 0; i < this.m_Size; i++) { for (int j = 0; j < this.m_Size; j++) { this.m_Strength[i * this.m_Size + j] = brushTex.GetPixelBilinear(((float)j + 0.5f) / num, (float)i / num).a; } } } else { for (int k = 0; k < this.m_Strength.Length; k++) { this.m_Strength[k] = 1f; } } UnityEngine.Object.DestroyImmediate(this.m_Preview); this.m_Preview = new Texture2D(this.m_Size, this.m_Size, TextureFormat.ARGB32, false); this.m_Preview.hideFlags = HideFlags.HideAndDontSave; this.m_Preview.wrapMode = TextureWrapMode.Repeat; this.m_Preview.filterMode = FilterMode.Point; Color[] array = new Color[this.m_Size * this.m_Size]; for (int l = 0; l < array.Length; l++) { array[l] = new Color(1f, 1f, 1f, this.m_Strength[l]); } this.m_Preview.SetPixels(0, 0, this.m_Size, this.m_Size, array, 0); this.m_Preview.Apply(); if (this.m_BrushProjector == null) { this.CreatePreviewBrush(); } this.m_BrushProjector.material.mainTexture = this.m_Preview; this.m_Brush = brushTex; return true; } this.m_Strength = new float[1]; this.m_Strength[0] = 1f; this.m_Size = 1; return false; }
public bool Load(Texture2D brushTex, int size) { if (((this.m_Brush == brushTex) && (size == this.m_Size)) && (this.m_Strength != null)) { return true; } if (brushTex != null) { float num = size; this.m_Size = size; this.m_Strength = new float[this.m_Size * this.m_Size]; if (this.m_Size > 3) { for (int j = 0; j < this.m_Size; j++) { for (int k = 0; k < this.m_Size; k++) { this.m_Strength[(j * this.m_Size) + k] = brushTex.GetPixelBilinear((k + 0.5f) / num, ((float) j) / num).a; } } } else { for (int m = 0; m < this.m_Strength.Length; m++) { this.m_Strength[m] = 1f; } } UnityEngine.Object.DestroyImmediate(this.m_Preview); this.m_Preview = new Texture2D(this.m_Size, this.m_Size, TextureFormat.ARGB32, false); this.m_Preview.hideFlags = HideFlags.HideAndDontSave; this.m_Preview.wrapMode = TextureWrapMode.Repeat; this.m_Preview.filterMode = UnityEngine.FilterMode.Point; Color[] colors = new Color[this.m_Size * this.m_Size]; for (int i = 0; i < colors.Length; i++) { colors[i] = new Color(1f, 1f, 1f, this.m_Strength[i]); } this.m_Preview.SetPixels(0, 0, this.m_Size, this.m_Size, colors, 0); this.m_Preview.Apply(); if (this.m_BrushProjector == null) { this.CreatePreviewBrush(); } this.m_BrushProjector.material.mainTexture = this.m_Preview; this.m_Brush = brushTex; return true; } this.m_Strength = new float[] { 1f }; this.m_Size = 1; return false; }
/* * http://jon-martin.com/?p=114 */ public Texture2D ScaleTexture(Texture2D source,int targetWidth,int targetHeight) { Texture2D result=new Texture2D(targetWidth,targetHeight,source.format,true); Color[] rpixels=result.GetPixels(0); float incX=((float)1/source.width)*((float)source.width/targetWidth); float incY=((float)1/source.height)*((float)source.height/targetHeight); for(int px=0; px<rpixels.Length; px++) { rpixels[px] = source.GetPixelBilinear(incX*((float)px%targetWidth), incY*((float)Mathf.Floor(px/targetWidth))); } result.SetPixels(rpixels,0); result.Apply(); return result; }
public static void ScaleTexture(Texture2D tex, int width, int height) { var newPixels = new Color[width * height]; for (int y = 0; y < height; ++y) { for (int x = 0; x < width; ++x) { newPixels[y * width + x] = tex.GetPixelBilinear(((float)x) / width, ((float)y) / height); } } tex.Resize(width, height); tex.SetPixels(newPixels); tex.Apply(); }
public static void Resize(Texture2D texture, int width, int height) { Color[] pixelArray = new Color[width * height]; float incX = (1.0f / (float)width); float incY = (1.0f / (float)height); for (int px = 0; px < pixelArray.Length; px++) { pixelArray[px] = texture.GetPixelBilinear(incX * ((float)px % width), incY * ((float)Mathf.Floor(px / width))); } texture.Resize(width, height); texture.SetPixels(pixelArray, 0); texture.Apply(); }
public static void ApplyHeightMap(GameObject sphere, float scaleFactor, Texture2D heightTex, float cutoff = 0f) { MeshFilter filter = sphere.GetComponent<MeshFilter>(); Mesh mesh = filter.mesh; Vector3[] vertices = mesh.vertices; Vector2[] uvs = mesh.uv; Vector3[] normals = mesh.normals; for (int i = 0; i < vertices.Length; i++) { float grayscale = heightTex.GetPixelBilinear(uvs[i].x, uvs[i].y).grayscale; if (grayscale >= cutoff) { vertices[i] += normals[i] * grayscale * scaleFactor; } } filter.mesh.vertices = vertices; }
static int QPYX_GetPixelBilinear_YXQP(IntPtr L_YXQP) { try { ToLua.CheckArgsCount(L_YXQP, 3); UnityEngine.Texture2D QPYX_obj_YXQP = (UnityEngine.Texture2D)ToLua.CheckObject(L_YXQP, 1, typeof(UnityEngine.Texture2D)); float QPYX_arg0_YXQP = (float)LuaDLL.luaL_checknumber(L_YXQP, 2); float QPYX_arg1_YXQP = (float)LuaDLL.luaL_checknumber(L_YXQP, 3); UnityEngine.Color QPYX_o_YXQP = QPYX_obj_YXQP.GetPixelBilinear(QPYX_arg0_YXQP, QPYX_arg1_YXQP); ToLua.Push(L_YXQP, QPYX_o_YXQP); return(1); } catch (Exception e_YXQP) { return(LuaDLL.toluaL_exception(L_YXQP, e_YXQP)); } }
static public int GetPixelBilinear(IntPtr l) { try { UnityEngine.Texture2D self = (UnityEngine.Texture2D)checkSelf(l); System.Single a1; checkType(l, 2, out a1); System.Single a2; checkType(l, 3, out a2); var ret = self.GetPixelBilinear(a1, a2); pushValue(l, ret); return(1); } catch (Exception e) { return(error(l, e)); } }
private static Texture2D ScaleTexture(Texture2D source, int targetWidth, int targetHeight) { Texture2D result = new Texture2D(targetWidth, targetHeight, source.format, false); /*float incX = (1.0f / (float)targetWidth); float incY = (1.0f / (float)targetHeight);*/ for (int i = 0; i < result.height; ++i) { for (int j = 0; j < result.width; ++j) { Color newColor = source.GetPixelBilinear((float)j / (float)result.width, (float)i / (float)result.height); result.SetPixel(j, i, newColor); } } result.Apply(); return result; }
public void Update(Texture2D tex) { Vector3 point = particle.transform.parent.parent.InverseTransformPoint(particle.transform.position).normalized; float u = (float)(.5 + (Mathf.Atan2(point.z, point.x) / (2f * Mathf.PI))); float v = Mathf.Acos(-point.y) / Mathf.PI; Color color = tex.GetPixelBilinear(u, v); MeshFilter filter = particle.GetComponent<MeshFilter>(); Mesh mesh = filter.mesh; mesh.colors = new Color[4] { new Color(color.r, color.g, color.b, color.a), new Color(color.r, color.g, color.b, color.a), new Color(color.r, color.g, color.b, color.a), new Color(color.r, color.g, color.b, color.a) }; }
static int GetPixelBilinear(IntPtr L) { try { ToLua.CheckArgsCount(L, 3); UnityEngine.Texture2D obj = (UnityEngine.Texture2D)ToLua.CheckObject(L, 1, typeof(UnityEngine.Texture2D)); float arg0 = (float)LuaDLL.luaL_checknumber(L, 2); float arg1 = (float)LuaDLL.luaL_checknumber(L, 3); UnityEngine.Color o = obj.GetPixelBilinear(arg0, arg1); ToLua.Push(L, o); return(1); } catch (Exception e) { return(LuaDLL.toluaL_exception(L, e)); } }
public bool Load(Texture2D brushTex, int size) { if ((Object) this.m_Brush == (Object) brushTex && size == this.m_Size && this.m_Strength != null) return true; if ((Object) brushTex != (Object) null) { float num = (float) size; this.m_Size = size; this.m_Strength = new float[this.m_Size * this.m_Size]; if (this.m_Size > 3) { for (int index1 = 0; index1 < this.m_Size; ++index1) { for (int index2 = 0; index2 < this.m_Size; ++index2) this.m_Strength[index1 * this.m_Size + index2] = brushTex.GetPixelBilinear(((float) index2 + 0.5f) / num, (float) index1 / num).a; } } else { for (int index = 0; index < this.m_Strength.Length; ++index) this.m_Strength[index] = 1f; } Object.DestroyImmediate((Object) this.m_Preview); this.m_Preview = new Texture2D(this.m_Size, this.m_Size, TextureFormat.ARGB32, false); this.m_Preview.hideFlags = HideFlags.HideAndDontSave; this.m_Preview.wrapMode = TextureWrapMode.Repeat; this.m_Preview.filterMode = UnityEngine.FilterMode.Point; Color[] colors = new Color[this.m_Size * this.m_Size]; for (int index = 0; index < colors.Length; ++index) colors[index] = new Color(1f, 1f, 1f, this.m_Strength[index]); this.m_Preview.SetPixels(0, 0, this.m_Size, this.m_Size, colors, 0); this.m_Preview.Apply(); if ((Object) this.m_BrushProjector == (Object) null) this.CreatePreviewBrush(); this.m_BrushProjector.material.mainTexture = (Texture) this.m_Preview; this.m_Brush = brushTex; return true; } this.m_Strength = new float[1]; this.m_Strength[0] = 1f; this.m_Size = 1; return false; }
public static int GetPixelBilinear_wrap(long L) { try { long nThisPtr = FCLibHelper.fc_get_inport_obj_ptr(L); UnityEngine.Texture2D obj = get_obj(nThisPtr); float arg0 = FCLibHelper.fc_get_float(L, 0); float arg1 = FCLibHelper.fc_get_float(L, 1); Color ret = obj.GetPixelBilinear(arg0, arg1); long ret_ptr = FCLibHelper.fc_get_return_ptr(L); Color temp_ret = ret; FCLibHelper.fc_set_value_color(ret_ptr, ref temp_ret); } catch (Exception e) { Debug.LogException(e); } return(0); }
static int GetPixelBilinear(IntPtr L) { #if UNITY_EDITOR ToluaProfiler.AddCallRecord("UnityEngine.Texture2D.GetPixelBilinear"); #endif try { ToLua.CheckArgsCount(L, 3); UnityEngine.Texture2D obj = (UnityEngine.Texture2D)ToLua.CheckObject(L, 1, typeof(UnityEngine.Texture2D)); float arg0 = (float)LuaDLL.luaL_checknumber(L, 2); float arg1 = (float)LuaDLL.luaL_checknumber(L, 3); UnityEngine.Color o = obj.GetPixelBilinear(arg0, arg1); ToLua.Push(L, o); return(1); } catch (Exception e) { return(LuaDLL.toluaL_exception(L, e)); } }
private void GenerateNormalsMap(Texture2D horizontals, Texture2D verticals) { // Create the asset if not existing yet if (OutputTexture == null) { OutputTexture = new Texture2D(Mathf.Max(horizontals.width, verticals.width), Mathf.Max(horizontals.height, verticals.height), TextureFormat.RGBA32, false); } // Actual map generation Color[] outputPixels = new Color[OutputTexture.width * OutputTexture.height]; float uNormalized = 0.0f, vNormalized = 0.0f; Color ch, cv; float r, g, b; Vector2 xyNormal; Vector3 normal; for (int v = 0; v < OutputTexture.height; ++v) { vNormalized = (float)v / OutputTexture.height; for (int u = 0; u < OutputTexture.width; ++u) { uNormalized = (float)u / OutputTexture.width; ch = horizontals.GetPixelBilinear(uNormalized, vNormalized); r = ch.r * 2.0f - 1.0f; cv = verticals.GetPixelBilinear(uNormalized, vNormalized); g = cv.g * 2.0f - 1.0f; xyNormal = Vector2.ClampMagnitude(new Vector2(r, g), 0.999f); r = xyNormal.x; g = xyNormal.y; b = (float)Math.Sqrt(1.0f - (double)(r * r) - (double)(g * g)); // z = sqrt (1 - x^2 - y^2) normal = new Vector3(r, g, b).normalized; outputPixels[u + v * OutputTexture.width] = new Color(normal.x * 0.5f + 0.5f, normal.y * 0.5f + 0.5f, normal.z * 0.5f + 0.5f, 1.0f); } } OutputTexture.SetPixels(outputPixels); OutputTexture.Apply(); File.WriteAllBytes(Path.GetDirectoryName(Application.dataPath) + "/Assets/SpriteNormals.png", OutputTexture.EncodeToPNG()); AssetDatabase.ImportAsset("Assets/SpriteNormals.png"); OutputTexture = AssetDatabase.LoadMainAssetAtPath("Assets/SpriteNormals.png") as Texture2D; }
// Rescale a texture // Only supports public static Texture2D RescaleTexture(Texture2D texture, float scale) { // If globalTextureRescale is 0.5 or 0.25, average pixels from the larger image. Otherwise just pick one pixel, and look really bad int niceRescaleK = NiceRescaleK( scale ); bool niceRescale = niceRescaleK != 0; if (texture != null) { int k = niceRescaleK; int srcW = texture.width, srcH = texture.height; int dstW = niceRescale ? ((srcW + k - 1) / k) : (int)(srcW * scale); int dstH = niceRescale ? ((srcH + k - 1) / k) : (int)(srcH * scale); Texture2D dstTex = new Texture2D(dstW, dstH); for (int dstY = 0; dstY < dstH; ++dstY) { for (int dstX = 0; dstX < dstW; ++dstX) { if (niceRescale) { Color sumColor = new Color(0, 0, 0, 0); float w = 0.0f; for (int dy = 0; dy < k; ++dy) { int srcY = dstY * k + dy; if (srcY >= srcH) continue; for (int dx = 0; dx < k; ++dx) { int srcX = dstX * k + dx; if (srcX >= srcW) continue; w += 1.0f; Color srcColor = texture.GetPixel(srcX, srcY); sumColor += srcColor; } } dstTex.SetPixel(dstX, dstY, (w > 0.0f) ? (sumColor * (1.0f / w)) : Color.black); } else { dstTex.SetPixel(dstX, dstY, texture.GetPixelBilinear((float)dstX / (float)dstW, (float)dstY / (float)dstH)); } } } dstTex.Apply(); return dstTex; } else { return null; } }
public static void DrawTexture (Texture2D texture, Rect previewArea, float textureSize, bool useAlpha = false) { int width = (int)textureSize; int height = (int)textureSize; Vector2 blockSize = new Vector2 (previewArea.width / (float)(width), previewArea.height / (float)(height)); for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { float horizontalPercent = (float)x / (textureSize - 1f); float verticalPercent = (float)y / (textureSize - 1f); Rect blockRect = new Rect (previewArea.x + blockSize.x * x, previewArea.y + blockSize.y * y, blockSize.x, blockSize.y); Color blockColor = texture.GetPixelBilinear(horizontalPercent, verticalPercent); if (!useAlpha) { blockColor.a = 1f; } EditorGUI.DrawRect(blockRect, blockColor); } } }
private static Color blend(Texture2D texture,int x, int y,Vector2 tiling, float weight,float maxWeight,bool blendTilling,float width,float height) { Color final = texture.GetPixelBilinear(((float)x/(float)width) * tiling.x,((float)y/(float)height) *tiling.y) * (weight /maxWeight ); if(blendTilling) { final /= 2f; final += texture.GetPixelBilinear((((float)x )/(float)width) * tiling.x * -0.25f,(((float)y )/(float)height) *tiling.y * -0.25f) * (weight /maxWeight )/2f; } return final; }
public static Texture2D resizeTexture(Texture2D oldTexture,float scaleFactor) { int height = Mathf.RoundToInt(oldTexture.height * scaleFactor); int width = Mathf.RoundToInt(oldTexture.width * scaleFactor); if(scaleFactor ==1) { return oldTexture; } if(height * width > 1000000000) { Debug.LogError("you are trying to build a texture with atleast 1billion pixels, this will most likeley crash unity"); return Texture2D.whiteTexture; } Texture2D texture = new Texture2D(height,width); texture.filterMode = FilterMode.Point; texture.wrapMode = TextureWrapMode.Clamp; Color[] pixels = new Color[height * width]; for(int y =0; y < height; y++) { for(int x =0; x < width; x++) { pixels[x + y * width] = oldTexture.GetPixelBilinear((float)x/(float)width,(float)y/(float)height); } } texture.SetPixels(pixels); texture.Apply(); return texture; }
/************************************************************************************************************************************************* Shared functions *************************************************************************************************************************************************/ /// <summary> /// Returns pixels from a texture. /// </summary> /// <returns>The pixels.</returns> /// <param name="image">Image.</param> public static Color32[] GetPixels (Texture2D image) { if (image == null) return null; if (reference && reference.pixelFilterMode==PIXELMODEC.Bilinear) { Color32[] pixels = new Color32[image.width*image.height]; for (int y = 0; y<image.height; y++) { for (int x = 0; x<image.width; x++) { pixels[(y*image.width)+x] = image.GetPixelBilinear((x*1f)/image.width, (y*1f)/image.height); } } return pixels; } else return image.GetPixels32(); }
/// <summary> /// Constructs Mesh data with texture. /// </summary> /// <param name="mesh">Mesh.</param> /// <param name="texture">Texture.</param> /// <param name="scale">Scale.</param> /// <param name="offset">Offset.</param> /// <param name="newStateName">New state name.</param> /// <param name="newStateTransform">New state transform.</param> public void ConstructParticles (Mesh mesh, Texture2D texture, float scale, Vector3 offset, string newStateName, Transform newStateTransform) { position = mesh.vertices; normals = mesh.normals; if (normals==null || normals.Length==0) { normals = new Vector3[position.Length]; for (int n = 0; n<normals.Length; n++) normals[n] = Vector3.forward; } Vector2[] uvs = mesh.uv; color = new Color32[uvs.Length]; for (int i = 0; i<position.Length; i++) color[i] = texture.GetPixelBilinear(uvs[i].x, uvs[i].y); stateMesh = mesh; stateTransform = newStateTransform; colorLength = color.Length; positionLength = position.Length; stateScale = scale; stateOffset = offset; stateName = newStateName; initialized = position.Length>0; }
public bool init(Texture2D mapTexture, float u, float v, float sizeUV, float nodeSize, float colorValue, Material mat) { size = nodeSize; //nodeEnabled = !checkAllZero(mapTexture, u, v, sizeUV); if (mapTexture != null) { Color tlc = mapTexture.GetPixelBilinear(u,v); Color trc = mapTexture.GetPixelBilinear(u+sizeUV,v); Color blc = mapTexture.GetPixelBilinear(u,v-sizeUV); Color brc = mapTexture.GetPixelBilinear(u+sizeUV,v-sizeUV); tl = Mathf.Clamp01(tlc.a); tr = Mathf.Clamp01(trc.a); bl = Mathf.Clamp01(blc.a); br = Mathf.Clamp01(brc.a); } else { tl = 0; tr = 0; bl = 0; br = 0; } //bool allOver = (tl >= colorValue) && (tr >= colorValue) && (bl >= colorValue) && (br >= colorValue); bool allUnder = (tl < colorValue) && (tr < colorValue) && (bl < colorValue) && (br < colorValue); nodeEnabled = !allUnder; isGeometry = false; isFull = false; if (nodeEnabled) createGeometry(colorValue); initialized = true; return isGeometry || isFull; }
private bool checkAllZero(Texture2D texture, float u, float v, float uvSize) { float delta = 1.0f / texture.width; for (float x = 0; x<uvSize; x+=delta) { if (texture.GetPixelBilinear(u+x, v).r != 0) return false; if (texture.GetPixelBilinear(u+x, v+uvSize).r != 0) return false; if (texture.GetPixelBilinear(u, v+x).r != 0) return false; if (texture.GetPixelBilinear(u+uvSize, v+x).r != 0) return false; } return true; }
// Resize a Texture2D // http://docs-jp.unity3d.com/Documentation/ScriptReference/Texture2D.GetPixelBilinear.html Texture2D Resize(Texture2D sourceTex, int Width, int Height, bool flipY) { Texture2D destTex = new Texture2D(Width, Height, sourceTex.format, false); Color[] destPix = new Color[Width * Height]; int y = 0; while (y < Height) { int x = 0; while (x < Width) { float xFrac = x * 1.0F / (Width ); float yFrac = y * 1.0F / (Height); if(flipY == true) yFrac = (1 - y - 2) * 1.0F / (Height); destPix[y * Width + x] = sourceTex.GetPixelBilinear(xFrac, yFrac); x++; } y++; } destTex.SetPixels(destPix); destTex.Apply(); return destTex; }
//routine to save screenshot public void SaveScreenshot(Texture2D source,int targetWidth,int targetHeight) { Texture2D result=new Texture2D(targetWidth,targetHeight, TextureFormat.RGB24 ,true); //Texture2D result=new Texture2D(targetWidth,targetHeight,source.format,true); Color[] rpixels=result.GetPixels(0); float incX=(1.0f / (float)targetWidth); float incY=(1.0f / (float)targetHeight); //scale texture into target width & height for(int px=0; px<rpixels.Length; px++) { rpixels[px] = source.GetPixelBilinear(incX*((float)px%targetWidth), incY*((float)Mathf.Floor(px/targetWidth))); } result.SetPixels(rpixels,0); result.Apply(); container.screenshot = result.EncodeToPNG(); }
private static Texture2D Resize(Texture2D source, int width, int height) { Debug.Log("Resizing image from " + source.width + "x" + source.height + " to " + width + "x" + height); Texture2D result = new Texture2D(width, height, source.format, false); for (int y = 0; y < result.height; ++y) { for (int x = 0; x < result.width; ++x) { Color newColor = source.GetPixelBilinear((float)x / (float)result.width, (float)y / (float)result.height); result.SetPixel(x, y, newColor); } } result.Apply(); Debug.Log("Done resizing."); return result; }
//look for a faster way of calculating this private Color[] changeDimensions(Color[] originalColors, int originalWidth, int originalHeight, int newWidth, int newHeight) { Color[] newColors; Texture2D originalTexture; int pixelCount; float u; float v; if (originalWidth == newWidth && originalHeight == newHeight) { newColors = originalColors; } else { newColors = new Color[newWidth * newHeight]; originalTexture = new Texture2D(originalWidth, originalHeight); originalTexture.SetPixels(originalColors); for (int y = 0; y < newHeight; y++) { for (int x = 0; x < newWidth; x++) { pixelCount = x + (y * newWidth); u = (float)x / newWidth; v = (float)y / newHeight; newColors[pixelCount] = originalTexture.GetPixelBilinear(u, v); } } } return newColors; }
private Texture2D ScaleTexture(Texture2D source, int targetWidth, int targetHeight) { Texture2D result = new Texture2D(targetWidth,targetHeight,source.format,false); Color[] rpixels = result.GetPixels(0); float incX=(1.0f / (float)targetWidth); float incY=(1.0f / (float)targetHeight); for (int px = 0; px < rpixels.Length; px++) rpixels[px] = source.GetPixelBilinear(incX*((float)px%targetWidth), incY*((float)Mathf.Floor(px/(float)targetWidth))); result.SetPixels(rpixels,0); result.Apply(); return result; }
private Texture2D UpdateGuiSkin(Texture2D texture, Color primaryColor) { Texture2D newTexture = new Texture2D((int)(texture.width / scalingFactor), (int) (texture.height / scalingFactor), texture.format, false); for (int i = 0; i < newTexture.width; i++) { for (int j = 0; j < newTexture.height; j++) { Color color = texture.GetPixelBilinear(((float)i * scalingFactor) / texture.width, ((float)j * scalingFactor) / texture.height) * primaryColor; newTexture.SetPixel(i, j, color); } } newTexture.Apply(); return newTexture; }
public static Color CustGetPixelBilinear(Texture2D GlobalHNMap, float _u, float _v) { if (GlobalHNMap.filterMode == FilterMode.Point) { return GlobalHNMap.GetPixel( Mathf.FloorToInt(_u*GlobalHNMap.width), Mathf.FloorToInt(_v*GlobalHNMap.width) ); } else { return GlobalHNMap.GetPixelBilinear( _u-0.5f/GlobalHNMap.width, _v-0.5f/GlobalHNMap.height ); } }
public bool LoadHeightMap(Texture2D tex, float min, float max) { if (tex == null) return false; float tw = tex.width, th = tex.height; float two = tw / settings.xCount, tho = th / settings.yCount; float d, ch, h; int i, j, ii, jj, pi, mi; List<Mesh> ms = new List<Mesh>(); List<Vector3[]> vs = new List<Vector3[]>(); Mesh mesh; Vector3[] vertices; bool c; PATile tile; PAPoint point; Color clr = Color.black; if (min < max) d = max - min; else d = min - max; for (j = 0; j <= settings.yCount; ++j) for (i = 0; i <= settings.xCount; ++i) { if (i == settings.xCount) ii = i - 1; else ii = i; if (j == settings.yCount) jj = j - 1; else jj = j; clr = tex.GetPixelBilinear((ii * two) / tw, (jj * tho) / th); ch = (clr.r + clr.g + clr.b) / 3.0f; h = min + d * ch; //SetPointHeight(i, j, h, false); point = settings.points[(settings.xCount + 1) * j + i]; for (pi = 0; pi < 4; ++pi) if (point.t[pi] >= 0) { c = false; tile = GetTile(point.t[pi]); mesh = GetChunkMesh(tile.chunkId); vertices = null; for (mi = 0; mi < ms.Count; ++mi) if (ms[mi] == mesh) { c = true; vertices = vs[mi]; } if (vertices == null) vertices = mesh.vertices; vertices[tile.cId * 4 + point.p[pi]].y = Mathf.Clamp(h, settings.minHeight, settings.maxHeight); if (!c) { ms.Add(mesh); vs.Add(vertices); } } } for (i = 0; i < ms.Count; ++i) ms[i].vertices = vs[i]; UpdateMesh(); return true; }
public static Texture2D ScaleTexture(Texture2D source, int targetWidth, int targetHeight, bool mipmap) { Texture2D texture2D = new Texture2D(targetWidth, targetHeight, source.format, mipmap); Color[] pixels = texture2D.GetPixels(0); float num = 1f / (float)targetWidth; float num2 = 1f / (float)targetHeight; for (int i = 0; i < pixels.Length; i++) { pixels[i] = source.GetPixelBilinear(num * (float)(i % targetWidth), num2 * Mathf.Floor((float)(i / targetWidth))); } texture2D.SetPixels(pixels, 0); texture2D.Apply(); return texture2D; }