-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathLTCGI_Surface_v2.shader
More file actions
146 lines (124 loc) · 5.87 KB
/
Copy pathLTCGI_Surface_v2.shader
File metadata and controls
146 lines (124 loc) · 5.87 KB
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
Shader "LTCGI/Surface (APIv2)"
{
Properties
{
_Color ("Color", Color) = (1,1,1,1)
_MainTex ("Albedo (RGB)", 2D) = "white" {}
[HideInInspector] _LightMap ("(for surface ST only)", 2D) = "white" {}
_BumpMap ("Normal Map", 2D) = "bump" {}
_Metallic ("Metallic", Range(0,1)) = 0.0
_Glossiness ("Smoothness", Range(0,1)) = 0.5
_GlossinessMap ("Smoothness Map", 2D) = "white" {}
[ToggleUI] _MapIsRoughness ("Invert Smoothness Map", Float) = 0.0
[ToggleUI] _LTCGI ("LTCGI enabled", Float) = 1.0
_LTCGI_DiffuseColor ("LTCGI Diffuse Color", Color) = (1,1,1,1)
_LTCGI_SpecularColor ("LTCGI Specular Color", Color) = (1,1,1,1)
[Enum(CullMode)] _CullMode ("Cull Mode", Float) = 2
}
SubShader
{
// The LTCGI tag can either be "ALWAYS" or specify a "Toggle"/"ToggleUI" property.
// It is required so that renderers using this material will be updated by the controller.
Tags { "RenderType"="Opaque" "LTCGI"="_LTCGI" }
LOD 200
Cull [_CullMode]
CGPROGRAM
#pragma surface surf Standard fullforwardshadows
#pragma target 3.0
// This shader demonstrates how to use the APIv2 LTCGI functionality, which has access to per-light callbacks
// preamble: include this first to get access to required types
#include "Packages/at.pimaker.ltcgi/Shaders/LTCGI_structs.cginc"
// then define the accumulator type and callback functions (can forward-declare functions to keep things tidy)
// note the function signatures, especially that the accumulator is "inout" so it will keep modifications between calls
struct accumulator_struct {
// let your imagination run wild on what to accumulate here...
float3 diffuse;
float3 specular;
};
void callback_diffuse(inout accumulator_struct acc, in ltcgi_output output);
void callback_specular(inout accumulator_struct acc, in ltcgi_output output);
// tell LTCGI that we want the V2 API, and which constructs to use
#define LTCGI_V2_CUSTOM_INPUT accumulator_struct
#define LTCGI_V2_DIFFUSE_CALLBACK callback_diffuse
#define LTCGI_V2_SPECULAR_CALLBACK callback_specular
// then include this to finish the deal
#include "Packages/at.pimaker.ltcgi/Shaders/LTCGI.cginc"
// standard shader stuff follows...
sampler2D _MainTex;
sampler2D _BumpMap;
sampler2D _GlossinessMap;
struct Input
{
float2 uv_MainTex;
float2 uv_BumpMap;
float2 uv2_LightMap;
float3 worldPos;
float3 worldNormal;
float facing : SV_IsFrontFace;
INTERNAL_DATA
};
half _Glossiness;
float _MapIsRoughness;
half _Metallic;
half4 _Color;
float _LTCGI;
half4 _LTCGI_DiffuseColor;
half4 _LTCGI_SpecularColor;
float3 get_camera_pos() {
float3 worldCam;
worldCam.x = unity_CameraToWorld[0][3];
worldCam.y = unity_CameraToWorld[1][3];
worldCam.z = unity_CameraToWorld[2][3];
return worldCam;
}
// now we declare LTCGI APIv2 functions for real
void callback_diffuse(inout accumulator_struct acc, in ltcgi_output output) {
// you can do whatever here! check out the ltcgi_output struct in
// "LTCGI_structs.cginc" to see what data you have available
acc.diffuse += output.intensity * output.color * _LTCGI_DiffuseColor;
}
void callback_specular(inout accumulator_struct acc, in ltcgi_output output) {
// same here, this example one is pretty boring though.
// you could accumulate intensity separately for example,
// to emulate total{Specular,Diffuse}Intensity from APIv1
acc.specular += output.intensity * output.color * _LTCGI_SpecularColor;
}
void surf (Input IN, inout SurfaceOutputStandard o)
{
// standard surface shader stuff again...
fixed4 mainColor = tex2D (_MainTex, IN.uv_MainTex) * _Color;
o.Albedo = mainColor.rgb;
o.Metallic = _Metallic;
o.Smoothness = tex2D (_GlossinessMap, IN.uv_MainTex);
if (_MapIsRoughness)
o.Smoothness = 1.0f - o.Smoothness;
o.Smoothness *= _Glossiness;
o.Alpha = mainColor.a;
o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap));
float3 worldSpaceNormal = normalize(WorldNormalVector(IN, o.Normal));
if (IN.facing <= 0.0f) {
worldSpaceNormal = -worldSpaceNormal;
}
if (_LTCGI) {
// and lastly, here's how we call the APIv2 version of LTCGI:
// first, we create the struct that'll be passed through
accumulator_struct acc = (accumulator_struct)0;
// then we make the LTCGI_Contribution call as usual, but with slightly different params
LTCGI_Contribution(
acc, // our accumulator
IN.worldPos, // world position of the shaded point
worldSpaceNormal, // world space normal
normalize(get_camera_pos() - IN.worldPos), // view vector to shaded point, normalized
1.0f - o.Smoothness, // roughness
IN.uv2_LightMap // shadowmap coordinates (the normal Unity ones, they should be in sync with LTCGI maps)
);
// after the call, our accumulator struct will have been modified by our callbacks
// we can now use it to set the output color
o.Emission += acc.specular;
o.Emission += acc.diffuse * mainColor.rgb;
}
}
ENDCG
}
FallBack "Diffuse"
}