├── LICENSE ├── README.md ├── atlasTriangle ├── ClipTriangle.hx ├── SpriteTriangle.hx ├── parser │ ├── AtlasTriangle.hx │ ├── Mesh.hx │ ├── SpriteUVParser.hx │ └── TexturePackerParser.hx ├── renderer │ ├── DrawTrianglesRenderer.hx │ └── Renderer.hx └── shaders │ ├── Deuteranopia.hx │ ├── DotScreen.hx │ ├── FilmShader.hx │ ├── GraphicsShader.hx │ ├── GrayScale.hx │ ├── Hexagonate.hx │ ├── HueSaturationShader.hx │ ├── Invert.hx │ ├── Pixelated.hx │ └── Technicolor.hx └── haxelib.json /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 loudoweb 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # AtlasTriangle 2 | Allow to use atlas packed with triangles with such tools as [SpriteUV2](https://www.spriteuv.com) or [TexturePacker](https://www.codeandweb.com/texturepacker/) for Haxe language. 3 | 4 | # Renderer 5 | Openfl Renderer using graphics.drawTriangles (gpu accelerated) 6 | I'll try to make a renderer using context3d.drawTriangles or directly gl later if it can improve performance or reduce memory footprint 7 | PR welcome to add other engines renderer :) 8 | 9 | 10 | # features 11 | * Batching (one drawcall per bitmapdata or shader instance) 12 | * rotation/scale using matrix or not 13 | * alpha 14 | * shader 15 | * GraphicsShader (default : alpha and colorTransform attributes) 16 | * HueSaturationShader (hue and saturation uniform). You could use this shader to use uniform instead of attributes to change the color of a whole set of triangles 17 | * GrayScale 18 | * Invert (invert colors) 19 | * Pixelated (pixelize the image) 20 | * Hexagonate (like Pixelated but with hexagonal 'pixels') 21 | * colorTransform 22 | * center (for position and rotation) 23 | * clip (set of mesh that updates depending of fps set) 24 | 25 | # TODO 26 | * group (wip in other branch) 27 | 28 | 29 | # Usage 30 | Use SpriteUV2 or TexturePacker to pack your sprites with triangles. This allows to spare some spaces in your atlas and possibly to have smaller atlas. 31 | The more triangles you use, the smallest atlas you'll get. It also means more CPU and less GPU. 32 | 33 | With SpriteUV2, please set **Pixel Per Unit** to **1** in the **exportGroup** panel. One more advice with this tool: check **As Single File** option. 34 | SpriteUV2 allows you to set the pivot point for each mesh. By default, it's the center of the original image. 35 | 36 | With TexturePacker, use **XML (generic)** exporter and then set **Algorithm** to **Polygon**. I also recommend to set **Extrude** to **0**. 37 | With TexturePacker, the pivot point is the top left of the original image by default. 38 | 39 | You can change the center/pivot point like this: `new SpriteTriangle(mesh, new Point(mesh.oW / 2, mesh.oH /2));` 40 | oW,oH (original width and height of the image source) and bounds (position and size of bounding box) are not available with SpriteUV2 due to lacks of data in the json. 41 | 42 | 43 | 44 | # Example (openfl) 45 | 46 | //create the renderer 47 | renderer = new DrawTrianglesRenderer(this); 48 | 49 | //create atlases data 50 | var data1 = TexturePackerParser.parseXML(Xml.parse(Assets.getText("atlas/atlas1.xml"))); 51 | var data2 = TexturePackerParser.parseXML(Xml.parse(Assets.getText("atlas/atlas2.xml"))); 52 | //add atlas bitmap to renderer 53 | var img1:BitmapData = Assets.getBitmapData("atlas/atlas1.png"); 54 | var img2:BitmapData = Assets.getBitmapData("atlas/atlas2.png"); 55 | //set name of bitmap used in atlases xml 56 | renderer.bitmaps.set("atlas1.png", img); 57 | renderer.bitmaps.set("atlas2.png", img2); 58 | 59 | //get meshes (shared data) 60 | var mesh1 = data.get("mesh1.png"); 61 | var meshes = data2.getClip("mesh_"); 62 | //create sprite (unique sprite. use shared mesh data) 63 | sprite = new SpriteTriangle(mesh1); 64 | clip = new ClipTriangle(meshes); 65 | 66 | //set the shader (must inherits GraphicsShader) 67 | //these lines are optional, default shader is already GraphicsShader 68 | var shader = new GraphicsShader(); 69 | sprite.shader = shader; 70 | clip.shader = shader; 71 | 72 | sprite.x = 120; 73 | sprite.y = 150; 74 | sprite.colorTransform = new ColorTransform(0.5); 75 | 76 | clip.x = 400; 77 | clip.y = 150; 78 | clip.alpha = 0.5; 79 | 80 | //add to the display list 81 | renderer.addChild(sprite); 82 | renderer.addChild(clip); 83 | 84 | //render 85 | renderer.update(deltaTime); 86 | -------------------------------------------------------------------------------- /atlasTriangle/ClipTriangle.hx: -------------------------------------------------------------------------------- 1 | package atlasTriangle; 2 | import atlasTriangle.parser.Mesh; 3 | import openfl.Vector; 4 | 5 | /** 6 | * A ClipTriangle is an animated SpriteTriangle 7 | * @author loudo 8 | */ 9 | class ClipTriangle extends SpriteTriangle 10 | { 11 | var _meshes:Array; 12 | 13 | public var fps:Int; 14 | var _lastFrame:Int; 15 | 16 | 17 | public function new(meshes:Array, fps:Int = 12) 18 | { 19 | _meshes = meshes; 20 | this.fps = fps; 21 | this.elapsedTime = 0; 22 | _lastFrame = -1; 23 | 24 | super(_meshes[0]); 25 | } 26 | 27 | override public function update(deltaTime:Int):Void 28 | { 29 | super.update(deltaTime); 30 | 31 | var frame = Math.floor((elapsedTime / 1000) * fps) % _meshes.length; 32 | 33 | if (_lastFrame != frame) 34 | { 35 | isDirty = true; 36 | _lastFrame = frame; 37 | 38 | //update current mesh 39 | var current = _meshes[frame]; 40 | coordinates = current.coordinates; 41 | indices = current.indices; 42 | uv = current.uv; 43 | textureID = current.textureID; 44 | } 45 | 46 | 47 | 48 | 49 | } 50 | 51 | } -------------------------------------------------------------------------------- /atlasTriangle/SpriteTriangle.hx: -------------------------------------------------------------------------------- 1 | package atlasTriangle; 2 | 3 | import atlasTriangle.parser.Mesh; 4 | import openfl.Vector; 5 | import atlasTriangle.shaders.GraphicsShader; 6 | import openfl.geom.ColorTransform; 7 | import openfl.geom.Matrix; 8 | import openfl.geom.Point; 9 | 10 | /** 11 | * A SpriteTriangle is made of a shared mesh but have unique x, y, alpha...on screen 12 | * @author loudo 13 | * @author some code from openfl 14 | */ 15 | class SpriteTriangle extends Mesh 16 | { 17 | public static var DEFAULT_SHADER = new GraphicsShader(); 18 | 19 | 20 | public var x(get, set):Float; 21 | public var y(get, set):Float; 22 | @:isVar public var alpha(default, set):Float; 23 | @:isVar public var shader(default, set):GraphicsShader; 24 | @:isVar public var center(default, set):Point; 25 | @:isVar public var matrix(default, set):Matrix; 26 | @:isVar public var colorTransform (default, set):ColorTransform; 27 | public var rotation(get, set):Null; 28 | public var scaleX(get, set):Null; 29 | public var scaleY(get, set):Null; 30 | 31 | public var isDirty:Bool; 32 | 33 | public var elapsedTime:Int; 34 | 35 | private var __rotationCosine:Float; 36 | private var __rotationSine:Float; 37 | private var __rotation:Null; 38 | private var __scaleX:Null; 39 | private var __scaleY:Null; 40 | //TODO pool 41 | 42 | public function new(mesh:Mesh, center:Point = null) 43 | { 44 | super(mesh.indices, mesh.uv, mesh.coordinates, mesh.textureID, mesh.oW, mesh.oH, mesh.bounds); 45 | this.matrix = new Matrix(); 46 | alpha = 1; 47 | this.center = center == null ? new Point() : center; 48 | 49 | elapsedTime = 0; 50 | shader = DEFAULT_SHADER; 51 | } 52 | 53 | public function get_x():Float 54 | { 55 | return matrix.tx; 56 | } 57 | 58 | public function set_x(value:Float):Float 59 | { 60 | if (x != value) 61 | { 62 | isDirty = true; 63 | matrix.tx = value; 64 | } 65 | 66 | return value; 67 | } 68 | 69 | public function get_y():Float 70 | { 71 | return matrix.ty; 72 | } 73 | 74 | public function set_y(value:Float):Float 75 | { 76 | if (y != value) 77 | { 78 | isDirty = true; 79 | matrix.ty = value; 80 | } 81 | 82 | return value; 83 | } 84 | 85 | public function set_alpha(value:Float):Float 86 | { 87 | if (alpha != value) 88 | { 89 | isDirty = true; 90 | alpha = value; 91 | } 92 | 93 | return value; 94 | } 95 | 96 | public function set_shader(value:GraphicsShader):GraphicsShader 97 | { 98 | if (shader != value) 99 | { 100 | isDirty = true; 101 | shader = value; 102 | } 103 | 104 | return value; 105 | } 106 | 107 | private function get_rotation ():Float { 108 | 109 | if (__rotation == null) { 110 | 111 | if (matrix.b == 0 && matrix.c == 0) { 112 | 113 | __rotation = 0; 114 | __rotationSine = 0; 115 | __rotationCosine = 1; 116 | 117 | } else { 118 | 119 | var radians = Math.atan2 (matrix.d, matrix.c) - (Math.PI / 2); 120 | 121 | __rotation = radians * (180 / Math.PI); 122 | __rotationSine = Math.sin (radians); 123 | __rotationCosine = Math.cos (radians); 124 | 125 | } 126 | 127 | } 128 | 129 | return __rotation; 130 | 131 | } 132 | 133 | public function set_rotation(value:Float):Float 134 | { 135 | if (__rotation != value) 136 | { 137 | isDirty = true; 138 | __rotation = value; 139 | 140 | var radians = value * (Math.PI / 180); 141 | __rotationSine = Math.sin (radians); 142 | __rotationCosine = Math.cos (radians); 143 | 144 | var __scaleX = this.scaleX; 145 | var __scaleY = this.scaleY; 146 | 147 | matrix.a = __rotationCosine * __scaleX; 148 | matrix.b = __rotationSine * __scaleX; 149 | matrix.c = -__rotationSine * __scaleY; 150 | matrix.d = __rotationCosine * __scaleY; 151 | 152 | } 153 | 154 | return value; 155 | } 156 | 157 | private function get_scaleX ():Float { 158 | 159 | if (__scaleX == null) { 160 | 161 | if (matrix.b == 0) { 162 | 163 | __scaleX = matrix.a; 164 | 165 | } else { 166 | 167 | __scaleX = Math.sqrt (matrix.a * matrix.a + matrix.b * matrix.b); 168 | 169 | } 170 | 171 | } 172 | 173 | return __scaleX; 174 | 175 | } 176 | 177 | public function set_scaleX(value:Float):Float 178 | { 179 | if (__scaleX != value) 180 | { 181 | isDirty = true; 182 | __scaleX = value; 183 | 184 | if (matrix.b == 0) { 185 | 186 | matrix.a = value; 187 | 188 | } else { 189 | 190 | var rotation = this.rotation; 191 | 192 | var a = __rotationCosine * value; 193 | var b = __rotationSine * value; 194 | 195 | matrix.a = a; 196 | matrix.b = b; 197 | 198 | } 199 | } 200 | 201 | return value; 202 | } 203 | 204 | private function get_scaleY ():Float { 205 | 206 | if (__scaleY == null) { 207 | 208 | if (matrix.c == 0) { 209 | 210 | __scaleY = matrix.d; 211 | 212 | } else { 213 | 214 | __scaleY = Math.sqrt (matrix.c * matrix.c + matrix.d * matrix.d); 215 | 216 | } 217 | 218 | } 219 | 220 | return __scaleY; 221 | 222 | } 223 | 224 | public function set_scaleY(value:Float):Float 225 | { 226 | if (__scaleY != value) 227 | { 228 | isDirty = true; 229 | __scaleY = value; 230 | 231 | if (matrix.c == 0) { 232 | 233 | matrix.d = value; 234 | 235 | } else { 236 | 237 | var rotation = this.rotation; 238 | 239 | var c = -__rotationSine * value; 240 | var d = __rotationCosine * value; 241 | 242 | matrix.c = c; 243 | matrix.d = d; 244 | 245 | } 246 | } 247 | 248 | return value; 249 | } 250 | 251 | public function set_matrix(value:Matrix):Matrix 252 | { 253 | if (matrix != value) 254 | { 255 | matrix = value; 256 | __rotation = null; 257 | __scaleX = null; 258 | __scaleY = null; 259 | isDirty = true; 260 | } 261 | 262 | 263 | 264 | return value; 265 | } 266 | 267 | public function set_center(value:Point):Point 268 | { 269 | if (center != value) 270 | { 271 | isDirty = true; 272 | center = value; 273 | } 274 | 275 | return value; 276 | } 277 | 278 | public function set_colorTransform(value:ColorTransform):ColorTransform 279 | { 280 | if (colorTransform != value) 281 | { 282 | isDirty = true; 283 | colorTransform = value; 284 | } 285 | 286 | return value; 287 | } 288 | 289 | public function update(deltaTime:Int):Void 290 | { 291 | 292 | elapsedTime += deltaTime; 293 | } 294 | 295 | } -------------------------------------------------------------------------------- /atlasTriangle/parser/AtlasTriangle.hx: -------------------------------------------------------------------------------- 1 | package atlasTriangle.parser; 2 | import openfl.Vector; 3 | import openfl.geom.Rectangle; 4 | using StringTools; 5 | 6 | /** 7 | * Atlas packed with triangles/polygons instead of squares. 8 | * Should reduce size of atlas and drawing surface, but takes more memory to manage more triangles. 9 | * @author loudo 10 | */ 11 | class AtlasTriangle 12 | { 13 | 14 | public var meshes:Map; 15 | public var textureID:String; 16 | 17 | public function new() 18 | { 19 | meshes = new Map(); 20 | } 21 | 22 | /** 23 | * Save data of a Sprite 24 | * @param name 25 | * @param indices 26 | * @param uv 27 | * @param coordinates 28 | */ 29 | public function add(name:String, indices:Vector, uv:Vector, coordinates:Vector, oW:Int = 0, oH:Int = 0, bounds:Rectangle = null) 30 | { 31 | meshes.set(name, new Mesh(indices, uv, coordinates, textureID, oW, oH, bounds)); 32 | } 33 | /** 34 | * Get data of a Sprite 35 | * @param name of the Sprite in the atlas 36 | * @return 37 | */ 38 | public function get(name:String):Mesh 39 | { 40 | if (meshes.exists(name)) 41 | { 42 | return meshes.get(name); 43 | } 44 | return null; 45 | } 46 | /** 47 | * Get data of a Clip 48 | * @param name of the Sprite in the atlas 49 | * @return All meshes starting with `name` 50 | */ 51 | public function getClip(name:String):Array 52 | { 53 | var clip = new Array(); 54 | for (key in meshes.keys()) 55 | { 56 | if (key.startsWith(name)) 57 | clip.push(meshes.get(key)); 58 | } 59 | //TODO ordering? cache? 60 | return clip; 61 | } 62 | 63 | } -------------------------------------------------------------------------------- /atlasTriangle/parser/Mesh.hx: -------------------------------------------------------------------------------- 1 | package atlasTriangle.parser; 2 | import openfl.Vector; 3 | import openfl.geom.Rectangle; 4 | 5 | /** 6 | * ... 7 | * @author loudo 8 | */ 9 | class Mesh 10 | { 11 | 12 | public var indices:Vector;//TODO fixed length 13 | public var uv:Vector;//TODO fixed length 14 | public var coordinates:Vector;//TODO fixed length 15 | public var textureID:String; 16 | 17 | /** 18 | * Original width of the source image 19 | * @usage TexturePacker 20 | */ 21 | public var oW:Int; 22 | /** 23 | * Original height of the source image 24 | * @usage TexturePacker 25 | */ 26 | public var oH:Int; 27 | /** 28 | * Bounding box of the mesh 29 | * @usage TexturePacker 30 | */ 31 | public var bounds:Rectangle; 32 | 33 | public function new(indices:Vector, uv:Vector, coordinates:Vector, textureID:String, oW:Int = 0, oH:Int = 0, bounds:Rectangle = null ) 34 | { 35 | this.indices = indices; 36 | this.uv = uv; 37 | this.coordinates = coordinates; 38 | this.textureID = textureID; 39 | this.oW = oW; 40 | this.oH = oH; 41 | this.bounds = bounds; 42 | } 43 | 44 | } -------------------------------------------------------------------------------- /atlasTriangle/parser/SpriteUVParser.hx: -------------------------------------------------------------------------------- 1 | package atlasTriangle.parser; 2 | import atlasTriangle.parser.AtlasTriangle; 3 | 4 | /** 5 | * Tools: SpriteUV2 6 | * @author loudo 7 | */ 8 | typedef SpriteUV = { 9 | var mat:Material; 10 | var mesh:Array; 11 | } 12 | typedef Material = { 13 | var name:String; 14 | var txName:Array; 15 | } 16 | typedef Mesh = { 17 | var mat:String; 18 | var name:String; 19 | var pos:MeshPos; 20 | var tri:Array; 21 | var uv:Array; 22 | var v2:Array; 23 | } 24 | typedef MeshPos = { 25 | var x:Float; 26 | var y:Float; 27 | var z:Float; 28 | } 29 | class SpriteUVParser extends AtlasTriangle 30 | { 31 | 32 | /** 33 | * 34 | * @param json 35 | * @param topLeft fix origin to topleft by default (SpriteUV set it to bottom left) 36 | */ 37 | public function new(json:String, topLeft:Bool = true) 38 | { 39 | super(); 40 | 41 | var data:SpriteUV = haxe.Json.parse(json); 42 | textureID = data.mat.txName; 43 | 44 | for (i in 0...data.mesh.length) 45 | { 46 | var mesh = data.mesh[i]; 47 | if (topLeft) 48 | { 49 | 50 | for (j in 0...mesh.uv.length) 51 | { 52 | if(j % 2 != 0) 53 | mesh.uv[j] = 1 - mesh.uv[j]; 54 | } 55 | for (j in 0...mesh.v2.length) 56 | { 57 | if(j % 2 != 0) 58 | mesh.v2[j] = 1 - mesh.v2[j]; 59 | } 60 | } 61 | add(mesh.name, mesh.tri, mesh.uv, mesh.v2);//TODO calculate bounding box 62 | } 63 | 64 | } 65 | 66 | } -------------------------------------------------------------------------------- /atlasTriangle/parser/TexturePackerParser.hx: -------------------------------------------------------------------------------- 1 | package atlasTriangle.parser; 2 | import atlasTriangle.parser.AtlasTriangle; 3 | import openfl.Vector; 4 | import openfl.geom.Rectangle; 5 | 6 | #if (haxe_ver >= 4) 7 | typedef Access = haxe.xml.Access; 8 | #else 9 | typedef Access = haxe.xml.Fast; 10 | #end 11 | 12 | typedef TexturePackerPolygon = { 13 | var frames:Array; 14 | var meta:Meta; 15 | } 16 | typedef Meta = { 17 | var image:String; 18 | var size:Size; 19 | var format:String; 20 | var scale:Float; 21 | } 22 | typedef Size = { 23 | var w:Int; 24 | var h:Int; 25 | } 26 | typedef Sprite = { 27 | var filename:String; 28 | var frame:Frame; 29 | var rotated:Bool; 30 | var trimmed:Bool; 31 | var spriteSourceSize:Frame; 32 | var sourceSize:Size; 33 | var pivot:Pivot; 34 | var vertices:Array>; 35 | var verticesUV:Array>; 36 | var triangles:Array>; 37 | } 38 | typedef Frame = { 39 | var x:Int; 40 | var y:Int; 41 | var w:Int; 42 | var h:Int; 43 | } 44 | typedef Pivot = { 45 | var x:Int; 46 | var y:Int; 47 | } 48 | 49 | /** 50 | * Tools: TexturePacker 51 | * Export format: XML (generic) or JSON (generic) with Algorithm: Polygon 52 | * @author loudo 53 | */ 54 | class TexturePackerParser extends AtlasTriangle 55 | { 56 | 57 | public static function parseXML(xml:Xml):TexturePackerParser 58 | { 59 | var t = new TexturePackerParser(); 60 | var fast:Access = new Access(xml.firstElement()); 61 | 62 | t.textureID = fast.att.imagePath; 63 | 64 | var w = Std.parseInt(fast.att.width); 65 | var h = Std.parseInt(fast.att.height); 66 | 67 | var i:Int; 68 | 69 | for (sprite in fast.nodes.sprite) 70 | { 71 | i = 0; 72 | 73 | var indicesArray = sprite.node.triangles.innerData.toString().split(' ').map(Std.parseInt); 74 | var indices:Vector = Vector.ofArray(indicesArray); 75 | var uv:Vector = Vector.ofArray(sprite.node.verticesUV.innerData.toString().split(' ').map(function (str:String) { 76 | var out = i % 2 == 0 ? Std.parseInt(str) / w : Std.parseInt(str) / h; 77 | i++; 78 | return out; 79 | })); 80 | 81 | var vertices:Vector = Vector.ofArray(sprite.node.vertices.innerData.toString().split(' ').map(Std.parseFloat)); 82 | /** 83 | //TODO test this instead of map for performance 84 | var index = 0, search = -1, lastSearch = 0; 85 | while ((search = text.indexOf (",", lastSearch)) > -1) { 86 | indices[index] = Std.parseInt(text.substr(lastSearch, search); 87 | index++; 88 | } 89 | */ 90 | var oW = sprite.has.oW ? Std.parseInt(sprite.att.oW) : Std.parseInt(sprite.att.w); 91 | var oH = sprite.has.oH ? Std.parseInt(sprite.att.oH) : Std.parseInt(sprite.att.h); 92 | var oX = sprite.has.oX ? Std.parseInt(sprite.att.oX) : 0; 93 | var oY = sprite.has.oY ? Std.parseInt(sprite.att.oY) : 0; 94 | t.add(sprite.att.n, indices, uv, vertices, 95 | oW, oH, 96 | new Rectangle(oX, oY, Std.parseInt(sprite.att.w), Std.parseInt(sprite.att.h))); 97 | } 98 | return t; 99 | } 100 | 101 | /** 102 | * 103 | * @param json (ARRAY) 104 | * @return 105 | */ 106 | public static function parseJSON(json:String):TexturePackerParser 107 | { 108 | var t = new TexturePackerParser(); 109 | 110 | var data:TexturePackerPolygon = haxe.Json.parse(json); 111 | 112 | t.textureID = data.meta.image; 113 | var w = data.meta.size.w; 114 | var h = data.meta.size.h; 115 | 116 | 117 | var indices:Vector = new Vector(); 118 | var vertices:Vector = new Vector(); 119 | var uv:Vector = new Vector(); 120 | 121 | for (i in 0...data.frames.length) 122 | { 123 | var mesh = data.frames[i]; 124 | for (j in 0...mesh.triangles.length) 125 | { 126 | for (k in 0...3) 127 | { 128 | indices.push(mesh.triangles[j][k]); 129 | } 130 | 131 | } 132 | 133 | for (j in 0...mesh.vertices.length) 134 | { 135 | for (k in 0...2) 136 | { 137 | vertices.push(mesh.vertices[j][k]); 138 | } 139 | 140 | } 141 | 142 | for (j in 0...mesh.verticesUV.length) 143 | { 144 | for (k in 0...2) 145 | { 146 | if (k == 0) 147 | uv.push(mesh.verticesUV[j][k] / w); 148 | else 149 | uv.push(mesh.verticesUV[j][k] / h); 150 | } 151 | } 152 | 153 | t.add(mesh.filename, indices, uv, vertices, mesh.sourceSize.w, mesh.sourceSize.h, new Rectangle(mesh.spriteSourceSize.x, mesh.spriteSourceSize.y, mesh.spriteSourceSize.w, mesh.spriteSourceSize.h) ); 154 | } 155 | 156 | return t; 157 | } 158 | 159 | public function new() 160 | { 161 | super(); 162 | } 163 | 164 | } -------------------------------------------------------------------------------- /atlasTriangle/renderer/DrawTrianglesRenderer.hx: -------------------------------------------------------------------------------- 1 | package atlasTriangle.renderer; 2 | import atlasTriangle.parser.AtlasTriangle; 3 | import openfl.display.BitmapData; 4 | import atlasTriangle.shaders.GraphicsShader; 5 | import openfl.display.Sprite; 6 | 7 | /** 8 | * ... 9 | * @author loudo 10 | */ 11 | class DrawTrianglesRenderer extends Renderer 12 | { 13 | #if triangle_debug 14 | var _debug:Sprite; 15 | #end 16 | 17 | public function new(canvas:Sprite, debug:Sprite = null) 18 | { 19 | super(canvas); 20 | 21 | #if triangle_debug 22 | _debug = debug; 23 | #end 24 | } 25 | 26 | override public function update(deltaTime:Int):Void 27 | { 28 | 29 | advance(deltaTime); 30 | 31 | var _wasDirty = isDirty; 32 | 33 | if (_wasDirty) 34 | { 35 | _canvas.graphics.clear(); 36 | #if triangle_debug 37 | _debug.graphics.clear(); 38 | #end 39 | } 40 | 41 | prepareBuffers(); 42 | 43 | if (_wasDirty) 44 | { 45 | _canvas.graphics.endFill(); 46 | #if triangle_debug 47 | _debug.graphics.endFill(); 48 | #end 49 | } 50 | } 51 | 52 | override function render(bitmapID:String, shader:GraphicsShader, hasColor:Bool):Void 53 | { 54 | super.render(bitmapID, shader, hasColor); 55 | //trace(_bufferCoor, "\n", _bufferIndices, "\n", _bufferUV, "\n", _bufferAlpha); 56 | //trace(_bufferColorMultiplier, "\n", _bufferColorOffset); 57 | #if flash 58 | //TODO pxb for the flash shader?? 59 | //for now alpha, shader and colortransform are not supported on flash 60 | _canvas.graphics.beginBitmapFill(bitmaps.get(bitmapID), null, false, true); 61 | #else 62 | shader.bitmap.input = bitmaps.get(bitmapID); 63 | shader.bitmap.filter = LINEAR; 64 | shader.triangle_Alpha.value = _bufferAlpha; 65 | shader.triangle_HasColorTransform.value = [hasColor]; 66 | if (hasColor) 67 | { 68 | shader.triangle_ColorMultiplier.value = _bufferColorMultiplier; 69 | shader.triangle_ColorOffset.value = _bufferColorOffset; 70 | } 71 | _canvas.graphics.beginShaderFill(shader, null); 72 | #end 73 | _canvas.graphics.drawTriangles(_bufferCoor, _bufferIndices, _bufferUV); 74 | 75 | #if triangle_debug 76 | _debug.graphics.lineStyle(2, 0xff0000, 0.7); 77 | var t = 0; 78 | for (i in 0..._bufferIndices.length) 79 | { 80 | var buf = _bufferIndices[i]; 81 | var x = _bufferCoor[buf * 2]; 82 | var y = _bufferCoor[buf * 2 + 1]; 83 | if (t == 0){ 84 | _debug.graphics.moveTo(x, y); 85 | } 86 | _debug.graphics.lineTo(x, y); 87 | t++; 88 | if (t == 3) 89 | { 90 | buf = _bufferIndices[i - 2]; 91 | x = _bufferCoor[buf * 2]; 92 | y = _bufferCoor[buf * 2 + 1]; 93 | _debug.graphics.lineTo(x, y); 94 | t = 0; 95 | } 96 | } 97 | #end 98 | 99 | } 100 | 101 | } -------------------------------------------------------------------------------- /atlasTriangle/renderer/Renderer.hx: -------------------------------------------------------------------------------- 1 | package atlasTriangle.renderer; 2 | import atlasTriangle.SpriteTriangle; 3 | import haxe.ds.StringMap; 4 | import lime.utils.Log; 5 | import openfl.Vector; 6 | import openfl.display.BitmapData; 7 | import atlasTriangle.shaders.GraphicsShader; 8 | import openfl.display.Sprite; 9 | import openfl.geom.Matrix; 10 | #if gl_stats 11 | import openfl._internal.renderer.context3D.stats.Context3DStats; 12 | #end 13 | 14 | @:access(openfl.geom.Matrix) 15 | @:access(openfl.display.DisplayObject) 16 | 17 | /** 18 | * ... 19 | * @author loudo 20 | */ 21 | class Renderer 22 | { 23 | 24 | var _children:Array; 25 | 26 | var _bufferCoor:Vector; 27 | var _bufferUV:Vector; 28 | var _bufferIndices:Vector; 29 | var _bufferAlpha:Array; 30 | var _bufferColorMultiplier:Array; 31 | var _bufferColorOffset:Array; 32 | 33 | var _canvas:Sprite; 34 | 35 | var _lastBitmap:String; 36 | var _lastShader:GraphicsShader; 37 | 38 | var _drawCall:Int = 0; 39 | 40 | public var isDirty:Bool; 41 | public var hasColorTransform:Bool; 42 | 43 | public var bitmaps:StringMap; 44 | 45 | public function new(canvas:Sprite) 46 | { 47 | _canvas = canvas; 48 | _children = []; 49 | _bufferCoor = new Vector(); 50 | _bufferUV = new Vector(); 51 | _bufferIndices = new Vector(); 52 | _bufferAlpha = []; 53 | _bufferColorMultiplier = []; 54 | _bufferColorOffset = []; 55 | bitmaps = new StringMap(); 56 | hasColorTransform = false; 57 | } 58 | 59 | public function update(deltaTime:Int):Void 60 | { 61 | 62 | advance(deltaTime); 63 | prepareBuffers(); 64 | 65 | } 66 | 67 | inline function advance(deltaTime:Int):Void 68 | { 69 | var _current:SpriteTriangle; 70 | 71 | for (i in 0..._children.length) 72 | { 73 | _current = _children[i]; 74 | _current.update(deltaTime); 75 | if (_current.isDirty) 76 | { 77 | isDirty = true; 78 | } 79 | if (_current.colorTransform != null) 80 | { 81 | hasColorTransform = true; 82 | } 83 | } 84 | } 85 | 86 | function prepareBuffers():Void 87 | { 88 | if (isDirty) 89 | { 90 | cleanBuffers(); 91 | 92 | var _current:SpriteTriangle; 93 | 94 | _lastBitmap = ""; 95 | _lastShader = null; 96 | var _render:Bool; 97 | 98 | var _len = 0; 99 | var _len2 = 0; 100 | var _len3 = 0; 101 | var triangleTransform = #if !flash Matrix.__pool.get () #else new Matrix() #end;//TODO pool for flash 102 | 103 | var defaultColorTransform = #if !flash _canvas.__worldColorTransform #else _canvas.transform.colorTransform #end; 104 | var currentColorTransform = null; 105 | 106 | for (i in 0..._children.length) 107 | { 108 | _render = false; 109 | 110 | _current = _children[i]; 111 | _current.isDirty = false; 112 | 113 | triangleTransform.setTo (1, 0, 0, 1, -_current.center.x, -_current.center.y); 114 | triangleTransform.concat (_current.matrix); 115 | //triangleTransform.concat (parentTransform); 116 | //triangleTransform.tx = Math.round (triangleTransform.tx); 117 | //triangleTransform.ty = Math.round (triangleTransform.ty); 118 | 119 | if (_current.textureID != _lastBitmap) { 120 | if (_lastBitmap != "") 121 | { 122 | _render = true; 123 | 124 | }else { 125 | _lastBitmap = _current.textureID; 126 | } 127 | } 128 | if (_current.shader != _lastShader){ 129 | if (_lastShader != null) 130 | { 131 | _render = true; 132 | 133 | }else { 134 | _lastShader = _current.shader; 135 | } 136 | } 137 | if (_render) 138 | { 139 | render(_lastBitmap, _lastShader, hasColorTransform); 140 | _len = 0; 141 | _len2 = 0; 142 | _len3 = 0; 143 | cleanBuffers(); 144 | _lastBitmap = _current.textureID; 145 | _lastShader = _current.shader; 146 | } 147 | 148 | //_bufferCoor = _bufferCoor.concat(_current.coor_computed); 149 | //_bufferUV = _bufferUV.concat(_current.uv); 150 | //_bufferIndices = _bufferIndices.concat(_current.indices); 151 | 152 | 153 | for (j in 0..._current.uv.length) 154 | { 155 | _bufferUV[_len2 + j] = _current.uv[j]; 156 | } 157 | _len2 += _current.uv.length; 158 | 159 | 160 | if (hasColorTransform) 161 | { 162 | currentColorTransform = _current.colorTransform != null ? _current.colorTransform : defaultColorTransform; 163 | } 164 | 165 | for (j in 0..._current.indices.length) 166 | { 167 | _bufferAlpha[_len3 + j] = _current.alpha; 168 | _bufferIndices[_len3 + j] = _current.indices[j] + Std.int(_len / 2); 169 | 170 | if (hasColorTransform) 171 | { 172 | _bufferColorMultiplier[(_len3 + j) * 4] = currentColorTransform.redMultiplier; 173 | _bufferColorMultiplier[(_len3 + j) * 4 + 1] = currentColorTransform.greenMultiplier; 174 | _bufferColorMultiplier[(_len3 + j) * 4 + 2] = currentColorTransform.blueMultiplier; 175 | _bufferColorMultiplier[(_len3 + j) * 4 + 3] = currentColorTransform.alphaMultiplier; 176 | _bufferColorOffset[(_len3 + j) * 4] = currentColorTransform.redOffset; 177 | _bufferColorOffset[(_len3 + j) * 4 + 1] = currentColorTransform.greenOffset; 178 | _bufferColorOffset[(_len3 + j) * 4 + 2] = currentColorTransform.blueOffset; 179 | _bufferColorOffset[(_len3 + j) * 4 + 3] = currentColorTransform.alphaOffset; 180 | } 181 | } 182 | 183 | _len3 += _current.indices.length; 184 | 185 | for (j in 0..._current.coordinates.length) 186 | { 187 | if (j % 2 == 0) { 188 | #if !flash 189 | _bufferCoor[_len + j] = triangleTransform.__transformX(_current.coordinates[j], _current.coordinates[j + 1]); 190 | #else 191 | _bufferCoor[_len + j] = _current.coordinates[j] * triangleTransform.a + _current.coordinates[j + 1] * triangleTransform.c + triangleTransform.tx; 192 | #end 193 | }else { 194 | #if !flash 195 | _bufferCoor[_len + j] = triangleTransform.__transformY(_current.coordinates[j - 1], _current.coordinates[j]); 196 | #else 197 | _bufferCoor[_len + j] = _current.coordinates[j - 1] * triangleTransform.b + _current.coordinates[j] * triangleTransform.d + triangleTransform.ty; 198 | #end 199 | } 200 | } 201 | _len += _current.coordinates.length; 202 | 203 | } 204 | 205 | isDirty = false; 206 | hasColorTransform = false; 207 | 208 | if(_len3 > 0) 209 | render(_lastBitmap, _lastShader, hasColorTransform); 210 | 211 | #if !flash 212 | Matrix.__pool.release (triangleTransform); 213 | #end 214 | 215 | } 216 | 217 | 218 | 219 | #if gl_stats 220 | Log.info('$_drawCall drawCalls (total ${Context3DStats.totalDrawCalls()})');//must compile with -Dgl_stats 221 | #else 222 | Log.info('$_drawCall drawCalls'); 223 | #end 224 | _drawCall = 0; 225 | } 226 | 227 | inline function cleanBuffers():Void 228 | { 229 | #if flash 230 | _bufferCoor.splice(0, _bufferCoor.length); 231 | _bufferUV.splice(0, _bufferUV.length); 232 | _bufferIndices.splice(0, _bufferIndices.length); 233 | _bufferAlpha.splice(0, _bufferAlpha.length); 234 | _bufferColorMultiplier.splice(0, _bufferColorMultiplier.length); 235 | _bufferColorOffset.splice(0, _bufferColorOffset.length); 236 | #else 237 | //don't use splice to empty the buffers, otherwise Vector sent will be overwritten before rendering 238 | //TODO use pool 239 | _bufferCoor = new Vector(); 240 | _bufferUV = new Vector(); 241 | _bufferIndices = new Vector(); 242 | _bufferAlpha = []; 243 | _bufferColorMultiplier = []; 244 | _bufferColorOffset = []; 245 | #end 246 | 247 | } 248 | 249 | function render(bitmapID:String, shader:GraphicsShader, hasColor:Bool):Void 250 | { 251 | _drawCall++; 252 | } 253 | 254 | public function addChild(sprite:SpriteTriangle):Void 255 | { 256 | _children.push(sprite); 257 | isDirty = true; 258 | } 259 | 260 | public function addChildAt(sprite:SpriteTriangle, index:Int):Void 261 | { 262 | 263 | if( index > _children.length){ 264 | _children.push(sprite); 265 | }else if(index <= 0){ 266 | _children.unshift(sprite); 267 | }else { 268 | _children.insert(index, sprite); 269 | } 270 | isDirty = true; 271 | } 272 | 273 | /** 274 | * Remove a SpriteTriangle from screen 275 | * @param index of the SpriteTriangle you want to remove 276 | */ 277 | public function removeChildAt(index:Int):Void 278 | { 279 | if (index >= 0 && index < _children.length) { 280 | _children.splice(index, 1); 281 | }else { 282 | trace('index outside range'); 283 | } 284 | isDirty = true; 285 | } 286 | /** 287 | * Remove all SpriteTriangle from screen 288 | */ 289 | public function removeAll():Void 290 | { 291 | _children.splice(0, _children.length); 292 | isDirty = true; 293 | } 294 | 295 | /** 296 | * Retrieve the index of a SpriteTriangle 297 | * @param spriter 298 | * @return index 299 | */ 300 | public function getIndex(sprite:SpriteTriangle):Int 301 | { 302 | return _children.indexOf(sprite); 303 | } 304 | 305 | /** 306 | * Get a SpriteTriangle from its index 307 | * @param index 308 | * @return 309 | */ 310 | public function getSpriterAt(index:Int):SpriteTriangle 311 | { 312 | if (index >= 0 && index < _children.length) 313 | return _children[index]; 314 | else 315 | trace("index outside range"); 316 | return null; 317 | } 318 | 319 | /** 320 | * Swaps the indexes of two children. 321 | * @param spriter1 322 | * @param spriter2 323 | */ 324 | public function swap(sprite1:SpriteTriangle, sprite2:SpriteTriangle):Void 325 | { 326 | var index1 = getIndex(sprite1); 327 | var index2 = getIndex(sprite2); 328 | if (index1 == -1 || index2 == -1) trace("Not in this container"); 329 | swapAt(index1, index2); 330 | } 331 | 332 | /** 333 | * Swaps the indexes of two children. 334 | * @param index1 335 | * @param index2 336 | */ 337 | public function swapAt(index1:Int, index2:Int):Void 338 | { 339 | var spriter1 = getSpriterAt(index1); 340 | var spriter2 = getSpriterAt(index2); 341 | _children[index1] = spriter2; 342 | _children[index2] = spriter1; 343 | isDirty = true; 344 | } 345 | 346 | } -------------------------------------------------------------------------------- /atlasTriangle/shaders/Deuteranopia.hx: -------------------------------------------------------------------------------- 1 | package atlasTriangle.shaders; 2 | import atlasTriangle.shaders.GraphicsShader; 3 | /** 4 | * ... 5 | * @author loudo 6 | */ 7 | class Deuteranopia extends GraphicsShader 8 | { 9 | 10 | @:glFragmentSource( 11 | "#pragma header 12 | const mat4 mDeuteranopia = mat4( 0.43 , 0.72 , -0.15 , 0.0 , 13 | 0.34 , 0.57 , 0.09 , 0.0 , 14 | -0.02 , 0.03 , 1.00 , 0.0 , 15 | 0.0 , 0.0 , 0.0 , 1.0 ); 16 | 17 | void main(void) { 18 | 19 | gl_FragColor = mDeuteranopia * texture2D(bitmap, openfl_TextureCoordv); 20 | gl_FragColor = gl_FragColor * openfl_Alphav; 21 | 22 | }" 23 | ) 24 | 25 | public function new() 26 | { 27 | super(); 28 | } 29 | 30 | } -------------------------------------------------------------------------------- /atlasTriangle/shaders/DotScreen.hx: -------------------------------------------------------------------------------- 1 | package atlasTriangle.shaders; 2 | 3 | import openfl.utils.ByteArray; 4 | 5 | /** 6 | * @description Simulates a black and white halftone rendering of the image by multiplying 7 | * pixel values with a rotated 2D sine wave pattern. 8 | * @param center The x and y coordinates of the pattern origin. 9 | * @param angle The rotation of the pattern in radians. 10 | * @param size The diameter of a dot in pixels. 11 | * @author Evan Wallace https://github.com/evanw/glfx.js/blob/master/src/filters/fun/dotscreen.js 12 | * @author loudo 13 | */ 14 | class DotScreen extends GraphicsShader 15 | { 16 | 17 | @:glFragmentSource( 18 | "#pragma header 19 | uniform vec2 center; 20 | uniform float angle; 21 | uniform float scale; 22 | uniform vec2 texSize; 23 | 24 | float pattern() { 25 | float s = sin(angle), c = cos(angle); 26 | vec2 tex = openfl_TextureCoordv * texSize - center; 27 | vec2 point = vec2( 28 | c * tex.x - s * tex.y, 29 | s * tex.x + c * tex.y 30 | ) * scale; 31 | return (sin(point.x) * sin(point.y)) * 4.0; 32 | } 33 | 34 | void main(void) { 35 | 36 | vec4 color = texture2D(bitmap, openfl_TextureCoordv); 37 | float average = (color.r + color.g + color.b) / 3.0; 38 | gl_FragColor = vec4(vec3(average * 10.0 - 5.0 + pattern()), color.a); 39 | 40 | }" 41 | ) 42 | 43 | public function new(size:Int = 1, angle:Float = 0) 44 | { 45 | super(); 46 | data.scale.value = [Math.PI / size]; 47 | data.angle.value = [angle]; 48 | data.center.value = [0,0]; 49 | data.texSize.value = [400,400];//should be Sprite size 50 | } 51 | 52 | } -------------------------------------------------------------------------------- /atlasTriangle/shaders/FilmShader.hx: -------------------------------------------------------------------------------- 1 | package atlasTriangle.shaders; 2 | 3 | import openfl.utils.ByteArray; 4 | 5 | /** 6 | * 7 | * * Film grain & scanlines shader 8 | * 9 | * - ported from HLSL to WebGL / GLSL 10 | * http://www.truevision3d.com/forums/showcase/staticnoise_colorblackwhite_scanline_shaders-t18698.0.html 11 | * 12 | * Screen Space Static Postprocessor 13 | * 14 | * Produces an analogue noise overlay similar to a film grain / TV static 15 | * 16 | * Original implementation and noise algorithm 17 | * Pat 'Hawthorne' Shearon 18 | * 19 | * Optimized scanlines + noise version with intensity scaling 20 | * Georg 'Leviathan' Steinrohder 21 | * 22 | * This version is provided under a Creative Commons Attribution 3.0 License 23 | * http://creativecommons.org/licenses/by/3.0/ 24 | * 25 | * @author alteredq https://github.com/mrdoob/three.js/blob/master/examples/js/shaders/FilmShader.js 26 | * @author adapted by loudo 27 | */ 28 | class FilmShader extends GraphicsShader 29 | { 30 | 31 | @:glFragmentSource( 32 | "#pragma header 33 | // control parameter 34 | uniform float uTime; 35 | 36 | uniform bool grayscale; 37 | 38 | // noise effect intensity value (0 = no effect, 1 = full effect) 39 | uniform float nIntensity; 40 | 41 | // scanlines effect intensity value (0 = no effect, 1 = full effect) 42 | uniform float sIntensity; 43 | 44 | // scanlines effect count value (0 = no effect, 4096 = full effect) 45 | uniform float sCount; 46 | 47 | float rand(vec2 co){ 48 | return fract(sin(dot(co.xy ,vec2(12.9898,78.233))) * 43758.5453); 49 | } 50 | 51 | void main(void) { 52 | 53 | // sample the source 54 | vec4 cTextureScreen = texture2D( bitmap, openfl_TextureCoordv ); 55 | 56 | // make some noise 57 | float dx = rand( openfl_TextureCoordv + uTime); 58 | 59 | // add noise 60 | vec3 cResult = cTextureScreen.rgb + cTextureScreen.rgb * clamp( 0.1 + dx, 0.0, 1.0 ); 61 | 62 | // get us a sine and cosine 63 | vec2 sc = vec2( sin( openfl_TextureCoordv.y * sCount ), cos( openfl_TextureCoordv.y * sCount ) ); 64 | 65 | // add scanlines 66 | cResult += cTextureScreen.rgb * vec3( sc.x, sc.y, sc.x ) * sIntensity; 67 | 68 | // interpolate between source and result by intensity 69 | cResult = cTextureScreen.rgb + clamp( nIntensity, 0.0,1.0 ) * ( cResult - cTextureScreen.rgb ); 70 | 71 | // convert to grayscale if desired 72 | if( grayscale ) { 73 | 74 | cResult = vec3( cResult.r * 0.3 + cResult.g * 0.59 + cResult.b * 0.11 ); 75 | 76 | } 77 | 78 | gl_FragColor = vec4( cResult, cTextureScreen.a ); 79 | 80 | }" 81 | 82 | ) 83 | 84 | public function new() 85 | { 86 | super(); 87 | 88 | data.uTime.value = [0]; 89 | data.nIntensity.value = [0.5]; 90 | data.sIntensity.value = [0.05]; 91 | data.sCount.value = [4096]; 92 | data.grayscale.value = [0]; 93 | 94 | } 95 | 96 | } -------------------------------------------------------------------------------- /atlasTriangle/shaders/GraphicsShader.hx: -------------------------------------------------------------------------------- 1 | package atlasTriangle.shaders; 2 | 3 | import openfl.utils.ByteArray; 4 | import openfl.display.Shader; 5 | 6 | #if !openfl_debug 7 | @:fileXml('tags="haxe,release"') 8 | @:noDebug 9 | #end 10 | /** 11 | * cloned from openfl.display.GraphicsShader 12 | * This cloned shader allow us more possibilities (attributes are not overwritten) 13 | * changed: name of attributes from openfl_ prefix to triangle_ 14 | * @author jgranick 15 | */ 16 | class GraphicsShader extends openfl.display.GraphicsShader 17 | { 18 | 19 | @:glVertexSource(" 20 | 21 | attribute float triangle_Alpha; 22 | attribute vec4 triangle_ColorMultiplier; 23 | attribute vec4 triangle_ColorOffset; 24 | attribute vec4 openfl_Position; 25 | attribute vec2 openfl_TextureCoord; 26 | 27 | varying float openfl_Alphav; 28 | varying vec4 openfl_ColorMultiplierv; 29 | varying vec4 openfl_ColorOffsetv; 30 | varying vec2 openfl_TextureCoordv; 31 | 32 | uniform mat4 openfl_Matrix; 33 | uniform bool triangle_HasColorTransform; 34 | uniform vec2 openfl_TextureSize; 35 | 36 | void main(void) { 37 | 38 | openfl_Alphav = triangle_Alpha; 39 | openfl_TextureCoordv = openfl_TextureCoord; 40 | 41 | if (triangle_HasColorTransform) { 42 | 43 | openfl_ColorMultiplierv = triangle_ColorMultiplier; 44 | openfl_ColorOffsetv = triangle_ColorOffset / 255.0; 45 | 46 | } 47 | 48 | gl_Position = openfl_Matrix * openfl_Position; 49 | 50 | }") 51 | 52 | 53 | @:glFragmentSource(" 54 | 55 | varying float openfl_Alphav; 56 | varying vec4 openfl_ColorMultiplierv; 57 | varying vec4 openfl_ColorOffsetv; 58 | varying vec2 openfl_TextureCoordv; 59 | 60 | uniform bool triangle_HasColorTransform; 61 | uniform vec2 openfl_TextureSize; 62 | uniform sampler2D bitmap; 63 | 64 | void main(void) { 65 | 66 | vec4 color = texture2D (bitmap, openfl_TextureCoordv); 67 | 68 | if (color.a == 0.0) { 69 | 70 | gl_FragColor = vec4 (0.0, 0.0, 0.0, 0.0); 71 | 72 | } else if (triangle_HasColorTransform) { 73 | 74 | color = vec4 (color.rgb / color.a, color.a); 75 | 76 | mat4 colorMultiplier = mat4 (0); 77 | colorMultiplier[0][0] = openfl_ColorMultiplierv.x; 78 | colorMultiplier[1][1] = openfl_ColorMultiplierv.y; 79 | colorMultiplier[2][2] = openfl_ColorMultiplierv.z; 80 | colorMultiplier[3][3] = openfl_ColorMultiplierv.w; 81 | 82 | color = clamp (openfl_ColorOffsetv + (color * colorMultiplier), 0.0, 1.0); 83 | 84 | if (color.a > 0.0) { 85 | 86 | gl_FragColor = vec4 (color.rgb * color.a * openfl_Alphav, color.a * openfl_Alphav); 87 | 88 | } else { 89 | 90 | gl_FragColor = vec4 (0.0, 0.0, 0.0, 0.0); 91 | 92 | } 93 | 94 | } else { 95 | 96 | gl_FragColor = color * openfl_Alphav; 97 | 98 | } 99 | 100 | }" 101 | ) 102 | public function new(code:ByteArray = null) 103 | { 104 | super(code); 105 | } 106 | } 107 | -------------------------------------------------------------------------------- /atlasTriangle/shaders/GrayScale.hx: -------------------------------------------------------------------------------- 1 | package atlasTriangle.shaders; 2 | 3 | import atlasTriangle.shaders.GraphicsShader; 4 | 5 | /** 6 | * ... 7 | * @author Loudo 8 | */ 9 | class GrayScale extends GraphicsShader 10 | { 11 | 12 | @:glFragmentSource( 13 | "#pragma header 14 | 15 | void main(void) { 16 | 17 | gl_FragColor = texture2D(bitmap, openfl_TextureCoordv); 18 | float sum = (gl_FragColor.r + gl_FragColor.g + gl_FragColor.b) / 3.0; 19 | gl_FragColor = vec4(sum, sum, sum, gl_FragColor.a); 20 | gl_FragColor = gl_FragColor * openfl_Alphav; 21 | 22 | }" 23 | ) 24 | 25 | public function new() 26 | { 27 | super(); 28 | } 29 | } -------------------------------------------------------------------------------- /atlasTriangle/shaders/Hexagonate.hx: -------------------------------------------------------------------------------- 1 | package atlasTriangle.shaders; 2 | 3 | import openfl.utils.ByteArray; 4 | 5 | /** 6 | * Renders the image using a pattern of hexagonal tiles. 7 | * Tile colors are nearest-neighbor sampled from the centers of the tiles. 8 | * 9 | * @param center The x and y coordinates of the pattern center. 10 | * @param scale The width of an individual tile, in pixels. 11 | * 12 | * @author Evan Wallace https://github.com/evanw/glfx.js/blob/master/src/filters/fun/hexagonalpixelate.js 13 | * @author adapted by loudo 14 | */ 15 | class Hexagonate extends GraphicsShader 16 | { 17 | 18 | @:glFragmentSource( 19 | "#pragma header 20 | uniform vec2 center; 21 | uniform vec2 texSize; 22 | uniform float scale; 23 | 24 | 25 | void main(void) { 26 | 27 | vec2 tex = (openfl_TextureCoordv * texSize - center) / scale; 28 | tex.y /= 0.866025404; 29 | tex.x -= tex.y * 0.5; 30 | 31 | 32 | vec2 a; 33 | if (tex.x + tex.y - floor(tex.x) - floor(tex.y) < 1.0) a = vec2(floor(tex.x), floor(tex.y)); 34 | else a = vec2(ceil(tex.x), ceil(tex.y)); 35 | vec2 b = vec2(ceil(tex.x), floor(tex.y)); 36 | vec2 c = vec2(floor(tex.x), ceil(tex.y)); 37 | 38 | vec3 TEX = vec3(tex.x, tex.y, 1.0 - tex.x - tex.y); 39 | vec3 A = vec3(a.x, a.y, 1.0 - a.x - a.y); 40 | vec3 B = vec3(b.x, b.y, 1.0 - b.x - b.y); 41 | vec3 C = vec3(c.x, c.y, 1.0 - c.x - c.y); 42 | 43 | float alen = length(TEX - A); 44 | float blen = length(TEX - B); 45 | float clen = length(TEX - C); 46 | 47 | vec2 choice; 48 | if (alen < blen) { 49 | if (alen < clen) choice = a; 50 | else choice = c; 51 | } else { 52 | if (blen < clen) choice = b; 53 | else choice = c; 54 | } 55 | 56 | choice.x += choice.y * 0.5; 57 | choice.y *= 0.866025404; 58 | choice *= scale / texSize; 59 | gl_FragColor = texture2D(bitmap, choice + center / texSize); 60 | }" 61 | ) 62 | 63 | 64 | public function new(scale:Float = 0.15, center:Int = 0 ) 65 | { 66 | super(); 67 | 68 | data.scale.value = [scale]; 69 | data.center.value = [center, center]; 70 | data.texSize.value = [100,100];//should be Sprite size 71 | } 72 | 73 | } -------------------------------------------------------------------------------- /atlasTriangle/shaders/HueSaturationShader.hx: -------------------------------------------------------------------------------- 1 | package atlasTriangle.shaders; 2 | 3 | /** 4 | * 5 | * @author tapio / http://tapio.github.com/ 6 | * 7 | * Hue and saturation adjustment 8 | * https://github.com/evanw/glfx.js 9 | * hue: -1 to 1 (-1 is 180 degrees in the negative direction, 0 is no change, etc. 10 | * saturation: -1 to 1 (-1 is solid gray, 0 is no change, and 1 is maximum contrast) 11 | * adapted by @loudo 12 | */ 13 | class HueSaturationShader extends GraphicsShader { 14 | 15 | @:glFragmentSource( 16 | "#pragma header 17 | uniform float hue; 18 | uniform float saturation; 19 | 20 | void main(void) { 21 | 22 | gl_FragColor = texture2D( bitmap, openfl_TextureCoordv ); 23 | 24 | // hue 25 | float angle = hue * 3.14159265; 26 | float s = sin(angle), c = cos(angle); 27 | vec3 weights = (vec3(2.0 * c, -sqrt(3.0) * s - c, sqrt(3.0) * s - c) + 1.0) / 3.0; 28 | float len = length(gl_FragColor.rgb); 29 | gl_FragColor.rgb = vec3( 30 | dot(gl_FragColor.rgb, weights.xyz), 31 | dot(gl_FragColor.rgb, weights.zxy), 32 | dot(gl_FragColor.rgb, weights.yzx) 33 | ); 34 | 35 | // saturation 36 | float average = (gl_FragColor.r + gl_FragColor.g + gl_FragColor.b) / 3.0; 37 | if (saturation > 0.0) { 38 | gl_FragColor.rgb += (average - gl_FragColor.rgb) * (1.0 - 1.0 / (1.001 - saturation)); 39 | } else { 40 | gl_FragColor.rgb += (average - gl_FragColor.rgb) * (-saturation); 41 | } 42 | 43 | gl_FragColor = gl_FragColor * openfl_Alphav; 44 | 45 | }" 46 | ) 47 | 48 | public function new() 49 | { 50 | super(); 51 | 52 | 53 | //data.hue.value = [0]; 54 | //data.saturation.value = [0]; 55 | } 56 | 57 | } 58 | 59 | 60 | 61 | -------------------------------------------------------------------------------- /atlasTriangle/shaders/Invert.hx: -------------------------------------------------------------------------------- 1 | package atlasTriangle.shaders; 2 | 3 | import atlasTriangle.shaders.GraphicsShader; 4 | 5 | /** 6 | * ... 7 | * @author MrCdK 8 | * @author adapted by Loudo 9 | */ 10 | class Invert extends GraphicsShader 11 | { 12 | 13 | @:glFragmentSource( 14 | "#pragma header 15 | 16 | void main(void) { 17 | 18 | gl_FragColor = texture2D(bitmap, openfl_TextureCoordv); 19 | gl_FragColor = vec4(1.0 - gl_FragColor.r, 1.0 - gl_FragColor.g, 1.0 - gl_FragColor.b, gl_FragColor.a); 20 | gl_FragColor = gl_FragColor * openfl_Alphav; 21 | 22 | }" 23 | ) 24 | 25 | public function new() 26 | { 27 | super(); 28 | } 29 | } -------------------------------------------------------------------------------- /atlasTriangle/shaders/Pixelated.hx: -------------------------------------------------------------------------------- 1 | package atlasTriangle.shaders; 2 | 3 | import atlasTriangle.shaders.GraphicsShader; 4 | 5 | /** 6 | * @author Agnius Vasiliauskas 7 | * @author adapted by Loudo 8 | */ 9 | class Pixelated extends GraphicsShader 10 | { 11 | 12 | @:glFragmentSource( 13 | "#pragma header 14 | uniform float pixelization; 15 | 16 | void main(void) { 17 | 18 | float dx = 15.*(1. / pixelization); 19 | float dy = 10.*(1. / pixelization); 20 | 21 | vec2 coord = vec2(dx*floor(openfl_TextureCoordv.x/dx), 22 | dy * floor(openfl_TextureCoordv.y / dy)); 23 | 24 | gl_FragColor = texture2D(bitmap, coord); 25 | gl_FragColor = gl_FragColor * openfl_Alphav; 26 | 27 | }" 28 | ) 29 | 30 | public function new(pixelization:Float = 8192.0) 31 | { 32 | super(); 33 | data.pixelization.value = [pixelization];//higher is smaller pixels 34 | } 35 | } -------------------------------------------------------------------------------- /atlasTriangle/shaders/Technicolor.hx: -------------------------------------------------------------------------------- 1 | package atlasTriangle.shaders; 2 | import atlasTriangle.shaders.GraphicsShader; 3 | /** 4 | * Technicolor Shader 5 | * Simulates the look of the two-strip technicolor process popular in early 20th century films. 6 | * More historical info here: http://www.widescreenmuseum.com/oldcolor/technicolor1.htm 7 | * Demo here: http://charliehoey.com/technicolor_shader/shader_test.html 8 | * @author flimshaw 9 | * @author adapted by loudo 10 | */ 11 | class Technicolor extends GraphicsShader 12 | { 13 | 14 | @:glFragmentSource( 15 | "#pragma header 16 | 17 | void main(void) { 18 | 19 | gl_FragColor = texture2D(bitmap, openfl_TextureCoordv); 20 | gl_FragColor = vec4(gl_FragColor.r, (gl_FragColor.g + gl_FragColor.b) * .5, (gl_FragColor.g + gl_FragColor.b) * .5, gl_FragColor.a); 21 | gl_FragColor = gl_FragColor * openfl_Alphav; 22 | 23 | }" 24 | ) 25 | 26 | public function new() 27 | { 28 | super(); 29 | } 30 | 31 | } -------------------------------------------------------------------------------- /haxelib.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "atlas-triangle", 3 | "url": "https://github.com/loudoweb/AtlasTriangle", 4 | "license": "MIT", 5 | "tags": ["openfl", "game", "parser"], 6 | "description": "Parse atlases packed with triangles with such tools as SpriteUV2 or TexturePacker for Haxe language. Basic openfl renderer included.", 7 | "version": "0.2.0", 8 | "releasenote": "Initial project.", 9 | "contributors": [ "loudoweb" ] 10 | } --------------------------------------------------------------------------------