unity-shader-example-animationTree

buildin效果图

urp效果图

AnimationTreeFoliage.shader

AnimationTreeFoliage.shader
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422

Shader "HHF/Example/AnimationTreeFoliage"
{
Properties
{
[MainTexture]_MainTex("MainTex", 2D) = "white" {}
[Header(Settings)][Space(5)]_MainColor("Main Color", Color) = (1,1,1,0)
_AlphaCutoff("Alpha Cutoff", Range( 0 , 1)) = 0.35
[Header(Second Color Settings)][Space(5)][Toggle(_COLOR2ENABLE_ON)] _Color2Enable("Enable", Float) = 0
_SecondColor("Second Color", Color) = (0,0,0,0)
// [KeywordEnum(World_Position,UV_Based)] _SecondColorOverlayType("Overlay Type", Float) = 0
_SecondColorOffset("Offset", Float) = 0
_SecondColorFade("Fade", Range( -1 , 1)) = 0.5
// _WorldScale("World Scale", Float) = 1
[Header(Wind Settings)][Space(5)][Toggle(_ENABLEWIND_ON)] _EnableWind("Enable", Float) = 1
_WindForce("Force", Range( 0 , 2)) = 0.3
_WindWavesScale("Waves Scale", Range( 0 , 1)) = 0.25
_WindSpeed("Speed", Range( 0 , 2)) = 1
}

SubShader
{
Tags { "Queue"="Geometry" "RenderType" = "Opaque" "IgnoreProjector" = "True" "RenderPipeline" = "UniversalPipeline" }
Cull Back

Pass
{
HLSLPROGRAM
// Required to compile gles 2.0 with standard srp library
#pragma prefer_hlslcc gles
#pragma exclude_renderers d3d11_9x
#pragma vertex vert
#pragma fragment frag
#pragma multi_compile_instancing
#pragma shader_feature_local _ENABLEWIND_ON
#pragma shader_feature_local _COLOR2ENABLE_ON
#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Color.hlsl"
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"
#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/UnityInstancing.hlsl"

struct appdata {
float4 vertex : POSITION;
float3 normal : NORMAL;
float2 uv : TEXCOORD0;

#ifdef UNITY_INSTANCING_ENABLED
// 在顶点着色器的输入定义instanceID
UNITY_VERTEX_INPUT_INSTANCE_ID
#endif
};

struct v2f {
float4 positionCS : SV_POSITION;
float3 positionWS : TEXCOORD0;
float3 normalWS : TEXCOORD1;
float2 uv : TEXCOORD2;
float4 positionSS : TEXCOORD3;

#ifdef UNITY_INSTANCING_ENABLED
// 在顶点着色器的输出定义instanceID
UNITY_VERTEX_INPUT_INSTANCE_ID
#endif
};

CBUFFER_START(UnityPerMaterial)
float _WindSpeed;
float _WindWavesScale;
float _WindForce;
float4 _MainColor;
float4 _SecondColor;
// float _WorldScale;
float _SecondColorOffset;
float _SecondColorFade;
float _AlphaCutoff;
CBUFFER_END

TEXTURE2D (_MainTex);SAMPLER(sampler_MainTex);

float3 mod3D289( float3 x )
{
return x - floor( x / 289.0 ) * 289.0;
}

float4 mod3D289( float4 x )
{
return x - floor( x / 289.0 ) * 289.0;
}

float4 permute( float4 x )
{
return mod3D289( ( x * 34.0 + 1.0 ) * x );
}

float4 taylorInvSqrt( float4 r )
{
return 1.79284291400159 - r * 0.85373472095314;
}

float perlinNoise( float3 v )
{
const float2 C = float2( 1.0 / 6.0, 1.0 / 3.0 );
float3 i = floor( v + dot( v, C.yyy ) );
float3 x0 = v - i + dot( i, C.xxx );
float3 g = step( x0.yzx, x0.xyz );
float3 l = 1.0 - g;
float3 i1 = min( g.xyz, l.zxy );
float3 i2 = max( g.xyz, l.zxy );
float3 x1 = x0 - i1 + C.xxx;
float3 x2 = x0 - i2 + C.yyy;
float3 x3 = x0 - 0.5;
i = mod3D289( i);
float4 p = permute( permute( permute( i.z + float4( 0.0, i1.z, i2.z, 1.0 ) ) + i.y + float4( 0.0, i1.y, i2.y, 1.0 ) ) + i.x + float4( 0.0, i1.x, i2.x, 1.0 ) );
float4 j = p - 49.0 * floor( p / 49.0 ); // mod(p,7*7)
float4 x_ = floor( j / 7.0 );
float4 y_ = floor( j - 7.0 * x_ ); // mod(j,N)
float4 x = ( x_ * 2.0 + 0.5 ) / 7.0 - 1.0;
float4 y = ( y_ * 2.0 + 0.5 ) / 7.0 - 1.0;
float4 h = 1.0 - abs( x ) - abs( y );
float4 b0 = float4( x.xy, y.xy );
float4 b1 = float4( x.zw, y.zw );
float4 s0 = floor( b0 ) * 2.0 + 1.0;
float4 s1 = floor( b1 ) * 2.0 + 1.0;
float4 sh = -step( h, 0.0 );
float4 a0 = b0.xzyw + s0.xzyw * sh.xxyy;
float4 a1 = b1.xzyw + s1.xzyw * sh.zzww;
float3 g0 = float3( a0.xy, h.x );
float3 g1 = float3( a0.zw, h.y );
float3 g2 = float3( a1.xy, h.z );
float3 g3 = float3( a1.zw, h.w );
float4 norm = taylorInvSqrt( float4( dot( g0, g0 ), dot( g1, g1 ), dot( g2, g2 ), dot( g3, g3 ) ) );
g0 *= norm.x;
g1 *= norm.y;
g2 *= norm.z;
g3 *= norm.w;
float4 m = max( 0.6 - float4( dot( x0, x0 ), dot( x1, x1 ), dot( x2, x2 ), dot( x3, x3 ) ), 0.0 );
m = m* m;
m = m* m;
float4 px = float4( dot( x0, g0 ), dot( x1, g1 ), dot( x2, g2 ), dot( x3, g3 ) );
return 42.0 * dot( m, px);
}

v2f vert(appdata v) {

v2f o = (v2f)0;

// 在顶点着色器中设置相应的instanceID,默认的坐标变换就已经支持了
UNITY_SETUP_INSTANCE_ID(v);
// 把instanceID从顶点着色器传到片断着色器
UNITY_TRANSFER_INSTANCE_ID(v, o);

// 风
#if _ENABLEWIND_ON
float3 worldPosOri = mul( unity_ObjectToWorld, v.vertex );
float posOffsetWind = _Time.y * _WindSpeed;
float perlin = perlinNoise( (worldPosOri + posOffsetWind) * _WindWavesScale );

v.vertex.xyz += perlin * pow(v.uv.y, 2) * _WindForce ;
v.vertex.w = 1;
#endif

o.positionWS = TransformObjectToWorld(v.vertex.xyz);
o.positionCS = TransformObjectToHClip(v.vertex.xyz);
o.positionSS = ComputeScreenPos( o.positionCS);
o.normalWS = TransformObjectToWorldNormal(v.normal);
o.uv = v.uv;

return o;
}

half4 frag(v2f i) : SV_Target {

// 在片断着色器中设置相应的instanceID
UNITY_SETUP_INSTANCE_ID(i);

half4 c = 1;

// 主纹理
float4 mainTex = SAMPLE_TEXTURE2D( _MainTex, sampler_MainTex, i.uv );
clip(mainTex.a - _AlphaCutoff );

float4 mainColor = mainTex * _MainColor;
float4 combineColor = mainColor ;

// 双色处理
#if _COLOR2ENABLE_ON
float secondColorOffset2 = i.uv.y;
// #if _SECONDCOLOROVERLAYTYPE_WORLD_POSITION
// secondColorOffset2 = perlinNoise( i.positionWS * _WorldScale );
// secondColorOffset2 = secondColorOffset2 * 0.5 + 0.5;
// #endif

float secondColorMask = saturate(( secondColorOffset2 + _SecondColorOffset ) * ( _SecondColorFade * 2 ));
float4 secColor = mainTex * _SecondColor;
combineColor = lerp(mainColor, secColor, secondColorMask);
#endif

// 光照处理
// 漫反射
Light mainLight = GetMainLight();
half3 N = normalize(i.normalWS);
half3 L = normalize(mainLight.direction);
half3 diffuse = mainLight.color.rgb * saturate(dot(N, L));
diffuse = diffuse * 0.5 + 0.5 ;
// return half4(diffuse.rgb, 1);
// 间接光照
float3 indirectLight = diffuse + i.normalWS * 0.0001;
// return half4(indirectLight, 1);
// 直接光照
// float3 L2 = Unity_SafeNormalize( UnityWorldSpaceLightDir( i.positionWS ));
// float3 L2 = SafeNormalize(mainLight.direction);
float L2ON = dot( L, N );
float3 directLight = saturate(L2ON) * mainLight.color;
// return half4(directLight, 1);

c.rgb = (indirectLight + directLight) * combineColor.rgb;

return c;
}

ENDHLSL
}
}

SubShader
{
Tags{ "RenderType" = "Opaque" "Queue" = "Geometry" }
Cull Back

Pass {
// Cull Off

CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma multi_compile_instancing
#pragma shader_feature_local _ENABLEWIND_ON
#pragma shader_feature_local _COLOR2ENABLE_ON
// #pragma shader_feature_local _SECONDCOLOROVERLAYTYPE_WORLD_POSITION _SECONDCOLOROVERLAYTYPE_UV_BASED

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

struct appdata {
float4 vertex : POSITION;
float3 normal : NORMAL;
float2 uv : TEXCOORD0;

#ifdef UNITY_INSTANCING_ENABLED
// 在顶点着色器的输入定义instanceID
UNITY_VERTEX_INPUT_INSTANCE_ID
#endif
};

struct v2f {
float4 positionCS : SV_POSITION;
float3 positionWS : TEXCOORD0;
float3 normalWS : TEXCOORD1;
float2 uv : TEXCOORD2;
float4 positionSS : TEXCOORD3;

#ifdef UNITY_INSTANCING_ENABLED
// 在顶点着色器的输出定义instanceID
UNITY_VERTEX_INPUT_INSTANCE_ID
#endif
};

float _WindSpeed;
float _WindWavesScale;
float _WindForce;
sampler2D _MainTex;
float4 _MainColor;
float4 _SecondColor;
// float _WorldScale;
float _SecondColorOffset;
float _SecondColorFade;
float _AlphaCutoff;

float3 mod3D289( float3 x )
{
return x - floor( x / 289.0 ) * 289.0;
}

float4 mod3D289( float4 x )
{
return x - floor( x / 289.0 ) * 289.0;
}

float4 permute( float4 x )
{
return mod3D289( ( x * 34.0 + 1.0 ) * x );
}

float4 taylorInvSqrt( float4 r )
{
return 1.79284291400159 - r * 0.85373472095314;
}

float perlinNoise( float3 v )
{
const float2 C = float2( 1.0 / 6.0, 1.0 / 3.0 );
float3 i = floor( v + dot( v, C.yyy ) );
float3 x0 = v - i + dot( i, C.xxx );
float3 g = step( x0.yzx, x0.xyz );
float3 l = 1.0 - g;
float3 i1 = min( g.xyz, l.zxy );
float3 i2 = max( g.xyz, l.zxy );
float3 x1 = x0 - i1 + C.xxx;
float3 x2 = x0 - i2 + C.yyy;
float3 x3 = x0 - 0.5;
i = mod3D289( i);
float4 p = permute( permute( permute( i.z + float4( 0.0, i1.z, i2.z, 1.0 ) ) + i.y + float4( 0.0, i1.y, i2.y, 1.0 ) ) + i.x + float4( 0.0, i1.x, i2.x, 1.0 ) );
float4 j = p - 49.0 * floor( p / 49.0 ); // mod(p,7*7)
float4 x_ = floor( j / 7.0 );
float4 y_ = floor( j - 7.0 * x_ ); // mod(j,N)
float4 x = ( x_ * 2.0 + 0.5 ) / 7.0 - 1.0;
float4 y = ( y_ * 2.0 + 0.5 ) / 7.0 - 1.0;
float4 h = 1.0 - abs( x ) - abs( y );
float4 b0 = float4( x.xy, y.xy );
float4 b1 = float4( x.zw, y.zw );
float4 s0 = floor( b0 ) * 2.0 + 1.0;
float4 s1 = floor( b1 ) * 2.0 + 1.0;
float4 sh = -step( h, 0.0 );
float4 a0 = b0.xzyw + s0.xzyw * sh.xxyy;
float4 a1 = b1.xzyw + s1.xzyw * sh.zzww;
float3 g0 = float3( a0.xy, h.x );
float3 g1 = float3( a0.zw, h.y );
float3 g2 = float3( a1.xy, h.z );
float3 g3 = float3( a1.zw, h.w );
float4 norm = taylorInvSqrt( float4( dot( g0, g0 ), dot( g1, g1 ), dot( g2, g2 ), dot( g3, g3 ) ) );
g0 *= norm.x;
g1 *= norm.y;
g2 *= norm.z;
g3 *= norm.w;
float4 m = max( 0.6 - float4( dot( x0, x0 ), dot( x1, x1 ), dot( x2, x2 ), dot( x3, x3 ) ), 0.0 );
m = m* m;
m = m* m;
float4 px = float4( dot( x0, g0 ), dot( x1, g1 ), dot( x2, g2 ), dot( x3, g3 ) );
return 42.0 * dot( m, px);
}

v2f vert(appdata v) {

v2f o = (v2f)0;

// 在顶点着色器中设置相应的instanceID,默认的坐标变换就已经支持了
UNITY_SETUP_INSTANCE_ID(v);
// 把instanceID从顶点着色器传到片断着色器
UNITY_TRANSFER_INSTANCE_ID(v, o);

// 风
#if _ENABLEWIND_ON
float3 worldPosOri = mul( unity_ObjectToWorld, v.vertex );
float posOffsetWind = _Time.y * _WindSpeed;
float perlin = perlinNoise( (worldPosOri + posOffsetWind) * _WindWavesScale );

v.vertex.xyz += perlin * pow(v.uv.y, 2) * _WindForce ;
v.vertex.w = 1;
#endif

o.positionWS = mul(UNITY_MATRIX_M, v.vertex);
o.positionCS = UnityObjectToClipPos(v.vertex);
o.positionSS = ComputeScreenPos(o.positionCS);
o.normalWS = UnityObjectToWorldNormal(v.normal);
o.uv = v.uv;

return o;
}

half4 frag(v2f i) : SV_Target {

// 在片断着色器中设置相应的instanceID
UNITY_SETUP_INSTANCE_ID(i);

half4 c = 1;

// 主纹理
float4 mainTex = tex2D( _MainTex, i.uv );
clip(mainTex.a - _AlphaCutoff );

float4 mainColor = mainTex * _MainColor;
float4 combineColor = mainColor ;

// 双色处理
#if _COLOR2ENABLE_ON
float secondColorOffset2 = i.uv.y;
// #if _SECONDCOLOROVERLAYTYPE_WORLD_POSITION
// secondColorOffset2 = perlinNoise( i.positionWS * _WorldScale );
// secondColorOffset2 = secondColorOffset2 * 0.5 + 0.5;
// #endif

float secondColorMask = saturate(( secondColorOffset2 + _SecondColorOffset ) * ( _SecondColorFade * 2 ));
float4 secColor = mainTex * _SecondColor;
combineColor = lerp(mainColor, secColor, secondColorMask);
#endif

// 光照处理
// 漫反射
half3 N = normalize(i.normalWS);
half3 L = normalize(_WorldSpaceLightPos0.xyz);
half3 diffuse = _LightColor0.rgb * saturate(dot(N, L));
diffuse = diffuse * 0.5 + 0.5 ;
// return half4(diffuse.rgb, 1);
// 间接光照
float3 indirectLight = diffuse + i.normalWS * 0.0001;
// return half4(indirectLight, 1);
// 直接光照
// float3 L2 = Unity_SafeNormalize( UnityWorldSpaceLightDir( i.positionWS ));
float L2ON = dot( L, N );
float3 directLight = saturate(L2ON) * _LightColor0;
// return half4(directLight, 1);

c.rgb = (indirectLight + directLight) * combineColor;

return c;
}

ENDCG
}
}
}

AnimationTreeSurface.shader

AnimationTreeSurface.shader
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
Shader "HHF/Example/AnimationTreeSurface"
{
Properties
{
[MainTexture]_MainTex("MainTex", 2D) = "white" {}
[Normal][SingleLineTexture]_Normal("Normal", 2D) = "bump" {}
[Header(Settings)][Space(5)]_Color("Color", Color) = (1,1,1,0)
_NormalScale("Normal", Float) = 1
}

SubShader
{
Tags{"RenderPipeline"="UniversalPipeline" "RenderType"="Opaque"}
Cull Back

Pass
{
HLSLPROGRAM
#pragma prefer_hlslcc gles
#pragma exclude_renderers d3d11_9x
#pragma vertex vert
#pragma fragment frag
#pragma multi_compile_instancing

#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Color.hlsl"
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/ShaderGraphFunctions.hlsl"

struct appdata {
float4 vertex : POSITION;
float2 uv : TEXCOORD0;

#ifdef UNITY_INSTANCING_ENABLED
// 在顶点着色器的输入定义instanceID
UNITY_VERTEX_INPUT_INSTANCE_ID
#endif
};

struct v2f {
float4 positionCS : SV_POSITION;
float3 positionWS : TEXCOORD0;
float2 uv : TEXCOORD2;

#ifdef UNITY_INSTANCING_ENABLED
// 在顶点着色器的输出定义instanceID
UNITY_VERTEX_INPUT_INSTANCE_ID
#endif
};

CBUFFER_START(UnityPerMaterial)
float4 _MainTex_ST;
float4 _LightTex_ST;
float _NormalScale;
half4 _Color;
CBUFFER_END

TEXTURE2D (_MainTex);SAMPLER(sampler_MainTex);
TEXTURE2D (_Normal);SAMPLER(sampler_Normal);

v2f vert(appdata v) {
v2f o = (v2f)0;

// 在顶点着色器中设置相应的instanceID,默认的坐标变换就已经支持了
UNITY_SETUP_INSTANCE_ID(v);
// 把instanceID从顶点着色器传到片断着色器
UNITY_TRANSFER_INSTANCE_ID(v, o);

o.positionCS = TransformObjectToHClip(v.vertex.xyz);
o.positionWS = TransformObjectToWorld(v.vertex.xyz);
o.uv = TRANSFORM_TEX(v.uv, _MainTex);

return o;
}

half4 frag(v2f i) : SV_Target {

// 在片断着色器中设置相应的instanceID
UNITY_SETUP_INSTANCE_ID(i);

half4 c = 1;

float4 mainTex = SAMPLE_TEXTURE2D( _MainTex, sampler_MainTex, i.uv );
float4 mainColor = mainTex * _Color;

// 光照处理
// 漫反射
Light mainLight = GetMainLight();
float3 normalMap = UnpackNormalScale( SAMPLE_TEXTURE2D( _Normal, sampler_Normal, i.uv ), _NormalScale);
half3 N = normalMap; //normalize(Normal);
half3 L = normalize(mainLight.direction.xyz);
half3 diffuse = mainLight.color.rgb * saturate(dot(N, L));
diffuse = diffuse * 0.5 + 0.5 ;
// 间接光照
float3 indirectLight = diffuse + normalMap * 0.0001;
// 直接光照
// float3 L2 = Unity_SafeNormalize( UnityWorldSpaceLightDir( i.positionWS ));
float L2ON = dot( L, N );
float3 directLight = saturate(L2ON) * mainLight.color;
c.rgb = (indirectLight + directLight) * mainColor;

return c;
}

ENDHLSL
}
}

SubShader
{
Tags{ "RenderType" = "Opaque" "Queue" = "Geometry" }
Cull Back

Pass {
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma multi_compile_instancing

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

struct appdata {
float4 vertex : POSITION;
float2 uv : TEXCOORD0;

#ifdef UNITY_INSTANCING_ENABLED
// 在顶点着色器的输入定义instanceID
UNITY_VERTEX_INPUT_INSTANCE_ID
#endif
};

struct v2f {
float4 positionCS : SV_POSITION;
float3 positionWS : TEXCOORD0;
float2 uv : TEXCOORD2;

#ifdef UNITY_INSTANCING_ENABLED
// 在顶点着色器的输出定义instanceID
UNITY_VERTEX_INPUT_INSTANCE_ID
#endif
};

sampler2D _Normal;
float _NormalScale;
float4 _Color;
sampler2D _MainTex; float4 _MainTex_ST;

v2f vert(appdata v) {
v2f o = (v2f)0;

// 在顶点着色器中设置相应的instanceID,默认的坐标变换就已经支持了
UNITY_SETUP_INSTANCE_ID(v);
// 把instanceID从顶点着色器传到片断着色器
UNITY_TRANSFER_INSTANCE_ID(v, o);

o.positionCS = UnityObjectToClipPos(v.vertex.xyz);
o.positionWS = mul(UNITY_MATRIX_M, v.vertex);
o.uv = TRANSFORM_TEX(v.uv, _MainTex);

return o;
}

half4 frag(v2f i) : SV_Target {

// 在片断着色器中设置相应的instanceID
UNITY_SETUP_INSTANCE_ID(i);

half4 c = 1;

float4 mainTex = tex2D( _MainTex, i.uv );
float4 mainColor = mainTex * _Color;

// 光照处理
// 漫反射
float3 normalMap = UnpackScaleNormal( tex2D( _Normal, i.uv ), _NormalScale);
half3 N = normalMap; //normalize(Normal);
half3 L = normalize(_WorldSpaceLightPos0.xyz);
half3 diffuse = _LightColor0.rgb * saturate(dot(N, L));
diffuse = diffuse * 0.5 + 0.5 ;
// 间接光照
float3 indirectLight = diffuse + normalMap * 0.0001;
// 直接光照
// float3 L2 = Unity_SafeNormalize( UnityWorldSpaceLightDir( i.positionWS ));
float L2ON = dot( L, N );
float3 directLight = saturate(L2ON) * _LightColor0;
c.rgb = (indirectLight + directLight) * mainColor;

return c;
}

ENDCG
}
}
}