1/7
https://react.dev/reference/rsc/use-client
What does ‘use client’ do, why in Three.js?
code runs on client for Next.js (every component is a server component by default)
server components on server produce HTML, server has no browser
can’t use React hooks (useEffect, useRef)
no browser APIs (webAPI for window, requestAnimationFrame, mouseEvents)
client component has these.
What is a common misunderstanding about client components?
They don't render on the server once to produce the first HTML.
What is the point of useRef?
Keeps stored variable value outside of re-render of function to prevent erasing.
React draws canvas from JSX (<canvas ref={canvasRef}, Three.js needs DOM element to draw into (const renderer = new THREE.WebGLRenderer({canvas}) // canvas = canvasRef.current;
In this case, create a empty box for the canvas element to refer to, then draw into it using useEffect(()=>…), then assign once to canvas. Will auto update but keep the same reference
What is the high level process of a tick function?
Define delta = new THREE.Timer() outside of tick function call
Define tick function, where:
Mesh updates occur
Camera updates occur
Renderer updates occur
Time update occurs
Tick update occurs
Write out the code for a tick function for a basic object spinning, and for the scene?
const delta = new THREE.timer();
const tick = () => {
mesh.rotation.y += 0.1;
renderer.render(scene, camera);
delta.update();
requestAnimationFrame(tick);
};
From a high level perspective, explain the relation of Three.js components to one another.
Renderer connects the camera to the scene and also the renderer to the canvas.
Meshes and cameras exist within the scene.
Tick function, updates, mesh, cameras, renderer, time, and itself.
Canvas paints the scene onto itself. Updates occur by tick function.
‘Optionally, add renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2)) before setSize for extra sharpness on high-DPI screens.’
What does renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2)) do, and why before setSize ?
window.devicePixelRatio # of physical pixels in one CSS pixel:
monitor: 1, laptops: 2, phone: 3
Basically, it's setting a cap of a ratio of two, as most standard laptops have this physical pixel ratio. And also on phones, if set to three, it would tax the GPU heavily. So for monitors, it would take the lowest, which would either be one or if it's a laptop, then it would be two.