diff options
author | hwc487 | 2012-04-10 10:12:49 -0700 |
---|---|---|
committer | hwc487 | 2012-04-10 10:12:49 -0700 |
commit | c1a6cacf364d79cbf23b41e7089a1a6d39afea85 (patch) | |
tree | c900a4f6063da1be1c7febb66ce8021def739ea1 /js/lib/rdge | |
parent | 5b878fdfaa696c1a01afa191c1d65c0ccb006b65 (diff) | |
download | ninja-c1a6cacf364d79cbf23b41e7089a1a6d39afea85.tar.gz |
Cloud material
Diffstat (limited to 'js/lib/rdge')
-rw-r--r-- | js/lib/rdge/materials/cloud-material.js | 122 |
1 files changed, 81 insertions, 41 deletions
diff --git a/js/lib/rdge/materials/cloud-material.js b/js/lib/rdge/materials/cloud-material.js index f7f4c6bb..198f7cfa 100644 --- a/js/lib/rdge/materials/cloud-material.js +++ b/js/lib/rdge/materials/cloud-material.js | |||
@@ -4,8 +4,11 @@ | |||
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 | |||
9 | /////////////////////////////////////////////////////////////////////// | 12 | /////////////////////////////////////////////////////////////////////// |
10 | // Class GLMaterial | 13 | // Class GLMaterial |
11 | // RDGE representation of a material. | 14 | // RDGE representation of a material. |
@@ -20,9 +23,19 @@ var CloudMaterial = function CloudMaterial() { | |||
20 | this._texMap = 'assets/images/cloud2.jpg'; | 23 | this._texMap = 'assets/images/cloud2.jpg'; |
21 | this._diffuseColor = [0.5, 0.5, 0.5, 0.5]; | 24 | this._diffuseColor = [0.5, 0.5, 0.5, 0.5]; |
22 | 25 | ||
26 | // base size of cloud polygons. Random adjustments made to each quad | ||
27 | this._cloudSize = 40; | ||
28 | |||
23 | this._time = 0.0; | 29 | this._time = 0.0; |
24 | this._dTime = 0.01; | 30 | this._dTime = 0.01; |
25 | 31 | ||
32 | // parameter initial values | ||
33 | this._time = 0.0; | ||
34 | this._surfaceAlpha = 0.5; | ||
35 | this._zmin = 0.1; | ||
36 | this._zmax = 10.0; | ||
37 | |||
38 | |||
26 | /////////////////////////////////////////////////////////////////////// | 39 | /////////////////////////////////////////////////////////////////////// |
27 | // Property Accessors | 40 | // Property Accessors |
28 | /////////////////////////////////////////////////////////////////////// | 41 | /////////////////////////////////////////////////////////////////////// |
@@ -32,9 +45,6 @@ var CloudMaterial = function CloudMaterial() { | |||
32 | this.getTextureMap = function() { return this._propValues[this._propNames[0]] ? this._propValues[this._propNames[0]].slice() : null }; | 45 | 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(); }; | 46 | this.setTextureMap = function(m) { this._propValues[this._propNames[0]] = m ? m.slice(0) : null; this.updateTexture(); }; |
34 | 47 | ||
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; }; | 48 | this.isAnimated = function() { return true; }; |
39 | 49 | ||
40 | /////////////////////////////////////////////////////////////////////// | 50 | /////////////////////////////////////////////////////////////////////// |
@@ -64,10 +74,6 @@ var CloudMaterial = function CloudMaterial() { | |||
64 | this.setTextureMap(value); | 74 | this.setTextureMap(value); |
65 | break; | 75 | break; |
66 | 76 | ||
67 | case "diffusecolor": | ||
68 | this.setDiffuseColor( value ); | ||
69 | break; | ||
70 | |||
71 | case "color": | 77 | case "color": |
72 | break; | 78 | break; |
73 | } | 79 | } |
@@ -100,6 +106,8 @@ var CloudMaterial = function CloudMaterial() { | |||
100 | 106 | ||
101 | this.init = function( world ) | 107 | this.init = function( world ) |
102 | { | 108 | { |
109 | var GLWorld = require("js/lib/drawing/world").World; | ||
110 | |||
103 | // save the world | 111 | // save the world |
104 | if (world) this.setWorld( world ); | 112 | if (world) this.setWorld( world ); |
105 | 113 | ||
@@ -107,6 +115,18 @@ var CloudMaterial = function CloudMaterial() { | |||
107 | // the cloud material runs a little faster | 115 | // the cloud material runs a little faster |
108 | this._dTime = 0.01; | 116 | this._dTime = 0.01; |
109 | 117 | ||
118 | // create a canvas to render into | ||
119 | var doc = world.getCanvas().ownerDocument; | ||
120 | var canvasID = "__canvas__"; | ||
121 | this._srcCanvas = doc.createElement(canvasID); | ||
122 | |||
123 | // build a world to do the rendering | ||
124 | this._srcWorld = new GLWorld( this._srcCanvas, true ); | ||
125 | var srcWorld = this._srcWorld; | ||
126 | |||
127 | // build the geometry | ||
128 | var prim = this.buildGeometry(); | ||
129 | |||
110 | // set up the shader | 130 | // set up the shader |
111 | this._shader = new jshader(); | 131 | this._shader = new jshader(); |
112 | this._shader.def = cloudMaterialDef; | 132 | this._shader.def = cloudMaterialDef; |
@@ -116,31 +136,34 @@ var CloudMaterial = function CloudMaterial() { | |||
116 | this._materialNode = createMaterialNode("cloudMaterial" + "_" + world.generateUniqueNodeID()); | 136 | this._materialNode = createMaterialNode("cloudMaterial" + "_" + world.generateUniqueNodeID()); |
117 | this._materialNode.setShader(this._shader); | 137 | this._materialNode.setShader(this._shader); |
118 | 138 | ||
139 | // initialize the shader uniforms | ||
119 | this._time = 0; | 140 | this._time = 0; |
120 | if (this._shader && this._shader['default']) { | 141 | if (this._shader && this._shader['default']) { |
121 | this._shader['default'].u_time.set( [this._time] ); | 142 | var t = this._shader['default']; |
122 | this._shader['default'].u_DiffuseColor.set( this._diffuseColor ); | 143 | if (t) |
144 | { | ||
145 | t.u_time.set( [this._time] ); | ||
146 | t.u_surfaceAlpha.set( [this._surfaceAlpha] ); | ||
147 | t.u_zmin.set( [this._zmin] ); | ||
148 | t.u_zmax.set( [this._zmax] ); | ||
149 | } | ||
123 | } | 150 | } |
124 | 151 | ||
152 | // add the nodes to the tree | ||
153 | var trNode = createTransformNode("objRootNode_" + this._srcWorld._nodeCounter++); | ||
154 | srcWorld._rootNode.insertAsChild( trNode ); | ||
155 | trNode.attachMeshNode(srcWorld.renderer.id + "_prim_" + srcWorld._nodeCounter++, prim); | ||
156 | trNode.attachMaterial( this._materialNode ); | ||
157 | |||
158 | // create the texture | ||
159 | var wrap = 'REPEAT', mips = true; | ||
160 | this._glTex = new Texture( dstWorld, canvasID, wrap, mips ); | ||
161 | |||
125 | // set the shader values in the shader | 162 | // set the shader values in the shader |
126 | this.updateTexture(); | 163 | this.updateTexture(); |
127 | this.update( 0 ); | 164 | this.update( 0 ); |
128 | }; | 165 | }; |
129 | 166 | ||
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() { | 167 | this.updateTexture = function() { |
145 | var material = this._materialNode; | 168 | var material = this._materialNode; |
146 | if (material) { | 169 | if (material) { |
@@ -171,30 +194,45 @@ var CloudMaterial = function CloudMaterial() { | |||
171 | } | 194 | } |
172 | this._time += this._dTime; | 195 | this._time += this._dTime; |
173 | 196 | ||
197 | if (this._glTex) | ||
198 | { | ||
199 | this._glTex.render(); | ||
200 | var tex = this._glTex.getTexture(); | ||
201 | technique.u_tex0.set( tex ); | ||
202 | } | ||
203 | |||
174 | if (this._time > 200.0) this._time = 0.0; | 204 | if (this._time > 200.0) this._time = 0.0; |
175 | } | 205 | } |
176 | } | 206 | } |
177 | }; | 207 | }; |
178 | 208 | ||
179 | this.generateQuads = function() | 209 | this.buildGeometry = function() |
180 | { | 210 | { |
181 | var quads = []; | 211 | var RectangleGeometry = require("js/lib/geom/rectangle").RectangleGeometry; |
182 | for ( i = 0; i < 8000; i++ ) | 212 | |
213 | RectangleGeometry.init(); | ||
214 | |||
215 | var verts = [], | ||
216 | norms = [ [0,0,1], [0,0,1], [0,0,1], [0,0,1] ], | ||
217 | uvs = [ [0,0], [1,0], [1,1], [0,1] ]; | ||
218 | |||
219 | for ( i = 0; i < 2; i++ ) | ||
183 | { | 220 | { |
184 | var quad = | 221 | var x = Math.random() * 1000 - 500, |
185 | { | ||
186 | } | ||
187 | x: Math.random() * 1000 - 500, | ||
188 | y = - Math.random() * Math.random() * 200 - 15, | 222 | y = - Math.random() * Math.random() * 200 - 15, |
189 | z = i, | 223 | z = i, |
190 | rotation.z = Math.random() * Math.PI, | 224 | zRot = Math.random() * Math.PI, |
191 | scale = Math.random() * Math.random() * 1.5 + 0.5, | 225 | size = this._cloudSize * Math.random() * Math.random() * 1.5 + 0.5; |
192 | } | 226 | var sz = 0.5*size; |
193 | 227 | ||
194 | quads.push( quad ); | 228 | verts[0] = [x-sz, y-sz, z]; |
229 | verts[1] = [x-sz, y+sz, z]; | ||
230 | verts[2] = [x+sz, y+sz, z]; | ||
231 | verts[3] = [x+sz, y-sz, z]; | ||
232 | RectangleGeometry.addQuad( verts, normals, uvs ) | ||
195 | } | 233 | } |
196 | 234 | ||
197 | this._quads = quads; | 235 | return RectangleGeometry.buildPrimitive(); |