-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
168 lines (125 loc) · 4.08 KB
/
index.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
<html>
<style>
html, body {
margin: 0;
padding: 0;
border: 0;
}
</style>
<body>
<script src="./node_modules/three/build/three.min.js"></script>
<script src="./node_modules/three/examples/js/controls/OrbitControls.js"></script>
<script>
//three.js - Isometric Projection using an Orthographic Camera
var mesh, renderer, scene, camera, controls;
init();
render();
function init() {
// info
info = document.createElement( 'div' );
info.style.position = 'absolute';
info.style.top = '30px';
info.style.width = '100%';
info.style.textAlign = 'center';
info.style.color = '#fff';
info.style.fontWeight = 'bold';
info.style.backgroundColor = 'transparent';
info.style.zIndex = '1';
info.style.fontFamily = 'Monospace';
info.innerHTML = 'three.js - Isometric Projection<br/>drag mouse to rotate camera';
document.body.appendChild( info );
// renderer
renderer = new THREE.WebGLRenderer();
renderer.setSize( window.innerWidth, window.innerHeight );
document.body.appendChild( renderer.domElement );
// scene
scene = new THREE.Scene();
// camera
var aspect = window.innerWidth / window.innerHeight;
var d = 100;
camera = new THREE.OrthographicCamera( - d * aspect, d * aspect, d, - d, 1, 2000 );
// /////////////////////////////////////////////////////////////////////////
// method 1 - use lookAt
// camera.position.set( 20, 20, 20 );
// camera.lookAt( scene.position );
console.log('scene.position ' + JSON.stringify(scene.position))
// method 2 - set the x-component of rotation
camera.position.set( 20, 20, 20 );
camera.rotation.order = 'YXZ';
camera.rotation.y = - Math.PI / 4;
camera.rotation.x = Math.atan( - 1 / Math.sqrt( 2 ) );
// /////////////////////////////////////////////////////////////////////////
// controls
controls = new THREE.OrbitControls( camera, renderer.domElement );
controls.addEventListener( 'change', render );
controls.enableZoom = false;
controls.enablePan = false;
controls.maxPolarAngle = Math.PI / 2;
// ambient
scene.add( new THREE.AmbientLight( 0x444444 ) );
// light
var light = new THREE.PointLight( 0xffffff, 0.8 );
light.position.set( 0, 50, 50 );
scene.add( light );
// axes
//scene.add( new THREE.AxisHelper( 40 ) );
// grid
var geometry = new THREE.PlaneBufferGeometry( 100, 100, 10, 10 );
var material = new THREE.MeshBasicMaterial( { wireframe: true, opacity: 0.5, transparent: true } );
var grid = new THREE.Mesh( geometry, material );
grid.rotation.order = 'YXZ';
grid.rotation.y = - Math.PI / 2;
grid.rotation.x = - Math.PI / 2;
scene.add( grid );
// geometry
var geometry = new THREE.BoxGeometry( 10, 10, 10 );
// material
var material = new THREE.MeshNormalMaterial();
// mesh
mesh = new THREE.Mesh( geometry, material );
mesh.translateY(5);
mesh.translateZ(5);
mesh.translateX(5);
scene.add( mesh );
}
function render() {
renderer.render( scene, camera );
}
document.addEventListener('keydown', function (event) {
if (event.keyCode == '38' /* UP ARROW */) {
mesh.translateX( -10 );
mesh.translateZ( -10 );
camera.position.x -= 10;
camera.position.z -= 10;
} else if (event.keyCode == '40' /* UP DOWN */) {
mesh.translateX( 10 );
mesh.translateZ( 10 );
camera.position.x += 10;
camera.position.z += 10;
} else if (event.keyCode == '37' /* UP LEFT */) {
mesh.translateX( -10 );
mesh.translateZ( 10 );
camera.position.x -= 10;
camera.position.z += 10;
} else if (event.keyCode == '39' /* UP RIGHT */) {
mesh.translateX( 10 );
mesh.translateZ( -10 );
camera.position.x += 10;
camera.position.z -= 10;
}
render();
}, false);
var world = {};
world.spawn = function (object) {
var geometry = new THREE.BoxGeometry( 10, 10, 10 );
// material
var material = new THREE.MeshNormalMaterial();
mesh = new THREE.Mesh( geometry, material );
mesh.translateY(5);
mesh.translateZ(5);
mesh.translateX(5);
scene.add( mesh );
}
</script>
</body>
</html>