-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimulator12.html
More file actions
416 lines (362 loc) · 18.5 KB
/
simulator12.html
File metadata and controls
416 lines (362 loc) · 18.5 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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Articren Wave</title>
<style>
body { margin: 0; overflow: hidden; background-color: transparent;
background-image: url('m.png');
background-size: cover; /* Ensures the image covers the entire background */
background-repeat: no-repeat; /* Prevents the image from repeating */
background-position: center; /* Centers the image */
}
background-urp
canvas { display: block; background: transparent; }
.controls {
position: absolute;
top: 10px;
left: 10px;
z-index: 100;
background: rgba(255, 20, 147, 0.3); /* Neon pink transparent background */
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; }
h1 { color: white; font-weight: bold }
</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>
<script src="https://cdn.jsdelivr.net/npm/three@0.128.0/examples/js/loaders/GLTFLoader.js"></script>
</head>
<body>
<div class="controls" id="toolbar">
<img src="https://static.wixstatic.com/media/2451db_5b0addcd6bad4cbdaa9cdb8e3e88669a~mv2.png" width="175px" alt="Logo">
<br>
<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>
<button id="inactGravityDisturbanceButton" onclick="toggleTool('disturbance')">Inact Gravity Disturbance</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="clearGravityPoints()">Clear Gravity Points</button>
<button onclick="resetScene()">Reset Scene</button>
<button onclick="exportGLB()">Export Wave GLB</button>
<button onclick="exportPNG()">Export Wave PNG</button>
<button id="loadGLB">Load Wave GLB (InDev!)</button>
</div>
<script>
let scene, camera, renderer, particles = [], gravityPoints = [], disturbancePoint = null;
let particleCount = 5000;
let brushColor = 0xffffff;
let globalGravity = 9.81;
let currentTool = null;
let controls;
let touchInProgress = false;
const clippingBounds = { x: 500000, y: 500000, z: 500000 }; // Clipping bounds for the particles
const gravityPointGeometry = new THREE.SphereGeometry(0.2, 32, 32);
const gravityPointMaterial = new THREE.MeshBasicMaterial({ color: 0xff0000 });
const particleGeometry = new THREE.SphereGeometry(0.1, 32, 32);
const particleMaterial = new THREE.MeshBasicMaterial({ color: 0xffffff });
init();
animate();
function init() {
// Scene setup
scene = new THREE.Scene();
camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.01, 1000000);
camera.position.set(0, 0, 5); // Initial position at the origin
renderer = new THREE.WebGLRenderer({ alpha: true }); // Enable transparency
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);
document.addEventListener('touchstart', onTouchStart, false);
document.addEventListener('touchmove', onTouchMove, false);
document.addEventListener('touchend', onTouchEnd, false);
document.getElementById('loadGLB').addEventListener('click', loadFromGLB);
}
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 => {
if (g.mesh && p.position) {
const distance = g.mesh.position.distanceTo(p.position);
const force = g.mesh.position.clone().sub(p.position).normalize().multiplyScalar(g.intensity / Math.pow(distance, 2));
p.velocity.add(force);
}
});
if (disturbancePoint && disturbancePoint.mesh && p.position) {
const distance = disturbancePoint.mesh.position.distanceTo(p.position);
const force = disturbancePoint.mesh.position.clone().sub(p.position).normalize().multiplyScalar(disturbancePoint.intensity / Math.pow(distance, 2));
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 clearGravityPoints() {
gravityPoints.forEach(g => scene.remove(g.mesh));
gravityPoints = [];
}
function resetScene() {
clearParticles();
clearGravityPoints();
if (disturbancePoint) {
scene.remove(disturbancePoint.mesh);
disturbancePoint = null;
}
}
function exportGLB() {
const exporter = new THREE.GLTFExporter();
exporter.parse(scene, function(result) {
const blob = new Blob([result], {type: 'model/gltf-binary'});
const link = document.createElement('a');
link.href = URL.createObjectURL(blob);
link.download = 'simulation.glb';
link.click();
}, {binary: true});
}
function exportPNG() {
renderer.render(scene, camera);
renderer.domElement.toBlob(function(blob) {
const link = document.createElement('a');
link.href = URL.createObjectURL(blob);
link.download = 'simulation.png';
link.click();
});
}
function loadFromGLB() {
const input = document.createElement('input');
input.type = 'file';
input.accept = '.glb';
input.onchange = async (event) => {
const file = event.target.files[0];
const reader = new FileReader();
reader.onload = async (e) => {
try {
const content = e.target.result;
console.log("Loaded GLB content");
const loader = new THREE.GLTFLoader();
loader.parse(content, '', (gltf) => {
resetScene();
scene.add(gltf.scene);
gltf.scene.traverse((child) => {
if (child.isMesh) {
if (child.material.color.getHex() === 0xff0000) { // Assuming red color is for gravity points
gravityPoints.push({
mesh: child,
intensity: parseFloat(document.getElementById('gravityIntensity').value),
velocity: new THREE.Vector3()
});
} else {
particles.push(child);
}
}
});
});
} catch (error) {
console.error("Error loading the GLB:", error);
}
};
reader.onerror = error => {
console.error("Error reading the file:", error);
};
reader.readAsArrayBuffer(file);
};
input.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 gravityPoint = new THREE.Mesh(gravityPointGeometry, gravityPointMaterial);
gravityPoint.position.copy(position);
scene.add(gravityPoint);
gravityPoints.push({ mesh: gravityPoint, intensity: intensity, velocity: velocity });
}
function addParticleSpawnPoint(position, count, color) {
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(particleGeometry, 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) {
if (currentTool && !isInToolbar(event.clientX, event.clientY)) {
addPoint(event.clientX, event.clientY, event.pressure || 1);
}
}
function onTouchStart(event) {
if (currentTool && !isInToolbar(event.touches[0].clientX, event.touches[0].clientY)) {
touchInProgress = true;
if (currentTool === 'disturbance') {
moveDisturbancePoint(event.touches[0].clientX, event.touches[0].clientY, event.touches[0].pressure || 1);
} else {
addPoint(event.touches[0].clientX, event.touches[0].clientY, event.touches[0].pressure || 1);
}
}
}
function onTouchMove(event) {
if (currentTool === 'disturbance' && touchInProgress && !isInToolbar(event.touches[0].clientX, event.touches[0].clientY)) {
moveDisturbancePoint(event.touches[0].clientX, event.touches[0].clientY, event.touches[0].pressure || 1);
}
}
function onTouchEnd(event) {
touchInProgress = false;
if (currentTool === 'disturbance') {
removeDisturbancePoint();
}
}
function isInToolbar(clientX, clientY) {
const toolbar = document.getElementById('toolbar').getBoundingClientRect();
return clientX >= toolbar.left && clientX <= toolbar.right && clientY >= toolbar.top && clientY <= toolbar.bottom;
}
function addPoint(clientX, clientY, pressure) {
const rect = renderer.domElement.getBoundingClientRect();
const mouse = new THREE.Vector2(
((clientX - rect.left) / rect.width) * 2 - 1,
-((clientY - rect.top) / rect.height) * 2 + 1
);
const raycaster = new THREE.Raycaster();
raycaster.setFromCamera(mouse, camera);
const intersects = raycaster.intersectObjects(scene.children, true);
const point = intersects.length > 0 ? intersects[0].point : null;
const distance = (pressure - 0.5) * 10; // Scale based on pressure, can be adjusted
const direction = new THREE.Vector3(mouse.x, mouse.y, 0.5).unproject(camera).sub(camera.position).normalize();
const targetPoint = camera.position.clone().add(direction.multiplyScalar(distance));
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(targetPoint, intensity, velocity);
} else if (currentTool === 'particle') {
addParticleSpawnPoint(targetPoint, particleCount, brushColor);
}
}
function moveDisturbancePoint(clientX, clientY, pressure) {
const rect = renderer.domElement.getBoundingClientRect();
const mouse = new THREE.Vector2(
((clientX - rect.left) / rect.width) * 2 - 1,
-((clientY - rect.top) / rect.height) * 2 + 1
);
const raycaster = new THREE.Raycaster();
raycaster.setFromCamera(mouse, camera);
const intersects = raycaster.intersectObjects(scene.children, true);
const point = intersects.length > 0 ? intersects[0].point : null;
const distance = (pressure - 0.5) * 10; // Scale based on pressure, can be adjusted
const direction = new THREE.Vector3(mouse.x, mouse.y, 0.5).unproject(camera).sub(camera.position).normalize();
const targetPoint = camera.position.clone().add(direction.multiplyScalar(distance));
if (!disturbancePoint) {
const geometry = new THREE.SphereGeometry(0.2, 32, 32);
const material = new THREE.MeshBasicMaterial({ color: 0x00ff00 });
const mesh = new THREE.Mesh(geometry, material);
mesh.position.copy(targetPoint);
scene.add(mesh);
disturbancePoint = { mesh, intensity: parseFloat(document.getElementById('gravityIntensity').value) };
} else {
disturbancePoint.mesh.position.copy(targetPoint);
}
}
function removeDisturbancePoint() {
if (disturbancePoint) {
scene.remove(disturbancePoint.mesh);
disturbancePoint = null;
}
}
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';
document.getElementById('inactGravityDisturbanceButton').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';
document.getElementById('inactGravityDisturbanceButton').style.backgroundColor = tool === 'disturbance' ? '#0a0' : '#333';
}
}
function resetGravity() {
document.getElementById('globalGravity').value = 9.81;
globalGravity = 9.81;
}
</script>
</body>
</html>