Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move WEBGL_CONSTANTS and THREE_TO_WEBGL constants out from GLTFExporter and GLTFLoader #11978

Closed
wants to merge 8 commits into from
79 changes: 21 additions & 58 deletions examples/js/exporters/GLTFExporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,6 @@
* @author fernandojsg / http://fernandojsg.com
*/

//------------------------------------------------------------------------------
// Constants
//------------------------------------------------------------------------------
var WEBGL_CONSTANTS = {
POINTS: 0x0000,
LINES: 0x0001,
LINE_LOOP: 0x0002,
LINE_STRIP: 0x0003,
TRIANGLES: 0x0004,
TRIANGLE_STRIP: 0x0005,
TRIANGLE_FAN: 0x0006,

UNSIGNED_BYTE: 0x1401,
UNSIGNED_SHORT: 0x1403,
FLOAT: 0x1406,
UNSIGNED_INT: 0x1405,
ARRAY_BUFFER: 0x8892,
ELEMENT_ARRAY_BUFFER: 0x8893,

NEAREST: 0x2600,
LINEAR: 0x2601,
NEAREST_MIPMAP_NEAREST: 0x2700,
LINEAR_MIPMAP_NEAREST: 0x2701,
NEAREST_MIPMAP_LINEAR: 0x2702,
LINEAR_MIPMAP_LINEAR: 0x2703
};

var THREE_TO_WEBGL = {
// @TODO Replace with computed property name [THREE.*] when available on es6
1003: WEBGL_CONSTANTS.NEAREST,
1004: WEBGL_CONSTANTS.NEAREST_MIPMAP_NEAREST,
1005: WEBGL_CONSTANTS.NEAREST_MIPMAP_LINEAR,
1006: WEBGL_CONSTANTS.LINEAR,
1007: WEBGL_CONSTANTS.LINEAR_MIPMAP_NEAREST,
1008: WEBGL_CONSTANTS.LINEAR_MIPMAP_LINEAR
};

//------------------------------------------------------------------------------
// GLTF Exporter
//------------------------------------------------------------------------------
Expand Down Expand Up @@ -186,7 +149,7 @@ THREE.GLTFExporter.prototype = {
}

var offset = 0;
var componentSize = componentType === WEBGL_CONSTANTS.UNSIGNED_SHORT ? 2 : 4;
var componentSize = componentType === THREE.WebGLConstants.UNSIGNED_SHORT ? 2 : 4;

// Create a new dataview and dump the attribute's array into it
var byteLength = count * attribute.itemSize * componentSize;
Expand All @@ -199,15 +162,15 @@ THREE.GLTFExporter.prototype = {

var value = attribute.array[ i * attribute.itemSize + a ];

if ( componentType === WEBGL_CONSTANTS.FLOAT ) {
if ( componentType === THREE.WebGLConstants.FLOAT ) {

dataView.setFloat32( offset, value, true );

} else if ( componentType === WEBGL_CONSTANTS.UNSIGNED_INT ) {
} else if ( componentType === THREE.WebGLConstants.UNSIGNED_INT ) {

dataView.setUint8( offset, value, true );

} else if ( componentType === WEBGL_CONSTANTS.UNSIGNED_SHORT ) {
} else if ( componentType === THREE.WebGLConstants.UNSIGNED_SHORT ) {

dataView.setUint16( offset, value, true );

Expand All @@ -234,15 +197,15 @@ THREE.GLTFExporter.prototype = {
*/
function processBufferView( data, componentType, start, count ) {

var isVertexAttributes = componentType === WEBGL_CONSTANTS.FLOAT;
var isVertexAttributes = componentType === THREE.WebGLConstants.FLOAT;

if ( ! outputJSON.bufferViews ) {

outputJSON.bufferViews = [];

}

var componentSize = componentType === WEBGL_CONSTANTS.UNSIGNED_SHORT ? 2 : 4;
var componentSize = componentType === THREE.WebGLConstants.UNSIGNED_SHORT ? 2 : 4;

// Create a new dataview and dump the attribute's array into it
var byteLength = count * data.itemSize * componentSize;
Expand All @@ -253,7 +216,7 @@ THREE.GLTFExporter.prototype = {
byteOffset: byteOffset,
byteLength: byteLength,
byteStride: data.itemSize * componentSize,
target: isVertexAttributes ? WEBGL_CONSTANTS.ARRAY_BUFFER : WEBGL_CONSTANTS.ELEMENT_ARRAY_BUFFER
target: isVertexAttributes ? THREE.WebGLConstants.ARRAY_BUFFER : THREE.WebGLConstants.ELEMENT_ARRAY_BUFFER

};

Expand Down Expand Up @@ -300,15 +263,15 @@ THREE.GLTFExporter.prototype = {
// Detect the component type of the attribute array (float, uint or ushort)
if ( attribute.array.constructor === Float32Array ) {

componentType = WEBGL_CONSTANTS.FLOAT;
componentType = THREE.WebGLConstants.FLOAT;

} else if ( attribute.array.constructor === Uint32Array ) {

componentType = WEBGL_CONSTANTS.UNSIGNED_INT;
componentType = THREE.WebGLConstants.UNSIGNED_INT;

} else if ( attribute.array.constructor === Uint16Array ) {

componentType = WEBGL_CONSTANTS.UNSIGNED_SHORT;
componentType = THREE.WebGLConstants.UNSIGNED_SHORT;

} else {

Expand Down Expand Up @@ -405,10 +368,10 @@ THREE.GLTFExporter.prototype = {

var gltfSampler = {

magFilter: THREE_TO_WEBGL[ map.magFilter ],
minFilter: THREE_TO_WEBGL[ map.minFilter ],
wrapS: THREE_TO_WEBGL[ map.wrapS ],
wrapT: THREE_TO_WEBGL[ map.wrapT ]
magFilter: THREE.WebGLUtils.toGL( map.magFilter ),
minFilter: THREE.WebGLUtils.toGL( map.minFilter ),
wrapS: THREE.WebGLUtils.toGL( map.wrapS ),
wrapT: THREE.WebGLUtils.toGL( map.wrapT)

};

Expand Down Expand Up @@ -639,19 +602,19 @@ THREE.GLTFExporter.prototype = {
// Use the correct mode
if ( mesh instanceof THREE.LineSegments ) {

mode = WEBGL_CONSTANTS.LINES;
mode = THREE.WebGLConstants.LINES;

} else if ( mesh instanceof THREE.LineLoop ) {

mode = WEBGL_CONSTANTS.LINE_LOOP;
mode = THREE.WebGLConstants.LINE_LOOP;

} else if ( mesh instanceof THREE.Line ) {

mode = WEBGL_CONSTANTS.LINE_STRIP;
mode = THREE.WebGLConstants.LINE_STRIP;

} else if ( mesh instanceof THREE.Points ) {

mode = WEBGL_CONSTANTS.POINTS;
mode = THREE.WebGLConstants.POINTS;

} else {

Expand All @@ -666,15 +629,15 @@ THREE.GLTFExporter.prototype = {
if ( mesh.drawMode === THREE.TriangleFanDrawMode ) {

console.warn( 'GLTFExporter: TriangleFanDrawMode and wireframe incompatible.' );
mode = WEBGL_CONSTANTS.TRIANGLE_FAN;
mode = THREE.WebGLConstants.TRIANGLE_FAN;

} else if ( mesh.drawMode === THREE.TriangleStripDrawMode ) {

mode = mesh.material.wireframe ? WEBGL_CONSTANTS.LINE_STRIP : WEBGL_CONSTANTS.TRIANGLE_STRIP;
mode = mesh.material.wireframe ? THREE.WebGLConstants.LINE_STRIP : THREE.WebGLConstants.TRIANGLE_STRIP;

} else {

mode = mesh.material.wireframe ? WEBGL_CONSTANTS.LINES : WEBGL_CONSTANTS.TRIANGLES;
mode = mesh.material.wireframe ? THREE.WebGLConstants.LINES : THREE.WebGLConstants.TRIANGLES;

}

Expand Down
149 changes: 16 additions & 133 deletions examples/js/loaders/GLTFLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -836,124 +836,7 @@ THREE.GLTFLoader = ( function () {
/*********************************/
/********** INTERNALS ************/
/*********************************/

/* CONSTANTS */

var WEBGL_CONSTANTS = {
FLOAT: 5126,
//FLOAT_MAT2: 35674,
FLOAT_MAT3: 35675,
FLOAT_MAT4: 35676,
FLOAT_VEC2: 35664,
FLOAT_VEC3: 35665,
FLOAT_VEC4: 35666,
LINEAR: 9729,
REPEAT: 10497,
SAMPLER_2D: 35678,
POINTS: 0,
LINES: 1,
LINE_LOOP: 2,
LINE_STRIP: 3,
TRIANGLES: 4,
TRIANGLE_STRIP: 5,
TRIANGLE_FAN: 6,
UNSIGNED_BYTE: 5121,
UNSIGNED_SHORT: 5123
};

var WEBGL_TYPE = {
5126: Number,
//35674: THREE.Matrix2,
35675: THREE.Matrix3,
35676: THREE.Matrix4,
35664: THREE.Vector2,
35665: THREE.Vector3,
35666: THREE.Vector4,
35678: THREE.Texture
};

var WEBGL_COMPONENT_TYPES = {
5120: Int8Array,
5121: Uint8Array,
5122: Int16Array,
5123: Uint16Array,
5125: Uint32Array,
5126: Float32Array
};

var WEBGL_FILTERS = {
9728: THREE.NearestFilter,
9729: THREE.LinearFilter,
9984: THREE.NearestMipMapNearestFilter,
9985: THREE.LinearMipMapNearestFilter,
9986: THREE.NearestMipMapLinearFilter,
9987: THREE.LinearMipMapLinearFilter
};

var WEBGL_WRAPPINGS = {
33071: THREE.ClampToEdgeWrapping,
33648: THREE.MirroredRepeatWrapping,
10497: THREE.RepeatWrapping
};

var WEBGL_TEXTURE_FORMATS = {
6406: THREE.AlphaFormat,
6407: THREE.RGBFormat,
6408: THREE.RGBAFormat,
6409: THREE.LuminanceFormat,
6410: THREE.LuminanceAlphaFormat
};

var WEBGL_TEXTURE_DATATYPES = {
5121: THREE.UnsignedByteType,
32819: THREE.UnsignedShort4444Type,
32820: THREE.UnsignedShort5551Type,
33635: THREE.UnsignedShort565Type
};

var WEBGL_SIDES = {
1028: THREE.BackSide, // Culling front
1029: THREE.FrontSide // Culling back
//1032: THREE.NoSide // Culling front and back, what to do?
};

var WEBGL_DEPTH_FUNCS = {
512: THREE.NeverDepth,
513: THREE.LessDepth,
514: THREE.EqualDepth,
515: THREE.LessEqualDepth,
516: THREE.GreaterEqualDepth,
517: THREE.NotEqualDepth,
518: THREE.GreaterEqualDepth,
519: THREE.AlwaysDepth
};

var WEBGL_BLEND_EQUATIONS = {
32774: THREE.AddEquation,
32778: THREE.SubtractEquation,
32779: THREE.ReverseSubtractEquation
};

var WEBGL_BLEND_FUNCS = {
0: THREE.ZeroFactor,
1: THREE.OneFactor,
768: THREE.SrcColorFactor,
769: THREE.OneMinusSrcColorFactor,
770: THREE.SrcAlphaFactor,
771: THREE.OneMinusSrcAlphaFactor,
772: THREE.DstAlphaFactor,
773: THREE.OneMinusDstAlphaFactor,
774: THREE.DstColorFactor,
775: THREE.OneMinusDstColorFactor,
776: THREE.SrcAlphaSaturateFactor
// The followings are not supported by Three.js yet
//32769: CONSTANT_COLOR,
//32770: ONE_MINUS_CONSTANT_COLOR,
//32771: CONSTANT_ALPHA,
//32772: ONE_MINUS_CONSTANT_COLOR
};

var WEBGL_TYPE_SIZES = {
var WEBGL_TYPE_SIZES = {
'SCALAR': 1,
'VEC2': 2,
'VEC3': 3,
Expand Down Expand Up @@ -1467,7 +1350,7 @@ THREE.GLTFLoader = ( function () {
return parser.getDependency( 'bufferView', accessor.bufferView ).then( function ( bufferView ) {

var itemSize = WEBGL_TYPE_SIZES[ accessor.type ];
var TypedArray = WEBGL_COMPONENT_TYPES[ accessor.componentType ];
var TypedArray = THREE.WebGLUtils.fromGL[ accessor.componentType ];

// For VEC3: itemSize is 3, elementBytes is 4, itemBytes is 12.
var elementBytes = TypedArray.BYTES_PER_ELEMENT;
Expand Down Expand Up @@ -1561,24 +1444,24 @@ THREE.GLTFLoader = ( function () {

if ( textureDef.name !== undefined ) texture.name = textureDef.name;

texture.format = textureDef.format !== undefined ? WEBGL_TEXTURE_FORMATS[ textureDef.format ] : THREE.RGBAFormat;
texture.format = textureDef.format !== undefined ? THREE.WebGLUtils.fromGL[ textureDef.format ] : THREE.RGBAFormat;

if ( textureDef.internalFormat !== undefined && texture.format !== WEBGL_TEXTURE_FORMATS[ textureDef.internalFormat ] ) {
if ( textureDef.internalFormat !== undefined && texture.format !== THREE.WebGLUtils.fromGL[ textureDef.internalFormat ] ) {

console.warn( 'THREE.GLTFLoader: Three.js does not support texture internalFormat which is different from texture format. ' +
'internalFormat will be forced to be the same value as format.' );

}

texture.type = textureDef.type !== undefined ? WEBGL_TEXTURE_DATATYPES[ textureDef.type ] : THREE.UnsignedByteType;
texture.type = textureDef.type !== undefined ? THREE.WebGLUtils.fromGL[ textureDef.type ] : THREE.UnsignedByteType;

var samplers = json.samplers || {};
var sampler = samplers[ textureDef.sampler ] || {};

texture.magFilter = WEBGL_FILTERS[ sampler.magFilter ] || THREE.LinearFilter;
texture.minFilter = WEBGL_FILTERS[ sampler.minFilter ] || THREE.LinearMipMapLinearFilter;
texture.wrapS = WEBGL_WRAPPINGS[ sampler.wrapS ] || THREE.RepeatWrapping;
texture.wrapT = WEBGL_WRAPPINGS[ sampler.wrapT ] || THREE.RepeatWrapping;
texture.magFilter = THREE.WebGLUtils.fromGL[ sampler.magFilter ] || THREE.LinearFilter;
texture.minFilter = THREE.WebGLUtils.fromGL[ sampler.minFilter ] || THREE.LinearMipMapLinearFilter;
texture.wrapS = THREE.WebGLUtils.fromGL[ sampler.wrapS ] || THREE.RepeatWrapping;
texture.wrapT = THREE.WebGLUtils.fromGL[ sampler.wrapT ] || THREE.RepeatWrapping;

return texture;

Expand Down Expand Up @@ -1951,33 +1834,33 @@ THREE.GLTFLoader = ( function () {

var mesh;

if ( primitive.mode === WEBGL_CONSTANTS.TRIANGLES || primitive.mode === undefined ) {
if ( primitive.mode === THREE.WebGLConstants.TRIANGLES || primitive.mode === undefined ) {

mesh = new THREE.Mesh( geometry, material );

} else if ( primitive.mode === WEBGL_CONSTANTS.TRIANGLE_STRIP ) {
} else if ( primitive.mode === THREE.WebGLConstants.TRIANGLE_STRIP ) {

mesh = new THREE.Mesh( geometry, material );
mesh.drawMode = THREE.TriangleStripDrawMode;

} else if ( primitive.mode === WEBGL_CONSTANTS.TRIANGLE_FAN ) {
} else if ( primitive.mode === THREE.WebGLConstants.TRIANGLE_FAN ) {

mesh = new THREE.Mesh( geometry, material );
mesh.drawMode = THREE.TriangleFanDrawMode;

} else if ( primitive.mode === WEBGL_CONSTANTS.LINES ) {
} else if ( primitive.mode === THREE.WebGLConstants.LINES ) {

mesh = new THREE.LineSegments( geometry, material );

} else if ( primitive.mode === WEBGL_CONSTANTS.LINE_STRIP ) {
} else if ( primitive.mode === THREE.WebGLConstants.LINE_STRIP ) {

mesh = new THREE.Line( geometry, material );

} else if ( primitive.mode === WEBGL_CONSTANTS.LINE_LOOP ) {
} else if ( primitive.mode === THREE.WebGLConstants.LINE_LOOP ) {

mesh = new THREE.LineLoop( geometry, material );

} else if ( primitive.mode === WEBGL_CONSTANTS.POINTS ) {
} else if ( primitive.mode === THREE.WebGLConstants.POINTS ) {

mesh = new THREE.Points( geometry, material );

Expand Down
1 change: 1 addition & 0 deletions src/Three.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,5 +156,6 @@ export { Curve } from './extras/core/Curve.js';
export { ShapeUtils } from './extras/ShapeUtils.js';
export { SceneUtils } from './extras/SceneUtils.js';
export { WebGLUtils } from './renderers/webgl/WebGLUtils.js';
export { WebGLConstants } from './renderers/webgl/WebGLConstants.js';
export * from './constants.js';
export * from './Three.Legacy.js';
Loading