Curved Shader

HLSL/Unity

This is a curved shader I developed while working on a prototype for a game. The prototype was originally designed as top down RTS, where you would tap and spawn units to fight your way through an array of towers and enemies as you progressed across the map. It was decided that instead of targeting mobile for this project it would instead be released on Oculus Rift and Gear VR. With that in mind a top down perspective would no longer work. The idea was suggested that the world be converted into an Ellysium© style. So the entire enviornment would be curved allowing the player to see the whole world as they progressed.

Placeholder5   Placeholder5   Placeholder5  

With a curved surface like that things such as physics and navigation become difficult, especially using the native tools of Unity which was the engine being used for the project. I therefore suggested instead keeping the already existing "flat" enviorment and writing a shader that would curve the world.

The math behind the shader is rather simplistic. All verticies are rotated based off their distance from a given point. The further away from the point the larger the angle, this does cause some distortion in the objects, but if the angle is small enough it is minimal. One problem quickly arose from this though. If the world is being altered on the GPU the raycasts done using the camera on the CPU would no longer work. Therefore determining where on the map the player is looking, for example placing a recticle in the world, required additional math.

The math required is actually a ray circle intersection equation. Using the camera's position and the camera's look at direction as the ray's origin and direction I then use the same values being passed to the shaders to create a circle. Then performing the intersection equation will give a point from which a distance can be determined. Then placing the reticle this distance away from the starting point and applying the curved shader to the reticle results in an accurate location within the world for where the player is looking.