diff options
Diffstat (limited to 'js/lib/rdge/materials/flag-material.js')
-rw-r--r-- | js/lib/rdge/materials/flag-material.js | 151 |
1 files changed, 151 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..312ca1c1 --- /dev/null +++ b/js/lib/rdge/materials/flag-material.js | |||
@@ -0,0 +1,151 @@ | |||
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 | var Material = require("js/lib/rdge/materials/material").Material; | ||
9 | var PulseMaterial = require("js/lib/rdge/materials/pulse-material").PulseMaterial; | ||
10 | var Texture = require("js/lib/rdge/texture").Texture; | ||
11 | |||
12 | var 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._defaultWaveWidth = 1.0; | ||
25 | this._defaultWaveHeight = 1.0; | ||
26 | |||
27 | this._hasVertexDeformation = true; | ||
28 | |||
29 | /////////////////////////////////////////////////////////////////////// | ||
30 | // Properties | ||
31 | /////////////////////////////////////////////////////////////////////// | ||
32 | // all defined in parent PulseMaterial.js | ||
33 | // load the local default value | ||
34 | this._propNames = ["texmap", "wavewidth", "waveheight" ]; | ||
35 | this._propLabels = ["Texture map", "Wave Width", "Wave Height" ]; | ||
36 | this._propTypes = ["file", "float", "float" ]; | ||
37 | this._propValues = []; | ||
38 | |||
39 | this._propValues[ this._propNames[0] ] = this._texMap.slice(0); | ||
40 | this._propValues[ this._propNames[1] ] = this._defaultWaveWidth; | ||
41 | this._propValues[ this._propNames[2] ] = this._defaultWaveHeight; | ||
42 | |||
43 | // a material can be animated or not. default is not. | ||
44 | // Any material needing continuous rendering should override this method | ||
45 | this.isAnimated = function() { return true; }; | ||
46 | |||
47 | /////////////////////////////////////////////////////////////////////// | ||
48 | // Methods | ||
49 | /////////////////////////////////////////////////////////////////////// | ||
50 | // duplcate method requirde | ||
51 | this.dup = function( world ) | ||
52 | { | ||
53 | // get the current values; | ||
54 | var propNames = [], propValues = [], propTypes = [], propLabels = []; | ||
55 | this.getAllProperties(propNames, propValues, propTypes, propLabels); | ||
56 | |||
57 | // allocate a new uber material | ||
58 | var newMat = new FlagMaterial(); | ||
59 | |||
60 | // copy over the current values; | ||
61 | var n = propNames.length; | ||
62 | for (var i = 0; i < n; i++) | ||
63 | newMat.setProperty(propNames[i], propValues[i]); | ||
64 | |||
65 | return newMat; | ||
66 | }; | ||
67 | |||
68 | this.init = function( world ) | ||
69 | { | ||
70 | // save the world | ||
71 | if (world) this.setWorld( world ); | ||
72 | |||
73 | // set up the shader | ||
74 | this._shader = new RDGE.jshader(); | ||
75 | this._shader.def = flagMaterialDef; | ||
76 | this._shader.init(); | ||
77 | |||
78 | // set up the material node | ||
79 | this._materialNode = RDGE.createMaterialNode("flagMaterial" + "_" + world.generateUniqueNodeID()); | ||
80 | this._materialNode.setShader(this._shader); | ||
81 | |||
82 | this._time = 0; | ||
83 | if (this._shader && this._shader['default']) { | ||
84 | this._shader['default'].u_time.set( [this._time] ); | ||
85 | this._shader['default'].u_waveWidth.set( [this._propValues[ this._propNames[1] ]] ); | ||
86 | this._shader['default'].u_waveHeight.set( [this._propValues[ this._propNames[2] ]] ); | ||
87 | } | ||
88 | |||
89 | // set up the texture | ||
90 | var texMapName = this._propValues[this._propNames[0]]; | ||
91 | this._glTex = new Texture( world, texMapName ); | ||
92 | |||
93 | // set the shader values in the shader | ||
94 | this.updateTexture(); | ||
95 | this.update( 0 ); | ||
96 | } | ||
97 | }; | ||
98 | |||
99 | /////////////////////////////////////////////////////////////////////////////////////// | ||
100 | // RDGE shader | ||
101 | |||
102 | // shader spec (can also be loaded from a .JSON file, or constructed at runtime) | ||
103 | var flagMaterialDef = | ||
104 | {'shaders': | ||
105 | { | ||
106 | 'defaultVShader':"assets/shaders/Flag.vert.glsl", | ||
107 | 'defaultFShader':"assets/shaders/Flag.frag.glsl" | ||
108 | }, | ||
109 | 'techniques': | ||
110 | { | ||
111 | 'default': | ||
112 | [ | ||
113 | { | ||
114 | 'vshader' : 'defaultVShader', | ||
115 | 'fshader' : 'defaultFShader', | ||
116 | // attributes | ||
117 | 'attributes' : | ||
118 | { | ||
119 | 'vert' : { 'type' : 'vec3' }, | ||
120 | 'normal' : { 'type' : 'vec3' }, | ||
121 | 'texcoord' : { 'type' : 'vec2' } | ||
122 | }, | ||
123 | // parameters | ||
124 | 'params' : | ||
125 | { | ||
126 | 'u_tex0': { 'type' : 'tex2d' }, | ||
127 | 'u_time' : { 'type' : 'float' }, | ||
128 | 'u_waveWidth' : { 'type' : 'float' }, | ||
129 | 'u_waveHeight' : { 'type' : 'float' } | ||
130 | }, | ||
131 | |||
132 | // render states | ||
133 | 'states' : | ||
134 | { | ||
135 | 'depthEnable' : true, | ||
136 | 'offset':[1.0, 0.1] | ||
137 | } | ||
138 | } | ||
139 | ] | ||
140 | } | ||
141 | }; | ||
142 | |||
143 | FlagMaterial.prototype = new PulseMaterial(); | ||
144 | |||
145 | if (typeof exports === "object") { | ||
146 | exports.FlagMaterial = FlagMaterial; | ||
147 | } | ||
148 | |||
149 | |||
150 | |||
151 | |||