aboutsummaryrefslogtreecommitdiff
path: root/js/lib/rdge/materials/flat-material.js
diff options
context:
space:
mode:
authorJon Reid2012-03-06 14:15:18 -0800
committerJon Reid2012-03-06 14:15:18 -0800
commit78d42c7cbf9205ed2e4faee1d8ddf02a4725edd3 (patch)
treec395b703f21431dcb4432c725f4c860431add61b /js/lib/rdge/materials/flat-material.js
parentfa8b81d4f436c0b8564c8dcdd22d8dd6d5bccfaa (diff)
parent60bba95eaffa8b5c741c6c85fb84b327cd75d6c3 (diff)
downloadninja-78d42c7cbf9205ed2e4faee1d8ddf02a4725edd3.tar.gz
Merge branch 'Timeline-uber' into timeline-serialized
Diffstat (limited to 'js/lib/rdge/materials/flat-material.js')
-rwxr-xr-xjs/lib/rdge/materials/flat-material.js159
1 files changed, 159 insertions, 0 deletions
diff --git a/js/lib/rdge/materials/flat-material.js b/js/lib/rdge/materials/flat-material.js
new file mode 100755
index 00000000..be8bf2b8
--- /dev/null
+++ b/js/lib/rdge/materials/flat-material.js
@@ -0,0 +1,159 @@
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
7var MaterialParser = require("js/lib/rdge/materials/material-parser").MaterialParser;
8var Material = require("js/lib/rdge/materials/material").Material;
9///////////////////////////////////////////////////////////////////////
10// Class GLMaterial
11// RDGE representation of a material.
12///////////////////////////////////////////////////////////////////////
13var FlatMaterial = function FlatMaterial() {
14 ///////////////////////////////////////////////////////////////////////
15 // Instance variables
16 ///////////////////////////////////////////////////////////////////////
17 this._name = "FlatMaterial";
18 this._shaderName = "flat";
19
20 this._color = [1,0,0,1];
21
22 ///////////////////////////////////////////////////////////////////////
23 // Property Accessors
24 ///////////////////////////////////////////////////////////////////////
25 this.getColor = function() { return this._color; };
26 this.getShaderName = function() { return this._shaderName; };
27
28 this.isAnimated = function() { return false; };
29 this.hasVertexDeformation = function() { return true; };
30 this._hasVertexDeformation = true;
31 this._vertexDeformationTolerance = 0.2;
32
33 //////////////////////////////////s/////////////////////////////////////
34 // Methods
35 ///////////////////////////////////////////////////////////////////////
36 // duplcate method requirde
37 this.dup = function() { return new FlatMaterial(); } ;
38
39 this.init = function()
40 {
41 // set up the shader
42 this._shader = new jshader();
43 this._shader.def = flatShaderDef;
44 this._shader.init();
45
46 // set the defaults
47 this._shader.colorMe.color.set( this.getColor() );
48
49 // set up the material node
50 this._materialNode = createMaterialNode("flatMaterial");
51 this._materialNode.setShader(this._shader);
52 };
53
54
55 ///////////////////////////////////////////////////////////////////////
56 // Material Property Accessors
57 ///////////////////////////////////////////////////////////////////////
58 this._propNames = ["color"];
59 this._propLabels = ["Color"];
60 this._propTypes = ["color"];
61 this._propValues = [];
62
63 this._propValues[ this._propNames[0] ] = this._color;
64
65 this.setProperty = function( prop, value ) {
66 // make sure we have legitimate input
67 if (this.validateProperty( prop, value )) {
68 this._propValues[prop] = value;
69 if (this._shader && this._shader.colorMe) {
70 this._shader.colorMe[prop].set(value);
71 }
72 }
73 };
74 ///////////////////////////////////////////////////////////////////////
75
76 this.export = function() {
77 // this function should be overridden by subclasses
78 var exportStr = "material: " + this.getShaderName() + "\n";
79 exportStr += "name: " + this.getName() + "\n";
80
81 if (this._shader)
82 exportStr += "color: " + String(this._shader.colorMe.color) + "\n";
83 else
84 exportStr += "color: " + this.getColor() + "\n";
85 exportStr += "endMaterial\n";
86
87 return exportStr;
88 };
89
90 this.import = function( importStr ) {
91 var pu = new MaterialParser( importStr );
92 var material = pu.nextValue( "material: " );
93 if (material != this.getShaderName()) throw new Error( "ill-formed material" );
94 this.setName( pu.nextValue( "name: ") );
95
96 var rtnStr;
97 try
98 {
99 var color = eval( "[" + pu.nextValue( "color: " ) + "]" );
100
101 this.setProperty( "color", color);
102
103 var endKey = "endMaterial\n";
104 var index = importStr.indexOf( endKey );
105 index += endKey.length;
106 rtnStr = importStr.substr( index );
107 }
108 catch (e)
109 {
110 throw new Error( "could not import material: " + importStr );
111 }
112
113 return rtnStr;
114 };
115
116 this.update = function( time )
117 {
118 };
119
120};
121
122///////////////////////////////////////////////////////////////////////////////////////
123// RDGE shader
124
125// shader spec (can also be loaded from a .JSON file, or constructed at runtime)
126flatShaderDef =
127{
128 'shaders': { // shader files
129 'defaultVShader':"assets/shaders/Basic.vert.glsl",
130 'defaultFShader':"assets/shaders/Basic.frag.glsl"
131 },
132 'techniques': { // rendering control
133 'colorMe':[ // simple color pass
134 {
135 'vshader' : 'defaultVShader',
136 'fshader' : 'defaultFShader',
137
138 // attributes
139 'attributes' :
140 {
141 'vert' : { 'type' : 'vec3' },
142 'normal' : { 'type' : 'vec3' },
143 'texcoord' : { 'type' : 'vec2' }
144 },
145 // attributes
146 'params' :
147 {
148 'color' : { 'type' : 'vec4' }
149 }
150 }
151 ]
152 }
153};
154
155FlatMaterial.prototype = new Material();
156
157if (typeof exports === "object") {
158 exports.FlatMaterial = FlatMaterial;
159}