-
Notifications
You must be signed in to change notification settings - Fork 0
/
ShaderParkPreview.js
202 lines (176 loc) · 7.13 KB
/
ShaderParkPreview.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
import React, { useEffect, useRef } from 'react';
import * as THREE from 'three';
import { createSculptureWithGeometry, sculptToThreeJSMaterial } from 'shader-park-core';
import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls.js';
import { environments } from '../environments';
export function ShaderParkPreview({ code, geometry, environment = 'plain', environmentParams = {} }) {
const containerRef = useRef();
const sceneRef = useRef();
const rendererRef = useRef();
const cameraRef = useRef();
const controlsRef = useRef();
const sculptureRef = useRef();
const animationFrameRef = useRef();
const timeRef = useRef(0);
const resizeObserverRef = useRef(null);
const environmentRef = useRef(null);
useEffect(() => {
// Setup scene
sceneRef.current = new THREE.Scene();
sceneRef.current.background = new THREE.Color(0x000000);
// Setup camera
cameraRef.current = new THREE.PerspectiveCamera(
75,
containerRef.current.clientWidth / containerRef.current.clientHeight,
0.1,
1000
);
cameraRef.current.position.z = 4;
// Setup renderer
rendererRef.current = new THREE.WebGLRenderer({
antialias: true,
alpha: true,
shadowMap: true,
shadowMapType: THREE.PCFSoftShadowMap
});
rendererRef.current.setPixelRatio(window.devicePixelRatio);
rendererRef.current.setSize(
containerRef.current.clientWidth,
containerRef.current.clientHeight
);
rendererRef.current.setClearColor(new THREE.Color(0x000000), 0);
containerRef.current.appendChild(rendererRef.current.domElement);
// Setup controls
controlsRef.current = new OrbitControls(cameraRef.current, rendererRef.current.domElement);
controlsRef.current.enableDamping = true;
controlsRef.current.dampingFactor = 0.25;
controlsRef.current.zoomSpeed = 0.5;
controlsRef.current.rotateSpeed = 0.5;
// Handle resize
const handleResize = () => {
if (!containerRef.current) return;
const width = containerRef.current.clientWidth;
const height = containerRef.current.clientHeight;
if (width === 0 || height === 0) return;
cameraRef.current.aspect = width / height;
cameraRef.current.updateProjectionMatrix();
rendererRef.current.setSize(width, height, false);
};
// Setup ResizeObserver for container size changes
resizeObserverRef.current = new ResizeObserver((entries) => {
for (const entry of entries) {
if (entry.target === containerRef.current) {
handleResize();
}
}
});
resizeObserverRef.current.observe(containerRef.current);
window.addEventListener('resize', handleResize);
// Initialize environment
if (environment && environments[environment]) {
try {
environmentRef.current = new environments[environment].component(
sceneRef.current,
rendererRef.current
);
} catch (error) {
console.error('Error initializing environment:', error);
}
}
// Animation
const animate = () => {
timeRef.current += 0.01;
animationFrameRef.current = requestAnimationFrame(animate);
if (sculptureRef.current) {
sculptureRef.current.material.uniforms.time.value = timeRef.current;
}
// Update environment
if (environmentRef.current) {
environmentRef.current.update(timeRef.current);
}
controlsRef.current.update();
rendererRef.current.render(sceneRef.current, cameraRef.current);
};
animate();
// Initial sculpture setup
const defaultGeometry = new THREE.SphereGeometry(1, 45, 45);
const defaultCode = 'sphere(1.0)';
try {
const material = sculptToThreeJSMaterial(defaultCode);
sculptureRef.current = new THREE.Mesh(defaultGeometry, material);
sculptureRef.current.position.y = 2;
sculptureRef.current.castShadow = true;
sculptureRef.current.receiveShadow = true;
sceneRef.current.add(sculptureRef.current);
} catch (error) {
console.error('Error creating initial sculpture:', error);
}
// Cleanup
return () => {
resizeObserverRef.current?.disconnect();
window.removeEventListener('resize', handleResize);
cancelAnimationFrame(animationFrameRef.current);
if (rendererRef.current) {
rendererRef.current.dispose();
}
if (sculptureRef.current) {
sceneRef.current.remove(sculptureRef.current);
}
containerRef.current?.removeChild(rendererRef.current.domElement);
if (environmentRef.current) {
environmentRef.current.dispose();
}
};
}, []);
// Update sculpture when code changes
useEffect(() => {
if (!sceneRef.current || !code) return;
try {
if (sculptureRef.current) {
sceneRef.current.remove(sculptureRef.current);
}
const material = sculptToThreeJSMaterial(code);
const defaultGeometry = new THREE.SphereGeometry(1, 45, 45);
sculptureRef.current = new THREE.Mesh(geometry || defaultGeometry, material);
sculptureRef.current.position.y = 2;
sculptureRef.current.castShadow = true;
sculptureRef.current.receiveShadow = true;
sceneRef.current.add(sculptureRef.current);
} catch (error) {
console.error('Error updating sculpture:', error);
}
}, [code, geometry]);
// Update environment when it changes
useEffect(() => {
if (!sceneRef.current || !rendererRef.current) return;
if (environmentRef.current) {
environmentRef.current.dispose();
}
if (environment && environments[environment]) {
try {
environmentRef.current = new environments[environment].component(
sceneRef.current,
rendererRef.current
);
} catch (error) {
console.error('Error updating environment:', error);
}
}
}, [environment]);
// Update environment when params change
useEffect(() => {
if (environmentRef.current && Object.keys(environmentParams).length > 0) {
environmentRef.current.updateParams(environmentParams);
}
}, [environmentParams]);
return (
<div
ref={containerRef}
className="w-full h-full bg-gray-900"
style={{
position: 'relative',
overflow: 'hidden'
}}
/>
);
}