Help: Tile Shader error

Hello everyone, I used a tilemap as the display object, which placed a 7x7 tile object. I placed a tile container with a character image and set a custom shader for it.

Shader code:

package zygame.shader;

import openfl.display.DisplayObjectShader;

/**
 * 灰度着色器
 */
class GeryShader extends DisplayObjectShader {

    @:glFragmentSource("
		
		#pragma header
				
		void main(void) {
			
			#pragma body
			float mColor = 0.0;
			mColor += gl_FragColor.r + gl_FragColor.g + gl_FragColor.b;
			mColor = mColor/3.0;
			gl_FragColor.r = mColor;
			gl_FragColor.g = mColor;
			gl_FragColor.b = mColor;
			gl_FragColor *= openfl_Alphav;
			
		}
		
	")
    public function new(){
        super();
    }

}

This is not the result I want, the rendering has gone wrong.

I want to use a shader for a tile container with images, not all, and at the same time more than half of the rendering is not rendered correctly.

versions:
openfl8.7.0
lime7.2.0

Openfl sample:

package;

import openfl.display.Sprite;
import openfl.display.Tilemap;
import openfl.display.Tileset;
import openfl.utils.Assets;
import openfl.geom.Rectangle;
import openfl.display.Tile;
import openfl.display.DisplayObjectShader;

class Main extends Sprite
{
	public function new()
	{
		super();

		var tileset:Tileset = new Tileset(Assets.getBitmapData("assets/BaseUI.png"));
		tileset.addRect(new Rectangle(0, 0, 172, 172));
		var tilemap:Tilemap = new Tilemap(600, 600, tileset);
		this.addChild(tilemap);

		for (ix in 0...7)
		{
			for (iy in 0...8)
			{
				var tile:Tile = new Tile(0);
				tilemap.addTile(tile);
				tile.scaleX = 50 / tile.getBounds(null).width;
				tile.scaleY = 50 / tile.getBounds(null).height;
				tile.x = ix * 52;
				tile.y = iy * 52;

				if (ix == 3 && iy == 6)
				{
					tile.shader = new GeryShader();
				}
			}
		}
	}
}

/**
 * 灰度着色器
 */
class GeryShader extends DisplayObjectShader
{
	@:glFragmentSource("
		#pragma header

		void main(void) {

			#pragma body

			float mColor = 0.0;
			mColor += gl_FragColor.r + gl_FragColor.g + gl_FragColor.b;
			mColor = mColor/3.0;
			
			gl_FragColor.r = mColor;
			gl_FragColor.g = mColor;
			gl_FragColor.b = mColor;
			gl_FragColor *= openfl_Alphav;
		}
	")
	public function new()
	{
		super();
	}
}

Ask for help, thank you :disappointed_relieved:

Do you think it would be possible to try a dev version of OpenFL from GIT? This issue may be resolved already – it would be helpful to know!

Thank you for your reply, I downloaded the development version of openfl and then tested it.

It is now fully rendered, but I only added a shader to a tile, It affects the tiles that were added before.

Here’s the fix:

1 Like

Great, this solved the problem, thank you!

%E4%BC%81%E4%B8%9A%E5%BE%AE%E4%BF%A1%E6%88%AA%E5%9B%BE_58867c34-8840-43f6-9de5-415bb0ad0615 企业微信截图_58867c34-8840-43f6-9de5-415bb0ad0615.png

1 Like