/
StandardTriplanar.shader
105 lines (83 loc) · 2.82 KB
/
StandardTriplanar.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
// Standard shader with triplanar mapping
// https://github.com/keijiro/StandardTriplanar
Shader "Standard Triplanar"
{
Properties
{
_Color("", Color) = (1, 1, 1, 1)
_MainTex("", 2D) = "white" {}
_Glossiness("", Range(0, 1)) = 0.5
[Gamma] _Metallic("", Range(0, 1)) = 0
_BumpScale("", Float) = 1
_BumpMap("", 2D) = "bump" {}
_OcclusionStrength("", Range(0, 1)) = 1
_OcclusionMap("", 2D) = "white" {}
_MapScale("", Float) = 1
}
SubShader
{
Tags { "RenderType"="Opaque" }
CGPROGRAM
#pragma surface surf Standard vertex:vert fullforwardshadows addshadow
#pragma shader_feature _NORMALMAP
#pragma shader_feature _OCCLUSIONMAP
#pragma target 3.0
half4 _Color;
sampler2D _MainTex;
half _Glossiness;
half _Metallic;
half _BumpScale;
sampler2D _BumpMap;
half _OcclusionStrength;
sampler2D _OcclusionMap;
half _MapScale;
struct Input
{
float3 localCoord;
float3 localNormal;
};
void vert(inout appdata_full v, out Input data)
{
UNITY_INITIALIZE_OUTPUT(Input, data);
data.localCoord = v.vertex.xyz;
data.localNormal = v.normal.xyz;
}
void surf(Input IN, inout SurfaceOutputStandard o)
{
// Blending factor of triplanar mapping
float3 bf = normalize(abs(IN.localNormal));
bf /= dot(bf, (float3)1);
// Triplanar mapping
float2 tx = IN.localCoord.yz * _MapScale;
float2 ty = IN.localCoord.zx * _MapScale;
float2 tz = IN.localCoord.xy * _MapScale;
// Base color
half4 cx = tex2D(_MainTex, tx) * bf.x;
half4 cy = tex2D(_MainTex, ty) * bf.y;
half4 cz = tex2D(_MainTex, tz) * bf.z;
half4 color = (cx + cy + cz) * _Color;
o.Albedo = color.rgb;
o.Alpha = color.a;
#ifdef _NORMALMAP
// Normal map
half4 nx = tex2D(_BumpMap, tx) * bf.x;
half4 ny = tex2D(_BumpMap, ty) * bf.y;
half4 nz = tex2D(_BumpMap, tz) * bf.z;
o.Normal = UnpackScaleNormal(nx + ny + nz, _BumpScale);
#endif
#ifdef _OCCLUSIONMAP
// Occlusion map
half ox = tex2D(_OcclusionMap, tx).g * bf.x;
half oy = tex2D(_OcclusionMap, ty).g * bf.y;
half oz = tex2D(_OcclusionMap, tz).g * bf.z;
o.Occlusion = lerp((half4)1, ox + oy + oz, _OcclusionStrength);
#endif
// Misc parameters
o.Metallic = _Metallic;
o.Smoothness = _Glossiness;
}
ENDCG
}
FallBack "Diffuse"
CustomEditor "StandardTriplanarInspector"
}