Unity_调用Shader使YUV420p贴到Materials

来源:互联网 发布:php单页面源码 编辑:程序博客网 时间:2024/06/04 01:18

Shader下载地址http://download.csdn.net/download/le_sam/9980147

相关Inspector如下:



    public class Player : MonoBehaviour    {        public Renderer mRenderer;        private Texture2D texY, texU, texV;        private void Awake()        {            mRenderer.material = new Material(Shader.Find("Unlit/Transparent AvatarPi D3D"));            shareMemRead();        }        private void shareMemRead()        {            Application.targetFrameRate = 30;            StartCoroutine(readFrames());        }        private IEnumerator readFrames()        {            yield return new WaitForSeconds(3);            bool isRead = true;            //float lastTiem = Time.deltaTime;            while (isRead)            {                yield return new WaitForEndOfFrame();                byte[] data = new byte[Width * Height * 3 / 2];                //从dll里获取YUV420__byte[]                 Sharemem.pisoft_share_mem_read(ref isVideo, ref len, data, Sharemem.hLib);                Debug.LogFormat("单帧接收_Byte[]:{0}", len);                if (len > 0)                {                    byte[] dataY = new byte[Width * Height];                    byte[] dataU = new byte[Width * Height / 4];                    byte[] dataV = new byte[Width * Height / 4];                    Buffer.BlockCopy(data, 0, dataY, 0, Width * Height);                    Buffer.BlockCopy(data, Width * Height, dataU, 0, Width * Height / 4);                    Buffer.BlockCopy(data, Width * Height * 5 / 4, dataV, 0, Width * Height / 4);                    CreateTexture(Width, Height, dataY, dataU, dataV);                }            }        }        private void CreateTexture(int width, int height, byte[] dataY, byte[] dataU, byte[] dataV)        {            //if (width == mLastWidth && height == mLastHeight)            //{            //    return;            //}            //if (width == 0 || height == 0)            //{            //    return;            //}            //mLastWidth = width;            //mLastHeight = height;            //Color32[] grayColors = new Color32[width * height];            //for (int i = 0; i < grayColors.Length; ++i)            //{            //    grayColors[i].a = 0;            //}            //Y            if (texY)            {                Destroy(texY);            }            texY = new Texture2D(width, height, TextureFormat.Alpha8, false);            //texY.filterMode = FilterMode.Bilinear;            //texY.wrapMode = TextureWrapMode.Clamp;            //texY.SetPixels32(grayColors);            texY.LoadRawTextureData(dataY);            texY.Apply();            //grayColors = new Color32[(width / 2) * (height / 2)];            //for (int i = 0; i < grayColors.Length; ++i)            //{            //    grayColors[i].a = 128;            //}            //U            if (texU)            {                Destroy(texU);            }            texU = new Texture2D(width / 2, height / 2, TextureFormat.Alpha8, false);            //texU.filterMode = FilterMode.Point;            //texU.wrapMode = TextureWrapMode.Clamp;            //texU.SetPixels32(grayColors);            texU.LoadRawTextureData(dataU);            texU.Apply();            //V            if (texV)            {                Destroy(texV);            }            texV = new Texture2D(width / 2, height / 2, TextureFormat.Alpha8, false);            //texV.filterMode = FilterMode.Point;            //texV.wrapMode = TextureWrapMode.Clamp;            //texV.SetPixels32(grayColors);            texV.LoadRawTextureData(dataV);            texV.Apply();            Material mMaterial = mRenderer.sharedMaterial;            mMaterial.mainTexture = texY;            mMaterial.SetTexture("_MainTexU", texU);            mMaterial.SetTexture("_MainTexV", texV);        }    }


阅读全文
0 0