-
Notifications
You must be signed in to change notification settings - Fork 187
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
basis example app decodes reference textures
- Loading branch information
1 parent
0b8a2af
commit 7dad448
Showing
9 changed files
with
194 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
/* global document, URL */ | ||
import {load} from '@loaders.gl/core'; | ||
import {BasisLoader, BasisFormat} from '@loaders.gl/basis'; | ||
|
||
const canvas = document.createElement('canvas'); | ||
document.body.appendChild(canvas); | ||
canvas.width = 768; | ||
canvas.height = 512; | ||
canvas.style.display = 'block'; | ||
const gl = canvas.getContext('webgl'); | ||
const dxtSupported = Boolean(gl.getExtension('WEBGL_compressed_texture_s3tc')); | ||
const loadOptions = {format: BasisFormat.cTFRGB565}; | ||
|
||
if (dxtSupported) { | ||
loadOptions.format = { | ||
alpha: BasisFormat.cTFBC3, | ||
noAlpha: BasisFormat.cTFBC1 | ||
}; | ||
} | ||
|
||
const vs = ` | ||
precision highp float; | ||
attribute vec2 position; | ||
varying vec2 uv; | ||
void main() { | ||
gl_Position = vec4(position, 0.0, 1.0); | ||
uv = vec2(position.x * .5, -position.y * .5) + vec2(.5, .5); | ||
} | ||
`; | ||
|
||
const fs = ` | ||
precision highp float; | ||
uniform sampler2D tex; | ||
varying vec2 uv; | ||
void main() { | ||
gl_FragColor = vec4(texture2D(tex, uv).rgb, 1.); | ||
} | ||
`; | ||
|
||
const vertexShader = gl.createShader(gl.VERTEX_SHADER); | ||
gl.shaderSource(vertexShader, vs); | ||
gl.compileShader(vertexShader); | ||
|
||
const fragmentShader = gl.createShader(gl.FRAGMENT_SHADER); | ||
gl.shaderSource(fragmentShader, fs); | ||
gl.compileShader(fragmentShader); | ||
|
||
const program = gl.createProgram(); | ||
gl.attachShader(program, vertexShader); | ||
gl.attachShader(program, fragmentShader); | ||
gl.linkProgram(program); | ||
|
||
if (!gl.getProgramParameter(program, gl.LINK_STATUS)) { | ||
// eslint-disable-next-line | ||
console.error(gl.getProgramInfoLog(program)); | ||
} else { | ||
gl.enableVertexAttribArray(0); | ||
const buffer = gl.createBuffer(); | ||
gl.bindBuffer(gl.ARRAY_BUFFER, buffer); | ||
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([-1, -1, -1, 1, 1, -1, 1, 1]), gl.STATIC_DRAW); | ||
gl.vertexAttribPointer(0, 2, gl.FLOAT, false, 0, 0); | ||
|
||
gl.useProgram(program); | ||
} | ||
|
||
function renderTexture({width, height, format, decodedData}) { | ||
if (!dxtSupported) { | ||
// TODO: implement cTFRGB565 support | ||
return; | ||
} | ||
|
||
const ext = gl.getExtension('WEBGL_compressed_texture_s3tc'); | ||
const glFormat = { | ||
[BasisFormat.cTFBC1]: ext.COMPRESSED_RGB_S3TC_DXT1_EXT, | ||
[BasisFormat.cTFBC3]: ext.COMPRESSED_RGBA_S3TC_DXT5_EXT | ||
}[format]; | ||
|
||
const texture = gl.createTexture(); | ||
gl.bindTexture(gl.TEXTURE_2D, texture); | ||
gl.compressedTexImage2D(gl.TEXTURE_2D, 0, glFormat, width, height, 0, decodedData); | ||
|
||
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR); | ||
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR); | ||
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE); | ||
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE); | ||
|
||
gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4); | ||
} | ||
|
||
const btn = document.createElement('button'); | ||
document.body.appendChild(btn); | ||
btn.innerText = 'Load Basis File'; | ||
|
||
btn.addEventListener('click', e => { | ||
const el = document.createElement('input'); | ||
el.type = 'file'; | ||
el.addEventListener('input', async ev => { | ||
const url = URL.createObjectURL(ev.target.files[0]); | ||
const result = await load(url, BasisLoader, loadOptions); | ||
|
||
// eslint-disable-next-line | ||
console.log(result); | ||
renderTexture(result[0][0]); | ||
}); | ||
el.click(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
export {BasisLoader, BasisWorkerLoader} from './basis-loader'; | ||
export {BasisLoader, BasisWorkerLoader, BasisFormat} from './basis-loader'; | ||
export {CompressedTextureLoader as _CompressedTextureLoader} from './compressed-texture-loader'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,56 @@ | ||
import {BasisFormat} from '../basis-loader'; | ||
import {loadBasisModule} from './basis-module-loader'; | ||
|
||
function transcodeImage(basisFile, imageIndex, levelIndex, options) { | ||
const width = basisFile.getImageWidth(imageIndex, levelIndex); | ||
const height = basisFile.getImageHeight(imageIndex, levelIndex); | ||
|
||
// See https://github.com/BinomialLLC/basis_universal/pull/83 | ||
const hasAlpha = basisFile.getHasAlpha(/* imageIndex, levelIndex */); | ||
|
||
let format = (options && options.format) || BasisFormat.cTFRGB565; | ||
if (typeof format === 'object') { | ||
format = hasAlpha ? format.alpha : format.noAlpha; | ||
} | ||
|
||
const decodedSize = basisFile.getImageTranscodedSizeInBytes(imageIndex, levelIndex, format); | ||
const decodedData = new Uint8Array(decodedSize); | ||
|
||
if (!basisFile.transcodeImage(decodedData, imageIndex, levelIndex, format, 0, 0)) { | ||
return null; | ||
} | ||
|
||
return {width, height, hasAlpha, format, decodedSize, decodedData}; | ||
} | ||
|
||
async function parse(data, options) { | ||
const {BasisFile} = await loadBasisModule(options); | ||
return new BasisFile(new Uint8Array(data)); | ||
const basisFile = new BasisFile(new Uint8Array(data)); | ||
|
||
try { | ||
if (!basisFile.startTranscoding()) { | ||
return null; | ||
} | ||
|
||
const imageCount = basisFile.getNumImages(); | ||
const images = []; | ||
|
||
for (let imageIndex = 0; imageIndex < imageCount; imageIndex++) { | ||
const levelsCount = basisFile.getNumLevels(imageIndex); | ||
const levels = []; | ||
|
||
for (let levelIndex = 0; levelIndex < levelsCount; levelIndex++) { | ||
levels.push(transcodeImage(basisFile, imageIndex, levelIndex, options)); | ||
} | ||
|
||
images.push(levels); | ||
} | ||
|
||
return images; | ||
} finally { | ||
basisFile.close(); | ||
basisFile.delete(); | ||
} | ||
} | ||
|
||
export default parse; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters