CHECK OUT InterXeptor 3D, the 3D space-shooter game that came out of some of the ideas within this project: http://scratch.mit.edu/projects/10085324 ---------------------------------------------------------------------- This project is a test of 3D surface rendering, with lighting and intersection. It's very slow on my old Mac Mini, and I suspect it won't be fast enough on anything to be of practical use with a more realistic set of objects, but it's been an interesting thing to try out... Press P to pause the movement. Use arrow keys to rotate the view yourself (also while paused). [Watch out for the lag scratch has when holding down keys - if you suffer from this, try switching off or reducing key-repeat in your system prefs/control panel.]
Hmmm... I made this months ago, but I recently looked inside and accidentally switched to costume tab - and it autosaved :/ So now this looks like it's recently changed, but it hasn't really... Anyway, credit to maths and logic :) For step-by-step intro to 3D in Scratch, see this studio: http://scratch.mit.edu/studios/202972 *update* - added rotation with arrow keys, so you can watch the action from any angle :) How it works... To create a frame it goes through the list of surfaces (triangles in 3D space). Each surface has a list of (three) points in 3D space, so it can then work out the angle of the normal relative to the light source - this gives a base value for brightness. It also works out where the surface's three vertex points should be projected onto the 2D screen. It then splits the resulting 2D screen-projected triangle, if necessary, into two triangles that have horizontal bases. These horizontal-base triangles are rasterised into horizontal lines. It starts from the vertex and heads to the base, storing the 2D screen X position for the start and end of each horizontal line in a list. It also stores the 3D Z distance of the start and end of each line as it is placed into the list (to allow for overlap/intersect checking). The list has linking between horizontal lines that have the same Y co-ordinate on screen. Since lines with the same Y co-ordinate can overlap on the screen, it has to check a newly added line against all other lines with the same Y co-ordinate, doing various things (chopping and splitting) to the old and new lines when it detects an overlap - or worse, an intersection! Once all the horizontal lines are finished for all surfaces, it goes through them all and draws them onto the screen, modifying the pen brightness according to the original lighting value that line has inherited from its corresponding surface (i.e. the surface's normal relative to the light source), but also adding an extra brightness factor that depends upon the Z distance of the various parts of the line that it's drawing. Phew!