-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxiaoche5.html
More file actions
221 lines (211 loc) · 5.6 KB
/
xiaoche5.html
File metadata and controls
221 lines (211 loc) · 5.6 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="author" content="" />
<meta name="description" content="" />
<meta name="viewport" content="width=device-width,initial-scsale=1,maximum-scale=1" />
<title>小车</title>
<style>
body,html {
padding: 0;
margin: 0;
border: 0;
}
</style>
</head>
<body>
<canvas id="canvas">您的设备不支持canvas</canvas>
<script src="js/three.js" type="text/javascript"></script>
<!--<script src="js/lib.js" type="text/javascript"></script>-->
<script src="js/stats.min.js" type="text/javascript"></script>
<script src="js/TrackballControls.js" type="text/javascript"></script>
<script type="text/javascript">
var camera;
var scene;
var renderer;
var controls;
var canvas;
var cube={};
var floor={};
var torusArr=[];
var car={};
var stats;
function initThree(){
canvas = document.getElementById('canvas');
canvas.width=window.innerWidth;
canvas.height=window.innerHeight;
scene = new THREE.Scene();
renderer = new THREE.WebGLRenderer({
canvas:canvas
});
renderer.setClearColor(0x404040);
renderer.shadowMap.enabled=true;
renderer.shadowMap.type=THREE.PCFSoftShadowMap;
}
function initCamera(){
camera=new THREE.PerspectiveCamera(45,window.innerWidth/window.innerHeight,1,100);
camera.position.set(0,7,10);
camera.lookAt(new THREE.Vector3(0,0,0));
scene.add(camera);
}
function initLight(){
var ambienLight = new THREE.AmbientLight(0xaaaaaa);
scene.add(ambienLight);
var light= new THREE.PointLight(0xffffff);
light.position.set(-10,10,7);
light.castShadow=true;
scene.add(light);
}
function initControl(){
controls= new THREE.TrackballControls(camera,renderer.domElement);
controls.rotateSpeed=1.0;
controls.zoomSpeed=1.2;
controls.panSpeed=0.8;
controls.noZoom=false;
controls.noPan=false;
controls.minDistance=30;
controls.maxDistance=100;
controls.staticMoving=true;
controls.dynamicDampingFactor=0.3;
controls.keys=[65,83,68];
controls.addEventListener('change',render);
}
function animate(){
controls.update();
requestAnimationFrame(animate);
}
function initFloor(){
var texture = new THREE.TextureLoader().load('img/floor.jpg',function(texture){
renderer.render(scene,camera);
});
var material = new THREE.MeshLambertMaterial({
map:texture,
side:THREE.DoubleSide
});
floor = new THREE.Mesh(new THREE.PlaneGeometry(16,16,1,1),material);
floor.rotation.set(Math.PI/2,0,0);
floor.position.set(-1,-0.2,-1);
floor.receiveShadow=true;
scene.add(floor);
}
function initCube(){
var materials=[];
for(var i=1;i<=6;i++){
materials.push(new THREE.MeshLambertMaterial({
map:new THREE.TextureLoader().load('img/0'+i+'.png',function(){
renderer.render(scene,camera);
}),
overdraw:true
}));
}
cube = new THREE.Mesh(new THREE.CubeGeometry(3,1,1),new THREE.MultiMaterial(materials));
cube.position.set(0,.45,0);
cube.castShadow=true;
scene.add(cube);
}
var dir=[
[.85,.5],
[.85,-.5],
[-.65,.5],
[-.65,-.5]
]
function initTorus(){
var texture = new THREE.TextureLoader().load('img/wood.jpg',function(){
renderer.render(scene,camera);
});
texture.wrapS = texture.wrapT = THREE.RepeatWrapping;
texture.repeat.set(1,1);
for(var i=0;i<dir.length;i++){
var torus = new THREE.Mesh(new THREE.TorusGeometry(.14,.06,20,30),new THREE.MeshLambertMaterial({
map:texture,
color:0x555555
}));
torus.castShadow=true;
torus.receiveShadow=true;
torus.position.set(dir[i][0],0,dir[i][1]);
torusArr.push(torus);
scene.add(torus);
}
}
function initCar(){
car = new THREE.Object3D();
car.add(cube);
for(var i=0;i<dir.length;i++){
car.add(torusArr[i]);
}
scene.add(car);
}
var turning =false
function move(moveOn){
if(turning){
turning=false;
for(var i=0;i<=1;i++){
torusArr[i].rotation.y=0;
}
}else{
var carDirection = car.getWorldDirection();
carDirection.applyAxisAngle((new THREE.Vector3(0,1,0)).normalize(),Math.PI/2);
var theta = Math.atan2(carDirection.x,carDirection.z);
car.position.x+=.1*moveOn*Math.sin(theta);
car.position.z+=.1*moveOn*Math.cos(theta);
}
render();
}
function turn(turnleft){
if(!turning){
turning=true;
}else{
car.rotation.y +=.1*turnleft;
}
for(var i=0;i<=1;i++){
torusArr[i].rotation.y=.5*turnleft;
}
render();
}
function initMove(){
window.onkeydown=function(e){
switch(e.keyCode){
case 37:
case 65:
turn(1);
break;
case 38:
case 87:
move(1);
break;
case 39:
case 68:
turn(-1);
break;
case 40:
case 83:
move(-1);
break;
}
};
}
function render(){
renderer.render(scene,camera);
stats.update();
}
function init(){
initThree();
initCamera();
initLight();
initFloor();
initCube();
initTorus();
initCar();
stats = new Stats();
document.body.appendChild(stats.domElement);
// drawAxes(scene);
initControl();
animate();
render();
initMove();
}
init();
</script>
</body>
</html>