|
| 1 | +import { cancelFrame, frame } from "motion" |
| 2 | +import { animate } from "motion/three" |
| 3 | +import * as THREE from "three" |
| 4 | +import "./gpu-adapters.css" |
| 5 | + |
| 6 | +const canvas = document.querySelector("canvas") |
| 7 | +const status = document.querySelector("#status") |
| 8 | +const renderer = new THREE.WebGLRenderer({ canvas, antialias: true }) |
| 9 | +const scene = new THREE.Scene() |
| 10 | +const camera = new THREE.PerspectiveCamera(42, 1, 0.1, 100) |
| 11 | + |
| 12 | +renderer.setPixelRatio(Math.min(devicePixelRatio, 2)) |
| 13 | +renderer.setClearColor("#09090b") |
| 14 | +camera.position.set(0, 0, 7) |
| 15 | + |
| 16 | +scene.add(new THREE.HemisphereLight("#c4b5fd", "#111827", 3)) |
| 17 | + |
| 18 | +const meshMaterial = new THREE.MeshStandardMaterial({ |
| 19 | + color: "#8b5cf6", |
| 20 | + metalness: 0.25, |
| 21 | + roughness: 0.2, |
| 22 | + transparent: true, |
| 23 | +}) |
| 24 | +const mesh = new THREE.Mesh(new THREE.IcosahedronGeometry(1, 2), meshMaterial) |
| 25 | +mesh.position.x = -1.5 |
| 26 | +scene.add(mesh) |
| 27 | + |
| 28 | +const uniforms = { |
| 29 | + progress: { value: 0 }, |
| 30 | + tint: { value: new THREE.Color("#22d3ee") }, |
| 31 | +} |
| 32 | +const shaderMaterial = new THREE.ShaderMaterial({ |
| 33 | + uniforms, |
| 34 | + transparent: true, |
| 35 | + vertexShader: ` |
| 36 | + varying vec2 vUv; |
| 37 | +
|
| 38 | + void main() { |
| 39 | + vUv = uv; |
| 40 | + gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0); |
| 41 | + } |
| 42 | + `, |
| 43 | + fragmentShader: ` |
| 44 | + varying vec2 vUv; |
| 45 | + uniform float progress; |
| 46 | + uniform vec3 tint; |
| 47 | +
|
| 48 | + void main() { |
| 49 | + float edge = smoothstep(progress - 0.18, progress + 0.18, vUv.x); |
| 50 | + vec3 color = mix(tint, vec3(0.12, 0.05, 0.32), edge); |
| 51 | + gl_FragColor = vec4(color, 1.0); |
| 52 | + } |
| 53 | + `, |
| 54 | +}) |
| 55 | +const uniformMesh = new THREE.Mesh( |
| 56 | + new THREE.PlaneGeometry(1.8, 1.8, 24, 24), |
| 57 | + shaderMaterial |
| 58 | +) |
| 59 | +uniformMesh.position.x = 1.45 |
| 60 | +scene.add(uniformMesh) |
| 61 | + |
| 62 | +let meshActive = false |
| 63 | +let uniformActive = false |
| 64 | + |
| 65 | +document.querySelector("#move").addEventListener("click", () => { |
| 66 | + meshActive = !meshActive |
| 67 | + status.textContent = "Animating object and material properties" |
| 68 | + |
| 69 | + animate( |
| 70 | + mesh, |
| 71 | + meshActive |
| 72 | + ? { |
| 73 | + x: -0.8, |
| 74 | + rotateX: 35, |
| 75 | + rotateY: 180, |
| 76 | + scale: 1.25, |
| 77 | + color: "#f43f5e", |
| 78 | + opacity: 0.65, |
| 79 | + } |
| 80 | + : { |
| 81 | + x: -1.5, |
| 82 | + rotateX: 0, |
| 83 | + rotateY: 0, |
| 84 | + scale: 1, |
| 85 | + color: "#8b5cf6", |
| 86 | + opacity: 1, |
| 87 | + }, |
| 88 | + { type: "spring", visualDuration: 0.7, bounce: 0.25 } |
| 89 | + ).then(() => { |
| 90 | + status.textContent = "Object animation complete" |
| 91 | + }) |
| 92 | +}) |
| 93 | + |
| 94 | +document.querySelector("#uniform").addEventListener("click", () => { |
| 95 | + uniformActive = !uniformActive |
| 96 | + status.textContent = "Animating shader uniforms" |
| 97 | + |
| 98 | + animate( |
| 99 | + uniforms, |
| 100 | + { |
| 101 | + progress: uniformActive ? 1 : 0, |
| 102 | + tint: uniformActive ? "#fbbf24" : "#22d3ee", |
| 103 | + }, |
| 104 | + { duration: 1, ease: "easeInOut" } |
| 105 | + ).then(() => { |
| 106 | + status.textContent = "Uniform animation complete" |
| 107 | + }) |
| 108 | +}) |
| 109 | + |
| 110 | +document.querySelector("#reset").addEventListener("click", () => { |
| 111 | + meshActive = true |
| 112 | + uniformActive = true |
| 113 | + document.querySelector("#move").click() |
| 114 | + document.querySelector("#uniform").click() |
| 115 | +}) |
| 116 | + |
| 117 | +function resize() { |
| 118 | + const { clientWidth, clientHeight } = canvas |
| 119 | + renderer.setSize(clientWidth, clientHeight, false) |
| 120 | + camera.aspect = clientWidth / clientHeight |
| 121 | + camera.updateProjectionMatrix() |
| 122 | +} |
| 123 | + |
| 124 | +function render() { |
| 125 | + renderer.render(scene, camera) |
| 126 | +} |
| 127 | + |
| 128 | +resize() |
| 129 | +new ResizeObserver(resize).observe(canvas) |
| 130 | +frame.render(render, true) |
| 131 | + |
| 132 | +window.addEventListener("pagehide", () => { |
| 133 | + cancelFrame(render) |
| 134 | + mesh.geometry.dispose() |
| 135 | + uniformMesh.geometry.dispose() |
| 136 | + meshMaterial.dispose() |
| 137 | + shaderMaterial.dispose() |
| 138 | + renderer.dispose() |
| 139 | +}) |
0 commit comments