PageRenderTime 57ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/src/org/papervision3d/materials/shaders/FlatShader.as

http://github.com/tcha-tcho/EZFLAR
ActionScript | 89 lines | 70 code | 10 blank | 9 comment | 3 complexity | d44fcf949f4ebdff85f14cdd1c6b9d4b MD5 | raw file
  1. package org.papervision3d.materials.shaders
  2. {
  3. import flash.display.BitmapData;
  4. import flash.display.Graphics;
  5. import flash.display.Sprite;
  6. import flash.geom.Matrix;
  7. import org.papervision3d.core.geom.renderables.Triangle3D;
  8. import org.papervision3d.core.math.Matrix3D;
  9. import org.papervision3d.core.proto.LightObject3D;
  10. import org.papervision3d.core.render.data.RenderSessionData;
  11. import org.papervision3d.core.render.shader.ShaderObjectData;
  12. import org.papervision3d.materials.utils.LightMaps;
  13. /**
  14. * @Author Ralph Hauwert
  15. */
  16. public class FlatShader extends LightShader implements IShader, ILightShader
  17. {
  18. private static var triMatrix:Matrix = new Matrix();
  19. private static var currentGraphics:Graphics;
  20. private static var zAngle:Number;
  21. private static var currentColor:int;
  22. private static var vx:Number;
  23. private static var vy:Number;
  24. private static var vz:Number;
  25. public var lightColor:int;
  26. public var ambientColor:int;
  27. private var _colors:Array;
  28. private var _colorRamp:BitmapData;
  29. public function FlatShader(light:LightObject3D, lightColor:int = 0xFFFFFF, ambientColor:int = 0x000000)
  30. {
  31. super();
  32. this.light = light;
  33. this.lightColor = lightColor;
  34. this.ambientColor = ambientColor;
  35. this._colors = LightMaps.getFlatMapArray(lightColor, ambientColor,1);
  36. this._colorRamp = LightMaps.getFlatMap(lightColor, ambientColor,1);
  37. }
  38. /**
  39. * Localized vars
  40. */
  41. private static var zd:Number;
  42. private static var lightMatrix:Matrix3D;
  43. private static var sod:ShaderObjectData;
  44. override public function renderLayer(triangle:Triangle3D, renderSessionData:RenderSessionData, sod:ShaderObjectData):void
  45. {
  46. lightMatrix = Matrix3D(sod.lightMatrices[this]);
  47. zd = triangle.faceNormal.x * lightMatrix.n31 + triangle.faceNormal.y * lightMatrix.n32 + triangle.faceNormal.z * lightMatrix.n33;
  48. if(zd < 0){
  49. zd = 0;
  50. };
  51. zd = zd*0xFF;
  52. triMatrix = sod.uvMatrices[triangle] ? sod.uvMatrices[triangle] : sod.getUVMatrixForTriangle(triangle);
  53. currentColor = _colors[int(zd)];
  54. currentGraphics = Sprite(layers[sod.object]).graphics;
  55. currentGraphics.beginFill(currentColor,1);
  56. currentGraphics.moveTo(triMatrix.tx, triMatrix.ty);
  57. currentGraphics.lineTo(triMatrix.a+triMatrix.tx, triMatrix.b+triMatrix.ty);
  58. currentGraphics.lineTo(triMatrix.c+triMatrix.tx, triMatrix.d+triMatrix.ty);
  59. currentGraphics.lineTo(triMatrix.tx, triMatrix.ty);
  60. currentGraphics.endFill();
  61. }
  62. /**
  63. *Localized var
  64. */
  65. public static var scaleMatrix:Matrix = new Matrix();
  66. override public function renderTri(triangle:Triangle3D, renderSessionData:RenderSessionData, sod:ShaderObjectData,bmp:BitmapData):void
  67. {
  68. lightMatrix = Matrix3D(sod.lightMatrices[this]);
  69. if(lightMatrix){
  70. zd = triangle.faceNormal.x * lightMatrix.n31 + triangle.faceNormal.y * lightMatrix.n32 + triangle.faceNormal.z * lightMatrix.n33;
  71. if(zd < 0){zd = 0;};
  72. scaleMatrix.a = bmp.width;
  73. scaleMatrix.d = bmp.height;
  74. scaleMatrix.tx =-int(zd*0xFF)*bmp.width;
  75. bmp.draw(_colorRamp, scaleMatrix,null,layerBlendMode, bmp.rect, false);
  76. }
  77. }
  78. }
  79. }