JavaScript RayTracing Raytracing is a relatively simple way to render images of 3D objects The core is an elegant idea that one can simulate the real world behavior of photons of light bouncing off of surfaces and colors accumulating from their paths It s not inherently fast but the simplicity of the concepts lets s model interesting things like refraction reflection and depth of field in ways that mirror natural processes Background Raytracing is a popular topic for learning about computer graphics As implementing a raytracer requires a person to understand vector math fast code and even recursion The reward is a sexy image more compelling is the journey and the learning experience But raytracing is still hard to learn Sometimes understanding concepts and explanations requires an indepth understanding of the programming language or the mathematics when people start out it s often hard to connect the dots There are a range of raytracer implementations online ranging from compact examples that fits on a business card to larger more complex solutions that supports nearly every potential feature The aim of these text is to provide you with a step by step example that will give you a running start into understanding raytracing using JavaScript as the learning tool Setup To begin with you ll create a rander CANVAS to output your graphics that is the raytracer output Create a CANVAS and get a context in order to generate a proper data array You aren t going to use traditional CANVAS drawing functions like fillRect instead this raytracer will directly compute pixel data and then put it into an image hence the getImageData var c document createElement canvas c width 640 0 5 c height 480 0 5 c style border 1px solid blue document body appendChild c var ctx c getContext 2d var data ctx getImageData 0 0 c width c height The Scene You need to define three different kinds of things in 3D space a camera from which we cast rays into the scene objects that can be hit by those rays and are drawn into the scene and lights that change the color of rays by extension coloring objects In this case you ll define these objects as simple objects with vectors defined as x y z objects var scene The Camera Your camera is pretty simple it s a point in space where you can imagine that the camera sits a fieldOfView which is the angle from the right to the left side of its frame and a vector which determines what angle it points in scene camera point x 0 y 1 8 z 10 fieldOfView 45 vector x 0 y 3 z 0 Lights Lights are defined by a type and some additional attributes such as a points in space The surfaces in the scene will have lambert shading which will be affected by any visible lights Lights are stored as an array as you ll typically have more than one light For example you d define a light and its location like this scene lights type point point x 30 y 10 z 20 You can add other light properties as you develop more complex lights e g directional light or a cone light Objects This raytracer handles sphere objects with any color position radius and surface properties scene objects type sphere point x 0 y 3 5 z 3 color x 155 y 200 z 155 specular 0 2 lambert 0 7 ambient 0 1 radius 3 type sphere point x 4 y 2 z 1 color x 155 y 155 z 155 specular 0 1 lambert 0 9 ambient 0 0 radius 0 2 type sphere point x 4 y 3 z 1 color x 255 y 255 z 255 specular 0 2 lambert 0 7 ambient 0 1 radius 0 1 Firing Rays This is one part where you can t follow nature exactly technically photons come out of lights bounce off of objects and then some hit the eye and many don t Simulating this sending rays in all directions out of each light and most not having any real effect would be too inefficient Luckily the reverse is more efficient and has practically the same result instead of rays going from lights to the eye you follow rays from the eye and see if they end up hitting any features and lights on their travels For each pixel in the canvas there needs to be at least one ray of light that determines its color by bouncing through the scene function render scene first unpack the scene to make it easier to reference var camera scene camera var objects scene objects var lights scene lights This process is a bit odd because there s a disconnect between pixels and vectors given the left and right top and bottom rays the rays you shoot are just interpolated between them in little increments Starting with the height and width of the scene the cameras place direction and field of view you calculate factors that create width height vectors for each ray Start by creating a simple vector pointing in the direction the camera is pointing a unit vector var eyeVector vec3 normalize vec3 subtract camera vector camera point and then well rotate this by combining it with a version that s turned 90 right and one that s turned 90 up Since the cross product takes two vectors and creates a third that s perpendicular to both you use a pure UP vector to turn the camera right and that right vector to turn the camera up vpRight Vector unitVector Vector crossProduct eyeVector Vector UP vpUp Vector unitVector Vector crossProduct vpRight eyeVector The actual ending pixel dimensions of the image arent important here note that width and height are in pixels but the numbers you compute here are just based on the ratio between them height width and the fieldOfView of the camera var fovRadians Math PI camera fieldOfView 2 180 var heightWidthRatio height width var halfWidth Math tan fovRadians var halfHeight heightWidthRatio halfWidth var camerawidth halfWidth 2 var cameraheight halfHeight 2 var pixelWidth camerawidth width 1 var pixelHeight cameraheight height 1 var index var color var ray point camera point for var x 0 x width x for var y 0 y height y turn the raw pixel x and y values into values from 1 to 1 and use these values to scale the facing right and facing up vectors so that we generate versions of the eyeVector that are skewed in each necessary direction var xcomp vec3 scale vpRight x pixelWidth halfWidth var ycomp vec3 scale vpUp y pixelHeight halfHeight ray vector vec3 normalize vec3 add3 eyeVector xcomp ycomp use the vector generated to raytrace the scene returning a color as a x y z vector of RGB values color trace ray scene 0 index x 4 y width 4 data data index 0 color x data data index 1 color y data data index 2 color z data data index 3 255 Now that each ray has returned and populated the data array with correctly lit colors fill the canvas with the generated data ctx putImageData data 0 0 Trace Given a ray you shoot it until it hits an object and return that objects color or the background color e g WHITE WHITE if no object is found This is the main function that s called in order to draw the image and it recurses into itself if rays reflect off objects and acquire more color function trace ray scene depth This is a recursive method if you hit something that s reflective then the call to surface t the bottom will return here and try to find what the ray reflected into Since this could easily go on forever first check that you haven t gone more than three bounces into a reflection if depth 3 return var distObject intersectScene ray scene If you don t hit anything fill this pixel with the background color in this case the color white if distObject 0 INFINITY return WHITE var dist distObject 0 var object distObject 1 The pointAtTime is another way of saying the intersection point of this ray into this object You compute this by simply taking the direction of the ray and making it as long as the distance returned by the intersection check var pointAtTime vec3 add ray point Vector scale ray vector dist return surface ray scene object pointAtTime sphereNormal object pointAtTime depth Detecting collisions against all objects Given a ray let s figure out whether it hit s anything and if so what s the closest thing it hits function intersectScene ray scene The base case is that it hits nothing and travels for INFINITY var closest INFINITY null But for each object you check whether it has any intersection and compare that intersection is it closer than INFINITY at first and then is it closer than other objects that have been hit for var i 0 i scene objects length i var object scene objects i dist sphereIntersection object ray if dist undefined dist closest 0 closest dist object return closest Detecting Sphere Collisions Spheres are one of the simplest objects for rays to interact with since the geometrical math for finding intersections and reflections with them is pretty straightforward function sphereIntersection sphere ray var eye_to_center vec3 sub sphere point ray point picture a triangle with one side going straight from the camera point to the center of the sphere another side being the vector The final side is a right angle This calculates the length of the vector side var v vec3 dot eye_to_center ray vector then the length of the straight from the camera to the center of the sphere var eoDot vec3 dot eye_to_center eye_to_center and compute a segment from the right angle of the triangle to a point on the v line that also intersects the circle discriminant sphere radius sphere radius eoDot v v If the discriminant is negative that means that the sphere hasn t been hit by the ray if discriminant 0 return else otherwise you return the distance from the camera point to the sphere Math sqrt dotProduct a a is the length of a vector so v Math sqrt discriminant means the length of the the vector just from the camera to the intersection point return v Math sqrt discriminant A normal is at each point on the surface of a sphere or some other object a vector that s perpendicular to the surface and radiates outward You need to know this so that you can calculate the way that a ray reflects off of a sphere function sphereNormal sphere pos return vec3 normalize vec3 sub pos sphere point Surface If trace determines that a ray intersected with an object surface decides what color it acquires from the interaction function surface ray scene object pointAtTime normal depth var b object color var c vec3 ZERO var lambertAmount 0 Lambert shading is your sexy shading which shows gradations from the most lit point on the object to the least if object lambert for var i 0 i scene lights length i var lightPoint scene lights i point First can you see the light If not this is a shadowy area and it gets no light from the lambert shading process if isLightVisible pointAtTime scene lightPoint continue Otherwise calculate the lambertian reflectance which essentially is a diffuse lighting system direct light is bright and from there less direct light is gradually beautifully less light var contribution vec3 dot vec3 normalize vec3 sub lightPoint pointAtTime normal sometimes this formula can return negatives so you check you only want positive values for lighting if contribution 0 lambertAmount contribution Specular is a fancy word for reflective rays that hit objects with specular surfaces bounce off and acquire the colors of other objects they bounce into if object specular This is basically the same thing as what you did in render just instead of looking from the viewpoint of the camera you re looking from a point on the surface of a shiny object seeing what it sees and making that part of a reflection var reflectedRay point pointAtTime vector vec3 reflectThrough ray vector normal var reflectedColor trace reflectedRay scene depth if reflectedColor c vec3 add c Vector scale reflectedColor object specular lambert should never blow out the lighting of an object even if the ray bounces between a lot of things and hits lights lambertAmount Math min 1 lambertAmount Ambient colors shine bright regardless of whether theres a light visible a circle with a totally ambient blue color will always just be a flat blue circle return vec3 add3 c vec3 scale b lambertAmount object lambert vec3 scale b object ambient Check whether a light is visible from some point on the surface of something Note that there might be an intersection here which is tricky but if its tiny its actually an intersection with the object you re trying to decide the surface of Thats why we check for 0 005 at the end This is the part that makes objects cast shadows on each other from here you d check to see if the area is in a shadowy spot can t see the light and when this returns false you make the area shadowy function isLightVisible pt scene light var distObject intersectScene point pt vector vec3 normalize vec3 sub pt light scene return distObject 0 0 005 Here you do a little fun magic just for the heck of it You have three spheres in the scene scene objects 0 is the big one kind of like Earth The other two are little so lets make them orbit around the big one and look cool The orbits of the two planets You use some basic trigonetry to do the orbits using Math sin and Math cos it s simple to get a unit circle for each planet var planet1 0 var planet2 0 function tick make one planet spin a little bit faster than the other just for effect planet1 0 1 planet2 0 2 set the position of each moon with some trig scene objects 1 point x Math sin planet1 3 5 scene objects 1 point z 3 Math cos planet1 3 5 scene objects 2 point x Math sin planet2 4 scene objects 2 point z 3 Math cos planet2 4 finally render the scene render scene and as soon as were finished render it again and move the planets again if playing setTimeout tick 10 var playing false function toggle e if playing e target innerText Play playing false else e target innerText Pause playing true tick render scene Then let the user control a cute playing animation document getElementById play onclick toggle Putting it all together into a single file document body style minHeight 600px let vp await fetch https notebook xbdev net var scripts vector0 9 js let vt await vp text let vs document createElement script document body appendChild vs vs innerHTML vt vec3 normalize vec3 norm vec3 add3 function a b c return vec3 add vec3 add a b c vec3 valid function v valid v x valid v y valid v z vec3 reflect function I N vec3 valid I vec3 valid N return vec3 sub I vec3 scale vec3 scale N vec3 dot N I 2 0 valid function val if val undefined throw new Error invalid value if isNaN val throw new Error NaN value if val 9999999 throw new Error number warning range if val 9999999 throw new Error number warning range var c document createElement canvas document body appendChild c c width 640 c height 480 var ctx c getContext 2d var data ctx getImageData 0 0 c width c height constants const WHITE vec3 1 1 1 const INFINITY 99999999 var scene scene camera point x 0 y 1 8 z 10 fieldOfView 45 vector x 0 y 3 z 0 scene lights type point point x 30 y 10 z 20 scene objects type sphere point x 0 y 3 5 z 3 color x 155 y 200 z 155 specular 0 2 lambert 0 7 ambient 0 1 radius 3 type sphere point x 4 y 2 z 1 color x 155 y 155 z 155 specular 0 1 lambert 0 9 ambient 0 0 radius 0 2 type sphere point x 4 y 3 z 1 color x 255 y 255 z 255 specular 0 2 lambert 0 7 ambient 0 1 radius 0 1 function render scene var camera scene camera var objects scene objects var lights scene lights var eyeVector vec3 normalize vec3 sub camera vector camera point var vUP vec3 0 1 0 var vpRight vec3 normalize vec3 cross eyeVector vUP var vpUp vec3 normalize vec3 cross vpRight eyeVector var fovRadians Math PI camera fieldOfView 2 180 var heightWidthRatio c height c width var halfWidth Math tan fovRadians var halfHeight heightWidthRatio halfWidth var camerawidth halfWidth 2 var cameraheight halfHeight 2 var pixelWidth camerawidth c width 1 var pixelHeight cameraheight c height 1 var index color var ray point camera point for var x 0 x c width x for var y 0 y c height y var xcomp vec3 scale vpRight x pixelWidth halfWidth var ycomp vec3 scale vpUp y pixelHeight halfHeight ray vector vec3 normalize vec3 add3 eyeVector xcomp ycomp color trace ray scene 0 var index x 4 y c width 4 data data index 0 color x data data index 1 color y data data index 2 color z data data index 3 255 ctx putImageData data 0 0 function trace ray scene depth if depth 3 return var distObject intersectScene ray scene if distObject 0 INFINITY return WHITE var dist distObject 0 var object distObject 1 var pointAtTime vec3 add ray point vec3 scale ray vector dist return surface ray scene object pointAtTime sphereNormal object pointAtTime depth function intersectScene ray scene var closest INFINITY null for var i 0 i scene objects length i var object scene objects i var dist sphereIntersection object ray if dist undefined dist closest 0 closest dist object return closest function sphereIntersection sphere ray var eye_to_center vec3 sub sphere point ray point var v vec3 dot eye_to_center ray vector var eoDot vec3 dot eye_to_center eye_to_center var discriminant sphere radius sphere radius eoDot v v if discriminant 0 return else return v Math sqrt discriminant function sphereNormal sphere pos return vec3 normalize vec3 sub pos sphere point function surface ray scene object pointAtTime normal depth var b object color var c vec3 0 0 0 var lambertAmount 0 if object lambert for var i 0 i scene lights length i var lightPoint scene lights i point if isLightVisible pointAtTime scene lightPoint continue var contribution vec3 dot vec3 normalize vec3 sub lightPoint pointAtTime normal if contribution 0 lambertAmount contribution if object specular var reflectedRay point pointAtTime vector vec3 reflect ray vector normal var reflectedColor trace reflectedRay scene depth if reflectedColor c vec3 add c vec3 scale reflectedColor object specular lambertAmount Math min 1 lambertAmount return vec3 add3 c vec3 scale b lambertAmount object lambert vec3 scale b object ambient function isLightVisible pt scene light var distObject intersectScene point pt vector vec3 normalize vec3 sub pt light scene return distObject 0 0 005 var planet1 0 var planet2 0 function tick planet1 0 1 planet2 0 2 scene objects 1 point x Math sin planet1 3 5 scene objects 1 point z 3 Math cos planet1 3 5 scene objects 2 point x Math sin planet2 4 scene objects 2 point z 3 Math cos planet2 4 try render scene catch e console log error e line e lineNumber for let iid 0 iid 99999 iid clearInterval iid return if playing setTimeout tick 10 var playing false function toggle e if playing e target innerText Start playing false else e target innerText Pause playing true tick document body appendChild document createElement hr let button document createElement button button innerHTML Start document body appendChild button button onclick toggle document body style text align center tick console log ready
return vec3 normalize vec3 sub pos sphere point Surface If trace determines that a ray intersected with an object surface decides what color it acquires from the interaction function surface ray scene object pointAtTime normal depth var b object color var c vec3 ZERO var lambertAmount 0 Lambert shading is your sexy shading which shows gradations from the most lit point on the object to the least if object lambert for var i 0 i scene lights length i var lightPoint scene lights i point First can you see the light If not this is a shadowy area and it gets no light from the lambert shading process if isLightVisible pointAtTime scene lightPoint continue Otherwise calculate the lambertian reflectance which essentially is a diffuse lighting system direct light is bright and from there less direct light is gradually beautifully less light var contribution vec3 dot vec3 normalize vec3 sub lightPoint pointAtTime normal sometimes this formula can return negatives so you check you only want positive values for lighting if contribution 0 lambertAmount contribution Specular is a fancy word for reflective rays that hit objects with specular surfaces bounce off and acquire the colors of other objects they bounce into if object specular This is basically the same thing as what you did in render just instead of looking from the viewpoint of the camera you re looking from a point on the surface of a shiny object seeing what it sees and making that part of a reflection var reflectedRay point pointAtTime vector vec3 reflectThrough ray vector normal var reflectedColor trace reflectedRay scene depth if reflectedColor c vec3 add c Vector scale reflectedColor object specular lambert should never blow out the lighting of an object even if the ray bounces between a lot of things and hits lights lambertAmount Math min 1 lambertAmount Ambient colors shine bright regardless of whether theres a light visible a circle with a totally ambient blue color will always just be a flat blue circle return vec3 add3 c vec3 scale b lambertAmount object lambert vec3 scale b object ambient Check whether a light is visible from some point on the surface of something Note that there might be an intersection here which is tricky but if its tiny its actually an intersection with the object you re trying to decide the surface of Thats why we check for 0 005 at the end This is the part that makes objects cast shadows on each other from here you d check to see if the area is in a shadowy spot can t see the light and when this returns false you make the area shadowy function isLightVisible pt scene light var distObject intersectScene point pt vector vec3 normalize vec3 sub pt light scene return distObject 0 0 005 Here you do a little fun magic just for the heck of it You have three spheres in the scene scene objects 0 is the big one kind of like Earth The other two are little so lets make them orbit around the big one and look cool The orbits of the two planets You use some basic trigonetry to do the orbits using Math sin and Math cos it s simple to get a unit circle for each planet var planet1 0 var planet2 0 function tick make one planet spin a little bit faster than the other just for effect planet1 0 1 planet2 0 2 set the position of each moon with some trig scene objects 1 point x Math sin planet1 3 5 scene objects 1 point z 3 Math cos planet1 3 5 scene objects 2 point x Math sin planet2 4 scene objects 2 point z 3 Math cos planet2 4 finally render the scene render scene and as soon as were finished render it again and move the planets again if playing setTimeout tick 10 var playing false function toggle e if playing e target innerText Play playing false else e target innerText Pause playing true tick render scene Then let the user control a cute playing animation document getElementById play onclick toggle Putting it all together into a single file document body style minHeight 600px let vp await fetch https notebook xbdev net var scripts vector0 9 js let vt await vp text let vs document createElement script document body appendChild vs vs innerHTML vt vec3 normalize vec3 norm vec3 add3 function a b c return vec3 add vec3 add a b c vec3 valid function v valid v x valid v y valid v z vec3 reflect function I N vec3 valid I vec3 valid N return vec3 sub I vec3 scale vec3 scale N vec3 dot N I 2 0 valid function val if val undefined throw new Error invalid value if isNaN val throw new Error NaN value if val 9999999 throw new Error number warning range if val 9999999 throw new Error number warning range var c document createElement canvas document body appendChild c c width 640 c height 480 var ctx c getContext 2d var data ctx getImageData 0 0 c width c height constants const WHITE vec3 1 1 1 const INFINITY 99999999 var scene scene camera point x 0 y 1 8 z 10 fieldOfView 45 vector x 0 y 3 z 0 scene lights type point point x 30 y 10 z 20 scene objects type sphere point x 0 y 3 5 z 3 color x 155 y 200 z 155 specular 0 2 lambert 0 7 ambient 0 1 radius 3 type sphere point x 4 y 2 z 1 color x 155 y 155 z 155 specular 0 1 lambert 0 9 ambient 0 0 radius 0 2 type sphere point x 4 y 3 z 1 color x 255 y 255 z 255 specular 0 2 lambert 0 7 ambient 0 1 radius 0 1 function render scene var camera scene camera var objects scene objects var lights scene lights var eyeVector vec3 normalize vec3 sub camera vector camera point var vUP vec3 0 1 0 var vpRight vec3 normalize vec3 cross eyeVector vUP var vpUp vec3 normalize vec3 cross vpRight eyeVector var fovRadians Math PI camera fieldOfView 2 180 var heightWidthRatio c height c width var halfWidth Math tan fovRadians var halfHeight heightWidthRatio halfWidth var camerawidth halfWidth 2 var cameraheight halfHeight 2 var pixelWidth camerawidth c width 1 var pixelHeight cameraheight c height 1 var index color var ray point camera point for var x 0 x c width x for var y 0 y c height y var xcomp vec3 scale vpRight x pixelWidth halfWidth var ycomp vec3 scale vpUp y pixelHeight halfHeight ray vector vec3 normalize vec3 add3 eyeVector xcomp ycomp color trace ray scene 0 var index x 4 y c width 4 data data index 0 color x data data index 1 color y data data index 2 color z data data index 3 255 ctx putImageData data 0 0 function trace ray scene depth if depth 3 return var distObject intersectScene ray scene if distObject 0 INFINITY return WHITE var dist distObject 0 var object distObject 1 var pointAtTime vec3 add ray point vec3 scale ray vector dist return surface ray scene object pointAtTime sphereNormal object pointAtTime depth function intersectScene ray scene var closest INFINITY null for var i 0 i scene objects length i var object scene objects i var dist sphereIntersection object ray if dist undefined dist closest 0 closest dist object return closest function sphereIntersection sphere ray var eye_to_center vec3 sub sphere point ray point var v vec3 dot eye_to_center ray vector var eoDot vec3 dot eye_to_center eye_to_center var discriminant sphere radius sphere radius eoDot v v if discriminant 0 return else return v Math sqrt discriminant function sphereNormal sphere pos return vec3 normalize vec3 sub pos sphere point function surface ray scene object pointAtTime normal depth var b object color var c vec3 0 0 0 var lambertAmount 0 if object lambert for var i 0 i scene lights length i var lightPoint scene lights i point if isLightVisible pointAtTime scene lightPoint continue var contribution vec3 dot vec3 normalize vec3 sub lightPoint pointAtTime normal if contribution 0 lambertAmount contribution if object specular var reflectedRay point pointAtTime vector vec3 reflect ray vector normal var reflectedColor trace reflectedRay scene depth if reflectedColor c vec3 add c vec3 scale reflectedColor object specular lambertAmount Math min 1 lambertAmount return vec3 add3 c vec3 scale b lambertAmount object lambert vec3 scale b object ambient function isLightVisible pt scene light var distObject intersectScene point pt vector vec3 normalize vec3 sub pt light scene return distObject 0 0 005 var planet1 0 var planet2 0 function tick planet1 0 1 planet2 0 2 scene objects 1 point x Math sin planet1 3 5 scene objects 1 point z 3 Math cos planet1 3 5 scene objects 2 point x Math sin planet2 4 scene objects 2 point z 3 Math cos planet2 4 try render scene catch e console log error e line e lineNumber for let iid 0 iid 99999 iid clearInterval iid return if playing setTimeout tick 10 var playing false function toggle e if playing e target innerText Start playing false else e target innerText Pause playing true tick document body appendChild document createElement hr let button document createElement button button innerHTML Start document body appendChild button button onclick toggle document body style text align center tick console log ready