-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgltf-asset-test.html
217 lines (192 loc) · 6.66 KB
/
gltf-asset-test.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
<!DOCTYPE html>
<html>
<head>
<title>GLTF Loader Test</title>
<link rel="stylesheet" href="./testing-e2e/styles/tachyons.min.css" />
<link rel="stylesheet" href="./testing-e2e/styles/main.css" />
<script src="https://cdn.jsdelivr.net/npm/@zeainc/zea-engine/dist/index.umd.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@zeainc/zea-engine/dist/plugins.umd.js"></script>
<script src="https://www.gstatic.com/draco/v1/decoders/draco_decoder_gltf.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@zeainc/gltf-loader/dist/index.umd.js"></script>
<style>
body {
height: 100vh;
overflow: hidden;
margin: 0em;
}
#canvas {
position: relative;
height: 100%;
width: 100%;
}
.fps-display {
position: fixed;
bottom: 10px;
right: 10px;
color: #333333;
}
.progress-display {
position: fixed;
left: 0px;
bottom: 0px;
width: 100%;
height: 10px;
}
.hidden {
visibility: hidden;
}
</style>
</head>
<body>
<canvas id="canvas"></canvas>
<div class="fps-display" id="fps"></div>
<progress class="progress-display" id="progress" value="0" max="100">0%</progress>
<div id="dropZoneWrapper">
<div id="dropZone">
<form class="my-form">
<p>Drag and drop a GLTF file to view</p>
<input type="file" id="fileElem" multiple accept="*.glb" onchange="handleFiles(this.files)" />
<label class="button" for="fileElem">Select a GLTF file</label>
</form>
</div>
<script type="module">
const {
GLRenderer,
Scene,
Vec3,
Xfo,
Color,
Sphere,
Material,
GeomItem,
GLBoundingBoxPass,
EnvMap,
resourceLoader,
} = zeaEngine
const scene = new Scene()
const renderer = new GLRenderer(document.getElementById('canvas'), { hideSplash: true, xrCompatible: false })
renderer.setScene(scene)
const envMap = new EnvMap()
envMap.load('./testing-e2e/data/pisa.zenv')
scene.setEnvMap(envMap)
scene.displayEnvMapParam.value = false
renderer.exposure = 1.2
renderer.getViewport().getCamera().setPositionAndTarget(new Vec3(3, -3, 1.7), new Vec3(0, 0, 0.1))
// /////////////////////////////////////
// Setup a Bounding Box pass to display all
// the bounding boxes of the items in the scene.
// const boundingBoxPass = new GLBoundingBoxPass()
// renderer.addPass(boundingBoxPass)
// boundingBoxPass.addTreeItem(scene.getRoot(), true)
// /////////////////////////////////////
// Setup the custom classes
// The custom pass to handle rendering, and add a custom
// TreeItem to the scene tree
const { GLTFAsset } = gltfLoader
const loadGLTF = (url, filename) => {
document.getElementById('dropZone').classList.add('hidden')
const asset = new GLTFAsset('gltf')
asset.load(url, filename).then(() => {
console.log('Loading done')
const xfo = new Xfo()
const box = asset.getParameter('BoundingBox').getValue()
if (urlParams.has('y2zup')) {
xfo.ori.setFromAxisAndAngle(new Vec3(1, 0, 0), Math.PI * 0.5)
xfo.tr.z = -box.p0.y
} else {
xfo.tr.z = -box.p0.z
}
asset.getParameter('LocalXfo').setValue(xfo)
renderer.frameAll()
})
if (scene.getRoot().getNumChildren() > 1) scene.getRoot().removeChild(1)
scene.getRoot().addChild(asset)
}
const urlParams = new URLSearchParams(window.location.search)
if (urlParams.has('gltf')) {
loadGLTF(urlParams.get('gltf'))
}
// /////////////////////////////////////
// Check that pointer interactions work.
let highlighted
renderer.getViewport().on('pointerMove', (event) => {
if (event.intersectionData) {
const { geomItem, dist } = event.intersectionData
if (geomItem != highlighted) {
if (highlighted) highlighted.removeHighlight('custom')
highlighted = geomItem
highlighted.addHighlight('custom', new Color(0, 1, 0, 0.1))
}
} else {
if (highlighted) highlighted.removeHighlight('custom')
highlighted = null
}
})
// /////////////////////////////////////
// Drag-n-Drop support
const dropZoneWrapper = document.getElementById('dropZoneWrapper')
const dropArea = document.getElementById('dropZone')
;['dragenter', 'dragover', 'dragleave', 'drop'].forEach((eventName) => {
dropZoneWrapper.addEventListener(eventName, preventDefaults, false)
})
function preventDefaults(e) {
e.preventDefault()
e.stopPropagation()
}
;['dragenter', 'dragover'].forEach((eventName) => {
dropZoneWrapper.addEventListener(eventName, highlight, false)
})
;['dragleave', 'drop'].forEach((eventName) => {
dropZoneWrapper.addEventListener(eventName, unhighlight, false)
})
function highlight(e) {
dropArea.classList.remove('hidden')
dropArea.classList.add('highlight')
}
function unhighlight(e) {
dropArea.classList.remove('highlight')
}
dropArea.addEventListener('drop', handleDrop, false)
function handleDrop(e) {
let dt = e.dataTransfer
let files = dt.files
handleFiles(files)
}
function handleFiles(files) {
([...files]).forEach((file) => {
if (file) {
const reader = new FileReader();
reader.addEventListener("load", function () {
// convert image file to base64 string
loadGLTF(reader.result, file.name);
}, false);
reader.readAsDataURL(file);
}
})
}
window.handleFiles = handleFiles
// /////////////////////////////////////
let frameCounter = 0
let fps = 0
renderer.on('redrawOccurred', () => {
frameCounter++
})
const fpsElement = document.getElementById('fps')
setInterval(() => {
if (fps != frameCounter * 2) {
fps = frameCounter * 2
fpsElement.textContent = `FPS: ${fps}`
}
frameCounter = 0
}, 500)
// /////////////////////////////////////
resourceLoader.on('progressIncremented', (event) => {
const pct = document.getElementById('progress')
pct.value = event.percent
if (event.percent >= 100) {
setTimeout(() => pct.classList.add('hidden'), 1000)
}
})
</script>
</body>
</html>