aboutsummaryrefslogtreecommitdiff
path: root/js/lib/rdge/materials/flat-material.js
diff options
context:
space:
mode:
Diffstat (limited to 'js/lib/rdge/materials/flat-material.js')
-rwxr-xr-xjs/lib/rdge/materials/flat-material.js151
1 files changed, 71 insertions, 80 deletions
diff --git a/js/lib/rdge/materials/flat-material.js b/js/lib/rdge/materials/flat-material.js
index e1363c06..106641a9 100755
--- a/js/lib/rdge/materials/flat-material.js
+++ b/js/lib/rdge/materials/flat-material.js
@@ -14,129 +14,120 @@ var FlatMaterial = function FlatMaterial() {
14 /////////////////////////////////////////////////////////////////////// 14 ///////////////////////////////////////////////////////////////////////
15 // Instance variables 15 // Instance variables
16 /////////////////////////////////////////////////////////////////////// 16 ///////////////////////////////////////////////////////////////////////
17 this._name = "FlatMaterial"; 17 this._name = "FlatMaterial";
18 this._shaderName = "flat"; 18 this._shaderName = "flat";
19 19
20 this._color = [1,0,0,1]; 20 this._color = [1, 0, 0, 1];
21 21
22 /////////////////////////////////////////////////////////////////////// 22 ///////////////////////////////////////////////////////////////////////
23 // Property Accessors 23 // Property Accessors
24 /////////////////////////////////////////////////////////////////////// 24 ///////////////////////////////////////////////////////////////////////
25 this.getColor = function() { return this._color; }; 25 this.getColor = function () { return this._color; };
26 this.getShaderName = function() { return this._shaderName; }; 26 this.getShaderName = function () { return this._shaderName; };
27 27
28 this.isAnimated = function() { return false; }; 28 this.isAnimated = function () { return false; };
29 this.hasVertexDeformation = function() { return true; }; 29 this.hasVertexDeformation = function () { return true; };
30 this._hasVertexDeformation = true; 30 this._hasVertexDeformation = true;
31 this._vertexDeformationTolerance = 0.2; 31 this._vertexDeformationTolerance = 0.2;
32 32
33 //////////////////////////////////s///////////////////////////////////// 33 //////////////////////////////////s/////////////////////////////////////
34 // Methods 34 // Methods
35 /////////////////////////////////////////////////////////////////////// 35 ///////////////////////////////////////////////////////////////////////
36 // duplcate method requirde 36 // duplcate method requirde
37 this.dup = function() { return new FlatMaterial(); } ; 37 this.dup = function () { return new FlatMaterial(); };
38 38
39 this.init = function( world ) 39 this.init = function (world) {
40 { 40 // save the world
41 // save the world 41 if (world) {
42 if (world) 42 this.setWorld(world);
43 {
44 this.setWorld( world );
45 43
46 // set up the shader 44 // set up the shader
47 this._shader = new RDGE.jshader(); 45 this._shader = new RDGE.jshader();
48 this._shader.def = flatShaderDef; 46 this._shader.def = flatShaderDef;
49 this._shader.init(); 47 this._shader.init();
50 48
51 // set the defaults 49 // set the defaults
52 this._shader.colorMe.color.set( this.getColor() ); 50 this._shader.colorMe.color.set(this.getColor());
53 51
54 // set up the material node 52 // set up the material node
55 this._materialNode = RDGE.createMaterialNode("flatMaterial_" + world.generateUniqueNodeID() ); 53 this._materialNode = RDGE.createMaterialNode("flatMaterial_" + world.generateUniqueNodeID());
56 this._materialNode.setShader(this._shader); 54 this._materialNode.setShader(this._shader);
57 } 55 }
58 else 56 else
59 throw new Error( "GLWorld not supplied to material initialization" ); 57 throw new Error("GLWorld not supplied to material initialization");
60 }; 58 };
61 59
62 60
63 /////////////////////////////////////////////////////////////////////// 61 ///////////////////////////////////////////////////////////////////////
64 // Material Property Accessors 62 // Material Property Accessors
65 /////////////////////////////////////////////////////////////////////// 63 ///////////////////////////////////////////////////////////////////////
66 this._propNames = ["color"]; 64 this._propNames = ["color"];
67 this._propLabels = ["Color"]; 65 this._propLabels = ["Color"];
68 this._propTypes = ["color"]; 66 this._propTypes = ["color"];
69 this._propValues = []; 67 this._propValues = [];
70 68
71 this._propValues[ this._propNames[0] ] = this._color; 69 this._propValues[this._propNames[0]] = this._color;
72 70
73 this.setProperty = function( prop, value ) { 71 this.setProperty = function (prop, value) {
74 // make sure we have legitimate input 72 // make sure we have legitimate input
75 if (this.validateProperty( prop, value )) { 73 if (this.validateProperty(prop, value)) {
76 this._propValues[prop] = value; 74 this._propValues[prop] = value;
77 if (this._shader && this._shader.colorMe) { 75 if (this._shader && this._shader.colorMe) {
78 this._shader.colorMe[prop].set(value); 76 this._shader.colorMe[prop].set(value);
79 } 77 }
80 } 78 }
81 }; 79 };
82 /////////////////////////////////////////////////////////////////////// 80 ///////////////////////////////////////////////////////////////////////
83 81
84 this.export = function() 82 this.export = function () {
85 { 83 // this function should be overridden by subclasses
86 // this function should be overridden by subclasses 84 var exportStr = "material: " + this.getShaderName() + "\n";
87 var exportStr = "material: " + this.getShaderName() + "\n"; 85 exportStr += "name: " + this.getName() + "\n";
88 exportStr += "name: " + this.getName() + "\n"; 86 exportStr += "color: " + String(this._propValues["color"]) + "\n";
89 exportStr += "color: " + String(this._propValues["color"]) + "\n"; 87 exportStr += "endMaterial\n";
90 exportStr += "endMaterial\n";
91 88
92 return exportStr; 89 return exportStr;
93 }; 90 };
94 91
95 this.import = function( importStr ) { 92 this.import = function (importStr) {
96 var pu = new MaterialParser( importStr ); 93 var pu = new MaterialParser(importStr);
97 var material = pu.nextValue( "material: " ); 94 var material = pu.nextValue("material: ");
98 if (material != this.getShaderName()) throw new Error( "ill-formed material" ); 95 if (material != this.getShaderName()) throw new Error("ill-formed material");
99 this.setName( pu.nextValue( "name: ") ); 96 this.setName(pu.nextValue("name: "));
100 97
101 var rtnStr; 98 var rtnStr;
102 try 99 try {
103 { 100 var color = eval("[" + pu.nextValue("color: ") + "]");
104 var color = eval( "[" + pu.nextValue( "color: " ) + "]" ); 101 this.setProperty("color", color);
105 this.setProperty( "color", color);
106 } 102 }
107 catch (e) 103 catch (e) {
108 { 104 throw new Error("could not import material: " + importStr);
109 throw new Error( "could not import material: " + importStr );
110 } 105 }
111 106
112 return rtnStr; 107 return rtnStr;
113 }; 108 };
114 109
115 this.exportJSON = function() 110 this.exportJSON = function () {
116 { 111 var jObj =
117 var jObj =
118 { 112 {
119 'material' : this.getShaderName(), 113 'material': this.getShaderName(),
120 'name' : this.getName(), 114 'name': this.getName(),
121 'color' : this._propValues["color"] 115 'color': this._propValues["color"]
122 }; 116 };
123 117
124 return jObj; 118 return jObj;
125 } 119 };
126 120
127 this.importJSON = function( jObj ) 121 this.importJSON = function (jObj) {
128 { 122 if (this.getShaderName() != jObj.material) throw new Error("ill-formed material");
129 if (this.getShaderName() != jObj.material) throw new Error( "ill-formed material" ); 123 this.setName(jObj.name);
130 this.setName( jObj.name );
131
132 var color = jObj.color;
133 this.setProperty( "color", color);
134 }
135 124
136 this.update = function( time ) 125 var color = jObj.color;
137 { 126 this.setProperty("color", color);
138 }; 127 };
139 128
129 this.update = function (time) {
130 };
140}; 131};
141 132
142/////////////////////////////////////////////////////////////////////////////////////// 133///////////////////////////////////////////////////////////////////////////////////////