diff --git a/webgl-renderables/Mesh.js b/webgl-renderables/Mesh.js index 4fe8bab3..45364428 100644 --- a/webgl-renderables/Mesh.js +++ b/webgl-renderables/Mesh.js @@ -54,9 +54,12 @@ function Mesh (node, options) { color: null, expressions: {}, flatShading: null, - glossiness: null, positionOffset: null, - normals: null + normals: null, + glossiness: { + strength: null, + color: [0, 0, 0] + } }; if (options) this.setDrawOptions(options); @@ -291,24 +294,29 @@ Mesh.prototype.getNormals = function getNormals (materialExpression) { * * @method * - * @param {MaterialExpression|Color} glossiness Accepts either a material expression or Color instance - * @param {Number} strength Optional value for changing the strength of the glossiness + * @param {MaterialExpression|Number} glossiness Accepts either a material expression or a number setting its strength + * @param {Color} specularColor Optional value for being able to change the specular color for glossiness. * * @return {Mesh} Mesh */ -Mesh.prototype.setGlossiness = function setGlossiness(glossiness, strength) { +Mesh.prototype.setGlossiness = function setGlossiness(glossiness, specularColor) { var isMaterial = glossiness.__isAMaterial__; - var isColor = !!glossiness.getNormalizedRGB; + var hasSpecularColor = specularColor && specularColor.getNormalizedRGB; if (isMaterial) { - this.value.glossiness = [null, null]; + this.value.glossiness.strength = null; this.value.expressions.glossiness = glossiness; } - else if (isColor) { + else { this.value.expressions.glossiness = null; - this.value.glossiness = [glossiness, strength || 20]; - glossiness = glossiness ? glossiness.getNormalizedRGB() : [0, 0, 0]; - glossiness.push(strength || 20); + this.value.glossiness.strength = glossiness; + var glossinessValue = this.value.glossiness.color; + if (hasSpecularColor) { + this.value.glossiness.color = specularColor; + glossinessValue = this.value.glossiness.color.getNormalizedRGB(); + } + glossinessValue.push(this.value.glossiness.strength); + glossiness = glossinessValue; } if (this._initialized) { @@ -326,7 +334,7 @@ Mesh.prototype.setGlossiness = function setGlossiness(glossiness, strength) { * * @method * - * @returns {MaterialExpress|Number} MaterialExpress or Number + * @returns {MaterialExpress|Object} MaterialExpression or Glossiness */ Mesh.prototype.getGlossiness = function getGlossiness() { return this.value.expressions.glossiness || this.value.glossiness; @@ -445,11 +453,11 @@ Mesh.prototype.onUpdate = function onUpdate() { this._node.sendDrawCommand(this.value.color.getNormalizedRGBA()); this._node.requestUpdateOnNextTick(this._id); } - if (this.value.glossiness && this.value.glossiness[0] && this.value.glossiness[0].isActive()) { - this._node.sendDrawCommand(Commands.GL_UNIFORMS); + if (this.value.glossiness.color.isActive && this.value.glossiness.color.isActive()) { + this._node.sendDrawCommand('GL_UNIFORMS'); this._node.sendDrawCommand('u_glossiness'); - var glossiness = this.value.glossiness[0].getNormalizedRGB(); - glossiness.push(this.value.glossiness[1]); + var glossiness = this.value.glossiness.color.getNormalizedRGB(); + glossiness.push(this.value.glossiness.strength); this._node.sendDrawCommand(glossiness); this._node.requestUpdateOnNextTick(this._id); } @@ -649,7 +657,7 @@ Mesh.prototype.draw = function draw () { if (value.geometry != null) this.setGeometry(value.geometry); if (value.color != null) this.setBaseColor(value.color); - if (value.glossiness != null) this.setGlossiness.apply(this, value.glossiness); + if (value.glossiness.strength != null) this.setGlossiness.apply(this, value.glossiness.strength); if (value.drawOptions != null) this.setDrawOptions(value.drawOptions); if (value.flatShading != null) this.setFlatShading(value.flatShading); diff --git a/webgl-renderables/test/Mesh.js b/webgl-renderables/test/Mesh.js index 06cd1a91..4a7d03d4 100644 --- a/webgl-renderables/test/Mesh.js +++ b/webgl-renderables/test/Mesh.js @@ -385,9 +385,11 @@ test('Mesh', function(t) { 'should be able to return the glossiness expression'); var x = new MockColor(); - mesh.setGlossiness(x, 10); - t.equal(mesh.getGlossiness()[0], x, - 'should be able to return the glossiness value'); + mesh.setGlossiness(10, x); + t.equal(mesh.getGlossiness().strength, 10, + 'should be able to return the glossiness strength'); + t.equal(mesh.getGlossiness().color, x, + 'should be able to return the glossiness color'); t.end(); });