When we were working in 2D, we didn't really need to worry about lighting.
We can do this in 3D too, it just doesn't work nearly as well.
Light and shadow matter for being able to recognize objects!
Approximate the (real) interaction of light with matter.
Toy Story (1995)
Big Hero 6 (2014)
The answers do not have to be realistic!
Sometimes we'll deliberately make shading unrealistic, for performance or stylistic reasons.
TLoZ: Wind Waker (2002)
Return of the Obra Dinn (2018)
We will focus on direct illumination.
(I have watched a two graduate students struggle for two weeks to get an indirect illumination model correct for a class)
Intensity and direction of light sources change what surfaces are affected, and how.
Light source with no position, but a direction. Can think of this like sunshine: a light located at infinity so that there's no "source".
In Processing: directionalLight(r, g, b, dir_x, dir_y, dir_z)
void setup() {
size(500, 500, P3D);
noStroke();
}
void draw() {
background(0);
//This shines a light in the -y direction (note:
// Processing defaults to a left-handed coordinate system)
directionalLight(255, 255, 255, 0, -1, 0);
translate(width/2, height/2, 0);
sphere(50);
}
Light source with no direction, but position. Can think of this like an idealized lightbulb: shines in all directions.
pointLight(r, g, b, pos_x, pos_y, pos_z)
How does intensity of light change if we change position relative to it?
Where is the point light in this image?
Which image has the point light, and which has the directional light?
Has position and direction. Approximates a single source of light with direction and fall off. Like a spotlight!
spotLight(r, g, b,
pos_x, pos_y, pos_z,
dir_x, dir_y, dir_z,
angle, concentration)
A non-directional "general" source of light. Approximates many light bounces within a scene.
Similar to indirect lighting on a cloudy day, or (to a lesser extent) during magic hour.
ambientLight(r, g, b)
void setup(){
size(800, 800, P3D);
}
float T = 0.01;
void keyPressed(){
beginCamera();
if(keyCode == LEFT){
rotateY(T);
} else if (keyCode == RIGHT){
rotateY(-T);
} else if (keyCode == UP){
rotateX(T);
} else if (keyCode == DOWN){
rotateX(-T);
}
endCamera();
}
void draw(){
noStroke();
background(50);
for(int i = 0; i < 800; i+=15){
for(int j = 0; j < 800; j+=15){
pushMatrix();
translate(i, j, 200);
box(5);
popMatrix();
}
}
println(frameRate);
}
Draw 2800 boxes to the screen.
Each box is 12 triangles.
Total: over 33,600 triangles.
Result: reasonable framerate.
Is 3D slower than 2D?
Yes, but not actually by that much.
Not built-in to Processing, but this isn't that hard to do yourself!
What would we put in a class that represents a (fixed) camera position that we want to switch to?
class PresetCamera {
PresetCamera(float eyeX, float eyeY, float eyeZ,
float atX, float atY, float atZ,
float upX, float upY, float upZ){
// Stuff stuff stuff
}
void activate(){
camera(this.eyeX, this.eyeY, ...)
}
}
What does the up
parameter do in the camera?
eye
center
up
Sorry! It's up on Canvas now, along with yesterday's demo.
No real great ways to do this, unfortunately :\
Take your scene, draw it out on graph paper, and then try to plot where the camera goes. (But this is labor intensive!)
Lots of options, just be aware that 3D file formats are not standardized (i.e. some files that work in some apps may not work in others).
For OBJ files (can load directly into Processing):
For STL files (require conversion): ThingiVerse
In 2D
void setup(){
size(800,800);
}
void draw(){
background(50);
float ctrl = (frameCount % 100) / 100.0;
float x = lerp(0, 800, ctrl);
float y = lerp(0, 800, ctrl);
translate(x, y);
rect(0, 0, 50, 50);
}
void setup(){
size(800,800, P3D);
}
void draw(){
background(50);
float ctrl = (frameCount % 100) / 100.0;
float x = lerp(0, 800, ctrl);
float y = lerp(0, 800, ctrl);
translate(x, y, 0);
box(50);
}
In 3D
Boxes and triangles.
Just box()
and sphere()
Lots of hard work. Programs like Blender exist, but word of mouth seems to be that programs like AutoDesk Maya and Houdini are much better for experienced users.
Take time to think about what your shapes are doing and why they're doing that. Drawing on paper is often helpful.
Order matters more in 3D than in 2D. Example: triangle directions.
1
2
3
1
3
2