yum/3ner
A toon shader for Unity's BIRP.
git clone https://git.yummers.dev/yum/3ner
50e4514
master
1// Unity built-in shader source. Copyright (c) 2016 Unity Technologies. MIT license (see license.txt) 2 3#ifndef UNITY_IMAGE_BASED_LIGHTING_INCLUDED 4#define UNITY_IMAGE_BASED_LIGHTING_INCLUDED 5 6#include "pbr_utils.cginc" 7#include "UnityStandardUtils.cginc" 8 9// ---------------------------------------------------------------------------- 10// GlossyEnvironment - Function to integrate the specular lighting with default sky or reflection probes 11// ---------------------------------------------------------------------------- 12struct Unity_GlossyEnvironmentData 13{ 14 // - Deferred case have one cubemap 15 // - Forward case can have two blended cubemap (unusual should be deprecated). 16 17 // Surface properties use for cubemap integration 18 half roughness; // CAUTION: This is perceptualRoughness but because of compatibility this name can't be change :( 19 half3 reflUVW; 20}; 21 22// ---------------------------------------------------------------------------- 23 24Unity_GlossyEnvironmentData UnityGlossyEnvironmentSetup(half Smoothness, half3 worldViewDir, half3 Normal, half3 fresnel0) 25{ 26 Unity_GlossyEnvironmentData g; 27 28 g.roughness /* perceptualRoughness */ = smoothnessToPerceptualRoughness(Smoothness); 29 g.reflUVW = reflect(-worldViewDir, Normal); 30 31 return g; 32} 33 34// ---------------------------------------------------------------------------- 35half perceptualRoughnessToMipmapLevel(half perceptualRoughness) 36{ 37 return perceptualRoughness * UNITY_SPECCUBE_LOD_STEPS; 38} 39 40// ---------------------------------------------------------------------------- 41half mipmapLevelToPerceptualRoughness(half mipmapLevel) 42{ 43 return mipmapLevel / UNITY_SPECCUBE_LOD_STEPS; 44} 45 46// ---------------------------------------------------------------------------- 47half3 Unity_GlossyEnvironment (UNITY_ARGS_TEXCUBE(tex), half4 hdr, Unity_GlossyEnvironmentData glossIn) 48{ 49 half perceptualRoughness = glossIn.roughness /* perceptualRoughness */ ; 50 51// TODO: CAUTION: remap from Morten may work only with offline convolution, see impact with runtime convolution! 52// For now disabled 53#if 0 54 float m = PerceptualRoughnessToRoughness(perceptualRoughness); // m is the real roughness parameter 55 const float fEps = 1.192092896e-07F; // smallest such that 1.0+FLT_EPSILON != 1.0 (+1e-4h is NOT good here. is visibly very wrong) 56 float n = (2.0/max(fEps, m*m))-2.0; // remap to spec power. See eq. 21 in --> https://dl.dropboxusercontent.com/u/55891920/papers/mm_brdf.pdf 57 58 n /= 4; // remap from n_dot_h formulatino to n_dot_r. See section "Pre-convolved Cube Maps vs Path Tracers" --> https://s3.amazonaws.com/docs.knaldtech.com/knald/1.0.0/lys_power_drops.html 59 60 perceptualRoughness = pow( 2/(n+2), 0.25); // remap back to square root of real roughness (0.25 include both the sqrt root of the conversion and sqrt for going from roughness to perceptualRoughness) 61#else 62 // MM: came up with a surprisingly close approximation to what the #if 0'ed out code above does. 63 perceptualRoughness = perceptualRoughness*(1.7 - 0.7*perceptualRoughness); 64#endif 65 66 67 half mip = perceptualRoughnessToMipmapLevel(perceptualRoughness); 68 half3 R = glossIn.reflUVW; 69 half4 rgbm = UNITY_SAMPLE_TEXCUBE_LOD(tex, R, mip); 70 71 return DecodeHDR(rgbm, hdr); 72} 73#endif // UNITY_IMAGE_BASED_LIGHTING_INCLUDED