Skip to content

Extend custom class#966

Open
HEIHUAa wants to merge 1 commit intoCodenameCrew:mainfrom
HEIHUAa:Extend-custom-class
Open

Extend custom class#966
HEIHUAa wants to merge 1 commit intoCodenameCrew:mainfrom
HEIHUAa:Extend-custom-class

Conversation

@HEIHUAa
Copy link
Copy Markdown
Contributor

@HEIHUAa HEIHUAa commented Apr 24, 2026

Make BitmapFilter overridable by scripts (already verified to work)

Previously, I thought writing extends BitmapFilter in a script was invalid, but after extensive testing, it turned out to be a viable approach.

Below is a code example of BitmapFilter:

BloomFilter.hx

import funkin.backend.shaders.FunkinShader;
import openfl.filters.BitmapFilter;
import openfl.display.BitmapData;
import openfl.display.DisplayObjectRenderer;
import openfl.display.Shader;
import openfl.geom.Point;
import openfl.geom.Rectangle;

class BloomFilter extends BitmapFilter {
	static var blurShader_reduce:FunkinShader = new FunkinShader("...");

	static var blurShader_amplification:FunkinShader = new FunkinShader("...");

	static var blurShader_null:FunkinShader = new FunkinShader("...");

	var textureScale:Float = 0;

	public function new(?scale:Float) {
		super();

		scale ??= 0;
		textureScale = scale;

		__needSecondBitmapData = true;
		__preserveObject = false;
		__numShaderPasses = 2;
		__renderDirty = true;

		if (scale > 0) {
			blurShader_reduce.textureScale = scale;
			blurShader_amplification.textureScale = scale;
		}
	}

	public override function clone():BitmapFilter {
		// var newFilter = new BloomFilter(textureScale);
		return this.superClass;
	}

	override function __applyFilter(bitmapData:BitmapData, sourceBitmapData:BitmapData, sourceRect:Rectangle, destPoint:Point):BitmapData {
		return sourceBitmapData;
	}

	override function __initShader(renderer:DisplayObjectRenderer, pass:Int, sourceBitmapData:BitmapData):Shader {
		if (textureScale <= 0) return blurShader_null;
		
		if (pass == 0) {
			return blurShader_reduce;
		} else {
			return blurShader_amplification;
		}
	}
}

1.hx

import BloomFilter;

function postCreate() {
	var bloom = new BloomFilter(100);
	var realFilter = bloom.superClass;
	FlxG.camera.setFilters([realFilter]);
}

@ItsLJcool
Copy link
Copy Markdown
Contributor

seems hacky imo
Why not just use the ScriptableShader instead of stalling the rendering pipeline with HScript

@HEIHUAa
Copy link
Copy Markdown
Contributor Author

HEIHUAa commented Apr 24, 2026

seems hacky imo Why not just use the ScriptableShader instead of stalling the rendering pipeline with HScript

If ScriptableShader doesn’t have a way to call the sourceBitmapData function, I wouldn’t have used BitmapFilter at all.

@HEIHUAa
Copy link
Copy Markdown
Contributor Author

HEIHUAa commented Apr 24, 2026

seems hacky imo Why not just use the ScriptableShader instead of stalling the rendering pipeline with HScript

Or do you know how to read sourceBitmapData using ScriptableShader?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants