Save
Saving
  • P
    pataxu

    Thanks a lot, I got it!

    Here's some of my incomplete badly written and unoptimized code in case anyone else needs it:

    class ImageObject extends Entity {
    	public var framebuffers:Array<Framebuffer>;
    	public var image:Image;
    	var textureID:TextureUnit;
    	var maskID:TextureUnit;
    	public var mask:Image;
    
    	public function new(image:Image,x:Float, y:Float, width:Int, height: Int){
    	super(x, y, width, height);
    
    	textureID = Project.pipeline.getTextureUnit("tex");
    	this.image = image;
    
    	maskID = Project.pipeline.getTextureUnit("mask");
    	mask = Assets.images.armopacity;
    
    	}
    
    
    public function update(){
    
    }
    
    public function render(graphics:Graphics,frames:Array<Framebuffer>){
    		var g4 = frames[0].g4;
    		var g2 = graphics;
    		g4.begin();
    		graphics.pipeline = Project.pipeline;
    			g4.setTexture(textureID, image);
    			g4.setTexture(maskID, mask);
    		g4.end();
    		
    		g2.begin(true, Color.Green);
    		
    		graphics.drawScaledImage(image,x - width / 2, y,width * 1.1, height * 1.1);
    
    		g2.end();
    
    		
    
    }
    
    }
    

    I don't even know if I did the rendering correctly, I've only done trial and error until it worked.

    Shader:

    #version 450
    
    uniform sampler2D tex;
    uniform sampler2D mask;
    in vec2 texCoord;
    in vec4 color;
    
    
    out vec4 FragColor;
    
    
    void main() {
    	vec4 texcolor = texture(tex, texCoord) * color;
    	vec4 maskcolor = texture(mask, texCoord) * color;
    	texcolor.rgb *= color.a;
    	maskcolor.rgb *= color.a;
    	if(maskcolor.rgb == vec3(0.0,0.0,0.0))
        {
            discard;
        }
        FragColor = texcolor;
    }
    

    The shader gets the alpha channel black pixels and delete them (I don't need alpha blending though).

    And here's the final result.

    3f0c314c-4308-4a76-85d9-a84433221c2b-image.png

    But there's a line on the top of the image. I've checked the opacity image and it does have all the black pixels actually black, rgb(0,0,0) and the white pixels are fully white, so the line isn't caused by the image. Also, both diffuse and opacity are the same size. And nope, it isn't also caused by "graphics.drawScaledImage" width and height, no matter the in-code image size the line still appear.
    d73448c7-bb17-4a10-b18b-45d685111042-image.png

    But I think I'll solve this out, some day. Anyways, thanks a lot for the help!

    posted in Kode read more
  • P
    pataxu

    Can you make me a quick example please? I can't find any online documentation about how it works, no more than
    "pipeline.getTextureUnit();, you have put a string between the parenthesis"

    posted in Kode read more
  • P
    pataxu

    I beg your pardon, may I request your help again? (I know, it's been a while since I posted this, I didn't know how to do it and I've been busy lately but today I gave it a shot and finally I've figured some basic stuff out.)

    I'm stuck on the second texture step.

    Here's my ImageObject.hx (The whole pipeline set-up is in Project.hx but I don't think it's necessary to show it)

    package;
    
    import kha.Assets;
    import kha.graphics2.Graphics;
    import kha.Color;
    import kha.Image;
    
    import Entity;
    
    
    
    class ImageObject extends Entity{
    	public var image:Image;
    	//public var mask:Image; ?
    	public function new(image:Image,x:Float, y:Float, width:Int, height: Int){
    	this.image = image;
    	super(x, y, width, height);
    }
    
    public function update(){
    
    }
    
    public function render(graphics:Graphics){
    
    		//graphics.color = Color.Green;
    		graphics.pipeline = Project.pipeline;
    		//Dibujar imagen!!! Se divide X entre 2 para centrarlo con el raton, al igual que con Y
    		graphics.drawScaledImage(image,x - width / 2, y,width * 1.1, height * 1.1);
    
    		
    
    }
    
    }
    

    And the fragment shader:

    #version 450
    
    uniform sampler2D tex;
    uniform sampler2D mask;
    in vec2 texCoord;
    in vec4 color;
    
    
    out vec4 FragColor;
    
    void main() {
    	vec4 texcolor = texture(tex, texCoord) * color;
    	texcolor.rgb *= color.a;
    	texcolor.rgb = texcolor.bgr; // swap color channels
    	if(texture(mask, texCoord).a != 1.0f)
        {
            discard;
        }
       FragColor = texture(tex, texCoord);
    }
    

    Ok, I have Image, which is my diffuse texture, but now I'm stuck doing the mask. What do I have to do to make the shader recognize a 2nd texture and set is as "uniform sampler2D mask;"?
    PD: The shader itself is working (Colors channels do get swapped)

    posted in Kode read more
  • P
    pataxu

    arm.jpg
    (Quick example image I just made)

    I'm making a simple android game with kha and I need to apply an opacity mask, so I just have 1 opacity texture and I reuse it across a lot of diffuse textures instead of making a lot of .png files with the opacity included them. I want to know if this is possible, and how to do it. Thanks.

    posted in Kode read more