I was wondering if it is possible to create a dynamic 2D raycast lighting system inside of unity4, I have been browsing around and am looking to create something like this :
I was wondering :
- If it is actually possible using Unity’s built in raycast physics
- Would a modern mobile device be able to handle up to 10,000 rays
- How I would even start going about tackling such challenge
Any help given is much appreicated, thanks
This is called a raytrace renderer. These are completely possible, but you ABSOLUTELY DO NOT WANT TO USE UNITY’S BUILT IN RAYCAST FOR IT. The reason for this is that Unity’s Raycast runs on the CPU. The CPU can handle a few of these rays, tens, maybe even hundreds, but a raytracer requires many, MANY rays. It can get to the point where you are using literally millions of them. You do NOT want to try this on the CPU.
The GPU, on the other hand, is designed to be hugely parallel (that is, it does a lot of things at the same time), and is also optimised for vector calculation, so it can do this a lot faster. That video is running the raytracer on the GPU (since it is using OpenGL). However, even if you had it working on the GPU, you would be very hard pressed to get it working on a mobile platform. Polygon projection, the method Unity uses by default, is a MUCH faster method of calculating lighting, and while it gives lower quality effects, on the whole, it is in more general use because it is much easier for a GPU to handle. It is only recently that real-time raytracing has actually become possible, even though the technique has been around since the very early days of CGI. This is because raytracing is inherently a hugely expensive form of rendering, and it is only in recent years that GPUs have become powerful enough to handle complex raytracing in real-time.
That said, a 2D raytracer will be much less taxing than a 3D one, but you still face the issues that:
- You will not be able to do it on the CPU
- It is not likely to work on mobile unless you do some SERIOUS optimising, and sacrifice some quality.
This lighting looks ok for Unity on a computer (= maybe not on mobile/tablet). The guy explains how he does it:
He computes some rays from the mouse cursor to an angle, going from 0 to 360 degrees with little increments (he says 700, I would guess he goes in 0.5 degrees increments, so 720 rays). He then checks if this ray collides with one of the occluder lines that he defined.
Then I guess he draws a triangle between each angle: 3 points: one point under the mouse cursor, one point for collision between ray at angle A and one point for collision with tay at angle (A + increment). There may be imperfections but using a smaller increment will hide them (but increasing the CPU usage)
It’s easy to define a dynamic mesh with Unity and update it at each frame = Update. 720 rays means 720 triangles, it’s a very small amount even modifying them at each frame.
You could set vertex colors or UV+texture to make the red gradient (using the distance between the mouse cursor and the collision point to compute the color gradient)
The ray collision could be done using a simple segment to segment 2D collision system. In the video, the guy says he is using Box2D. Unity has Physics2D.Linecast for example but writing a line intersection script is not too difficult and well documented on the net. There are also lots of ways to optimise this so each ray doesn’t check every occluder lines with a 2D partitioning system (I guess Unity’s one is already improved in that way).
It’s not beginner scripting but I think it can be done in Unity and run at 60Hz on less than 5 years old computers/laptop.