/
ParseObjMaterial.js
72 lines (59 loc) · 2.11 KB
/
ParseObjMaterial.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2020 Photon Storm Ltd.
* @license {@link https://opensource.org/licenses/MIT|MIT License}
*/
var GetColor = require('../../display/color/GetColor');
/**
* Takes a Wavefront Material file and extracts the diffuse reflectivity of the named
* materials, converts them to integer color values and returns them.
*
* This is used internally by the `addOBJ` and `addModel` methods, but is exposed for
* public consumption as well.
*
* Note this only works with diffuse values, specified in the `Kd r g b` format, where
* `g` and `b` are optional, but `r` is required. It does not support spectral rfl files,
* or any other material statement (such as `Ka` or `Ks`)
*
* @method Phaser.Geom.Mesh.ParseObjMaterial
* @since 3.50.0
*
* @param {string} mtl - The OBJ MTL file as a raw string, i.e. loaded via `this.load.text`.
*
* @return {object} The parsed material colors, where each property of the object matches the material name.
*/
var ParseObjMaterial = function (mtl)
{
var output = {};
var lines = mtl.split('\n');
var currentMaterial = '';
for (var i = 0; i < lines.length; i++)
{
var line = lines[i].trim();
if (line.indexOf('#') === 0 || line === '')
{
continue;
}
var lineItems = line.replace(/\s\s+/g, ' ').trim().split(' ');
switch (lineItems[0].toLowerCase())
{
case 'newmtl':
{
currentMaterial = lineItems[1];
break;
}
// The diffuse reflectivity of the current material
// Support r, [g], [b] format, where g and b are optional
case 'kd':
{
var r = Math.floor(lineItems[1] * 255);
var g = (lineItems.length >= 2) ? Math.floor(lineItems[2] * 255) : r;
var b = (lineItems.length >= 3) ? Math.floor(lineItems[3] * 255) : r;
output[currentMaterial] = GetColor(r, g, b);
break;
}
}
}
return output;
};
module.exports = ParseObjMaterial;