yum-archive/Tooner

A toon shader for Unity's BIRP.

git clone https://git.yummers.dev/yum-archive/Tooner

yumSwitch to Mochie's BRDF2af60b2

master
5.9 KiB176 linesraw
1#ifndef MOCHIE_STANDARD_SSR_INCLUDED
2#define MOCHIE_STANDARD_SSR_INCLUDED
3
4/*
5 * MIT License
6 *
7 * Copyright (c) 2020 MochiesCode
8 *
9 * Permission is hereby granted, free of charge, to any person obtaining a copy
10 * of this software and associated documentation files (the "Software"), to
11 * deal
12 * in the Software without restriction, including without limitation the rights
13 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 * copies of the Software, and to permit persons to whom the Software is
15 * furnished to do so, subject to the following conditions:
16 *
17 * The above copyright notice and this permission notice shall be included in
18 * all
19 * copies or substantial portions of the Software.
20 *
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26 * FROM,
27 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
28 * THE
29 * SOFTWARE.
30 */
31
32//-----------------------------------------------------------------------------------
33// SCREEN SPACE REFLECTIONS
34// 
35// Original made by error.mdl, Toocanzs, and Xiexe.
36// Reworked and updated by Mochie
37//-----------------------------------------------------------------------------------
38
39#if SSR_ENABLED
40
41bool IsInMirror(){
42  return unity_CameraProjection[2][0] != 0.f || unity_CameraProjection[2][1] != 0.f;
43}
44
45float3 GetBlurredGP(const sampler2D ssrg, const float2 texelSize, const float2 uvs, const float dim){
46	float2 pixSize = 2/texelSize;
47	float dimFloored = floor(dim);
48	float center = floor(dim*0.5);
49	float3 refTotal = float3(0,0,0);
50	for (int i = 0; i < dimFloored; i++){
51		for (int j = 0; j < dimFloored; j++){
52			float4 refl = tex2Dlod(ssrg, float4(uvs.x + pixSize.x*(i-center), uvs.y + pixSize.y*(j-center),0,0));
53			refTotal += refl.rgb;
54		}
55	}
56	return refTotal/(dimFloored*dimFloored);
57}
58
59float4 ReflectRay(float3 reflectedRay, float3 rayDir, float _LRad, float _SRad, float _Step, float noise, const int maxIterations){
60
61	#if UNITY_SINGLE_PASS_STEREO || defined(UNITY_STEREO_INSTANCING_ENABLED) || defined(UNITY_STEREO_MULTIVIEW_ENABLED)
62		half x_min = 0.5*unity_StereoEyeIndex;
63		half x_max = 0.5 + 0.5*unity_StereoEyeIndex;
64	#else
65		half x_min = 0.0;
66		half x_max = 1.0;
67	#endif
68	
69	reflectedRay = mul(UNITY_MATRIX_V, float4(reflectedRay, 1));
70	rayDir = mul(UNITY_MATRIX_V, float4(rayDir, 0));
71	int totalIterations = 0;
72	int direction = 1;
73	float3 finalPos = 0;
74	float step = _Step;
75	float lRad = _LRad;
76	float sRad = _SRad;
77
78	for (int i = 0; i < maxIterations; i++){
79		totalIterations = i;
80		float4 spos = ComputeGrabScreenPos(mul(UNITY_MATRIX_P, float4(reflectedRay, 1)));
81		float2 uvDepth = spos.xy / spos.w;
82		UNITY_BRANCH
83		if (uvDepth.x > x_max || uvDepth.x < x_min || uvDepth.y > 1 || uvDepth.y < 0){
84			break;
85		}
86
87		float rawDepth = DecodeFloatRG(SAMPLE_DEPTH_TEXTURE_LOD(_CameraDepthTexture,float4(uvDepth,0,0)));
88		float linearDepth = Linear01Depth(rawDepth);
89		float sampleDepth = -reflectedRay.z;
90		float realDepth = linearDepth * _ProjectionParams.z;
91		float depthDifference = abs(sampleDepth - realDepth);
92
93		if (depthDifference < lRad){ 
94			if (direction == 1){
95				if(sampleDepth > (realDepth - sRad)){
96					if(sampleDepth < (realDepth + sRad)){
97						finalPos = reflectedRay;
98						break;
99					}
100					direction = -1;
101					step = step*0.1;
102				}
103			}
104			else {
105				if(sampleDepth < (realDepth + sRad)){
106					direction = 1;
107					step = step*0.1;
108				}
109			}
110		}
111		reflectedRay = reflectedRay + direction*step*rayDir;
112		step += step*(0.025 + 0.005*noise);
113		lRad += lRad*(0.025 + 0.005*noise);
114		sRad += sRad*(0.025 + 0.005*noise);
115	}
116	return float4(finalPos, totalIterations);
117}
118
119float4 GetSSR(const float3 wPos, const float3 viewDir, float3 rayDir, const half3 faceNormal, float smoothness, float3 albedo, float metallic, float2 screenUVs, float4 screenPos){
120	
121	float FdotR = dot(faceNormal, rayDir.xyz);
122	float roughness = 1-smoothness;
123
124	UNITY_BRANCH
125	if (IsInMirror() || FdotR < 0 || roughness > 0.65){
126		return 0;
127	}
128	else {
129		float4 noiseUvs = screenPos;
130		noiseUvs.xy = (noiseUvs.xy * _GrabTexture_TexelSize.zw) / (_NoiseTexSSR_TexelSize.zw * noiseUvs.w);	
131		float4 noiseRGBA = tex2Dlod(_NoiseTexSSR, float4(noiseUvs.xy,0,0));
132		float noise = noiseRGBA.r;
133		
134		float3 reflectedRay = wPos + (_SSRHeight*_SSRHeight/FdotR + noise*_SSRHeight)*rayDir;
135		float4 finalPos = ReflectRay(reflectedRay, rayDir, _SSRHeight, 0.02, _SSRHeight, noise, 50);
136		float totalSteps = finalPos.w;
137		finalPos.w = 1;
138		
139		if (!any(finalPos.xyz)){
140			return 0;
141		}
142		
143		float4 uvs = UNITY_PROJ_COORD(ComputeGrabScreenPos(mul(UNITY_MATRIX_P, finalPos)));
144		uvs.xy = uvs.xy / uvs.w;
145
146		#if UNITY_SINGLE_PASS_STEREO || defined(UNITY_STEREO_INSTANCING_ENABLED) || defined(UNITY_STEREO_MULTIVIEW_ENABLED)
147			float xfade = 1;
148		#else
149			float xfade = smoothstep(0, _EdgeFade, uvs.x) * smoothstep(1, 1-_EdgeFade, uvs.x); //Fade x uvs out towards the edges
150		#endif
151		float yfade = smoothstep(0, _EdgeFade, uvs.y)*smoothstep(1, 1-_EdgeFade, uvs.y); //Same for y
152		// float lengthFade = smoothstep(1, 0, 2*(totalSteps / 50)-1);
153		float smoothFade = smoothstep(0.65, 0.5, 1-smoothness);
154		float reflectionAlpha = xfade * yfade * smoothFade; // * lengthFade;
155
156
157		float4 reflection = 0;
158		if (reflectionAlpha > 0){
159			float blurFac = max(1,min(12, 12 * (-2)*(smoothness-1)));
160			// if (blurFac > 1){
161				reflection.rgb = GetBlurredGP(_GrabTexture, _GrabTexture_TexelSize.zw, uvs.xy, blurFac);
162			// }
163			// else {
164			// 	reflection.rgb = tex2Dlod(_GrabTexture, float4(uvs.xy,0,0)).rgb;
165			// }
166			reflection.rgb = lerp(reflection.rgb, reflection.rgb*albedo.rgb,smoothstep(0, 1.75, metallic));
167			reflection.a = reflectionAlpha;
168		}
169
170		return max(0,reflection);
171	}	
172}
173
174#endif
175
176#endif // MOCHIE_STANDARD_SSR_INCLUDED