zoukankan      html  css  js  c++  java
  • shader实现积雪效果

    Shader "Custom/Shader1" {
        Properties {
            _MainTex ("Base (RGB)", 2D) = "white" {}
            _Bump ("Bump", 2D) = "bump" {}
            _Snow ("Snow Level", Range(0,1) ) = 0
            _SnowColor ("Snow Color", Color) = (1.0,1.0,1.0,1.0)
            _SnowDirection ("Snow Direction", Vector) = (0,1,0)
            _SnowDepth ("Snow Depth", Range(0,0.3)) = 0.1
        }
        SubShader {
            Tags { "RenderType"="Opaque" }
            LOD 200
            
            CGPROGRAM
            #pragma surface surf CustomDiffuse vertex:vert
            
            sampler2D _MainTex;
            sampler2D _Bump;                
            float _Snow;
            float4 _SnowColor;
            float4 _SnowDirection;
            float _SnowDepth;
    
            struct Input {
                float2 uv_MainTex;
                
                //3
                float2 uv_Bump;
                float3 worldNormal; INTERNAL_DATA
            };
    
            void surf (Input IN, inout SurfaceOutput o) {
                half4 c = tex2D (_MainTex, IN.uv_MainTex);
                o.Normal = UnpackNormal(tex2D(_Bump, IN.uv_Bump));
                
                if (dot(WorldNormalVector(IN, o.Normal), _SnowDirection.xyz) > lerp(1,-1,_Snow)) {
                    o.Albedo = _SnowColor.rgb;
                } else {
                    o.Albedo = c.rgb;
                }
    
                o.Alpha = c.a;
            }
    
            inline float4 LightingCustomDiffuse (SurfaceOutput s, fixed3 lightDir, fixed atten) {
                float difLight = max(0, dot (s.Normal, lightDir));
                float hLambert = difLight * 0.5 + 0.5;
                float4 col;
                col.rgb = s.Albedo * _LightColor0.rgb * (hLambert * atten * 2);
                col.a = s.Alpha;
                return col;
            }
    
            void vert (inout appdata_full v) {
                float4 sn = mul(transpose(_Object2World) , _SnowDirection);
                if(dot(v.normal, sn.xyz) >= lerp(1,-1, (_Snow * 2) / 3)) {
                    v.vertex.xyz += (sn.xyz + v.normal) * _SnowDepth * _Snow;
                }
            }
            ENDCG
        } 
        FallBack "Diffuse"
    }

    通过以上shader可以实现在石头上叠加积雪的效果,_SnowDepth为积雪的厚度,这个shader也能实现其他一些类似的效果(如灰尘的堆积)。

    一直想把之前工作、学习时记录的文档整理到博客上,一方面温故而知新,一方面和大家一起学习 -程序小白
  • 相关阅读:
    条件随机场(Conditional random field)
    隐马尔科夫模型(hidden Markov Model)
    什么是EM算法?
    非线性支持向量机基础——核函数之我见
    支持向量机(support vector machine)
    决策树之CART算法
    决策树到底是什么?
    pytorch下对简单的数据进行分类(classification)
    git status 命令
    spring中事务的实现方式和失效场景
  • 原文地址:https://www.cnblogs.com/wang-jin-fu/p/8279638.html
Copyright © 2011-2022 走看看