Professional Documents
Culture Documents
GLTFLoader
GLTFLoader
THREE.GLTFLoader = ( function () {
this.dracoLoader = null;
this.ddsLoader = null;
this.pluginCallbacks = [];
this.register( function ( parser ) {
} );
GLTFLoader.prototype =
Object.assign( Object.create( THREE.Loader.prototype ), {
constructor: GLTFLoader,
var resourcePath;
resourcePath = this.resourcePath;
resourcePath = this.path;
} else {
if ( onError ) {
onError( e );
} else {
console.error( e );
scope.manager.itemError( url );
scope.manager.itemEnd( url );
};
loader.setPath( this.path );
loader.setResponseType( 'arraybuffer' );
loader.setRequestHeader( this.requestHeader );
loader.setWithCredentials( true );
try {
onLoad( gltf );
scope.manager.itemEnd( url );
}, _onError );
} catch ( e ) {
_onError( e );
}, onProgress, _onError );
},
this.dracoLoader = dracoLoader;
return this;
},
this.ddsLoader = ddsLoader;
return this;
},
this.pluginCallbacks.push( callback );
return this;
},
return this;
},
var content;
var extensions = {};
var plugins = {};
content = data;
} else {
try {
} catch ( error ) {
if ( onError ) onError( error );
return;
content =
extensions[ EXTENSIONS.KHR_BINARY_GLTF ].content;
} else {
} );
parser.fileLoader.setRequestHeader( this.requestHeader );
if ( json.extensionsUsed ) {
case EXTENSIONS.KHR_LIGHTS_PUNCTUAL:
extensions[ extensionName ] = new
GLTFLightsExtension( json );
break;
case EXTENSIONS.KHR_MATERIALS_UNLIT:
extensions[ extensionName ] = new
GLTFMaterialsUnlitExtension();
break;
case
EXTENSIONS.KHR_MATERIALS_PBR_SPECULAR_GLOSSINESS:
extensions[ extensionName ] = new
GLTFMaterialsPbrSpecularGlossinessExtension();
break;
case EXTENSIONS.KHR_DRACO_MESH_COMPRESSION:
extensions[ extensionName ] = new
GLTFDracoMeshCompressionExtension( json, this.dracoLoader );
break;
case EXTENSIONS.MSFT_TEXTURE_DDS:
extensions[ extensionName ] = new
GLTFTextureDDSExtension( this.ddsLoader );
break;
case EXTENSIONS.KHR_TEXTURE_TRANSFORM:
extensions[ extensionName ] = new
GLTFTextureTransformExtension();
break;
case EXTENSIONS.KHR_MESH_QUANTIZATION:
extensions[ extensionName ] = new
GLTFMeshQuantizationExtension();
break;
default:
if
( extensionsRequired.indexOf( extensionName ) >= 0 && plugins[ extensionName ] ===
undefined ) {
console.warn( 'THREE.GLTFLoader:
Unknown extension "' + extensionName + '".' );
parser.setExtensions( extensions );
parser.setPlugins( plugins );
parser.parse( onLoad, onError );
}
} );
/* GLTFREGISTRY */
function GLTFRegistry() {
return {
},
},
},
removeAll: function () {
objects = {};
};
/*********************************/
/********** EXTENSIONS ***********/
/*********************************/
var EXTENSIONS = {
KHR_BINARY_GLTF: 'KHR_binary_glTF',
KHR_DRACO_MESH_COMPRESSION: 'KHR_draco_mesh_compression',
KHR_LIGHTS_PUNCTUAL: 'KHR_lights_punctual',
KHR_MATERIALS_CLEARCOAT: 'KHR_materials_clearcoat',
KHR_MATERIALS_PBR_SPECULAR_GLOSSINESS:
'KHR_materials_pbrSpecularGlossiness',
KHR_MATERIALS_UNLIT: 'KHR_materials_unlit',
KHR_TEXTURE_TRANSFORM: 'KHR_texture_transform',
KHR_MESH_QUANTIZATION: 'KHR_mesh_quantization',
MSFT_TEXTURE_DDS: 'MSFT_texture_dds'
};
/**
* DDS Texture Extension
*
* Specification:
https://github.com/KhronosGroup/glTF/tree/master/extensions/2.0/Vendor/MSFT_texture
_dds
*
*/
function GLTFTextureDDSExtension( ddsLoader ) {
if ( ! ddsLoader ) {
this.name = EXTENSIONS.MSFT_TEXTURE_DDS;
this.ddsLoader = ddsLoader;
/**
* Punctual Lights Extension
*
* Specification:
https://github.com/KhronosGroup/glTF/tree/master/extensions/2.0/Khronos/KHR_lights_
punctual
*/
function GLTFLightsExtension( json ) {
this.name = EXTENSIONS.KHR_LIGHTS_PUNCTUAL;
switch ( lightDef.type ) {
case 'directional':
lightNode = new THREE.DirectionalLight( color );
lightNode.target.position.set( 0, 0, - 1 );
lightNode.add( lightNode.target );
break;
case 'point':
lightNode = new THREE.PointLight( color );
lightNode.distance = range;
break;
case 'spot':
lightNode = new THREE.SpotLight( color );
lightNode.distance = range;
// Handle spotlight properties.
lightDef.spot = lightDef.spot || {};
lightDef.spot.innerConeAngle = lightDef.spot.innerConeAngle
!== undefined ? lightDef.spot.innerConeAngle : 0;
lightDef.spot.outerConeAngle = lightDef.spot.outerConeAngle
!== undefined ? lightDef.spot.outerConeAngle : Math.PI / 4.0;
lightNode.angle = lightDef.spot.outerConeAngle;
lightNode.penumbra = 1.0 - lightDef.spot.innerConeAngle /
lightDef.spot.outerConeAngle;
lightNode.target.position.set( 0, 0, - 1 );
lightNode.add( lightNode.target );
break;
default:
throw new Error( 'THREE.GLTFLoader: Unexpected light type,
"' + lightDef.type + '".' );
// Some lights (e.g. spot) default to a position other than the origin.
Reset the position
// here, because node-level parsing will only override position if
explicitly specified.
lightNode.position.set( 0, 0, 0 );
lightNode.decay = 2;
};
/**
* Unlit Materials Extension
*
* Specification:
https://github.com/KhronosGroup/glTF/tree/master/extensions/2.0/Khronos/KHR_materia
ls_unlit
*/
function GLTFMaterialsUnlitExtension() {
this.name = EXTENSIONS.KHR_MATERIALS_UNLIT;
GLTFMaterialsUnlitExtension.prototype.getMaterialType = function () {
return THREE.MeshBasicMaterial;
};
GLTFMaterialsUnlitExtension.prototype.extendParams = function
( materialParams, materialDef, parser ) {
if ( metallicRoughness ) {
if ( Array.isArray( metallicRoughness.baseColorFactor ) ) {
materialParams.color.fromArray( array );
materialParams.opacity = array[ 3 ];
};
/**
* Clearcoat Materials Extension
*
* Specification:
https://github.com/KhronosGroup/glTF/tree/master/extensions/2.0/Khronos/KHR_materia
ls_clearcoat
*/
function GLTFMaterialsClearcoatExtension( parser ) {
this.parser = parser;
this.name = EXTENSIONS.KHR_MATERIALS_CLEARCOAT;
GLTFMaterialsClearcoatExtension.prototype.getMaterialType = function ( /*
materialIndex */ ) {
return THREE.MeshPhysicalMaterial;
};
GLTFMaterialsClearcoatExtension.prototype.extendMaterialParams = function
( materialIndex, materialParams ) {
if ( ! materialDef.extensions || !
materialDef.extensions[ this.name ] ) {
return Promise.resolve();
materialParams.clearcoat = extension.clearcoatFactor;
materialParams.clearcoatRoughness =
extension.clearcoatRoughnessFactor;
materialParams.clearcoatNormalScale = new
THREE.Vector2( scale, scale );
};
/* BINARY EXTENSION */
var BINARY_EXTENSION_HEADER_MAGIC = 'glTF';
var BINARY_EXTENSION_HEADER_LENGTH = 12;
var BINARY_EXTENSION_CHUNK_TYPES = { JSON: 0x4E4F534A, BIN: 0x004E4942 };
this.name = EXTENSIONS.KHR_BINARY_GLTF;
this.content = null;
this.body = null;
this.header = {
magic: THREE.LoaderUtils.decodeText( new
Uint8Array( data.slice( 0, 4 ) ) ),
version: headerView.getUint32( 4, true ),
length: headerView.getUint32( 8, true )
};
chunkIndex += chunkLength;
/**
* DRACO Mesh Compression Extension
*
* Specification:
https://github.com/KhronosGroup/glTF/tree/master/extensions/2.0/Khronos/KHR_draco_m
esh_compression
*/
function GLTFDracoMeshCompressionExtension( json, dracoLoader ) {
if ( ! dracoLoader ) {
this.name = EXTENSIONS.KHR_DRACO_MESH_COMPRESSION;
this.json = json;
this.dracoLoader = dracoLoader;
this.dracoLoader.preload();
GLTFDracoMeshCompressionExtension.prototype.decodePrimitive = function
( primitive, parser ) {
threeAttributeMap[ threeAttributeName ] =
gltfAttributeMap[ attributeName ];
}
for ( attributeName in primitive.attributes ) {
var accessorDef =
json.accessors[ primitive.attributes[ attributeName ] ];
var componentType =
WEBGL_COMPONENT_TYPES[ accessorDef.componentType ];
var attribute =
geometry.attributes[ attributeName ];
var normalized =
attributeNormalizedMap[ attributeName ];
resolve( geometry );
}, threeAttributeMap, attributeTypeMap );
} );
} );
};
/**
* Texture Transform Extension
*
* Specification:
https://github.com/KhronosGroup/glTF/tree/master/extensions/2.0/Khronos/KHR_texture
_transform
*/
function GLTFTextureTransformExtension() {
this.name = EXTENSIONS.KHR_TEXTURE_TRANSFORM;
texture = texture.clone();
texture.offset.fromArray( transform.offset );
texture.rotation = transform.rotation;
texture.repeat.fromArray( transform.scale );
texture.needsUpdate = true;
return texture;
};
/**
* Specular-Glossiness Extension
*
* Specification:
https://github.com/KhronosGroup/glTF/tree/master/extensions/2.0/Khronos/KHR_materia
ls_pbrSpecularGlossiness
*/
/**
* A sub class of THREE.StandardMaterial with some of the functionality
* changed via the `onBeforeCompile` callback
* @pailhead
*/
THREE.MeshStandardMaterial.call( this );
this.isGLTFSpecularGlossinessMaterial = true;
var glossinessMapParsFragmentChunk = [
'#ifdef USE_GLOSSINESSMAP',
' uniform sampler2D glossinessMap;',
'#endif'
].join( '\n' );
var specularMapFragmentChunk = [
'vec3 specularFactor = specular;',
'#ifdef USE_SPECULARMAP',
' vec4 texelSpecular = texture2D( specularMap, vUv );',
' texelSpecular = sRGBToLinear( texelSpecular );',
' // reads channel RGB, compatible with a glTF Specular-
Glossiness (RGBA) texture',
' specularFactor *= texelSpecular.rgb;',
'#endif'
].join( '\n' );
var glossinessMapFragmentChunk = [
'float glossinessFactor = glossiness;',
'#ifdef USE_GLOSSINESSMAP',
' vec4 texelGlossiness = texture2D( glossinessMap, vUv );',
' // reads channel A, compatible with a glTF Specular-
Glossiness (RGBA) texture',
' glossinessFactor *= texelGlossiness.a;',
'#endif'
].join( '\n' );
var lightPhysicalFragmentChunk = [
'PhysicalMaterial material;',
'material.diffuseColor = diffuseColor.rgb;',
'vec3 dxy = max( abs( dFdx( geometryNormal ) ),
abs( dFdy( geometryNormal ) ) );',
'float geometryRoughness = max( max( dxy.x, dxy.y ), dxy.z );',
'material.specularRoughness = max( 1.0 - glossinessFactor, 0.0525
);// 0.0525 corresponds to the base mip of a 256 cubemap.',
'material.specularRoughness += geometryRoughness;',
'material.specularRoughness = min( material.specularRoughness,
1.0 );',
'material.specularColor = specularFactor.rgb;',
].join( '\n' );
var uniforms = {
specular: { value: new THREE.Color().setHex( 0xffffff ) },
glossiness: { value: 1 },
specularMap: { value: null },
glossinessMap: { value: null }
};
this._extraUniforms = uniforms;
// please see #14031 or #13198 for an alternate approach
this.onBeforeCompile = function ( shader ) {
};
/*eslint-disable*/
Object.defineProperties(
this,
{
specular: {
get: function () { return uniforms.specular.value; },
set: function ( v ) { uniforms.specular.value = v; }
},
specularMap: {
get: function () { return uniforms.specularMap.value;
},
set: function ( v ) { uniforms.specularMap.value = v;
}
},
glossiness: {
get: function () { return
uniforms.glossiness.value; },
set: function ( v ) { uniforms.glossiness.value =
v; }
},
glossinessMap: {
get: function () { return
uniforms.glossinessMap.value; },
set: function ( v ) {
uniforms.glossinessMap.value = v;
//how about something like this - @pailhead
if ( v ) {
this.defines.USE_GLOSSINESSMAP = '';
// set USE_ROUGHNESSMAP to enable vUv
this.defines.USE_ROUGHNESSMAP = '';
} else {
delete this.defines.USE_ROUGHNESSMAP;
delete this.defines.USE_GLOSSINESSMAP;
}
}
}
);
/*eslint-enable*/
delete this.metalness;
delete this.roughness;
delete this.metalnessMap;
delete this.roughnessMap;
this.setValues( params );
GLTFMeshStandardSGMaterial.prototype =
Object.create( THREE.MeshStandardMaterial.prototype );
GLTFMeshStandardSGMaterial.prototype.constructor =
GLTFMeshStandardSGMaterial;
};
function GLTFMaterialsPbrSpecularGlossinessExtension() {
return {
name: EXTENSIONS.KHR_MATERIALS_PBR_SPECULAR_GLOSSINESS,
specularGlossinessParams: [
'color',
'map',
'lightMap',
'lightMapIntensity',
'aoMap',
'aoMapIntensity',
'emissive',
'emissiveIntensity',
'emissiveMap',
'bumpMap',
'bumpScale',
'normalMap',
'normalMapType',
'displacementMap',
'displacementScale',
'displacementBias',
'specularMap',
'specular',
'glossinessMap',
'glossiness',
'alphaMap',
'envMap',
'envMapIntensity',
'refractionRatio',
],
getMaterialType: function () {
return GLTFMeshStandardSGMaterial;
},
var pbrSpecularGlossiness =
materialDef.extensions[ this.name ];
if ( Array.isArray( pbrSpecularGlossiness.diffuseFactor ) )
{
materialParams.color.fromArray( array );
materialParams.opacity = array[ 3 ];
if
( Array.isArray( pbrSpecularGlossiness.specularFactor ) ) {
materialParams.specular.fromArray( pbrSpecularGlossiness.specularFactor );
if ( pbrSpecularGlossiness.specularGlossinessTexture !==
undefined ) {
var specGlossMapDef =
pbrSpecularGlossiness.specularGlossinessTexture;
pending.push( parser.assignTexture( materialParams,
'glossinessMap', specGlossMapDef ) );
pending.push( parser.assignTexture( materialParams,
'specularMap', specGlossMapDef ) );
},
material.color = materialParams.color;
material.lightMap = null;
material.lightMapIntensity = 1.0;
material.emissive = materialParams.emissive;
material.emissiveIntensity = 1.0;
material.emissiveMap = materialParams.emissiveMap ===
undefined ? null : materialParams.emissiveMap;
if ( materialParams.normalScale ) material.normalScale =
materialParams.normalScale;
material.displacementMap = null;
material.displacementScale = 1;
material.displacementBias = 0;
material.alphaMap = null;
material.refractionRatio = 0.98;
return material;
},
};
/**
* Mesh Quantization Extension
*
* Specification:
https://github.com/KhronosGroup/glTF/tree/master/extensions/2.0/Khronos/KHR_mesh_qu
antization
*/
function GLTFMeshQuantizationExtension() {
this.name = EXTENSIONS.KHR_MESH_QUANTIZATION;
/*********************************/
/********** INTERPOLATION ********/
/*********************************/
// Spline Interpolation
// Specification:
https://github.com/KhronosGroup/glTF/blob/master/specification/2.0/README.md#append
ix-c-spline-interpolation
function GLTFCubicSplineInterpolant( parameterPositions, sampleValues,
sampleSize, resultBuffer ) {
GLTFCubicSplineInterpolant.prototype =
Object.create( THREE.Interpolant.prototype );
GLTFCubicSplineInterpolant.prototype.constructor =
GLTFCubicSplineInterpolant;
return result;
};
GLTFCubicSplineInterpolant.prototype.beforeStart_ =
GLTFCubicSplineInterpolant.prototype.copySampleValue_;
GLTFCubicSplineInterpolant.prototype.afterEnd_ =
GLTFCubicSplineInterpolant.prototype.copySampleValue_;
var td = t1 - t0;
var p = ( t - t0 ) / td;
var pp = p * p;
var ppp = pp * p;
return result;
};
/*********************************/
/********** 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_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_TYPE_SIZES = {
'SCALAR': 1,
'VEC2': 2,
'VEC3': 3,
'VEC4': 4,
'MAT2': 4,
'MAT3': 9,
'MAT4': 16
};
var ATTRIBUTES = {
POSITION: 'position',
NORMAL: 'normal',
TANGENT: 'tangent',
TEXCOORD_0: 'uv',
TEXCOORD_1: 'uv2',
COLOR_0: 'color',
WEIGHTS_0: 'skinWeight',
JOINTS_0: 'skinIndex',
};
var PATH_PROPERTIES = {
scale: 'scale',
translation: 'position',
rotation: 'quaternion',
weights: 'morphTargetInfluences'
};
var INTERPOLATION = {
CUBICSPLINE: undefined, // We use a custom interpolant
(GLTFCubicSplineInterpolation) for CUBICSPLINE tracks. Each
// keyframe track will be initialized with a
default interpolation type, then modified.
LINEAR: THREE.InterpolateLinear,
STEP: THREE.InterpolateDiscrete
};
var ALPHA_MODES = {
OPAQUE: 'OPAQUE',
MASK: 'MASK',
BLEND: 'BLEND'
};
var MIME_TYPE_FORMATS = {
'image/png': THREE.RGBAFormat,
'image/jpeg': THREE.RGBFormat
};
/* UTILITY FUNCTIONS */
// Invalid URL
if ( typeof url !== 'string' || url === '' ) return '';
// Data URI
if ( /^data:.*,.*$/i.test( url ) ) return url;
// Blob URL
if ( /^blob:.*$/i.test( url ) ) return url;
// Relative URL
return path + url;
/**
* Specification:
https://github.com/KhronosGroup/glTF/blob/master/specification/2.0/README.md#defaul
t-material
*/
function createDefaultMaterial( cache ) {
object.userData.gltfExtensions =
object.userData.gltfExtensions || {};
object.userData.gltfExtensions[ name ] =
objectDef.extensions[ name ];
}
}
/**
* @param {THREE.Object3D|THREE.Material|THREE.BufferGeometry} object
* @param {GLTF.definition} gltfDef
*/
function assignExtrasToUserData( object, gltfDef ) {
} else {
/**
* Specification:
https://github.com/KhronosGroup/glTF/blob/master/specification/2.0/README.md#morph-
targets
*
* @param {THREE.BufferGeometry} geometry
* @param {Array<GLTF.Target>} targets
* @param {GLTFParser} parser
* @return {Promise<THREE.BufferGeometry>}
*/
function addMorphTargets( geometry, targets, parser ) {
if ( hasMorphPosition ) {
pendingPositionAccessors.push( pendingAccessor );
if ( hasMorphNormal ) {
pendingNormalAccessors.push( pendingAccessor );
return Promise.all( [
Promise.all( pendingPositionAccessors ),
Promise.all( pendingNormalAccessors )
] ).then( function ( accessors ) {
if ( hasMorphPosition ) geometry.morphAttributes.position =
morphPositions;
if ( hasMorphNormal ) geometry.morphAttributes.normal =
morphNormals;
geometry.morphTargetsRelative = true;
return geometry;
} );
/**
* @param {THREE.Mesh} mesh
* @param {GLTF.Mesh} meshDef
*/
function updateMorphTargets( mesh, meshDef ) {
mesh.updateMorphTargets();
mesh.morphTargetDictionary = {};
mesh.morphTargetDictionary[ targetNames[ i ] ] = i;
} else {
if ( dracoExtension ) {
} else {
return geometryKey;
return attributesKey;
/* GLTF PARSER */
// BufferGeometry caching
this.primitiveCache = {};
this.textureLoader = new
THREE.ImageBitmapLoader( this.options.manager );
} else {
this.textureLoader = new
THREE.TextureLoader( this.options.manager );
this.textureLoader.setCrossOrigin( this.options.crossOrigin );
this.fileLoader.setWithCredentials( true );
}
}
this.extensions = extensions;
};
this.plugins = plugins;
};
Promise.all( [
this.getDependencies( 'scene' ),
this.getDependencies( 'animation' ),
this.getDependencies( 'camera' ),
var result = {
scene: dependencies[ 0 ][ json.scene || 0 ],
scenes: dependencies[ 0 ],
animations: dependencies[ 1 ],
cameras: dependencies[ 2 ],
asset: json.asset,
parser: parser,
userData: {}
};
onLoad( result );
} ).catch( onError );
};
/**
* Marks the special nodes/meshes in json for efficient parse.
*/
GLTFParser.prototype.markDefs = function () {
var nodeDefs = this.json.nodes || [];
var skinDefs = this.json.skins || [];
var meshDefs = this.json.meshes || [];
meshReferences[ nodeDef.mesh ] =
meshUses[ nodeDef.mesh ] = 0;
this.json.meshReferences = meshReferences;
this.json.meshUses = meshUses;
};
};
};
/**
* Requests the specified dependency asynchronously, with caching.
* @param {string} type
* @param {number} index
* @return {Promise<THREE.Object3D|THREE.Material|THREE.Texture|
THREE.AnimationClip|ArrayBuffer|Object>}
*/
GLTFParser.prototype.getDependency = function ( type, index ) {
if ( ! dependency ) {
switch ( type ) {
case 'scene':
dependency = this.loadScene( index );
break;
case 'node':
dependency = this.loadNode( index );
break;
case 'mesh':
dependency = this._invokeOne( function ( ext ) {
} );
break;
case 'accessor':
dependency = this.loadAccessor( index );
break;
case 'bufferView':
dependency = this._invokeOne( function ( ext ) {
} );
break;
case 'buffer':
dependency = this.loadBuffer( index );
break;
case 'material':
dependency = this._invokeOne( function ( ext ) {
} );
break;
case 'texture':
dependency = this.loadTexture( index );
break;
case 'skin':
dependency = this.loadSkin( index );
break;
case 'animation':
dependency = this.loadAnimation( index );
break;
case 'camera':
dependency = this.loadCamera( index );
break;
case 'light':
dependency =
this.extensions[ EXTENSIONS.KHR_LIGHTS_PUNCTUAL ].loadLight( index );
break;
default:
throw new Error( 'Unknown type: ' + type );
}
this.cache.add( cacheKey, dependency );
return dependency;
};
/**
* Requests all dependencies of the specified type asynchronously, with
caching.
* @param {string} type
* @return {Promise<Array<Object>>}
*/
GLTFParser.prototype.getDependencies = function ( type ) {
if ( ! dependencies ) {
} ) );
return dependencies;
};
/**
* Specification:
https://github.com/KhronosGroup/glTF/blob/master/specification/2.0/README.md#buffer
s-and-buffer-views
* @param {number} bufferIndex
* @return {Promise<ArrayBuffer>}
*/
GLTFParser.prototype.loadBuffer = function ( bufferIndex ) {
return
Promise.resolve( this.extensions[ EXTENSIONS.KHR_BINARY_GLTF ].body );
} );
} );
};
/**
* Specification:
https://github.com/KhronosGroup/glTF/blob/master/specification/2.0/README.md#buffer
s-and-buffer-views
* @param {number} bufferViewIndex
* @return {Promise<ArrayBuffer>}
*/
GLTFParser.prototype.loadBufferView = function ( bufferViewIndex ) {
} );
};
/**
* Specification:
https://github.com/KhronosGroup/glTF/blob/master/specification/2.0/README.md#access
ors
* @param {number} accessorIndex
* @return {Promise<THREE.BufferAttribute|THREE.InterleavedBufferAttribute>}
*/
GLTFParser.prototype.loadAccessor = function ( accessorIndex ) {
} else {
pendingBufferViews.push( null );
if ( ! ib ) {
parser.cache.add( ibCacheKey, ib );
} else {
} else {
//
https://github.com/KhronosGroup/glTF/blob/master/specification/2.0/README.md#sparse
-accessors
if ( accessorDef.sparse !== undefined ) {
var byteOffsetIndices =
accessorDef.sparse.indices.byteOffset || 0;
var byteOffsetValues = accessorDef.sparse.values.byteOffset
|| 0;
return bufferAttribute;
} );
};
/**
* Specification:
https://github.com/KhronosGroup/glTF/tree/master/specification/2.0#textures
* @param {number} textureIndex
* @return {Promise<THREE.Texture>}
*/
GLTFParser.prototype.loadTexture = function ( textureIndex ) {
var source;
if ( textureExtensions[ EXTENSIONS.MSFT_TEXTURE_DDS ] ) {
source =
json.images[ textureExtensions[ EXTENSIONS.MSFT_TEXTURE_DDS ].source ];
} else {
isObjectURL = true;
var blob = new Blob( [ bufferView ], { type:
source.mimeType } );
sourceURI = URL.createObjectURL( blob );
return sourceURI;
} );
if ( ! loader ) {
};
}
} );
URL.revokeObjectURL( sourceURI );
texture.flipY = false;
parser.associations.set( texture, {
type: 'textures',
index: textureIndex
} );
return texture;
} );
};
/**
* Asynchronously assigns a texture to the given material parameters.
* @param {Object} materialParams
* @param {string} mapName
* @param {Object} mapDef
* @return {Promise}
*/
GLTFParser.prototype.assignTexture = function ( materialParams, mapName,
mapDef ) {
if ( ! texture.isCompressedTexture ) {
switch ( mapName ) {
case 'aoMap':
case 'emissiveMap':
case 'metalnessMap':
case 'normalMap':
case 'roughnessMap':
texture.format = THREE.RGBFormat;
break;
if ( parser.extensions[ EXTENSIONS.KHR_TEXTURE_TRANSFORM ] ) {
if ( transform ) {
var gltfReference =
parser.associations.get( texture );
texture =
parser.extensions[ EXTENSIONS.KHR_TEXTURE_TRANSFORM ].extendTexture( texture,
transform );
parser.associations.set( texture, gltfReference );
} );
};
/**
* Assigns final material to a Mesh, Line, or Points instance. The instance
* already has a material (generated from the glTF material options alone)
* but reuse of the same glTF material may require multiple threejs materials
* to accomodate different primitive types, defines, etc. New materials will
* be created if necessary, and reused from a cache.
* @param {THREE.Object3D} mesh Mesh, Line, or Points instance.
*/
GLTFParser.prototype.assignFinalMaterial = function ( mesh ) {
if ( mesh.isPoints ) {
if ( ! pointsMaterial ) {
material = pointsMaterial;
} else if ( mesh.isLine ) {
if ( ! lineMaterial ) {
material = lineMaterial;
if ( material.isGLTFSpecularGlossinessMaterial ) cacheKey +=
'specular-glossiness:';
if ( useSkinning ) cacheKey += 'skinning:';
if ( useVertexTangents ) cacheKey += 'vertex-tangents:';
if ( useVertexColors ) cacheKey += 'vertex-colors:';
if ( useFlatShading ) cacheKey += 'flat-shading:';
if ( useMorphTargets ) cacheKey += 'morph-targets:';
if ( useMorphNormals ) cacheKey += 'morph-normals:';
if ( ! cachedMaterial ) {
cachedMaterial = material.clone();
this.associations.set( cachedMaterial,
this.associations.get( material ) );
material = cachedMaterial;
// https://github.com/mrdoob/three.js/issues/11438#issuecomment-
507003995
if ( material.normalScale && ! useVertexTangents ) {
material.normalScale.y = - material.normalScale.y;
material.clearcoatNormalScale.y = -
material.clearcoatNormalScale.y;
mesh.material = material;
};
return THREE.MeshStandardMaterial;
};
/**
* Specification:
https://github.com/KhronosGroup/glTF/blob/master/specification/2.0/README.md#materi
als
* @param {number} materialIndex
* @return {Promise<THREE.Material>}
*/
GLTFParser.prototype.loadMaterial = function ( materialIndex ) {
var materialType;
var materialParams = {};
var materialExtensions = materialDef.extensions || {};
if
( materialExtensions[ EXTENSIONS.KHR_MATERIALS_PBR_SPECULAR_GLOSSINESS ] ) {
var sgExtension =
extensions[ EXTENSIONS.KHR_MATERIALS_PBR_SPECULAR_GLOSSINESS ];
materialType = sgExtension.getMaterialType();
pending.push( sgExtension.extendParams( materialParams,
materialDef, parser ) );
} else {
// Specification:
//
https://github.com/KhronosGroup/glTF/tree/master/specification/2.0#metallic-
roughness-material
if ( Array.isArray( metallicRoughness.baseColorFactor ) ) {
materialParams.color.fromArray( array );
materialParams.opacity = array[ 3 ];
} );
} ) );
materialParams.side = THREE.DoubleSide;
}
materialParams.transparent = true;
// See: https://github.com/mrdoob/three.js/issues/17706
materialParams.depthWrite = false;
} else {
materialParams.transparent = false;
materialParams.normalScale.set( materialDef.normalTexture.scale,
materialDef.normalTexture.scale );
materialParams.aoMapIntensity =
materialDef.occlusionTexture.strength;
materialParams.emissive = new
THREE.Color().fromArray( materialDef.emissiveFactor );
var material;
material =
extensions[ EXTENSIONS.KHR_MATERIALS_PBR_SPECULAR_GLOSSINESS ].createMaterial( mate
rialParams );
} else {
if ( materialDef.extensions )
addUnknownExtensionsToUserData( extensions, material, materialDef );
return material;
} );
};
/**
* @param {THREE.BufferGeometry} geometry
* @param {GLTF.Primitive} primitiveDef
* @param {GLTFParser} parser
*/
function computeBounds( geometry, primitiveDef, parser ) {
var attributes = primitiveDef.attributes;
// glTF requires 'min' and 'max', but VRM (which extends glTF)
currently ignores that requirement.
box.set(
new THREE.Vector3( min[ 0 ], min[ 1 ], min[ 2 ] ),
new THREE.Vector3( max[ 0 ], max[ 1 ], max[ 2 ] ) );
} else {
return;
} else {
return;
} else {
geometry.boundingBox = box;
box.getCenter( sphere.center );
sphere.radius = box.min.distanceTo( box.max ) / 2;
geometry.boundingSphere = sphere;
/**
* @param {THREE.BufferGeometry} geometry
* @param {GLTF.Primitive} primitiveDef
* @param {GLTFParser} parser
* @return {Promise<THREE.BufferGeometry>}
*/
function addPrimitiveAttributes( geometry, primitiveDef, parser ) {
} );
geometry.setIndex( accessor );
} );
pending.push( accessor );
} );
/**
* @param {THREE.BufferGeometry} geometry
* @param {Number} drawMode
* @return {THREE.BufferGeometry}
*/
function toTrianglesDrawMode( geometry, drawMode ) {
var index = geometry.getIndex();
indices.push( i );
geometry.setIndex( indices );
index = geometry.getIndex();
} else {
console.error( 'THREE.GLTFLoader.toTrianglesDrawMode():
Undefined position attribute. Processing not possible.' );
return geometry;
//
// gl.TRIANGLE_FAN
newIndices.push( index.getX( 0 ) );
newIndices.push( index.getX( i ) );
newIndices.push( index.getX( i + 1 ) );
} else {
// gl.TRIANGLE_STRIP
if ( i % 2 === 0 ) {
newIndices.push( index.getX( i ) );
newIndices.push( index.getX( i + 1 ) );
newIndices.push( index.getX( i + 2 ) );
} else {
newIndices.push( index.getX( i + 2 ) );
newIndices.push( index.getX( i + 1 ) );
newIndices.push( index.getX( i ) );
return newGeometry;
/**
* Specification:
https://github.com/KhronosGroup/glTF/blob/master/specification/2.0/README.md#geomet
ry
*
* Creates BufferGeometries from primitives.
*
* @param {Array<GLTF.Primitive>} primitives
* @return {Promise<Array<THREE.BufferGeometry>>}
*/
GLTFParser.prototype.loadGeometries = function ( primitives ) {
} );
}
var pending = [];
if ( cached ) {
} else {
var geometryPromise;
if ( primitive.extensions &&
primitive.extensions[ EXTENSIONS.KHR_DRACO_MESH_COMPRESSION ] ) {
} else {
pending.push( geometryPromise );
};
/**
* Specification:
https://github.com/KhronosGroup/glTF/blob/master/specification/2.0/README.md#meshes
* @param {number} meshIndex
* @return {Promise<THREE.Group|THREE.Mesh|THREE.SkinnedMesh>}
*/
GLTFParser.prototype.loadMesh = function ( meshIndex ) {
pending.push( material );
// 1. create Mesh
var mesh;
if ( primitive.mode ===
WEBGL_CONSTANTS.TRIANGLE_STRIP ) {
mesh.geometry =
toTrianglesDrawMode( mesh.geometry, THREE.TriangleStripDrawMode );
mesh.geometry =
toTrianglesDrawMode( mesh.geometry, THREE.TriangleFanDrawMode );
} else {
parser.assignFinalMaterial( mesh );
meshes.push( mesh );
if ( meshes.length === 1 ) {
return meshes[ 0 ];
}
group.add( meshes[ i ] );
return group;
} );
};
/**
* Specification:
https://github.com/KhronosGroup/glTF/tree/master/specification/2.0#cameras
* @param {number} cameraIndex
* @return {Promise<THREE.Camera>}
*/
GLTFParser.prototype.loadCamera = function ( cameraIndex ) {
var camera;
var cameraDef = this.json.cameras[ cameraIndex ];
var params = cameraDef[ cameraDef.type ];
if ( ! params ) {
camera = new
THREE.PerspectiveCamera( THREE.MathUtils.radToDeg( params.yfov ),
params.aspectRatio || 1, params.znear || 1, params.zfar || 2e6 );
};
/**
* Specification:
https://github.com/KhronosGroup/glTF/tree/master/specification/2.0#skins
* @param {number} skinIndex
* @return {Promise<Object>}
*/
GLTFParser.prototype.loadSkin = function ( skinIndex ) {
skinEntry.inverseBindMatrices = accessor;
return skinEntry;
} );
};
/**
* Specification:
https://github.com/KhronosGroup/glTF/tree/master/specification/2.0#animations
* @param {number} animationIndex
* @return {Promise<THREE.AnimationClip>}
*/
GLTFParser.prototype.loadAnimation = function ( animationIndex ) {
return Promise.all( [
Promise.all( pendingNodes ),
Promise.all( pendingInputAccessors ),
Promise.all( pendingOutputAccessors ),
Promise.all( pendingSamplers ),
Promise.all( pendingTargets )
node.updateMatrix();
node.matrixAutoUpdate = true;
var TypedKeyframeTrack;
case PATH_PROPERTIES.weights:
TypedKeyframeTrack = THREE.NumberKeyframeTrack;
break;
case PATH_PROPERTIES.rotation:
TypedKeyframeTrack =
THREE.QuaternionKeyframeTrack;
break;
case PATH_PROPERTIES.position:
case PATH_PROPERTIES.scale:
default:
TypedKeyframeTrack = THREE.VectorKeyframeTrack;
break;
}
targetNames.push( object.name ?
object.name : object.uuid );
} );
} else {
targetNames.push( targetName );
if ( outputAccessor.normalized ) {
var scale;
scale = 1 / 127;
scale = 1 / 255;
scale = 1 / 32767;
scale = 1 / 65535;
} else {
throw new Error( 'THREE.GLTFLoader: Unsupported
output accessor component type.' );
outputArray = scaled;
track.createInterpolant = function
InterpolantFactoryMethodGLTFCubicSpline( result ) {
return new
GLTFCubicSplineInterpolant( this.times, this.values, this.getValueSize() / 3,
result );
};
// Mark as CUBICSPLINE.
`track.getInterpolation()` doesn't support custom interpolants.
track.createInterpolant.isInterpolantFactoryMethodGLTFCubicSpline = true;
tracks.push( track );
}
var name = animationDef.name ? animationDef.name : 'animation_' +
animationIndex;
} );
};
/**
* Specification:
https://github.com/KhronosGroup/glTF/tree/master/specification/2.0#nodes-and-
hierarchy
* @param {number} nodeIndex
* @return {Promise<THREE.Object3D>}
*/
GLTFParser.prototype.loadNode = function ( nodeIndex ) {
return ( function () {
var node;
node = mesh.clone();
node.name += '_instance_' + instanceNum;
} else {
node = mesh;
node.traverse( function ( o ) {
if ( ! o.isMesh ) return;
for ( var i = 0, il =
nodeDef.weights.length; i < il; i ++ ) {
o.morphTargetInfluences[ i ] =
nodeDef.weights[ i ];
} );
return node;
} ) );
if ( nodeDef.extensions
&& nodeDef.extensions[ EXTENSIONS.KHR_LIGHTS_PUNCTUAL ]
&&
nodeDef.extensions[ EXTENSIONS.KHR_LIGHTS_PUNCTUAL ].light !== undefined ) {
var node;
node = objects[ 0 ];
} else {
node.add( objects[ i ] );
if ( nodeDef.name ) {
node.userData.name = nodeDef.name;
node.name =
THREE.PropertyBinding.sanitizeNodeName( nodeDef.name );
if ( nodeDef.extensions )
addUnknownExtensionsToUserData( extensions, node, nodeDef );
} else {
node.position.fromArray( nodeDef.translation );
node.quaternion.fromArray( nodeDef.rotation );
node.scale.fromArray( nodeDef.scale );
return node;
} );
};
/**
* Specification:
https://github.com/KhronosGroup/glTF/tree/master/specification/2.0#scenes
* @param {number} sceneIndex
* @return {Promise<THREE.Group>}
*/
GLTFParser.prototype.loadScene = function () {
var skinEntry;
skinEntry = skin;
if ( ! mesh.isMesh ) return;
if ( jointNode ) {
bones.push( jointNode );
if
( skinEntry.inverseBindMatrices !== undefined ) {
mat.fromArray( skinEntry.inverseBindMatrices.array, j * 16 );
boneInverses.push( mat );
} else {
console.warn( 'THREE.GLTFLoader:
Joint "%s" could not be found.', skinEntry.joints[ j ] );
} );
return node;
} );
parentObject.add( node );
if ( nodeDef.children ) {
} );
}
if ( sceneDef.extensions )
addUnknownExtensionsToUserData( extensions, scene, sceneDef );
return scene;
} );
};
}();
return GLTFLoader;
} )();