Skip to content

Commit dd28a11

Browse files
committed
Loader.OBJFile is a new File Loader type that can load Wavefront OBJ files, which are then parsed and stored in the OBJ Cache.
1 parent a0b47e8 commit dd28a11

2 files changed

Lines changed: 180 additions & 0 deletions

File tree

src/loader/filetypes/OBJFile.js

Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
/**
2+
* @author Richard Davey <rich@photonstorm.com>
3+
* @copyright 2020 Photon Storm Ltd.
4+
* @license {@link https://opensource.org/licenses/MIT|MIT License}
5+
*/
6+
7+
var Class = require('../../utils/Class');
8+
var CONST = require('../const');
9+
var File = require('../File');
10+
var FileTypesManager = require('../FileTypesManager');
11+
var GetFastValue = require('../../utils/object/GetFastValue');
12+
var IsPlainObject = require('../../utils/object/IsPlainObject');
13+
var ParseObj = require('../../geom/ParseObj');
14+
15+
/**
16+
* @classdesc
17+
* A single Wavefront OBJ File suitable for loading by the Loader.
18+
*
19+
* These are created when you use the Phaser.Loader.LoaderPlugin#obj method and are not typically created directly.
20+
*
21+
* For documentation about what all the arguments and configuration options mean please see Phaser.Loader.LoaderPlugin#obj.
22+
*
23+
* @class OBJFile
24+
* @extends Phaser.Loader.File
25+
* @memberof Phaser.Loader.FileTypes
26+
* @constructor
27+
* @since 3.50.0
28+
*
29+
* @param {Phaser.Loader.LoaderPlugin} loader - A reference to the Loader that is responsible for this file.
30+
* @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 {boolean} [flipUV=true] - Flip the UV coordinates stored in the texture locations?
33+
* @param {Phaser.Types.Loader.XHRSettingsObject} [xhrSettings] - Extra XHR Settings specifically for this file.
34+
*/
35+
var OBJFile = new Class({
36+
37+
Extends: File,
38+
39+
initialize:
40+
41+
function OBJFile (loader, key, url, flipUV, xhrSettings)
42+
{
43+
var extension = 'obj';
44+
45+
if (IsPlainObject(key))
46+
{
47+
var config = key;
48+
49+
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+
}
55+
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
66+
}
67+
};
68+
69+
File.call(this, loader, fileConfig);
70+
},
71+
72+
/**
73+
* Called automatically by Loader.nextFile.
74+
* This method controls what extra work this File does with its loaded data.
75+
*
76+
* @method Phaser.Loader.FileTypes.HTMLFile#onProcess
77+
* @since 3.50.0
78+
*/
79+
onProcess: function ()
80+
{
81+
this.state = CONST.FILE_PROCESSING;
82+
83+
this.data = ParseObj(this.xhrLoader.responseText, this.config.flipUV);
84+
85+
this.onProcessComplete();
86+
}
87+
88+
});
89+
90+
/**
91+
* Adds a Wavefront OBJ file, or array of OBJ files, to the current load queue.
92+
*
93+
* Note: You should ensure your 3D package has triangulated the OBJ file prior to export.
94+
*
95+
* You can call this method from within your Scene's `preload`, along with any other files you wish to load:
96+
*
97+
* ```javascript
98+
* function preload ()
99+
* {
100+
* this.load.obj('ufo', 'files/spaceship.obj');
101+
* }
102+
* ```
103+
*
104+
* The file is **not** loaded right away. It is added to a queue ready to be loaded either when the loader starts,
105+
* or if it's already running, when the next free load slot becomes available. This happens automatically if you
106+
* are calling this from within the Scene's `preload` method, or a related callback. Because the file is queued
107+
* it means you cannot use the file immediately after calling this method, but must wait for the file to complete.
108+
* The typical flow for a Phaser Scene is that you load assets in the Scene's `preload` method and then when the
109+
* Scene's `create` method is called you are guaranteed that all of those assets are ready for use and have been
110+
* loaded.
111+
*
112+
* The key must be a unique String. It is used to add the file to the global OBJ Cache upon a successful load.
113+
* The key should be unique both in terms of files being loaded and files already present in the OBJ Cache.
114+
* Loading a file using a key that is already taken will result in a warning. If you wish to replace an existing file
115+
* then remove it from the OBJ Cache first, before loading a new one.
116+
*
117+
* Instead of passing arguments you can pass a configuration object, such as:
118+
*
119+
* ```javascript
120+
* this.load.obj({
121+
* key: 'ufo',
122+
* url: 'files/spaceship.obj',
123+
* flipUV: true
124+
* });
125+
* ```
126+
*
127+
* See the documentation for `Phaser.Types.Loader.FileTypes.OBJFileConfig` for more details.
128+
*
129+
* Once the file has finished loading you can access it from its Cache using its key:
130+
*
131+
* ```javascript
132+
* this.load.obj('ufo', 'files/spaceship.obj');
133+
* // and later in your game ...
134+
* var data = this.cache.obj.get('ufo');
135+
* ```
136+
*
137+
* If you have specified a prefix in the loader, via `Loader.setPrefix` then this value will be prepended to this files
138+
* key. For example, if the prefix was `LEVEL1.` and the key was `Story` the final key will be `LEVEL1.Story` and
139+
* this is what you would use to retrieve the obj from the OBJ Cache.
140+
*
141+
* The URL can be relative or absolute. If the URL is relative the `Loader.baseURL` and `Loader.path` values will be prepended to it.
142+
*
143+
* If the URL isn't specified the Loader will take the key and create a filename from that. For example if the key is "story"
144+
* and no URL is given then the Loader will set the URL to be "story.obj". It will always add `.obj` as the extension, although
145+
* this can be overridden if using an object instead of method arguments. If you do not desire this action then provide a URL.
146+
*
147+
* Note: The ability to load this type of file will only be available if the OBJ File type has been built into Phaser.
148+
* It is available in the default build but can be excluded from custom builds.
149+
*
150+
* @method Phaser.Loader.LoaderPlugin#obj
151+
* @fires Phaser.Loader.LoaderPlugin#ADD
152+
* @since 3.50.0
153+
*
154+
* @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".
156+
* @param {boolean} [flipUVs=true] - Flip the UV coordinates stored in the texture locations?
157+
* @param {Phaser.Types.Loader.XHRSettingsObject} [xhrSettings] - An XHR Settings configuration object. Used in replacement of the Loaders default XHR Settings.
158+
*
159+
* @return {this} The Loader instance.
160+
*/
161+
FileTypesManager.register('obj', function (key, url, flipUVs, xhrSettings)
162+
{
163+
if (Array.isArray(key))
164+
{
165+
for (var i = 0; i < key.length; i++)
166+
{
167+
// 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]));
169+
}
170+
}
171+
else
172+
{
173+
this.addFile(new OBJFile(this, key, url, flipUVs, xhrSettings));
174+
}
175+
176+
return this;
177+
});
178+
179+
module.exports = OBJFile;

src/loader/filetypes/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ module.exports = {
2626
JSONFile: require('./JSONFile'),
2727
MultiAtlasFile: require('./MultiAtlasFile'),
2828
MultiScriptFile: require('./MultiScriptFile'),
29+
OBJFile: require('./OBJFile'),
2930
PackFile: require('./PackFile'),
3031
PluginFile: require('./PluginFile'),
3132
SceneFile: require('./SceneFile'),

0 commit comments

Comments
 (0)