mini.tsx 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /* eslint-disable */
  2. import * as THREE from "three";
  3. import { useLayoutEffect, useRef, useState, useEffect } from "react";
  4. import { Canvas, applyProps, useFrame, useThree } from "@react-three/fiber";
  5. import {
  6. PerformanceMonitor,
  7. AccumulativeShadows,
  8. RandomizedLight,
  9. Environment,
  10. Lightformer,
  11. Float,
  12. useGLTF,
  13. } from "@react-three/drei";
  14. import { useTheme } from "next-themes";
  15. export function MiniModel() {
  16. const [degraded, degrade] = useState(false);
  17. const { resolvedTheme } = useTheme();
  18. const [mounted, setMounted] = useState(false);
  19. useEffect(() => {
  20. setMounted(true);
  21. }, []);
  22. if (!mounted) return null;
  23. return (
  24. <Canvas
  25. shadows
  26. camera={{ position: [5, 0, 15], fov: 30 }}
  27. onCreated={({ scene }) => {
  28. scene.environmentIntensity = 2;
  29. }}
  30. >
  31. <Background theme={resolvedTheme} />
  32. <spotLight
  33. position={[0, 15, 0]}
  34. angle={0.3}
  35. penumbra={1}
  36. castShadow
  37. intensity={2}
  38. shadow-bias={-0.0001}
  39. />
  40. <ambientLight intensity={0.5} />
  41. <Mini
  42. scale={2}
  43. position={[-0.14, -0.22, 1]}
  44. rotation={[0, -Math.PI / 4, 0]}
  45. />
  46. <AccumulativeShadows
  47. position={[0, -1.16, 0]}
  48. frames={50}
  49. alphaTest={0.75}
  50. scale={10}
  51. >
  52. <RandomizedLight
  53. amount={8}
  54. radius={10}
  55. ambient={0.5}
  56. position={[1, 5, -1]}
  57. />
  58. </AccumulativeShadows>
  59. <PerformanceMonitor onDecline={() => degrade(true)} />
  60. <Environment frames={degraded ? 1 : Infinity} resolution={128} blur={1}>
  61. <Lightformers />
  62. </Environment>
  63. <CameraRig />
  64. </Canvas>
  65. );
  66. }
  67. function Background({ theme }: { theme: string | undefined }) {
  68. const { scene } = useThree();
  69. useEffect(() => {
  70. scene.background = new THREE.Color(theme === "dark" ? "#0f1217" : "white");
  71. }, [theme, scene]);
  72. return null;
  73. }
  74. function Mini(props: any) {
  75. const { scene, nodes, materials } = useGLTF("/mini.glb") as any;
  76. useLayoutEffect(() => {
  77. Object.values(nodes).forEach((node) => {
  78. if (node instanceof THREE.Mesh) {
  79. node.receiveShadow = true;
  80. node.castShadow = true;
  81. }
  82. });
  83. if (materials["CAR_BODY_PAINT"]) {
  84. applyProps(materials["CAR_BODY_PAINT"], {
  85. color: "black",
  86. roughness: 0.45,
  87. metalness: 0.8,
  88. envMapIntensity: 2,
  89. });
  90. }
  91. if (materials["MATT"]) {
  92. applyProps(materials["MATT"], {
  93. color: "black",
  94. roughness: 0.45,
  95. metalness: 0.8,
  96. envMapIntensity: 2,
  97. });
  98. }
  99. if (materials["WINDOW_GLASS"]) {
  100. applyProps(materials["WINDOW_GLASS"], {
  101. color: "black",
  102. roughness: 0,
  103. clearcoat: 0.1,
  104. });
  105. }
  106. if (materials["LIGHT_GLASS"]) {
  107. applyProps(materials["LIGHT_GLASS"], {
  108. color: "black",
  109. roughness: 0,
  110. clearcoat: 0.1,
  111. });
  112. }
  113. if (materials["LIGHTS_POD"]) {
  114. applyProps(materials["LIGHTS_POD"], {
  115. roughness: 0,
  116. clearcoat: 0.1,
  117. });
  118. }
  119. if (materials["SHINY_METAL"]) {
  120. applyProps(materials["SHINY_METAL"], {
  121. color: "black",
  122. envMapIntensity: 4,
  123. roughness: 0.5,
  124. metalness: 1,
  125. });
  126. }
  127. if (materials["CALIPER"]) {
  128. applyProps(materials["CALIPER"], {
  129. color: "red",
  130. envMapIntensity: 4,
  131. roughness: 0.5,
  132. metalness: 1,
  133. });
  134. }
  135. }, [nodes, materials]);
  136. return <primitive object={scene} {...props} />;
  137. }
  138. function CameraRig({ v = new THREE.Vector3() }) {
  139. return useFrame((state) => {
  140. const t = state.clock.elapsedTime;
  141. state.camera.position.lerp(
  142. v.set(Math.sin(t / 2) * 1.2, 0, 12 + Math.cos(t / 5) / 2),
  143. 0.05
  144. );
  145. state.camera.lookAt(0, 0, 0);
  146. });
  147. }
  148. function Lightformers({ positions = [2, 0, 2, 0, 2, 0, 2, 0] }) {
  149. const group = useRef<THREE.Group>(null);
  150. useFrame(
  151. (state, delta) =>
  152. (group.current!.position.z += delta * 10) > 20 &&
  153. (group.current!.position.z = -60)
  154. );
  155. return (
  156. <>
  157. <Lightformer
  158. intensity={0.5}
  159. rotation-x={Math.PI / 2}
  160. position={[0, 5, -9]}
  161. scale={[10, 10, 1]}
  162. />
  163. <group rotation={[0, 0.5, 0]}>
  164. <group ref={group}>
  165. {positions.map((x, i) => (
  166. <Lightformer
  167. key={i}
  168. form="circle"
  169. intensity={2}
  170. rotation={[Math.PI / 2, 0, 0]}
  171. position={[x, 4, i * 4]}
  172. scale={[3, 1, 1]}
  173. />
  174. ))}
  175. </group>
  176. </group>
  177. <Lightformer
  178. intensity={4}
  179. rotation-y={Math.PI / 2}
  180. position={[-5, 1, -1]}
  181. scale={[20, 0.1, 1]}
  182. />
  183. <Lightformer
  184. rotation-y={Math.PI / 2}
  185. position={[-5, -1, -1]}
  186. scale={[20, 0.5, 1]}
  187. />
  188. <Lightformer
  189. rotation-y={-Math.PI / 2}
  190. position={[10, 1, 0]}
  191. scale={[20, 1, 1]}
  192. />
  193. <Float speed={5} floatIntensity={2} rotationIntensity={2}>
  194. <Lightformer
  195. form="ring"
  196. color="blue"
  197. intensity={1}
  198. scale={10}
  199. position={[-15, 4, -18]}
  200. target={[0, 0, 0]}
  201. />
  202. </Float>
  203. {/* White environment background for reflections */}
  204. <mesh scale={100}>
  205. <sphereGeometry args={[1, 64, 64]} />
  206. <meshBasicMaterial color="white" side={THREE.BackSide} />
  207. </mesh>
  208. </>
  209. );
  210. }