CS 4406 Assignment 3
CS 4406 Assignment 3
Introduction
apply color principles in computer graphics. The object, a 3D sphere, will be animated within a
viewport where it bounces off the edges and changes color every time it hits the boundary.
Additionally, the scene will be illuminated with a fixed light source from the upper left corner to
Key Concepts
Transformations: Moving the object along the x and y axes to simulate motion across the
viewport.
Bouncing: Detecting collisions with the edges of the viewport and reversing the object’s
direction.
Color: Changing the color of the object whenever it hits the edges.
Lighting: Using a fixed light source to create light and shadow effects as the object moves.
Step-by-Step Solution
First, I set up the basic scene using Three.js. This includes initializing the scene, camera, and
renderer.
// Initialize the scene, camera, and renderer
0.1, 1000);
Renderer.setSize(window.innerWidth, window.innerHeight);
Document.body.appendChild(renderer.domElement);
Camera.position.z = 5;
The object in this program is a sphere. I used the THREE.SphereGeometry class to create it, with
Const material = new THREE.MeshPhongMaterial({ color: 0x00ff00 }); // Initial green color
Scene.add(myMesh);
3. Adding a Light Source
A fixed light source is positioned at the upper left corner of the scene to create lighting and
shadows. The light is a yellow point light that illuminates the object.
// Add a yellow point light at the upper left corner of the scene
Scene.add(light);
The sphere moves across the viewport by updating its x and y coordinates on every frame. When
it hits the edges of the viewing area, the object “bounces” by reversing its direction and changing
color.
Let currentColor = 0;
Function animate() {
myMesh.position.x += velocity.x;
myMesh.position.y += velocity.y;
}
// Function to change the color when the object bounces
Function changeColor() {
Animate(): This function is called every frame to update the position of the object and check for
boundary collisions. When the object reaches the edges, the direction is reversed, and the color is
changed.
changeColor(): This function cycles through an array of predefined colors. Every time the object
CONCLUSION
This program demonstrates how to apply transformations, create bouncing motion, and use color
changes based on interaction with the viewport's boundaries. The use of lighting helps create
depth and realism by casting shadows on the 3D object. By continuously altering the object's
position and color, the program showcases dynamic and interactive principles in computer
graphics.
REFERENCES
Three.js Documentation. (n.d.). Basic Materials and Geometry in Three.js. Retrieved from
https://threejs.org/docs/
University of the People (UoPeople). (n.d.). Unit 3: Basic Concepts of Color and Transparency.