Skip to content

Commit 4b7c344

Browse files
committed
Added parseOBJMaterial method and set default texture to __WHITE
1 parent dff414f commit 4b7c344

1 file changed

Lines changed: 88 additions & 6 deletions

File tree

src/gameobjects/mesh/Mesh.js

Lines changed: 88 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ var Face = require('../../geom/mesh/Face');
1111
var GameObject = require('../GameObject');
1212
var GameObjectEvents = require('../events');
1313
var GetCalcMatrix = require('../GetCalcMatrix');
14+
var GetColor = require('../../display/color/GetColor');
1415
var GetFastValue = require('../../utils/object/GetFastValue');
1516
var MeshRender = require('./MeshRender');
1617
var StableSort = require('../../utils/array/StableSort');
@@ -93,6 +94,10 @@ var Mesh = new Class({
9394

9495
function Mesh (scene, x, y, texture, frame, vertices, uvs, indicies, colors, alphas)
9596
{
97+
if (x === undefined) { x = 0; }
98+
if (y === undefined) { y = 0; }
99+
if (texture === undefined) { texture = '__WHITE'; }
100+
96101
GameObject.call(this, scene, 'Mesh');
97102

98103
/**
@@ -262,20 +267,21 @@ var Mesh = new Class({
262267
* @since 3.50.0
263268
*
264269
* @param {string} key - The key of the model data in the OBJ Cache to add to this Mesh.
270+
* @param {string} [material] - An optional Wavefront mtl file, given as a string. Supports `Kd` diffuse rgb value formats. See `parseOBJMaterial` method for details.
265271
* @param {number} [scale=1] - An amount to scale the model data by. Use this if the model has exported too small, or large, to see.
266272
* @param {number} [x=0] - Offset the model x position by this amount.
267273
* @param {number} [y=0] - Offset the model y position by this amount.
268274
* @param {number} [z=0] - Offset the model z position by this amount.
269275
*
270276
* @return {this} This Mesh Game Object.
271277
*/
272-
addOBJ: function (key, scale, x, y, z)
278+
addOBJ: function (key, material, scale, x, y, z)
273279
{
274280
var data = this.scene.sys.cache.obj.get(key);
275281

276282
if (data)
277283
{
278-
this.addModel(data, scale, x, y, z);
284+
this.addModel(data, material, scale, x, y, z);
279285
}
280286

281287
return this;
@@ -456,20 +462,26 @@ var Mesh = new Class({
456462
* @since 3.50.0
457463
*
458464
* @param {Phaser.Types.Geom.Mesh.OBJData} data - The parsed OBJ model data.
465+
* @param {string} [material] - An optional Wavefront mtl file, given as a string. Supports `Kd` diffuse rgb value formats. See `parseOBJMaterial` method for details.
459466
* @param {number} [scale=1] - An amount to scale the model data by. Use this if the model has exported too small, or large, to see.
460467
* @param {number} [x=0] - Offset the model x position by this amount.
461468
* @param {number} [y=0] - Offset the model y position by this amount.
462469
* @param {number} [z=0] - Offset the model z position by this amount.
463470
*
464471
* @return {this} This Mesh Game Object.
465472
*/
466-
addModel: function (data, scale, x, y, z)
473+
addModel: function (data, material, scale, x, y, z)
467474
{
468475
if (scale === undefined) { scale = 1; }
469476
if (x === undefined) { x = 0; }
470477
if (y === undefined) { y = 0; }
471478
if (z === undefined) { z = 0; }
472479

480+
if (material)
481+
{
482+
material = this.parseOBJMaterial(material);
483+
}
484+
473485
for (var m = 0; m < data.models.length; m++)
474486
{
475487
var model = data.models[m];
@@ -498,9 +510,16 @@ var Mesh = new Class({
498510
var uv2 = (t2 === -1) ? { u: 0, v: 0 } : textureCoords[t2];
499511
var uv3 = (t3 === -1) ? { u: 1, v: 1 } : textureCoords[t3];
500512

501-
var vert1 = this.addVertex(x + m1.x * scale, y + m1.y * scale, z + m1.z * scale, uv1.u, uv1.v);
502-
var vert2 = this.addVertex(x + m2.x * scale, y + m2.y * scale, z + m2.z * scale, uv2.u, uv2.v);
503-
var vert3 = this.addVertex(x + m3.x * scale, y + m3.y * scale, z + m3.z * scale, uv3.u, uv3.v);
513+
var color = 0xffffff;
514+
515+
if (material && face.material !== '' && material[face.material])
516+
{
517+
color = material[face.material];
518+
}
519+
520+
var vert1 = this.addVertex(x + m1.x * scale, y + m1.y * scale, z + m1.z * scale, uv1.u, uv1.v, color);
521+
var vert2 = this.addVertex(x + m2.x * scale, y + m2.y * scale, z + m2.z * scale, uv2.u, uv2.v, color);
522+
var vert3 = this.addVertex(x + m3.x * scale, y + m3.y * scale, z + m3.z * scale, uv3.u, uv3.v, color);
504523

505524
this.addFace(vert1, vert2, vert3);
506525
}
@@ -639,6 +658,69 @@ var Mesh = new Class({
639658
return this;
640659
},
641660

661+
/**
662+
* Takes a Wavefront Material file and extracts the diffuse reflectivity of the named
663+
* materials, converts them to integer color values and returns them.
664+
*
665+
* This is used internally by the `addOBJ` and `addModel` methods, but is exposed for
666+
* public consumption as well.
667+
*
668+
* Note this only works with diffuse values, specified in the `Kd r g b` format, where
669+
* `g` and `b` are optional, but `r` is required. It does not support spectral rfl files,
670+
* or any other material statement (such as `Ka` or `Ks`)
671+
*
672+
* @method Phaser.GameObjects.Mesh#parseOBJMaterial
673+
* @since 3.50.0
674+
*
675+
* @param {string} The OBJ MTL file.
676+
*
677+
* @return {object} The parsed material colors.
678+
*/
679+
parseOBJMaterial: function (mtl)
680+
{
681+
var output = {};
682+
683+
var lines = mtl.split('\n');
684+
685+
var currentMaterial = '';
686+
687+
for (var i = 0; i < lines.length; i++)
688+
{
689+
var line = lines[i].trim();
690+
691+
if (line.indexOf('#') === 0 || line === '')
692+
{
693+
continue;
694+
}
695+
696+
var lineItems = line.replace(/\s\s+/g, ' ').trim().split(' ');
697+
698+
switch (lineItems[0].toLowerCase())
699+
{
700+
case 'newmtl':
701+
{
702+
currentMaterial = lineItems[1];
703+
break;
704+
}
705+
706+
// The diffuse reflectivity of the current material
707+
// Support r, [g], [b] format, where g and b are optional
708+
case 'kd':
709+
{
710+
var r = Math.floor(lineItems[1] * 255);
711+
var g = (lineItems.length >= 2) ? Math.floor(lineItems[2] * 255) : r;
712+
var b = (lineItems.length >= 3) ? Math.floor(lineItems[3] * 255) : r;
713+
714+
output[currentMaterial] = GetColor(r, g, b);
715+
716+
break;
717+
}
718+
}
719+
}
720+
721+
return output;
722+
},
723+
642724
/**
643725
* Adds a new Vertex into the vertices array of this Mesh.
644726
*

0 commit comments

Comments
 (0)