UNPKG

mdx-m3-viewer

Version:

A browser WebGL model viewer. Mainly focused on models of the games Warcraft 3 and Starcraft 2.

43 lines (35 loc) 1.31 kB
import {scaleNPOT} from '../../../common/canvas'; import Texture from '../../texture'; /** * A TGA texture handler. */ export default class TgaTexture extends Texture { /** * @param {ArrayBuffer} src */ load(src) { let gl = this.viewer.gl; let dataView = new DataView(src); let imageType = dataView.getUint8(2); if (imageType !== 2) { throw new Error('Unsupported image type'); } let width = dataView.getUint16(12, true); let height = dataView.getUint16(14, true); let pixelDepth = dataView.getUint8(16); if (pixelDepth !== 32) { throw new Error('Unsupported bits per pixel'); } // Upscale to POT if the size is NPOT. let imageData = scaleNPOT(new ImageData(new Uint8ClampedArray(src, 18, width * height * 4), width, height)); let id = gl.createTexture(); gl.bindTexture(gl.TEXTURE_2D, id); this.viewer.webgl.setTextureMode(gl.REPEAT, gl.REPEAT, gl.LINEAR, gl.LINEAR_MIPMAP_LINEAR); gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, imageData); gl.generateMipmap(gl.TEXTURE_2D); this.imageData = imageData; this.width = imageData.width; // Note: might not be the same as 'width' and 'height' due to NPOT upscaling. this.height = imageData.height; this.webglResource = id; } }