Scene.jsx
This file
Path
examples/demo/src/components/Scene.jsx
244 lines · 5.9 KiB · JavaScript
Last changed by
refactor: remove unused import in example & fix typo in tests (#3161)Noritaka Kobayashibefore adoption73b91d3
History of this fileTake it
The walk stopped after 200 commits: lines older than f85898f point at it.
chore(eslint.config.mjs): update 'eslint-plugin-react-hooks', 'eslint-plugin-react-compiler' config (#3043)Wonsuk Choibefore adoptionf85898f
line 1why, one step back1import { Mesh, PlaneGeometry, Group, Vector3, MathUtils } from 'three'refactor: remove unused import in example & fix typo in tests (#3161)Noritaka Kobayashibefore adoption73b91d3
line 2why, one step back2import { useRef, useState, useLayoutEffect } from 'react'chore(eslint.config.mjs): update 'eslint-plugin-react-hooks', 'eslint-plugin-react-compiler' config (#3043)Wonsuk Choibefore adoptionf85898f
lines 3–149why, one step back3import { createRoot, events, extend, useFrame } from '@react-three/fiber'4import { Plane, useAspect, useTexture } from '@react-three/drei'5import {6 EffectComposer,7 DepthOfField,8 Vignette,9} from '@react-three/postprocessing'10import { MaskFunction } from 'postprocessing'11import Fireflies from './Fireflies'12import bgUrl from '../resources/bg.jpg'13import starsUrl from '../resources/stars.png'14import groundUrl from '../resources/ground.png'15import bearUrl from '../resources/bear.png'16import leaves1Url from '../resources/leaves1.png'17import leaves2Url from '../resources/leaves2.png'18import '../materials/layerMaterial'1920function Experience() {21 const scaleN = useAspect(1600, 1000, 1.05)22 const scaleW = useAspect(2200, 1000, 1.05)23 const textures = useTexture([24 bgUrl,25 starsUrl,26 groundUrl,27 bearUrl,28 leaves1Url,29 leaves2Url,30 ])31 const group = useRef()32 const layersRef = useRef([])33 const [movement] = useState(() => new Vector3())34 const [temp] = useState(() => new Vector3())35 const layers = [36 { texture: textures[0], x: 0, y: 0, z: 0, factor: 0.005, scale: scaleW },37 { texture: textures[1], x: 0, y: 0, z: 10, factor: 0.005, scale: scaleW },38 { texture: textures[2], x: 0, y: 0, z: 20, scale: scaleW },39 {40 texture: textures[3],41 x: 0,42 y: 0,43 z: 30,44 scaleFactor: 0.83,45 scale: scaleN,46 },47 {48 texture: textures[4],49 x: 0,50 y: 0,51 z: 40,52 factor: 0.03,53 scaleFactor: 1,54 wiggle: 0.6,55 scale: scaleW,56 },57 {58 texture: textures[5],59 x: -20,60 y: -20,61 z: 49,62 factor: 0.04,63 scaleFactor: 1.3,64 wiggle: 1,65 scale: scaleW,66 },67 ]6869 useFrame((state, delta) => {70 movement.lerp(temp.set(state.pointer.x, state.pointer.y * 0.2, 0), 0.2)71 group.current.position.x = MathUtils.lerp(72 group.current.position.x,73 state.pointer.x * 20,74 0.05,75 )76 group.current.rotation.x = MathUtils.lerp(77 group.current.rotation.x,78 state.pointer.y / 20,79 0.05,80 )81 group.current.rotation.y = MathUtils.lerp(82 group.current.rotation.y,83 -state.pointer.x / 2,84 0.05,85 )86 layersRef.current[4].uniforms.time.value =87 layersRef.current[5].uniforms.time.value += delta88 }, 1)8990 return (91 <group ref={group}>92 <Fireflies count={20} radius={80} colors={['orange']} />93 {layers.map(94 (95 {96 scale,97 texture,98 ref,99 factor = 0,100 scaleFactor = 1,101 wiggle = 0,102 x,103 y,104 z,105 },106 i,107 ) => (108 <Plane109 scale={scale}110 args={[1, 1, wiggle ? 10 : 1, wiggle ? 10 : 1]}111 position={[x, y, z]}112 key={i}113 ref={ref}114 >115 <layerMaterial116 movement={movement}117 textr={texture}118 factor={factor}119 ref={(el) => (layersRef.current[i] = el)}120 wiggle={wiggle}121 scale={scaleFactor}122 />123 </Plane>124 ),125 )}126 </group>127 )128}129130function Effects() {131 const ref = useRef()132 useLayoutEffect(() => {133 const maskMaterial = ref.current.maskPass.getFullscreenMaterial()134 maskMaterial.maskFunction = MaskFunction.MULTIPLY_RGB_SET_ALPHA135 })136 return (137 <EffectComposer disableNormalPass multisampling={0}>138 <DepthOfField139 ref={ref}140 target={[0, 0, 30]}141 bokehScale={8}142 focalLength={0.1}143 width={1024}144 />145 <Vignette />146 </EffectComposer>147 )148}149fix: handle hardware acceleration disabled scenario in demo site (#3082)Rahul Singhbefore adoption4d3a017
lines 150–177why, one step back150function FallbackScene() {151 return (152 <div153 style={{154 position: 'absolute',155 top: 0,156 left: 0,157 width: '100%',158 height: '100%',159 display: 'flex',160 alignItems: 'center',161 justifyContent: 'center',162 background: '#010101',163 }}164 >165 <img166 src="/ogimage.jpg"167 alt="Zustand Bear"168 style={{169 width: '100%',170 height: '100%',171 objectFit: 'cover',172 }}173 />174 </div>175 )176}177chore(eslint.config.mjs): update 'eslint-plugin-react-hooks', 'eslint-plugin-react-compiler' config (#3043)Wonsuk Choibefore adoptionf85898f
line 178why, one step back178export default function Scene() {fix: handle hardware acceleration disabled scenario in demo site (#3082)Rahul Singhbefore adoption4d3a017
lines 179–184why, one step back179 const [error, setError] = useState(null)180181 if (error) {182 return <FallbackScene />183 }184chore(eslint.config.mjs): update 'eslint-plugin-react-hooks', 'eslint-plugin-react-compiler' config (#3043)Wonsuk Choibefore adoptionf85898f
line 185why, one step back185 return (fix: handle hardware acceleration disabled scenario in demo site (#3082)Rahul Singhbefore adoption4d3a017
line 186why, one step back186 <Canvas onError={setError}>chore(eslint.config.mjs): update 'eslint-plugin-react-hooks', 'eslint-plugin-react-compiler' config (#3043)Wonsuk Choibefore adoptionf85898f
lines 187–192why, one step back187 <Experience />188 <Effects />189 </Canvas>190 )191}192fix: handle hardware acceleration disabled scenario in demo site (#3082)Rahul Singhbefore adoption4d3a017
line 193why, one step back193function Canvas({ children, onError }) {chore(eslint.config.mjs): update 'eslint-plugin-react-hooks', 'eslint-plugin-react-compiler' config (#3043)Wonsuk Choibefore adoptionf85898f
lines 194–197why, one step back194 extend({ Mesh, PlaneGeometry, Group })195 const canvas = useRef(null)196 const root = useRef(null)197 useLayoutEffect(() => {fix: handle hardware acceleration disabled scenario in demo site (#3082)Rahul Singhbefore adoption4d3a017
lines 198–228why, one step back198 try {199 if (!root.current) {200 root.current = createRoot(canvas.current).configure({201 events,202 orthographic: true,203 gl: { antialias: false },204 camera: { zoom: 5, position: [0, 0, 200], far: 300, near: 50 },205 onCreated: (state) => {206 state.events.connect(document.getElementById('root'))207 state.setEvents({208 compute: (event, state) => {209 state.pointer.set(210 (event.clientX / state.size.width) * 2 - 1,211 -(event.clientY / state.size.height) * 2 + 1,212 )213 state.raycaster.setFromCamera(state.pointer, state.camera)214 },215 })216 },217 })218 }219 const resize = () =>220 root.current.configure({221 width: window.innerWidth,222 height: window.innerHeight,223 })224 window.addEventListener('resize', resize)225 root.current.render(children)226 return () => window.removeEventListener('resize', resize)227 } catch (e) {228 onError?.(e)chore(eslint.config.mjs): update 'eslint-plugin-react-hooks', 'eslint-plugin-react-compiler' config (#3043)Wonsuk Choibefore adoptionf85898f
line 229why, one step back229 }fix: handle hardware acceleration disabled scenario in demo site (#3082)Rahul Singhbefore adoption4d3a017
line 230why, one step back230 }, [children, onError])chore(eslint.config.mjs): update 'eslint-plugin-react-hooks', 'eslint-plugin-react-compiler' config (#3043)Wonsuk Choibefore adoptionf85898f
lines 231–244why, one step back231232 return (233 <canvas234 ref={canvas}235 style={{236 position: 'relative',237 width: '100%',238 height: '100%',239 overflow: 'hidden',240 display: 'block',241 }}242 />243 )244}