diff options
Diffstat (limited to 'js/lib/rdge/materials/cloud-material.js')
-rw-r--r-- | js/lib/rdge/materials/cloud-material.js | 427 |
1 files changed, 363 insertions, 64 deletions
diff --git a/js/lib/rdge/materials/cloud-material.js b/js/lib/rdge/materials/cloud-material.js index bde42ac3..0cf428b0 100644 --- a/js/lib/rdge/materials/cloud-material.js +++ b/js/lib/rdge/materials/cloud-material.js | |||
@@ -4,25 +4,55 @@ No rights, expressed or implied, whatsoever to this software are provided by Mot | |||
4 | (c) Copyright 2011 Motorola Mobility, Inc. All Rights Reserved. | 4 | (c) Copyright 2011 Motorola Mobility, Inc. All Rights Reserved. |
5 | </copyright> */ | 5 | </copyright> */ |
6 | 6 | ||
7 | var MaterialParser = require("js/lib/rdge/materials/material-parser").MaterialParser; | 7 | var MaterialParser = require("js/lib/rdge/materials/material-parser").MaterialParser; |
8 | var Material = require("js/lib/rdge/materials/material").Material; | 8 | var Material = require("js/lib/rdge/materials/material").Material; |
9 | var GLWorld = require("js/lib/drawing/world").World; | ||
10 | var Texture = require("js/lib/rdge/texture").Texture; | ||
11 | var ElementMediator = require("js/mediators/element-mediator").ElementMediator; | ||
12 | var TagTool = require("js/tools/TagTool").TagTool; | ||
13 | |||
9 | /////////////////////////////////////////////////////////////////////// | 14 | /////////////////////////////////////////////////////////////////////// |
10 | // Class GLMaterial | 15 | // Class GLMaterial |
11 | // RDGE representation of a material. | 16 | // RDGE representation of a material. |
12 | /////////////////////////////////////////////////////////////////////// | 17 | /////////////////////////////////////////////////////////////////////// |
13 | var CloudMaterial = function CloudMaterial() { | 18 | var CloudMaterial = function CloudMaterial() |
19 | { | ||
14 | /////////////////////////////////////////////////////////////////////// | 20 | /////////////////////////////////////////////////////////////////////// |
15 | // Instance variables | 21 | // Instance variables |
16 | /////////////////////////////////////////////////////////////////////// | 22 | /////////////////////////////////////////////////////////////////////// |
17 | this._name = "CloudMaterial"; | 23 | this._name = "CloudMaterial"; |
18 | this._shaderName = "cloud"; | 24 | this._shaderName = "cloud"; |
19 | 25 | ||
26 | this._texMap = 'assets/images/cloud10.png'; | ||
20 | this._texMap = 'assets/images/cloud2.jpg'; | 27 | this._texMap = 'assets/images/cloud2.jpg'; |
28 | //this._texMap = 'assets/images/CL13.png'; | ||
29 | //this._texMap = 'assets/images/material_paint.png'; | ||
30 | //this._texMap = 'assets/images/us_flag.png'; | ||
31 | //this._texMap = 'assets/images/cubelight.png'; | ||
21 | this._diffuseColor = [0.5, 0.5, 0.5, 0.5]; | 32 | this._diffuseColor = [0.5, 0.5, 0.5, 0.5]; |
22 | 33 | ||
34 | // base size of cloud polygons. Random adjustments made to each quad | ||
35 | this._cloudSize = 40; | ||
36 | |||
23 | this._time = 0.0; | 37 | this._time = 0.0; |
24 | this._dTime = 0.01; | 38 | this._dTime = 0.01; |
25 | 39 | ||
40 | // parameter initial values | ||
41 | this._time = 0.0; | ||
42 | this._surfaceAlpha = 0.5; | ||
43 | // this._zmin = 2.0; | ||
44 | // this._zmax = 5.0; | ||
45 | this._zmin = 5.0; | ||
46 | this._zmax = 10.0; | ||
47 | |||
48 | // the adjusted zMin and zMax values are | ||
49 | // what get sent to the shader. They are initialized | ||
50 | // in buildGeometry | ||
51 | this._adjustedZMin = this._zmin; | ||
52 | this._adjustedZMax = this._zmax; | ||
53 | |||
54 | |||
55 | |||
26 | /////////////////////////////////////////////////////////////////////// | 56 | /////////////////////////////////////////////////////////////////////// |
27 | // Property Accessors | 57 | // Property Accessors |
28 | /////////////////////////////////////////////////////////////////////// | 58 | /////////////////////////////////////////////////////////////////////// |
@@ -34,7 +64,6 @@ var CloudMaterial = function CloudMaterial() { | |||
34 | 64 | ||
35 | this.setDiffuseColor = function (c) { this._propValues[this._propNames[1]] = c.slice(0); this.updateColor(); }; | 65 | 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; }; | 66 | this.getDiffuseColor = function () { return this._propValues[this._propNames[1]] ? this._propValues[this._propNames[1]].slice() : null; }; |
37 | |||
38 | this.isAnimated = function () { return true; }; | 67 | this.isAnimated = function () { return true; }; |
39 | 68 | ||
40 | /////////////////////////////////////////////////////////////////////// | 69 | /////////////////////////////////////////////////////////////////////// |
@@ -65,7 +94,6 @@ var CloudMaterial = function CloudMaterial() { | |||
65 | case "diffusecolor": | 94 | case "diffusecolor": |
66 | this.setDiffuseColor(value); | 95 | this.setDiffuseColor(value); |
67 | break; | 96 | break; |
68 | |||
69 | case "color": | 97 | case "color": |
70 | break; | 98 | break; |
71 | } | 99 | } |
@@ -77,7 +105,8 @@ var CloudMaterial = function CloudMaterial() { | |||
77 | // Methods | 105 | // Methods |
78 | /////////////////////////////////////////////////////////////////////// | 106 | /////////////////////////////////////////////////////////////////////// |
79 | // duplicate method required | 107 | // duplicate method required |
80 | this.dup = function (world) { | 108 | /**************************************************************/ |
109 | this.dup = function (world) { | ||
81 | // save the world | 110 | // save the world |
82 | if (world) this.setWorld(world); | 111 | if (world) this.setWorld(world); |
83 | 112 | ||
@@ -96,78 +125,304 @@ var CloudMaterial = function CloudMaterial() { | |||
96 | }; | 125 | }; |
97 | 126 | ||
98 | this.init = function (world) { | 127 | this.init = function (world) { |
99 | // save the world | 128 | var GLWorld = require("js/lib/drawing/world").World, |
100 | if (world) this.setWorld(world); | 129 | NJUtils = require("js/lib/NJUtils").NJUtils; |
101 | 130 | ||
102 | // this variable declared above is inherited set to a smaller delta. | 131 | // save the world |
103 | // the cloud material runs a little faster | 132 | if (world) this.setWorld( world ); |
104 | this._dTime = 0.01; | 133 | var dstWorld = world; |
105 | 134 | ||
106 | // set up the shader | 135 | // create a canvas to render into |
107 | this._shader = new RDGE.jshader(); | 136 | var dstCanvas = this.getWorld().getCanvas(); |
108 | this._shader.def = cloudMaterialDef; | 137 | var doc = this.getWorld().getCanvas().ownerDocument; |
109 | this._shader.init(); | 138 | var canvasID = "__canvas__"; |
110 | 139 | //this._srcCanvas = doc.createElement(canvasID); | |
111 | // set up the material node | 140 | this._srcCanvas = NJUtils.makeNJElement("canvas", canvasID, "shape", {"data-RDGE-id": NJUtils.generateRandom()}, true); |
112 | this._materialNode = RDGE.createMaterialNode("cloudMaterial" + "_" + world.generateUniqueNodeID()); | 141 | srcCanvas = this._srcCanvas; |
113 | this._materialNode.setShader(this._shader); | 142 | srcCanvas.width = dstCanvas.width; |
114 | 143 | srcCanvas.height = dstCanvas.height; | |
115 | this._time = 0; | 144 | |
116 | if (this._shader && this._shader['default']) { | 145 | ////////////////////////////////////////////////////////////////////////////////// |
117 | this._shader['default'].u_time.set([this._time]); | 146 | // IS THIS NECESSARY?? |
118 | this._shader['default'].u_DiffuseColor.set(this._diffuseColor); | 147 | // var elementModel = TagTool.makeElement(~~srcCanvas.width, ~~srcCanvas.height, |
119 | } | 148 | // Matrix.I(4), [0,0,0], srcCanvas); |
120 | 149 | // ElementMediator.addElement(srcCanvas, elementModel.data, true); | |
121 | // set the shader values in the shader | 150 | ////////////////////////////////////////////////////////////////////////////////// |
122 | this.updateTexture(); | 151 | |
123 | this.update(0); | 152 | // build the source. |
153 | // the source being the world/canvas/geometry of the clouds. | ||
154 | // the source is used to create a texture map that is then used by | ||
155 | // the destimation. | ||
156 | this.buildSource(); | ||
157 | |||
158 | // set up the shader | ||
159 | this._shader = new RDGE.jshader(); | ||
160 | this._shader.def = cloudMapMaterialDef; | ||
161 | this._shader.init(); | ||
162 | |||
163 | // set up the material node | ||
164 | this._materialNode = RDGE.createMaterialNode("cloudMapMaterial" + "_" + world.generateUniqueNodeID()); | ||
165 | this._materialNode.setShader(this._shader); | ||
166 | |||
167 | // initialize the time | ||
168 | this._time = 0; | ||
169 | |||
170 | // create the texture to map the source cloud generation world/canvas to the destination | ||
171 | var wrap = 'REPEAT', mips = true; | ||
172 | this._glTex = new Texture( world, this._srcCanvas, wrap, mips ); | ||
173 | |||
174 | // set the shader values in the shader | ||
175 | this.updateTexture(); | ||
176 | this.update( 0 ); | ||
124 | }; | 177 | }; |
178 | /**************************************************************/ | ||
125 | 179 | ||
126 | this.updateColor = function () { | 180 | this.updateTexture = function () |
127 | var material = this._materialNode; | 181 | { |
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; | 182 | var material = this._materialNode; |
140 | if (material) { | 183 | if (material) |
184 | { | ||
141 | var technique = material.shaderProgram['default']; | 185 | var technique = material.shaderProgram['default']; |
142 | var renderer = RDGE.globals.engine.getContext().renderer; | 186 | var renderer = RDGE.globals.engine.getContext().renderer; |
143 | if (renderer && technique) { | 187 | if (renderer && technique) |
188 | { | ||
144 | var texMapName = this._propValues[this._propNames[0]]; | 189 | var texMapName = this._propValues[this._propNames[0]]; |
145 | var wrap = 'REPEAT', mips = true; | 190 | var wrap = 'REPEAT', mips = true; |
146 | var tex = this.loadTexture(texMapName, wrap, mips); | 191 | if (this._glTex) |
192 | { | ||
193 | this._glTex.render(); | ||
147 | 194 | ||
148 | if (tex) { | 195 | var tex = this._glTex.getTexture(); |
149 | technique.u_tex0.set(tex); | 196 | if (tex) |
197 | technique.u_tex0.set( tex ); | ||
150 | } | 198 | } |
151 | } | 199 | } |
200 | |||
201 | // restore the context | ||
202 | RDGE.globals.engine.setContext( saveContext.id ); | ||
152 | } |