Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean: Update glossiness API, to be able to have a default specular color if none provided #279

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 25 additions & 17 deletions webgl-renderables/Mesh.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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) {
Expand All @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);

Expand Down
8 changes: 5 additions & 3 deletions webgl-renderables/test/Mesh.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
Expand Down