Skip to content

Commit 0c84853

Browse files
committed
Expanding GLSL File Loader
1 parent 3c655bf commit 0c84853

1 file changed

Lines changed: 176 additions & 3 deletions

File tree

src/loader/filetypes/GLSLFile.js

Lines changed: 176 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,14 @@ var File = require('../File');
1010
var FileTypesManager = require('../FileTypesManager');
1111
var GetFastValue = require('../../utils/object/GetFastValue');
1212
var IsPlainObject = require('../../utils/object/IsPlainObject');
13+
var Shader = require('../../display/shader/Shader');
1314

1415
/**
1516
* @typedef {object} Phaser.Loader.FileTypes.GLSLFileConfig
1617
*
1718
* @property {string} key - The key of the file. Must be unique within both the Loader and the Text Cache.
1819
* @property {string} [url] - The absolute or relative URL to load the file from.
20+
* @property {string} [shaderType='fragment'] - The type of shader. Either `fragment` for a fragment shader, or `vertex` for a vertex shader. This is ignored if you load a shader bundle.
1921
* @property {string} [extension='glsl'] - The default file extension to use if no url is provided.
2022
* @property {Phaser.Loader.Types.XHRSettingsObject} [xhrSettings] - Extra XHR Settings specifically for this file.
2123
*/
@@ -37,6 +39,7 @@ var IsPlainObject = require('../../utils/object/IsPlainObject');
3739
* @param {Phaser.Loader.LoaderPlugin} loader - A reference to the Loader that is responsible for this file.
3840
* @param {(string|Phaser.Loader.FileTypes.TextFileConfig)} key - The key to use for this file, or a file configuration object.
3941
* @param {string} [url] - The absolute or relative URL to load this file from. If undefined or `null` it will be set to `<key>.txt`, i.e. if `key` was "alien" then the URL will be "alien.txt".
42+
* @param {string} [shaderType='fragment'] - The type of shader. Either `fragment` for a fragment shader, or `vertex` for a vertex shader. This is ignored if you load a shader bundle.
4043
* @param {Phaser.Loader.Types.XHRSettingsObject} [xhrSettings] - Extra XHR Settings specifically for this file.
4144
*/
4245
var GLSLFile = new Class({
@@ -45,7 +48,7 @@ var GLSLFile = new Class({
4548

4649
initialize:
4750

48-
function GLSLFile (loader, key, url, xhrSettings)
51+
function GLSLFile (loader, key, url, shaderType, xhrSettings)
4952
{
5053
var extension = 'glsl';
5154

@@ -55,9 +58,14 @@ var GLSLFile = new Class({
5558

5659
key = GetFastValue(config, 'key');
5760
url = GetFastValue(config, 'url');
61+
shaderType = GetFastValue(config, 'shaderType', 'fragment');
5862
xhrSettings = GetFastValue(config, 'xhrSettings');
5963
extension = GetFastValue(config, 'extension', extension);
6064
}
65+
else if (shaderType === undefined)
66+
{
67+
shaderType = 'frag';
68+
}
6169

6270
var fileConfig = {
6371
type: 'glsl',
@@ -66,6 +74,7 @@ var GLSLFile = new Class({
6674
responseType: 'text',
6775
key: key,
6876
url: url,
77+
shaderType: shaderType,
6978
xhrSettings: xhrSettings
7079
};
7180

@@ -86,6 +95,168 @@ var GLSLFile = new Class({
8695
this.data = this.xhrLoader.responseText;
8796

8897
this.onProcessComplete();
98+
},
99+
100+
/**
101+
* Adds this file to its target cache upon successful loading and processing.
102+
*
103+
* @method Phaser.Loader.FileTypes.GLSLFile#addToCache
104+
* @since 3.17.0
105+
*/
106+
addToCache: function ()
107+
{
108+
var data = this.data.split('\n');
109+
110+
// Check to see if this is a shader bundle, or raw glsl file.
111+
var block = this.extractBlock(data, 0);
112+
113+
if (block)
114+
{
115+
console.log('glsl bundle loaded');
116+
117+
while (block)
118+
{
119+
var key = this.getShaderName(block.header);
120+
var shaderType = this.getShaderType(block.header);
121+
var shaderSrc = block.shader;
122+
123+
console.log('shader: ', key);
124+
125+
if (this.cache.has(key))
126+
{
127+
var shader = this.cache.get(key);
128+
129+
if (shaderType === 'fragment')
130+
{
131+
shader.fragmentSrc = shaderSrc;
132+
}
133+
else
134+
{
135+
shader.vertexSrc = shaderSrc;
136+
}
137+
}
138+
else if (shaderType === 'fragment')
139+
{
140+
this.cache.add(key, new Shader(key, shaderSrc));
141+
}
142+
else
143+
{
144+
this.cache.add(key, new Shader(key, '', shaderSrc));
145+
}
146+
147+
block = this.extractBlock(data, block.offset);
148+
}
149+
}
150+
else
151+
{
152+
console.log(this.key, 'raw glsl');
153+
154+
// Single shader
155+
if (this.config.shaderType === 'fragment')
156+
{
157+
this.cache.add(this.key, new Shader(this.key, data));
158+
}
159+
else
160+
{
161+
this.cache.add(this.key, new Shader(this.key, '', data));
162+
}
163+
}
164+
165+
this.pendingDestroy();
166+
},
167+
168+
getShaderName: function (headerSource)
169+
{
170+
for (var i = 0; i < headerSource.length; i++)
171+
{
172+
var line = headerSource[i].trim();
173+
174+
if (line.substr(0, 5) === 'name:')
175+
{
176+
return line.substr(5).trim();
177+
}
178+
}
179+
180+
return this.key;
181+
},
182+
183+
getShaderType: function (headerSource)
184+
{
185+
for (var i = 0; i < headerSource.length; i++)
186+
{
187+
var line = headerSource[i].trim();
188+
189+
if (line.substr(0, 5) === 'type:')
190+
{
191+
return line.substr(5).trim();
192+
}
193+
}
194+
195+
return this.config.shaderType;
196+
},
197+
198+
extractBlock: function (data, offset)
199+
{
200+
var headerStart = -1;
201+
var headerEnd = -1;
202+
var blockEnd = -1;
203+
var headerOpen = false;
204+
var captureSource = false;
205+
var headerSource = [];
206+
var shaderSource = [];
207+
208+
for (var i = offset; i < data.length; i++)
209+
{
210+
var line = data[i].trim();
211+
212+
if (line === '---')
213+
{
214+
if (headerStart === -1)
215+
{
216+
headerStart = i;
217+
headerOpen = true;
218+
}
219+
else if (headerOpen)
220+
{
221+
headerEnd = i;
222+
headerOpen = false;
223+
captureSource = true;
224+
}
225+
else
226+
{
227+
// We've hit another --- delimeter, break out
228+
captureSource = false;
229+
break;
230+
}
231+
}
232+
else if (headerOpen)
233+
{
234+
headerSource.push(line);
235+
}
236+
else if (captureSource)
237+
{
238+
shaderSource.push(line);
239+
blockEnd = i;
240+
}
241+
}
242+
243+
// console.log('headerStart', headerStart);
244+
// console.log('headerEnd', headerEnd);
245+
// console.log('headerOpen', headerOpen);
246+
// console.log('blockEnd', blockEnd);
247+
// console.log('headerSource');
248+
// console.log(headerSource);
249+
// console.log('shaderSource');
250+
// console.log(shaderSource);
251+
252+
if (!headerOpen && headerEnd !== -1)
253+
{
254+
return { header: headerSource, shader: shaderSource.join('\n'), offset: blockEnd };
255+
}
256+
else
257+
{
258+
return null;
259+
}
89260
}
90261

91262
});
@@ -121,6 +292,7 @@ var GLSLFile = new Class({
121292
* ```javascript
122293
* this.load.glsl({
123294
* key: 'plasma',
295+
* shaderType: 'fragment',
124296
* url: 'shaders/Plasma.glsl'
125297
* });
126298
* ```
@@ -154,11 +326,12 @@ var GLSLFile = new Class({
154326
*
155327
* @param {(string|Phaser.Loader.FileTypes.GLSLFileConfig|Phaser.Loader.FileTypes.GLSLFileConfig[])} key - The key to use for this file, or a file configuration object, or array of them.
156328
* @param {string} [url] - The absolute or relative URL to load this file from. If undefined or `null` it will be set to `<key>.glsl`, i.e. if `key` was "alien" then the URL will be "alien.glsl".
329+
* @param {string} [shaderType='fragment'] - The type of shader. Either `fragment` for a fragment shader, or `vertex` for a vertex shader. This is ignored if you load a shader bundle.
157330
* @param {Phaser.Loader.Types.XHRSettingsObject} [xhrSettings] - An XHR Settings configuration object. Used in replacement of the Loaders default XHR Settings.
158331
*
159332
* @return {Phaser.Loader.LoaderPlugin} The Loader instance.
160333
*/
161-
FileTypesManager.register('glsl', function (key, url, xhrSettings)
334+
FileTypesManager.register('glsl', function (key, url, shaderType, xhrSettings)
162335
{
163336
if (Array.isArray(key))
164337
{
@@ -170,7 +343,7 @@ FileTypesManager.register('glsl', function (key, url, xhrSettings)
170343
}
171344
else
172345
{
173-
this.addFile(new GLSLFile(this, key, url, xhrSettings));
346+
this.addFile(new GLSLFile(this, key, url, shaderType, xhrSettings));
174347
}
175348

176349
return this;

0 commit comments

Comments
 (0)