Use Haxe to write GLSL in OpenFL

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

Note

-Currently, only GLSL writing support of :glFragmentSource is implemented.
-Need to rely on vector-math

5 Likes

Now haxelib:https://lib.haxe.org/p/openfl-glsl/

1 Like

Samples: https://github.com/rainyt/openfl-glsl-samples
There are not too many examples, because I am in the process of learning GLSL and hope to fill it up quickly!

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;
}

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

}

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.


Now, just add <define name="output" value="./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.

A nine-square grid rendering shader is implemented using openfl-glsl.

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.
1 Like