diff options
Diffstat (limited to 'js/lib/rdge/materials/radial-blur-material.js')
-rw-r--r-- | js/lib/rdge/materials/radial-blur-material.js | 244 |
1 files changed, 244 insertions, 0 deletions
diff --git a/js/lib/rdge/materials/radial-blur-material.js b/js/lib/rdge/materials/radial-blur-material.js new file mode 100644 index 00000000..41e68182 --- /dev/null +++ b/js/lib/rdge/materials/radial-blur-material.js | |||
@@ -0,0 +1,244 @@ | |||
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 | var RadialBlurMaterial = function RadialBlurMaterial() { | ||
11 | /////////////////////////////////////////////////////////////////////// | ||
12 | // Instance variables | ||
13 | /////////////////////////////////////////////////////////////////////// | ||
14 | this._name = "RadialBlurMaterial"; | ||
15 | this._shaderName = "radialBlur"; | ||
16 | |||
17 | this._texMap = 'assets/images/cubelight.png'; | ||
18 | this._color = [1,0,0,1]; | ||
19 | |||
20 | this._time = 0.0; | ||
21 | this._dTime = 0.01; | ||
22 | |||
23 | /////////////////////////////////////////////////////////////////////// | ||
24 | // Property Accessors | ||
25 | /////////////////////////////////////////////////////////////////////// | ||
26 | this.getName = function() { return this._name; }; | ||
27 | this.getShaderName = function() { return this._shaderName; }; | ||
28 | |||
29 | this.getTextureMap = function() { return this._texMap.slice(0); }; | ||
30 | this.setTextureMap = function(m) { this._propValues[this._propNames[0]] = m.slice(0); this.updateTexture(); }; | ||
31 | |||
32 | this.isAnimated = function() { return true; }; | ||
33 | |||
34 | /////////////////////////////////////////////////////////////////////// | ||
35 | // Material Property Accessors | ||
36 | /////////////////////////////////////////////////////////////////////// | ||
37 | this._propNames = ["texmap", "color"]; | ||
38 | this._propLabels = ["Texture map", "Color"]; | ||
39 | this._propTypes = ["file", "color"]; | ||
40 | this._propValues = []; | ||
41 | |||
42 | this._propValues[ this._propNames[0] ] = this._texMap.slice(0); | ||
43 | this._propValues[ this._propNames[1] ] = this._color.slice(0); | ||
44 | |||
45 | this.setProperty = function( prop, value ) { | ||
46 | // make sure we have legitimate imput | ||
47 | var ok = this.validateProperty( prop, value ); | ||
48 | if (!ok) { | ||
49 | console.log( "invalid property in Radial Gradient Material:" + prop + " : " + value ); | ||
50 | } | ||
51 | |||
52 | switch (prop) | ||
53 | { | ||
54 | case "texmap": | ||
55 | this.setTextureMap(value); | ||
56 | break; | ||
57 | |||
58 | case "color": | ||
59 | this._propValues[prop] = value.slice(0); | ||
60 | if (this._shader && this._shader['default']) { | ||
61 | this._shader['default'][prop].set(value); | ||
62 | } | ||
63 | break; | ||
64 | } | ||
65 | }; | ||
66 | /////////////////////////////////////////////////////////////////////// | ||
67 | |||
68 | |||
69 | /////////////////////////////////////////////////////////////////////// | ||
70 | // Methods | ||
71 | /////////////////////////////////////////////////////////////////////// | ||
72 | // duplcate method requirde | ||
73 | this.dup = function( ) { | ||
74 | // allocate a new uber material | ||
75 | var newMat = new RadialBlurMaterial(); | ||
76 | |||
77 | // copy over the current values; | ||
78 | var propNames = [], propValues = [], propTypes = [], propLabels = []; | ||
79 | this.getAllProperties( propNames, propValues, propTypes, propLabels); | ||
80 | var n = propNames.length; | ||
81 | for (var i=0; i<n; i++) | ||
82 | newMat.setProperty( propNames[i], propValues[i] ); | ||
83 | |||
84 | return newMat; | ||
85 | }; | ||
86 | |||
87 | this.init = function( world ) { | ||
88 | // save the world | ||
89 | if (world) this.setWorld( world ); | ||
90 | |||
91 | // set up the shader | ||
92 | this._shader = new jshader(); | ||
93 | this._shader.def = radialBlurMaterialDef; | ||
94 | this._shader.init(); | ||
95 | |||
96 | // set up the material node | ||
97 | this._materialNode = createMaterialNode("radialBlurMaterial"); | ||
98 | this._materialNode.setShader(this._shader); | ||
99 | |||
100 | this._time = 0; | ||
101 | if (this._shader && this._shader['default']) | ||
102 | this._shader['default'].u_time.set( [this._time] ); | ||
103 | this.setProperty( "color", [this._time, 0, 0, 1] ); | ||
104 | |||
105 | // set the shader values in the shader | ||
106 | this.updateTexture(); | ||
107 | this.setResolution( [world.getViewportWidth(),world.getViewportHeight()] ); | ||
108 | this.update( 0 ); | ||
109 | }; | ||
110 | |||
111 | this.updateTexture = function() { | ||
112 | var material = this._materialNode; | ||
113 | if (material) { | ||
114 | var technique = material.shaderProgram['default']; | ||
115 | var renderer = g_Engine.getContext().renderer; | ||
116 | if (renderer && technique) { | ||
117 | var texMapName = this._propValues[this._propNames[0]]; | ||
118 | var tex = renderer.getTextureByName(texMapName, 'REPEAT'); | ||
119 | // if (tex) | ||
120 | // { | ||
121 | // var res = [tex.image.naturalWidth, tex.image.naturalHeight]; | ||
122 | // this.setResoloution( res ); | ||
123 | // } | ||
124 | technique.u_tex0.set( tex ); | ||
125 | } | ||
126 | } | ||
127 | }; | ||
128 | |||
129 | this.update = function( ) { | ||
130 | var material = this._materialNode; | ||
131 | if (material) { | ||
132 | var technique = material.shaderProgram['default']; | ||
133 | var renderer = g_Engine.getContext().renderer; | ||
134 | if (renderer && technique) { | ||
135 | if (this._shader && this._shader['default']) { | ||
136 | this._shader['default'].u_time.set( [this._time] ); | ||
137 | } | ||
138 | |||
139 | var color = this.getProperty( "color" ); | ||
140 | color[0] = this._time; | ||
141 | this.setProperty( "color", color ); | ||
142 | //console.log( "update color to: " + color ); | ||
143 | this._time += this._dTime; | ||
144 | } | ||
145 | } | ||
146 | }; | ||
147 | |||
148 | this.setResolution = function( res ) { | ||
149 | var material = this._materialNode; | ||
150 | if (material) | ||
151 | { | ||
152 | var technique = material.shaderProgram['default']; | ||
153 | var renderer = g_Engine.getContext().renderer; | ||
154 | if (renderer && technique) { | ||
155 | technique.u_resolution.set( res ); | ||
156 | } | ||
157 | } | ||
158 | }; | ||
159 | |||
160 | this.export = function() { | ||
161 | // every material needs the base type and instance name | ||
162 | var exportStr = "material: " + this.getShaderName() + "\n"; | ||
163 | exportStr += "name: " + this.getName() + "\n"; | ||
164 | |||
165 | // every material needs to terminate like this | ||
166 | exportStr += "endMaterial\n"; | ||
167 | |||
168 | return exportStr; | ||
169 | }; | ||
170 | |||
171 | this.import = function( importStr ) { | ||
172 | var pu = new MaterialParser( importStr ); | ||
173 | var material = pu.nextValue( "material: " ); | ||
174 | if (material != this.getShaderName()) throw new Error( "ill-formed material" ); | ||
175 | this.setName( pu.nextValue( "name: ") ); | ||
176 | |||
177 | var rtnStr; | ||
178 | try | ||
179 | { | ||
180 | var endKey = "endMaterial\n"; | ||
181 | var index = importStr.indexOf( endKey ); | ||
182 | index += endKey.length; | ||
183 | rtnStr = importStr.substr( index ); | ||
184 | } | ||
185 | catch (e) | ||
186 | { | ||
187 | throw new Error( "could not import material: " + importStr ); | ||
188 | } | ||
189 | |||
190 | return rtnStr; | ||
191 | } | ||
192 | }; | ||
193 | |||
194 | /////////////////////////////////////////////////////////////////////////////////////// | ||
195 | // RDGE shader | ||
196 | |||
197 | // shader spec (can also be loaded from a .JSON file, or constructed at runtime) | ||
198 | var radialBlurMaterialDef = | ||
199 | {'shaders': | ||
200 | { | ||
201 | 'defaultVShader':"assets/shaders/Basic.vert.glsl", | ||
202 | 'defaultFShader':"assets/shaders/radialBlur.frag.glsl" | ||
203 | }, | ||
204 | 'techniques': | ||
205 | { | ||
206 | 'default': | ||
207 | [ | ||
208 | { | ||
209 | 'vshader' : 'defaultVShader', | ||
210 | 'fshader' : 'defaultFShader', | ||
211 | // attributes | ||
212 | 'attributes' : | ||
213 | { | ||
214 | 'vert' : { 'type' : 'vec3' }, | ||
215 | 'normal' : { 'type' : 'vec3' }, | ||
216 | 'texcoord' : { 'type' : 'vec2' } | ||
217 | }, | ||
218 | // parameters | ||