当前位置: 首页>>代码示例>>C#>>正文


C# Texture2D.GetPixel方法代码示例

本文整理汇总了C#中UnityEngine.Texture2D.GetPixel方法的典型用法代码示例。如果您正苦于以下问题:C# Texture2D.GetPixel方法的具体用法?C# Texture2D.GetPixel怎么用?C# Texture2D.GetPixel使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在UnityEngine.Texture2D的用法示例。


在下文中一共展示了Texture2D.GetPixel方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: CalculateNormalMap

    public static Texture2D CalculateNormalMap(Texture2D source, float strength)
    {
        Texture2D result;
        float xLeft, xRight;
        float yUp, yDown;
        float yDelta, xDelta;
        var pixels = new Color[source.width * source.height];
        strength = Mathf.Clamp(strength, 0.0F, 10.0F);        
        result = new Texture2D(source.width, source.height, TextureFormat.ARGB32, true);
        
        for (int by = 0; by < result.height; by++)
        {
            for (int bx = 0; bx < result.width; bx++)
            {
                xLeft = source.GetPixel(bx - 1, by).grayscale * strength;
                xRight = source.GetPixel(bx + 1, by).grayscale * strength;
                yUp = source.GetPixel(bx, by - 1).grayscale * strength;
                yDown = source.GetPixel(bx, by + 1).grayscale * strength;
                xDelta = ((xLeft - xRight) + 1) * 0.5f;
                yDelta = ((yUp - yDown) + 1) * 0.5f;

                pixels[bx + by * source.width] = new Color(xDelta, yDelta, 1.0f, yDelta);
            }
        }

        result.SetPixels(pixels);
        result.wrapMode = TextureWrapMode.Clamp;
        result.Apply();
        return result;
    }
开发者ID:Cyberbanan,项目名称:WorldGeneratorFinal,代码行数:30,代码来源:TextureGenerator.cs

示例2: OnRenderImage

	void OnRenderImage (RenderTexture source, RenderTexture destination){

		Graphics.Blit(source,destination,mat);
		//mat is the material which contains the shader
		//we are passing the destination RenderTexture to


		int width = Screen.width;
		int height = Screen.height;

		Debug.Log (width.ToString ());
		Debug.Log (height.ToString ());

		Texture2D tex = new Texture2D(width, height);
		tex.ReadPixels(new Rect(0, 0, width, height), 0, 0);


		if (flag == true) {
			string[] blue = new string[width * height];

			for (int i = 0; i < height; i++) {
				for (int j=0; j < width; j++) {
					blue[(i * width) + j]  = tex.GetPixel (j, i).b.ToString ();
				}
			}
			string output = string.Join(",", blue);
			File.WriteAllText ("/Users/mettinger/Desktop/temp/output.txt", output);
			flag = false;
		}
		Debug.Log (tex.GetPixel(x,y).b.ToString());
	
	}
开发者ID:mettinger,项目名称:unity,代码行数:32,代码来源:PostProcessDepthGrayscale.cs

示例3: changeSpritesColour

	public void changeSpritesColour()
	{
		texWidth = 32;
		texHeight = 32;
		newTexture = new Texture2D(texWidth, texHeight, TextureFormat.ARGB32, false);
		newTexture.SetPixels(spriteParent.sprite.texture.GetPixels());
		cArray = newTexture.GetPixels();
		int y = 0;
		while(y < texHeight)
		{
			int x = 0;
			while(x < texWidth)
			{
				if(newTexture.GetPixel(x, y) == skinColourBase)
					newTexture.SetPixel(x, y, skinColour);
				if(newTexture.GetPixel(x, y) == hairColourBase)
					newTexture.SetPixel(x, y, hairColour);
				if(newTexture.GetPixel(x, y) == eyeColourBase)
					newTexture.SetPixel(x, y, eyeColour);
				if(newTexture.GetPixel(x, y) == shoeColourBase)
					newTexture.SetPixel(x, y, shoeColour);
				if(newTexture.GetPixel(x, y) == shirtColourBase)
					newTexture.SetPixel(x, y, shirtColour);
				if(newTexture.GetPixel(x, y) == pantsColourBase)
					newTexture.SetPixel(x, y, pantsColour);
				x++;
			}
			y++;
		}
		newTexture.wrapMode = TextureWrapMode.Clamp;
		newTexture.filterMode = FilterMode.Point;
		newTexture.Apply();
		spriteParent.sprite = Sprite.Create(newTexture, new Rect(0, 0, texWidth, texHeight), new Vector2(0.5f, 0.5f), 16);
	}
开发者ID:PirateJack,项目名称:Unity-Top-Down-2D-RPG-Tutorial,代码行数:34,代码来源:Entity.cs

示例4: GreyscaleToNormal

        public static Texture2D GreyscaleToNormal(Texture2D greyscale)
        {
            int width = greyscale.width;
            int height = greyscale.height;

            int lenght = width * height;

            Color[] pixelsNormal = new Color[lenght];

            for (int x = 0; x < width; x++) {
                for (int y = 0; y < height; y++) {

                    float currValue = greyscale.GetPixel(x, y).grayscale;

                    float x1n = greyscale.GetPixel(x - 1, y).grayscale;
                    float x1 = greyscale.GetPixel(x + 1, y).grayscale;
                    float y1n = greyscale.GetPixel(x, y - 1).grayscale;
                    float y1 = greyscale.GetPixel(x, y + 1).grayscale;

                    float normalXValue = (currValue - x1n) + (x1 - currValue) * 10 + 0.5f;
                    float normalYValue = (currValue - y1n) + (y1 - currValue) * 10 + 0.5f;

                    pixelsNormal[x + y * width] = new Color(1, 1-normalYValue, 1, 1-normalXValue);
                }
            }

            Texture2D normalmap = new Texture2D(width, height);
            normalmap.SetPixels(pixelsNormal);
            normalmap.Apply();
            return normalmap;
        }
开发者ID:Bullshitzu,项目名称:SGU,代码行数:31,代码来源:TextureHelper.cs

示例5: GenerateNormalMap

        public Texture2D GenerateNormalMap(Texture2D heightMap)
        {
            var dx = 0f; var dy = 0f; var strength = 0.5f;
            var left = 0f; var right = 0f; var up = 0f; var down = 0f;

            var result = new Texture2D(heightMap.width, heightMap.height, TextureFormat.ARGB32, true);

            for (var i = 0; i < result.height; i++)
            {
                for (var j = 0; j < result.width; j++)
                {
                    left = heightMap.GetPixel(j - 1, i).grayscale * strength;
                    right = heightMap.GetPixel(j + 1, i).grayscale * strength;
                    up = heightMap.GetPixel(j, i - 1).grayscale * strength;
                    down = heightMap.GetPixel(j, i + 1).grayscale * strength;
                    dx = (left - right) * 0.5f;
                    dy = (down - up) * 0.5f;

                    result.SetPixel(j, i, new Color(dx, dy, 1.0f, dx));
                }
            }

            result.Apply();

            return result;
        }
开发者ID:Tridimensional,项目名称:Tridimensional.Puzzle,代码行数:26,代码来源:GraphicsService.cs

示例6: Count

    Vector4 Count(Texture2D texture, Edge edge)
    {
        Vector4 result = new Vector4();
        int depth = 0;
        int until, position, step, size;
        until = position = step = size = 0;
        MatchDirection dir = 0;
        switch(edge) {
        case Edge.Right:
            until = texture.height;
            position = texture.width;
            size = texture.width;
            step = -1;
            dir = MatchDirection.Horizontal;
            break;
        case Edge.Left:
            until = texture.height;
            position = 0;
            size = texture.width;
            step = 1;
            dir = MatchDirection.Horizontal;
            break;
        case Edge.Bottom:
            until = texture.width;
            position = 0;
            size = texture.height;
            step = 1;
            dir = MatchDirection.Vertical;
            break;
        case Edge.Top:
            until = texture.width;
            position = texture.height;
            size = texture.height;
            step = -1;
            dir = MatchDirection.Vertical;
            break;
        }

        for(int i = _buffer; i < until-_buffer; i++) {
            int counter = 0;
            int pos = position;
            Color pixel;
            do {
                if(dir == MatchDirection.Horizontal)
                    pixel = texture.GetPixel(pos, i);
                else
                    pixel = texture.GetPixel(i, pos);

                counter++;
                pos += step;
            } while(pixel.a == 0 && counter < size);
            depth = Mathf.Max(depth, counter);
            result += (Vector4)pixel;
        }
        if(depth < _buffer)
            return Vector4.zero;
        else
            return result;
    }
开发者ID:hilvi,项目名称:MimmitAndroid,代码行数:59,代码来源:PuzzleSolver.cs

示例7: DitherTexture

        /// <summary>
        /// Destructive dithering of texture.
        /// Texture is 8888, will be written out as 8888 too
        /// </summary>
        public static void DitherTexture(Texture2D texture, TextureFormat targetTextureFormat, int x0, int y0, int w, int h)
        {
            int quantShiftR = 0, quantShiftG = 0, quantShiftB = 0, quantShiftA = 0;
            switch (targetTextureFormat)
            {
            case TextureFormat.ARGB4444:
                quantShiftR = quantShiftG = quantShiftB = quantShiftA = 4;
                break;
            case TextureFormat.RGB565:
                quantShiftR = 0;
                quantShiftB = 6;
                quantShiftG = 5;
                quantShiftA = 0;
                break;
            }

            int x1 = x0 + w;
            int y1 = y0 + h;

            for (int y = y0; y < y1; ++y)
            {
                for (int x = x0; x < x1; ++x)
                {
                    Color oldPixel = texture.GetPixel(x, y);

                    Color newPixel = new Color(  (((int)(oldPixel.r * 255.0f + 0.5f) >> quantShiftR) << quantShiftR) / 255.0f,
                                                 (((int)(oldPixel.g * 255.0f + 0.5f) >> quantShiftG) << quantShiftG) / 255.0f,
                                                 (((int)(oldPixel.b * 255.0f + 0.5f) >> quantShiftB) << quantShiftB) / 255.0f,
                                                 (((int)(oldPixel.a * 255.0f + 0.5f) >> quantShiftA) << quantShiftA) / 255.0f );
                    Color quantizationError = oldPixel - newPixel;

                    // write out color, but "fix up" whites
                    Color targetColor = new Color((oldPixel.r == 1.0f)?1.0f:newPixel.r,
                                                  (oldPixel.g == 1.0f)?1.0f:newPixel.g,
                                                  (oldPixel.b == 1.0f)?1.0f:newPixel.b,
                                                  (oldPixel.a == 1.0f)?1.0f:newPixel.a);
                    texture.SetPixel(x, y, targetColor);

                    if (x < x1 - 1) texture.SetPixel(x + 1, y, texture.GetPixel(x + 1, y) + (quantizationError * 7.0f / 16.0f));
                    if (y < y1 - 1)
                    {
                        if (x > x0) texture.SetPixel(x - 1, y + 1, texture.GetPixel(x - 1, y + 1) + (quantizationError * 3.0f / 16.0f));
                        if (x < x1 - 1) texture.SetPixel(x + 1, y + 1, texture.GetPixel(x + 1, y + 1) + (quantizationError / 16.0f));
                        texture.SetPixel(x, y + 1, texture.GetPixel(x, y + 1) + (quantizationError * 5.0f / 16.0f));
                    }
                }
            }
        }
开发者ID:stubuchbinder,项目名称:weave,代码行数:52,代码来源:tk2dFloydSteinbergDithering.cs

示例8: OnImageLoad

    public void OnImageLoad(string imgPath, Texture2D tex)
    {
        Data.Instance.SetRoomFromLocalFiles(true);

        float currAspect = Screen.currentResolution.width * 0.8f / Screen.currentResolution.height;
        float texAspect = tex.width / tex.height;
        if (texAspect > currAspect) {
            Texture2D result = new Texture2D ((int)(tex.width * 1.2f), (int)(tex.height * 1.2f), tex.format, true);
            for (int y = 0; y < result.height; y++) {
                for (int x = 0; x < result.width; x++) {
                    if (y > (result.height * 0.1f) && y < (result.height * 0.9f) && x > (result.width * 0.1f) && x < (result.width * 0.9f)) {
                        result.SetPixel (x, y, tex.GetPixel (x - (int)(tex.width * 0.1f), y - (int)(tex.height * 0.1f)));
                    } else {
                        result.SetPixel (x, y, Color.black);
                    }
                }
            }
            result.Apply ();
            Data.Instance.lastPhotoTexture = result;
        } else {
            Data.Instance.lastPhotoTexture = tex;
        }

        Data.Instance.LoadLevel("ConfirmPhoto");
    }
开发者ID:pontura,项目名称:ArtPlacer,代码行数:25,代码来源:Rooms.cs

示例9: convert

    public static void convert(Texture2D texture)
    {
        int size = texture.width * texture.height;
        Color[] color = new Color[size];

        for(int i = 0; i < texture.height; ++i) {
            for(int j = 0; j < texture.width; ++j) {
                Color c = texture.GetPixel(j, i);
                int index = i * texture.width + j;
                color[index].r = c.r;
                color[index].g = c.g;
                color[index].b = c.b;
                color[index].a = c.a;
            }
        }

        process(color, texture.width, texture.height);

        for(int i = 0; i < texture.height; ++i) {
            for(int j = 0; j < texture.width; ++j) {
                int index = i * texture.width + j;

                float a = Mathf.Clamp01(color[index].a);
                float r = Mathf.Clamp01(color[index].r);
                float g = Mathf.Clamp01(color[index].g);
                float b = Mathf.Clamp01(color[index].b);
                texture.SetPixel(j, i, new Color(r, g, b, a));
            }
        }
        texture.Apply();
    }
开发者ID:taqu,项目名称:Dither,代码行数:31,代码来源:FloydSteinbergErrorDiffusion.cs

示例10: DownSample

        private Texture2D DownSample(Texture2D src)
        {
            int width = 1024, height = 512;
            Texture2D dst = new Texture2D(width, height, TextureFormat.ARGB32, false);
            Color[] cs = new Color[width * height];
            for (int x = 0; x < width; ++x)
            for (int y = 0; y < height; ++y) {
                int dstIndex = x + y * width;
                Color dstColor = Color.black;

                for (int subX = 0; subX < 4; ++subX)
                    for (int subY = 0; subY < 4; ++subY) {
                        Color srcColor = src.GetPixel(x * 4 + subX, y * 4 + subY);
                        dstColor += srcColor;
                    }

                dstColor /= 16.0f;
                dstColor.a = 1.0f;

                cs[dstIndex] = dstColor;
            }
            dst.SetPixels(cs);
            dst.Apply();

            return dst;
        }
开发者ID:shadercoder,项目名称:Unity3D-experiments,代码行数:26,代码来源:Screenshot.cs

示例11: Load

    /// <summary>
    /// Scans through the pixels in the given texture and loads breakout block
    /// prefabs for each one. Returns a GameObject containing all these blocks,
    /// appropriately scaled and positioned in the center of the screen.
    /// </summary>
    public GameObject Load(Texture2D levelTex)
    {
        if (levelTex == null) throw new ArgumentNullException("level", "Level texture cannot be null.");
        GameObject levelContainer = new GameObject("LevelContainer");

        for (int x = 0; x < levelTex.width; x++)
        {
            for (int y = 0; y < levelTex.height; y++)
            {
                Color color = levelTex.GetPixel(x, y);
                if (color.a == 0) continue; //ignore transparent pixels
                var block = Instantiate(BlockPrefab, new Vector2(x, y), Quaternion.identity) as GameObject;
                block.GetComponent<SpriteRenderer>().color = color;
                block.transform.parent = levelContainer.transform;
                BlockCount++;
            }
        }

        //scale the level to be SIZE world units large
        const float SIZE = 5.0f;
        float scaledWidth = SIZE / levelTex.width;
        float scaledHeight = SIZE / levelTex.height;
        levelContainer.transform.localScale = new Vector2(scaledWidth, scaledHeight);

        //center the level
        levelContainer.transform.position = new Vector2(-(scaledWidth * (levelTex.width - 1)) / 2, -(scaledHeight * (levelTex.height - 1)) / 2);

        return levelContainer;
    }
开发者ID:taylus,项目名称:unity,代码行数:34,代码来源:LevelLoader.cs

示例12: pick

    public static Texture2D pick(int[,] pPatternMark,int pPickPatternID,
        Texture2D pSource, zzPointBounds pBounds, zzPoint pOutSize)
    {
        Texture2D lOut = new Texture2D(pOutSize.x, pOutSize.y, TextureFormat.ARGB32, false);
        var lMin = pBounds.min;
        var lMax = pBounds.max;
        var lDrawOffset = -lMin;
        for (int lY = lMin.y; lY < lMax.y; ++lY)
        {
            var lDrawedPointY = lY + lDrawOffset.y;
            for (int lX = lMin.x; lX < lMax.x; ++lX)
            {
                var lColor = pPatternMark[lX, lY] == pPickPatternID
                    ? pSource.GetPixel(lX, lY) : Color.clear;

                lOut.SetPixel(lX + lDrawOffset.x, lDrawedPointY, lColor);
            }
            for (int i = lMax.x+lDrawOffset.x; i < lOut.width; ++i)
            {
                lOut.SetPixel(i, lDrawedPointY, Color.clear);
            }
        }
        for (int lY = lMax.y + lDrawOffset.y; lY < lOut.height; ++lY)
        {
            for (int lX = 0; lX < lOut.width; ++lX)
                lOut.SetPixel(lX, lY, Color.clear);
        }
        lOut.Apply();
        return lOut;
    }
开发者ID:Seraphli,项目名称:TheInsectersWar,代码行数:30,代码来源:zzImagePatternPicker.cs

示例13: Multiply

		private static Texture2D Multiply(Texture2D lowerLayer, float lowerOpacity, Texture2D upperLayer, float upperOpacity) {
			Texture2D result = new Texture2D (lowerLayer.width, lowerLayer.height);
			result.filterMode = FilterMode.Point;
			for (int x = 0; x < lowerLayer.width; x++) {
				for (int y = 0; y < lowerLayer.height; y++) {
					Color lower = lowerLayer.GetPixel (x, y);
					Color upper = upperLayer.GetPixel (x, y);
					float srca = upper.a * upperOpacity;
					float lowa = lower.a * lowerOpacity;

					Color c = Color.clear;
					if (srca == 0 && lowa != 0) {
						c = new Color(lower.r, lower.g, lower.b, lowa);
					} else if (lowa == 0 && srca != 0){
						c = new Color(upper.r, upper.g, upper.b, srca);
					} else if (lowa != 0 && srca != 0) {
						float outa = srca + lowa * (1 - srca);
						c = new Color ((upper.r * upper.a * lower.r * lower.a) / outa,
					                     (upper.g * upper.a * lower.g * lower.a) / outa,
					                     (upper.b * upper.a * lower.b * lower.a) / outa,
					                     outa);
					}

					result.SetPixel (x, y, c);
				}
			}
			
			result.Apply ();
			return result;
		}
开发者ID:Alexander144,项目名称:GamejamHamar,代码行数:30,代码来源:UPABlendModes.cs

示例14: Start

    // 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));
    }
开发者ID:Lukasz199312,项目名称:ClickCraft,代码行数:26,代码来源:ImagePixelBuild.cs

示例15: CreateStencil

 void CreateStencil(int x, int y, Texture2D texture)
 {
     this.AonMaterial.mainTexture = tex;
     for (int xPix = 0; xPix < texture.width; xPix++)
     {
         for (int yPix = 0; yPix < texture.height; yPix++)
         {
             stencilUV[i] = texture.GetPixel(xPix, yPix) * texture.GetPixel(xPix, yPix).a + tex.GetPixel((x - texture.width / 2) + xPix, (y - texture.height / 2) + yPix) * (1 - texture.GetPixel(xPix, yPix).a);
             i++;
         }
     }
     //Debug.Log("x = "+ x + ", y = " + y);
     i = 0;
     tex.SetPixels(x - texture.width / 2, y - texture.height / 2, texture.width, texture.height, stencilUV);
     tex.Apply();
 }
开发者ID:kw0006667,项目名称:Aon,代码行数:16,代码来源:TextureBrush.cs


注:本文中的UnityEngine.Texture2D.GetPixel方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。