Fork me on GitHub

API Documentation

  1 /*
  2 GLGE WebGL Graphics Engine
  3 Copyright (c) 2010, Paul Brunt
  4 All rights reserved.
  5 
  6 Redistribution and use in source and binary forms, with or without
  7 modification, are permitted provided that the following conditions are met:
  8     * Redistributions of source code must retain the above copyright
  9       notice, this list of conditions and the following disclaimer.
 10     * Redistributions in binary form must reproduce the above copyright
 11       notice, this list of conditions and the following disclaimer in the
 12       documentation and/or other materials provided with the distribution.
 13     * Neither the name of GLGE nor the
 14       names of its contributors may be used to endorse or promote products
 15       derived from this software without specific prior written permission.
 16 
 17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 18 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 19 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 20 DISCLAIMED. IN NO EVENT SHALL PAUL BRUNT BE LIABLE FOR ANY
 21 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 22 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 23 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 24 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 25 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 26 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 27 */
 28 
 29 /**
 30  * @fileOverview
 31  * @name glge_materiallayer.js
 32  * @author me@paulbrunt.co.uk
 33  */
 34 
 35 
 36 (function(GLGE){
 37 
 38 
 39 
 40 
 41 /**
 42 * @class The material layer describes how to apply this layer to the material
 43 * @see GLGE.Material
 44 * @augments GLGE.Animatable
 45 * @augments GLGE.QuickNotation
 46 * @augments GLGE.JSONLoader
 47 * @augments GLGE.Events
 48 */
 49 GLGE.MaterialLayer=function(uid){
 50 	this.blendMode=GLGE.BL_MIX;
 51 	GLGE.Assets.registerAsset(this,uid);
 52 };
 53 GLGE.augment(GLGE.Animatable,GLGE.MaterialLayer);
 54 GLGE.augment(GLGE.QuickNotation,GLGE.MaterialLayer);
 55 GLGE.augment(GLGE.JSONLoader,GLGE.MaterialLayer);
 56 GLGE.augment(GLGE.Events,GLGE.MaterialLayer);
 57 /**
 58  * @name GLGE.MaterialLayer#shaderupdated
 59  * @event Fires when a change will result in a change to the GLSL shader
 60  * @param {object} data
 61  */
 62  
 63 GLGE.MaterialLayer.prototype.className="MaterialLayer";
 64 GLGE.MaterialLayer.prototype.texture=null;
 65 GLGE.MaterialLayer.prototype.blendMode=null;
 66 GLGE.MaterialLayer.prototype.mapto=GLGE.M_COLOR;
 67 GLGE.MaterialLayer.prototype.mapinput=GLGE.UV1;
 68 GLGE.MaterialLayer.prototype.scaleX=1;
 69 GLGE.MaterialLayer.prototype.offsetX=0;
 70 GLGE.MaterialLayer.prototype.rotX=0;
 71 GLGE.MaterialLayer.prototype.scaleY=1;
 72 GLGE.MaterialLayer.prototype.offsetY=0;
 73 GLGE.MaterialLayer.prototype.rotY=0;
 74 GLGE.MaterialLayer.prototype.scaleZ=1;
 75 GLGE.MaterialLayer.prototype.offsetZ=0;
 76 GLGE.MaterialLayer.prototype.rotZ=0;
 77 GLGE.MaterialLayer.prototype.dScaleX=0;
 78 GLGE.MaterialLayer.prototype.dOffsetX=0;
 79 GLGE.MaterialLayer.prototype.dRotX=0;
 80 GLGE.MaterialLayer.prototype.dScaleY=0;
 81 GLGE.MaterialLayer.prototype.dOffsetY=0;
 82 GLGE.MaterialLayer.prototype.dRotY=0;
 83 GLGE.MaterialLayer.prototype.dScaleZ=0;
 84 GLGE.MaterialLayer.prototype.dOffsetZ=0;
 85 GLGE.MaterialLayer.prototype.dRotZ=0;
 86 GLGE.MaterialLayer.prototype.alpha=1;
 87 GLGE.MaterialLayer.prototype.height=0.05;
 88 GLGE.MaterialLayer.prototype.matrix=null;
 89 
 90 /**
 91 * Gets the textures used by the layer
 92 * @return {GLGE.Texture} The current shininess of the material
 93 */
 94 GLGE.MaterialLayer.prototype.getMatrix=function(){
 95 	if(!this.matrix){
 96 		var offset=this.getOffset();
 97 		var scale=this.getScale();
 98 		var rotation=this.getRotation();
 99 		this.matrix=GLGE.mulMat4(GLGE.mulMat4(GLGE.translateMatrix(offset.x,offset.y,offset.z),GLGE.scaleMatrix(scale.x,scale.y,scale.z)),GLGE.rotateMatrix(rotation.x,rotation.y,rotation.z));
100 	}
101 	return this.matrix;
102 };
103 /**
104 * Sets the height for this layer, currently only used for parallax mapping
105 * @param {number} the height of this layer
106 */
107 GLGE.MaterialLayer.prototype.setHeight=function(value){
108 	this.height=value;
109 	return this;
110 };
111 /**
112 * Gets the height for this layer, currently only used for parallax mapping
113 * @return {number} the height of this layer
114 */
115 GLGE.MaterialLayer.prototype.getHeight=function(){
116 	return this.height;
117 };
118 
119 /**
120 * Sets the textures alpha blending value
121 * @param {number} the alpha for this layer
122 */
123 GLGE.MaterialLayer.prototype.setAlpha=function(value){
124 	this.alpha=value;
125 	return this;
126 };
127 /**
128 * Gets the textures alpha blending value
129 * @return {number} the alpha for this layer
130 */
131 GLGE.MaterialLayer.prototype.getAlpha=function(){
132 	return this.alpha;
133 };
134 
135 /**
136 * Sets the textures used by the layer
137 * @param {GLGE.Texture} value the teture to associate with this layer
138 */
139 GLGE.MaterialLayer.prototype.setTexture=function(value){
140 	if(typeof value=="string")  value=GLGE.Assets.get(value);
141 	this.texture=value;
142 	this.fireEvent("shaderupdate",{});
143 	return this;
144 };
145 /**
146 * Gets the textures used by the layer
147 * @return {GLGE.Texture} The current shininess of the material
148 */
149 GLGE.MaterialLayer.prototype.getTexture=function(){
150 	return this.texture;
151 };
152 /**
153 * Sets the flag for how this layer maps to the material
154 * @param {Number} value the flags to set for this layer
155 */
156 GLGE.MaterialLayer.prototype.setMapto=function(value){
157 	this.mapto=value;
158 	this.fireEvent("shaderupdate",{});
159 	return this;
160 };
161 /**
162 * Gets the flag representing the way the layer maps to the material
163 * @return {Number} The flags currently set for this layer
164 */
165 GLGE.MaterialLayer.prototype.getMapto=function(){
166 	return this.mapto;
167 };
168 /**
169 * Sets the texture coordinate system
170 * @param {Number} value the mapping to use
171 */
172 173 GLGE.MaterialLayer.prototype.setMapinput=function(value){
174 	this.mapinput=value;
175 	this.fireEvent("shaderupdate",{});
176 	return this;
177 };
178 /**
179 * Gets the texture coordinate system
180 * @return {Number} The current mapping
181 */
182 GLGE.MaterialLayer.prototype.getMapinput=function(){
183 	return this.mapinput;
184 };
185 
186 /**
187 * Gets the layers texture offset
188 * @return {object} the current offset
189 */
190 GLGE.MaterialLayer.prototype.getOffset=function(){
191 	var offset={};
192 	offset.x=parseFloat(this.getOffsetX())+parseFloat(this.getDOffsetX());
193 	offset.y=parseFloat(this.getOffsetY())+parseFloat(this.getDOffsetY());
194 	offset.z=parseFloat(this.getOffsetZ())+parseFloat(this.getDOffsetZ());
195 	return offset;
196 };
197 
198 /**
199 * Gets the layers texture rotation
200 * @return {object} the current rotation
201 */
202 GLGE.MaterialLayer.prototype.getRotation=function(){
203 	var rotation={};
204 	rotation.x=parseFloat(this.getRotX())+parseFloat(this.getDRotX());
205 	rotation.y=parseFloat(this.getRotY())+parseFloat(this.getDRotY());
206 	rotation.z=parseFloat(this.getRotZ())+parseFloat(this.getDRotZ());
207 	return rotation;
208 };
209 
210 /**
211 * Gets the layers texture scale
212 213 * @return {object} the current scale
214 */
215 GLGE.MaterialLayer.prototype.getScale=function(){
216 	var scale={};
217 	scale.x=parseFloat(this.getScaleX())+parseFloat(this.getDScaleX());
218 	scale.y=parseFloat(this.getScaleY())+parseFloat(this.getDScaleY());
219 	scale.z=parseFloat(this.getScaleZ())+parseFloat(this.getDScaleZ());
220 	return scale;
221 };
222 
223 /**
224 * Sets the layers texture X offset
225 * @param {Number} value the amount to offset the texture
226 */
227 GLGE.MaterialLayer.prototype.setOffsetX=function(value){
228 	this.matrix=null;
229 	this.offsetX=value;
230 	return this;
231 };
232 /**
233 * Gets the layers texture X offset
234 * @return {Number} the current offset
235 */
236 GLGE.MaterialLayer.prototype.getOffsetX=function(){
237 	return this.offsetX;
238 };
239 /**
240 * Sets the layers texture Y offset
241 * @param {Number} value the amount to offset the texture
242 */
243 GLGE.MaterialLayer.prototype.setOffsetY=function(value){
244 	this.matrix=null;
245 	this.offsetY=value;
246 	return this;
247 };
248 /**
249 * Gets the layers texture Y offset
250 * @return {Number} the current offset
251 */
252 GLGE.MaterialLayer.prototype.getOffsetY=function(){
253 	return this.offsetY;
254 };
255 /**
256 * Sets the layers texture Z offset
257 * @param {Number} value the amount to offset the texture
258 */
259 GLGE.MaterialLayer.prototype.setOffsetZ=function(value){
260 	this.matrix=null;
261 	this.offsetZ=value;
262 	return this;
263 };
264 /**
265 * Gets the layers texture Z offset
266 * @return {Number} the current offset
267 */
268 GLGE.MaterialLayer.prototype.getOffsetZ=function(){
269 	return this.offsetZ;
270 };
271 /**
272 * Sets the layers texture X displacment offset, useful for animation
273 * @param {Number} value the amount to offset the texture
274 */
275 GLGE.MaterialLayer.prototype.setDOffsetX=function(value){
276 	this.matrix=null;
277 	this.dOffsetX=value;
278 	return this;
279 };
280 /**
281 * Gets the layers texture X displacment offset, useful for animation
282 * @return {Number} the current offset
283 */
284 GLGE.MaterialLayer.prototype.getDOffsetX=function(){
285 	return this.dOffsetX;
286 };
287 /**
288 * Sets the layers texture Y displacment offset, useful for animation
289 * @param {Number} value the amount to offset the texture
290 */
291 GLGE.MaterialLayer.prototype.setDOffsetY=function(value){
292 	this.matrix=null;
293 	this.dOffsetY=value;
294 	return this;
295 };
296 /**
297 * Gets the layers texture Y displacment offset, useful for animation
298 * @return {Number} the current offset
299 */
300 GLGE.MaterialLayer.prototype.getDOffsetY=function(){
301 	return this.dOffsetY;
302 };
303 /**
304 * Sets the layers texture Z displacment offset, useful for animation
305 * @param {Number} value the amount to offset the texture
306 */
307 GLGE.MaterialLayer.prototype.setDOffsetZ=function(value){
308 	this.matrix=null;
309 	this.dOffsetZ=value;
310 	return this;
311 };
312 /**
313 * Gets the layers texture X displacment offset, useful for animation
314 * @return {Number} the current offset
315 */
316 GLGE.MaterialLayer.prototype.getDOffsetZ=function(){
317 	return this.dOffsetZ;
318 };
319 /**
320 * Sets the layers texture X scale
321 * @param {Number} value the amount to scale the texture
322 */
323 GLGE.MaterialLayer.prototype.setScaleX=function(value){
324 	this.matrix=null;
325 	this.scaleX=value;
326 	return this;
327 };
328 /**
329 * Gets the layers texture X scale
330 * @return {Number} the current scale
331 */
332 GLGE.MaterialLayer.prototype.getScaleX=function(){
333 	return this.scaleX;
334 };
335 /**
336 * Sets the layers texture Y scale
337 * @param {Number} value the amount to scale the texture
338 */
339 GLGE.MaterialLayer.prototype.setScaleY=function(value){
340 	this.matrix=null;
341 	this.scaleY=value;
342 	return this;
343 };
344 /**
345 * Gets the layers texture Y scale
346 * @return {Number} the current scale
347 */
348 GLGE.MaterialLayer.prototype.getScaleY=function(){
349 	return this.scaleY;
350 };
351 /**
352 * Sets the layers texture Z scale
353 * @param {Number} value the amount to scale the texture
354 */
355 GLGE.MaterialLayer.prototype.setScaleZ=function(value){
356 	this.matrix=null;
357 	this.scaleZ=value;
358 	return this;
359 };
360 /**
361 * Gets the layers texture Z offset
362 * @return {Number} the current offset
363 */
364 GLGE.MaterialLayer.prototype.getScaleZ=function(){
365 	return this.scaleZ;
366 };
367 /**
368 * Sets the layers texture X displacment scale, useful for animation
369 * @param {Number} value the amount to scale the texture
370 */
371 GLGE.MaterialLayer.prototype.setDScaleX=function(value){
372 	this.matrix=null;
373 	this.dScaleX=value;
374 	return this;
375 };
376 /**
377 * Gets the layers texture X displacment scale, useful for animation
378 * @return {Number} the current scale
379 */
380 GLGE.MaterialLayer.prototype.getDScaleX=function(){
381 	return this.dScaleX;
382 };
383 /**
384 * Sets the layers texture Y displacment scale, useful for animation
385 * @param {Number} value the amount to scale the texture
386 */
387 GLGE.MaterialLayer.prototype.setDScaleY=function(value){
388 	this.matrix=null;
389 	this.dScaleY=value;
390 	return this;
391 };
392 /**
393 * Gets the layers texture Y displacment scale, useful for animation
394 * @return {Number} the current scale
395 */
396 GLGE.MaterialLayer.prototype.getDScaleY=function(){
397 	return this.dScaleY;
398 399 };
400 /**
401 * Sets the layers texture Z displacment scale, useful for animation
402 * @param {Number} value the amount to scale the texture
403 */
404 GLGE.MaterialLayer.prototype.setDScaleZ=function(value){
405 	this.matrix=null;
406 	this.dScaleZ=value;
407 	return this;
408 };
409 /**
410 * Gets the layers texture X displacment scale, useful for animation
411 * @return {Number} the current scale
412 */
413 GLGE.MaterialLayer.prototype.getDScaleZ=function(){
414 	return this.dScaleZ;
415 };
416 
417 
418 /**
419 * Sets the layers texture X Rotation
420 * @param {Number} value the amount to roate the texture
421 */
422 GLGE.MaterialLayer.prototype.setRotX=function(value){
423 	this.matrix=null;
424 	this.rotX=value;
425 	return this;
426 };
427 /**
428 * Gets the layers texture X rotate
429 * @return {Number} the current rotate
430 */
431 GLGE.MaterialLayer.prototype.getRotX=function(){
432 	return this.rotX;
433 };
434 /**
435 * Sets the layers texture Y rotate
436 * @param {Number} value the amount to rotate the texture
437 */
438 GLGE.MaterialLayer.prototype.setRotY=function(value){
439 	this.matrix=null;
440 	this.rotY=value;
441 	return this;
442 };
443 /**
444 * Gets the layers texture Y rotate
445 * @return {Number} the current rotate
446 */
447 GLGE.MaterialLayer.prototype.getRotY=function(){
448 	return this.rotY;
449 };
450 /**
451 * Sets the layers texture Z rotate
452 * @param {Number} value the amount to rotate the texture
453 */
454 GLGE.MaterialLayer.prototype.setRotZ=function(value){
455 	this.matrix=null;
456 	this.rotZ=value;
457 	return this;
458 };
459 /**
460 * Gets the layers texture Z rotate
461 * @return {Number} the current rotate
462 */
463 GLGE.MaterialLayer.prototype.getRotZ=function(){
464 	return this.rotZ;
465 };
466 /**
467 * Sets the layers texture X displacment rotation, useful for animation
468 * @param {Number} value the amount to rotation the texture
469 */
470 GLGE.MaterialLayer.prototype.setDRotX=function(value){
471 	this.matrix=null;
472 	this.dRotX=value;
473 	return this;
474 };
475 /**
476 * Gets the layers texture X displacment rotation, useful for animation
477 * @return {Number} the current rotation
478 */
479 GLGE.MaterialLayer.prototype.getDRotX=function(){
480 	return this.dRotX;
481 };
482 /**
483 * Sets the layers texture Y displacment rotation, useful for animation
484 * @param {Number} value the amount to rotaion the texture
485 */
486 GLGE.MaterialLayer.prototype.setDRotY=function(value){
487 	this.matrix=null;
488 	this.dRotY=value;
489 	return this;
490 };
491 /**
492 * Gets the layers texture Y displacment rotation, useful for animation
493 * @return {Number} the current rotation
494 */
495 GLGE.MaterialLayer.prototype.getDRotY=function(){
496 	return this.dRotY;
497 };
498 /**
499 * Sets the layers texture Z displacment rotation, useful for animation
500 * @param {Number} value the amount to rotation the texture
501 */
502 GLGE.MaterialLayer.prototype.setDRotZ=function(value){
503 	this.matrix=null;
504 	this.dRotZ=value;
505 	return this;
506 };
507 /**
508 * Gets the layers texture X displacment rotation, useful for animation
509 * @return {Number} the current rotation
510 */
511 GLGE.MaterialLayer.prototype.getDRotZ=function(){
512 	return this.dRotZ;
513 };
514 
515 /**
516 * Sets the layers blending mode
517 * @param {Number} value the blend mode for the layer
518 */
519 GLGE.MaterialLayer.prototype.setBlendMode=function(value){
520 	this.blendMode=value;
521 	this.fireEvent("shaderupdate",{});
522 	return this;
523 };
524 /**
525 * Gets the layers tblending mode
526 * @return {Number} the blend mode for the layer
527 */
528 GLGE.MaterialLayer.prototype.getBlendMode=function(){
529 	return this.blendMode;
530 };
531 
532 
533 
534 })(GLGE);