zoukankan      html  css  js  c++  java
  • Unity shader学习之反射

    shader如下:

    Shader "Custom/Reflection"
    {
        Properties
        {
            _Cubemap("Cubemap", Cube) = "" {}
            _ReflectionAmount("Reflection Amount", Range(0, 1)) = 0.5
        }
    
        SubShader
        {
            Pass
            {
                Tags
                {
                    "LightMode" = "ForwardBase"
                }
                Cull Off
    
                CGPROGRAM
                #pragma vertex vert
                #pragma fragment frag
                #pragma multi_compile_fwdbase
    
                #include "UnityCG.cginc"
                #include "Lighting.cginc"
                #include "AutoLight.cginc"
    
                samplerCUBE _Cubemap;
                float _ReflectionAmount;
    
                struct appdata
                {
                    float4 vertex : POSITION;
                    fixed4 color : COLOR;
                    float3 normal : NORMAl;
                };
    
                struct v2f
                {
                    float4 pos : SV_POSITION;
                    fixed4 color : COLOR;
                    float3 worldNormal : TEXCOORD0;
                    float3 worldPos : TEXCOORD1;
                    SHADOW_COORDS(2)
                };
    
                v2f vert(appdata v)
                {
                    v2f o;
                    o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
                    o.color = v.color;
                    o.worldNormal = UnityObjectToWorldNormal(v.normal);
                    o.worldPos = mul(unity_ObjectToWorld, v.vertex);
                    TRANSFER_SHADOW(o);
                    return o;
                }
    
                fixed4 frag(v2f i) : SV_TARGET
                {
                    fixed3 albedo = i.color.rgb;
    
                    fixed3 ambient = albedo * UNITY_LIGHTMODEL_AMBIENT.rgb;
    
                    float3 worldLight = UnityWorldSpaceLightDir(i.worldPos);
                    fixed3 diffuse = albedo * _LightColor0.rgb * max(0, dot(worldLight, i.worldNormal));
    
                    float3 worldView = UnityWorldSpaceViewDir(i.worldPos);
                    float3 refDir = reflect(-worldView, i.worldNormal);
                    fixed3 refCol = texCUBE(_Cubemap, refDir);
    
                    UNITY_LIGHT_ATTENUATION(atten, i, i.worldPos);
    
                    fixed3 col = ambient + lerp(diffuse, refCol, _ReflectionAmount) * atten;
    
                    return fixed4(col, 1);
                }
    
                ENDCG
            }
        }
    
        Fallback "VertexLit"
    }

    效果如下:

  • 相关阅读:
    移动端前端开发调试
    webkit图片滤镜
    ruby安装sass报错解决办法
    mongodb的查询语句学习摘要
    signedCookies
    [cookie篇]从cookie-parser中间件说起
    node.js下mongoose简单操作实例
    在ExpressJS中设置二级域名跨域共享Cookie
    Node.js开发工具、开发包、框架等总结
    hibernate框架学习笔记4:主键生成策略、对象状态
  • 原文地址:https://www.cnblogs.com/jietian331/p/7200212.html
Copyright © 2011-2022 走看看