-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimulator.html
More file actions
241 lines (209 loc) · 9.38 KB
/
simulator.html
File metadata and controls
241 lines (209 loc) · 9.38 KB
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>3JS Particle Simulator</title>
<style>
body { margin: 0; overflow: hidden; }
canvas { display: block; }
.controls {
position: absolute;
top: 10px;
left: 10px;
z-index: 100;
background: rgba(0, 0, 0, 0.5);
padding: 10px;
border-radius: 8px;
color: white;
}
.controls button, .controls select, .controls input {
display: block;
margin-bottom: 10px;
background: #333;
color: white;
border: none;
padding: 5px;
border-radius: 5px;
}
.controls label {
margin-bottom: 5px;
}
.brush {
position: absolute;
border: 2px solid red;
border-radius: 50%;
pointer-events: none;
z-index: 1000;
}
.hidden {
display: none;
}
</style>
<script src="https://cdn.jsdelivr.net/npm/three@0.128.0/build/three.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/three@0.128.0/examples/js/controls/OrbitControls.js"></script>
<script src="https://cdn.jsdelivr.net/npm/three@0.128.0/examples/js/exporters/GLTFExporter.js"></script>
</head>
<body>
<div class="controls">
<label for="particleCount">Particle Count:</label>
<input type="number" id="particleCount" min="5000" max="5000000" value="5000">
<label for="brushSize">Brush Size:</label>
<input type="range" id="brushSize" min="1" max="100" value="10">
<label for="gravityStrength">Gravity Strength:</label>
<input type="range" id="gravityStrength" min="0" max="10" value="1">
<label for="particleColor">Particle Color:</label>
<input type="color" id="particleColor" value="#ffffff">
<button onclick="toggleBrush()">Toggle Brush</button>
<button onclick="clearParticles()">Clear Particles</button>
<button onclick="exportSimulation()">Export Simulation</button>
</div>
<div class="brush hidden" id="brush"></div>
<script>
let scene, camera, renderer, particles = [], gravityPoints = [];
let brushSize = 10, gravityStrength = 1, particleCount = 5000;
let brushColor = 0xffffff;
const brush = document.getElementById('brush');
let isBrushActive = false;
let brushSphere;
init();
animate();
function init() {
// Scene setup
scene = new THREE.Scene();
camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
camera.position.z = 5;
renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
const controls = new THREE.OrbitControls(camera, renderer.domElement);
window.addEventListener('resize', onWindowResize, false);
document.getElementById('brushSize').addEventListener('input', updateBrushSize);
document.getElementById('gravityStrength').addEventListener('input', updateGravityStrength);
document.getElementById('particleCount').addEventListener('input', updateParticleCount);
document.getElementById('particleColor').addEventListener('input', updateParticleColor);
// Add gravity points
addGravityPoint(new THREE.Vector3(-2, 0, 0));
addGravityPoint(new THREE.Vector3(2, 0, 0));
// Brush sphere
const brushGeometry = new THREE.SphereGeometry(1, 32, 32);
const brushMaterial = new THREE.MeshBasicMaterial({ color: 0xff0000, wireframe: true });
brushSphere = new THREE.Mesh(brushGeometry, brushMaterial);
brushSphere.visible = false;
scene.add(brushSphere);
// Add event listeners for painting particles
document.addEventListener('mousedown', onMouseDown, false);
document.addEventListener('mousemove', onMouseMove, false);
document.addEventListener('mouseup', onMouseUp, false);
}
function animate() {
requestAnimationFrame(animate);
particles.forEach(p => {
gravityPoints.forEach(g => {
const distance = g.position.distanceTo(p.position);
const force = g.position.clone().sub(p.position).normalize().multiplyScalar(gravityStrength / distance);
p.velocity.add(force);
});
p.position.add(p.velocity);
p.rotation.x += 0.01;
p.rotation.y += 0.01;
});
renderer.render(scene, camera);
}
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
}
function clearParticles() {
particles.forEach(p => scene.remove(p));
particles = [];
}
function exportSimulation() {
const exporter = new THREE.GLTFExporter();
exporter.parse(scene, function(result) {
const output = JSON.stringify(result, null, 2);
const blob = new Blob([output], {type: 'application/json'});
const link = document.createElement('a');
link.href = URL.createObjectURL(blob);
link.download = 'simulation.json';
link.click();
});
}
function updateBrushSize(event) {
brushSize = event.target.value;
brush.style.width = brush.style.height = `${brushSize * 2}px`;
brushSphere.scale.set(brushSize / 10, brushSize / 10, brushSize / 10); // Update brush sphere scale
}
function updateGravityStrength(event) {
gravityStrength = event.target.value;
}
function updateParticleCount(event) {
particleCount = event.target.value;
}
function updateParticleColor(event) {
brushColor = parseInt(event.target.value.replace('#', ''), 16);
}
function addGravityPoint(position) {
const geometry = new THREE.SphereGeometry(0.2, 32, 32);
const material = new THREE.MeshBasicMaterial({ color: 0xff0000 });
const gravityPoint = new THREE.Mesh(geometry, material);
gravityPoint.position.copy(position);
scene.add(gravityPoint);
gravityPoints.push(gravityPoint);
}
function onMouseDown(event) {
if (isBrushActive) {
paintParticles(event);
}
}
function onMouseMove(event) {
if (isBrushActive) {
const rect = renderer.domElement.getBoundingClientRect();
const mouse = new THREE.Vector2(
((event.clientX - rect.left) / rect.width) * 2 - 1,
-((event.clientY - rect.top) / rect.height) * 2 + 1
);
const raycaster = new THREE.Raycaster();
raycaster.setFromCamera(mouse, camera);
const intersects = raycaster.intersectObjects([scene]);
const point = intersects.length > 0 ? intersects[0].point : new THREE.Vector3(mouse.x, mouse.y, 0);
brushSphere.position.copy(point);
const screenPosition = point.clone().project(camera);
const screenX = (screenPosition.x + 1) / 2 * window.innerWidth;
const screenY = -(screenPosition.y - 1) / 2 * window.innerHeight;
brush.style.left = `${screenX - brushSize}px`;
brush.style.top = `${screenY - brushSize}px`;
if (event.buttons > 0) {
paintParticles(event);
}
}
}
function onMouseUp(event) {
// Stop painting when mouse is released
}
function paintParticles(event) {
const geometry = new THREE.SphereGeometry(0.1, 32, 32);
const material = new THREE.MeshBasicMaterial({ color: brushColor });
for (let i = 0; i < particleCount; i++) {
const particle = new THREE.Mesh(geometry, material);
const distance = Math.random() * brushSize / 10;
const angle = Math.random() * Math.PI * 2;
particle.position.set(
brushSphere.position.x + distance * Math.cos(angle),
brushSphere.position.y + distance * Math.sin(angle),
brushSphere.position.z + (Math.random() - 0.5) * distance
);
particle.velocity = new THREE.Vector3();
scene.add(particle);
particles.push(particle);
}
}
function toggleBrush() {
isBrushActive = !isBrushActive;
brush.classList.toggle('hidden', !isBrushActive);
brushSphere.visible = isBrushActive;
const button = document.querySelector('button[onclick="toggleBrush()"]');
button.style.backgroundColor = isBrushActive ? '#0a0' : '#333';
}
</script> </body> </html>