-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathUpdatableTexture.js
83 lines (62 loc) · 2.38 KB
/
UpdatableTexture.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
function UpdatableTexture( format, type, mapping, wrapS, wrapT, magFilter, minFilter, anisotropy, encoding ) {
THREE.Texture.call( this, null, mapping, wrapS, wrapT, magFilter, minFilter, format, type, anisotropy, encoding );
var canvas = document.createElement('canvas');
canvas.width = 1;
canvas.height = 1;
var ctx = canvas.getContext('2d');
var imageData = ctx.createImageData(1, 1);
this.image = imageData;
this.magFilter = magFilter !== undefined ? magFilter : THREE.LinearFilter;
this.minFilter = minFilter !== undefined ? minFilter : THREE.LinearMipMapLinearFilter;
this.generateMipmaps = true;
this.flipY = true;
this.unpackAlignment = 1;
this.needsUpdate = true;
}
UpdatableTexture.prototype = Object.create( THREE.Texture.prototype );
UpdatableTexture.prototype.constructor = UpdatableTexture;
UpdatableTexture.prototype.isUpdatableTexture = true;
UpdatableTexture.prototype.setRenderer = function( renderer ) {
this.renderer = renderer;
this.gl = this.renderer.getContext()
this.utils = THREE.WebGLUtils(this.gl, this.renderer.extensions)
}
UpdatableTexture.prototype.setSize = function( width, height ) {
if( width === this.width && height === this.height ) return;
var textureProperties = this.renderer.properties.get( this );
if( !textureProperties.__webglTexture ) return;
this.width = width;
this.height = height;
var activeTexture = this.gl.getParameter( this.gl.TEXTURE_BINDING_2D );
this.gl.bindTexture( this.gl.TEXTURE_2D, textureProperties.__webglTexture );
if( !textureProperties.__webglTexture ) this.width = null;
this.gl.texImage2D(
this.gl.TEXTURE_2D,
0,
this.utils.convert( this.format ),
width,
height,
0,
this.utils.convert( this.format ),
this.utils.convert( this.type ),
null
);
this.gl.bindTexture( this.gl.TEXTURE_2D, activeTexture );
}
UpdatableTexture.prototype.update = function( src, x, y ) {
var textureProperties = this.renderer.properties.get( this );
if( !textureProperties.__webglTexture ) return;
var activeTexture = this.gl.getParameter( this.gl.TEXTURE_BINDING_2D );
this.gl.bindTexture( this.gl.TEXTURE_2D, textureProperties.__webglTexture );
this.gl.texSubImage2D(
this.gl.TEXTURE_2D,
0,
x,
this.height - y - src.height,
this.utils.convert( this.format ),
this.utils.convert( this.type ),
src
);
this.gl.generateMipmap( this.gl.TEXTURE_2D );
this.gl.bindTexture( this.gl.TEXTURE_2D, activeTexture );
}