Skip to content

Instantly share code, notes, and snippets.

@cortvi
Last active June 20, 2023 14:18
  • Star 12 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save cortvi/094f5233be6db0492b386cb494aa709a to your computer and use it in GitHub Desktop.
A PBR two-sided cloth shader that works correctly with Unity's lighting.

Extended from this article

two-sided custom shader cover image

/// Created by @cortvi
Shader "Custom/Two sided"
{
Properties
{
// You can learn everything about properties here:
// https://docs.unity3d.com/Manual/SL-Properties.html
_Color ("Main Color", Color) = (1,1,1,1)
_MainTex ("Albedo (RGB)", 2D) = "white" {}
_Cutoff ("Cutout threshold", Range(0.0,1.0)) = 0.1
[NoScaleOffset] _SmoothnessMap ("Roughness map (A)", 2D) = "black" {}
_Smoothness ("Flat roughness", Range(0.0, 1.0)) = 0.0
[NoScaleOffset][Normal] _BumpMap ("Normal map (RGB)", 2D) = "bump" {}
_BumpScale ("Normal scale", Float) = 1.0
}
SubShader
{
Tags { "Queue"="AlphaTest" "RenderType"="TransparentCutout" }
CGINCLUDE
// Whatever you write inside here
// will be included in every pass
struct Input
{ float2 uv_MainTex; };
fixed4 _Color;
sampler2D _MainTex;
sampler2D _BumpMap;
float _BumpScale;
sampler2D _SmoothnessMap;
float _Smoothness;
void surf (Input IN, inout SurfaceOutputStandard o)
{
fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
fixed3 normal = UnpackScaleNormal (tex2D(_BumpMap, IN.uv_MainTex), _BumpScale).xyz;
fixed smooth = tex2D (_SmoothnessMap, IN.uv_MainTex).a;
// Feed surface output
o.Albedo = c.rgb;
o.Normal = normal;
o.Metallic = 0.0;
o.Smoothness = lerp(smooth, 1.0, _Smoothness);
o.Alpha = c.a;
}
ENDCG
Cull Front
CGPROGRAM
#pragma surface surf Standard fullforwardshadows vertex:vert alphatest:_Cutoff
#pragma target 3.0
void vert (inout appdata_full v)
{
v.normal = -v.normal;
}
ENDCG
Cull Back
CGPROGRAM
#pragma surface surf Standard fullforwardshadows alphatest:_Cutoff
#pragma target 3.0
ENDCG
}
Fallback "Standard"
}
@JonRurka
Copy link

I can not tell you how much I would have appreciated a version with traditional frag/vert shaders. I HAVE to use them for what i'm doing (compute buffers don't work well with surf shaders), even if you cut out the lighting bit and just left the part that matters for double sided rendering.

@DoguD
Copy link

DoguD commented Feb 9, 2021

It works great! Thank you very much, do you have a donation link?

@foopis23
Copy link

foopis23 commented Apr 2, 2021

Using Unity 2020.2.6f1 and It seems to have lighting errors. Could just be something specific about my setup though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment