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.js218
1 files changed, 218 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..fa844cc3
--- /dev/null
+++ b/js/lib/rdge/materials/flag-material.js
@@ -0,0 +1,218 @@
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
165///////////////////////////////////////////////////////////////////////////////////////
166// RDGE shader
167
168// shader spec (can also be loaded from a .JSON file, or constructed at runtime)
169var flagMaterialDef =
170{'shaders':
171 {
172 'defaultVShader':"assets/shaders/Flag.vert.glsl",
173 'defaultFShader':"assets/shaders/Flag.frag.glsl"
174 },
175 'techniques':
176 {
177 'default':
178 [
179 {
180 'vshader' : 'defaultVShader',
181 'fshader' : 'defaultFShader',
182 // attributes
183 'attributes' :
184 {
185 'vert' : { 'type' : 'vec3' },
186 'normal' : { 'type' : 'vec3' },
187 'texcoord' : { 'type' : 'vec2' }
188 },
189 // parameters
190 'params' :
191 {
192 'u_tex0': { 'type' : 'tex2d' },
193 'u_time' : { 'type' : 'float' },
194 'u_speed' : { 'type' : 'float' },
195 'u_waveWidth' : { 'type' : 'float' },
196 'u_waveHeight' : { 'type' : 'float' }
197 },
198
199 // render states
200 'states' :
201 {
202 'depthEnable' : true,
203 'offset':[1.0, 0.1]
204 }
205 }
206 ]
207 }
208};
209
210FlagMaterial.prototype = new PulseMaterial();
211
212if (typeof exports === "object") {
213 exports.FlagMaterial = FlagMaterial;
214}
215
216
217
218