美文网首页
unity shader 始终看向摄像机的眼珠

unity shader 始终看向摄像机的眼珠

作者: 独步江雪 | 来源:发表于2020-12-06 13:52 被阅读0次
image.png
Shader "Custom/CameraDirectionAttenuationShader"
{
    Properties
    {
        _MainTex ("Texture", 2D) = "white" {}
    }
    SubShader
    {
        Tags
        {
            "RenderType"="Opaque"
        }
        LOD 100

        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            // make fog work
            #pragma multi_compile_fog

            #include "UnityCG.cginc"

            struct appdata
            {
            
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
            };

            struct v2f
            
            {
        
                float2 uv : TEXCOORD0;
                UNITY_FOG_COORDS(1)
                float4 vertex : SV_POSITION;
                float3 vertex_w :  TEXCOORD1;
            };

            sampler2D _MainTex;
            float4 _MainTex_ST; 

            v2f vert (appdata v)
            {
                v2f o;
                o.vertex_w= mul(unity_ObjectToWorld,v.vertex)    ;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.uv = TRANSFORM_TEX(v.uv, _MainTex);
                UNITY_TRANSFER_FOG(o,o.vertex);
                return o;
            }

            half getMold(fixed3 v)
            {
                return pow( (pow(v.x,2)+pow(v.y,2)+pow(v.z,2)),0.5);
                
            }
            
            fixed4 frag (v2f i) : SV_Target
            {
                // sample the texture
                fixed3 center = unity_ObjectToWorld._14_24_34; //获取物体轴点世界空间位置
                half r = 0.5; //球半径

                //球面上离相机最近的一点 
                fixed3 nearestPos =  center+  normalize (_WorldSpaceCameraPos.xyz-center.xyz)*r;
 
                fixed4 col = {0,0,0,1};

                fixed3 delta = i. vertex_w.xyz - nearestPos .xyz ;// * UNITY_MATRIX_V[2].xyz
                col.x =1- getMold(delta) /(r*2);
                if(getMold(delta) >= r*0.5)
                {
                    col.y=1;
                }
                // apply fog
                UNITY_APPLY_FOG(i.fogCoord, col);
                return col;
            }
            ENDCG
        }
    }
}

相关文章

网友评论

      本文标题:unity shader 始终看向摄像机的眼珠

      本文链接:https://www.haomeiwen.com/subject/ityowktx.html