diff options
Diffstat (limited to 'js/lib/rdge/materials/cloud-material.js')
-rw-r--r-- | js/lib/rdge/materials/cloud-material.js | 300 |
1 files changed, 300 insertions, 0 deletions
diff --git a/js/lib/rdge/materials/cloud-material.js b/js/lib/rdge/materials/cloud-material.js new file mode 100644 index 00000000..85088f91 --- /dev/null +++ b/js/lib/rdge/materials/cloud-material.js | |||
@@ -0,0 +1,300 @@ | |||
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 | var MaterialParser = require("js/lib/rdge/materials/material-parser").MaterialParser; | ||
8 | var Material = require("js/lib/rdge/materials/material").Material; | ||
9 | /////////////////////////////////////////////////////////////////////// | ||
10 | // Class GLMaterial | ||
11 | // RDGE representation of a material. | ||
12 | /////////////////////////////////////////////////////////////////////// | ||
13 | var CloudMaterial = function CloudMaterial() { | ||
14 | /////////////////////////////////////////////////////////////////////// | ||
15 | // Instance variables | ||
16 | /////////////////////////////////////////////////////////////////////// | ||
17 | this._name = "CloudMaterial"; | ||
18 | this._shaderName = "cloud"; | ||
19 | |||
20 | this._texMap = 'assets/images/cloud2.jpg'; | ||
21 | this._diffuseColor = [0.5, 0.5, 0.5, 0.5]; | ||
22 | |||
23 | this._time = 0.0; | ||
24 | this._dTime = 0.01; | ||
25 | |||
26 | /////////////////////////////////////////////////////////////////////// | ||
27 | // Property Accessors | ||
28 | /////////////////////////////////////////////////////////////////////// | ||
29 | this.getName = function() { return this._name; }; | ||
30 | this.getShaderName = function() { return this._shaderName; }; | ||
31 | |||
32 | this.getTextureMap = function() { return this._propValues[this._propNames[0]] ? this._propValues[this._propNames[0]].slice() : null }; | ||
33 | this.setTextureMap = function(m) { this._propValues[this._propNames[0]] = m ? m.slice(0) : null; this.updateTexture(); }; | ||
34 | |||
35 | this.setDiffuseColor = function(c) { this._propValues[this._propNames[1]] = c.slice(0); this.updateColor(); }; | ||
36 | this.getDiffuseColor = function() { return this._propValues[this._propNames[1]] ? this._propValues[this._propNames[1]].slice() : null; }; | ||
37 | |||
38 | this.isAnimated = function() { return true; }; | ||
39 | |||
40 | /////////////////////////////////////////////////////////////////////// | ||
41 | // Material Property Accessors | ||
42 | /////////////////////////////////////////////////////////////////////// | ||
43 | this._propNames = ["texmap", "diffusecolor"]; | ||
44 | this._propLabels = ["Texture map", "Diffuse Color"]; | ||
45 | this._propTypes = ["file", "color"]; | ||
46 | this._propValues = []; | ||
47 | |||
48 | this._propValues[ this._propNames[0] ] = this._texMap.slice(0); | ||
49 | this._propValues[ this._propNames[1] ] = this._diffuseColor.slice(); | ||
50 | |||
51 | this.setProperty = function( prop, value ) | ||
52 | { | ||
53 | if (prop === 'color') prop = 'diffusecolor'; | ||
54 | |||
55 | // make sure we have legitimate imput | ||
56 | var ok = this.validateProperty( prop, value ); | ||
57 | if (!ok) { | ||
58 | console.log( "invalid property in Radial Gradient Material:" + prop + " : " + value ); | ||
59 | } | ||
60 | |||
61 | switch (prop) | ||
62 | { | ||
63 | case "texmap": | ||
64 | this.setTextureMap(value); | ||
65 | break; | ||
66 | |||
67 | case "diffusecolor": | ||
68 | this.setDiffuseColor( value ); | ||
69 | break; | ||
70 | |||
71 | case "color": | ||
72 | break; | ||
73 | } | ||
74 | }; | ||
75 | /////////////////////////////////////////////////////////////////////// | ||
76 | |||
77 | |||
78 | /////////////////////////////////////////////////////////////////////// | ||
79 | // Methods | ||
80 | /////////////////////////////////////////////////////////////////////// | ||
81 | // duplcate method requirde | ||
82 | this.dup = function( world ) | ||
83 | { | ||
84 | // save the world | ||
85 | if (world) this.setWorld( world ); | ||
86 | |||
87 | // allocate a new uber material | ||
88 | var newMat = new CloudMaterial(); | ||
89 | |||
90 | // copy over the current values; | ||
91 | var propNames = [], propValues = [], propTypes = [], propLabels = []; | ||
92 | this.getAllProperties( propNames, propValues, propTypes, propLabels); | ||
93 | var n = propNames.length; | ||
94 | for (var i=0; i<n; i++) { | ||
95 | newMat.setProperty( propNames[i], propValues[i] ); | ||
96 | } | ||
97 | |||
98 | return newMat; | ||
99 | }; | ||
100 | |||
101 | this.init = function( world ) | ||
102 | { | ||
103 | // save the world | ||
104 | if (world) this.setWorld( world ); | ||
105 | |||
106 | // this variable declared above is inherited set to a smaller delta. | ||
107 | // the cloud material runs a little faster | ||
108 | this._dTime = 0.01; | ||
109 | |||
110 | // set up the shader | ||
111 | this._shader = new jshader(); | ||
112 | this._shader.def = cloudMaterialDef; | ||
113 | this._shader.init(); | ||
114 | |||
115 | // set up the material node | ||
116 | this._materialNode = createMaterialNode("cloudMaterial" + "_" + world.generateUniqueNodeID()); | ||
117 | this._materialNode.setShader(this._shader); | ||
118 | |||
119 | this._time = 0; | ||
120 | if (this._shader && this._shader['default']) { | ||
121 | this._shader['default'].u_time.set( [this._time] ); | ||
122 | this._shader['default'].u_DiffuseColor.set( this._diffuseColor ); | ||
123 | } | ||
124 | |||
125 | // set the shader values in the shader | ||
126 | this.updateTexture(); | ||
127 | this.update( 0 ); | ||
128 | }; | ||
129 | |||
130 | this.updateColor = function() | ||
131 | { | ||
132 | var material = this._materialNode; | ||
133 | if (material) | ||
134 | { | ||
135 | var technique = material.shaderProgram['default']; | ||
136 | var renderer = g_Engine.getContext().renderer; | ||
137 | if (renderer && technique) { | ||
138 | var color = this._propValues[this._propNames[1]]; | ||
139 | technique.u_DiffuseColor.set( this._diffuseColor ); | ||
140 | } | ||
141 | } | ||
142 | } | ||
143 | |||
144 | this.updateTexture = function() { | ||
145 | var material = this._materialNode; | ||
146 | if (material) { | ||
147 | var technique = material.shaderProgram['default']; | ||
148 | var renderer = g_Engine.getContext().renderer; | ||
149 | if (renderer && technique) { | ||
150 | var texMapName = this._propValues[this._propNames[0]]; | ||
151 | var wrap = 'REPEAT', mips = true; | ||
152 | var tex = this.loadTexture( texMapName, wrap, mips ); | ||
153 | |||
154 | if (tex) { | ||
155 | technique.u_tex0.set( tex ); | ||
156 | } | ||
157 | } | ||
158 | } | ||
159 | }; | ||
160 | |||
161 | this.update = function( time ) | ||
162 | { | ||
163 | var material = this._materialNode; | ||
164 | if (material) | ||
165 | { | ||
166 | var technique = material.shaderProgram['default']; | ||
167 | var renderer = g_Engine.getContext().renderer; | ||
168 | if (renderer && technique) { | ||
169 | if (this._shader && this._shader['default']) { | ||
170 | this._shader['default'].u_time.set( [this._time] ); | ||
171 | } | ||
172 | this._time += this._dTime; | ||
173 | |||
174 | if (this._time > 200.0) this._time = 0.0; | ||
175 | } | ||
176 | } | ||
177 | }; | ||
178 | |||
179 | // JSON export | ||
180 | this.exportJSON = function() | ||
181 | { | ||
182 | var jObj = | ||
183 | { | ||
184 | 'material' : this.getShaderName(), | ||
185 | 'name' : this.getName(), | ||
186 | 'texture' : this._propValues[this._propNames[0]] | ||
187 | }; | ||
188 | |||
189 | return jObj; | ||
190 | }; | ||
191 | |||
192 | this.importJSON = function( jObj ) | ||
193 | { | ||
194 | if (this.getShaderName() != jObj.material) throw new Error( "ill-formed material" ); | ||
195 | this.setName( jObj.name ); | ||
196 | |||
197 | try { | ||
198 | this._propValues[this._propNames[0]] = jObj.texture; | ||
199 | } | ||
200 | catch (e) | ||
201 | { | ||
202 | throw new Error( "could not import material: " + jObj ); | ||
203 | } | ||
204 | } | ||
205 | |||
206 | |||
207 | this.export = function() { | ||
208 | // every material needs the base type and instance name | ||
209 | var exportStr = "material: " + this.getShaderName() + "\n"; | ||
210 | exportStr += "name: " + this.getName() + "\n"; | ||
211 | |||
212 | var world = this.getWorld(); | ||
213 | if (!world) | ||
214 | throw new Error( "no world in material.export, " + this.getName() ); | ||
215 | |||
216 | var texMapName = this._propValues[this._propNames[0]]; | ||
217 | exportStr += "texture: " +texMapName + "\n"; | ||
218 | |||
219 | // every material needs to terminate like this | ||
220 | exportStr += "endMaterial\n"; | ||
221 | |||
222 | return exportStr; | ||
223 | }; | ||