-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimulator3.html
More file actions
245 lines (214 loc) · 10.2 KB
/
simulator3.html
File metadata and controls
245 lines (214 loc) · 10.2 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
242
243
244
245
<!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;
}
</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="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 id="addGravityPointButton" onclick="toggleTool('gravity')">Add Gravity Point</button>
<button id="addParticleSpawnButton" onclick="toggleTool('particle')">Add Particle Spawn Point</button>
<label for="gravityIntensity">Gravity Intensity:</label>
<input type="range" id="gravityIntensity" min="0" max="10" value="1">
<label for="gravityVelocity">Velocity (m/s):</label>
<input type="range" id="gravityVelocity" min="0" max="50" value="0">
<label for="globalGravity">Global Gravity:</label>
<input type="number" id="globalGravity" value="9.81"> <!-- Default gravity value on Earth in m/s^2 -->
<button onclick="resetGravity()">Reset Gravity to Earth</button>
<button onclick="clearParticles()">Clear Particles</button>
<button onclick="exportSimulation()">Export Simulation</button>
</div>
<script>
let scene, camera, renderer, particles = [], gravityPoints = [];
let particleCount = 5000;
let brushColor = 0xffffff;
let globalGravity = 9.81;
let currentTool = null;
let controls;
const clippingBounds = { x: 50, y: 50, z: 50 }; // Clipping bounds for the particles
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);
controls = new THREE.OrbitControls(camera, renderer.domElement);
window.addEventListener('resize', onWindowResize, false);
document.getElementById('gravityStrength').addEventListener('input', updateGravityStrength);
document.getElementById('particleCount').addEventListener('input', updateParticleCount);
document.getElementById('particleColor').addEventListener('input', updateParticleColor);
document.getElementById('globalGravity').addEventListener('input', updateGlobalGravity);
// Add event listeners for adding gravity points and particle spawn points
document.addEventListener('mousedown', onMouseDown, false);
}
function animate() {
requestAnimationFrame(animate);
particles.forEach(p => {
// Apply global gravity
p.velocity.y -= globalGravity * 0.01; // Adjust the multiplier as necessary for simulation scaling
gravityPoints.forEach(g => {
const distance = g.mesh.position.distanceTo(p.position);
const force = g.mesh.position.clone().sub(p.position).normalize().multiplyScalar(g.intensity / distance);
p.velocity.add(force);
});
p.position.add(p.velocity);
p.rotation.x += 0.01;
p.rotation.y += 0.01;
// Remove particles that exit the clipping bounds
if (Math.abs(p.position.x) > clippingBounds.x ||
Math.abs(p.position.y) > clippingBounds.y ||
Math.abs(p.position.z) > clippingBounds.z) {
scene.remove(p);
}
});
particles = particles.filter(p => scene.children.includes(p)); // Keep only particles that are still in the scene
gravityPoints.forEach(g => {
g.mesh.position.add(g.velocity);
});
controls.update();
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 updateGravityStrength(event) {
gravityStrength = event.target.value;
}
function updateParticleCount(event) {
particleCount = event.target.value;
}
function updateParticleColor(event) {
brushColor = parseInt(event.target.value.replace('#', ''), 16);
}
function updateGlobalGravity(event) {
globalGravity = parseFloat(event.target.value);
}
function addGravityPoint(position, intensity, velocity) {
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({ mesh: gravityPoint, intensity: intensity, velocity: velocity });
}
function addParticleSpawnPoint(position, count, color) {
const geometry = new THREE.SphereGeometry(0.1, 32, 32);
const material = new THREE.MeshBasicMaterial({ color: color });
for (let i = 0; i < count; i++) {
const phi = Math.acos(2 * Math.random() - 1);
const theta = 2 * Math.PI * Math.random();
const distance = Math.random() * 0.2;
const particle = new THREE.Mesh(geometry, material);
particle.position.set(
position.x + distance * Math.sin(phi) * Math.cos(theta),
position.y + distance * Math.sin(phi) * Math.sin(theta),
position.z + distance * Math.cos(phi)
);
particle.velocity = new THREE.Vector3();
scene.add(particle);
particles.push(particle);
}
}
function onMouseDown(event) {
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.children);
if (intersects.length > 0) {
const point = intersects[0].point;
if (currentTool === 'gravity') {
const intensity = parseFloat(document.getElementById('gravityIntensity').value);
const velocityValue = parseFloat(document.getElementById('gravityVelocity').value);
const velocity = new THREE.Vector3(
(Math.random() - 0.5) * velocityValue,
(Math.random() - 0.5) * velocityValue,
(Math.random() - 0.5) * velocityValue
);
addGravityPoint(point, intensity, velocity);
} else if (currentTool === 'particle') {
addParticleSpawnPoint(point, particleCount, brushColor);
}
}
}
function toggleTool(tool) {
if (currentTool === tool)
{
// If the same tool is toggled again, turn it off
currentTool = null;
document.getElementById('addGravityPointButton').style.backgroundColor = '#333';
document.getElementById('addParticleSpawnButton').style.backgroundColor = '#333';
} else {
// Toggle the new tool on and turn off the previous one
currentTool = tool;
document.getElementById('addGravityPointButton').style.backgroundColor = tool === 'gravity' ? '#0a0' : '#333';
document.getElementById('addParticleSpawnButton').style.backgroundColor = tool === 'particle' ? '#0a0' : '#333';
}
}
function resetGravity() {
document.getElementById('globalGravity').value = 9.81;
globalGravity = 9.81;
}
</script>
</body>
</html>