Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

gl-texture2d

Package Overview
Dependencies
Maintainers
3
Versions
34
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

gl-texture2d - npm Package Compare versions

Comparing version 1.2.0 to 2.0.0

6

package.json
{
"name": "gl-texture2d",
"version": "1.2.0",
"version": "2.0.0",
"description": "WebGL texture wrapper",

@@ -15,3 +15,3 @@ "main": "texture.js",

"type": "git",
"url": "git://github.com/mikolalysenko/gl-texture2d.git"
"url": "git://github.com/stackgl/gl-texture2d.git"
},

@@ -29,3 +29,3 @@ "keywords": [

"bugs": {
"url": "https://github.com/mikolalysenko/gl-texture2d/issues"
"url": "https://github.com/stackgl/gl-texture2d/issues"
},

@@ -32,0 +32,0 @@ "devDependencies": {

@@ -10,8 +10,8 @@ gl-texture2d

```javascript
var shell = require("gl-now")()
var createShader = require("gl-shader")
var shell = require("gl-now")()
var createShader = require("gl-shader")
var createTexture = require("gl-texture2d")
var drawTriangle = require("a-big-triangle")
var baboon = require("baboon-image")
var glslify = require("glslify")
var drawTriangle = require("a-big-triangle")
var baboon = require("baboon-image")
var glslify = require("glslify")

@@ -77,3 +77,3 @@ var createShader = glslify({

* `shape` is a length 2 array representing the `[rows, columns]` of the texture
* `shape` is a length 2 array representing the `[width, height]` of the texture
* `format` (optional) is the format of the texture (default `gl.RGBA`)

@@ -102,12 +102,12 @@ * `type` is the type of texture (default `gl.UNSIGNED_BYTE`)

| ------------ |:----------:|:---------------:|:----------------------:|
| `float*` | [m,n] | LUMINANCE | FLOAT |
| `float*` | [m,n,1] | ALPHA | FLOAT |
| `float*` | [m,n,2] | LUMINANCE_ALPHA | FLOAT |
| `float*` | [m,n,3] | RGB | FLOAT |
| `float*` | [m,n,4] | RGBA | FLOAT |
| `(u)int*` | [m,n] | LUMINANCE | UNSIGNED_BYTE |
| `(u)int*` | [m,n,1] | ALPHA | UNSIGNED_BYTE |
| `(u)int*` | [m,n,2] | LUMINANCE_ALPHA | UNSIGNED_BYTE |
| `(u)int*` | [m,n,3] | RGB | UNSIGNED_BYTE |
| `(u)int*` | [m,n,4] | RGBA | UNSIGNED_BYTE |
| `float*` | [w,h] | LUMINANCE | FLOAT |
| `float*` | [w,h,1] | ALPHA | FLOAT |
| `float*` | [w,h,2] | LUMINANCE_ALPHA | FLOAT |
| `float*` | [w,h,3] | RGB | FLOAT |
| `float*` | [w,h,4] | RGBA | FLOAT |
| `(u)int*` | [w,h] | LUMINANCE | UNSIGNED_BYTE |
| `(u)int*` | [w,h,1] | ALPHA | UNSIGNED_BYTE |
| `(u)int*` | [w,h,2] | LUMINANCE_ALPHA | UNSIGNED_BYTE |
| `(u)int*` | [w,h,3] | RGB | UNSIGNED_BYTE |
| `(u)int*` | [w,h,4] | RGBA | UNSIGNED_BYTE |

@@ -139,3 +139,3 @@ Other combinations of shape and dtype are invalid and throw an error.

* `offset` is a length 2 array representing the offset into which the pixels will be written in `[rows,columns]`. (Default: `[0,0]`)
* `offset` is a length 2 array representing the offset into which the pixels will be written in `[x,y]`. (Default: `[0,0]`)
* `mipLevel` is the mip level to write to. (Default `0`)

@@ -151,6 +151,6 @@

#### `tex.shape`
An array representing the dimensions of the texture in `[rows, columns]`. Writing to this value will resize the texture and invalidate its contents. For example, to resize the texture `tex` to the shape `[newRows, newColumns]` you can do:
An array representing the dimensions of the texture in `[rows, columns]`. Writing to this value will resize the texture and invalidate its contents. For example, to resize the texture `tex` to the shape `[newWidth, newHeight]` you can do:
```javascript
tex.shape = [newRows, newColumns]
tex.shape = [newWidth, newHeight]
```

@@ -194,4 +194,3 @@

# Credits
(c) 2013 Mikola Lysenko. MIT License
(c) 2013-2014 Mikola Lysenko. MIT License
"use strict"
var ndarray = require("ndarray")
var ops = require("ndarray-ops")
var pool = require("typedarray-pool")
var ops = require("ndarray-ops")
var pool = require("typedarray-pool")
var webglew = require("webglew")
module.exports = createTexture2D
var linearTypes = null
var filterTypes = null
var wrapTypes = null
var wrapTypes = null

@@ -38,2 +40,15 @@ function lazyInitLinearTypes(gl) {

function reshapeTexture(tex, w, h) {
var gl = tex.gl
var maxSize = gl.getParameter(gl.MAX_TEXTURE_SIZE)
if(w < 0 || w > maxSize || h < 0 || h > maxSize) {
throw new Error("gl-texture2d: Invalid texture size")
}
tex._shape = [w, h]
tex.bind()
gl.texImage2D(gl.TEXTURE_2D, 0, tex.format, w, h, 0, tex.format, tex.type, null)
tex._mipLevels = [0]
return tex
}
function Texture2D(gl, handle, width, height, format, type) {

@@ -44,3 +59,3 @@ this.gl = gl

this.type = type
this._shape = [height, width]
this._shape = [width, height]
this._mipLevels = [0]

@@ -52,151 +67,196 @@ this._magFilter = gl.NEAREST

this._anisoSamples = 1
}
Object.defineProperty(Texture2D.prototype, "minFilter", {
get: function() {
return this._minFilter
},
set: function(v) {
this.bind()
var gl = this.gl
if(this.type === gl.FLOAT && linearTypes.indexOf(v) >= 0) {
if(!webglew(gl).OES_texture_float_linear) {
v = gl.NEAREST
var parent = this
var wrapVector = [this._wrapS, this._wrapT]
Object.defineProperties(wrapVector, {
"0": {
get: function() {
return parent._wrapS
},
set: function(v) {
return parent.wrapS = v
}
},
"1": {
get: function() {
return parent._wrapT
},
set: function(v) {
return parent.wrapT = v
}
}
if(filterTypes.indexOf(v) < 0) {
throw new Error("gl-texture2d: Unknown filter mode " + v)
})
this._wrapVector = wrapVector
var shapeVector = [this._shape[0], this._shape[1]]
Object.defineProperties(shapeVector, {
"0": {
get: function() {
return parent._shape[0]
},
set: function(v) {
return parent.width = v
}
},
"1" : {
get: function() {
return parent._shape[1]
},
set: function(v) {
return parent.height = v
}
}
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, v)
return this._minFilter = v
}
})
})
this._shapeVector = shapeVector
}
var proto = Texture2D.prototype
Object.defineProperty(proto, "magFilter", {
get: function() {
return this._magFilter
Object.defineProperties(proto, {
"minFilter": {
get: function() {
return this._minFilter
},
set: function(v) {
this.bind()
var gl = this.gl
if(this.type === gl.FLOAT && linearTypes.indexOf(v) >= 0) {
if(!webglew(gl).OES_texture_float_linear) {
v = gl.NEAREST
}
}
if(filterTypes.indexOf(v) < 0) {
throw new Error("gl-texture2d: Unknown filter mode " + v)
}
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, v)
return this._minFilter = v
}
},
set: function(v) {
this.bind()
var gl = this.gl
if(this.type === gl.FLOAT && linearTypes.indexOf(v) >= 0) {
if(!webglew(gl).OES_texture_float_linear) {
v = gl.NEAREST
"magFilter": {
get: function() {
return this._magFilter
},
set: function(v) {
this.bind()
var gl = this.gl
if(this.type === gl.FLOAT && linearTypes.indexOf(v) >= 0) {
if(!webglew(gl).OES_texture_float_linear) {
v = gl.NEAREST
}
}
if(filterTypes.indexOf(v) < 0) {
throw new Error("gl-texture2d: Unknown filter mode " + v)
}
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, v)
return this._magFilter = v
}
if(filterTypes.indexOf(v) < 0) {
throw new Error("gl-texture2d: Unknown filter mode " + v)
}
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, v)
return this._magFilter = v
}
})
Object.defineProperty(proto, "wrapS", {
get: function() {
return this._wrapS
},
set: function(v) {
this.bind()
if(wrapTypes.indexOf(v) < 0) {
throw new Error("gl-texture2d: Unknown wrap mode " + v)
"mipSamples": {
get: function() {
return this._anisoSamples
},
set: function(i) {
var psamples = this._anisoSamples
this._anisoSamples = Math.max(i, 1)|0
if(psamples !== this._anisoSamples) {
var ext = webglew(this.gl).EXT_texture_filter_anisotropic
if(ext) {
this.gl.texParameterf(this.gl.TEXTURE_2D, ext.TEXTURE_MAX_ANISOTROPY_EXT, this._anisoSamples)
}
}
return this._anisoSamples
}
this.gl.texParameteri(this.gl.TEXTURE_2D, this.gl.TEXTURE_WRAP_S, v)
return this._wrapS = v
}
})
Object.defineProperty(proto, "wrapT", {
get: function() {
return this._wrapT
},
set: function(v) {
this.bind()
if(wrapTypes.indexOf(v) < 0) {
throw new Error("gl-texture2d: Unknown wrap mode " + v)
"wrapS": {
get: function() {
return this._wrapS
},
set: function(v) {
this.bind()
if(wrapTypes.indexOf(v) < 0) {
throw new Error("gl-texture2d: Unknown wrap mode " + v)
}
this.gl.texParameteri(this.gl.TEXTURE_2D, this.gl.TEXTURE_WRAP_S, v)
return this._wrapS = v
}
this.gl.texParameteri(this.gl.TEXTURE_2D, this.gl.TEXTURE_WRAP_T, v)
return this._wrapT = v
}
})
Object.defineProperty(proto, "wrap", {
get: function() {
return [this._wrapT, this._wrapS]
},
set: function(v) {
if(!Array.isArray(v)) {
v = [v,v]
}
if(v.length !== 2) {
throw new Error("gl-texture2d: Must specify wrap mode for rows and columns")
}
for(var i=0; i<2; ++i) {
if(wrapTypes.indexOf(v[i]) < 0) {
"wrapT": {
get: function() {
return this._wrapT
},
set: function(v) {
this.bind()
if(wrapTypes.indexOf(v) < 0) {
throw new Error("gl-texture2d: Unknown wrap mode " + v)
}
this.gl.texParameteri(this.gl.TEXTURE_2D, this.gl.TEXTURE_WRAP_T, v)
return this._wrapT = v
}
this._wrapT = v[0]
this._wrapS = v[1]
},
"wrap": {
get: function() {
return this._wrapVector
},
set: function(v) {
if(!Array.isArray(v)) {
v = [v,v]
}
if(v.length !== 2) {
throw new Error("gl-texture2d: Must specify wrap mode for rows and columns")
}
for(var i=0; i<2; ++i) {
if(wrapTypes.indexOf(v[i]) < 0) {
throw new Error("gl-texture2d: Unknown wrap mode " + v)
}
}
this._wrapS = v[0]
this._wrapT = v[1]
var gl = this.gl
this.bind()
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, this._wrapT)
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, this._wrapS)
var gl = this.gl
this.bind()
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, this._wrapS)
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, this._wrapT)
return v
}
})
Object.defineProperty(proto, "mipSamples", {
get: function() {
return this._anisoSamples
return v
}
},
set: function(i) {
var psamples = this._anisoSamples
this._anisoSamples = Math.max(i, 1)|0
if(psamples !== this._anisoSamples) {
var ext = webglew(this.gl).EXT_texture_filter_anisotropic
if(ext) {
this.gl.texParameterf(this.gl.TEXTURE_2D, ext.TEXTURE_MAX_ANISOTROPY_EXT, this._anisoSamples)
"shape": {
get: function() {
return this._shapeVector
},
set: function(x) {
if(!Array.isArray(x)) {
x = [x|0,x|0]
} else {
if(x.length !== 2) {
throw new Error("gl-texture2d: Invalid texture shape")
}
}
reshapeTexture(this, x[0]|0, x[1]|0)
return [x[0]|0, x[1]|0]
}
return this._anisoSamples
}
})
Object.defineProperty(proto, "shape", {
get: function() {
return this._shape
},
set: function(x) {
if(!Array.isArray(x)) {
x = [x|0,x|0]
} else {
if(x.length !== 2) {
throw new Error("gl-texture2d: Invalid texture shape")
}
},
"width": {
get: function() {
return this._shape[0]
},
set: function(w) {
w = w|0
reshapeTexture(this, w, this._shape[1])
return w
}
var r = x[0]|0
var c = x[1]|0
if(this.height === r && this.width === c) {
return x
},
"height": {
get: function() {
return this._shape[1]
},
set: function(h) {
h = h|0
reshapeTexture(this, this._shape[0], h)
return h
}
var gl = this.gl
var maxSize = gl.getParameter(gl.MAX_TEXTURE_SIZE)
if(c < 0 || c > maxSize || r < 0 || r > maxSize) {
throw new Error("gl-texture2d: Invalid texture size")
}
this._shape = [r, c]
this.bind()
gl.texImage2D(gl.TEXTURE_2D, 0, this.format, c, r, 0, this.format, this.type, null)
this._mipLevels = [0]
return x
}
})
proto.bind = function bindTexture2D(unit) {
proto.bind = function(unit) {
var gl = this.gl

@@ -213,3 +273,3 @@ if(unit !== undefined) {

proto.dispose = function disposeTexture2D() {
proto.dispose = function() {
this.gl.deleteTexture(this.handle)

@@ -236,4 +296,4 @@ }

mip_level = y_off
y_off = x_off[0]|0
x_off = x_off[1]|0
y_off = x_off[1]|0
x_off = x_off[0]|0
} else {

@@ -257,4 +317,4 @@ x_off = x_off || 0

if(data.shape.length < 2 ||
x_off + data.shape[1] > this._shape[1]>>>mip_level ||
y_off + data.shape[0] > this._shape[0]>>>mip_level ||
x_off + data.shape[0] > this._shape[0]>>>mip_level ||
y_off + data.shape[1] > this._shape[1]>>>mip_level ||
x_off < 0 ||

@@ -270,7 +330,21 @@ y_off < 0) {

function isPacked(shape, stride) {
if(shape.length === 3) {
return (stride[2] === 1) &&
(stride[1] === shape[0]*shape[2]) &&
(stride[0] === shape[2])
}
return (stride[0] === 1) &&
(stride[1] === shape[0])
}
function texSubImageArray(gl, x_off, y_off, mip_level, cformat, ctype, mipLevels, array) {
var dtype = array.dtype
var shape = array.shape
var packed = isPacked(array)
var shape = array.shape.slice()
if(shape.length < 2 || shape.length > 3) {
throw new Error("gl-texture2d: Invalid ndarray, must be 2d or 3d")
}
var type = 0, format = 0
var packed = isPacked(shape, array.stride.slice())
if(dtype === "float32") {

@@ -289,4 +363,6 @@ type = gl.FLOAT

}
var channels = 1
if(shape.length === 2) {
format = gl.LUMINANCE
shape = [shape[0], shape[1], 1]
} else if(shape.length === 3) {

@@ -304,2 +380,3 @@ if(shape[2] === 1) {

}
channels = shape[2]
} else {

@@ -325,11 +402,11 @@ throw new Error("gl-texture2d: Invalid shape for texture")

if(needsMip) {
gl.texImage2D(gl.TEXTURE_2D, mip_level, cformat, shape[1], shape[0], 0, cformat, ctype, array.data)
gl.texImage2D(gl.TEXTURE_2D, mip_level, cformat, shape[0], shape[1], 0, cformat, ctype, array.data)
} else {
gl.texSubImage2D(gl.TEXTURE_2D, mip_level, x_off, y_off, shape[1], shape[0], cformat, ctype, array.data)
gl.texSubImage2D(gl.TEXTURE_2D, mip_level, x_off, y_off, shape[0], shape[1], cformat, ctype, array.data)
}
} else {
if(needsMip) {
gl.texImage2D(gl.TEXTURE_2D, mip_level, cformat, shape[1], shape[0], 0, cformat, ctype, array.data.subarray(array.offset, array.offset+size))
gl.texImage2D(gl.TEXTURE_2D, mip_level, cformat, shape[0], shape[1], 0, cformat, ctype, array.data.subarray(array.offset, array.offset+size))
} else {
gl.texSubImage2D(gl.TEXTURE_2D, mip_level, x_off, y_off, shape[1], shape[0], cformat, ctype, array.data.subarray(array.offset, array.offset+size))
gl.texSubImage2D(gl.TEXTURE_2D, mip_level, x_off, y_off, shape[0], shape[1], cformat, ctype, array.data.subarray(array.offset, array.offset+size))
}

@@ -345,3 +422,3 @@ }

}
var pack_view = ndarray(pack_buffer, shape)
var pack_view = ndarray(pack_buffer, shape, [shape[2], shape[2]*shape[0], 1])
if(type === gl.FLOAT && ctype === gl.UNSIGNED_BYTE) {

@@ -353,5 +430,5 @@ convertFloatToUint8(pack_view, array)

if(needsMip) {
gl.texImage2D(gl.TEXTURE_2D, mip_level, cformat, shape[1], shape[0], 0, cformat, ctype, pack_buffer.subarray(0, size))
gl.texImage2D(gl.TEXTURE_2D, mip_level, cformat, shape[0], shape[1], 0, cformat, ctype, pack_buffer.subarray(0, size))
} else {
gl.texSubImage2D(gl.TEXTURE_2D, mip_level, x_off, y_off, shape[1], shape[0], cformat, ctype, pack_buffer.subarray(0, size))
gl.texSubImage2D(gl.TEXTURE_2D, mip_level, x_off, y_off, shape[0], shape[1], cformat, ctype, pack_buffer.subarray(0, size))
}

@@ -392,15 +469,2 @@ if(ctype === gl.FLOAT) {

function isPacked(array) {
var shape = array.shape
var stride = array.stride
var s = 1
for(var i=shape.length-1; i>=0; --i) {
if(stride[i] !== s) {
return false
}
s *= shape[i]
}
return true
}
//Creates a texture from an ndarray

@@ -414,4 +478,3 @@ function createTextureArray(gl, array) {

}
var packed = isPacked(array)
var packed = isPacked(shape, array.stride.slice())
var type = 0

@@ -434,2 +497,3 @@ if(dtype === "float32") {

format = gl.LUMINANCE
shape = [shape[0], shape[1], 1]
} else if(shape.length === 3) {

@@ -455,11 +519,7 @@ if(shape[2] === 1) {

var buffer, buf_store
var size = array.size
if(!packed) {
var sz = 1
var stride = new Array(shape.length)
for(var i=shape.length-1; i>=0; --i) {
stride[i] = sz
sz *= shape[i]
}
buf_store = pool.malloc(sz, dtype)
var buf_array = ndarray(buf_store, array.shape, stride, 0)
var stride = [shape[2], shape[2]*shape[0], 1]
buf_store = pool.malloc(size, dtype)
var buf_array = ndarray(buf_store, shape, stride, 0)
if((dtype === "float32" || dtype === "float64") && type === gl.UNSIGNED_BYTE) {

@@ -470,6 +530,7 @@ convertFloatToUint8(buf_array, array)

}
buffer = buf_store.subarray(0, sz)
buffer = buf_store.subarray(0, size)
} else if (array.offset === 0 && array.data.length === size) {
buffer = array.data
} else {
var array_size = array.size
buffer = array.data.subarray(array.offset, array.offset + array_size)
buffer = array.data.subarray(array.offset, array.offset + size)
}

@@ -495,3 +556,3 @@ var tex = initTexture(gl)

if(Array.isArray(arguments[1])) {
return createTextureShape(gl, arguments[1][1]|0, arguments[1][0]|0, arguments[2]||gl.RGBA, arguments[4]||gl.UNSIGNED_BYTE)
return createTextureShape(gl, arguments[1][0]|0, arguments[1][1]|0, arguments[2]||gl.RGBA, arguments[4]||gl.UNSIGNED_BYTE)
}

@@ -510,3 +571,2 @@ if(typeof arguments[1] === "object") {

throw new Error("Invalid arguments for texture2d constructor")
}
module.exports = createTexture2D
}
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc