Spherical World Effect in Shader Graph
13th April 2020
UnityShader Graph
Let's start with just the z axis - offsetting around a 2D circle - then move to a sphere. The basic concept is to use the z distance from the origin as the arc length to calculate the angle around the circle each vertex should be placed. The coordinates for a point on a circle can be found with:
x = r * sin(θ)
y = r * cos(θ)
y = r * cos(θ)
Where r is the circle's radius + vertex y coordinate. To find θ we can use:
θ = d / r
Where d is simply the z distance from the origin. Let's put this all into Shader Graph: First we get the distance from the origin and split it into it's x, y and z components. Next we can get θ by dividing d (z distance) by the radius. We'll need to multiply this by -1 and add 90° to it (1.57~ radians) to get the angle from the vertical axis. Complete the equation, subtracting the radius from the y value as the centre of the circle is at origin.y - radius. The x value remains unchanged. Finally we can add this new position to the origin and transform it from world to object space.
Our shader in action
x = r * cos(θ) * sin(ϕ)
y = r * sin(θ) * sin(ϕ)
z = r * cos(ϕ)
y = r * sin(θ) * sin(ϕ)
z = r * cos(ϕ)
Where θ is the longitude, the angle along the z/y plane (the same angle we've been working with so far) and ϕ is the colatitude, the angle along the x/y plane. We'll replace the middle section of our graph with new calculations for x, y and z.
The final result