aboutsummaryrefslogtreecommitdiff
path: root/js/lib/rdge/materials/flag-material.js
diff options
context:
space:
mode:
Diffstat (limited to 'js/lib/rdge/materials/flag-material.js')
-rw-r--r--js/lib/rdge/materials/flag-material.js249
1 files changed, 249 insertions, 0 deletions
diff --git a/js/lib/rdge/materials/flag-material.js b/js/lib/rdge/materials/flag-material.js
new file mode 100644
index 00000000..d1788fb8
--- /dev/null
+++ b/js/lib/rdge/materials/flag-material.js
@@ -0,0 +1,249 @@
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
8var Material = require("js/lib/rdge/materials/material").Material;
9var PulseMaterial = require("js/lib/rdge/materials/pulse-material").PulseMaterial;
10var Texture = require("js/lib/rdge/texture").Texture;
11
12var FlagMaterial = function FlagMaterial() {
13 ///////////////////////////////////////////////////////////////////////
14 // Instance variables
15 ///////////////////////////////////////////////////////////////////////
16 this._name = "FlagMaterial";
17 this._shaderName = "flag";
18
19 this._texMap = 'assets/images/us_flag.png';
20
21 this._time = 0.0;
22 this._dTime = 0.1;
23
24 this._speed = 1.0;
25 this._waveWidth = 1.0;
26 this._waveHeight = 1.0;
27
28 this._hasVertexDeformation = true;
29
30 ///////////////////////////////////////////////////////////////////////
31 // Properties
32 ///////////////////////////////////////////////////////////////////////
33 // all defined in parent PulseMaterial.js
34 // load the local default value
35 this._propNames = ["texmap", "wavewidth", "waveheight", "speed" ];
36 this._propLabels = ["Texture map", "Wave Width", "Wave Height", "Speed" ];
37 this._propTypes = ["file", "float", "float", "float" ];
38 this._propValues = [];
39
40 this._propValues[ this._propNames[0] ] = this._texMap.slice(0);
41 this._propValues[ this._propNames[1] ] = this._waveWidth;
42 this._propValues[ this._propNames[2] ] = this._waveHeight;
43 this._propValues[ this._propNames[3] ] = this._speed;
44
45
46 // a material can be animated or not. default is not.
47 // Any material needing continuous rendering should override this method
48 this.isAnimated = function() { return true; };
49
50 ///////////////////////////////////////////////////////////////////////
51 // Methods
52 ///////////////////////////////////////////////////////////////////////
53 // duplcate method requirde
54 this.dup = function( world )
55 {
56 // get the current values;
57 var propNames = [], propValues = [], propTypes = [], propLabels = [];
58 this.getAllProperties(propNames, propValues, propTypes, propLabels);
59
60 // allocate a new uber material
61 var newMat = new FlagMaterial();
62
63 // copy over the current values;
64 var n = propNames.length;
65 for (var i = 0; i < n; i++)
66 newMat.setProperty(propNames[i], propValues[i]);
67
68 return newMat;
69 };
70
71 this.setProperty = function( prop, value )
72 {
73 // make sure we have legitimate imput
74 var ok = this.validateProperty( prop, value );
75 if (!ok) {
76 console.log( "invalid property in Radial Gradient Material:" + prop + " : " + value );
77 }
78
79 switch (prop)
80 {
81 case "texmap":
82 this.setTextureMap(value);
83 break;
84
85 case "wavewidth":
86 this._waveWidth = value;
87 this._propValues[ this._propNames[1] ] = this._waveWidth;
88 this.updateParameters();
89 break;
90
91 case "waveheight":
92 this._waveHeight = value;
93 this._propValues[ this._propNames[2] ] = this._waveHeight;
94 this.updateParameters();
95 break;
96
97 case "speed":
98 this._speed = value;
99 this._propValues[ this._propNames[3] ] = this._speed;
100 this.updateParameters();
101 break;
102
103 case "color":
104 break;
105 }
106 };
107
108
109 this.updateParameters = function()
110 {
111 this._propValues[ this._propNames[1] ] = this._waveWidth;
112 this._propValues[ this._propNames[2] ] = this._waveHeight;
113 this._propValues[ this._propNames[3] ] = this._speed;
114
115 var material = this._materialNode;
116 if (material)
117 {
118 var technique = material.shaderProgram['default'];
119 var renderer = RDGE.globals.engine.getContext().renderer;
120 if (renderer && technique)
121 {
122
123 if (this._shader && this._shader['default']) {
124 this._shader['default'].u_speed.set( [this._speed] );
125 this._shader['default'].u_waveWidth.set( [this._waveWidth] );
126 this._shader['default'].u_waveHeight.set( [this._waveHeight] );
127 }
128 }
129 }
130 };
131
132
133 this.init = function( world )
134 {
135 // save the world
136 if (world) this.setWorld( world );
137
138 // set up the shader
139 this._shader = new RDGE.jshader();
140 this._shader.def = flagMaterialDef;
141 this._shader.init();
142
143 // set up the material node
144 this._materialNode = RDGE.createMaterialNode("flagMaterial" + "_" + world.generateUniqueNodeID());
145 this._materialNode.setShader(this._shader);
146
147 this._time = 0;
148 if (this._shader && this._shader['default']) {
149 this._shader['default'].u_time.set( [this._time] );
150 this._shader['default'].u_waveWidth.set( [this._propValues[ this._propNames[1] ]] );
151 this._shader['default'].u_waveHeight.set( [this._propValues[ this._propNames[2] ]] );
152 }
153
154 // set up the texture
155 var texMapName = this._propValues[this._propNames[0]];
156 this._glTex = new Texture( world, texMapName );
157
158 // set the shader values in the shader
159 this.updateParameters();
160 this.updateTexture();
161 this.update( 0 );
162 }
163
164 this.exportJSON = function () {
165 var jObj =
166 {
167 'material' : this.getShaderName(),
168 'name' : this.getName(),
169
170 'texMap' : this._propValues[this._propNames[0]],
171 'waveWidth' : this._propValues[this._propNames[1]],
172 'waveHeight' : this._propValues[this._propNames[2]],
173 'speed' : this._propValues[this._propNames[3]]
174 };
175
176 return jObj;
177 };
178
179 this.importJSON = function (jObj) {
180 if (this.getShaderName() != jObj.material) throw new Error("ill-formed material");
181 this.setName(jObj.name);
182
183 try {
184
185 this._texMap = this._propValues[this._propNames[0]] = jObj.texMap;
186 this._waveWidth = this._propValues[this._propNames[1]] = jObj.waveWidth;
187 this._waveHeight = this._propValues[this._propNames[2]] = jObj.waveHeight;
188 this._speed = this._propValues[this._propNames[3]] = jObj.speed;
189 }
190 catch (e) {
191 throw new Error("could not import material: " + importStr);
192 }
193 };
194};
195
196///////////////////////////////////////////////////////////////////////////////////////
197// RDGE shader
198
199// shader spec (can also be loaded from a .JSON file, or constructed at runtime)
200var flagMaterialDef =
201{'shaders':
202 {
203 'defaultVShader':"assets/shaders/Flag.vert.glsl",
204 'defaultFShader':"assets/shaders/Flag.frag.glsl"
205 },
206 'techniques':
207 {
208 'default':
209 [
210 {
211 'vshader' : 'defaultVShader',
212 'fshader' : 'defaultFShader',
213 // attributes
214 'attributes' :
215 {
216 'vert' : { 'type' : 'vec3' },
217 'normal' : { 'type' : 'vec3' },
218 'texcoord' : { 'type' : 'vec2' }
219 },
220 // parameters
221 'params' :
222 {
223 'u_tex0': { 'type' : 'tex2d' },