-
-
Notifications
You must be signed in to change notification settings - Fork 36.4k
Expand file tree
/
Copy pathwebgl_loader_texture_ktx.html
More file actions
217 lines (157 loc) · 5.57 KB
/
webgl_loader_texture_ktx.html
File metadata and controls
217 lines (157 loc) · 5.57 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
<!DOCTYPE html>
<html lang="en">
<head>
<title>three.js webgl - materials - compressed textures</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">
</head>
<body>
<div id="info">
<a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - webgl - compressed KTX textures<br />
<a href="https://www.khronos.org/opengles/sdk/tools/KTX/file_format_spec/">Khronos Texture</a> is a lightweight file format for OpenGL
</div>
<script type="importmap">
{
"imports": {
"three": "../build/three.module.js",
"three/addons/": "./jsm/"
}
}
</script>
<script type="module">
import * as THREE from 'three';
import { KTXLoader } from 'three/addons/loaders/KTXLoader.js';
/*
This is how compressed textures are supposed to be used:
best for desktop:
BC1(DXT1) - opaque textures
BC3(DXT5) - transparent textures with full alpha range
best for iOS:
PVR2, PVR4 - opaque textures or alpha
best for Android:
ETC1 - opaque textures
ASTC_4x4, ASTC8x8 - transparent textures with full alpha range
*/
let camera, scene, renderer;
const meshes = [];
init();
function init() {
renderer = new THREE.WebGLRenderer( { antialias: true } );
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( window.innerWidth, window.innerHeight );
renderer.setAnimationLoop( animate );
document.body.appendChild( renderer.domElement );
const formats = {
astc: renderer.extensions.has( 'WEBGL_compressed_texture_astc' ),
etc1: renderer.extensions.has( 'WEBGL_compressed_texture_etc1' ),
etc2: renderer.extensions.has( 'WEBGL_compressed_texture_etc' ),
s3tc: renderer.extensions.has( 'WEBGL_compressed_texture_s3tc' ),
pvrtc: renderer.extensions.has( 'WEBGL_compressed_texture_pvrtc' )
};
camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 1, 2000 );
camera.position.z = 1000;
scene = new THREE.Scene();
const ambientLight = new THREE.AmbientLight( 0xffffff, 0.02 );
scene.add( ambientLight );
const pointLight = new THREE.PointLight( 0xffffff, 2, 0, 0 );
pointLight.position.z = - 300;
scene.add( pointLight );
const geometry = new THREE.BoxGeometry( 200, 200, 200 );
let material1, material2, material3;
// TODO: add cubemap support
const loader = new KTXLoader();
if ( formats.pvrtc ) {
material1 = new THREE.MeshBasicMaterial( {
map: loader.load( 'textures/compressed/disturb_PVR2bpp.ktx' )
} );
material1.map.colorSpace = THREE.SRGBColorSpace;
material2 = new THREE.MeshBasicMaterial( {
map: loader.load( 'textures/compressed/lensflare_PVR4bpp.ktx' ),
depthTest: false,
transparent: true,
side: THREE.DoubleSide
} );
material2.map.colorSpace = THREE.SRGBColorSpace;
meshes.push( new THREE.Mesh( geometry, material1 ) );
meshes.push( new THREE.Mesh( geometry, material2 ) );
}
if ( formats.s3tc ) {
material1 = new THREE.MeshBasicMaterial( {
map: loader.load( 'textures/compressed/disturb_BC1.ktx' )
} );
material1.map.colorSpace = THREE.SRGBColorSpace;
material2 = new THREE.MeshBasicMaterial( {
map: loader.load( 'textures/compressed/lensflare_BC3.ktx' ),
depthTest: false,
transparent: true,
side: THREE.DoubleSide
} );
material2.map.colorSpace = THREE.SRGBColorSpace;
material3 = new THREE.MeshStandardMaterial( {
normalMap: loader.load( 'textures/compressed/normal.bc5.ktx' )
} );
meshes.push( new THREE.Mesh( geometry, material1 ) );
meshes.push( new THREE.Mesh( geometry, material2 ) );
meshes.push( new THREE.Mesh( geometry, material3 ) );
}
if ( formats.etc1 ) {
material1 = new THREE.MeshBasicMaterial( {
map: loader.load( 'textures/compressed/disturb_ETC1.ktx' )
} );
meshes.push( new THREE.Mesh( geometry, material1 ) );
}
if ( formats.etc2 ) {
material1 = new THREE.MeshStandardMaterial( {
normalMap: loader.load( 'textures/compressed/normal.eac_rg.ktx' )
} );
meshes.push( new THREE.Mesh( geometry, material1 ) );
}
if ( formats.astc ) {
material1 = new THREE.MeshBasicMaterial( {
map: loader.load( 'textures/compressed/disturb_ASTC4x4.ktx' )
} );
material1.map.colorSpace = THREE.SRGBColorSpace;
material2 = new THREE.MeshBasicMaterial( {
map: loader.load( 'textures/compressed/lensflare_ASTC8x8.ktx' ),
depthTest: false,
transparent: true,
side: THREE.DoubleSide
} );
material2.map.colorSpace = THREE.SRGBColorSpace;
meshes.push( new THREE.Mesh( geometry, material1 ) );
meshes.push( new THREE.Mesh( geometry, material2 ) );
}
let x0 = - Math.min( 4, meshes.length ) * 300 / 2 + 150;
for ( let i = 0; i < Math.min( 4, meshes.length ); ++ i, x0 += 300 ) {
const mesh = meshes[ i ];
mesh.position.x = x0;
mesh.position.y = 150;
scene.add( mesh );
}
let x1 = - ( meshes.length - 4 ) * 300 / 2 + 150;
for ( let i = 4; i < meshes.length; ++ i, x1 += 300 ) {
const mesh = meshes[ i ];
mesh.position.x = x1;
mesh.position.y = - 150;
scene.add( mesh );
}
window.addEventListener( 'resize', onWindowResize );
}
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize( window.innerWidth, window.innerHeight );
}
function animate() {
const time = Date.now() * 0.001;
for ( let i = 0; i < meshes.length; i ++ ) {
const mesh = meshes[ i ];
mesh.rotation.x = time;
mesh.rotation.y = time;
}
renderer.render( scene, camera );
}
</script>
</body>
</html>