feat: initial commit for Paralax creative engineering system
This commit is contained in:
@@ -0,0 +1,170 @@
|
||||
# Interactive 3D & WebGL Engineering Guide
|
||||
|
||||
> **Core Libraries:** Three.js, `@react-three/fiber` (R3F), `@react-three/drei`.
|
||||
> **Philosophy:** Progressive enhancement. 3D must serve the narrative, not burden the hardware.
|
||||
|
||||
---
|
||||
|
||||
## 🎯 1. The Dimensional Decision Matrix
|
||||
|
||||
Before adding 3D dependencies (`three` ~150KB gzip), evaluate where the experience belongs on the dimensional spectrum:
|
||||
|
||||
```
|
||||
[Tier 1: CSS 3D] ------> [Tier 2: 2D Canvas] ------> [Tier 3: Three.js/R3F]
|
||||
perspective: 1200px Fluid particles, Real 3D models,
|
||||
transform: rotateX/Y interactive grid trails, custom GLSL shaders,
|
||||
Sub-pixel GPU compositing image displacement dynamic PBR lighting
|
||||
Cost: ~0KB Cost: ~3KB Cost: ~150KB+
|
||||
```
|
||||
|
||||
| Project Need | Optimal Technology | Why |
|
||||
| :--- | :--- | :--- |
|
||||
| Floating cards tilting to cursor | **CSS 3D Transforms** (`perspective`, `rotateX/Y`) | Native browser compositing, 0KB runtime overhead. |
|
||||
| Background interactive constellation / fluid particles | **HTML5 2D Canvas** | Direct draw calls, lightweight, full 60fps on mobile. |
|
||||
| Interactive vehicle, watch, product inspectable model | **React Three Fiber (R3F) + Drei** | Declarative scene graph, PBR lighting, GLTF compression. |
|
||||
| Cinematic full-screen liquid distortion or image wave | **Three.js Custom ShaderMaterial (GLSL)** | Fragment shader execution directly on GPU cores. |
|
||||
|
||||
---
|
||||
|
||||
## 🚀 2. Production React Three Fiber (R3F) Boilerplate
|
||||
|
||||
### Installation:
|
||||
```bash
|
||||
npm install three @types/three @react-three/fiber @react-three/drei
|
||||
```
|
||||
|
||||
### High-Performance Canvas Component:
|
||||
```tsx
|
||||
'use client';
|
||||
|
||||
import React, { Suspense, useRef } from 'react';
|
||||
import { Canvas, useFrame } from '@react-three/fiber';
|
||||
import { Float, Environment, ContactShadows, useGLTF } from '@react-three/drei';
|
||||
import * as THREE from 'three';
|
||||
|
||||
// 1. Model Component with GLTF pre-loading
|
||||
function ArtifactModel({ pointer }: { pointer: React.MutableRefObject<[number, number]> }) {
|
||||
const meshRef = useRef<THREE.Group>(null);
|
||||
// Replace with real GLTF model or geometry
|
||||
// const { scene } = useGLTF('/models/artifact.glb');
|
||||
|
||||
useFrame((state, delta) => {
|
||||
if (!meshRef.current) return;
|
||||
|
||||
// Organic continuous floating rotation
|
||||
meshRef.current.rotation.y += delta * 0.2;
|
||||
|
||||
// Smooth Lerp toward cursor position
|
||||
const targetX = (pointer.current[0] * Math.PI) / 8;
|
||||
const targetY = (pointer.current[1] * Math.PI) / 8;
|
||||
|
||||
meshRef.current.rotation.x = THREE.MathUtils.lerp(meshRef.current.rotation.x, targetY, 0.05);
|
||||
meshRef.current.rotation.z = THREE.MathUtils.lerp(meshRef.current.rotation.z, -targetX, 0.05);
|
||||
});
|
||||
|
||||
return (
|
||||
<group ref={meshRef}>
|
||||
<Float speed={2} rotationIntensity={0.5} floatIntensity={1}>
|
||||
<mesh castShadow receiveShadow>
|
||||
<torusKnotGeometry args={[1, 0.3, 128, 32]} />
|
||||
<meshStandardMaterial
|
||||
color="#FF3E00"
|
||||
roughness={0.15}
|
||||
metalness={0.85}
|
||||
clearcoat={1}
|
||||
clearcoatRoughness={0.1}
|
||||
/>
|
||||
</mesh>
|
||||
</Float>
|
||||
</group>
|
||||
);
|
||||
}
|
||||
|
||||
// 2. Main Stage with Hardware-Conscious Settings
|
||||
export const Interactive3DStage: React.FC = () => {
|
||||
const pointerPos = useRef<[number, number]>([0, 0]);
|
||||
|
||||
const handlePointerMove = (e: React.PointerEvent) => {
|
||||
// Normalize coordinates from -1 to 1
|
||||
pointerPos.current = [
|
||||
(e.clientX / window.innerWidth) * 2 - 1,
|
||||
-(e.clientY / window.innerHeight) * 2 + 1,
|
||||
];
|
||||
};
|
||||
|
||||
return (
|
||||
<div
|
||||
onPointerMove={handlePointerMove}
|
||||
className="relative h-screen w-full bg-neutral-950"
|
||||
>
|
||||
<Canvas
|
||||
camera={{ position: [0, 0, 5], fov: 45 }}
|
||||
// STRICT PERFORMANCE GUARD: Cap device pixel ratio at 2.0 (never 3x or 4x on Retina)
|
||||
dpr={[1, Math.min(typeof window !== 'undefined' ? window.devicePixelRatio : 2, 2)]}
|
||||
gl={{
|
||||
antialias: true,
|
||||
powerPreference: 'high-performance',
|
||||
alpha: true,
|
||||
}}
|
||||
>
|
||||
<ambientLight intensity={0.4} />
|
||||
<directionalLight position={[10, 10, 5]} intensity={1.5} castShadow />
|
||||
<pointLight position={[-10, -10, -5]} color="#4F46E5" intensity={0.8} />
|
||||
|
||||
<Suspense fallback={null}>
|
||||
<ArtifactModel pointer={pointerPos} />
|
||||
<ContactShadows position={[0, -2, 0]} opacity={0.6} scale={10} blur={2.5} far={4} />
|
||||
<Environment preset="city" />
|
||||
</Suspense>
|
||||
</Canvas>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 🌊 3. Liquid Image Wave Shader (Codrops-Tier Distortion)
|
||||
|
||||
To create organic liquid ripples across an image on hover or scroll, compile a custom `ShaderMaterial`:
|
||||
|
||||
### Vertex Shader:
|
||||
```glsl
|
||||
varying vec2 vUv;
|
||||
uniform float uTime;
|
||||
uniform vec2 uFrequency;
|
||||
|
||||
void main() {
|
||||
vUv = uv;
|
||||
vec3 newPosition = position;
|
||||
newPosition.z += sin(newPosition.x * uFrequency.x + uTime) * 0.1;
|
||||
newPosition.z += cos(newPosition.y * uFrequency.y + uTime) * 0.1;
|
||||
gl_Position = projectionMatrix * modelViewMatrix * vec4(newPosition, 1.0);
|
||||
}
|
||||
```
|
||||
|
||||
### Fragment Shader:
|
||||
```glsl
|
||||
uniform sampler2D uTexture;
|
||||
uniform float uProgress;
|
||||
varying vec2 vUv;
|
||||
|
||||
void main() {
|
||||
vec2 distortedUv = vUv;
|
||||
distortedUv.x += sin(vUv.y * 10.0 + uProgress * 3.14) * 0.03 * uProgress;
|
||||
vec4 color = texture2D(uTexture, distortedUv);
|
||||
gl_FragColor = color;
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 🔋 4. GPU Conservation & Mobile Fallbacks
|
||||
|
||||
1. **Always Cap DPR**: High-DPI smartphones (iPhone Pro Max, Samsung Ultra) have physical pixel ratios of 3.0 or 4.0. Rendering a WebGL scene at 4x resolution requires rendering 16x as many pixels per frame, melting the GPU. Set:
|
||||
```ts
|
||||
dpr={[1, 2]} // Never higher than 2
|
||||
```
|
||||
2. **Dispose Geometries & Textures**: In vanilla Three.js, unmounted meshes must have their geometry, material, and textures explicitly disposed (`geometry.dispose()`, `material.dispose()`). R3F automatically disposes objects on unmount.
|
||||
3. **Turn Off Off-Screen Canvases**: Use `IntersectionObserver` to pause the render loop (`frameloop="never"`) when the 3D canvas scrolls out of the active viewport.
|
||||
4. **Mobile Fallback**: On low-end mobile devices, swap the 3D Canvas with a high-resolution WebP video loop (`<video autoPlay loop muted playsInline poster="...">`).
|
||||
Reference in New Issue
Block a user