diff options
Diffstat (limited to 'js/lib/rdge/runtime/RuntimeMaterial.js')
-rw-r--r-- | js/lib/rdge/runtime/RuntimeMaterial.js | 351 |
1 files changed, 351 insertions, 0 deletions
diff --git a/js/lib/rdge/runtime/RuntimeMaterial.js b/js/lib/rdge/runtime/RuntimeMaterial.js new file mode 100644 index 00000000..1bbbc3b1 --- /dev/null +++ b/js/lib/rdge/runtime/RuntimeMaterial.js | |||
@@ -0,0 +1,351 @@ | |||
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 RuntimeGeomObjDict = require("js/lib/rdge/runtime/RuntimeGeomObj"); | ||
8 | //var getPropertyFromString = RuntimeGeomObjDict.getPropertyFromString; | ||
9 | |||
10 | //var GeomObj = require("js/lib/geom/geom-obj").GeomObj, | ||
11 | //MaterialsModel = require("js/models/materials-model").MaterialsModel, | ||
12 | //CanvasDataManager = require("js/lib/rdge/runtime/CanvasDataManager"), | ||
13 | //RuntimeGeomObj = require("js/lib/rdge/runtime/RuntimeGeomObj"), | ||
14 | //RuntimeRectangle = RuntimeGeomObj.RuntimeRectangle, | ||
15 | //RuntimeOval = RuntimeGeomObj.RuntimeOval, | ||
16 | //getPropertyFromString = require("js/lib/rdge/runtime/RuntimeGeomObj").getPropertyFromString; | ||
17 | |||
18 | /////////////////////////////////////////////////////////////////////// | ||
19 | // Class RuntimeMaterial | ||
20 | // Runtime representation of a material. | ||
21 | /////////////////////////////////////////////////////////////////////// | ||
22 | var RuntimeMaterial = function RuntimeMaterial( world ) | ||
23 | { | ||
24 | /////////////////////////////////////////////////////////////////////// | ||
25 | // Instance variables | ||
26 | /////////////////////////////////////////////////////////////////////// | ||
27 | this._name = "GLMaterial"; | ||
28 | this._shaderName = "undefined"; | ||
29 | |||
30 | // variables for animation speed | ||
31 | this._time = 0.0; | ||
32 | this._dTime = 0.01; | ||
33 | |||
34 | // RDGE variables | ||
35 | this._shader; | ||
36 | this._materialNode; | ||
37 | |||
38 | /////////////////////////////////////////////////////////////////////// | ||
39 | // Property Accessors | ||
40 | /////////////////////////////////////////////////////////////////////// | ||
41 | |||
42 | // a material can be animated or not. default is not. | ||
43 | // Any material needing continuous rendering should override this method | ||
44 | this.isAnimated = function() { return false; } | ||
45 | |||
46 | /////////////////////////////////////////////////////////////////////// | ||
47 | // Methods | ||
48 | /////////////////////////////////////////////////////////////////////// | ||
49 | this.init = function() | ||
50 | { | ||
51 | } | ||
52 | |||
53 | this.update = function( time ) | ||
54 | { | ||
55 | } | ||
56 | |||
57 | this.import = function( importStr ) | ||
58 | { | ||
59 | } | ||
60 | |||
61 | this.getPropertyFromString = function( prop, str ) | ||
62 | { | ||
63 | var index = str.indexOf( prop ); | ||
64 | if (index < 0) throw new Error( "property " + prop + " not found in string: " + str); | ||
65 | |||
66 | var rtnStr = str.substr( index+prop.length ); | ||
67 | index = rtnStr.indexOf( "\n" ); | ||
68 | if (index >= 0) | ||
69 | rtnStr = rtnStr.substr(0, index); | ||
70 | |||
71 | return rtnStr; | ||
72 | } | ||
73 | |||
74 | } | ||
75 | |||
76 | function RuntimeFlatMaterial() | ||
77 | { | ||
78 | // inherit the members of RuntimeMaterial | ||
79 | this.inheritedFrom = RuntimeMaterial; | ||
80 | this.inheritedFrom(); | ||
81 | |||
82 | this._name = "FlatMaterial"; | ||
83 | this._shaderName = "flat"; | ||
84 | |||
85 | // assign a default color | ||
86 | this._color = [1,0,0,1]; | ||
87 | |||
88 | this.import = function( importStr ) | ||
89 | { | ||
90 | var colorStr = this.getPropertyFromString( "color: ", importStr ); | ||
91 | if (colorStr) | ||
92 | this._color = eval( "[" + colorStr + "]" ); | ||
93 | }; | ||
94 | |||
95 | |||
96 | this.init = function() | ||
97 | { | ||
98 | if (this._shader) | ||
99 | { | ||
100 | this._shader.colorMe["color"].set( this._color ); | ||
101 | } | ||
102 | } | ||
103 | } | ||
104 | |||
105 | function RuntimePulseMaterial() | ||
106 | { | ||
107 | // inherit the members of RuntimeMaterial | ||
108 | this.inheritedFrom = RuntimeMaterial; | ||
109 | this.inheritedFrom(); | ||
110 | |||
111 | this._name = "PulseMaterial"; | ||
112 | this._shaderName = "pulse"; | ||
113 | |||
114 | this._texMap = 'assets/images/cubelight.png'; | ||
115 | |||
116 | this.isAnimated = function() { return true; } | ||
117 | |||
118 | |||
119 | this.import = function( importStr ) | ||
120 | { | ||
121 | this._texMap = this.getPropertyFromString( "texture: ", importStr ); | ||
122 | } | ||
123 | |||
124 | this.init = function() | ||
125 | { | ||
126 | var material = this._materialNode; | ||
127 | if (material) | ||
128 | { | ||
129 | var technique = material.shaderProgram.default; | ||
130 | var renderer = g_Engine.getContext().renderer; | ||
131 | if (renderer && technique) | ||
132 | { | ||
133 | if (this._shader && this._shader.default) | ||
134 | { | ||
135 | var res = [ renderer.vpWidth, renderer.vpHeight ]; | ||
136 | technique.u_resolution.set( res ); | ||
137 | |||
138 | var wrap = 'REPEAT', mips = true; | ||
139 | var tex = renderer.getTextureByName(this._texMap, wrap, mips ); | ||
140 | if (tex) | ||
141 | technique.u_tex0.set( tex ); | ||
142 | |||
143 | this._shader.default.u_time.set( [this._time] ); | ||
144 | } | ||
145 | } | ||
146 | } | ||
147 | } | ||
148 | |||
149 | // several materials inherit from pulse. | ||
150 | // they may share this update method | ||
151 | this.update = function( time ) | ||
152 | { | ||
153 | var material = this._materialNode; | ||
154 | if (material) | ||
155 | { | ||
156 | var technique = material.shaderProgram.default; | ||
157 | var renderer = g_Engine.getContext().renderer; | ||
158 | if (renderer && technique) | ||
159 | { | ||
160 | if (this._shader && this._shader.default) | ||
161 | this._shader.default.u_time.set( [this._time] ); | ||
162 | this._time += this._dTime; | ||
163 | if (this._time > 200.0) this._time = 0.0; | ||
164 | } | ||
165 | } | ||
166 | } | ||
167 | } | ||
168 | |||
169 | function RuntimeRadialGradientMaterial() | ||
170 | { | ||
171 | // inherit the members of RuntimeMaterial | ||
172 | this.inheritedFrom = RuntimeMaterial; | ||
173 | this.inheritedFrom(); | ||
174 | |||
175 | this._name = "RadialGradientMaterial"; | ||
176 | this._shaderName = "radialGradient"; | ||
177 | |||
178 | // setup default values | ||
179 | this._color1 = [1,0,0,1]; this._colorStop1 = 0.0; | ||
180 | this._color2 = [0,1,0,1]; this._colorStop2 = 0.3; | ||
181 | this._color3 = [0,1,0,1]; this._colorStop3 = 0.6; | ||
182 | this._color4 = [0,1,0,1]; this._colorStop4 = 1.0; | ||
183 | |||
184 | this.init = function() | ||
185 | { | ||
186 | var material = this._materialNode; | ||
187 | if (material) | ||
188 | { | ||
189 | var technique = material.shaderProgram.default; | ||
190 | var renderer = g_Engine.getContext().renderer; | ||
191 | if (renderer && technique) | ||
192 | { | ||
193 | if (this._shader && this._shader.default) | ||
194 | { | ||
195 | this._shader.default.u_color1.set( this._color1 ); | ||
196 | this._shader.default.u_color2.set( this._color2 ); | ||
197 | this._shader.default.u_color3.set( this._color3 ); | ||
198 | this._shader.default.u_color4.set( this._color4 ); | ||
199 | |||
200 | this._shader.default.u_colorStop1.set( [this._colorStop1] ); | ||
201 | this._shader.default.u_colorStop2.set( [this._colorStop2] ); | ||
202 | this._shader.default.u_colorStop3.set( [this._colorStop3] ); | ||
203 | this._shader.default.u_colorStop4.set( [this._colorStop4] ); | ||
204 | |||
205 | if (this._angle !== undefined) | ||
206 | this._shader.default.u_cos_sin_angle.set([Math.cos(this._angle), Math.sin(this._angle)]); | ||
207 | } | ||
208 | } | ||
209 | } | ||
210 | } | ||
211 | |||
212 | this.import = function( importStr ) | ||
213 | { | ||
214 | var colorStr; | ||
215 | colorStr = this.getPropertyFromString( "color1: ", importStr ); | ||
216 | this._color1 = eval( "[" + colorStr + "]" ); | ||
217 | colorStr = this.getPropertyFromString( "color2: ", importStr ); | ||
218 | this._color2 = eval( "[" + colorStr + "]" ); | ||
219 | colorStr = this.getPropertyFromString( "color3: ", importStr ); | ||
220 | this._color3 = eval( "[" + colorStr + "]" ); | ||
221 | colorStr = this.getPropertyFromString( "color4: ", importStr ); | ||
222 | this._color4 = eval( "[" + colorStr + "]" ); | ||
223 | |||
224 | this._colorStop1 = Number( this.getPropertyFromString( "colorStop1: ", importStr ) ); | ||
225 | this._colorStop2 = Number( this.getPropertyFromString( "colorStop2: ", importStr ) ); | ||