当前位置:   article > 正文

0817世界空间法线(未出现相应效果,待查)_ue4世界法线空间

ue4世界法线空间

//注 和切线空间法线使用的相同纹理图,但并未出现法现效果。

// Upgrade NOTE: replaced '_Object2World' with 'unity_ObjectToWorld'

// Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'


Shader "Test/WorldNormalShader" 
{
Properties
{
_Color("Color",Color)=(1,1,1,1)
_MainTex("Main",2D) = "white"{}
_Bump("Bump",2D) = "white"{}
_BumpScale("Scale",float) = 1
_Specular("Specular",Color) = (1,1,1,1)
_Gloss("Gloss",Range(1,20)) = 1
}


SubShader
{
Pass
{
Tags{"LightMode" = "ForwardBase"}
CGPROGRAM


#pragma vertex vert
#pragma fragment frag


#include "Lighting.cginc"
#include "UnityCG.cginc"


sampler2D _MainTex;
sampler2D _Bump;
float4 _MainTex_ST;
float4 _Bump_ST;
float4 _Color;
float4 _Specular; 
float _BumpScale;
float _Gloss;


struct a2v
{
float4 vertex : POSITION;
float3 normal : NORMAL;
float4 tangent : TANGENT;
float3 texcoord : TEXCOORD0;
};


struct v2f
{
float4 pos : SV_POSITION;
float4 uv : TEXCOORD0;
float4 toWorld0 : TEXCOORD1;
float4 toWorld1 : TEXCOORD2;
float4 toWorld2 : TEXCOORD3;


};


v2f vert(a2v v)
{
v2f o;
o.pos = UnityObjectToClipPos(v.vertex);
o.uv.xy = TRANSFORM_TEX(v.texcoord,_MainTex);
o.uv.zw = TRANSFORM_TEX(v.texcoord,_Bump);


float3 worldPos = mul(unity_ObjectToWorld,v.vertex).xyz;
float3 worldNormal = UnityObjectToWorldNormal(v.normal);
float3 worldTangent = UnityObjectToWorldDir(v.tangent.xyz);
float3 worldBiNormal = cross(worldNormal,worldTangent) * v.tangent.w;


o.toWorld0 = float4(worldTangent.x,worldBiNormal.x,worldNormal.x,worldPos.x);
o.toWorld1 = float4(worldTangent.x,worldBiNormal.x,worldNormal.x,worldPos.x);
o.toWorld2 = float4(worldTangent.x,worldBiNormal.x,worldNormal.x,worldPos.x);




return o;
}


fixed4 frag(v2f o) : SV_Target
{
//世界空间计算信息
float3 worldPos = float3(o.toWorld0.w,o.toWorld1.w,o.toWorld2.w);
float3 lightDir = normalize(UnityWorldSpaceLightDir(worldPos)); 
float3 viewDir = normalize(UnityWorldSpaceViewDir(worldPos));


fixed4 packedNormal = tex2D(_Bump,o.uv.zw);
fixed3 bumpNormal =UnpackNormal(packedNormal);
bumpNormal.xy *= _BumpScale;
bumpNormal.z = sqrt(1 - saturate(dot(bumpNormal.xy,bumpNormal.xy)));

bumpNormal  = normalize(half3(dot(o.toWorld0.xyz,bumpNormal),dot(o.toWorld1.xyz,bumpNormal),dot(o.toWorld2.xyz,bumpNormal)));
//tex2D(纹理名,UV坐标)
float3 albedo = tex2D(_MainTex,o.uv.xy).rgb * _Color.rgb;
//环境光
float3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz * albedo;
//漫返射
float3 diffuse = _LightColor0.rgb * albedo * saturate(dot(bumpNormal,lightDir));



float3 halfDir = normalize(lightDir+viewDir);


//高光反射
float3 reflectColor = _LightColor0.rgb * _Specular * pow(saturate(dot(bumpNormal,halfDir)),_Gloss); 


float3 finalColor = reflectColor + diffuse + ambient;
return fixed4(finalColor,1);
}




ENDCG
}
}
}
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/649828
推荐阅读
相关标签
  

闽ICP备14008679号