Sdg icon indicating copy to clipboard operation
Sdg copied to clipboard

Shaders

Open RafaelOliveira opened this issue 8 years ago • 3 comments

See a way to integrate shaders.

RafaelOliveira avatar Jan 03 '17 14:01 RafaelOliveira

just to ask. you the shadertool class. how do you use it properly. or is this not being worked on?

lewislepton avatar Apr 27 '17 11:04 lewislepton

The ShaderTool class is being used in the shader filters. It is class to create custom shaders to use with kha.Graphics2. The class is very portable, there is nothing specific to Sdg there. With Graphics2 you can use three types of shaders, one shader for images, other for color(used in shapes), and other for fonts (there is also another for video). You can see here:
https://github.com/Kode/Kha/tree/master/Sources/Shaders (the painter-*.frag.glsl shaders)
So in ShaderTool.createPipeline() you pass one of the these 3 constants:

ShaderTool.IMAGE_SHADER
ShaderTool.COLOR_SHADER
ShaderTool.FONT_SHADER

And the custom fragment shader. The class then create the correct pipeline for you, with the parameters that are used in Kha. And then you create the code for the parameters used in your shader. I just copied from the Kha source how the shaders are constructed in Graphics2. You will also need to create a fragment shader with the same parameters that are in the original shaders (the painter-*.frag.glsl files). With the pipeline created you apply this way:
g2.pipeline = pipeline;

// apply the parameters of the shader in g4 ...

g2.drawImage(image, 0, 0);
g2.pipeline = null;
There is a example here: https://github.com/RafaelOliveira/Sdg/blob/master/Sources/sdg/filters/DotScreen.hx

To use this class in Sdg, you create a pipeline with the class, then, in a class extended from Object, you do this:

override public function render(canvas:Canvas, cameraX:Float, cameraY:Float):Void 
{
	canvas.g2.pipeline = pipeline;

	// the parameters of the shader
	canvas.g4.setVector2(resolutionID, resolution);
	canvas.g4.setFloat(angleID, angle);
	canvas.g4.setFloat(scaleID, scale);

	super.render(canvas, cameraX, cameraY);
	canvas.g2.pipeline = null;
}

the pipeline is applied before the drawing.

I will write about this in the wiki (with a more complete code), and also a documentation about how to use Sdg in general.

RafaelOliveira avatar Apr 28 '17 02:04 RafaelOliveira

cool spot. thanks man. thought of less shader code in something thru class interestes me. we were talking about the marcos that deal with things like things from khage. but that felt to much over my head. need to learn macros ;)

lewislepton avatar Apr 28 '17 08:04 lewislepton