Use Haxe to GLSL

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.);

}
8 Likes

Now, just add <define name="output" value="./bin"/>or-D output=./bin to output all GLSL to the bin directory.

Start:

haxe haxebuild.hxml

After running, a bin directory will be generated, which contains GLSL files

haxelib Update to 0.0.5:

  1. Supplement: Supplement to operators of EUnop and Binop.
  2. 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.
  3. Fix: Fix elseif statement parsing.
  4. Fix: Fix == statement parsing.
  5. New: Added GLSL syntax formatting.
  6. New: Added support for Defineoutput, which can define an output directory. After compilation, all GLSL will be compiled into the directory.
  7. New: Added syntax support for (conf)?else:else.
1 Like

haxelib Update to 0.0.6

  1. Added: Added break grammar recognition.
  2. Improvement: Improve the generation of ;.
  3. Improvement: The redundant blank lines are automatically removed.
  4. Fix: Fix the problem that the array variable reference access error.
  5. 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.

1 Like

haxelib Update to 0.0.9:

  1. Improvement: Improve the formatting of GLSL to make GLSL look more beautiful.
  2. Improvement: Added support for else if.
  3. Improvement: Use /t instead of spaces.
  4. New: Added support for OpenFLGraphicsShader, which can be used in the beginShaderFill of OpenFL.
  5. Improvement: If super.fragment() and super.vertex() are not implemented, #pragma body can be removed.
  6. Fix: Fix the wrong definition of @:attribute in OpenFlShader. When accessing @:attribute, you need to use a_variable name for access.
2 Likes