MaterialLoader
// 初始化一个加载器
const loader = new THREE.MaterialLoader();
// 加载资源
loader.load(
// 资源URL
'path/to/material.json',
// onLoad回调
function ( material ) {
object.material = material;
},
// onProgress回调
function ( xhr ) {
console.log( (xhr.loaded / xhr.total * 100) + '% loaded' );
},
// onError回调
function ( err ) {
console.log( 'An error happened' );
}
);
构造函数
MaterialLoader( manager : LoadingManager )
manager — 加载器使用的loadingManager,默认为THREE.DefaultLoadingManager.
创建要给新的MaterialLoader.
属性
共有属性请参见其基类Loader。
.textures : Object
持有材质的任何纹理的对象,请参考 .setTextures.
方法
共有方法请参见其基类Loader。
.load ( url : String, onLoad : Function, onProgress : Function, onError : Function ) : null
url — 文件的URL或者路径,也可以为 Data URI.
onLoad — 加载完成时将调用。回调参数为将要加载的Material。
onProgress — 将在加载过程中进行调用,参数为进度事件。
onError — 在加载错误时被调用。
从URL中进行加载,并返回将包含数据的Material对象。
.parse ( json : Object ) : Material
json — JSON对象包含材质参数。
解析JSON结构,以json对象的参数中的json.type类型,创建一个新的Material。
.setTextures ( textures : Object ) : null
textures — 对象包含任何被材质所使用的纹理。
ObjectLoader
此加载器内部使用FileLoader进行加载文件。
const loader = new THREE.ObjectLoader();
loader.load(
// 资源的URL
"models/json/example.json",
// onLoad回调
// Here the loaded data is assumed to be an object
function ( obj ) {
// Add the loaded object to the scene
scene.add( obj );
},
// onProgress回调
function ( xhr ) {
console.log( (xhr.loaded / xhr.total * 100) + '% loaded' );
},
// onError回调
function ( err ) {
console.error( 'An error happened' );
}
);
// 或者,解析先前加载的JSON结构
const object = loader.parse( a_json_object );
scene.add( object );
构造函数
ObjectLoader( manager : LoadingManager )
manager — 加载器所使用的loadingManager,默认值为THREE.DefaultLoadingManager.
创建一个新的ObjectLoader.
属性
共有属性请参见其基类Loader。
方法
共有方法请参见其基类Loader。
.load ( url : String, onLoad : Function, onProgress : Function, onError : Function ) : null
url — 文件的URL或者路径,也可以为 Data URI.
onLoad — 加载完成时将调用。回调参数为将要加载的object.
onProgress — 将在加载过程中进行调用。参数为XMLHttpRequest实例,实例包含total和loaded字节。
onError — 在加载错误时被调用。
从URL中进行加载,并将被解析的响应内容传递给onLoad。
.parse ( json : Object, onLoad : Function ) : Object3D
json — 必选参数,需要被解析的JSON源。
onLoad — 当解析完成时被调用,其中参数被解析为object.
解析一个JSON结构,并返回一个threejs对象. 内部使用.load()进行加载, 但也可以直接用于解析先前加载的JSON结构。
.parseGeometries ( json : Object ) : Object3D
json — 必选参数,需要被解析的JSON源。
此函数以JSON结构,用.parse()去解析geometries。
.parseMaterials ( json : Object ) : Object3D
json — 必选参数,需要被解析的JSON源。
此函数通过[page:.parse()来使用MaterialLoader,以解析JSON结构中任意材质。
.parseAnimations ( json : Object ) : Object3D
json — 必选参数,需要被解析的JSON源。
此函数通过.parse()来使用AnimationClip.parse(), 以解析JSON结构中任意动画。
.parseImages ( json : Object ) : Object3D
json — 必选参数,需要被解析的JSON源。
此函数通过.parse()来使用ImageLoader, 以解析JSON结构中任意图片。
.parseTextures ( json : Object ) : Object3D
json — 必选参数,需要被解析的JSON源。
此函数通过.parse()来解析JSON结构中任意纹理。
.parseObject ( json : Object, geometries : BufferGeometry, materials : Material, animations : AnimationClip ) : Object3D
json — 必选参数,需要被解析的JSON源。
geometries — required. The geometries of the JSON.
materials — required. The materials of the JSON.
animations — required. The animations of the JSON.
此函数通过.parse()来解析JSON结构中任意对象。
TextureLoader
加载texture的一个类。 内部使用ImageLoader来加载文件
const texture = new THREE.TextureLoader().load( 'textures/land_ocean_ice_cloud_2048.jpg' );
// 立即使用纹理进行材质创建
const material = new THREE.MeshBasicMaterial( { map: texture } );
// 初始化一个加载器
const loader = new THREE.TextureLoader();
// 加载一个资源
loader.load(
// 资源URL
'textures/land_ocean_ice_cloud_2048.jpg',
// onLoad回调
function ( texture ) {
// in this example we create the material when the texture is loaded
const material = new THREE.MeshBasicMaterial( {
map: texture
} );
},
// 目前暂不支持onProgress的回调
undefined,
// onError回调
function ( err ) {
console.error( 'An error happened.' );
}
);
构造函数
TextureLoader( manager : LoadingManager )
manager — 加载器使用的loadingManager,默认值为THREE.DefaultLoadingManager.
创建一个新的TextureLoader.
属性
共有属性请参见其基类Loader。
方法
共有方法请参见其基类Loader。
.load ( url : String, onLoad : Function, onProgress : Function, onError : Function ) : Texture
url — 文件的URL或者路径,也可以为 Data URI.
onLoad — 加载完成时将调用。回调参数为将要加载的texture.
onProgress — 将在加载过程中进行调用。参数为XMLHttpRequest实例,实例包含total和loaded字节。
onError — 在加载错误时被调用。
从给定的URL开始加载并将完全加载的texture传递给onLoad。该方法还返回一个新的纹理对象,该纹理对象可以直接用于材质创建。 如果采用此方法,一旦相应的加载过程完成,纹理可能会在场景中出现。