Skip to content

Instantly share code, notes, and snippets.

@bgolus
Last active October 20, 2023 12:29
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save bgolus/112059bbb6a43591600cc05a4081eb10 to your computer and use it in GitHub Desktop.
Save bgolus/112059bbb6a43591600cc05a4081eb10 to your computer and use it in GitHub Desktop.
Shader "Custom/ACNH_SewingCloth"
{
Properties
{
[NoScaleOffset] _MainTex ("Albedo (RGB)", 2D) = "white" {}
[NoScaleOffset] _BumpMap ("Normal Map", 2D) = "bump" {}
[NoScaleOffset] _EdgeAlpha ("Alpha", 2D) = "white" {}
_Cutoff ("Alpha Test Cutoff", Range(0,1)) = 0.5
_Offset ("Offset (XY)", Vector) = (0,0,0,0)
_Rotation ("Rotation", Range(-360,360)) = 0.0
}
SubShader
{
Tags { "Queue"="AlphaTest" "IgnoreProjector"="True" "RenderType"="TransparentCutout" }
LOD 200
CGPROGRAM
#pragma surface surf Standard fullforwardshadows alphatest:_Cutoff
#pragma target 3.0
sampler2D _MainTex;
sampler2D _BumpMap;
sampler2D _EdgeAlpha;
struct Input
{
// using [NoScaleOffset] above so this is the unmodified mesh UVs
float2 uv_MainTex;
};
// using a custof offset instead of the built in scale offset
// so I can apply it after the rotation instead of before
float2 _Offset;
// using a rotation in degrees for ease of animation.
// would be cheaper to calculate the sine & cosine on CPU but
// I'm being lazy and didn't want to write an extra script.
float _Rotation;
void surf (Input IN, inout SurfaceOutputStandard o)
{
// generate rotation matrix
float s, c;
sincos(_Rotation * (UNITY_PI/180.0), s, c);
float2x2 rotMatrix = float2x2(c, s, -s, c);
// rotate and offset uv
float2 uv = mul(rotMatrix, IN.uv_MainTex) + _Offset;
// sample textures with modified uv
o.Albedo = tex2D(_MainTex, uv).rgb;
o.Alpha = tex2D(_EdgeAlpha, uv).a;
o.Normal = UnpackNormal(tex2D(_BumpMap, uv));
// counter-rotate tangent space normal
o.Normal.xy = mul(o.Normal.xy, rotMatrix);
}
ENDCG
}
FallBack "Diffuse"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment