Unity shader图集Atlas下的UV坐标归一化转换
生活随笔
收集整理的這篇文章主要介紹了
Unity shader图集Atlas下的UV坐标归一化转换
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
? unity中如果圖片打入了圖集中,在shader中取到的uv坐標(biāo)默認(rèn)是圖集中的坐標(biāo),如果需要shader做一些類似流光的效果,需要轉(zhuǎn)換成常用的0-1區(qū)間的歸一化uv坐標(biāo),轉(zhuǎn)換方法如下:
步驟一:?C#中向shader中傳入圖片在圖集中的Rect信息和Scale信息
Vector4 UVRect = UnityEngine.Sprites.DataUtility.GetOuterUV(spriteRenderer.sprite);Rect originRect = spriteRenderer.sprite.rect;Rect textureRect = spriteRenderer.sprite.textureRect;float scaleX = textureRect.width / originRect.width;float scaleY = textureRect.height / originRect.height;spriteRenderer.material.SetVector("_UVRect", UVRect);spriteRenderer.material.SetVector("_UVScale", new Vector4(scaleX, scaleY, 0, 0));步驟二:?shader中根據(jù)Rect信息和Scale信息換算得到歸一化UV坐標(biāo):
_UVRect("UVRect", Vector) = (0, 0, 1, 1)_UVScale("UVScale", Vector) = (0, 0, 0, 0)//Shader Pass中聲明變量屬性float4 _UVRect;float4 _UVScale;//Fragment Shader中換算算法,換算后的IN.texcoord就是歸一化后的UV坐標(biāo)float2 center = (_UVRect.zw - _UVRect.xy) / 2;IN.texcoord = IN.texcoord - _UVRect.xy - center;IN.texcoord *= _UVScale;IN.texcoord += center;IN.texcoord = IN.texcoord / (_UVRect.zw - _UVRect.xy);?
總結(jié)
以上是生活随笔為你收集整理的Unity shader图集Atlas下的UV坐标归一化转换的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 狂野星球世界观背景 狂野星球背景故事总览
- 下一篇: 使用arm-linux-androide