diff options
Diffstat (limited to 'js/lib/rdge/runtime/RuntimeMaterial.js')
-rw-r--r-- | js/lib/rdge/runtime/RuntimeMaterial.js | 317 |
1 files changed, 317 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..294c4787 --- /dev/null +++ b/js/lib/rdge/runtime/RuntimeMaterial.js | |||
@@ -0,0 +1,317 @@ | |||
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.getPropertyFromString = function( prop, str ) | ||
58 | { | ||
59 | var index = str.indexOf( prop ); | ||
60 | if (index < 0) throw new Error( "property " + prop + " not found in string: " + str); | ||
61 | |||
62 | var rtnStr = str.substr( index+prop.length ); | ||
63 | index = rtnStr.indexOf( "\n" ); | ||
64 | if (index >= 0) | ||
65 | rtnStr = rtnStr.substr(0, index); | ||
66 | |||
67 | return rtnStr; | ||
68 | } | ||
69 | |||
70 | } | ||
71 | |||
72 | function RuntimeFlatMaterial() | ||
73 | { | ||
74 | // inherit the members of RuntimeMaterial | ||
75 | this.inheritedFrom = RuntimeMaterial; | ||
76 | this.inheritedFrom(); | ||
77 | |||
78 | this._name = "FlatMaterial"; | ||
79 | this._shaderName = "flat"; | ||
80 | |||
81 | // assign a default color | ||
82 | this._color = [1,0,0,1]; | ||
83 | |||
84 | this.import = function( importStr ) | ||
85 | { | ||
86 | var colorStr = this.getPropertyFromString( "color: ", importStr ); | ||
87 | if (colorStr) | ||
88 | this._color = eval( "[" + colorStr + "]" ); | ||
89 | }; | ||
90 | |||
91 | |||
92 | this.init = function() | ||
93 | { | ||
94 | if (this._shader) | ||
95 | { | ||
96 | this._shader.colorMe["color"].set( this._color ); | ||
97 | } | ||
98 | } | ||
99 | } | ||
100 | |||
101 | function RuntimePulseMaterial() | ||
102 | { | ||
103 | // inherit the members of RuntimeMaterial | ||
104 | this.inheritedFrom = RuntimeMaterial; | ||
105 | this.inheritedFrom(); | ||
106 | |||
107 | this._name = "PulseMaterial"; | ||
108 | this._shaderName = "pulse"; | ||
109 | |||
110 | this._texMap = 'assets/images/cubelight.png'; | ||
111 | |||
112 | this.isAnimated = function() { return true; } | ||
113 | |||
114 | |||
115 | this.import = function( importStr ) | ||
116 | { | ||
117 | this._texMap = this.getPropertyFromString( "texture: ", importStr ); | ||
118 | } | ||
119 | |||
120 | this.init = function() | ||
121 | { | ||
122 | var material = this._materialNode; | ||
123 | if (material) | ||
124 | { | ||
125 | var technique = material.shaderProgram.default; | ||
126 | var renderer = g_Engine.getContext().renderer; | ||
127 | if (renderer && technique) | ||
128 | { | ||
129 | if (this._shader && this._shader.default) | ||
130 | { | ||
131 | var res = [ renderer.vpWidth, renderer.vpHeight ]; | ||
132 | technique.u_resolution.set( res ); | ||
133 | |||
134 | var wrap = 'REPEAT', mips = true; | ||
135 | var tex = renderer.getTextureByName(this._texMap, wrap, mips ); | ||
136 | if (tex) | ||
137 | technique.u_tex0.set( tex ); | ||
138 | |||
139 | this._shader.default.u_time.set( [this._time] ); | ||
140 | } | ||
141 | } | ||
142 | } | ||
143 | } | ||
144 | |||
145 | // several materials inherit from pulse. | ||
146 | // they may share this update method | ||
147 | this.update = function( time ) | ||
148 | { | ||
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 | { | ||
156 | if (this._shader && this._shader.default) | ||
157 | this._shader.default.u_time.set( [this._time] ); | ||
158 | this._time += this._dTime; | ||
159 | if (this._time > 200.0) this._time = 0.0; | ||
160 | } | ||
161 | } | ||
162 | } | ||
163 | } | ||
164 | |||
165 | function RuntimeRadialGradientMaterial() | ||
166 | { | ||
167 | // inherit the members of RuntimeMaterial | ||
168 | this.inheritedFrom = RuntimeMaterial; | ||
169 | this.inheritedFrom(); | ||
170 | |||
171 | this._name = "RadialGradientMaterial"; | ||
172 | this._shaderName = "radialGradient"; | ||
173 | |||
174 | // setup default values | ||
175 | this._color1 = [1,0,0,1]; this._colorStop1 = 0.0; | ||
176 | this._color2 = [0,1,0,1]; this._colorStop2 = 0.3; | ||
177 | this._color3 = [0,1,0,1]; this._colorStop3 = 0.6; | ||
178 | this._color4 = [0,1,0,1]; this._colorStop4 = 1.0; | ||
179 | |||
180 | this.init = function() | ||
181 | { | ||
182 | var material = this._materialNode; | ||
183 | if (material) | ||
184 | { | ||
185 | var technique = material.shaderProgram.default; | ||
186 | var renderer = g_Engine.getContext().renderer; | ||
187 | if (renderer && technique) | ||
188 | { | ||
189 | if (this._shader && this._shader.default) | ||
190 | { | ||
191 | this._shader.default.u_color1.set( this._color1 ); | ||
192 | this._shader.default.u_color2.set( this._color2 ); | ||
193 | this._shader.default.u_color3.set( this._color3 ); | ||
194 | this._shader.default.u_color4.set( this._color4 ); | ||
195 | |||
196 | this._shader.default.u_colorStop1.set( [this._colorStop1] ); | ||
197 | this._shader.default.u_colorStop2.set( [this._colorStop2] ); | ||
198 | this._shader.default.u_colorStop3.set( [this._colorStop3] ); | ||
199 | this._shader.default.u_colorStop4.set( [this._colorStop4] ); | ||
200 | |||
201 | if (this._angle !== undefined) | ||
202 | this._shader.default.u_cos_sin_angle.set([Math.cos(this._angle), Math.sin(this._angle)]); | ||
203 | } | ||
204 | } | ||
205 | } | ||
206 | } | ||
207 | |||
208 | this.import = function( importStr ) | ||
209 | { | ||
210 | var colorStr; | ||
211 | colorStr = this.getPropertyFromString( "color1: ", importStr ); | ||
212 | this._color1 = eval( "[" + colorStr + "]" ); | ||
213 | colorStr = this.getPropertyFromString( "color2: ", importStr ); | ||
214 | this._color2 = eval( "[" + colorStr + "]" ); | ||
215 | colorStr = this.getPropertyFromString( "color3: ", importStr ); | ||
216 | this._color3 = eval( "[" + colorStr + "]" ); | ||
217 | colorStr = this.getPropertyFromString( "color4: ", importStr ); | ||
218 | this._color4 = eval( "[" + colorStr + "]" ); | ||
219 | |||
220 | this._colorStop1 = Number( this.getPropertyFromString( "colorStop1: ", importStr ) ); | ||
221 | this._colorStop2 = Number( this.getPropertyFromString( "colorStop2: ", importStr ) ); | ||
222 | this._colorStop3 = Number( this.getPropertyFromString( "colorStop3: ", importStr ) ); | ||
223 | this._colorStop4 = Number( this.getPropertyFromString( "colorStop4: ", importStr ) ); | ||
224 | |||
225 | if (this._angle !== undefined) | ||
226 | this._angle = this.getPropertyFromString( "angle: ", importStr ); |