aboutsummaryrefslogtreecommitdiff
path: root/js/lib/rdge/materials/material.js
diff options
context:
space:
mode:
authorValerio Virgillito2012-06-19 07:58:10 -0700
committerValerio Virgillito2012-06-19 07:58:10 -0700
commit483ad57efcd6475776f580c3af5b60e6deeaf781 (patch)
tree3bc8f2b21826c0ee6d37bc77753d45a882783cbf /js/lib/rdge/materials/material.js
parent918a4f5870e972b6e4e301c3237e065a1ffd26f5 (diff)
parentc65c532d081afd73e4a325abe4ab97c6726c02db (diff)
downloadninja-483ad57efcd6475776f580c3af5b60e6deeaf781.tar.gz
Merge pull request #298 from ericmueller/Textures
Textures
Diffstat (limited to 'js/lib/rdge/materials/material.js')
-rwxr-xr-xjs/lib/rdge/materials/material.js306
1 files changed, 188 insertions, 118 deletions
diff --git a/js/lib/rdge/materials/material.js b/js/lib/rdge/materials/material.js
index b96768b3..65448c0c 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;
@@ -38,16 +38,14 @@ var Material = function GLMaterial( world ) {
38 this._shader = null; 38 this._shader = null;
39 this._materialNode = null; 39 this._materialNode = null;
40 40
41 // vertex deformation variables
42 this._hasVertexDeformation = false;
43 this._vertexDeformationRange = [0, 0, 1, 1]; // (xMin, yMin, xMax, yMax)
44 this._vertexDeformationTolerance = 0.02;
45
41 /////////////////////////////////////////////////////////////////////// 46 ///////////////////////////////////////////////////////////////////////
42 // Property Accessors 47 // Property Accessors
43 /////////////////////////////////////////////////////////////////////// 48 ///////////////////////////////////////////////////////////////////////
44 this.getShininess = function() {
45 return this._shininess;
46 };
47
48 this.setShininess = function(s) {
49 this._shininess = s;
50 };
51 49
52 this.setName = function(n) { 50 this.setName = function(n) {
53 this._name = n; 51 this._name = n;
@@ -73,30 +71,6 @@ var Material = function GLMaterial( world ) {
73 return this._world; 71 return this._world;
74 }; 72 };
75 73
76 this.setAmbient = function(r, g, b, a) {
77 this._ambient = [r, g, b, a];
78 };
79
80 this.getAmbient = function() {
81 return [this._ambient[0], this._ambient[1], this._ambient[2], this._ambient[3]];
82 };
83
84 this.setDiffuse = function(r, g, b, a) {
85 this._diffuse = [r, g, b, a];
86 };
87
88 this.getDiffuse = function() {
89 return [this._diffuse[0], this._diffuse[1], this._diffuse[2], this._diffuse[3]];
90 };
91
92 this.setSpecular = function(r, g, b, a) {
93 this._specular = [r, g, b, a];
94 };
95
96 this.getSpecular = function() {
97 return [this._specular[0], this._specular[1], this._specular[2], this._specular[3]];
98 };
99
100 this.getShader = function() { 74 this.getShader = function() {
101 return this._shader; 75 return this._shader;
102 }; 76 };
@@ -111,6 +85,10 @@ var Material = function GLMaterial( world ) {
111 return false; 85 return false;
112 }; 86 };
113 87
88 this.getTechniqueName = function() {
89 return 'default'
90 };
91
114 // the vertex shader can apply deformations requiring refinement in 92 // the vertex shader can apply deformations requiring refinement in
115 // certain areas. 93 // certain areas.
116 this.hasVertexDeformation = function() { 94 this.hasVertexDeformation = function() {
@@ -161,6 +139,43 @@ var Material = function GLMaterial( world ) {
161 } 139 }
162 }; 140 };
163 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
164 this.validateProperty = function( prop, value ) { 179 this.validateProperty = function( prop, value ) {
165 var rtnVal = false; 180 var rtnVal = false;
166 try 181 try
@@ -187,6 +202,7 @@ var Material = function GLMaterial( world ) {
187 rtnVal = ((valType == "object") && (value.length >= 3)); 202 rtnVal = ((valType == "object") && (value.length >= 3));
188 break; 203 break;
189 204
205 case "angle":
190 case "float": 206 case "float":
191 rtnVal = (valType == "number"); 207 rtnVal = (valType == "number");
192 break; 208 break;
@@ -204,22 +220,148 @@ var Material = function GLMaterial( world ) {
204 console.log( "setting invalid material property: " + prop + ", value: " + value ); 220 console.log( "setting invalid material property: " + prop + ", value: " + value );
205 } 221 }
206 222
207 if (!rtnVal) { 223// if (!rtnVal && (prop != 'color')) {
208 console.log( "invalid material property: " + prop + " : " + value ); 224// console.log( "invalid material property: " + prop + " : " + value );
209 } 225// }
210 226
211 return rtnVal; 227 return rtnVal;
212 }; 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 );