Skip to content

Commit 766ba05

Browse files
author
SamYeonKim
committed
diffuse_ambient_lightprobe 1차 추가
1 parent a81f05b commit 766ba05

10 files changed

+574
-18
lines changed

Assets/Tutorials/diffuse_ambient_lightprobe/diffuse_ambient_lightprobe.mat

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ Material:
3939
m_Scale: {x: 1, y: 1}
4040
m_Offset: {x: 0, y: 0}
4141
- _MainTex:
42-
m_Texture: {fileID: 2800000, guid: ddee01e2719a1394d95c1f33ec367a3a, type: 3}
42+
m_Texture: {fileID: 10910, guid: 0000000000000000f000000000000000, type: 0}
4343
m_Scale: {x: 1, y: 1}
4444
m_Offset: {x: 0, y: 0}
4545
- _MetallicGlossMap:
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# Abstract
2+
3+
diffuse lighting, ambient, light probe 를 적용해보자
4+
5+
# Shader
6+
7+
```c
8+
Shader "UnityShaderTutorial/diffuse_ambient_lightprobe" {
9+
Properties
10+
{
11+
[NoScaleOffset] _MainTex ("Texture", 2D) = "white" {}
12+
}
13+
SubShader
14+
{
15+
Pass
16+
{
17+
Tags {"LightMode"="ForwardBase"}
18+
19+
CGPROGRAM
20+
#pragma vertex vert
21+
#pragma fragment frag
22+
#include "UnityCG.cginc"
23+
#include "UnityLightingCommon.cginc"
24+
25+
struct v2f
26+
{
27+
float2 uv : TEXCOORD0;
28+
fixed4 diff : COLOR0;
29+
float4 vertex : SV_POSITION;
30+
};
31+
32+
v2f vert (appdata_base v)
33+
{
34+
v2f o;
35+
o.vertex = UnityObjectToClipPos(v.vertex);
36+
o.uv = v.texcoord;
37+
half3 worldNormal = UnityObjectToWorldNormal(v.normal);
38+
half nl = max(0, dot(worldNormal, _WorldSpaceLightPos0.xyz));
39+
o.diff = nl * _LightColor0;
40+
41+
// the only difference from previous shader:
42+
// in addition to the diffuse lighting from the main light,
43+
// add illumination from ambient or light probes
44+
// ShadeSH9 function from UnityCG.cginc evaluates it,
45+
// using world space normal
46+
o.diff.rgb += ShadeSH9(half4(worldNormal,1));
47+
return o;
48+
}
49+
50+
sampler2D _MainTex;
51+
52+
fixed4 frag (v2f i) : SV_Target
53+
{
54+
fixed4 col = tex2D(_MainTex, i.uv);
55+
col *= i.diff;
56+
return col;
57+
}
58+
ENDCG
59+
}
60+
}
61+
}
62+
```
63+
64+
# Description
65+
66+
이전챕터인 [diffuse](???) 와의 차이는 `환경광 (AmbientLight), 라이트프로브` 까지 적용된다는 점이다.
67+
68+
```
69+
o.diff.rgb += ShadeSH9(half4(worldNormal,1));
70+
```

Assets/Tutorials/diffuse_ambient_lightprobe/diffuse_ambient_lightprobe.md.meta

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Tutorials/diffuse_ambient_lightprobe/diffuse_ambient_lightprobe.meta

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Tutorials/diffuse_ambient_lightprobe/diffuse_ambient_lightprobe.shader

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,49 @@ Shader "UnityShaderTutorial/diffuse_ambient_lightprobe" {
1616
#include "UnityCG.cginc"
1717
#include "UnityLightingCommon.cginc"
1818

19+
// normal should be normalized, w=1.0
20+
half3 SHEvalLinearL0L1s (half4 normal)
21+
{
22+
half3 x;
23+
24+
// Linear (L1) + constant (L0) polynomial terms
25+
x.r = dot(unity_SHAr,normal);
26+
x.g = dot(unity_SHAg,normal);
27+
x.b = dot(unity_SHAb,normal);
28+
29+
return x;
30+
}
31+
32+
// normal should be normalized, w=1.0
33+
half3 SHEvalLinearL2s (half4 normal)
34+
{
35+
half3 x1, x2;
36+
// 4 of the quadratic (L2) polynomials
37+
half4 vB = normal.xyzz * normal.yzzx;
38+
x1.r = dot(unity_SHBr,vB);
39+
x1.g = dot(unity_SHBg,vB);
40+
x1.b = dot(unity_SHBb,vB);
41+
42+
// Final (5th) quadratic (L2) polynomial
43+
half vC = normal.x*normal.x - normal.y*normal.y;
44+
x2 = unity_SHC.rgb * vC;
45+
46+
return x1 + x2;
47+
}
48+
49+
// normal should be normalized, w=1.0
50+
// output in active color space
51+
half3 ShadeSH9s (half4 normal)
52+
{
53+
// Linear + constant polynomial terms
54+
half3 res = SHEvalLinearL0L1s (normal);
55+
56+
// Quadratic polynomials
57+
res += SHEvalLinearL2s (normal);
58+
59+
return res;
60+
}
61+
1962
struct v2f
2063
{
2164
float2 uv : TEXCOORD0;
@@ -37,7 +80,7 @@ Shader "UnityShaderTutorial/diffuse_ambient_lightprobe" {
3780
// add illumination from ambient or light probes
3881
// ShadeSH9 function from UnityCG.cginc evaluates it,
3982
// using world space normal
40-
o.diff.rgb += ShadeSH9(half4(worldNormal,1));
83+
o.diff.rgb += ShadeSH9s(half4(worldNormal,1));
4184
return o;
4285
}
4386

0 commit comments

Comments
 (0)