webgl-framework
Advanced tools
Comparing version 0.1.0 to 0.1.1
{ | ||
"name": "webgl-framework", | ||
"version": "0.1.0", | ||
"version": "0.1.1", | ||
"license": "MIT", | ||
@@ -5,0 +5,0 @@ "description": "Basic low-level WebGL framework", |
'use strict'; | ||
define(['./BinaryDataLoader'], function(BinaryDataLoader) { | ||
define(['./BinaryDataLoader'], function (BinaryDataLoader) { | ||
@@ -8,3 +8,3 @@ /** | ||
*/ | ||
function CompressedTextureLoader() {} | ||
function CompressedTextureLoader() { } | ||
@@ -14,41 +14,42 @@ /** | ||
* @param {string} url - URL to texture in PKM format | ||
* @param {Function} callbak - callback called after texture is loaded to GPU | ||
* @param {Function} callback - callback called after texture is loaded to GPU | ||
* @return {number} - WebGL texture | ||
*/ | ||
CompressedTextureLoader.loadETC1 = function(url, callback) { | ||
var root = this, | ||
CompressedTextureLoader.loadETC1 = function (url, callback) { | ||
const root = this, | ||
texture = gl.createTexture(); | ||
var PKM_HEADER_SIZE = 16; // size of PKM header | ||
var PKM_HEADER_WIDTH_OFFSET = 8; // offset to texture width | ||
var PKM_HEADER_HEIGHT_OFFSET = 10; // offset to texture height | ||
const PKM_HEADER_SIZE = 16; // size of PKM header | ||
const PKM_HEADER_WIDTH_OFFSET = 8; // offset to texture width | ||
const PKM_HEADER_HEIGHT_OFFSET = 10; // offset to texture height | ||
BinaryDataLoader.load(url, function(data) { | ||
var bufWidth, bufHeight, bufData, | ||
width, height; | ||
return new Promise((resolve, reject) => { | ||
BinaryDataLoader.load(url, data => { | ||
let bufWidth, bufHeight, bufData, | ||
width, height; | ||
var ETC1_RGB8_OES = 36196; | ||
const ETC1_RGB8_OES = 36196; | ||
if (data.byteLength > 0) { | ||
// Endianness depends on machine architecture, can't read Int16 | ||
// In PKM, width and height are big-endian, and x86 is little-endian and ARM is bi-endian | ||
bufWidth = new Uint8Array(data, PKM_HEADER_WIDTH_OFFSET, 2); | ||
width = bufWidth[0]*256 + bufWidth[1]; | ||
bufHeight = new Uint8Array(data, PKM_HEADER_HEIGHT_OFFSET, 2); | ||
height = bufHeight[0]*256 + bufHeight[1]; | ||
bufData = new Uint8Array(data, PKM_HEADER_SIZE, data.byteLength - PKM_HEADER_SIZE); | ||
if (data.byteLength > 0) { | ||
// Endianness depends on machine architecture, can't read Int16 | ||
// In PKM, width and height are big-endian, and x86 is little-endian and ARM is bi-endian | ||
bufWidth = new Uint8Array(data, PKM_HEADER_WIDTH_OFFSET, 2); | ||
width = bufWidth[0] * 256 + bufWidth[1]; | ||
bufHeight = new Uint8Array(data, PKM_HEADER_HEIGHT_OFFSET, 2); | ||
height = bufHeight[0] * 256 + bufHeight[1]; | ||
bufData = new Uint8Array(data, PKM_HEADER_SIZE, data.byteLength - PKM_HEADER_SIZE); | ||
gl.bindTexture(gl.TEXTURE_2D, texture); | ||
gl.compressedTexImage2D(gl.TEXTURE_2D, 0, ETC1_RGB8_OES, width, height, 0, bufData); | ||
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR); | ||
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR); | ||
gl.bindTexture(gl.TEXTURE_2D, null); | ||
gl.bindTexture(gl.TEXTURE_2D, texture); | ||
gl.compressedTexImage2D(gl.TEXTURE_2D, 0, ETC1_RGB8_OES, width, height, 0, bufData); | ||
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR); | ||
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR); | ||
gl.bindTexture(gl.TEXTURE_2D, null); | ||
console.log('Loaded texture ' + url + ' [' + width + 'x' + height + ']'); | ||
console.log('Loaded texture ' + url + ' [' + width + 'x' + height + ']'); | ||
callback && callback(); | ||
} | ||
callback && callback(); | ||
resolve(texture); | ||
} | ||
}); | ||
}); | ||
return texture; | ||
} | ||
@@ -55,0 +56,0 @@ |
@@ -25,25 +25,29 @@ 'use strict'; | ||
function loadBuffer(buffer, target, arrayBuffer) { | ||
var byteArray = new Uint8Array(arrayBuffer, 0, arrayBuffer.byteLength); | ||
gl.bindBuffer(target, buffer); | ||
gl.bufferData(target, byteArray, gl.STATIC_DRAW); | ||
} | ||
BinaryDataLoader.load(url + '-indices.bin', | ||
function(data) { | ||
root.bufferIndices = gl.createBuffer(); | ||
console.log('Loaded ' + url + '-indices.bin: ' + data.byteLength + ' bytes'); | ||
loadBuffer(root.bufferIndices, gl.ELEMENT_ARRAY_BUFFER, data); | ||
root.numIndices = data.byteLength / 2 / 3; | ||
root.bufferIndices && root.bufferStrides && callback(); | ||
return new Promise((resolve, reject) => { | ||
const loadBuffer = (buffer, target, arrayBuffer) => { | ||
var byteArray = new Uint8Array(arrayBuffer, 0, arrayBuffer.byteLength); | ||
gl.bindBuffer(target, buffer); | ||
gl.bufferData(target, byteArray, gl.STATIC_DRAW); | ||
} | ||
); | ||
BinaryDataLoader.load(url + '-strides.bin', | ||
function(data) { | ||
root.bufferStrides = gl.createBuffer(); | ||
console.log('Loaded ' + url + '-strides.bin: ' + data.byteLength + ' bytes'); | ||
loadBuffer(root.bufferStrides, gl.ARRAY_BUFFER, data); | ||
root.bufferIndices && root.bufferStrides && callback(); | ||
} | ||
); | ||
BinaryDataLoader.load(url + '-indices.bin', | ||
data => { | ||
root.bufferIndices = gl.createBuffer(); | ||
console.log('Loaded ' + url + '-indices.bin: ' + data.byteLength + ' bytes'); | ||
loadBuffer(root.bufferIndices, gl.ELEMENT_ARRAY_BUFFER, data); | ||
root.numIndices = data.byteLength / 2 / 3; | ||
root.bufferIndices && root.bufferStrides && callback(); | ||
root.bufferIndices && root.bufferStrides && resolve(); | ||
} | ||
); | ||
BinaryDataLoader.load(url + '-strides.bin', | ||
data => { | ||
root.bufferStrides = gl.createBuffer(); | ||
console.log('Loaded ' + url + '-strides.bin: ' + data.byteLength + ' bytes'); | ||
loadBuffer(root.bufferStrides, gl.ARRAY_BUFFER, data); | ||
root.bufferIndices && root.bufferStrides && callback(); | ||
root.bufferIndices && root.bufferStrides && resolve(); | ||
} | ||
); | ||
}); | ||
}, | ||
@@ -50,0 +54,0 @@ |
'use strict'; | ||
define(function() { | ||
define(function () { | ||
@@ -8,3 +8,3 @@ /** | ||
*/ | ||
function UncompressedTextureLoader() {} | ||
function UncompressedTextureLoader() { } | ||
@@ -17,12 +17,20 @@ /** | ||
*/ | ||
UncompressedTextureLoader.load = function(url, callback) { | ||
var texture = gl.createTexture(); | ||
UncompressedTextureLoader.load = function (url, callback, minFilter, magFilter, clamp) { | ||
return new Promise((resolve, reject) => { | ||
const texture = gl.createTexture(); | ||
texture.image = new Image(); | ||
texture.image.src = url; | ||
texture.image.onload = function() { | ||
texture.image.onload = () => { | ||
gl.bindTexture(gl.TEXTURE_2D, texture); | ||
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, texture.image); | ||
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_MIN_FILTER, minFilter || gl.LINEAR); | ||
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, magFilter || gl.LINEAR); | ||
if (typeof clamp !== "undefined" && clamp === true) { | ||
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); | ||
} else { | ||
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.REPEAT); | ||
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.REPEAT); | ||
} | ||
gl.bindTexture(gl.TEXTURE_2D, null); | ||
@@ -35,8 +43,8 @@ | ||
callback && callback(); | ||
resolve(texture); | ||
}; | ||
}); | ||
} | ||
return texture; | ||
} | ||
return UncompressedTextureLoader; | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
141957
12
4388