Skip to content

Commit c0ab4d4

Browse files
committed
OBJFile can now optionally load a material file along with the model data
1 parent 82c87ee commit c0ab4d4

2 files changed

Lines changed: 107 additions & 36 deletions

File tree

src/loader/filetypes/OBJFile.js

Lines changed: 105 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@
55
*/
66

77
var Class = require('../../utils/Class');
8-
var CONST = require('../const');
9-
var File = require('../File');
108
var FileTypesManager = require('../FileTypesManager');
119
var GetFastValue = require('../../utils/object/GetFastValue');
1210
var IsPlainObject = require('../../utils/object/IsPlainObject');
11+
var MultiFile = require('../MultiFile');
1312
var ParseObj = require('../../geom/mesh/ParseObj');
13+
var ParseObjMaterial = require('../../geom/mesh/ParseObjMaterial');
14+
var TextFile = require('./TextFile');
1415

1516
/**
1617
* @classdesc
@@ -21,68 +22,117 @@ var ParseObj = require('../../geom/mesh/ParseObj');
2122
* For documentation about what all the arguments and configuration options mean please see Phaser.Loader.LoaderPlugin#obj.
2223
*
2324
* @class OBJFile
24-
* @extends Phaser.Loader.File
25+
* @extends Phaser.Loader.MultiFile
2526
* @memberof Phaser.Loader.FileTypes
2627
* @constructor
2728
* @since 3.50.0
2829
*
2930
* @param {Phaser.Loader.LoaderPlugin} loader - A reference to the Loader that is responsible for this file.
3031
* @param {(string|Phaser.Types.Loader.FileTypes.OBJFileConfig)} key - The key to use for this file, or a file configuration object.
31-
* @param {string} [url] - The absolute or relative URL to load this file from. If undefined or `null` it will be set to `<key>.obj`, i.e. if `key` was "alien" then the URL will be "alien.obj".
32+
* @param {string} [objURL] - The absolute or relative URL to load the obj file from. If undefined or `null` it will be set to `<key>.obj`, i.e. if `key` was "alien" then the URL will be "alien.obj".
33+
* @param {string} [matURL] - The absolute or relative URL to load the material file from. If undefined or `null` it will be set to `<key>.mat`, i.e. if `key` was "alien" then the URL will be "alien.mat".
3234
* @param {boolean} [flipUV] - Flip the UV coordinates stored in the model data?
33-
* @param {Phaser.Types.Loader.XHRSettingsObject} [xhrSettings] - Extra XHR Settings specifically for this file.
35+
* @param {Phaser.Types.Loader.XHRSettingsObject} [xhrSettings] - Extra XHR Settings specifically for these files.
3436
*/
3537
var OBJFile = new Class({
3638

37-
Extends: File,
39+
Extends: MultiFile,
3840

3941
initialize:
4042

41-
function OBJFile (loader, key, url, flipUV, xhrSettings)
43+
function OBJFile (loader, key, objURL, matURL, flipUV, xhrSettings)
4244
{
43-
var extension = 'obj';
45+
var obj;
46+
var mat;
47+
48+
var cache = loader.cacheManager.obj;
4449

4550
if (IsPlainObject(key))
4651
{
4752
var config = key;
4853

4954
key = GetFastValue(config, 'key');
50-
url = GetFastValue(config, 'url');
51-
flipUV = GetFastValue(config, 'flipUV');
52-
xhrSettings = GetFastValue(config, 'xhrSettings');
53-
extension = GetFastValue(config, 'extension', extension);
54-
}
5555

56-
var fileConfig = {
57-
type: 'text',
58-
cache: loader.cacheManager.obj,
59-
extension: extension,
60-
responseType: 'text',
61-
key: key,
62-
url: url,
63-
xhrSettings: xhrSettings,
64-
config: {
65-
flipUV: flipUV
56+
obj = new TextFile(loader, {
57+
key: key,
58+
type: 'obj',
59+
cache: cache,
60+
url: GetFastValue(config, 'url'),
61+
extension: GetFastValue(config, 'extension', 'obj'),
62+
xhrSettings: GetFastValue(config, 'xhrSettings'),
63+
config: {
64+
flipUV: GetFastValue(config, 'flipUV', flipUV)
65+
}
66+
});
67+
68+
matURL = GetFastValue(config, 'matURL');
69+
70+
if (matURL)
71+
{
72+
mat = new TextFile(loader, {
73+
key: key,
74+
type: 'mat',
75+
cache: cache,
76+
url: matURL,
77+
extension: GetFastValue(config, 'matExtension', 'mat'),
78+
xhrSettings: GetFastValue(config, 'xhrSettings')
79+
});
80+
}
81+
}
82+
else
83+
{
84+
obj = new TextFile(loader, {
85+
key: key,
86+
url: objURL,
87+
type: 'obj',
88+
cache: cache,
89+
extension: 'obj',
90+
xhrSettings: xhrSettings,
91+
config: {
92+
flipUV: flipUV
93+
}
94+
});
95+
96+
if (matURL)
97+
{
98+
mat = new TextFile(loader, {
99+
key: key,
100+
url: matURL,
101+
type: 'mat',
102+
cache: cache,
103+
extension: 'mat',
104+
xhrSettings: xhrSettings
105+
});
66106
}
67-
};
107+
}
68108

69-
File.call(this, loader, fileConfig);
109+
MultiFile.call(this, loader, 'obj', key, [ obj, mat ]);
70110
},
71111

72112
/**
73-
* Called automatically by Loader.nextFile.
74-
* This method controls what extra work this File does with its loaded data.
113+
* Adds this file to its target cache upon successful loading and processing.
75114
*
76-
* @method Phaser.Loader.FileTypes.HTMLFile#onProcess
115+
* @method Phaser.Loader.FileTypes.OBJFile#addToCache
77116
* @since 3.50.0
78117
*/
79-
onProcess: function ()
118+
addToCache: function ()
80119
{
81-
this.state = CONST.FILE_PROCESSING;
120+
if (this.isReadyToProcess())
121+
{
122+
var obj = this.files[0];
123+
var mat = this.files[1];
124+
125+
var objData = ParseObj(obj.data, obj.config.flipUV);
82126

83-
this.data = ParseObj(this.xhrLoader.responseText, this.config.flipUV);
127+
if (mat)
128+
{
129+
objData.materials = ParseObjMaterial(mat.data);
130+
}
84131

85-
this.onProcessComplete();
132+
obj.cache.add(obj.key, objData);
133+
134+
this.complete = true;
135+
}
86136
}
87137

88138
});
@@ -101,6 +151,17 @@ var OBJFile = new Class({
101151
* }
102152
* ```
103153
*
154+
* You can optionally also load a Wavefront Material file as well, by providing the 3rd parameter:
155+
*
156+
* ```javascript
157+
* function preload ()
158+
* {
159+
* this.load.obj('ufo', 'files/spaceship.obj', 'files/spaceship.mtl');
160+
* }
161+
* ```
162+
*
163+
* If given, the material will be parsed and stored along with the obj data in the cache.
164+
*
104165
* The file is **not** loaded right away. It is added to a queue ready to be loaded either when the loader starts,
105166
* or if it's already running, when the next free load slot becomes available. This happens automatically if you
106167
* are calling this from within the Scene's `preload` method, or a related callback. Because the file is queued
@@ -120,6 +181,7 @@ var OBJFile = new Class({
120181
* this.load.obj({
121182
* key: 'ufo',
122183
* url: 'files/spaceship.obj',
184+
* matURL: 'files/spaceship.mtl',
123185
* flipUV: true
124186
* });
125187
* ```
@@ -152,25 +214,32 @@ var OBJFile = new Class({
152214
* @since 3.50.0
153215
*
154216
* @param {(string|Phaser.Types.Loader.FileTypes.OBJFileConfig|Phaser.Types.Loader.FileTypes.OBJFileConfig[])} key - The key to use for this file, or a file configuration object, or array of them.
155-
* @param {string} [url] - The absolute or relative URL to load this file from. If undefined or `null` it will be set to `<key>.obj`, i.e. if `key` was "alien" then the URL will be "alien.obj".
217+
* @param {string} [objURL] - The absolute or relative URL to load the obj file from. If undefined or `null` it will be set to `<key>.obj`, i.e. if `key` was "alien" then the URL will be "alien.obj".
218+
* @param {string} [matURL] - Optional absolute or relative URL to load the obj material file from.
156219
* @param {boolean} [flipUV] - Flip the UV coordinates stored in the model data?
157220
* @param {Phaser.Types.Loader.XHRSettingsObject} [xhrSettings] - An XHR Settings configuration object. Used in replacement of the Loaders default XHR Settings.
158221
*
159222
* @return {this} The Loader instance.
160223
*/
161-
FileTypesManager.register('obj', function (key, url, flipUVs, xhrSettings)
224+
FileTypesManager.register('obj', function (key, objURL, matURL, flipUVs, xhrSettings)
162225
{
226+
var multifile;
227+
163228
if (Array.isArray(key))
164229
{
165230
for (var i = 0; i < key.length; i++)
166231
{
232+
multifile = new OBJFile(this, key[i]);
233+
167234
// If it's an array it has to be an array of Objects, so we get everything out of the 'key' object
168-
this.addFile(new OBJFile(this, key[i]));
235+
this.addFile(multifile.files);
169236
}
170237
}
171238
else
172239
{
173-
this.addFile(new OBJFile(this, key, url, flipUVs, xhrSettings));
240+
multifile = new OBJFile(this, key, objURL, matURL, flipUVs, xhrSettings);
241+
242+
this.addFile(multifile.files);
174243
}
175244

176245
return this;

src/loader/filetypes/typedefs/OBJFileConfig.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,7 @@
55
* @property {string} [url] - The absolute or relative URL to load this file from. If undefined or `null` it will be set to `<key>.obj`, i.e. if `key` was "alien" then the URL will be "alien.obj".
66
* @property {string} [extension='obj'] - The default file extension to use if no url is provided.
77
* @property {boolean} [flipUV] - Flip the UV coordinates stored in the model data?
8+
* @property {string} [matURL] - An optional absolute or relative URL to the object material file from. If undefined or `null`, no material file will be loaded.
9+
* @property {string} [matExtension='mat'] - The default material file extension to use if no url is provided.
810
* @property {Phaser.Types.Loader.XHRSettingsObject} [xhrSettings] - Extra XHR Settings specifically for this file.
911
*/

0 commit comments

Comments
 (0)