Skip to content

Instantly share code, notes, and snippets.

@cjacobwade
Created July 25, 2018 02:55
Show Gist options
  • Save cjacobwade/3964f2f67537f3a136c2f38e345b9089 to your computer and use it in GitHub Desktop.
Save cjacobwade/3964f2f67537f3a136c2f38e345b9089 to your computer and use it in GitHub Desktop.
Shader "Sausage/Paint/Surface"
{
Properties
{
_Color("Color", Color) = (1,1,1,1)
_MainTex("Albedo (RGB)", 2D) = "white" {}
_Glossiness("Smoothness", Range(0,1)) = 0.5
_Metallic("Metallic", Range(0,1)) = 0.0
_BumpScale("Scale", Float) = 1.0
_BumpMap("Normal (RGB)", 2D) = "bump" {}
_PaintBump("Paint Normal Scale", Float) = 0.1
_PaintMaskTex("Paint Map", 2D) = "black" {}
_PaintBumpTex("Paint Normal", 2D) = "black" {}
_Emission("Emission", Range(0, 1)) = 0.0
//_NScale("Noise Scale", Range(0.0285, 0.03)) = 0.08
[Header(Stencil)]
_Stencil("Stencil ID [0;255]", Float) = 0
[Enum(UnityEngine.Rendering.CompareFunction)] _StencilComp("Stencil Comparison", Int) = 8
[Enum(UnityEngine.Rendering.StencilOp)] _StencilOp("Stencil Operation", Int) = 2
}
SubShader
{
Tags{ "RenderType" = "Opaque" }
LOD 200
Stencil
{
Ref [_Stencil]
Comp [_StencilComp]
Pass [_StencilOp]
}
CGPROGRAM
#include "../MathUtils.cginc"
#include "PaintUtils.cginc"
#include "UnityCG.cginc"
#pragma surface surf Standard vertex:vert fullforwardshadows
#pragma glsl
#pragma target 3.0
struct Input
{
half2 uv_MainTex : TEXCOORD0;
half2 uv_PaintBumpTex : TEXCOORD1;
half2 lightmapUVs : TEXCOORD2;
half3 localPos;
};
sampler2D _MainTex;
sampler2D _BumpMap;
sampler2D _PaintMaskTex;
sampler2D _PaintBumpTex;
half _PaintBump;
half4 _Color;
sampler2D _OcclusionTex;
half _Glossiness;
half _Metallic;
half _BumpScale;
half _Occlusion;
half4 _ShadowColor;
half _Emission;
half4 _PaintColor1;
half4 _PaintColor2;
half4 _PaintColor3;
half4 _PaintColor4;
half4 getPaintColor(half4 paint, half saturatedNoise)
{
half4 paintColor = _PaintColor4;
half r = step(1, paint.r + saturatedNoise);
paintColor = lerp(paintColor, _PaintColor1, r);
half g = step(1, paint.g + saturatedNoise);
paintColor = lerp(paintColor, _PaintColor2, g);
half b = step(1, paint.b + saturatedNoise);
paintColor = lerp(paintColor, _PaintColor3, b);
return lerp(paintColor, _PaintColor4, r * g * b);
}
void vert(inout appdata_full v, out Input o)
{
UNITY_INITIALIZE_OUTPUT(Input, o);
o.lightmapUVs = v.texcoord1.xy;
o.localPos = v.vertex.xyz;
}
void surf(Input IN, inout SurfaceOutputStandard o)
{
half4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
half2 appliedUVs = IN.lightmapUVs.xy * unity_LightmapST.xy + unity_LightmapST.zw;
half4 paint = tex2D(_PaintMaskTex, appliedUVs);
half noiseAmount = noise(IN.localPos * 20);
half satNoise = noiseAmount * 0.5 + 0.5;
half alpha = paint.a + noiseAmount;
half4 paintColor = getPaintColor(paint, satNoise);
half3 detailNorm = UnpackScaleNormal(tex2D(_PaintBumpTex, IN.uv_PaintBumpTex), _PaintBump);
half3 norm = UnpackScaleNormal(tex2D(_BumpMap, IN.uv_MainTex), _BumpScale);
half isPaint = step(1, alpha);
o.Normal = lerp(norm, detailNorm, isPaint);
o.Albedo = lerp(c.rgb, paintColor.rgb, isPaint);
o.Metallic = lerp(_Metallic, 0.00, isPaint);
o.Smoothness = lerp(_Glossiness, 0.5, isPaint);
o.Alpha = lerp(c.a, 1.0, isPaint);
half brightness = (o.Albedo.r + o.Albedo.g + o.Albedo.b) / 3;
o.Emission = o.Albedo * (brightness * brightness) * _Emission;
//o.Albedo = noiseAmount;
//o.Emission = noiseAmount;
}
ENDCG
}
FallBack "Diffuse"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment