Advertisement
pol2095

Starling OpenFl mask TextureMaskStyle

Oct 21st, 2017
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // =================================================================================================
  2. //
  3. //  Starling Framework
  4. //  Copyright 2011-2016 Gamua. All Rights Reserved.
  5. //
  6. //  This program is free software. You can redistribute and/or modify it
  7. //  in accordance with the terms of the accompanying license agreement.
  8. //
  9. // =================================================================================================
  10.  
  11. package starling.extensions;
  12.  
  13. import starling.display.Mesh;
  14. import starling.rendering.MeshEffect;
  15. import starling.rendering.VertexDataFormat;
  16. import starling.styles.MeshStyle;
  17. import flash.display3D.Context3D;
  18. import starling.extensions.TextureMaskStyle;
  19. import starling.rendering.FilterEffect;
  20.  
  21. import starling.rendering.Program;
  22.  
  23.  
  24. class TextureMaskStyle extends MeshStyle
  25. {
  26.     public var threshold(get, set) : Float;
  27.  
  28.     public static var VERTEX_FORMAT : VertexDataFormat =
  29.         MeshStyle.VERTEX_FORMAT.extend("threshold:float1");
  30.    
  31.     private var _threshold : Float;
  32.    
  33.     public function new(threshold : Float = 0.5)
  34.     {
  35.         super();
  36.         _threshold = threshold;
  37.     }
  38.    
  39.     override public function copyFrom(meshStyle : MeshStyle) : Void
  40.     {
  41.         var otherStyle : TextureMaskStyle = try cast(meshStyle, TextureMaskStyle) catch(e:Dynamic) null;
  42.         if (otherStyle != null)
  43.         {
  44.             _threshold = otherStyle._threshold;
  45.         }
  46.        
  47.         super.copyFrom(meshStyle);
  48.     }
  49.    
  50.     override public function createEffect() : MeshEffect
  51.     {
  52.         return new TextureMaskEffect();
  53.     }
  54.    
  55.     override private function get_vertexFormat() : VertexDataFormat
  56.     {
  57.         return VERTEX_FORMAT;
  58.     }
  59.    
  60.     override private function onTargetAssigned(target : Mesh) : Void
  61.     {
  62.         updateVertices();
  63.     }
  64.    
  65.     private function updateVertices() : Void
  66.     {
  67.         var numVertices : Int = vertexData.numVertices;
  68.         for (i in 0...numVertices)
  69.         {
  70.             vertexData.setFloat(i, "threshold", _threshold);
  71.         }
  72.        
  73.         setRequiresRedraw();
  74.     }
  75.    
  76.     // properties
  77.    
  78.     private function get_threshold() : Float
  79.     {
  80.         return _threshold;
  81.     }
  82.     private function set_threshold(value : Float) : Float
  83.     {
  84.         if (_threshold != value && target != null)
  85.         {
  86.             _threshold = value;
  87.             updateVertices();
  88.         }
  89.         return value;
  90.     }
  91. }
  92.  
  93.  
  94.  
  95.  
  96. class TextureMaskEffect extends MeshEffect
  97. {
  98.     public static var VERTEX_FORMAT : VertexDataFormat = TextureMaskStyle.VERTEX_FORMAT;
  99.    
  100.     public function new()
  101.     {
  102.         super();
  103.     }
  104.    
  105.     override private function createProgram() : Program
  106.     {
  107.         if (texture != null)
  108.         {
  109.             var vertexShader : String = [
  110.                     "m44 op, va0, vc0",   // 4x4 matrix transform to output clip-space  
  111.                     "mov v0, va1     ",   // pass texture coordinates to fragment program  
  112.                     "mul v1, va2, vc4",   // multiply alpha (vc4) with color (va2), pass to fp  
  113.                     "mov v2, va3     "  // pass threshold to fp  
  114.             ].join("\n");
  115.            
  116.             var fragmentShader : String = [
  117.                     FilterEffect.tex("ft0", "v0", 0, texture),
  118.                     "sub ft1, ft0, v2.xxxx",   // subtract threshold  
  119.                     "kil ft1.w            ",   // abort if alpha < 0  
  120.                     "mul  oc, ft0, v1     "  // else multiply with color & copy to output buffer  
  121.             ].join("\n");
  122.            
  123.             return Program.fromSource(vertexShader, fragmentShader);
  124.         }
  125.         else
  126.         {
  127.             return super.createProgram();
  128.         }
  129.     }
  130.    
  131.     override private function beforeDraw(context : Context3D) : Void
  132.     {
  133.         super.beforeDraw(context);
  134.         if (texture != null)
  135.         {
  136.             vertexFormat.setVertexBufferAt(3, vertexBuffer, "threshold");
  137.         }
  138.     }
  139.    
  140.     override private function afterDraw(context : Context3D) : Void
  141.     {
  142.         if (texture != null)
  143.         {
  144.             context.setVertexBufferAt(3, null);
  145.         }
  146.         super.afterDraw(context);
  147.     }
  148.    
  149.     override private function get_vertexFormat() : VertexDataFormat
  150.     {
  151.         return VERTEX_FORMAT;
  152.     }
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement