Skip to content

Instantly share code, notes, and snippets.

@partybusiness
Last active April 7, 2020 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save partybusiness/fe3872c022cd0c3375699d9bbdc144be to your computer and use it in GitHub Desktop.
Save partybusiness/fe3872c022cd0c3375699d9bbdc144be to your computer and use it in GitHub Desktop.
Shader that uses world-space coordinates to sample textures
Shader "Unlit/WorldSpaceTextures"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
}
SubShader
{
Tags { "RenderType"="Opaque" }
LOD 100
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float3 normal : NORMAL;
float3 tangent : TANGENT;
};
struct v2f
{
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
};
sampler2D _MainTex;
float4 _MainTex_ST;
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
float3 worldSpacePosition = mul(unity_ObjectToWorld, v.vertex);
float3 worldNormal = normalize(mul(unity_ObjectToWorld, float4(v.normal, 0.0)).xyz);
float3 worldTangent = normalize(mul(unity_ObjectToWorld, float4(v.tangent, 0.0)).xyz);
float3 worldBinormal = cross(worldNormal, worldTangent);
float2 calcUVs = float2(dot(worldTangent, worldSpacePosition), dot(worldBinormal, worldSpacePosition));
o.uv = TRANSFORM_TEX(calcUVs, _MainTex);
return o;
}
fixed4 frag (v2f i) : SV_Target
{
fixed4 col = tex2D(_MainTex, i.uv);
return col;
}
ENDCG
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment