Unity使用GL动态画点与Texture2D改变像素

需要挂在相机上

byte数组动态更新,实时画在界面上

using System.Collections;
using System.Collections.Generic;
using GeeVision.Core;
using UnityEngine;

/// <summary>
/// GL 图像库
/// 1、GL图像库是底层的图像库,主要功能是使用程序来绘制常见的2D与3D几何图形。这些图形
/// 具有一定的特殊性,它们不属于3D网格图形,只会以面的形式渲染
/// 
/// 2、绘制2D图像时,需要使用GL.LoadOrtho()方法来将图形映射在平面中,绘制的是3D图形,就无须使用此方法
/// 
/// 3、使用GL图像库时,需要将所有绘制相关的内容写在OnPostRender()方法中
/// </summary>
public class TDrawPoint : MonoBehaviour 
{
    /// <summary>
    /// 绘制线段的材质
    /// </summary>
    public Material material;

    public byte[] Recognized = new byte[224 * 171];

    void Start()
    {

        material = new Material(Shader.Find("Particles/Alpha Blended"))
        {
            hideFlags = HideFlags.HideAndDontSave,
            shader = { hideFlags = HideFlags.HideAndDontSave }
        };
    }

    void Update()
    {
        Recognized = GeeVisionListener.Recognized;
    }

    private int index;
    private void OnPostRender()
    {
        index = 0;

        if (!material)
        {
            Debug.LogError("Material is null");
            return;
        }

        material.SetPass(0);                  //设置该材质通道,0为默认值
        GL.LoadOrtho();                       //设置绘制2D图像
        GL.Begin(GL.LINES);                   //表示开始绘制,绘制类型为线段
        for (int i = 0; i < 171; i++)
        {
            for (int j = 0; j < 224; j++)
            {
                GL.Color(Recognized[index] == 0 ? Color.black : Color.white);
                index++;
                DrawOnePoint(i+100, j+100);   //绘制线段0
            }
        }
        GL.End();                             //结束绘制
    }

    private void DrawOnePoint(float x, float y)
    {
        GL.Vertex(new Vector3(x / Screen.width, y / Screen.height, 0));     //设置顶点
    }
}
for (int i = 0; i < 100; i++)
        {
            for (int j = 0; j < 500; j++)
            {
                m_Texture2D.SetPixel(i, j, Color.white);
            }
        }
        m_Texture2D.Apply();
        m_Image.sprite = Sprite.Create(m_Texture2D, new Rect(0,0,m_Texture2D.width, m_Texture2D.height), Vector2.one);
        Debug.Log(m_Texture2D.GetPixel(0, 0));

构造函数

Texture2D(int width, int height);

Texture2D(int width, int height, TextureFormat format, bool mipmap);

Texture2D(int width, int height, TextureFormat format, bool mipmap, bool linear);

方法

对像素的操作

1.获取像素颜色

Color GetPixel(int x, int y);

Returns pixel color at coordinates (x, y).

2.获取正交化坐标系下像素颜色

Color GetPixelBilinear(float u, float v);

Returns filtered pixel color at normalized coordinates (u, v).

多用于处理得知多边形UV坐标时对像素的处理

3.获取一个区块的像素颜色

Color[] GetPixels(int miplevel = 0);

Get a block of pixel colors.

获取以x,y 为起始点,大小为width,height的一个区块,

返回的是一个数组,数组内颜色的点顺序为从左至右,从下至上

4.获取(指定mipmap level级别)的整张贴图的像素颜色(使用Color32格式)

Color32[] GetPixels32(int miplevel = 0);

Get a block of pixel colors in Color32 format.

读取速度比反复使用getPixel读取速度快

5.设置像素颜色

void SetPixel(int x, int y, Color color);

Sets pixel color at coordinates (x,y).

6.设置(指定mipmap level级别)的整张贴图的像素颜色

void SetPixels(Color[] colors, int miplevel = 0);

Set a block of pixel colors.

设置指定mipmap level下的整张贴图颜色

7.设置(指定mipmap level级别)的整张贴图的像素颜色(使用Color32格式)

void SetPixels32(Color32[] colors, int miplevel = 0);

Set a block of pixel colors.

对贴图的操作

1.当对贴图的像素操作后必须调用的函数,使操作生效。

void Apply(bool updateMipmaps = true, bool makeNoLongerReadable = false);

Actually apply all previous SetPixel and SetPixels changes.

2.将贴图压缩成DXT格式

void Compress(bool highQuality);

Compress texture into DXT format.

3.将贴图转码为PNG格式

byte[] EncodeToPNG();

Encodes this texture into PNG format.

4.加载一张贴图

bool LoadImage(byte[] data);

Loads an image from a byte array.

可以加载的格式为:JPG,PNG

5.将多张贴图打包到一张图集中

Rect[] PackTextures(Texture2D[] textures, int padding, int maximumAtlasSize = 2048, bool makeNoLongerReadable = false);

Packs multiple Textures into a texture atlas.

6.将屏幕色读入到贴图

void ReadPixels(Rect source, int destX, int destY, bool recalculateMipMaps = true);

Read screen pixels into the saved texture data.

rect source 可以用来创建需要截取的屏幕区域,

destX,destY 表明了渲染到贴图的起始点,(0,0)点为屏幕的左下角。

readPixels函数主要可以配合camera.OnPostRender进行截图及RenderToTexture操作,

7.重新定义贴图

bool Resize(int width, int height, TextureFormat format, bool hasMipMap);

Resizes the texture.

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值