本文整理汇总了C#中UnityEngine.Texture2D.GetPixels32方法的典型用法代码示例。如果您正苦于以下问题:C# Texture2D.GetPixels32方法的具体用法?C# Texture2D.GetPixels32怎么用?C# Texture2D.GetPixels32使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类UnityEngine.Texture2D
的用法示例。
在下文中一共展示了Texture2D.GetPixels32方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Rotation
/// <summary>
/// Receives the original texture so it doesn't have to instantiate it every time.
/// </summary>
/// <param name="original"></param>
public Rotation(Texture2D original, Vector2 pivot, int pixelsPerUnit)
{
_newTexture = (Texture2D)MonoBehaviour.Instantiate(original);
_oldArray = original.GetPixels32();
_newArray = new Color32[original.GetPixels32().Length];
_width = original.width;
_height = original.height;
_pivot = pivot;
_pixelsPerUnit = pixelsPerUnit;
}
示例2: ApplyContourBleed
// ------------------------------------------------------------------
/// \param _tex the texture in which to apply contour bleed
/// prevents edge artifacts due to bilinear filtering
/// Note: Some image editors like Photoshop tend to fill purely transparent pixel with
/// white color (R=1, G=1, B=1, A=0). This is generally OK, because these white pixels
/// are impossible to see in normal circumstances. However, when such textures are
/// used in 3D with bilinear filtering, the shader will sometimes sample beyond visible
/// edges into purely transparent pixels and the white color stored there will bleed
/// into the visible edge. This method scans the texture to find all purely transparent
/// pixels that have a visible neighbor pixel, and copy the color data from that neighbor
/// into the transparent pixel, while preserving its 0 alpha value. In order to
/// optimize the algorithm for speed of execution, a compromise is made to use any
/// arbitrary neighboring pixel, as this should generally lead to correct results.
/// It also limits itself to the immediate neighbors around the edge, resulting in a
/// a bleed of a single pixel border around the edges, which should be fine, as bilinear
/// filtering should generally not sample beyond that one pixel range.
// ------------------------------------------------------------------
public static Texture2D ApplyContourBleed( Texture2D _tex )
{
// Extract pixel buffer to be modified
Color32[] pixels = _tex.GetPixels32(0);
for ( int x = 0; x < _tex.width; ++x ) {
for ( int y = 0; y < _tex.height; ++y ) {
// only try to bleed into purely transparent pixels
if ( pixels[x + y * _tex.width].a == 0 ) {
// sample all around to find any non-purely transparent pixels
for ( int i = 0; i < bleedXOffsets.Length; i++ ) {
int sampleX = x + bleedXOffsets[i];
int sampleY = y + bleedYOffsets[i];
// check to stay within texture bounds
if (sampleX >= 0 && sampleX < _tex.width && sampleY >= 0 && sampleY < _tex.height) {
Color32 color = pixels[sampleX + sampleY * _tex.width];
if (color.a != 0) {
// Copy RGB color channels to purely transparent pixel, but preserving its 0 alpha
pixels[x + y * _tex.width] = new Color32(color.r, color.g, color.b, 0);
break;
}
}
}
}
}
}
// Copy modified pixel buffer to new texture (to preserve original element texture and allow user to uncheck the option)
Texture2D tex = new Texture2D(_tex.width, _tex.height, _tex.format, false);
tex.SetPixels32(pixels);
return tex;
}
示例3: Start
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;
}
示例4: BinarizeTexture
// Distinguish completely transparent pixels from significant pixel by
// "binarizing" the texture via a bit array.
// false if a pixel is not significant (= transparent), true otherwise
public static BinarizedImage BinarizeTexture( Texture2D a_rTextureToFilter, float a_fAlphaCutOff )
{
if(a_rTextureToFilter == null)
{
return null;
}
float fAlphaCutOff32 = a_fAlphaCutOff * 255;
// Retrieve texture pixels (in 32bits format)
// Array is flattened / pixels laid left to right, bottom to top
Color32[ ] oTexturePixels = a_rTextureToFilter.GetPixels32( );
int iPixelCount = oTexturePixels.Length;
// Create (padded) sprite shape pixels array (bit array)
BinarizedImage oBinarizedTexture = new BinarizedImage( a_rTextureToFilter.width, a_rTextureToFilter.height, false );
// Parse all pixels
for( int iPixelIndex = 0; iPixelIndex < iPixelCount; ++iPixelIndex )
{
Color32 f4Pixel = oTexturePixels[ iPixelIndex ];
oBinarizedTexture.SetUnpaddedPixel( iPixelIndex, ( f4Pixel.a >= fAlphaCutOff32 ) );
}
// Fill 1px holes
oBinarizedTexture.FillInsulatedHoles( );
return oBinarizedTexture;
}
示例5: AddFrame
/// <summary>
/// Adds a frame to the animated GIF. If this is the first frame, it will be used to specify the size and color palette of the GIF.
/// </summary>
public void AddFrame(Texture2D frame)
{
if (!this.sizeSet)
{
// use first frame's size
this.width = frame.width;
this.height = frame.height;
this.sizeSet = true;
}
this.currentFramePixels = frame.GetPixels32();
this.GetImagePixels(); // convert to correct format if necessary
this.AnalyzePixels(); // build color table & map pixels
if (this.firstFrame)
{
this.WriteLSD(); // logical screen descriptior
this.WritePalette(); // global color table
if (this.repeat >= 0)
{
// use NS app extension to indicate reps
this.WriteNetscapeExt();
}
}
this.WriteGraphicCtrlExt(); // write graphic control extension
this.WriteImageDesc(); // image descriptor
this.WritePixels(); // encode and write pixel data
this.firstFrame = false;
}
示例6: LoadTexDistanceField
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
}
示例7: Update
void Update()
{
if (state == 0) {
AndroidJavaClass UnityOpenCVLoaderJava = new AndroidJavaClass(UNITY_OPENCV_LOADER);
var b = UnityOpenCVLoaderJava.CallStatic<Boolean>("isSuccess");
if (b) {
state = 1;
}
} else if (state == 1) {
if (cameraInstance == IntPtr.Zero) cameraInstance = CreateCameraInstance ();
if (Open (cameraInstance, 0, width, height)) {
texture = new Texture2D (width, height, TextureFormat.ARGB32, false);
pixels = texture.GetPixels32 ();
pixelsHandle = GCHandle.Alloc (pixels, GCHandleType.Pinned);
pixelsPtr = pixelsHandle.AddrOfPinnedObject ();
GetComponent<Renderer>().material.mainTexture = texture;
state = 2;
} else {
state = -1;
}
} else if (state == 2) {
getCameraTexture (cameraInstance, pixelsPtr, width, height);
texture.SetPixels32 (pixels);
texture.Apply ();
}
}
示例8: GetDXT
public static void GetDXT(Texture2D texture, int i, byte[] bytes, TextureFormat format)
{
Color32[] colors = texture.GetPixels32(i);
uint w = (uint) texture.width>>i;
uint h = (uint) texture.height>>i;
ColorBlock rgba = new ColorBlock();
BlockDXT1 block1 = new BlockDXT1();
BlockDXT5 block5 = new BlockDXT5();
int blocksize = format == TextureFormat.DXT1 ? 8 : 16;
int index = 0;
for (uint y = 0; y < h; y += 4) {
for (uint x = 0; x < w; x += 4) {
rgba.init(w, h, colors, x, y);
if (format == TextureFormat.DXT1)
{
QuickCompress.compressDXT1(rgba, block1);
block1.WriteBytes(bytes, index);
}
else
{
QuickCompress.compressDXT5(rgba, block5, 0);
block5.WriteBytes(bytes, index);
}
index += blocksize;
}
}
}
示例9: FixTransparency
//=========================================================================
// 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();
}
示例10: CopyTexture
// Texture Function --------------------------------------------------------------
public static Texture2D CopyTexture(Texture2D srcTex, Texture2D tarTex)
{
Color32[] colBuf = srcTex.GetPixels32();
tarTex.SetPixels32(colBuf);
tarTex.Apply(false);
return tarTex;
}
示例11: DoDithering
//
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;
}
示例12: UpdateByTexture2D
public void UpdateByTexture2D(Texture2D i_wtx)
{
System.Diagnostics.Debug.Assert(this._size.isEqualSize(i_wtx.width, i_wtx.height));
this._buf = i_wtx.GetPixels32();
this._rgb_pixel_driver.switchRaster(this);
return;
}
示例13: OnGUI
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);
}
}
}
示例14: FlipTexture2DVertically
public static void FlipTexture2DVertically(Texture2D texture2D)
{
var pixels = texture2D.GetPixels32();
Utils.Flip2DArrayVertically(pixels, texture2D.height, texture2D.width);
texture2D.SetPixels32(pixels);
texture2D.Apply();
}
示例15: Start
// Use this for initialization
void Start () {
camera_ = getCamera(device);
setCameraProp(camera_, width, height, fps);
texture_ = new Texture2D(width, height, TextureFormat.ARGB32, false);
pixels_ = texture_.GetPixels32();
pixels_handle_ = GCHandle.Alloc(pixels_, GCHandleType.Pinned);
pixels_ptr_ = pixels_handle_.AddrOfPinnedObject();
GetComponent<Renderer>().material.mainTexture = texture_;
}