LoaderCommons.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. if ( THREE.LoaderSupport === undefined ) { THREE.LoaderSupport = {} }
  2. /**
  3. * Validation functions.
  4. * @class
  5. */
  6. THREE.LoaderSupport.Validator = {
  7. /**
  8. * If given input is null or undefined, false is returned otherwise true.
  9. *
  10. * @param input Can be anything
  11. * @returns {boolean}
  12. */
  13. isValid: function( input ) {
  14. return ( input !== null && input !== undefined );
  15. },
  16. /**
  17. * If given input is null or undefined, the defaultValue is returned otherwise the given input.
  18. *
  19. * @param input Can be anything
  20. * @param defaultValue Can be anything
  21. * @returns {*}
  22. */
  23. verifyInput: function( input, defaultValue ) {
  24. return ( input === null || input === undefined ) ? defaultValue : input;
  25. }
  26. };
  27. /**
  28. * Callbacks utilized by loaders and builders.
  29. * @class
  30. */
  31. THREE.LoaderSupport.Callbacks = (function () {
  32. var Validator = THREE.LoaderSupport.Validator;
  33. function Callbacks() {
  34. this.onProgress = null;
  35. this.onMeshAlter = null;
  36. this.onLoad = null;
  37. this.onLoadMaterials = null;
  38. }
  39. /**
  40. * Register callback function that is invoked by internal function "announceProgress" to print feedback.
  41. * @memberOf THREE.LoaderSupport.Callbacks
  42. *
  43. * @param {callback} callbackOnProgress Callback function for described functionality
  44. */
  45. Callbacks.prototype.setCallbackOnProgress = function ( callbackOnProgress ) {
  46. this.onProgress = Validator.verifyInput( callbackOnProgress, this.onProgress );
  47. };
  48. /**
  49. * Register callback function that is called every time a mesh was loaded.
  50. * Use {@link THREE.LoaderSupport.LoadedMeshUserOverride} for alteration instructions (geometry, material or disregard mesh).
  51. * @memberOf THREE.LoaderSupport.Callbacks
  52. *
  53. * @param {callback} callbackOnMeshAlter Callback function for described functionality
  54. */
  55. Callbacks.prototype.setCallbackOnMeshAlter = function ( callbackOnMeshAlter ) {
  56. this.onMeshAlter = Validator.verifyInput( callbackOnMeshAlter, this.onMeshAlter );
  57. };
  58. /**
  59. * Register callback function that is called once loading of the complete OBJ file is completed.
  60. * @memberOf THREE.LoaderSupport.Callbacks
  61. *
  62. * @param {callback} callbackOnLoad Callback function for described functionality
  63. */
  64. Callbacks.prototype.setCallbackOnLoad = function ( callbackOnLoad ) {
  65. this.onLoad = Validator.verifyInput( callbackOnLoad, this.onLoad );
  66. };
  67. /**
  68. * Register callback function that is called when materials have been loaded.
  69. * @memberOf THREE.LoaderSupport.Callbacks
  70. *
  71. * @param {callback} callbackOnLoadMaterials Callback function for described functionality
  72. */
  73. Callbacks.prototype.setCallbackOnLoadMaterials = function ( callbackOnLoadMaterials ) {
  74. this.onLoadMaterials = Validator.verifyInput( callbackOnLoadMaterials, this.onLoadMaterials );
  75. };
  76. return Callbacks;
  77. })();
  78. /**
  79. * Object to return by callback onMeshAlter. Used to disregard a certain mesh or to return one to many meshes.
  80. * @class
  81. *
  82. * @param {boolean} disregardMesh=false Tell implementation to completely disregard this mesh
  83. * @param {boolean} disregardMesh=false Tell implementation that mesh(es) have been altered or added
  84. */
  85. THREE.LoaderSupport.LoadedMeshUserOverride = (function () {
  86. function LoadedMeshUserOverride( disregardMesh, alteredMesh ) {
  87. this.disregardMesh = disregardMesh === true;
  88. this.alteredMesh = alteredMesh === true;
  89. this.meshes = [];
  90. }
  91. /**
  92. * Add a mesh created within callback.
  93. *
  94. * @memberOf THREE.OBJLoader2.LoadedMeshUserOverride
  95. *
  96. * @param {THREE.Mesh} mesh
  97. */
  98. LoadedMeshUserOverride.prototype.addMesh = function ( mesh ) {
  99. this.meshes.push( mesh );
  100. this.alteredMesh = true;
  101. };
  102. /**
  103. * Answers if mesh shall be disregarded completely.
  104. *
  105. * @returns {boolean}
  106. */
  107. LoadedMeshUserOverride.prototype.isDisregardMesh = function () {
  108. return this.disregardMesh;
  109. };
  110. /**
  111. * Answers if new mesh(es) were created.
  112. *
  113. * @returns {boolean}
  114. */
  115. LoadedMeshUserOverride.prototype.providesAlteredMeshes = function () {
  116. return this.alteredMesh;
  117. };
  118. return LoadedMeshUserOverride;
  119. })();
  120. /**
  121. * A resource description used by {@link THREE.LoaderSupport.PrepData} and others.
  122. * @class
  123. *
  124. * @param {string} url URL to the file
  125. * @param {string} extension The file extension (type)
  126. */
  127. THREE.LoaderSupport.ResourceDescriptor = (function () {
  128. var Validator = THREE.LoaderSupport.Validator;
  129. function ResourceDescriptor( url, extension ) {
  130. var urlParts = url.split( '/' );
  131. if ( urlParts.length < 2 ) {
  132. this.path = null;
  133. this.name = url;
  134. this.url = url;
  135. } else {
  136. this.path = Validator.verifyInput( urlParts.slice( 0, urlParts.length - 1).join( '/' ) + '/', null );
  137. this.name = Validator.verifyInput( urlParts[ urlParts.length - 1 ], null );
  138. this.url = url;
  139. }
  140. this.extension = Validator.verifyInput( extension, "default" );
  141. this.extension = this.extension.trim();
  142. this.content = null;
  143. }
  144. /**
  145. * Set the content of this resource
  146. * @memberOf THREE.LoaderSupport.ResourceDescriptor
  147. *
  148. * @param {Object} content The file content as arraybuffer or text
  149. */
  150. ResourceDescriptor.prototype.setContent = function ( content ) {
  151. this.content = Validator.verifyInput( content, null );
  152. };
  153. return ResourceDescriptor;
  154. })();
  155. /**
  156. * Configuration instructions to be used by run method.
  157. * @class
  158. */
  159. THREE.LoaderSupport.PrepData = (function () {
  160. var Validator = THREE.LoaderSupport.Validator;
  161. function PrepData( modelName ) {
  162. this.logging = {
  163. enabled: true,
  164. debug: false
  165. };
  166. this.modelName = Validator.verifyInput( modelName, '' );
  167. this.resources = [];
  168. this.callbacks = new THREE.LoaderSupport.Callbacks();
  169. }
  170. /**
  171. * Enable or disable logging in general (except warn and error), plus enable or disable debug logging.
  172. * @memberOf THREE.LoaderSupport.PrepData
  173. *
  174. * @param {boolean} enabled True or false.
  175. * @param {boolean} debug True or false.
  176. */
  177. PrepData.prototype.setLogging = function ( enabled, debug ) {
  178. this.logging.enabled = enabled === true;
  179. this.logging.debug = debug === true;
  180. };
  181. /**
  182. * Returns all callbacks as {@link THREE.LoaderSupport.Callbacks}
  183. * @memberOf THREE.LoaderSupport.PrepData
  184. *
  185. * @returns {THREE.LoaderSupport.Callbacks}
  186. */
  187. PrepData.prototype.getCallbacks = function () {
  188. return this.callbacks;
  189. };
  190. /**
  191. * Add a resource description.
  192. * @memberOf THREE.LoaderSupport.PrepData
  193. *
  194. * @param {THREE.LoaderSupport.ResourceDescriptor} Adds a {@link THREE.LoaderSupport.ResourceDescriptor}
  195. */
  196. PrepData.prototype.addResource = function ( resource ) {
  197. this.resources.push( resource );
  198. };
  199. /**
  200. * Clones this object and returns it afterwards. Callbacks and resources are not cloned deep (references!).
  201. * @memberOf THREE.LoaderSupport.PrepData
  202. *
  203. * @returns {@link THREE.LoaderSupport.PrepData}
  204. */
  205. PrepData.prototype.clone = function () {
  206. var clone = new THREE.LoaderSupport.PrepData( this.modelName );
  207. clone.logging.enabled = this.logging.enabled;
  208. clone.logging.debug = this.logging.debug;
  209. clone.resources = this.resources;
  210. clone.callbacks = this.callbacks;
  211. var property, value;
  212. for ( property in this ) {
  213. value = this[ property ];
  214. if ( ! clone.hasOwnProperty( property ) && typeof this[ property ] !== 'function' ) {
  215. clone[ property ] = value;
  216. }
  217. }
  218. return clone;
  219. };
  220. /**
  221. * Identify files or content of interest from an Array of {@link THREE.LoaderSupport.ResourceDescriptor}.
  222. * @memberOf THREE.LoaderSupport.PrepData
  223. *
  224. * @param {THREE.LoaderSupport.ResourceDescriptor[]} resources Array of {@link THREE.LoaderSupport.ResourceDescriptor}
  225. * @param Object fileDesc Object describing which resources are of interest (ext, type (string or UInt8Array) and ignore (boolean))
  226. * @returns {{}} Object with each "ext" and the corresponding {@link THREE.LoaderSupport.ResourceDescriptor}
  227. */
  228. PrepData.prototype.checkResourceDescriptorFiles = function ( resources, fileDesc ) {
  229. var resource, triple, i, found;
  230. var result = {};
  231. for ( var index in resources ) {
  232. resource = resources[ index ];
  233. found = false;
  234. if ( ! Validator.isValid( resource.name ) ) continue;
  235. if ( Validator.isValid( resource.content ) ) {
  236. for ( i = 0; i < fileDesc.length && !found; i++ ) {
  237. triple = fileDesc[ i ];
  238. if ( resource.extension.toLowerCase() === triple.ext.toLowerCase() ) {
  239. if ( triple.ignore ) {
  240. found = true;
  241. } else if ( triple.type === "ArrayBuffer" ) {
  242. // fast-fail on bad type
  243. if ( ! ( resource.content instanceof ArrayBuffer || resource.content instanceof Uint8Array ) ) throw 'Provided content is not of type ArrayBuffer! Aborting...';
  244. result[ triple.ext ] = resource;
  245. found = true;
  246. } else if ( triple.type === "String" ) {
  247. if ( ! ( typeof( resource.content ) === 'string' || resource.content instanceof String) ) throw 'Provided content is not of type String! Aborting...';
  248. result[ triple.ext ] = resource;
  249. found = true;
  250. }
  251. }
  252. }
  253. if ( !found ) throw 'Unidentified resource "' + resource.name + '": ' + resource.url;
  254. } else {
  255. // fast-fail on bad type
  256. if ( ! ( typeof( resource.name ) === 'string' || resource.name instanceof String ) ) throw 'Provided file is not properly defined! Aborting...';
  257. for ( i = 0; i < fileDesc.length && !found; i++ ) {
  258. triple = fileDesc[ i ];
  259. if ( resource.extension.toLowerCase() === triple.ext.toLowerCase() ) {
  260. if ( ! triple.ignore ) result[ triple.ext ] = resource;
  261. found = true;
  262. }
  263. }
  264. if ( !found ) throw 'Unidentified resource "' + resource.name + '": ' + resource.url;
  265. }
  266. }
  267. return result;
  268. };
  269. return PrepData;
  270. })();