In class we will explore the following concepts - you will have the opportunity to edit the code directly here or to copy & paste code into your own codepen to play with three.js.
In codepen, underneath the JavaScript settings, you will need to add the Three.JS library in order for your code to work.
See the Pen Three.JS Intro - Rotating Cube by Sophia Sobers (@Sophia-Sobers) on CodePen.
This code shows four different primitive shapes available in WebGL:
See the Pen Untitled by Sophia Sobers (@Sophia-Sobers) on CodePen.
This code explore the use of Basic, Lambert, Phong, and Standard material. Each material has a different color. An ambient light is used to illuminate the scene. A point light is attached to the mouse. Move your mouse to see how the different materials interact with light.
MeshBasicMaterial,
MeshLambertMaterial,
MeshPhongMaterial, and
MeshStandardMaterial,
The MeshBasicMaterial will not respond to the scene's lighting, making it appear flat.
The MeshLambertMaterial and MeshPhongMaterial will reflect the light, showing how different materials react to light sources.
The MeshStandardMaterial is a physically-based rendering (PBR) material, providing more realistic lighting and shading.
See the Pen Untitled by Sophia Sobers (@Sophia-Sobers) on CodePen.
In this example, a box is animated using sin() and cos() to create a circular path.
angle This variable will change over time to update the cube's position.
radius The radius of the circle along which the cube will move.
See the Pen Untitled by Sophia Sobers (@Sophia-Sobers) on CodePen.
In this example, a box is animated using sin() and cos() to create a circular path.
speed Controls how fast the cube moves.
cubeMaxBoundary Defines the maximum distance the cube can move along the x-axis before it should "bounce" back.
direction A multiplier that will alternate between 1 and -1 to change the direction of the cube's movement.
See the Pen Untitled by Sophia Sobers (@Sophia-Sobers) on CodePen.
Mouse Position Tracking The onMouseMove function updates the mouse vector with the current mouse position, normalized to the range [-1, 1].
Easing Function easeTowardsTarget is a simple linear interpolation function that gradually changes the cube's position to the target position, creating a smooth transition.
The target position is set based on the mouse position, scaled to match the 3D space.
The cube's position is updated using the easing function, moving it towards the target position.
The renderer.render call updates the scene.
See the Pen Untitled by Sophia Sobers (@Sophia-Sobers) on CodePen.
Elements
Renderer
renderer.shadowMap.enabled = true; is crucial for enabling shadow rendering in the scene.
Ambient Light
Provides indirect, soft light that doesn't cast shadows. It's used to simulate the light that bounces off surfaces in the real world.
Point Light
A light that emits from a single point in all directions. pointLight.castShadow = true; enables this light to cast shadows.
Cube
A simple cube that will cast shadows. cube.castShadow = true; enables shadow casting for the cube.
Plane
A flat surface that receives shadows. plane.receiveShadow = true; enables shadow receiving for the plane.
Animation Loop
The cube rotates continuously. The point light moves in a circular path, creating dynamic shadows as its position changes relative to the cube and plane.
See the Pen Untitled by Sophia Sobers (@Sophia-Sobers) on CodePen.
OrbitControls is Three.js class that allows the camera to orbit around a target point. You can interact with the scene using your mouse: left-click and drag to orbit, right-click and drag to pan, and scroll to zoom. You can further customize the behavior of OrbitControls by adjusting its properties, such as controls.minDistance, controls.maxDistance, controls.enableDamping, etc.
Elements
OrbitControls
This class allows the camera to orbit around a target (the center of the scene by default). The user can rotate, zoom, and pan the scene using the mouse.
Camera Position and LookAt
Setting up the initial position of the camera and the point it should look at.
Scene and Objects
The scene contains a cube and a sphere for demonstration purposes.
Camera Setup
The camera is positioned to view the entire scene initially.
Animation Loop
The animate function updates the controls and renders the scene. The controls.update() call is necessary if damping (inertia) or auto-rotation is enabled in the controls.
See the Pen Untitled by Sophia Sobers (@Sophia-Sobers) on CodePen.
With this setup, the Three.js scene will appear as part of a webpage, overlaying the background color or any other HTML content you have.
Renderer Transparency
By setting { alpha: true } in the renderer and using renderer.setClearColor(0x000000, 0), the background of the Three.js canvas is fully transparent, allowing the webpage's background to show through.
See the Pen Untitled by Sophia Sobers (@Sophia-Sobers) on CodePen.