aboutsummaryrefslogtreecommitdiff
path: root/js/lib/rdge/materials/cloud-material.js
diff options
context:
space:
mode:
Diffstat (limited to 'js/lib/rdge/materials/cloud-material.js')
-rw-r--r--js/lib/rdge/materials/cloud-material.js247
1 files changed, 247 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..bde42ac3
--- /dev/null
+++ b/js/lib/rdge/materials/cloud-material.js
@@ -0,0 +1,247 @@
1/* <copyright>
2This file contains proprietary software owned by Motorola Mobility, Inc.<br/>
3No 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
7var MaterialParser = require("js/lib/rdge/materials/material-parser").MaterialParser;
8var Material = require("js/lib/rdge/materials/material").Material;
9///////////////////////////////////////////////////////////////////////
10// Class GLMaterial
11// RDGE representation of a material.
12///////////////////////////////////////////////////////////////////////
13var 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 if (prop === 'color') prop = 'diffusecolor';
53
54 // make sure we have legitimate imput
55 var ok = this.validateProperty(prop, value);
56 if (!ok) {
57 console.log("invalid property in Radial Gradient Material:" + prop + " : " + value);
58 }
59
60 switch (prop) {
61 case "texmap":
62 this.setTextureMap(value);
63 break;
64
65 case "diffusecolor":
66 this.setDiffuseColor(value);
67 break;
68
69 case "color":
70 break;
71 }
72 };
73 ///////////////////////////////////////////////////////////////////////
74
75
76 ///////////////////////////////////////////////////////////////////////
77 // Methods
78 ///////////////////////////////////////////////////////////////////////
79 // duplicate method required
80 this.dup = function (world) {
81 // save the world
82 if (world) this.setWorld(world);
83
84 // allocate a new uber material
85 var newMat = new CloudMaterial();
86
87 // copy over the current values;
88 var propNames = [], propValues = [], propTypes = [], propLabels = [];
89 this.getAllProperties(propNames, propValues, propTypes, propLabels);
90 var n = propNames.length;
91 for (var i = 0; i < n; i++) {
92 newMat.setProperty(propNames[i], propValues[i]);
93 }
94
95 return newMat;
96 };
97
98 this.init = function (world) {
99 // save the world
100 if (world) this.setWorld(world);
101
102 // this variable declared above is inherited set to a smaller delta.
103 // the cloud material runs a little faster
104 this._dTime = 0.01;
105
106 // set up the shader
107 this._shader = new RDGE.jshader();
108 this._shader.def = cloudMaterialDef;
109 this._shader.init();
110
111 // set up the material node
112 this._materialNode = RDGE.createMaterialNode("cloudMaterial" + "_" + world.generateUniqueNodeID());
113 this._materialNode.setShader(this._shader);
114
115 this._time = 0;
116 if (this._shader && this._shader['default']) {
117 this._shader['default'].u_time.set([this._time]);
118 this._shader['default'].u_DiffuseColor.set(this._diffuseColor);
119 }
120
121 // set the shader values in the shader
122 this.updateTexture();
123 this.update(0);
124 };
125
126 this.updateColor = function () {
127 var material = this._materialNode;
128 if (material) {
129 var technique = material.shaderProgram['default'];
130 var renderer = RDGE.globals.engine.getContext().renderer;
131 if (renderer && technique) {
132 var color = this._propValues[this._propNames[1]];
133 technique.u_DiffuseColor.set(this._diffuseColor);
134 }
135 }
136 };
137
138 this.updateTexture = function () {
139 var material = this._materialNode;
140 if (material) {
141 var technique = material.shaderProgram['default'];
142 var renderer = RDGE.globals.engine.getContext().renderer;
143 if (renderer && technique) {
144 var texMapName = this._propValues[this._propNames[0]];
145 var wrap = 'REPEAT', mips = true;
146 var tex = this.loadTexture(texMapName, wrap, mips);
147
148 if (tex) {
149 technique.u_tex0.set(tex);
150 }
151 }
152 }
153 };
154
155 this.update = function (time) {
156 var material = this._materialNode;
157 if (material) {
158 var technique = material.shaderProgram['default'];
159 var renderer = RDGE.globals.engine.getContext().renderer;
160 if (renderer && technique) {
161 if (this._shader && this._shader['default']) {
162 this._shader['default'].u_time.set([this._time]);
163 }
164 this._time += this._dTime;
165
166 if (this._time > 200.0) this._time = 0.0;
167 }
168 }
169 };
170
171 // JSON export
172 this.exportJSON = function () {
173 var jObj =
174 {
175 'material': this.getShaderName(),
176 'name': this.getName(),
177 'texture': this._propValues[this._propNames[0]]
178 };
179
180 return jObj;
181 };
182
183 this.importJSON = function (jObj) {
184 if (this.getShaderName() != jObj.material) throw new Error("ill-formed material");
185 this.setName(jObj.name);
186
187 try {
188 this._propValues[this._propNames[0]] = jObj.texture;
189 }
190 catch (e) {
191 throw new Error("could not import material: " + jObj);
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 cloudMaterialDef =
201{ 'shaders':
202 {
203 'defaultVShader': "assets/shaders/Cloud.vert.glsl",
204 'defaultFShader': "assets/shaders/Cloud.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' },
224 'u_time': { 'type': 'float' },
225 'u_DiffuseColor': { 'type': 'vec4' }
226 },
227
228 // render states
229 'states'