Allow Haxe to write GLSL code directly in OpenFL and use it directly in OpenFL.
Test.hx
@:debug
class Test extends glsl.OpenFLShader {
public function new() {
super();
}
override function fragment() {
super.fragment();
var v:Vec2 = this.gl_openfl_TextureCoordv * 0.5;
this.gl_FragColor = texture2D(gl_openfl_Texture, v);
}
}
Test.hx to GLSL
Finally, the fragment method of Test.hx will be compiled into GLSL and can be used directly by OpenFL.
#pragma header
void main(void){
#pragma body
vec2 v=openfl_TextureCoordv*0.5;
gl_FragColor=texture2D(openfl_Texture,v);
}
Use:
var bitmap = new Bitmap();
bitmap.shader = new Test();
Note
-Currently, only GLSL writing support of :glFragmentSource is implemented.
-Need to rely on vector-math
openfl-glsl v0.0.3 Relesase:
-1. New: Added GLSL parameter access of OpenFL.
-2. Added support for vertex vertex shader:glVertexSource.
-3. Added :varying support to define varying variables.
-4. Added :define independent support for vertex and fragment.
-5. Added support for setFrameEvent to directly start the onFrame method.
-6. Added support for parsing Array<T>.
Now, you can use Haxe to directly convert to GLSL, you can use it without OpenFL, and then define your own variables.
Haxe:
package glsl;
import glsl.OpenFLShader.float;
import VectorMath.vec4;
/**
* Use Haxe to convert to GLSL, access through fragmentSource and vertexSource
*/
@:debug
class Haxe2GLSL extends BaseGLSL {
@:define("TEXT 1")
public function fragment():Void {
this.gl_FragColor = vec4(float(TEXT), 1., 0., 1.);
}
public function vertex():Void {
this.gl_Position = vec4(1, 1, 1, 1);
}
}
@:autoBuild(glsl.macro.GLSLCompileMacro.build("glsl"))
class BaseGLSL {
public var gl_FragColor:Vec4;
public var gl_Position:Vec4;
}
Today, openfl-glsl v0.0.4 Relesase:
-1. Improvement: decouple GLSLCompileMacro from OpenFL functions, allowing separate use of classes.
-2. New: @:autoBuild(glsl.macro.GLSLCompileMacro.build("glsl")) is used to support Haxe parsing to GLSL.
-3. New: @:attribute creates attribute variables.
Supplement: Supplement to operators of EUnop and Binop.
New: Newly added subclasses inherit the variables of the parent class. For example, variables such as :glsl/:uniform created in the parent class will be inherited by the subclass.
Fix: Fix elseif statement parsing.
Fix: Fix == statement parsing.
New: Added GLSL syntax formatting.
New: Added support for Defineoutput, which can define an output directory. After compilation, all GLSL will be compiled into the directory.
Improvement: The redundant blank lines are automatically removed.
Fix: Fix the problem that the array variable reference access error.
Improvement: Modify the GLSL method and the sequence of variables.
At the same time, I added a lot of interesting examples of GLSL in openfl-glsl-samples: text stroke, circle mask, rounded corner rendering, nine-square grid rendering, etc.