-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbetaanimalsim.html
478 lines (406 loc) · 16.3 KB
/
betaanimalsim.html
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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Advanced 3D Animal Simulation Environment</title>
<!-- Load Three.js -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"></script>
<!-- Load OrbitControls -->
<script src="https://cdn.jsdelivr.net/npm/[email protected]/examples/js/controls/OrbitControls.js"></script>
<!-- Load Simplex Noise -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/simplex-noise/2.4.0/simplex-noise.min.js"></script>
<style>
body { font-family: Arial, sans-serif; margin: 0; background-color: #f0f2f5; overflow: hidden; }
#canvasContainer { width: 100vw; height: 100vh; }
#info, #controls {
position: absolute; top: 10px; color: white; background: rgba(0,0,0,0.7); padding: 10px; border-radius: 5px; z-index: 1;
}
#info { left: 10px; }
#controls { right: 10px; }
#controls label { display: block; margin-top: 10px; }
#controls input[type="range"], #controls select { width: 150px; }
#controls button { margin-top: 10px; padding: 5px 10px; }
</style>
</head>
<body>
<div id="canvasContainer"></div>
<div id="info">
<strong>Advanced Animal Simulation</strong><br>
<span id="timeDisplay">Time: Morning</span><br>
Animal Speed: <span id="speedDisplay">2.0</span><br>
Terrain Complexity: <span id="terrainDisplay">1.0</span><br>
Weather: <span id="weatherDisplay">Clear</span>
</div>
<div id="controls">
<label>Animal Speed:</label>
<input type="range" id="speedControl" min="0.5" max="5" step="0.1" value="2"><br>
<label>Terrain Complexity:</label>
<input type="range" id="terrainControl" min="0.1" max="1.5" step="0.1" value="1"><br>
<label>Day/Night Cycle Speed:</label>
<input type="range" id="timeControl" min="0.1" max="5" step="0.1" value="1"><br>
<label>Weather:</label>
<select id="weatherControl">
<option value="clear">Clear</option>
<option value="rain">Rain</option>
<option value="snow">Snow</option>
</select><br>
<button id="addAnimalBtn">Add Animal</button>
<button id="removeAnimalBtn">Remove Animal</button><br>
<button id="addFoodBtn">Add Food Source</button>
</div>
<script>
let scene, camera, renderer, controls, clock, env, animals = [], particles = [];
let animalSpeed = 2, terrainComplexity = 1, dayNightSpeed = 1, timeOfDay = 0;
let weather = 'clear';
let directionalLight, ambientLight, hemisphereLight;
let foodSources = [];
class Environment {
constructor(size) {
this.size = size;
this.simplex = new SimplexNoise();
}
getHeight(x, y) {
return this.simplex.noise2D(x * terrainComplexity / 10, y * terrainComplexity / 10) * 5;
}
isWater(x, y) {
return Math.abs(this.simplex.noise2D(x / 5, y / 5)) < 0.1;
}
addWaterAndVegetation() {
const waterMaterial = new THREE.MeshPhongMaterial({ color: 0x1e90ff, transparent: true, opacity: 0.7 });
const vegetationMaterial = new THREE.MeshLambertMaterial({ color: 0x228B22 });
for (let i = 0; i < this.size; i += 2) {
for (let j = 0; j < this.size; j += 2) {
const x = i - this.size / 2;
const z = j - this.size / 2;
if (this.isWater(i, j)) {
const water = new THREE.Mesh(new THREE.CircleGeometry(1, 16), waterMaterial);
water.rotation.x = -Math.PI / 2;
water.position.set(x, this.getHeight(x, z) + 0.05, z);
scene.add(water);
} else if (Math.random() < 0.1) {
const plant = new THREE.Mesh(new THREE.ConeGeometry(0.3, 1, 8), vegetationMaterial);
plant.position.set(x, this.getHeight(x, z) + 0.5, z);
plant.castShadow = true;
scene.add(plant);
}
}
}
}
}
class Animal {
constructor(environment) {
this.environment = environment;
this.position = [
Math.random() * environment.size,
Math.random() * environment.size
];
this.energy = 100;
this.rotation = Math.random() * Math.PI * 2;
this.mesh = new THREE.Group();
this.createMesh();
}
createMesh() {
const bodyMaterial = new THREE.MeshPhongMaterial({ color: this.getRandomColor() });
const body = new THREE.Mesh(new THREE.SphereGeometry(0.5, 16, 16), bodyMaterial);
const head = new THREE.Mesh(new THREE.SphereGeometry(0.3, 16, 16), bodyMaterial);
head.position.set(0.5, 0.2, 0);
this.mesh.add(body);
this.mesh.add(head);
// Adding legs with simple rotation animation
this.legs = [];
for (let i = 0; i < 4; i++) {
const leg = new THREE.Mesh(new THREE.CylinderGeometry(0.1, 0.1, 0.5, 8), bodyMaterial);
leg.position.set(i < 2 ? -0.3 : 0.3, -0.5, i % 2 === 0 ? -0.3 : 0.3);
this.legs.push(leg);
this.mesh.add(leg);
}
this.mesh.castShadow = true;
scene.add(this.mesh);
this.updateMeshPosition();
}
getRandomColor() {
const colors = [0xff5555, 0x55ff55, 0x5555ff, 0xffff55, 0xff55ff, 0x55ffff];
return colors[Math.floor(Math.random() * colors.length)];
}
updateMeshPosition() {
const x = this.position[0] - this.environment.size / 2;
const z = this.position[1] - this.environment.size / 2;
const height = this.environment.getHeight(x, z);
this.mesh.position.set(x, height + 0.5, z);
this.mesh.rotation.y = this.rotation;
const time = clock.getElapsedTime();
this.legs.forEach((leg, index) => {
leg.rotation.z = Math.sin(time * 5 + index) * 0.5;
});
}
move(delta) {
// Adjust speed based on time of day
const speedFactor = (Math.sin(timeOfDay * 0.1) + 1) / 2 + 0.5; // Animals slow down at night
let angle = this.rotation;
// AI Behavior: Seek nearest food source
const nearestFood = this.getNearestFoodSource();
if (nearestFood) {
const dx = nearestFood.x - (this.position[0] - this.environment.size / 2);
const dz = nearestFood.z - (this.position[1] - this.environment.size / 2);
angle = Math.atan2(dz, dx);
} else {
// Randomly change direction
this.rotation += (Math.random() - 0.5) * delta;
angle = this.rotation;
}
this.position[0] += Math.cos(angle) * animalSpeed * speedFactor * delta;
this.position[1] += Math.sin(angle) * animalSpeed * speedFactor * delta;
// Wrap around environment
this.position = this.position.map(pos => {
if (pos < 0) pos += this.environment.size;
if (pos >= this.environment.size) pos -= this.environment.size;
return pos;
});
// Update position
this.updateMeshPosition();
// Check for food consumption
if (nearestFood) {
const distance = this.getDistanceTo(nearestFood.x, nearestFood.z);
if (distance < 1) {
// Consume food
scene.remove(nearestFood.mesh);
const index = foodSources.indexOf(nearestFood);
if (index > -1) {
foodSources.splice(index, 1);
}
this.energy = 100; // Replenish energy
}
}
}
getNearestFoodSource() {
if (foodSources.length === 0) return null;
let nearestFood = null;
let minDistance = Infinity;
const x = this.position[0] - this.environment.size / 2;
const z = this.position[1] - this.environment.size / 2;
foodSources.forEach(food => {
const dx = food.x - x;
const dz = food.z - z;
const distance = Math.sqrt(dx * dx + dz * dz);
if (distance < minDistance) {
minDistance = distance;
nearestFood = food;
}
});
return nearestFood;
}
getDistanceTo(x, z) {
const dx = x - (this.position[0] - this.environment.size / 2);
const dz = z - (this.position[1] - this.environment.size / 2);
return Math.sqrt(dx * dx + dz * dz);
}
dispose() {
// Remove animal from scene and free memory
scene.remove(this.mesh);
this.mesh.traverse((object) => {
if (object.geometry) object.geometry.dispose();
if (object.material) object.material.dispose();
});
}
}
function initScene() {
scene = new THREE.Scene();
scene.background = new THREE.Color(0x87ceeb);
scene.fog = new THREE.FogExp2(0x87ceeb, 0.005);
camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
camera.position.set(0, 20, 30);
renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.shadowMap.enabled = true;
renderer.setSize(window.innerWidth, window.innerHeight);
document.getElementById("canvasContainer").appendChild(renderer.domElement);
controls = new THREE.OrbitControls(camera, renderer.domElement);
controls.enableDamping = true;
controls.dampingFactor = 0.05;
// Lights
hemisphereLight = new THREE.HemisphereLight(0xffffff, 0x444444, 1);
scene.add(hemisphereLight);
directionalLight = new THREE.DirectionalLight(0xffffff, 1);
directionalLight.position.set(10, 20, 10);
directionalLight.castShadow = true;
directionalLight.shadow.mapSize.width = 2048;
directionalLight.shadow.mapSize.height = 2048;
scene.add(directionalLight);
ambientLight = new THREE.AmbientLight(0x404040);
scene.add(ambientLight);
clock = new THREE.Clock();
createTerrain();
animate();
}
function createTerrain() {
const geometry = new THREE.PlaneGeometry(env.size, env.size, env.size - 1, env.size - 1);
const positions = geometry.attributes.position.array;
for (let i = 0; i < positions.length; i += 3) {
const x = positions[i];
const z = positions[i + 1];
positions[i + 2] = env.getHeight(x, z);
}
geometry.attributes.position.needsUpdate = true;
geometry.computeVertexNormals();
const material = new THREE.MeshPhongMaterial({
color: 0x3d8c40,
shininess: 0,
flatShading: true
});
const terrain = new THREE.Mesh(geometry, material);
terrain.name = 'terrain';
terrain.rotation.x = -Math.PI / 2;
terrain.receiveShadow = true;
scene.add(terrain);
env.addWaterAndVegetation();
}
function addAnimal() {
const animal = new Animal(env);
animals.push(animal);
}
function removeAnimal() {
if (animals.length > 0) {
const animal = animals.pop();
animal.dispose();
}
}
function addFoodSource() {
const x = Math.random() * env.size - env.size / 2;
const z = Math.random() * env.size - env.size / 2;
const height = env.getHeight(x, z) + 0.2;
const foodMaterial = new THREE.MeshPhongMaterial({ color: 0xffaa00 });
const foodMesh = new THREE.Mesh(new THREE.SphereGeometry(0.2, 8, 8), foodMaterial);
foodMesh.position.set(x, height, z);
foodMesh.castShadow = true;
scene.add(foodMesh);
const foodSource = { x, z, mesh: foodMesh };
foodSources.push(foodSource);
}
function updateDayNightCycle(delta) {
timeOfDay += delta * dayNightSpeed;
const sunIntensity = Math.max(Math.sin(timeOfDay * 0.1), 0);
const moonIntensity = 1 - sunIntensity;
directionalLight.intensity = sunIntensity;
ambientLight.intensity = sunIntensity * 0.5 + 0.1;
hemisphereLight.intensity = moonIntensity * 0.3 + 0.2;
const skyColor = new THREE.Color(0x87ceeb).lerp(new THREE.Color(0x000033), moonIntensity);
scene.background = skyColor;
scene.fog.color = skyColor;
// Update time display
const timeDisplay = document.getElementById('timeDisplay');
if (sunIntensity > 0.75) {
timeDisplay.textContent = 'Time: Morning';
} else if (sunIntensity > 0.25) {
timeDisplay.textContent = 'Time: Afternoon';
} else if (moonIntensity > 0.75) {
timeDisplay.textContent = 'Time: Night';
} else {
timeDisplay.textContent = 'Time: Evening';
}
}
function updateWeather(delta) {
if (weather === 'rain' || weather === 'snow') {
if (particles.length === 0) createParticles();
updateParticles(delta);
} else {
clearParticles();
}
}
function createParticles() {
const particleCount = 1000;
const particleMaterial = new THREE.PointsMaterial({
color: weather === 'rain' ? 0x5555ff : 0xffffff,
size: weather === 'rain' ? 0.1 : 0.3,
transparent: true,
opacity: 0.6
});
const particlesGeometry = new THREE.BufferGeometry();
const positions = new Float32Array(particleCount * 3);
for (let i = 0; i < particleCount; i++) {
positions[i * 3] = Math.random() * env.size - env.size / 2;
positions[i * 3 + 1] = Math.random() * 20 + 10;
positions[i * 3 + 2] = Math.random() * env.size - env.size / 2;
}
particlesGeometry.setAttribute('position', new THREE.BufferAttribute(positions, 3));
const particleSystem = new THREE.Points(particlesGeometry, particleMaterial);
particleSystem.name = 'particles';
scene.add(particleSystem);
particles.push(particleSystem);
}
function updateParticles(delta) {
particles.forEach(particleSystem => {
const positions = particleSystem.geometry.attributes.position.array;
for (let i = 0; i < positions.length; i += 3) {
positions[i + 1] -= delta * (weather === 'rain' ? 10 : 2);
if (positions[i + 1] < env.getHeight(positions[i], positions[i + 2]) + 0.5) {
positions[i + 1] = Math.random() * 20 + 10;
}
}
particleSystem.geometry.attributes.position.needsUpdate = true;
});
}
function clearParticles() {
particles.forEach(particleSystem => {
scene.remove(particleSystem);
particleSystem.geometry.dispose();
particleSystem.material.dispose();
});
particles = [];
}
function animate() {
requestAnimationFrame(animate);
const delta = clock.getDelta();
updateDayNightCycle(delta);
updateWeather(delta);
animals.forEach(animal => {
animal.move(delta);
});
controls.update();
renderer.render(scene, camera);
}
function simulate() {
for (let i = 0; i < 20; i++) {
addAnimal();
}
}
// Initialize simulation on load
window.addEventListener("load", () => {
env = new Environment(50); // Initialize env first
initScene(); // Then initialize the scene
simulate(); // Now add animals
});
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
});
document.getElementById("speedControl").addEventListener("input", (e) => {
animalSpeed = parseFloat(e.target.value);
document.getElementById("speedDisplay").textContent = animalSpeed.toFixed(1);
});
document.getElementById("terrainControl").addEventListener("input", (e) => {
terrainComplexity = parseFloat(e.target.value);
document.getElementById("terrainDisplay").textContent = terrainComplexity.toFixed(1);
// Recreate terrain
scene.children = scene.children.filter(child => child.name !== 'terrain' && child.name !== 'particles');
createTerrain();
});
document.getElementById("timeControl").addEventListener("input", (e) => {
dayNightSpeed = parseFloat(e.target.value);
});
document.getElementById("weatherControl").addEventListener("change", (e) => {
weather = e.target.value;
document.getElementById('weatherDisplay').textContent = weather.charAt(0).toUpperCase() + weather.slice(1);
clearParticles();
});
document.getElementById("addAnimalBtn").addEventListener("click", () => {
addAnimal();
});
document.getElementById("removeAnimalBtn").addEventListener("click", () => {
removeAnimal();
});
document.getElementById("addFoodBtn").addEventListener("click", () => {
addFoodSource();
});
</script>
</body>
</html>