Skip to content

Instantly share code, notes, and snippets.

@arif-pandu
Created October 29, 2024 16:20
Show Gist options
  • Save arif-pandu/2262784a60eba215b82a771b01e12c27 to your computer and use it in GitHub Desktop.
Save arif-pandu/2262784a60eba215b82a771b01e12c27 to your computer and use it in GitHub Desktop.
Unity shader for manipulating scale of a static object. Instead of controlling the transform directly, here i use shader to control the vertex
Shader "Custom/AnimatedUVScale"
{
Properties
{
_MainTex("Texture", 2D) = "white" {}
_Scale("Scale", Vector) = (1, 1, 1, 0)
}
SubShader
{
Tags { "RenderType"="Opaque" }
LOD 200
CGPROGRAM
#pragma surface surf Lambert vertex:vert
sampler2D _MainTex;
float4 _Scale;
struct Input
{
float2 uv_MainTex;
};
void vert(inout appdata_full v)
{
// Scale the vertex position by _Scale
v.vertex.x *= _Scale.x;
v.vertex.y *= _Scale.y;
v.vertex.z *= _Scale.z;
}
void surf(Input IN, inout SurfaceOutput o)
{
o.Albedo = tex2D(_MainTex, IN.uv_MainTex).rgb;
}
ENDCG
}
FallBack "Diffuse"
}
Shader "Custom/AnimatedUVScale"
{
Properties
{
_MainTex("Texture", 2D) = "white" {}
_Scale("Scale", Vector) = (1, 1, 1, 0)
}
SubShader
{
Tags { "RenderType"="Opaque" }
LOD 100
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
sampler2D _MainTex;
float4 _Scale;
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
float2 uv : TEXCOORD0;
float4 pos : SV_POSITION;
};
v2f vert(appdata v)
{
v2f o;
// Scale the vertex position by _Scale
v.vertex.x *= _Scale.x;
v.vertex.y *= _Scale.y;
v.vertex.z *= _Scale.z;
o.pos = UnityObjectToClipPos(v.vertex);
o.uv = v.uv;
return o;
}
fixed4 frag(v2f i) : SV_Target
{
// Sample the texture using the scaled UV coordinates
return tex2D(_MainTex, i.uv);
}
ENDCG
}
}
FallBack "Unlit/Texture"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment