-
-
Notifications
You must be signed in to change notification settings - Fork 36.3k
Expand file tree
/
Copy pathwebgpu_loader_texture_ktx2.html
More file actions
311 lines (221 loc) · 7.6 KB
/
webgpu_loader_texture_ktx2.html
File metadata and controls
311 lines (221 loc) · 7.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
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
<!DOCTYPE html>
<html lang="en">
<head>
<title>three.js WebGPU - KTX2 texture loader</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<link type="text/css" rel="stylesheet" href="main.css">
<style>
* {
box-sizing: border-box;
-moz-box-sizing: border-box;
}
body {
background-color: #fff;
color: #444;
}
a {
color: #08f;
}
#content {
position: absolute;
top: 0; width: 100%;
z-index: 1;
padding: 3em 0 0 0;
}
section {
padding: 1em;
}
#c {
position: absolute;
left: 0;
width: 100%;
height: 100%;
}
section .description {
max-width: 50em;
text-wrap: pretty;
}
.list-item {
display: inline-block;
margin: 1em;
padding: 1em;
box-shadow: 1px 2px 4px 0px rgba(0,0,0,0.25);
}
.list-item > div:nth-child(1) {
width: 200px;
height: 200px;
}
.list-item > div:nth-child(2) {
color: #888;
font-family: sans-serif;
width: 200px;
margin-top: 0.5em;
}
</style>
</head>
<body>
<canvas id="c"></canvas>
<div id="content">
<div id="info"><a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - KTX2 texture loader</div>
</div>
<script type="importmap">
{
"imports": {
"three": "../build/three.webgpu.js",
"three/webgpu": "../build/three.webgpu.js",
"three/tsl": "../build/three.tsl.js",
"three/addons/": "./jsm/"
}
}
</script>
<script type="module">
import * as THREE from 'three';
import { KTX2Loader } from 'three/addons/loaders/KTX2Loader.js';
const canvas = document.getElementById( 'c' );
const renderer = new THREE.WebGPURenderer( { canvas, antialias: true, forceWebGL: false } );
renderer.setClearColor( 0xffffff, 1 );
renderer.setPixelRatio( window.devicePixelRatio );
const loader = new KTX2Loader()
.setTranscoderPath( 'jsm/libs/basis/' )
.setPath( 'textures/ktx2/' );
const scenes = [];
let sections;
initRenderer();
async function initRenderer() {
await renderer.init();
loader.detectSupport( renderer );
const formats = loader.workerConfig;
sections = [
{
title: 'Uncompressed',
description: 'Uncompressed formats (rgba8, rgba16, rgba32) load as THREE.DataTexture objects.'
+ ' Lossless, easy to read/write, uncompressed on GPU, optionally compressed over the network.',
textures: [
{ path: '2d_rgba8.ktx2' },
{ path: '2d_rgba8_linear.ktx2' },
{ path: '2d_rgba16_linear.ktx2' },
{ path: '2d_rgba32_linear.ktx2' },
{ path: '2d_rgb9e5_linear.ktx2' },
{ path: '2d_r11g11b10_linear.ktx2' },
]
},
{
title: 'Compressed',
description: 'Compressed formats (ASTC, BCn, ...) load as THREE.CompressedTexture objects,'
+ ' reducing memory cost. Requires native support on the device GPU: no single compressed'
+ ' format is supported on every device.',
textures: [
{ path: '2d_astc4x4.ktx2', supported: formats[ 'astcSupported' ] },
{ path: '2d_etc1.ktx2', supported: formats[ 'etc2Supported' ] },
{ path: '2d_etc2.ktx2', supported: formats[ 'etc2Supported' ] },
{ path: '2d_bc1.ktx2', supported: formats[ 'bptcSupported' ] },
{ path: '2d_bc3.ktx2', supported: formats[ 'bptcSupported' ] },
{ path: '2d_bc4.ktx2', supported: formats[ 'bptcSupported' ] },
{ path: '2d_bc5.ktx2', supported: formats[ 'bptcSupported' ] },
{ path: '2d_bc7.ktx2', supported: formats[ 'bptcSupported' ] }
]
},
{
title: 'Universal',
description: 'Basis Universal textures are specialized intermediate formats supporting fast'
+ ' runtime transcoding into other GPU texture compression formats. After transcoding,'
+ ' universal textures can be used on any device at reduced memory cost.',
textures: [
{ path: '2d_etc1s.ktx2' },
{ path: '2d_uastc.ktx2' },
]
},
];
await init();
}
async function init() {
const geometry = flipY( new THREE.PlaneGeometry( 1, 1 ) );
const content = document.getElementById( 'content' );
for ( const section of sections ) {
const sectionElement = document.createElement( 'section' );
const sectionHeader = document.createElement( 'h2' );
sectionHeader.textContent = section.title;
sectionElement.appendChild( sectionHeader );
const sectionDescription = document.createElement( 'p' );
sectionDescription.className = 'description';
sectionDescription.textContent = section.description;
sectionElement.appendChild( sectionDescription );
for ( const { path, supported } of section.textures ) {
const scene = new THREE.Scene();
// make a list item
const element = document.createElement( 'div' );
element.className = 'list-item';
const sceneElement = document.createElement( 'div' );
element.appendChild( sceneElement );
const labelElement = document.createElement( 'div' );
labelElement.innerText = 'file: ' + path;
element.appendChild( labelElement );
// the element that represents the area we want to render the scene
scene.userData.element = sceneElement;
sectionElement.appendChild( element );
const camera = new THREE.PerspectiveCamera( 50, 1, 1, 10 );
camera.position.z = 2;
scene.userData.camera = camera;
try {
const texture = await loader.loadAsync( supported === false ? 'fail_load.ktx2' : path );
const mesh = new THREE.Mesh( geometry, new THREE.MeshBasicMaterial( { map: texture } ) );
labelElement.innerText += '\ncolorSpace: ' + texture.colorSpace;
scene.add( mesh );
scenes.push( scene );
} catch ( e ) {
console.error( `Failed to load ${path}`, e );
}
}
content.appendChild( sectionElement );
}
renderer.setAnimationLoop( animate );
}
function updateSize() {
const width = canvas.clientWidth;
const height = canvas.clientHeight;
if ( canvas.width !== width || canvas.height !== height ) {
renderer.setSize( width, height, false );
}
}
// Rewrite UVs for `flipY=false` textures.
function flipY( geometry ) {
const uv = geometry.attributes.uv;
for ( let i = 0; i < uv.count; i ++ ) {
uv.setY( i, 1 - uv.getY( i ) );
}
return geometry;
}
function animate() {
updateSize();
canvas.style.transform = `translateY(${window.scrollY}px)`;
renderer.setClearColor( 0xffffff );
renderer.setScissorTest( false );
renderer.clear();
renderer.setClearColor( 0xe0e0e0 );
renderer.setScissorTest( true );
scenes.forEach( function ( scene ) {
// get the element that is a place holder for where we want to
// draw the scene
const element = scene.userData.element;
// get its position relative to the page's viewport
const rect = element.getBoundingClientRect();
// check if it's offscreen. If so skip it
if ( rect.top < 0 || rect.bottom > renderer.domElement.clientHeight ||
rect.left < 0 || rect.right > renderer.domElement.clientWidth ) {
return; // it's off screen
}
// set the viewport
const width = rect.right - rect.left;
const height = rect.bottom - rect.top;
const left = rect.left;
const top = rect.top;
renderer.setViewport( left, top, width, height );
renderer.setScissor( left, top, width, height );
const camera = scene.userData.camera;
renderer.render( scene, camera );
} );
}
</script>
</body>
</html>