reality
reality
Fading strokes in Unity
@andybak had an awesome graphics guru help out with the shaders! Got a large portion of them to work as intended for the experience we were designing. Thank you so much for your help on this! I've made all the modified shader files we used in the experience available on GitHub, crediting you & the Open Brush Team, of course. https://github.com/CultureVerse/open-brush-stroke-shaders While this does work for the experience we were crafting, these scripts were most definitely thrown together in the shortest amount of time possible. So there is probably lots of cleanup & potential issues but it's a fantastic foundation. These shaders work as intended using Unity 2021.3.4f1 with the built-in render pipeline.
30 replies
Fading strokes in Unity
Going to keep at it and see if I can get it to look cleaner but honestly already really happy with this. I'm hoping this kind of combination isn't needed for all 45 brush types
30 replies
Fading strokes in Unity
This is perfect with the models I tested! I'm still extremely new to shaders but it does end up working with the StandardDoubleSided shader.
Shader "Brush/StandardDoubleSided_Stroke_Modified" {
Properties {
_Color ("Main Color", Color) = (1,1,1,1)
_SpecColor ("Specular Color", Color) = (0.5, 0.5, 0.5, 0)
_Shininess ("Shininess", Range (0.01, 1)) = 0.078125
_MainTex ("Base (RGB) TransGloss (A)", 2D) = "white" {}
_BumpMap ("Normalmap", 2D) = "bump" {}
_Cutoff ("Alpha cutoff", Range(0,1)) = 0.5
_ClipStart ("Clip Start", Float) = 0
_ClipEnd ("Clip End", Float) = 1
}

SubShader {
Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"}
LOD 400
Cull Off
Blend SrcAlpha OneMinusSrcAlpha
ColorMask RGB

Pass {
CGPROGRAM
#pragma vertex vert
#pragma fragment frag

#pragma multi_compile __ AUDIO_REACTIVE
#pragma multi_compile __ TBT_LINEAR_TARGETs

#include "../../../Shaders/Include/Brush.cginc"
#include "UnityCG.cginc"

struct appdata {
float4 vertex : POSITION;
float2 uv_MainTex : TEXCOORD0;
float2 uv_BumpMap : TEXCOORD1;
fixed4 color : COLOR;
float3 uv2 : TEXCOORD2;
float completion : TEXCOORD3;
};

struct v2f {
float2 uv_MainTex : TEXCOORD0;
float2 uv_BumpMap : TEXCOORD1;
fixed4 color : COLOR;
float3 uv2 : TEXCOORD2;
float completion : TEXCOORD3;
float4 vertex : SV_POSITION;
};

sampler2D _MainTex;
sampler2D _BumpMap;
fixed4 _Color;
half _Shininess;
float _Cutoff;
float _ClipStart;
float _ClipEnd;
uniform float4 _LightColor0;

float invLerp(float from, float to, float value) {
return (value - from) / (to - from);
}

v2f vert (appdata v) {
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv_MainTex = v.uv_MainTex;
o.uv_BumpMap = v.uv_BumpMap;
o.color = v.color;
o.uv2 = v.uv2;
o.completion = invLerp(v.uv2.x, v.uv2.y, v.uv2.z);
return o;
}

fixed4 frag (v2f i) : SV_Target {
// Normals & lighting
fixed3 tangentNormal = UnpackNormal(tex2D(_BumpMap, i.uv_BumpMap)) * 2 - 1;

fixed3 lightDirection = normalize(_WorldSpaceLightPos0.xyz - i.vertex.xyz);
fixed3 diffuseLight = max(0, dot(tangentNormal, lightDirection)) * _LightColor0.rgb;

// Alpha test
fixed4 mainTex = tex2D(_MainTex, i.uv_MainTex);
clip(mainTex.a - _Cutoff);

// Stroke completiom
float completion = (i.completion > _ClipStart && i.completion < _ClipEnd) ? 1 : -1;
clip(completion);

fixed4 tex = tex2D(_MainTex, i.uv_MainTex);
fixed4 color = tex * _Color * i.color;
color.a *= step(_ClipStart, i.completion) * (1-step(_ClipEnd, i.completion)); // Modify alpha channel based on completion
return color;
}
ENDCG
}
}
FallBack "Transparent/Cutout/VertexLit"
}
Shader "Brush/StandardDoubleSided_Stroke_Modified" {
Properties {
_Color ("Main Color", Color) = (1,1,1,1)
_SpecColor ("Specular Color", Color) = (0.5, 0.5, 0.5, 0)
_Shininess ("Shininess", Range (0.01, 1)) = 0.078125
_MainTex ("Base (RGB) TransGloss (A)", 2D) = "white" {}
_BumpMap ("Normalmap", 2D) = "bump" {}
_Cutoff ("Alpha cutoff", Range(0,1)) = 0.5
_ClipStart ("Clip Start", Float) = 0
_ClipEnd ("Clip End", Float) = 1
}

SubShader {
Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"}
LOD 400
Cull Off
Blend SrcAlpha OneMinusSrcAlpha
ColorMask RGB

Pass {
CGPROGRAM
#pragma vertex vert
#pragma fragment frag

#pragma multi_compile __ AUDIO_REACTIVE
#pragma multi_compile __ TBT_LINEAR_TARGETs

#include "../../../Shaders/Include/Brush.cginc"
#include "UnityCG.cginc"

struct appdata {
float4 vertex : POSITION;
float2 uv_MainTex : TEXCOORD0;
float2 uv_BumpMap : TEXCOORD1;
fixed4 color : COLOR;
float3 uv2 : TEXCOORD2;
float completion : TEXCOORD3;
};

struct v2f {
float2 uv_MainTex : TEXCOORD0;
float2 uv_BumpMap : TEXCOORD1;
fixed4 color : COLOR;
float3 uv2 : TEXCOORD2;
float completion : TEXCOORD3;
float4 vertex : SV_POSITION;
};

sampler2D _MainTex;
sampler2D _BumpMap;
fixed4 _Color;
half _Shininess;
float _Cutoff;
float _ClipStart;
float _ClipEnd;
uniform float4 _LightColor0;

float invLerp(float from, float to, float value) {
return (value - from) / (to - from);
}

v2f vert (appdata v) {
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv_MainTex = v.uv_MainTex;
o.uv_BumpMap = v.uv_BumpMap;
o.color = v.color;
o.uv2 = v.uv2;
o.completion = invLerp(v.uv2.x, v.uv2.y, v.uv2.z);
return o;
}

fixed4 frag (v2f i) : SV_Target {
// Normals & lighting
fixed3 tangentNormal = UnpackNormal(tex2D(_BumpMap, i.uv_BumpMap)) * 2 - 1;

fixed3 lightDirection = normalize(_WorldSpaceLightPos0.xyz - i.vertex.xyz);
fixed3 diffuseLight = max(0, dot(tangentNormal, lightDirection)) * _LightColor0.rgb;

// Alpha test
fixed4 mainTex = tex2D(_MainTex, i.uv_MainTex);
clip(mainTex.a - _Cutoff);

// Stroke completiom
float completion = (i.completion > _ClipStart && i.completion < _ClipEnd) ? 1 : -1;
clip(completion);

fixed4 tex = tex2D(_MainTex, i.uv_MainTex);
fixed4 color = tex * _Color * i.color;
color.a *= step(_ClipStart, i.completion) * (1-step(_ClipEnd, i.completion)); // Modify alpha channel based on completion
return color;
}
ENDCG
}
}
FallBack "Transparent/Cutout/VertexLit"
}
30 replies
Fading strokes in Unity
That sounds perfect! Curious what the gotchas are? I wasn’t able to dig into the UV channels to see the timestamps though my shader knowledge isn’t as extensive as I’d like it to be.
30 replies