
CODE 6
void setup() {
size(640, 640, P3D);
noStroke();
fill(253,222,238);
}
void draw() {
noStroke();
background(253,222,238);
float dirY = (mouseY / float(height) - 0.5) * 2;
float dirX = (mouseX / float(width) - 0.5) * 2;
directionalLight(221, 212, 232, -dirX, -dirY, -1);
translate(width/2 - 10, height/2, 0);
sphere(85);
translate(6, 165, 10);
sphere(100);
}
CODE 6
Original Code
void setup() {
size(640, 360, P3D);
noStroke();
fill(204);
}
void draw() {
noStroke();
background(204);
float dirY = (mouseY / float(height) - 0.5) * 2;
float dirX = (mouseX / float(width) - 0.5) * 2;
directionalLight(204, 204, 204, -dirX, -dirY, -1);
translate(width/2 - 100, height/2, 0);
sphere(80);
translate(200, 0, 0);
sphere(80);
}
Original Code
Modifications
1) Translation of the Sphere:
translate(width/2 - 100, height/2, 0);
sphere(80);
translate(200, 0, 0);
sphere(80);
to
translate(width/2 - 10, height/2, 0);
sphere(85);
translate(6, 165, 10);
sphere(100);
2) Size of the screen:
size(640, 360, P3D);
to
size(640, 640, P3D);
3) Background Colour:
background(204);
to
background(253,222,238);
Function Analyse
The code creates creating a 3d effect sphere with lighting using the P3D rendering and Directionallight. From the codeabove, it can be clearly seen that the first part of code is similar to other codes, it is use to declare the set up such as display size and background colour.
This code is a interactive code and it can be controlled by the viewer. The shade and tone of the sphere are able to change by the moving of the mouse:
float dirY = (mouseY / float(height) - 0.5) * 2;
float dirX = (mouseX / float(width) - 0.5) * 2;
The shade of the sphere is set by using the function directionalLight. This function adds a directional light. Directional light comes from one direction: it is stronger when hitting a surface squarely, and weaker if it hits at a gentle angle. After hitting a surface, directional light scatters in all directions. The directionalLight needs to be write in a form of directionalLight(v1, v2, v3, nx, ny, nz). The v1, v2, and v3 parameters are interpreted as either RGB values, depending on the current color mode. The nx, ny, and nz parameters specify the direction the light is facing. By this, the shade of the sphere will change according to the location of the mouse. As what the code shown, the translate function enable the user to change the location of the sphere.
directionalLight(221, 212, 232, -dirX, -dirY, -1);
translate(width/2 - 10, height/2, 0);
sphere(85);
translate(6, 165, 10);
sphere(100);
}