Last active
January 28, 2023 23:50
-
-
Save unitycoder/e7e455f5d3127f4dfe838afff9713db3 to your computer and use it in GitHub Desktop.
Keijiro Standard Triplanar Shader with Added Glossiness and Metallic Texture Slots (and updated material inspector)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Standard shader with triplanar mapping | |
// https://github.com/keijiro/StandardTriplanar | |
Shader "Standard Triplanar Slots" | |
{ | |
Properties | |
{ | |
_Color("", Color) = (1, 1, 1, 1) | |
_MainTex("", 2D) = "white" {} | |
_GlossinessTex("Glossiness (R)", 2D) = "white" {} | |
_Glossiness("Glossiness Multiplier", Range(0, 1)) = 0.5 | |
_MetallicTex("Metallic (R)", 2D) = "white" {} | |
[Gamma] _Metallic("Metallic Multiplier", Range(0, 1)) = 0 | |
_BumpScale("", Float) = 1 | |
_BumpMap("", 2D) = "bump" {} | |
_OcclusionStrength("", Range(0, 1)) = 1 | |
_OcclusionMap("", 2D) = "white" {} | |
_MapScale("", Float) = 1 | |
} | |
SubShader | |
{ | |
Tags { "RenderType"="Opaque" } | |
CGPROGRAM | |
#pragma surface surf Standard vertex:vert fullforwardshadows addshadow | |
#pragma shader_feature _NORMALMAP | |
#pragma shader_feature _OCCLUSIONMAP | |
#pragma shader_feature _GLOSSINESSMAP | |
#pragma shader_feature _METALLICMAP | |
#pragma target 3.0 | |
half4 _Color; | |
sampler2D _MainTex; | |
sampler2D _GlossinessTex; | |
sampler2D _MetallicTex; | |
half _Glossiness; | |
half _Metallic; | |
half _BumpScale; | |
sampler2D _BumpMap; | |
half _OcclusionStrength; | |
sampler2D _OcclusionMap; | |
half _MapScale; | |
struct Input | |
{ | |
float3 localCoord; | |
float3 localNormal; | |
}; | |
void vert(inout appdata_full v, out Input data) | |
{ | |
UNITY_INITIALIZE_OUTPUT(Input, data); | |
data.localCoord = v.vertex.xyz; | |
data.localNormal = v.normal.xyz; | |
} | |
void surf(Input IN, inout SurfaceOutputStandard o) | |
{ | |
// Blending factor of triplanar mapping | |
float3 bf = normalize(abs(IN.localNormal)); | |
bf /= dot(bf, (float3)1); | |
// Triplanar mapping | |
float2 tx = IN.localCoord.yz * _MapScale; | |
float2 ty = IN.localCoord.zx * _MapScale; | |
float2 tz = IN.localCoord.xy * _MapScale; | |
// Base color | |
half4 cx = tex2D(_MainTex, tx) * bf.x; | |
half4 cy = tex2D(_MainTex, ty) * bf.y; | |
half4 cz = tex2D(_MainTex, tz) * bf.z; | |
half4 color = (cx + cy + cz) * _Color; | |
o.Albedo = color.rgb; | |
o.Alpha = color.a; | |
#ifdef _NORMALMAP | |
// Normal map | |
half4 nx = tex2D(_BumpMap, tx) * bf.x; | |
half4 ny = tex2D(_BumpMap, ty) * bf.y; | |
half4 nz = tex2D(_BumpMap, tz) * bf.z; | |
o.Normal = UnpackScaleNormal(nx + ny + nz, _BumpScale); | |
#endif | |
#ifdef _OCCLUSIONMAP | |
// Occlusion map | |
half ox = tex2D(_OcclusionMap, tx).g * bf.x; | |
half oy = tex2D(_OcclusionMap, ty).g * bf.y; | |
half oz = tex2D(_OcclusionMap, tz).g * bf.z; | |
o.Occlusion = lerp((half4)1, ox + oy + oz, _OcclusionStrength); | |
#endif | |
#ifdef _OCCLUSIONMAP | |
// glossines | |
half gx = tex2D(_GlossinessTex, tx).r * bf.x; | |
half gy = tex2D(_GlossinessTex, ty).r * bf.y; | |
half gz = tex2D(_GlossinessTex, tz).r * bf.z; | |
half gloss = (gx + gy + gz); | |
o.Smoothness = gloss* _Glossiness; | |
#endif | |
#ifdef _METALLICMAP | |
// metallic | |
half mx = tex2D(_MetallicTex, tx).r * bf.x; | |
half my = tex2D(_MetallicTex, ty).r * bf.y; | |
half mz = tex2D(_MetallicTex, tz).r * bf.z; | |
half metal = (mx + my + mz); | |
o.Metallic = metal* _Metallic; | |
#endif | |
} | |
ENDCG | |
} | |
FallBack "Diffuse" | |
CustomEditor "StandardTriplanarInspector" | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Standard shader with triplanar mapping | |
// https://github.com/keijiro/StandardTriplanar | |
using UnityEngine; | |
using UnityEditor; | |
public class StandardTriplanarInspector : ShaderGUI | |
{ | |
static class Styles | |
{ | |
static public readonly GUIContent albedo = new GUIContent("Albedo", "Albedo (RGB)"); | |
static public readonly GUIContent glossiness = new GUIContent("Glossiness", "Glossiness (R)"); | |
static public readonly GUIContent metallic = new GUIContent("Metallic", "Metallic (R)"); | |
static public readonly GUIContent normalMap = new GUIContent("Normal Map", "Normal Map"); | |
static public readonly GUIContent occlusion = new GUIContent("Occlusion", "Occlusion (G)"); | |
} | |
bool _initialized; | |
public override void OnGUI(MaterialEditor editor, MaterialProperty[] props) | |
{ | |
EditorGUI.BeginChangeCheck(); | |
editor.TexturePropertySingleLine( | |
Styles.albedo, FindProperty("_MainTex", props), FindProperty("_Color", props) | |
); | |
editor.TexturePropertySingleLine( | |
Styles.glossiness, FindProperty("_GlossinessTex", props), FindProperty("_Color", props) | |
); | |
editor.TexturePropertySingleLine( | |
Styles.metallic, FindProperty("_MetallicTex", props), FindProperty("_Color", props) | |
); | |
editor.ShaderProperty(FindProperty("_Metallic", props), "Metallic"); | |
editor.ShaderProperty(FindProperty("_Glossiness", props), "Glossiness"); | |
var normal = FindProperty("_BumpMap", props); | |
editor.TexturePropertySingleLine( | |
Styles.normalMap, normal, | |
normal.textureValue ? FindProperty("_BumpScale", props) : null | |
); | |
var occ = FindProperty("_OcclusionMap", props); | |
editor.TexturePropertySingleLine( | |
Styles.occlusion, occ, | |
occ.textureValue ? FindProperty("_OcclusionStrength", props) : null | |
); | |
editor.ShaderProperty(FindProperty("_MapScale", props), "Texture Scale"); | |
if (EditorGUI.EndChangeCheck() || !_initialized) | |
foreach (Material m in editor.targets) | |
SetMaterialKeywords(m); | |
_initialized = true; | |
} | |
static void SetMaterialKeywords(Material material) | |
{ | |
SetKeyword(material, "_NORMALMAP", material.GetTexture("_BumpMap")); | |
SetKeyword(material, "_OCCLUSIONMAP", material.GetTexture("_OcclusionMap")); | |
if (material.HasProperty("_GlossinessMap")) | |
{ | |
SetKeyword(material, "_GLOSSINESSMAP", material.GetTexture("_GlossinessMap")); | |
} | |
if (material.HasProperty("_MetallicMap")) | |
{ | |
SetKeyword(material, "_METALLICMAP", material.GetTexture("_MetallicMap")); | |
} | |
} | |
static void SetKeyword(Material m, string keyword, bool state) | |
{ | |
if (state) | |
m.EnableKeyword(keyword); | |
else | |
m.DisableKeyword(keyword); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment