aboutsummaryrefslogtreecommitdiff
path: root/js/lib/rdge/materials/material.js
diff options
context:
space:
mode:
Diffstat (limited to 'js/lib/rdge/materials/material.js')
-rwxr-xr-xjs/lib/rdge/materials/material.js219
1 files changed, 176 insertions, 43 deletions
diff --git a/js/lib/rdge/materials/material.js b/js/lib/rdge/materials/material.js
index 856513e2..d2586b58 100755
--- a/js/lib/rdge/materials/material.js
+++ b/js/lib/rdge/materials/material.js
@@ -4,6 +4,9 @@ No rights, expressed or implied, whatsoever to this software are provided by Mot
4(c) Copyright 2011 Motorola Mobility, Inc. All Rights Reserved. 4(c) Copyright 2011 Motorola Mobility, Inc. All Rights Reserved.
5</copyright> */ 5</copyright> */
6 6
7var Texture = require("js/lib/rdge/texture").Texture;
8
9
7/////////////////////////////////////////////////////////////////////// 10///////////////////////////////////////////////////////////////////////
8// Class GLMaterial 11// Class GLMaterial
9// GL representation of a material. 12// GL representation of a material.
@@ -15,19 +18,16 @@ var Material = function GLMaterial( world ) {
15 this._name = "GLMaterial"; 18 this._name = "GLMaterial";
16 this._shaderName = "undefined"; 19 this._shaderName = "undefined";
17 20
21 this._time = 0.0;
22 this._dTime = 0.01;
23
18 // keep a reference to the owning GLWorld 24 // keep a reference to the owning GLWorld
19 this._world = null; 25 this._world = null;
20 if(world) { 26 if(world) {
21 this._world = world; 27 this._world = world;
22 } 28 }
23 29
24 this._shininess = 60; 30 this._glTextures = []; // indexed by uniform name
25
26 this._ambient = [0.0, 0.0, 0.0, 1.0];
27 this._diffuse = [0.0, 0.0, 0.0, 1.0];
28 this._specular = [0.0, 0.0, 0.0, 1.0];
29
30 this._texture = null;
31 31
32 // vertex deformation variables 32 // vertex deformation variables
33 this._hasVertexDeformation = false; 33 this._hasVertexDeformation = false;
@@ -46,13 +46,6 @@ var Material = function GLMaterial( world ) {
46 /////////////////////////////////////////////////////////////////////// 46 ///////////////////////////////////////////////////////////////////////
47 // Property Accessors 47 // Property Accessors
48 /////////////////////////////////////////////////////////////////////// 48 ///////////////////////////////////////////////////////////////////////
49 this.getShininess = function() {
50 return this._shininess;
51 };
52
53 this.setShininess = function(s) {
54 this._shininess = s;
55 };
56 49
57 this.setName = function(n) { 50 this.setName = function(n) {
58 this._name = n; 51 this._name = n;
@@ -78,30 +71,6 @@ var Material = function GLMaterial( world ) {
78 return this._world; 71 return this._world;
79 }; 72 };
80 73
81 this.setAmbient = function(r, g, b, a) {
82 this._ambient = [r, g, b, a];
83 };
84
85 this.getAmbient = function() {
86 return [this._ambient[0], this._ambient[1], this._ambient[2], this._ambient[3]];
87 };
88
89 this.setDiffuse = function(r, g, b, a) {
90 this._diffuse = [r, g, b, a];
91 };
92
93 this.getDiffuse = function() {
94 return [this._diffuse[0], this._diffuse[1], this._diffuse[2], this._diffuse[3]];
95 };
96
97 this.setSpecular = function(r, g, b, a) {
98 this._specular = [r, g, b, a];
99 };
100
101 this.getSpecular = function() {
102 return [this._specular[0], this._specular[1], this._specular[2], this._specular[3]];
103 };
104
105 this.getShader = function() { 74 this.getShader = function() {
106 return this._shader; 75 return this._shader;
107 }; 76 };
@@ -116,6 +85,10 @@ var Material = function GLMaterial( world ) {
116 return false; 85 return false;
117 }; 86 };
118 87
88 this.getTechniqueName = function() {
89 return 'default'
90 };
91
119 // the vertex shader can apply deformations requiring refinement in 92 // the vertex shader can apply deformations requiring refinement in
120 // certain areas. 93 // certain areas.
121 this.hasVertexDeformation = function() { 94 this.hasVertexDeformation = function() {
@@ -166,6 +139,43 @@ var Material = function GLMaterial( world ) {
166 } 139 }
167 }; 140 };
168 141
142 this.hasProperty = function( prop )
143 {
144 var propNames = [], dummy = [];
145 this.getAllProperties( propNames, dummy, dummy, dummy )
146 for (var i=0; i<propNames.length; i++)
147 {
148 if (prop === propNames[i]) return true;
149 }
150 };
151
152 this.getPropertyType = function( prop )
153 {
154 var n = this.getPropertyCount();
155 for (var i=0; i<n; i++)
156 {
157 if (prop === this._propNames[i]) return this._propTypes[i];
158 }
159 };
160
161 this.dup = function ()
162 {
163 // get the current values;
164 var propNames = [], propValues = [], propTypes = [], propLabels = [];
165 this.getAllProperties(propNames, propValues, propTypes, propLabels);
166
167 // allocate a new material
168 var MaterialLibrary = require("js/models/materials-model").MaterialsModel;
169 var newMat = MaterialLibrary.createMaterialByShaderName( this.getShaderName() );
170
171 // copy over the current values;
172 var n = propNames.length;
173 for (var i = 0; i < n; i++)
174 newMat.setProperty(propNames[i], propValues[i]);
175
176 return newMat;
177 };
178
169 this.validateProperty = function( prop, value ) { 179 this.validateProperty = function( prop, value ) {
170 var rtnVal = false; 180 var rtnVal = false;
171 try 181 try
@@ -216,16 +226,139 @@ var Material = function GLMaterial( world ) {
216 226
217 return rtnVal; 227 return rtnVal;
218 }; 228 };
229
230 this.setProperty = function( prop, value )
231 {
232 var ok = this.validateProperty( prop, value );
233 if (!ok && (prop != 'color')) {
234 console.log( "invalid property in Material:" + prop + " : " + value );
235 return;
236 }
237
238 // get the technique if the shader is instantiated
239 var technique;
240 var material = this._materialNode;
241 if (material) technique = material.shaderProgram[this.getTechniqueName()];
242
243 switch (this.getPropertyType(prop))
244 {
245 case "angle":
246 case "float":
247 this._propValues[prop] = value;
248 if (technique) technique[prop].set( [value] );
249 break;
250
251 case "file":
252 this._propValues[prop] = value.slice();
253 if (technique)
254 {
255 var glTex = new Texture( this.getWorld(), value );
256 this._glTextures[prop] = glTex;
257 glTex.render();
258 var tex = glTex.getTexture();
259 if (tex) technique[prop].set( tex );
260 }
261 break;
262
263 case "color":
264 case "vector2d":
265 case "vector3d":
266 this._propValues[prop] = value.slice();
267 if (technique) technique[prop].set( value );
268 break;
269 }
270 };
271
272 this.setShaderValues = function()
273 {
274 var material = this._materialNode;
275 if (material)
276 {
277 var technique = material.shaderProgram[this.getTechniqueName()];
278 if (technique)
279 {
280 var n = this.getPropertyCount();
281 for (var i=0; i<n; i++)
282 {
283 var prop = this._propNames[i],
284 value = this._propValues[prop];
285
286 switch (this._propTypes[i])
287 {
288 case "angle":
289 case "float":
290 technique[prop].set( [value] );
291 break;
292
293 case "file":
294 var glTex = this._glTextures[prop];
295 if (glTex)
296 {
297 var tex = glTex.getTexture();
298 if (tex) technique[prop].set( tex );
299 }
300 else
301 this.setProperty( prop, value );
302 break;
303
304 case "color":
305 case "vector2d":