From ac27d538af33ca8d67d3d88729f49c05793afda7 Mon Sep 17 00:00:00 2001 From: Nivesh Rajbhandari Date: Mon, 9 Jul 2012 11:43:36 -0700 Subject: PI, drawing and editing fixes for shapes and materials. IKNinja-1841 - Cannot change webgl shape with LinearGradient and RadialGradient to solid color. IKNINJA-1851 - Cannot draw webgl shapes with Linear/RadialGradient material. IKNINJA-1864 - PI doesn't update the color of shape if WebGL material switches to Flat. IKNINJA-1886 - Gradient edits not applied to WebGL Stage object. Signed-off-by: Nivesh Rajbhandari --- js/lib/rdge/materials/linear-gradient-material.js | 50 ++++++++++++++++ js/lib/rdge/materials/material.js | 69 ++++++++++++++--------- js/lib/rdge/materials/radial-gradient-material.js | 43 +++++++++++++- 3 files changed, 134 insertions(+), 28 deletions(-) (limited to 'js/lib/rdge') diff --git a/js/lib/rdge/materials/linear-gradient-material.js b/js/lib/rdge/materials/linear-gradient-material.js index 7e53db84..d26143de 100755 --- a/js/lib/rdge/materials/linear-gradient-material.js +++ b/js/lib/rdge/materials/linear-gradient-material.js @@ -79,6 +79,56 @@ var LinearGradientMaterial = function LinearGradientMaterial() { this.setShaderValues(); this.update( 0 ); }; + + // Only Linear Gradient and Radial Gradients support gradients; + this.gradientType = "linear"; + + this.getGradientData = function() { + var angle = Math.round(this._angle*180/Math.PI), + color, + colorStr, + css = "-webkit-gradient(linear, " + angle + "deg"; + + // TODO - Angle is not supported in -webkit-gradient syntax, so just default to across: + css = '-webkit-gradient(linear, left top, right top'; + + // TODO - Also, Color Model requires from and to in the gradient string + color = this.getProperty('u_color1'); + colorStr = Math.round(color[0] * 255) + ', ' + Math.round(color[1] * 255) + ', ' + Math.round(color[2] * 255) + ', ' + Math.round(color[3] * 100); + css += ', from(rgba(' + colorStr + '))'; + + for (var i=2; i < 4; i++) { + color = this.getProperty('u_color'+i); + colorStr = Math.round(color[0] * 255) + ', ' + Math.round(color[1] * 255) + ', ' + Math.round(color[2] * 255) + ', ' + Math.round(color[3] * 100); + css += ', color-stop(' + this.getProperty('u_colorStop'+i) + ', rgba(' + colorStr + '))'; + } + + color = this.getProperty('u_color4'); + colorStr = Math.round(color[0] * 255) + ', ' + Math.round(color[1] * 255) + ', ' + Math.round(color[2] * 255) + ', ' + Math.round(color[3] * 100); + css += ', to(rgba(' + colorStr + '))'; + + css += ')'; + + return css; + }; + + // Only Linear Gradient and Radial Gradient have gradient data. + this.setGradientData = function(colors) { + var len = colors.length; + // TODO - Current shaders only support 4 color stops + if (len > 4) { + len = 4; + } + + for (var n = 0; n < len; n++) { + var position = colors[n].position/100; + var cs = colors[n].value; + var stop = [cs.r/255, cs.g/255, cs.b/255, cs.a]; + + this.setProperty("u_color" + (n + 1), stop.slice(0)); + this.setProperty("u_colorStop" + (n + 1), position); + } + }; }; /////////////////////////////////////////////////////////////////////////////////////// diff --git a/js/lib/rdge/materials/material.js b/js/lib/rdge/materials/material.js index 65448c0c..1864b84e 100755 --- a/js/lib/rdge/materials/material.js +++ b/js/lib/rdge/materials/material.js @@ -177,6 +177,8 @@ var Material = function GLMaterial( world ) { }; this.validateProperty = function( prop, value ) { + if(prop === "gradient") return true; + var rtnVal = false; try { @@ -240,33 +242,37 @@ var Material = function GLMaterial( world ) { var material = this._materialNode; if (material) technique = material.shaderProgram[this.getTechniqueName()]; - switch (this.getPropertyType(prop)) - { - case "angle": - case "float": - this._propValues[prop] = value; - if (technique) technique[prop].set( [value] ); - break; - - case "file": - this._propValues[prop] = value.slice(); - if (technique) - { - var glTex = new Texture( this.getWorld(), value ); - this._glTextures[prop] = glTex; - glTex.render(); - var tex = glTex.getTexture(); - if (tex) technique[prop].set( tex ); - } - break; - - case "color": - case "vector2d": - case "vector3d": - this._propValues[prop] = value.slice(); - if (technique) technique[prop].set( value ); - break; - } + if(prop === "gradient") { + this.setGradientData(value); + } else { + switch (this.getPropertyType(prop)) + { + case "angle": + case "float": + this._propValues[prop] = value; + if (technique) technique[prop].set( [value] ); + break; + + case "file": + this._propValues[prop] = value.slice(); + if (technique) + { + var glTex = new Texture( this.getWorld(), value ); + this._glTextures[prop] = glTex; + glTex.render(); + var tex = glTex.getTexture(); + if (tex) technique[prop].set( tex ); + } + break; + + case "color": + case "vector2d": + case "vector3d": + this._propValues[prop] = value.slice(); + if (technique) technique[prop].set( value ); + break; + } + } }; this.setShaderValues = function() @@ -398,6 +404,15 @@ var Material = function GLMaterial( world ) { return tex; }; + this.gradientType = null; + + this.getGradientData = function() { + return null; + }; + + this.setGradientData = function() { + // override in linear-gradient-material and radial-gradient-material + }; }; if (typeof exports === "object") { diff --git a/js/lib/rdge/materials/radial-gradient-material.js b/js/lib/rdge/materials/radial-gradient-material.js index c9c2536f..c68cad43 100755 --- a/js/lib/rdge/materials/radial-gradient-material.js +++ b/js/lib/rdge/materials/radial-gradient-material.js @@ -113,7 +113,48 @@ var RadialGradientMaterial = function RadialGradientMaterial() { { jObj.u_texTransform = this._textureTransform.slice(); return jObj; - } + }; + + // Only Linear Gradient and Radial Gradient have gradient data. + this.gradientType = "radial"; + + this.getGradientData = function() { + var angle = Math.round(this._angle*180/Math.PI), + color, + colorStr, + css = "-webkit-gradient(linear, " + angle + "deg"; + + // TODO - Angle is not supported in -webkit-gradient syntax, so just default to across: + css = '-webkit-radial-gradient(50% 50%, ellipse cover'; + + // TODO - Also, Color Model requires from and to in the gradient string + for (var i=1; i < 5; i++) { + color = this.getProperty('u_color'+i); + colorStr = Math.round(color[0] * 255) + ', ' + Math.round(color[1] * 255) + ', ' + Math.round(color[2] * 255) + ', ' + Math.round(color[3] * 100); + css += ', rgba(' + colorStr + ') ' + Math.round(this.getProperty('u_colorStop'+i)*100) + '%'; + } + + css += ')'; + + return css; + }; + + this.setGradientData = function(colors) { + var len = colors.length; + // TODO - Current shaders only support 4 color stops + if (len > 4) { + len = 4; + } + + for (var n = 0; n < len; n++) { + var position = colors[n].position/100; + var cs = colors[n].value; + var stop = [cs.r/255, cs.g/255, cs.b/255, cs.a]; + + this.setProperty("u_color" + (n + 1), stop.slice(0)); + this.setProperty("u_colorStop" + (n + 1), position); + } + }; }; /////////////////////////////////////////////////////////////////////////////////////// -- cgit v1.2.3