aboutsummaryrefslogtreecommitdiff
path: root/js/helper-classes/RDGE/Materials/LinearGradientMaterial.js
diff options
context:
space:
mode:
Diffstat (limited to 'js/helper-classes/RDGE/Materials/LinearGradientMaterial.js')
-rw-r--r--js/helper-classes/RDGE/Materials/LinearGradientMaterial.js339
1 files changed, 339 insertions, 0 deletions
diff --git a/js/helper-classes/RDGE/Materials/LinearGradientMaterial.js b/js/helper-classes/RDGE/Materials/LinearGradientMaterial.js
new file mode 100644
index 00000000..357ce275
--- /dev/null
+++ b/js/helper-classes/RDGE/Materials/LinearGradientMaterial.js
@@ -0,0 +1,339 @@
1/* <copyright>
2 This file contains proprietary software owned by Motorola Mobility, Inc.<br/>
3 No rights, expressed or implied, whatsoever to this software are provided by Motorola Mobility, Inc. hereunder.<br/>
4 (c) Copyright 2011 Motorola Mobility, Inc. All Rights Reserved.
5 </copyright> */
6
7
8///////////////////////////////////////////////////////////////////////
9// Class GLMaterial
10// RDGE representation of a material.
11///////////////////////////////////////////////////////////////////////
12function LinearGradientMaterial()
13{
14 // initialize the inherited members
15 this.inheritedFrom = GLMaterial;
16 this.inheritedFrom();
17
18 ///////////////////////////////////////////////////////////////////////
19 // Instance variables
20 ///////////////////////////////////////////////////////////////////////
21 this._name = "LinearGradientMaterial";
22 this._shaderName = "linearGradient";
23
24 this._color1 = [1,0,0,1];
25 this._color2 = [0,1,0,1];
26 this._color3 = [0,0,1,1];
27 this._color4 = [0,1,1,1];
28 this._colorStop1 = 0.0;
29 this._colorStop2 = 0.3;
30 this._colorStop3 = 0.6;
31 this._colorStop4 = 1.0;
32 this._colorCount = 4;
33 this._angle = 0.0; // the shader takes [cos(a), sin(a)]
34
35 ///////////////////////////////////////////////////////////////////////
36 // Property Accessors
37 ///////////////////////////////////////////////////////////////////////
38 this.getShaderName = function() { return this._shaderName; }
39 this.getName = function() { return this._name; }
40
41 this.getColor1 = function() { return this._color1; }
42 this.setColor1 = function(c) { this._color1 = c.slice();
43 if (this._shader && this._shader.default)
44 this._shader.default.u_color1.set(c);
45 }
46
47 this.getColor2 = function() { return this._color2; }
48 this.setColor2 = function(c) { this._color2 = c.slice();
49 if (this._shader && this._shader.default)
50 this._shader.default.u_color2.set(c);
51 }
52
53 this.getColor3 = function() { return this._color3; }
54 this.setColor3 = function(c) { this._color3 = c.slice();
55 if (this._shader && this._shader.default)
56 this._shader.default.u_color3.set(c);
57 }
58
59 this.getColor4 = function() { return this._color4; }
60 this.setColor4 = function(c) { this._color4 = c.slice();
61 if (this._shader && this._shader.default)
62 this._shader.default.u_color4.set(c);
63 }
64
65 this.getColorStop1 = function() { return this._colorStop1; }
66 this.setColorStop1 = function(s) { this._colorStop1 = s;
67 if (this._shader && this._shader.default)
68 this._shader.default.u_colorStop1.set([s]);
69 }
70
71 this.getColorStop2 = function() { return this._colorStop2; }
72 this.setColorStop2 = function(s) { this._colorStop2 = s;
73 if (this._shader && this._shader.default)
74 this._shader.default.u_colorStop2.set([s]);
75 }
76
77 this.getColorStop3 = function() { return this._colorStop3; }
78 this.setColorStop3 = function(s) { this._colorStop3 = s;
79 if (this._shader && this._shader.default)
80 this._shader.default.u_colorStop3.set([s]);
81 }
82
83 this.getColorStop4 = function() { return this._colorStop4; }
84 this.setColorStop4 = function(s) { this._colorStop4 = s;
85 if (this._shader && this._shader.default)
86 this._shader.default.u_colorStop4.set([s]);
87 }
88
89 this.getColorCount = function() { return this._colorCount; }
90 this.setColorCount = function(c) { this._colorCount = c;
91 if (this._shader && this._shader.default)
92 this._shader.default.u_colorCount.set([c]);
93 }
94
95 this.getAngle = function() { return this._angle; }
96 this.setAngle = function(a) { this._angle = a;
97 if (this._shader && this._shader.default)
98 this._shader.default.u_cos_sin_angle.set([Math.cos(a), Math.sin(a)]);
99 }
100
101 ///////////////////////////////////////////////////////////////////////
102 // Material Property Accessors
103 ///////////////////////////////////////////////////////////////////////
104 this._propNames = ["color1", "color2", "angle"];
105 this._propLabels = ["Start Color", "Stop Color", "Angle"];
106 this._propTypes = ["color", "color", "float"];
107 this._propValues = [];
108
109 this._propValues[ this._propNames[0] ] = this._color1.slice(0);
110 this._propValues[ this._propNames[1] ] = this._color4.slice(0);
111 this._propValues[ this._propNames[2] ] = this._angle;
112
113 this.setProperty = function( prop, value )
114 {
115 if (prop === "color") prop = "color1";
116
117 // make sure we have legitimate imput
118 var ok = this.validateProperty( prop, value );
119 if (!ok)
120 console.log( "invalid property in Linear Gradient Material" + prop + " : " + value );
121
122 switch (prop)
123 {
124 case "color1": this.setColor1( value ); break;
125 case "color2": this.setColor2( value ); break;
126 case "angle": this.setAngle( value ); break;
127 }
128 }
129
130 ///////////////////////////////////////////////////////////////////////
131 // Methods
132 ///////////////////////////////////////////////////////////////////////
133 // duplcate method requirde
134 this.dup = function() { return new LinearGradientMaterial(); }
135
136 this.init = function()
137 {
138 // set up the shader
139 this._shader = new jshader();
140 this._shader.def = linearGradientMaterialDef;
141 this._shader.init();
142
143 // set up the material node
144 this._materialNode = createMaterialNode( this.getShaderName() );
145 this._materialNode.setShader(this._shader);
146
147 // send the current values to the shader
148 this.updateShaderValues();
149 }
150
151 this.updateShaderValues= function()
152 {
153 if (this._shader && this._shader.default)
154 {
155 //this._shader.default.u_colorCount.set( [4] );
156
157 var c;
158 c = this.getColor1();
159 this._shader.default.u_color1.set( c );
160 c = this.getColor2();
161 this._shader.default.u_color2.set( c );
162 c = this.getColor3();
163 this._shader.default.u_color3.set( c );
164 c = this.getColor4();
165 this._shader.default.u_color4.set( c );
166
167 var s;
168 s = this.getColorStop1();
169 this._shader.default.u_colorStop1.set( [s] );
170 s = this.getColorStop2();
171 this._shader.default.u_colorStop2.set( [s] );
172 s = this.getColorStop3();
173 this._shader.default.u_colorStop3.set( [s] );
174 s = this.getColorStop4();
175 this._shader.default.u_colorStop4.set( [s] );
176
177 this.setAngle( this.getAngle() );
178 }
179 }
180
181 this.export = function()
182 {
183 // every material needs the base type and instance name
184 var exportStr = "material: " + this.getShaderName() + "\n";
185 exportStr += "name: " + this.getName() + "\n";
186
187 exportStr += "startColor: " + this.getStartColor() + "\n";
188 exportStr += "stopColor: " + this.getStopColor() + "\n";
189 exportStr += "angle: " + this.getAngle() + "\n";
190
191 // every material needs to terminate like this
192 exportStr += "endMaterial\n";
193
194 return exportStr;
195 }
196
197 this.import = function( importStr )
<