I’m trying to achieve a parallax effect similar to this:
There are also several shadertoy shaders that are like this.
I’ve converted shadertoy shaders in the past but I’m not entirely sure how to convert these shaders…
Here’s my current 2D shader that creates a grid of dots that I’d like to parallax:
Shader "Unlit/Grid"
{
Properties
{
_BackgroundColor ("Background Color", Color) = (0,0,0,0)
_DotColor ("Dot Color", Color) = (1,1,1,1)
_DotOffset("Dot Offset", Vector) = (0,0,0,0)
_DotRadius("Dot Radius", Float) = 0.04
_DotSpacing("Dot Spacing", Float) = 1.0
_DotSmoothing("Dot Smoothing", Float) = 1.0
}
SubShader
{
Tags { "RenderType"="Transparent" "Queue" = "Transparent" "IgnoreProjector" = "True"}
Blend SrcAlpha OneMinusSrcAlpha
Cull Off ZWrite Off
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
// make fog work
#pragma multi_compile_fog
#include "UnityCG.cginc"
uniform float4 _BackgroundColor;
uniform float4 _DotColor;
uniform float2 _DotOffset;
uniform float _DotRadius;
uniform float _DotSpacing;
uniform float _DotSmoothing;
struct appdata
{
float2 uv : TEXCOORD0;
float4 vertex : POSITION;
float3 worldPos : TEXCOORD1;
};
struct v2f
{
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
float3 worldPos : TEXCOORD1;
};
float dots( float2 p, float2 offset, float spacing, float radius, float smoothing )
{
// Divide into squares, create grid
float2 dst = fmod(abs(p - offset), spacing);
//float2 dst = opRep(abs(p - offset), spacing);
// Create circles
float2 c = distance(dst, float2(0.5, 0.5));
float2 dc = fwidth(c) * smoothing;
float2 f = smoothstep(-dc+radius, dc+radius, c);
float result = saturate(f.x * f.y);
return 1-result;
}
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = v.uv;
o.worldPos = mul(unity_ObjectToWorld, v.vertex);
return o;
}
fixed4 frag (v2f i) : SV_Target
{
float3 p = i.worldPos.xyz;
fixed4 col = _BackgroundColor;
float gridDots = dots(p, _DotOffset, _DotSpacing, _DotRadius, _DotSmoothing);
col = lerp(col, _DotColor, gridDots);
return col;
}
ENDCG
}
}
}
I tried doing something like this:
for(int i = 0; i < 3; i++)
{
float gridDots = dots(p, _DotOffset, _DotSpacing, _DotRadius, _DotSmoothing);
p -= (p * 0.2) * i;
col = lerp(col, _DotColor, gridDots);
}
but it does a strange effect and doesn’t offset it “into the mesh” so I guess I’m going to need the camera position in the shader or something?
Any help would be appreciated.