当前位置:   article > 正文

Unity Shader学习:皮毛渲染_unity shader毛发代码

unity shader毛发代码

Unity Shader学习:皮毛渲染

主要思路:将顶点延法线挤出,一层一层pass往外叠加形成毛发效果。
原文链接:https://blog.csdn.net/qq_24153371/article/details/81742337

在这里插入图片描述
噪声图:
在这里插入图片描述

cginc部分:

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

float4 _Color;
float4 _Specular;
float _Shininess;
sampler2D _MainTex;
float4 _MainTex_ST;
sampler2D _FurTex;
float4 _FurTex_ST;
float _FurLength;
float _FurDensity;
float _FurThinness;
float _FurShading;
float _RimPower;

struct v2f {
   
	float4 pos:SV_POSITION;
	float4 uv:TEXCOORD0;
	float3 worldNormal:TEXCOORD1;
	float3 worldPos:TEXCOORD2;
};

v2f vert_surface(appdata_base v) {
   
	v2f o;
	o.pos = UnityObjectToClipPos(v.vertex);
	o.uv.xy = TRANSFORM_TEX(v.texcoord, _MainTex);
	o.worldNormal = UnityObjectToWorldNormal(v.normal);
	o.worldPos = mul(unity_ObjectToWorld, v.vertex).xyz;
	return o;
}

//lambert+blinn-phone
float4 frag_surface(v2f i) :SV_Target{
   
	float3 worldNormal = normalize(i.worldNormal);
	float3 worldLight = normalize(_WorldSpaceLightPos0.xyz);
	float3 worldView = normalize(_WorldSpaceCameraPos.xyz - i.worldPos.xyz);
	float3 worldHalf = normalize(worldView + worldLight);
	float3 albedo = tex2D(_MainTex, i.uv.xy).rgb *_Color;	
	float3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz * albedo;
	float3 diffuse = _LightColor0.rgb*albedo*saturate(dot(worldNormal, worldLight));
	float3 specular = _LightColor0.rgb*_Specular.rgb*pow(saturate(dot(worldNormal, worldHalf)), _Shininess);
	float3 color = ambient + diffuse + specular;
	return float4(color, 1.0);
}

v2f vert_base(appdata_base v) {
   
	v2f o;
	//每个pass延法线挤出顶点
	float3 P = v.vertex.xyz + v.normal*_FurLength*FURSTEP;
	o.pos = UnityObjectToClipPos(float4(P, 1.0));
	o.uv.xy = TRANSFORM_TEX(v.texcoord, _MainTex);
	o.uv.<
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/108509
推荐阅读
相关标签
  

闽ICP备14008679号