I created a GLSL library that allows OpenFL to be written directly in Haxe.
Github: https://github.com/rainyt/openfl-glsl
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();
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;
}
USE:
trace(Haxe2GLSL.fragmentSource);
trace(Haxe2GLSL.vertexSource);
GLSL:
#define TEXT 1
void main(void){
gl_FragColor=vec4(float(TEXT),1.,0.,1.);
}
void main(void){
gl_Position=vec4(1.,1.,1.,1.);
}