Skip to content

Commit 38ccba8

Browse files
committed
texture: add internalFormat support
1 parent 0f6051f commit 38ccba8

File tree

5 files changed

+72
-8
lines changed

5 files changed

+72
-8
lines changed

examples/webgl2_materials_texture3d_volume.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,8 @@
126126
// Also see https://www.khronos.org/registry/webgl/specs/latest/2.0/#TEXTURE_TYPES_FORMATS_FROM_DOM_ELEMENTS_TABLE
127127
// TODO: look the dtype up in the volume metadata
128128
var texture = new THREE.DataTexture3D( volume.data, volume.xLength, volume.yLength, volume.zLength );
129-
texture.format = THREE.RedFormat;
129+
texture.format = THREE.RedFormat;
130+
texture.internalFormat = THREE.R32Format;
130131
texture.type = THREE.FloatType;
131132
texture.minFilter = texture.magFilter = THREE.LinearFilter;
132133
texture.unpackAlignment = 1;

src/constants.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,29 @@ export var RGBEFormat = RGBAFormat;
9595
export var DepthFormat = 1026;
9696
export var DepthStencilFormat = 1027;
9797
export var RedFormat = 1028;
98+
99+
export var RedIntegerFormat = 1029;
100+
export var R8Format = 1030;
101+
export var R8UIFormat = 1031;
102+
export var R16FFormat = 1032;
103+
export var R32FFormat = 1033;
104+
export var RGFormat = 1034;
105+
export var RGIntegerFormat = 1035;
106+
export var RGBIntegerFormat = 1036;
107+
export var RGBAIntegerFormat = 1037;
108+
export var RG8Format = 1038;
109+
export var RG8UIFormat = 1039;
110+
export var RG16FFormat = 1040;
111+
export var RG32FFormat = 1041;
112+
export var RGB8Format = 1042;
113+
export var RGB8UIFormat = 1042;
114+
export var RGB16FFormat = 1043;
115+
export var RGB32FFormat = 1044;
116+
export var RGBA8Format = 1045;
117+
export var RGBA8UIFormat = 1046;
118+
export var RGBA16FFormat = 1047;
119+
export var RGBA32FFormat = 1048;
120+
98121
export var RGB_S3TC_DXT1_Format = 33776;
99122
export var RGBA_S3TC_DXT1_Format = 33777;
100123
export var RGBA_S3TC_DXT3_Format = 33778;

src/renderers/webgl/WebGLTextures.js

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,13 @@ function WebGLTextures( _gl, extensions, state, properties, capabilities, utils,
101101

102102
}
103103

104-
function getInternalFormat( glFormat, glType ) {
104+
function getInternalFormat( internalFormat, glFormat, glType ) {
105105

106106
if ( ! capabilities.isWebGL2 ) return glFormat;
107107

108+
// The user provided an internal format, no need to apply deduction.
109+
if ( internalFormat ) return utils.convert( internalFormat );
110+
108111
var internalFormat = glFormat;
109112

110113
if ( glFormat === _gl.RED ) {
@@ -357,8 +360,9 @@ function WebGLTextures( _gl, extensions, state, properties, capabilities, utils,
357360
var image = cubeImage[ 0 ],
358361
isPowerOfTwoImage = isPowerOfTwo( image ),
359362
glFormat = utils.convert( texture.format ),
363+
internalFormat = texture.internalFormat,
360364
glType = utils.convert( texture.type ),
361-
glInternalFormat = getInternalFormat( glFormat, glType );
365+
glInternalFormat = getInternalFormat( internalFormat, glFormat, glType );
362366

363367
setTextureParameters( _gl.TEXTURE_CUBE_MAP, texture, isPowerOfTwoImage );
364368

@@ -541,8 +545,9 @@ function WebGLTextures( _gl, extensions, state, properties, capabilities, utils,
541545

542546
var isPowerOfTwoImage = isPowerOfTwo( image ),
543547
glFormat = utils.convert( texture.format ),
548+
internalFormat = texture.internalFormat,
544549
glType = utils.convert( texture.type ),
545-
glInternalFormat = getInternalFormat( glFormat, glType );
550+
glInternalFormat = getInternalFormat( internalFormat, glFormat, glType );
546551

547552
setTextureParameters( textureType, texture, isPowerOfTwoImage );
548553

@@ -709,8 +714,9 @@ function WebGLTextures( _gl, extensions, state, properties, capabilities, utils,
709714
function setupFrameBufferTexture( framebuffer, renderTarget, attachment, textureTarget ) {
710715

711716
var glFormat = utils.convert( renderTarget.texture.format );
717+
var internalFormat = renderTarget.texture.internalFormat;
712718
var glType = utils.convert( renderTarget.texture.type );
713-
var glInternalFormat = getInternalFormat( glFormat, glType );
719+
var glInternalFormat = getInternalFormat( internalFormat, glFormat, glType );
714720
state.texImage2D( textureTarget, 0, glInternalFormat, renderTarget.width, renderTarget.height, 0, glFormat, glType, null );
715721
_gl.bindFramebuffer( _gl.FRAMEBUFFER, framebuffer );
716722
_gl.framebufferTexture2D( _gl.FRAMEBUFFER, attachment, textureTarget, properties.get( renderTarget.texture ).__webglTexture, 0 );
@@ -759,8 +765,9 @@ function WebGLTextures( _gl, extensions, state, properties, capabilities, utils,
759765
} else {
760766

761767
var glFormat = utils.convert( renderTarget.texture.format );
768+
var internalFormat = renderTarget.texture.internalFormat;
762769
var glType = utils.convert( renderTarget.texture.type );
763-
var glInternalFormat = getInternalFormat( glFormat, glType );
770+
var glInternalFormat = getInternalFormat( internalFormat, glFormat, glType );
764771

765772
if ( isMultisample ) {
766773

@@ -907,8 +914,9 @@ function WebGLTextures( _gl, extensions, state, properties, capabilities, utils,
907914

908915
_gl.bindRenderbuffer( _gl.RENDERBUFFER, renderTargetProperties.__webglColorRenderbuffer );
909916
var glFormat = utils.convert( renderTarget.texture.format );
917+
var internalFormat = renderTarget.texture.internalFormat;
910918
var glType = utils.convert( renderTarget.texture.type );
911-
var glInternalFormat = getInternalFormat( glFormat, glType );
919+
var glInternalFormat = getInternalFormat( internalFormat, glFormat, glType );
912920
var samples = getRenderTargetSamples( renderTarget );
913921
_gl.renderbufferStorageMultisample( _gl.RENDERBUFFER, samples, glInternalFormat, renderTarget.width, renderTarget.height );
914922

src/renderers/webgl/WebGLUtils.js

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* @author thespite / http://www.twitter.com/thespite
33
*/
44

5-
import { MaxEquation, MinEquation, RGBA_ASTC_4x4_Format, RGBA_ASTC_5x4_Format, RGBA_ASTC_5x5_Format, RGBA_ASTC_6x5_Format, RGBA_ASTC_6x6_Format, RGBA_ASTC_8x5_Format, RGBA_ASTC_8x6_Format, RGBA_ASTC_8x8_Format, RGBA_ASTC_10x5_Format, RGBA_ASTC_10x6_Format, RGBA_ASTC_10x8_Format, RGBA_ASTC_10x10_Format, RGBA_ASTC_12x10_Format, RGBA_ASTC_12x12_Format, RGB_ETC1_Format, RGBA_PVRTC_2BPPV1_Format, RGBA_PVRTC_4BPPV1_Format, RGB_PVRTC_2BPPV1_Format, RGB_PVRTC_4BPPV1_Format, RGBA_S3TC_DXT5_Format, RGBA_S3TC_DXT3_Format, RGBA_S3TC_DXT1_Format, RGB_S3TC_DXT1_Format, SrcAlphaSaturateFactor, OneMinusDstColorFactor, DstColorFactor, OneMinusDstAlphaFactor, DstAlphaFactor, OneMinusSrcAlphaFactor, SrcAlphaFactor, OneMinusSrcColorFactor, SrcColorFactor, OneFactor, ZeroFactor, ReverseSubtractEquation, SubtractEquation, AddEquation, DepthFormat, DepthStencilFormat, LuminanceAlphaFormat, LuminanceFormat, RedFormat, RGBAFormat, RGBFormat, AlphaFormat, HalfFloatType, FloatType, UnsignedIntType, IntType, UnsignedShortType, ShortType, ByteType, UnsignedInt248Type, UnsignedShort565Type, UnsignedShort5551Type, UnsignedShort4444Type, UnsignedByteType, LinearMipMapLinearFilter, LinearMipMapNearestFilter, LinearFilter, NearestMipMapLinearFilter, NearestMipMapNearestFilter, NearestFilter, MirroredRepeatWrapping, ClampToEdgeWrapping, RepeatWrapping } from '../../constants.js';
5+
import { MaxEquation, MinEquation, RGBA_ASTC_4x4_Format, RGBA_ASTC_5x4_Format, RGBA_ASTC_5x5_Format, RGBA_ASTC_6x5_Format, RGBA_ASTC_6x6_Format, RGBA_ASTC_8x5_Format, RGBA_ASTC_8x6_Format, RGBA_ASTC_8x8_Format, RGBA_ASTC_10x5_Format, RGBA_ASTC_10x6_Format, RGBA_ASTC_10x8_Format, RGBA_ASTC_10x10_Format, RGBA_ASTC_12x10_Format, RGBA_ASTC_12x12_Format, RGB_ETC1_Format, RGBA_PVRTC_2BPPV1_Format, RGBA_PVRTC_4BPPV1_Format, RGB_PVRTC_2BPPV1_Format, RGB_PVRTC_4BPPV1_Format, RGBA_S3TC_DXT5_Format, RGBA_S3TC_DXT3_Format, RGBA_S3TC_DXT1_Format, RGB_S3TC_DXT1_Format, SrcAlphaSaturateFactor, OneMinusDstColorFactor, DstColorFactor, OneMinusDstAlphaFactor, DstAlphaFactor, OneMinusSrcAlphaFactor, SrcAlphaFactor, OneMinusSrcColorFactor, SrcColorFactor, OneFactor, ZeroFactor, ReverseSubtractEquation, SubtractEquation, AddEquation, DepthFormat, DepthStencilFormat, LuminanceAlphaFormat, LuminanceFormat, RedFormat, RGBAFormat, RGBFormat, AlphaFormat, HalfFloatType, FloatType, UnsignedIntType, IntType, UnsignedShortType, ShortType, ByteType, UnsignedInt248Type, UnsignedShort565Type, UnsignedShort5551Type, UnsignedShort4444Type, UnsignedByteType, LinearMipMapLinearFilter, LinearMipMapNearestFilter, LinearFilter, NearestMipMapLinearFilter, NearestMipMapNearestFilter, NearestFilter, MirroredRepeatWrapping, ClampToEdgeWrapping, RepeatWrapping, RedIntegerFormat, R8Format, R8UIFormat, R16FFormat, R32FFormat, RGFormat, RGIntegerFormat, RGBIntegerFormat, RGBAIntegerFormat, RG8Format, RG8UIFormat, RG16FFormat, RG32FFormat, RGB8Format, RGB8UIFormat, RGB16FFormat, RGB32FFormat, RGBA8Format, RGBA8UIFormat, RGBA16FFormat, RGBA32FFormat } from '../../constants.js';
66

77
function WebGLUtils( gl, extensions, capabilities ) {
88

@@ -52,6 +52,36 @@ function WebGLUtils( gl, extensions, capabilities ) {
5252
if ( p === DepthFormat ) return gl.DEPTH_COMPONENT;
5353
if ( p === DepthStencilFormat ) return gl.DEPTH_STENCIL;
5454
if ( p === RedFormat ) return gl.RED;
55+
if ( p === RedIntegerFormat ) return gl.RED_INTEGER;
56+
57+
if ( p === R8Format ) return gl.R8;
58+
if ( p === R8UIFormat ) return gl.R8UI;
59+
if ( p === R16FFormat ) return gl.R16F;
60+
if ( p === R32FFormat ) return gl.R32F;
61+
62+
if ( p === RGFormat ) return gl.RG;
63+
if ( p === RGIntegerFormat ) return gl.RG_INTEGER;
64+
65+
if ( p === RG8Format ) return gl.RG8;
66+
if ( p === RG8UIFormat ) return gl.RG8UI;
67+
if ( p === RG16FFormat ) return gl.RG16F;
68+
if ( p === RG32FFormat ) return gl.RG32F;
69+
70+
if ( p === RGBFormat ) return gl.RGB;
71+
if ( p === RGBIntegerFormat ) return gl.RGB_INTEGER;
72+
73+
if ( p === RGB8Format ) return gl.RGB8;
74+
if ( p === RGB8UIFormat ) return gl.RGB8UI;
75+
if ( p === RGB16FFormat ) return gl.RGB16F;
76+
if ( p === RGB32FFormat ) return gl.RGB32F;
77+
78+
if ( p === RGBAFormat ) return gl.RGBA;
79+
if ( p === RGBAIntegerFormat ) return gl.RGBA_INTEGER;
80+
81+
if ( p === RGBA8Format ) return gl.RGBA8;
82+
if ( p === RGBA8UIFormat ) return gl.RGBA8UI;
83+
if ( p === RGBA16FFormat ) return gl.RGBA16F;
84+
if ( p === RGBA32FFormat ) return gl.RGBA32F;
5585

5686
if ( p === AddEquation ) return gl.FUNC_ADD;
5787
if ( p === SubtractEquation ) return gl.FUNC_SUBTRACT;

src/textures/Texture.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ function Texture( image, mapping, wrapS, wrapT, magFilter, minFilter, format, ty
4545
this.anisotropy = anisotropy !== undefined ? anisotropy : 1;
4646

4747
this.format = format !== undefined ? format : RGBAFormat;
48+
this.internalFormat = undefined;
4849
this.type = type !== undefined ? type : UnsignedByteType;
4950

5051
this.offset = new Vector2( 0, 0 );
@@ -110,6 +111,7 @@ Texture.prototype = Object.assign( Object.create( EventDispatcher.prototype ), {
110111
this.anisotropy = source.anisotropy;
111112

112113
this.format = source.format;
114+
this.internalFormat = source.internalFormat;
113115
this.type = source.type;
114116

115117
this.offset.copy( source.offset );

0 commit comments

Comments
 (0)