Skip to content

Commit

Permalink
Added lite varient of shader
Browse files Browse the repository at this point in the history
  • Loading branch information
cubedparadox committed Apr 18, 2018
1 parent 40aed6e commit d427403
Show file tree
Hide file tree
Showing 12 changed files with 633 additions and 3 deletions.
Binary file modified Assets/Cubed's Unity Shaders/Demo Scene.unity
Binary file not shown.
166 changes: 166 additions & 0 deletions Assets/Cubed's Unity Shaders/Editor/FlatLitToonLiteInspector.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
using UnityEditor;
using UnityEngine;
using System.Collections.Generic;
using System.Linq;
using System;

public class FlatLitToonLiteInspector : ShaderGUI
{

public enum OutlineMode
{
None,
Tinted,
Colored
}

public enum BlendMode
{
Opaque,
Cutout,
Fade, // Old school alpha-blending mode, fresnel does not affect amount of transparency
Transparent // Physically plausible transparency mode, implemented as alpha pre-multiply
}

MaterialProperty blendMode;
MaterialProperty mainTexture;
MaterialProperty color;
MaterialProperty colorMask;
MaterialProperty shadow;
MaterialProperty emissionMap;
MaterialProperty emissionColor;
MaterialProperty normalMap;
MaterialProperty alphaCutoff;

public override void OnGUI(MaterialEditor materialEditor, MaterialProperty[] props)
{
{ //Find Properties
blendMode = FindProperty("_Mode", props);
mainTexture = FindProperty("_MainTex", props);
color = FindProperty("_Color", props);
colorMask = FindProperty("_ColorMask", props);
shadow = FindProperty("_Shadow", props);
emissionMap = FindProperty("_EmissionMap", props);
emissionColor = FindProperty("_EmissionColor", props);
normalMap = FindProperty("_BumpMap", props);
alphaCutoff = FindProperty("_Cutoff", props);
}

Material material = materialEditor.target as Material;

{ //Shader Properties GUI
EditorGUIUtility.labelWidth = 0f;

EditorGUI.BeginChangeCheck();
{
EditorGUI.showMixedValue = blendMode.hasMixedValue;
var bMode = (BlendMode)blendMode.floatValue;

EditorGUI.BeginChangeCheck();
bMode = (BlendMode)EditorGUILayout.Popup("Rendering Mode", (int)bMode, Enum.GetNames(typeof(BlendMode)));
if (EditorGUI.EndChangeCheck())
{
materialEditor.RegisterPropertyChangeUndo("Rendering Mode");
blendMode.floatValue = (float)bMode;

foreach (var obj in blendMode.targets)
{
SetupMaterialWithBlendMode((Material)obj, (BlendMode)material.GetFloat("_Mode"));
}
}

EditorGUI.showMixedValue = false;


materialEditor.TexturePropertySingleLine(new GUIContent("Main Texture", "Main Color Texture (RGB)"), mainTexture, color);
EditorGUI.indentLevel += 2;
if((BlendMode)material.GetFloat("_Mode") == BlendMode.Cutout)
materialEditor.ShaderProperty(alphaCutoff, "Alpha Cutoff", 2);
materialEditor.TexturePropertySingleLine(new GUIContent("Color Mask", "Masks Color Tinting (G)"), colorMask);
EditorGUI.indentLevel -= 2;
materialEditor.TexturePropertySingleLine(new GUIContent("Normal Map", "Normal Map (RGB)"), normalMap);
materialEditor.TexturePropertySingleLine(new GUIContent("Emission", "Emission (RGB)"), emissionMap, emissionColor);
EditorGUI.BeginChangeCheck();
materialEditor.TextureScaleOffsetProperty(mainTexture);
if (EditorGUI.EndChangeCheck())
emissionMap.textureScaleAndOffset = mainTexture.textureScaleAndOffset;

EditorGUILayout.Space();
materialEditor.ShaderProperty(shadow, "Shadow");
}
EditorGUI.EndChangeCheck();
}

}

public static void SetupMaterialWithBlendMode(Material material, BlendMode blendMode)
{
switch ((BlendMode)material.GetFloat("_Mode"))
{
case BlendMode.Opaque:
material.SetOverrideTag("RenderType", "");
material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero);
material.SetInt("_ZWrite", 1);
material.DisableKeyword("_ALPHATEST_ON");
material.DisableKeyword("_ALPHABLEND_ON");
material.DisableKeyword("_ALPHAPREMULTIPLY_ON");
material.renderQueue = -1;
break;
case BlendMode.Cutout:
material.SetOverrideTag("RenderType", "TransparentCutout");
material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero);
material.SetInt("_ZWrite", 1);
material.EnableKeyword("_ALPHATEST_ON");
material.DisableKeyword("_ALPHABLEND_ON");
material.DisableKeyword("_ALPHAPREMULTIPLY_ON");
material.renderQueue = (int)UnityEngine.Rendering.RenderQueue.AlphaTest;
break;
case BlendMode.Fade:
material.SetOverrideTag("RenderType", "Transparent");
material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);
material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
material.SetInt("_ZWrite", 0);
material.DisableKeyword("_ALPHATEST_ON");
material.EnableKeyword("_ALPHABLEND_ON");
material.DisableKeyword("_ALPHAPREMULTIPLY_ON");
material.renderQueue = (int)UnityEngine.Rendering.RenderQueue.Transparent;
break;
case BlendMode.Transparent:
material.SetOverrideTag("RenderType", "Transparent");
material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
material.SetInt("_ZWrite", 0);
material.DisableKeyword("_ALPHATEST_ON");
material.DisableKeyword("_ALPHABLEND_ON");
material.EnableKeyword("_ALPHAPREMULTIPLY_ON");
material.renderQueue = (int)UnityEngine.Rendering.RenderQueue.Transparent;
break;
}
}

public static void SetupMaterialWithOutlineMode(Material material, OutlineMode outlineMode)
{
switch ((OutlineMode)material.GetFloat("_OutlineMode"))
{
case OutlineMode.None:
material.EnableKeyword("NO_OUTLINE");
material.DisableKeyword("TINTED_OUTLINE");
material.DisableKeyword("COLORED_OUTLINE");
break;
case OutlineMode.Tinted:
material.DisableKeyword("NO_OUTLINE");
material.EnableKeyword("TINTED_OUTLINE");
material.DisableKeyword("COLORED_OUTLINE");
break;
case OutlineMode.Colored:
material.DisableKeyword("NO_OUTLINE");
material.DisableKeyword("TINTED_OUTLINE");
material.EnableKeyword("COLORED_OUTLINE");
break;
default:
break;
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file not shown.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
Shader "CubedParadox/Flat Lit Toon Lite Double Sided"
{
Properties
{
_MainTex("MainTex", 2D) = "white" {}
_Color("Color", Color) = (1,1,1,1)
_ColorMask("ColorMask", 2D) = "black" {}
_Shadow("Shadow", Range(0, 1)) = 0.4
_EmissionMap("Emission Map", 2D) = "white" {}
[HDR]_EmissionColor("Emission Color", Color) = (0,0,0,1)
_BumpMap("BumpMap", 2D) = "bump" {}
_Cutoff("Alpha cutoff", Range(0,1)) = 0.5

// Blending state
[HideInInspector] _Mode ("__mode", Float) = 0.0
[HideInInspector] _SrcBlend ("__src", Float) = 1.0
[HideInInspector] _DstBlend ("__dst", Float) = 0.0
[HideInInspector] _ZWrite ("__zw", Float) = 1.0
}

SubShader
{
Tags
{
"RenderType" = "Opaque"
}
Cull Off
Pass
{

Name "FORWARD"
Tags { "LightMode" = "ForwardBase" }

Blend [_SrcBlend] [_DstBlend]
ZWrite [_ZWrite]

CGPROGRAM
#include "FlatLitToonCoreLite.cginc"
#pragma shader_feature _ _ALPHATEST_ON _ALPHABLEND_ON _ALPHAPREMULTIPLY_ON
#pragma vertex vert
#pragma fragment frag

#pragma only_renderers d3d11 glcore gles
#pragma target 4.0

#pragma multi_compile_fwdbase
#pragma multi_compile_fog

float4 frag(VertexOutput i) : COLOR
{
float4 objPos = mul(unity_ObjectToWorld, float4(0,0,0,1));
i.normalDir = normalize(i.normalDir);
float3x3 tangentTransform = float3x3(i.tangentDir, i.bitangentDir, i.normalDir);
float3 _BumpMap_var = UnpackNormal(tex2D(_BumpMap,TRANSFORM_TEX(i.uv0, _BumpMap)));
float3 normalDirection = normalize(mul(_BumpMap_var.rgb, tangentTransform)); // Perturbed normals
float4 _MainTex_var = tex2D(_MainTex,TRANSFORM_TEX(i.uv0, _MainTex));

float3 lightDirection = normalize(_WorldSpaceLightPos0.xyz);
float3 lightColor = _LightColor0.rgb;
UNITY_LIGHT_ATTENUATION(attenuation, i, i.posWorld.xyz);

float4 _EmissionMap_var = tex2D(_EmissionMap,TRANSFORM_TEX(i.uv0, _EmissionMap));
float3 emissive = (_EmissionMap_var.rgb*_EmissionColor.rgb);
float4 _ColorMask_var = tex2D(_ColorMask,TRANSFORM_TEX(i.uv0, _ColorMask));
float4 baseColor = lerp((_MainTex_var.rgba*_Color.rgba),_MainTex_var.rgba,_ColorMask_var.r);
baseColor *= float4(i.col.rgb, 1);

#if defined(_ALPHATEST_ON)
clip (baseColor.a - _Cutoff);
#endif

float3 lightmap = float4(1.0,1.0,1.0,1.0);
#ifdef LIGHTMAP_ON
lightmap = DecodeLightmap(UNITY_SAMPLE_TEX2D(unity_Lightmap, i.uv1 * unity_LightmapST.xy + unity_LightmapST.zw));
#endif

float3 reflectionMap = DecodeHDR(UNITY_SAMPLE_TEXCUBE_LOD(unity_SpecCube0, normalize((_WorldSpaceCameraPos - objPos.rgb)), 7), unity_SpecCube0_HDR)* 0.02;

float grayscalelightcolor = dot(_LightColor0.rgb, grayscale_vector);
float bottomIndirectLighting = grayscaleSH9(float3(0.0, -1.0, 0.0));
float topIndirectLighting = grayscaleSH9(float3(0.0, 1.0, 0.0));
float grayscaleDirectLighting = dot(lightDirection, normalDirection)*grayscalelightcolor*attenuation + grayscaleSH9(normalDirection);

float lightDifference = topIndirectLighting + grayscalelightcolor - bottomIndirectLighting;
float remappedLight = (grayscaleDirectLighting - bottomIndirectLighting) / lightDifference;

float3 indirectLighting = saturate((ShadeSH9(half4(0.0, -1.0, 0.0, 1.0)) + reflectionMap));
float3 directLighting = saturate((ShadeSH9(half4(0.0, 1.0, 0.0, 1.0)) + reflectionMap + _LightColor0.rgb));
float3 directContribution = saturate((1.0 - _Shadow) + floor(saturate(remappedLight) * 2.0));
float3 finalColor = emissive + (baseColor * lerp(indirectLighting, directLighting, directContribution));
fixed4 finalRGBA = fixed4(finalColor * lightmap, baseColor.a);
UNITY_APPLY_FOG(i.fogCoord, finalRGBA);
return finalRGBA;
}
ENDCG
}

Pass
{
Name "FORWARD_DELTA"
Tags { "LightMode" = "ForwardAdd" }
Blend [_SrcBlend] One

CGPROGRAM
#pragma shader_feature NO_OUTLINE TINTED_OUTLINE COLORED_OUTLINE
#pragma shader_feature _ _ALPHATEST_ON _ALPHABLEND_ON _ALPHAPREMULTIPLY_ON
#include "FlatLitToonCore.cginc"
#pragma vertex vert
#pragma fragment frag

#pragma only_renderers d3d11 glcore gles
#pragma target 4.0

#pragma multi_compile_fwdadd_fullshadows
#pragma multi_compile_fog

float4 frag(VertexOutput i) : COLOR
{
float4 objPos = mul(unity_ObjectToWorld, float4(0,0,0,1));
i.normalDir = normalize(i.normalDir);
float3x3 tangentTransform = float3x3(i.tangentDir, i.bitangentDir, i.normalDir);
float3 _BumpMap_var = UnpackNormal(tex2D(_BumpMap,TRANSFORM_TEX(i.uv0, _BumpMap)));
float3 normalDirection = normalize(mul(_BumpMap_var.rgb, tangentTransform)); // Perturbed normals
float4 _MainTex_var = tex2D(_MainTex,TRANSFORM_TEX(i.uv0, _MainTex));

float3 lightDirection = normalize(_WorldSpaceLightPos0.xyz);
float3 lightColor = _LightColor0.rgb;
UNITY_LIGHT_ATTENUATION(attenuation, i, i.posWorld.xyz);

float4 _ColorMask_var = tex2D(_ColorMask,TRANSFORM_TEX(i.uv0, _ColorMask));
float4 baseColor = lerp((_MainTex_var.rgba*_Color.rgba),_MainTex_var.rgba,_ColorMask_var.r);
baseColor *= float4(i.col.rgb, 1);

#if defined(_ALPHATEST_ON)
clip (baseColor.a - _Cutoff);
#endif

float lightContribution = dot(normalize(_WorldSpaceLightPos0.xyz - i.posWorld.xyz),normalDirection)*attenuation;
float3 directContribution = floor(saturate(lightContribution) * 2.0);
float3 finalColor = baseColor * lerp(0, _LightColor0.rgb, saturate(directContribution + ((1 - _Shadow) * attenuation)));
fixed4 finalRGBA = fixed4(finalColor,1) * i.col;
UNITY_APPLY_FOG(i.fogCoord, finalRGBA);
return finalRGBA;
}
ENDCG
}

Pass
{
Name "SHADOW_CASTER"
Tags{ "LightMode" = "ShadowCaster" }

ZWrite On ZTest LEqual

CGPROGRAM
#pragma shader_feature _ _ALPHATEST_ON _ALPHABLEND_ON _ALPHAPREMULTIPLY_ON
#include "FlatLitToonShadows.cginc"

#pragma multi_compile_shadowcaster
#pragma fragmentoption ARB_precision_hint_fastest

#pragma only_renderers d3d11 glcore gles
#pragma target 4.0

#pragma vertex vertShadowCaster
#pragma fragment fragShadowCaster
ENDCG
}
}
FallBack "Diffuse"
CustomEditor "FlatLitToonLiteInspector"
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit d427403

Please sign in to comment.