-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSimulationV1.html
88 lines (76 loc) · 3 KB
/
SimulationV1.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
<!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);
scene.add(sphere);
var grassGeometry = new THREE.BoxGeometry(0.1, 2, 0.1);
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);
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.x -= 0.1;
grass2.rotation.x -= 0.1;
grass3.rotation.x -= 0.1;
break;
case 38: // Up
sphere.rotation.x += 0.1;
grass1.rotation.y += 0.1;
grass2.rotation.y += 0.1;
grass3.rotation.y += 0.1;
break;
case 39: // Right
sphere.rotation.y += 0.1;
grass1.rotation.x += 0.1;
grass2.rotation.x += 0.1;
grass3.rotation.x += 0.1;
break;
case 40: // Down
sphere.rotation.x -= 0.1;
grass1.rotation.y -= 0.1;
grass2.rotation.y -= 0.1;
grass3.rotation.y -= 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>