-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhallo.html
195 lines (164 loc) · 5.44 KB
/
hallo.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
<html>
<head></head>
<body></body>
<script src="http://gamingJS.com/Three.js"></script>
<script src="http://gamingJS.com/ChromeFixes.js"></script>
<script>
var avatar, not_allowed = [], scene, camera, renderer, right_hand, is_moving_left, is_moving_right, is_moving_forward, is_moving_back;
var clock = new THREE.Clock(true);
function rotate() {
avatar.rotation.z = Date.now() * 0.00005;
avatar.rotation.x = Date.now() * 0.00005;
}
function walk() {
right_hand.position.z = Math.sin(clock.getElapsedTime()*10) * 100;
left_hand.position.z = Math.sin(clock.getElapsedTime()*10) * -100;
left_foot.position.z = Math.sin(clock.getElapsedTime()*10) * 50;
right_foot.position.z = Math.sin(clock.getElapsedTime()*10) * -50;
}
function isWalking() {
if (is_moving_left) return true
if (is_moving_right) return true
if (is_moving_forward) return true
if (is_moving_back) return true
return false;
}
function detectCollisions(marker) {
var vector = new THREE.Vector3(0, -1, 0);
var ray = new THREE.Ray(marker.position, vector);
var intersects = ray.intersectObjects(not_allowed)
if (intersects.length > 0) {
return true
} else {
return false
}
}
function makeTreeAt(scene, x, z) {
var trunk = new THREE.Mesh(
new THREE.CylinderGeometry(50, 50, 200),
new THREE.MeshBasicMaterial({color: 0x874400})
);
var top = new THREE.Mesh(
new THREE.SphereGeometry(150),
new THREE.MeshBasicMaterial({color: 0x009030})
);
top.position.y = 175;
trunk.add(top);
var boundary = new THREE.Mesh( new THREE.CircleGeometry(300),
new THREE.MeshBasicMaterial({color: 0x231200}) );
boundary.position.y = -100;
boundary.rotation.x = -Math.PI/2;
trunk.add(boundary);
not_allowed.push(boundary)
trunk.position.set(x, -75, z);
scene.add(trunk);
}
init();
animate();
function generateAvatar() {
// geometry
var eye = new THREE.SphereGeometry(30)
var nose = new THREE.SphereGeometry(20)
var pupille = new THREE.SphereGeometry(10)
var body = new THREE.SphereGeometry(100);
var hand = new THREE.SphereGeometry(50);
// material
var eye_material = new THREE.MeshBasicMaterial({
color: 0xffffff});
var nose_material = new THREE.MeshBasicMaterial({
color: 0xfc0000})
var pupille_material = new THREE.MeshBasicMaterial({
color: 0x000000})
var cover = new THREE.MeshBasicMaterial({
color: 0xd977ef});
// meshs
avatar = new THREE.Mesh(body, cover);
right_hand = new THREE.Mesh(hand, cover);
left_hand = new THREE.Mesh(hand, cover);
right_foot = new THREE.Mesh(hand, cover);
left_foot = new THREE.Mesh(hand, cover);
var left_eye = new THREE.Mesh(eye, eye_material);
var right_eye = new THREE.Mesh(eye, eye_material);
var middle_nose = new THREE.Mesh(nose, nose_material);
var left_pupille = new THREE.Mesh(pupille, pupille_material);
var right_pupille = new THREE.Mesh(pupille, pupille_material);
right_hand.position.set(-140, 0, 0);
left_hand.position.set(140, 0, 0);
right_foot.position.set(-75, -115, 0);
left_foot.position.set(75, -115, 0);
left_eye.position.set(40, 20, 70)
right_eye.position.set(-40, 20, 70)
middle_nose.position.set(0, -10, 70)
left_pupille.position.set(30, 20, 100)
right_pupille.position.set(-30, 20, 100)
avatar.add(right_hand)
avatar.add(left_hand)
avatar.add(right_foot)
avatar.add(left_eye)
avatar.add(left_foot)
avatar.add(right_eye)
avatar.add(middle_nose)
avatar.add(left_pupille)
avatar.add(right_pupille);
return avatar;
}
function init() {
// This is where stuff in our game will happen:
scene = new THREE.Scene();
var marker = new THREE.Object3D();
scene.add(marker);
// Trees
makeTreeAt(scene, 500, 0);
makeTreeAt(scene, -500, 0);
makeTreeAt(scene, 750, -1000);
makeTreeAt(scene, -750, -1000);
// This is what sees the stuff:
var aspect_ratio = window.innerWidth / window.innerHeight;
camera = new THREE.PerspectiveCamera(75, aspect_ratio, 1, 10000);
camera.position.z = 500;
// This will draw what the camera sees onto the screen:
renderer = new THREE.CanvasRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
document.addEventListener('keyup', function(event) {
is_moving_left=false;
is_moving_forward=false;
is_moving_right=false;
is_moving_back=false;
});
document.addEventListener('keydown', function(event){
var code = event.keyCode;
if (code == 37) { // left
marker.position.x = marker.position.x-5;
is_moving_left=true;
}
if (code == 38) { // up
marker.position.z = marker.position.z-5;
is_moving_forward=true;
}
if (code == 39) { // right
marker.position.x = marker.position.x+5;
is_moving_right = true;
}
if (code == 40) { // down
marker.position.z = marker.position.z+5;
is_moving_back=true;
}
if (detectCollisions(marker)) {
if (is_moving_left) marker.position.x = marker.position.x+5;
if (is_moving_forward) marker.position.z = marker.position.z+5;
if (is_moving_right) marker.position.x = marker.position.x-5;
if (is_moving_back) marker.position.z = marker.position.z-5;
}
});
marker.add(generateAvatar());
marker.add(camera);
}
function animate() {
requestAnimationFrame(animate);
// rotate();
if (isWalking()) {walk()}
// Now, show what the camera sees on the screen:
renderer.render(scene, camera);
}
</script></html>