mini.tsx 5.3 KB

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