Skip to content

Commit ade970c

Browse files
Subsurface Scattering
1 parent e08cf04 commit ade970c

File tree

1 file changed

+75
-0
lines changed

1 file changed

+75
-0
lines changed

Diff for: surface_shaders/SubsurfaceScattering.shader

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
// Reference: https://wiki.unity3d.com/index.php/Translucent_Shader
2+
3+
Shader "SubSurfaceScattering"
4+
{
5+
Properties
6+
{
7+
_Color ("Main Color", Color) = (1,1,1,1)
8+
_SpecColor ("Specular Color", Color) = (0.5, 0.5, 0.5, 1)
9+
_Shininess ("Shininess", Range (0.03, 1)) = 0.1
10+
_MainTex ("Albedo (RGB) Gloss (A)", 2D) = "white" {}
11+
_BumpMap ("Normal Map", 2D) = "bump" {}
12+
_TransMap ("Translucency Map", 2D) = "white" {}
13+
_Distortion ("Translucency Distortion", Range(0,0.5)) = 0.2
14+
_Power("Tranlucency Power", Range(0.0,15.0)) = 1.0
15+
_Scale("Translucency Scale", Range(0.0,10.0)) = 2.0
16+
}
17+
SubShader
18+
{
19+
Tags { "RenderType"="Transparent" "Queue" = "Transparent"}
20+
CGPROGRAM
21+
#pragma surface SurfaceShader SubsurfaceScattering
22+
23+
sampler2D _MainTex;
24+
sampler2D _TransMap;
25+
sampler2D _BumpMap;
26+
float4 _Color;
27+
float _Shininess, _Distortion, _Power, _Scale;
28+
29+
struct Input
30+
{
31+
float2 uv_MainTex;
32+
float2 uv_BumpMap;
33+
};
34+
35+
struct SurfaceOutputScattering
36+
{
37+
float3 Albedo;
38+
float3 Normal;
39+
float3 Emission;
40+
float Specular;
41+
float Gloss;
42+
float Alpha;
43+
float3 Translucency;
44+
};
45+
46+
float4 LightingSubsurfaceScattering (SurfaceOutputScattering s, float3 lightDir, float3 viewDir, float atten)
47+
{
48+
float attenuation = (atten * 2);
49+
float3 h = normalize (lightDir + viewDir);
50+
float nl = max(0.0, dot (s.Normal, lightDir));
51+
float nh = max (0.0, dot (s.Normal, h));
52+
float specular = pow (nh, s.Specular * 128.0) * s.Gloss;
53+
float3 diffuse = (s.Albedo * _LightColor0.rgb * nl) * attenuation;
54+
float3 transLight = lightDir + s.Normal * _Distortion;
55+
float transDot = pow(saturate(dot(viewDir, -transLight)),_Power) * _Scale;
56+
diffuse += s.Albedo * _LightColor0.rgb * attenuation * (transDot + _Color.rgb) * s.Translucency;
57+
float4 result = 0;
58+
result.rgb = diffuse + (_LightColor0.rgb * _SpecColor.rgb * specular) * attenuation;
59+
result.a = s.Alpha + _LightColor0.a * _SpecColor.a * specular * atten;
60+
return result;
61+
}
62+
63+
void SurfaceShader (Input IN, inout SurfaceOutputScattering o)
64+
{
65+
half4 albedo = tex2D(_MainTex, IN.uv_MainTex);
66+
o.Albedo = albedo.rgb * _Color.rgb;
67+
o.Gloss = albedo.a;
68+
o.Alpha = albedo.a * _Color.a;
69+
o.Specular = _Shininess;
70+
o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap));
71+
o.Translucency = tex2D(_TransMap, IN.uv_MainTex).rgb;
72+
}
73+
ENDCG
74+
}
75+
}

0 commit comments

Comments
 (0)