Drawable.hx 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. package h2d;
  2. class Drawable extends Sprite {
  3. public var color(default,null) : h3d.Vector;
  4. public var alpha(get, set) : Float;
  5. public var blendMode : BlendMode;
  6. public var filter : Bool;
  7. public var colorKey(default, set) : Null<Int>;
  8. var shaders : Array<hxsl.Shader>;
  9. function new(parent) {
  10. super(parent);
  11. blendMode = Normal;
  12. color = new h3d.Vector(1, 1, 1, 1);
  13. shaders = [];
  14. }
  15. function set_colorKey(v:Null<Int>) {
  16. if( shaders != null ) {
  17. var s = getShader(h3d.shader.ColorKey);
  18. if( s == null ) {
  19. if( v != null )
  20. s = addShader(new h3d.shader.ColorKey(0xFF000000 | v));
  21. } else {
  22. if( v == null )
  23. removeShader(s);
  24. else
  25. s.colorKey.setColor(0xFF000000 | v);
  26. }
  27. }
  28. return colorKey = v;
  29. }
  30. inline function get_alpha() {
  31. return color.a;
  32. }
  33. inline function set_alpha(v) {
  34. return color.a = v;
  35. }
  36. public function getDebugShaderCode( toHxsl = true ) {
  37. var shader = @:privateAccess {
  38. var ctx = getScene().ctx;
  39. var shaders : Array<hxsl.Shader> = [ctx.baseShader];
  40. shaders = shaders.concat(this.shaders);
  41. ctx.manager.compileShaders(shaders);
  42. }
  43. var toString = toHxsl ? function(d) return hxsl.Printer.shaderToString(d,true) : hxsl.GlslOut.toGlsl;
  44. return "VERTEX=\n" + toString(shader.vertex.data) + "\n\nFRAGMENT=\n" + toString(shader.fragment.data);
  45. }
  46. public function getShader< T:hxsl.Shader >( stype : Class<T> ) : T {
  47. for( s in shaders )
  48. if( Std.is(s, stype) )
  49. return cast s;
  50. return null;
  51. }
  52. public inline function getShaders() {
  53. return new hxd.impl.ArrayIterator<hxsl.Shader>(shaders);
  54. }
  55. public function addShader<T:hxsl.Shader>( s : T ) : T {
  56. this.shaders.push(s);
  57. return s;
  58. }
  59. public function removeShader( s : hxsl.Shader ) {
  60. return this.shaders.remove(s);
  61. }
  62. function emitTile( ctx : RenderContext, tile : Tile ) {
  63. if( tile == null )
  64. tile = new Tile(null, 0, 0, 5, 5);
  65. ctx.beginDrawBatch(this, tile.getTexture());
  66. var ax = absX + tile.dx * matA + tile.dy * matC;
  67. var ay = absY + tile.dx * matB + tile.dy * matD;
  68. var buf = ctx.buffer;
  69. var pos = ctx.bufPos;
  70. buf.grow(pos + 4 * 8);
  71. inline function emit(v:Float) buf[pos++] = v;
  72. emit(ax);
  73. emit(ay);
  74. emit(tile.u);
  75. emit(tile.v);
  76. emit(color.r);
  77. emit(color.g);
  78. emit(color.b);
  79. emit(color.a);
  80. var tw = tile.width;
  81. var th = tile.height;
  82. var dx1 = tw * matA;
  83. var dy1 = tw * matB;
  84. var dx2 = th * matC;
  85. var dy2 = th * matD;
  86. emit(ax + dx1);
  87. emit(ay + dy1);
  88. emit(tile.u2);
  89. emit(tile.v);
  90. emit(color.r);
  91. emit(color.g);
  92. emit(color.b);
  93. emit(color.a);
  94. emit(ax + dx2);
  95. emit(ay + dy2);
  96. emit(tile.u);
  97. emit(tile.v2);
  98. emit(color.r);
  99. emit(color.g);
  100. emit(color.b);
  101. emit(color.a);
  102. emit(ax + dx1 + dx2);
  103. emit(ay + dy1 + dy2);
  104. emit(tile.u2);
  105. emit(tile.v2);
  106. emit(color.r);
  107. emit(color.g);
  108. emit(color.b);
  109. emit(color.a);
  110. ctx.bufPos = pos;
  111. }
  112. }