-
Notifications
You must be signed in to change notification settings - Fork 154
/
pipelinethreejsdaescene.html
260 lines (191 loc) · 5.6 KB
/
pipelinethreejsdaescene.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
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Programming 3D Applications in HTML5 and WebGL — Loading COLLADA Scenes Using Three.js</title>
<link rel="stylesheet" href="../css/main.css" />
<div id="container" style="width:98%; height:98%; position:absolute;background-color:skyblue;">
</div>
<script src="../libs/jquery-1.9.1/jquery-1.9.1.js"></script>
<script src="../libs/three.js.r58/three.js"></script>
<script src="../libs/three.js.r58/controls/OrbitControls.js"></script>
<script src="../libs/three.js.r59/loaders/ColladaLoader.js"></script>
<script src="../libs/requestAnimationFrame/RequestAnimationFrame.js"></script>
<script type="text/javascript">
var renderer = null,
scene = null,
camera = null,
orbitControls = null,
headlight = null;
function run() {
requestAnimationFrame(function() { run(); });
// Update the camera controller
if (orbitControls)
orbitControls.update();
// Reposition the headlight to point at the model
if (headlight)
headlight.position.copy(camera.position);
// Render the scene
if (scene && camera)
renderer.render( scene, camera );
}
var shadows = true;
var addEnvironment = true;
var SHADOW_MAP_WIDTH = 2048, SHADOW_MAP_HEIGHT = 2048;
function createRenderer(container) {
// Create the Three.js renderer and attach it to our canvas
renderer = new THREE.WebGLRenderer( { antialias: true } );
// Turn on shadows
if (shadows) {
renderer.shadowMapEnabled = true;
renderer.shadowMapType = THREE.PCFSoftShadowMap;
}
// Set the viewport size
renderer.setSize(container.offsetWidth, container.offsetHeight);
container.appendChild(renderer.domElement);
}
function createDefaultLights() {
// Lighting setup
headlight = new THREE.DirectionalLight;
headlight.position.set(0, 0, 1);
scene.add(headlight);
var ambient = new THREE.AmbientLight(0x222222);
scene.add(ambient);
}
function createDefaultCamera()
{
// Add a camera so we can view the scene
camera = new THREE.PerspectiveCamera( 45, container.offsetWidth / container.offsetHeight, 0.001, 10000 );
camera.position.z = 5;
scene.add(camera);
}
// Compute the bounding box of an object or hierarchy of objects
computeBoundingBox = function(obj) {
if (obj instanceof THREE.Mesh) {
var geometry = obj.geometry;
if (geometry) {
if (!geometry.boundingBox) {
geometry.computeBoundingBox();
}
var geometryBBox = geometry.boundingBox;
obj.updateMatrix();
geometryBBox.applyMatrix4(obj.matrix);
return geometryBBox;
}
else {
return new THREE.Box3(new THREE.Vector3, new THREE.Vector3);
}
}
else {
var i, len = obj.children.length;
var boundingBox = new THREE.Box3(new THREE.Vector3, new THREE.Vector3);
for (i = 0; i < len; i++) {
var bbox = computeBoundingBox(obj.children[i]);
if ( bbox.min.x < boundingBox.min.x ) {
boundingBox.min.x = bbox.min.x;
}
if ( bbox.max.x > boundingBox.max.x ) {
boundingBox.max.x = bbox.max.x;
}
if ( bbox.min.y < boundingBox.min.y ) {
boundingBox.min.y = bbox.min.y;
}
if ( bbox.max.y > boundingBox.max.y ) {
boundingBox.max.y = bbox.max.y;
}
if ( bbox.min.z < boundingBox.min.z ) {
boundingBox.min.z = bbox.min.z;
}
if ( bbox.max.z > boundingBox.max.z ) {
boundingBox.max.z = bbox.max.z;
}
}
obj.updateMatrix();
boundingBox.applyMatrix4(obj.matrix);
return boundingBox;
}
}
function loadScene() {
// Ruins by ERLHN
// http://www.turbosquid.com/FullPreview/Index.cfm/ID/668298
// http://www.turbosquid.com/Search/Artists/ERLHN
var url = "../models/ruins/Ruins_dae.dae";
var loader = new THREE.ColladaLoader();
loader.load( url, function( data ) {
handleSceneLoaded(data) } );
}
function handleSceneLoaded(data) {
// Add the objects to the scene
scene.add(data.scene);
// Look for a camera and lighting
var result = {};
data.scene.traverse(function (n) { traverseScene(n, result); });
if (result.cameras && result.cameras.length)
camera = result.cameras[0];
else {
// Find a good camera position based on the size of the scene
createDefaultCamera();
var boundingBox = computeBoundingBox(data.scene);
var front = boundingBox.max;
camera.position.set(front.x, front.y, front.z);
}
if (result.lights && result.lights.length) {
}
else
createDefaultLights();
// Create the controller
initControls();
}
function traverseScene(n, result)
{
// Look for cameras
if (n instanceof THREE.Camera) {
if (!result.cameras)
result.cameras = [];
result.cameras.push(n);
}
// Look for lights
if (n instanceof THREE.Light) {
if (!result.lights)
result.lights = [];
result.lights.push(n);
}
}
function createEnvironment() {
// ground
var groundMaterial = new THREE.MeshPhongMaterial({
color: 0xffffff,
ambient: 0x555555,
shading: THREE.SmoothShading,
});
var ground = new THREE.Mesh( new THREE.PlaneGeometry(1024, 1024, 32, 32), groundMaterial);
ground.rotation.x = -Math.PI / 2;
scene.add(ground);
}
var addEnvironment = true;
function createScene(container) {
// Create a new Three.js scene
scene = new THREE.Scene();
// Ground
if (addEnvironment)
createEnvironment();
// Load the scene
loadScene();
}
function initControls() {
orbitControls = new THREE.OrbitControls(camera, renderer.domElement);
}
$(document).ready(
function() {
var container = document.getElementById("container");
// create the renderer
createRenderer(container);
// create the scene and load the DAE
createScene();
// Run the run loop
run();
}
);
</script>
</head>
<body>
</html>