summaryrefslogtreecommitdiffstats
path: root/Shaders
diff options
context:
space:
mode:
authoryum <yum.food.vr@gmail.com>2023-01-27 15:28:37 -0800
committeryum <yum.food.vr@gmail.com>2023-01-27 15:28:37 -0800
commit30680445b1c59fe107a0be8295da2b8a38c0f6ea (patch)
tree0a02a6727fba30ba2a14dc32ee7fbb250ff5f1d0 /Shaders
parent40ffc0c8a1038490b330c1f1f4b04d6ff34342df (diff)
Enable texture-based PBR rendering of backplate
Users can now use PBR textures on their custom backplate! * Update TaSTT.fbx: UV map aspect ratio matches board
Diffstat (limited to 'Shaders')
-rw-r--r--Shaders/TaSTT_lighting_template.cginc119
-rw-r--r--Shaders/TaSTT_template.shader12
2 files changed, 110 insertions, 21 deletions
diff --git a/Shaders/TaSTT_lighting_template.cginc b/Shaders/TaSTT_lighting_template.cginc
index e1798f9..e3b6239 100644
--- a/Shaders/TaSTT_lighting_template.cginc
+++ b/Shaders/TaSTT_lighting_template.cginc
@@ -14,7 +14,7 @@ struct appdata
struct v2f
{
float4 position : SV_POSITION;
- float2 uv : TEXCOORD0;
+ float4 uv : TEXCOORD0;
float3 normal : TEXCOORD1;
float3 worldPos : TEXCOORD2;
@@ -23,8 +23,20 @@ struct v2f
#endif
};
-float Use_Custom_Background;
-sampler2D Custom_Background;
+float BG_Enable;
+sampler2D BG_BaseColor;
+sampler2D BG_NormalMap;
+sampler2D BG_Metallic;
+sampler2D BG_Smoothness;
+sampler2D BG_Emission_Mask;
+float BG_Smoothness_Invert;
+float BG_NormalStrength;
+float3 BG_Emission_Color;
+float4 BG_BaseColor_ST;
+float4 BG_NormalMap_ST;
+float4 BG_Metallic_ST;
+float4 BG_Smoothness_ST;
+float4 BG_Emission_Mask_ST;
float Enable_Dithering;
@@ -117,13 +129,14 @@ void getVertexLightColor(inout v2f i)
#endif
}
-v2f vert (appdata v)
+v2f vert(appdata v)
{
v2f o;
o.position = mul(UNITY_MATRIX_MVP, v.position);
o.worldPos = mul(unity_ObjectToWorld, v.position);
- o.uv = 1.0 - v.uv;
o.normal = UnityObjectToWorldNormal(v.normal);
+ o.uv.xy = TRANSFORM_TEX(v.uv, BG_BaseColor);
+ o.uv.zw = 1.0 - v.uv;
getVertexLightColor(o);
return o;
}
@@ -271,6 +284,8 @@ fixed sq_dist(fixed2 p0, fixed2 p1)
fixed4 effect_squares (v2f i)
{
+ float2 uv = i.uv.zw;
+ uv.y *= 2; // Text box has 2:1 aspect ratio
const fixed time = _Time.y;
#define PI 3.1415926535
@@ -283,7 +298,7 @@ fixed4 effect_squares (v2f i)
#define NSQ_Y 5.0
// Map uv from [0, 1] to [-.5, .5].
- fixed2 p = i.uv - 0.5;
+ fixed2 p = uv - 0.5;
p *= fixed2(NSQ_X, NSQ_Y);
p = mul(rot, p);
p -= 0.5;
@@ -367,7 +382,7 @@ UnityLight GetLight(v2f i)
return light;
}
-UnityIndirect GetIndirect(v2f i, float3 view_dir) {
+UnityIndirect GetIndirect(v2f i, float3 view_dir, float smoothness) {
UnityIndirect indirect;
indirect.diffuse = 0;
indirect.specular = 0;
@@ -380,7 +395,7 @@ UnityIndirect GetIndirect(v2f i, float3 view_dir) {
indirect.diffuse += max(0, ShadeSH9(float4(i.normal, 1)));
float3 reflect_dir = reflect(-view_dir, i.normal);
// There's a nonlinear relationship between mipmap level and roughness.
- float roughness = 1 - Smoothness;
+ float roughness = 1 - smoothness;
roughness *= 1.7 - .7 * roughness;
float3 env_sample = UNITY_SAMPLE_TEXCUBE_LOD(
unity_SpecCube0,
@@ -392,6 +407,68 @@ UnityIndirect GetIndirect(v2f i, float3 view_dir) {
return indirect;
}
+void initNormal(inout v2f i)
+{
+ if (BG_Enable) {
+ i.normal = UnpackScaleNormal(
+ tex2Dgrad(BG_NormalMap, i.uv.xy, ddx(i.uv.x), ddy(i.uv.y)),
+ BG_NormalStrength);
+ // Swap Y and Z
+ i.normal = i.normal.xzy;
+ }
+ i.normal = normalize(i.normal);
+}
+
+fixed4 light(v2f i,
+ sampler2D albedo_map,
+ sampler2D normal_map,
+ float normal_str,
+ sampler2D metallic_map,
+ sampler2D smoothness_map,
+ float invert_smoothness,
+ sampler2D emission_mask,
+ float3 emission_color)
+{
+ initNormal(i);
+
+ float2 iddx = ddx(i.uv.x);
+ float2 iddy = ddy(i.uv.y);
+ fixed4 albedo = tex2Dgrad(albedo_map, i.uv, iddx, iddy);
+
+ fixed3 normal = UnpackScaleNormal(
+ tex2Dgrad(normal_map, i.uv.xy, iddx, iddy),
+ normal_str);
+ // Swap Y and Z
+ normal = normal.xzy;
+
+ float3 view_dir = normalize(_WorldSpaceCameraPos - i.worldPos);
+
+ float metallic = tex2Dgrad(metallic_map, i.uv.xy, iddx, iddy);
+
+ float3 specular_tint;
+ float one_minus_reflectivity;
+ albedo.rgb = DiffuseAndSpecularFromMetallic(
+ albedo, metallic, specular_tint, one_minus_reflectivity);
+
+ UnityIndirect indirect_light;
+ indirect_light.diffuse = 0;
+ indirect_light.specular = 0;
+
+ float smoothness = tex2Dgrad(smoothness_map, i.uv.xy, iddx, iddy);
+ if (invert_smoothness) {
+ smoothness = 1 - smoothness;
+ }
+
+ fixed3 emission = tex2Dgrad(emission_mask, i.uv.xy, iddx, iddy) * emission_color;
+
+ fixed3 pbr = UNITY_BRDF_PBS(albedo, specular_tint,
+ one_minus_reflectivity, smoothness,
+ i.normal, view_dir, GetLight(i), GetIndirect(i, view_dir, smoothness)).rgb;
+ pbr.rgb += emission;
+
+ return fixed4(pbr, albedo.a);
+}
+
fixed4 light(v2f i, fixed4 unlit)
{
// Get color in spherical harmonics
@@ -410,7 +487,7 @@ fixed4 light(v2f i, fixed4 unlit)
fixed3 pbr = UNITY_BRDF_PBS(albedo, specular_tint,
one_minus_reflectivity, Smoothness,
- i.normal, view_dir, GetLight(i), GetIndirect(i, view_dir)).rgb;
+ i.normal, view_dir, GetLight(i), GetIndirect(i, view_dir, Smoothness)).rgb;
pbr = lerp(pbr.rgb, unlit.rgb, Emissive);
@@ -424,10 +501,10 @@ bool f3ltf3(fixed3 a, fixed3 b)
a[2] < b[2];
}
-fixed4 frag (v2f i) : SV_Target
+fixed4 frag(v2f i) : SV_Target
{
- float2 uv = i.uv;
- i.normal = normalize(i.normal);
+ float2 uv = i.uv.zw;
+ uv.y *= 2; // Text box has 2:1 aspect ratio
// Derived from github.com/pema99/shader-knowledge (MIT license).
if (unity_CameraProjection[2][0] != 0.0 ||
@@ -537,14 +614,18 @@ fixed4 frag (v2f i) : SV_Target
}
// The edges of each letter cell can be slightly grey due to mip maps.
// Detect this and shade it as the background.
- fixed3 grey = fixed3(.1,.1,.1);
+ fixed3 grey = fixed3(.3,.3,.3);
if (f3ltf3(text.rgb, grey) || discard_text) {
- if (Use_Custom_Background) {
- float2 iddx = ddx(uv.x);
- float2 iddy = ddy(uv.y);
- return light(
- i,
- tex2Dgrad(Custom_Background, uv, iddx, iddy));
+ if (BG_Enable) {
+ return light(i,
+ BG_BaseColor,
+ BG_NormalMap,
+ BG_NormalStrength,
+ BG_Metallic,
+ BG_Smoothness,
+ BG_Smoothness_Invert,
+ BG_Emission_Mask,
+ BG_Emission_Color);
} else {
return light(i, Background_Color);
}
diff --git a/Shaders/TaSTT_template.shader b/Shaders/TaSTT_template.shader
index bf9f53a..e0d436c 100644
--- a/Shaders/TaSTT_template.shader
+++ b/Shaders/TaSTT_template.shader
@@ -19,8 +19,15 @@
[MaterialToggle] Enable_Dithering("Enable font dithering", float) = 1
- [MaterialToggle] Use_Custom_Background("Enable custom background", float) = 0
- Custom_Background("Custom background", 2D) = "black" {}
+ [MaterialToggle] BG_Enable("Enable custom background", float) = 0
+ BG_BaseColor("Background base color", 2D) = "black" {}
+ [NoScaleOffset] BG_NormalMap ("Background normal map", 2D) = "bump" {}
+ BG_NormalStrength ("Background normal strength", Float) = 1
+ BG_Smoothness("Background smoothness", 2D) = "black" {}
+ [MaterialToggle]BG_Smoothness_Invert("Invert background smoothness", float) = 1
+ BG_Metallic("Background metallic", 2D) = "black" {}
+ BG_Emission_Mask("Background emission mask", 2D) = "black" {}
+ BG_Emission_Color("Background emission color", Color) = (0, 0, 0)
_Font_0x0000_0x1FFF ("_Font 0 (unicode 0x0000 - 0x1FFFF)", 2D) = "white" {}
_Font_0x2000_0x3FFF ("_Font 1 (unicode 0x2000 - 0x3FFFF)", 2D) = "white" {}
@@ -79,5 +86,6 @@
ENDCG
}
}
+ //CustomEditor "TaSTTShaderGUI"
}