diff options
Diffstat (limited to 'js/helper-classes/RDGE/Materials/FlatMaterial.js')
-rw-r--r-- | js/helper-classes/RDGE/Materials/FlatMaterial.js | 149 |
1 files changed, 149 insertions, 0 deletions
diff --git a/js/helper-classes/RDGE/Materials/FlatMaterial.js b/js/helper-classes/RDGE/Materials/FlatMaterial.js new file mode 100644 index 00000000..5177a8a0 --- /dev/null +++ b/js/helper-classes/RDGE/Materials/FlatMaterial.js | |||
@@ -0,0 +1,149 @@ | |||
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 | /////////////////////////////////////////////////////////////////////// | ||
12 | function FlatMaterial() | ||
13 | { | ||
14 | // initialize the inherited members | ||
15 | this.inheritedFrom = GLMaterial; | ||
16 | this.inheritedFrom(); | ||
17 | |||
18 | /////////////////////////////////////////////////////////////////////// | ||
19 | // Instance variables | ||
20 | /////////////////////////////////////////////////////////////////////// | ||
21 | this._name = "FlatMaterial"; | ||
22 | this._shaderName = "flat"; | ||
23 | |||
24 | this._color = [1,0,0,1]; | ||
25 | |||
26 | /////////////////////////////////////////////////////////////////////// | ||
27 | // Property Accessors | ||
28 | /////////////////////////////////////////////////////////////////////// | ||
29 | this.getColor = function() { return this._color; } | ||
30 | this.getShaderName = function() { return this._shaderName; } | ||
31 | |||
32 | //////////////////////////////////s///////////////////////////////////// | ||
33 | // Methods | ||
34 | /////////////////////////////////////////////////////////////////////// | ||
35 | // duplcate method requirde | ||
36 | this.dup = function() { return new FlatMaterial(); } | ||
37 | |||
38 | this.init = function() | ||
39 | { | ||
40 | // set up the shader | ||
41 | this._shader = new jshader(); | ||
42 | this._shader.def = flatShaderDef; | ||
43 | this._shader.init(); | ||
44 | |||
45 | // set the defaults | ||
46 | this._shader.colorMe.color.set( this.getColor() ); | ||
47 | |||
48 | // set up the material node | ||
49 | this._materialNode = createMaterialNode("flatMaterial"); | ||
50 | this._materialNode.setShader(this._shader); | ||
51 | } | ||
52 | |||
53 | |||
54 | /////////////////////////////////////////////////////////////////////// | ||
55 | // Material Property Accessors | ||
56 | /////////////////////////////////////////////////////////////////////// | ||
57 | this._propNames = ["color"]; | ||
58 | this._propLabels = ["Color"]; | ||
59 | this._propTypes = ["color"]; | ||
60 | this._propValues = []; | ||
61 | |||
62 | this._propValues[ this._propNames[0] ] = this._color; | ||
63 | |||
64 | this.setProperty = function( prop, value ) | ||
65 | { | ||
66 | // make sure we have legitimate imput | ||
67 | if (this.validateProperty( prop, value )) | ||
68 | { | ||
69 | this._color = value.slice(0); | ||
70 | this._shader.colorMe[prop].set(value); | ||
71 | } | ||
72 | } | ||
73 | /////////////////////////////////////////////////////////////////////// | ||
74 | |||
75 | this.export = function() | ||
76 | { | ||
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 | { | ||
92 | var pu = new ParseUtils( importStr ); | ||
93 | var material = pu.nextValue( "material: " ); | ||
94 | if (material != this.getShaderName()) throw new Error( "ill-formed material" ); | ||
95 | this.setName( pu.nextValue( "material: ") ); | ||
96 | var color = pu.nextValue( "color: " ); | ||
97 | |||
98 | var endKey = "endMaterial\n"; | ||
99 | var index = importStr.indexOf( endKey ) + endKey.len; | ||
100 | var rtnStr = importStr.substr( index ); | ||
101 | return rtnStr; | ||
102 | } | ||
103 | } | ||
104 | |||
105 | // used to create unique names | ||
106 | var flatMaterialCounter = 0; | ||
107 | |||
108 | /////////////////////////////////////////////////////////////////////////////////////// | ||
109 | // RDGE shader | ||
110 | |||
111 | // shader spec (can also be loaded from a .JSON file, or constructed at runtime) | ||
112 | flatShaderDef = | ||
113 | { | ||
114 | 'shaders': { // shader files | ||
115 | 'defaultVShader': "\ | ||
116 | uniform mat4 u_mvMatrix;\ | ||
117 | uniform mat4 u_projMatrix;\ | ||
118 | attribute vec3 a_pos;\ | ||
119 | void main() {\ | ||
120 | gl_Position = u_projMatrix * u_mvMatrix * vec4(a_pos,1.0);\ | ||
121 | }", | ||
122 | 'defaultFShader': "\ | ||
123 | precision highp float;\ | ||
124 | uniform vec4 color;\ | ||
125 | void main() {\ | ||
126 | gl_FragColor = color;\ | ||
127 | }", | ||
128 | }, | ||
129 | 'techniques': { // rendering control | ||
130 | 'colorMe':[ // simple color pass | ||
131 | { | ||
132 | 'vshader' : 'defaultVShader', | ||
133 | 'fshader' : 'defaultFShader', | ||
134 | |||
135 | // attributes | ||
136 | 'attributes' : | ||
137 | { | ||
138 | 'a_pos' : { 'type' : 'vec3' } // only using position for this shader | ||
139 | }, | ||
140 | // attributes | ||
141 | 'params' : | ||
142 | { | ||
143 | 'color' : { 'type' : 'vec4' } | ||
144 | }, | ||
145 | }, | ||
146 | ] | ||
147 | } | ||
148 | }; | ||
149 | |||