Skip to content

Instantly share code, notes, and snippets.

@martinemmert
Created November 20, 2017 22:04
Show Gist options
  • Save martinemmert/8bd47e016a212e8847c2fa5e18cca03f to your computer and use it in GitHub Desktop.
Save martinemmert/8bd47e016a212e8847c2fa5e18cca03f to your computer and use it in GitHub Desktop.
import {
globalRegistry,
WebGLTextureLoaderSyncHashCache,
} from "webgltexture-loader";
export interface ArrayBufferInput {
data: ArrayBuffer;
hash: string;
}
class ArrayBufferTextureLoader extends WebGLTextureLoaderSyncHashCache<ArrayBufferInput> {
constructor(gl: WebGLRenderingContext) {
super(gl);
}
canLoad(input: ArrayBufferInput): boolean {
const res: boolean = input.data instanceof ArrayBuffer;
return res;
}
inputHash(input: ArrayBufferInput): string {
return input.hash;
}
getSize(obj: ArrayBufferInput): [number, number] {
return [100, 100];
}
getNoCache(input: ArrayBufferInput): WebGLTextureLoader.TextureAndSize {
const { gl } = this;
const arrayBufferView: Uint8Array = new Uint8Array(input.data);
const texture = gl.createTexture();
gl.bindTexture(gl.TEXTURE_2D, texture);
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, 100, 100, 0, gl.RGBA, gl.UNSIGNED_BYTE, arrayBufferView);
return {texture, width: 100, height: 100};
}
}
globalRegistry.add(ArrayBufferTextureLoader);
export default ArrayBufferTextureLoader;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment