Last active
February 3, 2023 22:01
-
-
Save olie304/dd8d10a8b303c82f06bf30b991823de3 to your computer and use it in GitHub Desktop.
Particle Custom Vertex Streams World Position Shader
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// All Credit goes to | |
// http://harrygodden.com/blog/?article=vrc-gridsnap | |
Shader "Terri/GridShaderUnlit" { | |
Properties { | |
_MainTex("Texture", 2 D) = "white" {} | |
_GridSize("Grid Size", Float) = 1.0 | |
_SnapDistance("Snap Distance", Float) = 2.0 | |
} | |
SubShader { | |
Tags { | |
"RenderType" = "Opaque" | |
} | |
LOD 100 | |
Pass { | |
CGPROGRAM | |
# pragma vertex vert | |
# pragma fragment frag | |
# pragma multi_compile_particles | |
# pragma multi_compile_fog | |
# include "UnityCG.cginc" | |
struct appdata_t { | |
float4 vertex: POSITION; | |
float4 texcoords: TEXCOORD0; | |
}; | |
struct v2f { | |
float4 vertex: SV_POSITION; | |
float2 texcoord: TEXCOORD0; | |
UNITY_FOG_COORDS(1) | |
}; | |
sampler2D _MainTex; | |
float4 _MainTex_ST; | |
float _GridSize; | |
float _SnapDistance; | |
v2f vert(appdata_t v) { | |
v2f o; | |
o.vertex = v.vertex; | |
o.texcoord = TRANSFORM_TEX(v.texcoords, _MainTex); | |
// Get world position of object | |
float3 ObjectWorldPosition = v.vertex.xyz; | |
// Snap object origin to grid | |
float3 SnappedOrigin = float3(floor(((ObjectWorldPosition.x / _GridSize) + 0.5)) * _GridSize, | |
floor(((ObjectWorldPosition.y / _GridSize) + 0.5)) * _GridSize, | |
floor(((ObjectWorldPosition.z / _GridSize) + 0.5)) * _GridSize); | |
// Calculate snap offset | |
float3 VertexOffset = ObjectWorldPosition - SnappedOrigin; | |
// Check if within snap distance | |
if (length(VertexOffset) < _SnapDistance) { | |
// Apply offset | |
o.vertex.xyz -= VertexOffset; | |
} | |
o.vertex = UnityObjectToClipPos(o.vertex); | |
UNITY_TRANSFER_FOG(o, o.vertex); | |
return o; | |
} | |
fixed4 frag(v2f i): SV_Target { | |
// sample the texture | |
fixed4 col = tex2D(_MainTex, i.texcoord); | |
// apply fog | |
UNITY_APPLY_FOG(i.fogCoord, col); | |
return col; | |
} | |
ENDCG | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment