-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSimulationV2.html
103 lines (89 loc) · 3.7 KB
/
SimulationV2.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
<!DOCTYPE html>
<html>
<head>
<title>3D Sphere Animation</title>
<style>
body { margin: 0; overflow: hidden; }
canvas { width: 100%; height: 100% }
</style>
<script src="https://threejs.org/build/three.js"></script>
</head>
<body>
<script>
var scene = new THREE.Scene();
var camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
var renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
var geometry = new THREE.SphereGeometry(1, 32, 32);
var material = new THREE.MeshPhongMaterial({color: 0x00ff00, specular: 0x050505});
var sphere = new THREE.Mesh(geometry, material);
sphere.position.y = 1.5; // Move the sphere up
scene.add(sphere);
// Add random tiny black dots on the sphere
for (let i = 0; i < 100; i++) {
let dotGeometry = new THREE.SphereGeometry(0.02, 32, 32);
let dotMaterial = new THREE.MeshBasicMaterial({color: 0x000000});
let dot = new THREE.Mesh(dotGeometry, dotMaterial);
let position = new THREE.Vector3();
let vertices = sphere.geometry.attributes.position;
let randomIndex = Math.floor(Math.random() * vertices.count) * vertices.itemSize;
position.fromArray(vertices.array, randomIndex);
dot.position.copy(position);
sphere.add(dot);
}
var grassGeometry = new THREE.CylinderGeometry(0.02, 0.1, 2, 32); // Broaden and widen the grass blade from tip to base
var grassMaterial = new THREE.MeshBasicMaterial({color: 0x008000});
var grass1 = new THREE.Mesh(grassGeometry, grassMaterial);
var grass2 = new THREE.Mesh(grassGeometry, grassMaterial);
var grass3 = new THREE.Mesh(grassGeometry, grassMaterial);
grass1.position.set(-1, -1, 0);
grass2.position.set(0, -1, 0);
grass3.position.set(1, -1, 0);
scene.add(grass1, grass2, grass3);
var light = new THREE.PointLight(0xffffff, 1, 100);
light.position.set(0, 0, 10);
scene.add(light);
// Add an infinite plane
var planeGeometry = new THREE.PlaneGeometry(100, 100);
var planeMaterial = new THREE.MeshPhongMaterial({color: 0x00ff00, specular: 0x050505});
var plane = new THREE.Mesh(planeGeometry, planeMaterial);
plane.rotation.x = - Math.PI / 2;
scene.add(plane);
camera.position.z = 5;
var animate = function () {
requestAnimationFrame(animate);
sphere.rotation.x += 0.01;
sphere.rotation.y += 0.01;
renderer.render(scene, camera);
};
animate();
window.addEventListener('keydown', function(event) {
switch (event.keyCode) {
case 37: // Left
sphere.rotation.y -= 0.1;
grass1.rotation.z += 0.1;
grass2.rotation.z += 0.1;
grass3.rotation.z += 0.1;
break;
case 39: // Right
sphere.rotation.y += 0.1;
grass1.rotation.z -= 0.1;
grass2.rotation.z -= 0.1;
grass3.rotation.z -= 0.1;
break;
case 38: // Up
sphere.rotation.x -= 0.1;
break;
case 40: // Down
sphere.rotation.x += 0.1;
break;
case 67: // C
var color = new THREE.Color(Math.random(), Math.random(), Math.random());
sphere.material.color = color;
break;
}
}, false);
</script>
</body>
</html>