瀏覽代碼

JSONLoader: Add setPath(), rename setTexturePath() to setResourcePath()

Mugen87 6 年之前
父節點
當前提交
3116de8c42
共有 4 個文件被更改,包括 41 次插入17 次删除
  1. 12 8
      docs/api/en/loaders/JSONLoader.html
  2. 1 1
      editor/js/Loader.js
  3. 12 0
      src/Three.Legacy.js
  4. 16 8
      src/loaders/JSONLoader.js

+ 12 - 8
docs/api/en/loaders/JSONLoader.html

@@ -93,23 +93,27 @@
 		Begin loading from url and pass the <em>JSON</em> to onLoad.
 		</p>
 
+		<h3>[method:Object3D parse]( [param:Object json], [param:String path] )</h3>
+		<p>
+		[page:String json] — JSON object to parse.<br />
+		[page:String path] — Base path for resources if no resource path is defined.<br /><br />
+
+		Parse a <em>JSON</em> structure and return an [page:object] containing the parsed [page:Geometry geometry] and [page:Array materials].
+		</p>
+
 		<h3>[method:JSONLoader setCrossOrigin]( [param:String value] )</h3>
 		<p>
 			Set the [page:.crossOrigin] attribute.
 		</p>
 
-		<h3>[method:JSONLoader setTexturePath]( [param:String texturePath] )</h3>
+		<h3>[method:JSONLoader setPath]( [param:String value] )</h3>
 		<p>
-			Set the base path or URL from which to load files. This can be useful if
-			you are loading many files from the same directory.
+			Set the base path for the original file.
 		</p>
 
-		<h3>[method:Object3D parse]( [param:Object json], [param:String texturePath] )</h3>
+		<h3>[method:JSONLoader setResourcePath]( [param:String value] )</h3>
 		<p>
-		[page:String json] — JSON object to parse.<br />
-		[page:String texturePath] — Base path for textures.<br /><br />
-
-		Parse a <em>JSON</em> structure and return an [page:object] containing the parsed [page:Geometry geometry] and [page:Array materials].
+			Set the base path for dependent resources like textures.
 		</p>
 
 		<h2>Source</h2>

+ 1 - 1
editor/js/Loader.js

@@ -577,7 +577,7 @@ var Loader = function ( editor ) {
 			case 'geometry':
 
 				var loader = new THREE.JSONLoader();
-				loader.setTexturePath( scope.texturePath );
+				loader.setResourcePath( scope.texturePath );
 
 				var result = loader.parse( data );
 

+ 12 - 0
src/Three.Legacy.js

@@ -44,6 +44,7 @@ import { FileLoader } from './loaders/FileLoader.js';
 import { AudioLoader } from './loaders/AudioLoader.js';
 import { CubeTextureLoader } from './loaders/CubeTextureLoader.js';
 import { DataTextureLoader } from './loaders/DataTextureLoader.js';
+import { JSONLoader } from './loaders/JSONLoader.js';
 import { TextureLoader } from './loaders/TextureLoader.js';
 import { Material } from './materials/Material.js';
 import { LineBasicMaterial } from './materials/LineBasicMaterial.js';
@@ -435,6 +436,17 @@ export function BinaryTextureLoader( manager ) {
 
 }
 
+Object.assign( JSONLoader.prototype, {
+
+	setTexturePath: function ( value ) {
+
+		console.warn( 'THREE.JSONLoader: .setTexturePath() has been renamed to .setResourcePath().' );
+		return this.setResourcePath( value );
+
+	}
+
+} );
+
 //
 
 Object.assign( Box2.prototype, {

+ 16 - 8
src/loaders/JSONLoader.js

@@ -38,9 +38,10 @@ Object.assign( JSONLoader.prototype, {
 
 		var scope = this;
 
-		var texturePath = this.texturePath && ( typeof this.texturePath === 'string' ) ? this.texturePath : LoaderUtils.extractUrlBase( url );
+		var path = ( this.path === undefined ) ? LoaderUtils.extractUrlBase( url ) : this.path;
 
 		var loader = new FileLoader( this.manager );
+		loader.setPath( this.path );
 		loader.setWithCredentials( this.withCredentials );
 		loader.load( url, function ( text ) {
 
@@ -64,23 +65,30 @@ Object.assign( JSONLoader.prototype, {
 
 			}
 
-			var object = scope.parse( json, texturePath );
+			var object = scope.parse( json, path );
 			onLoad( object.geometry, object.materials );
 
 		}, onProgress, onError );
 
 	},
 
-	setCrossOrigin: function ( value ) {
+	setPath: function ( value ) {
 
-		this.crossOrigin = value;
+		this.path = value;
 		return this;
 
 	},
 
-	setTexturePath: function ( value ) {
+	setResourcePath: function ( value ) {
 
-		this.texturePath = value;
+		this.resourcePath = value;
+		return this;
+
+	},
+
+	setCrossOrigin: function ( value ) {
+
+		this.crossOrigin = value;
 		return this;
 
 	},
@@ -528,7 +536,7 @@ Object.assign( JSONLoader.prototype, {
 
 		}
 
-		return function parse( json, texturePath ) {
+		return function parse( json, path ) {
 
 			if ( json.data !== undefined ) {
 
@@ -563,7 +571,7 @@ Object.assign( JSONLoader.prototype, {
 
 			} else {
 
-				var materials = Loader.prototype.initMaterials( json.materials, texturePath, this.crossOrigin );
+				var materials = Loader.prototype.initMaterials( json.materials, this.resourcePath || path, this.crossOrigin );
 
 				return { geometry: geometry, materials: materials };