博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【Shader】世界空间下的高光法线计算
阅读量:4088 次
发布时间:2019-05-25

本文共 3074 字,大约阅读时间需要 10 分钟。

Shader "Unlit/WorldNormalTex"{    Properties    {        _MainTex ("Texture", 2D) = "white" {}        _MainNormalTex("NormalTex",2D) = "white"{}    }    SubShader    {        Tags { "RenderType"="Opaque" }        LOD 100        Pass        {            Tags{                "LightMode"="ForwardBase"            }            CGPROGRAM            #pragma vertex vert            #pragma fragment frag            #include "UnityCG.cginc"            #include "Lighting.cginc"            struct a2v            {                float4 vertex : POSITION;                float2 uv : TEXCOORD0;                float3 normal:NORMAL;                float4 tangent:TANGENT;            };            struct v2f            {                float4 uv : TEXCOORD0;                float4 vertex : SV_POSITION;                float4 T2W0 :TEXCOORD1 ;                float4 T2W1 :TEXCOORD2 ;                float4 T2W2 :TEXCOORD3 ;            };            sampler2D _MainTex;            float4 _MainTex_ST;            sampler2D _MainNormalTex;            float4 _MainNormalTex_ST;            v2f vert (a2v v)            {                v2f o;                o.vertex = UnityObjectToClipPos(v.vertex);                // 缩放+偏移                o.uv.xy = TRANSFORM_TEX(v.uv,_MainTex);                o.uv.zw = TRANSFORM_TEX(v.uv,_MainNormalTex);                float3 worldPos = mul(unity_ObjectToWorld,v.vertex);                float3 worldNormal = UnityObjectToWorldNormal(v.normal);                float3 worldTangent = UnityObjectToWorldDir(v.tangent);                float3 worldBitangent = cross(worldNormal ,worldTangent) * v.tangent.w;                // 一个插值寄存器最多存储float4大小的变量                o.T2W0 = float4 (worldTangent.x,worldBitangent.x,worldNormal.x,worldPos .x);                o.T2W1 = float4 (worldTangent.y,worldBitangent.y,worldNormal.y,worldPos .y);                o.T2W2 = float4 (worldTangent.z,worldBitangent.z,worldNormal.z,worldPos .z);                return o;            }            fixed4 frag (v2f i) : SV_Target            {                float3 worldPos  = float3(i.T2W0.w,i.T2W1.w,i.T2W2.w);                fixed3 lightDir = normalize(UnityWorldSpaceLightDir(worldPos));                fixed3 viewDir = normalize(UnityWorldSpaceViewDir(worldPos));                fixed4 tangentNormal = tex2D(_MainNormalTex,i.uv.zw);                fixed3 bump = UnpackNormal(tangentNormal);                fixed3 worldBump = normalize(half3( dot(i.T2W0.xyz,bump),                                                    dot(i.T2W1.xyz,bump),                                                    dot(i.T2W2.xyz,bump)));                float3 albedo = tex2D(_MainTex,i.uv.xy).rgb;                fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT;                fixed3 diffuse = _LightColor0.rgb  * albedo* max(0,dot(worldBump,lightDir));                fixed3 halfDir = normalize(lightDir+viewDir);                fixed3 specular = _LightColor0.rgb *  pow(max(0,dot(worldBump,halfDir)),20);                return fixed4( diffuse+ambient+specular,1.0);            }            ENDCG        }    }}

转载地址:http://ndkii.baihongyu.com/

你可能感兴趣的文章
掌握 TS 这些工具类型,让你开发事半功倍
查看>>
前端如何搭建一个成熟的脚手架
查看>>
Flutter ListView如何添加HeaderView和FooterView
查看>>
Flutter key
查看>>
Flutter 组件通信(父子、兄弟)
查看>>
Flutter Animation动画
查看>>
Flutter 全局监听路由堆栈变化
查看>>
Android 混合Flutter之产物集成方式
查看>>
Flutter混合开发二-FlutterBoost使用介绍
查看>>
Flutter 混合开发框架模式探索
查看>>
Flutter 核心原理与混合开发模式
查看>>
Flutter Boost的router管理
查看>>
Android Flutter混合编译
查看>>
微信小程序 Audio API
查看>>
[React Native]react-native-scrollable-tab-view(进阶篇)
查看>>
Vue全家桶+Mint-Ui打造高仿QQMusic,搭配详细说明
查看>>
React Native for Android 发布独立的安装包
查看>>
React Native应用部署/热更新-CodePush最新集成总结(新)
查看>>
react-native-wechat
查看>>
基于云信的react-native聊天系统
查看>>