1
0
Fork 0
mirror of https://github.com/ninjamuffin99/Funkin.git synced 2024-11-14 19:05:15 +00:00
Funkin/source/funkin/graphics/FunkinCamera.hx

262 lines
7.7 KiB
Haxe
Raw Normal View History

2024-02-13 06:38:11 +00:00
package funkin.graphics;
2023-09-23 18:59:24 +00:00
2023-12-11 17:23:33 +00:00
import flash.geom.ColorTransform;
2023-09-23 18:59:24 +00:00
import flixel.FlxCamera;
import flixel.graphics.FlxGraphic;
import flixel.graphics.frames.FlxFrame;
import flixel.math.FlxMatrix;
import flixel.math.FlxRect;
2023-12-11 17:23:33 +00:00
import flixel.system.FlxAssets.FlxShader;
2023-12-19 01:49:01 +00:00
import funkin.graphics.shaders.RuntimeCustomBlendShader;
2024-02-13 06:38:11 +00:00
import funkin.graphics.framebuffer.BitmapDataUtil;
import funkin.graphics.framebuffer.FixedBitmapData;
2023-09-23 18:59:24 +00:00
import openfl.Lib;
import openfl.display.BitmapData;
2023-12-11 17:23:33 +00:00
import openfl.display.BlendMode;
2023-09-23 18:59:24 +00:00
import openfl.display3D.textures.TextureBase;
2023-12-11 17:23:33 +00:00
import openfl.filters.BitmapFilter;
import openfl.filters.ShaderFilter;
2023-09-23 18:59:24 +00:00
/**
2024-02-13 06:38:11 +00:00
* A FlxCamera with additional powerful features:
* - Grab the camera screen as a `BitmapData` and use it as a texture
* - Support `sprite.blend = DARKEN/HARDLIGHT/LIGHTEN/OVERLAY` to apply visual effects using certain sprites
* - NOTE: Several other blend modes work without FunkinCamera. Some still do not work.
* - NOTE: Framerate-independent camera tweening is fixed in Flixel 6.x. Rest in peace, SwagCamera.
2023-09-23 18:59:24 +00:00
*/
@:access(openfl.display.DisplayObject)
@:access(openfl.display.BitmapData)
@:access(openfl.display3D.Context3D)
@:access(openfl.display3D.textures.TextureBase)
@:access(flixel.graphics.FlxGraphic)
@:access(flixel.graphics.frames.FlxFrame)
2024-02-13 06:38:11 +00:00
class FunkinCamera extends FlxCamera
2023-09-23 18:59:24 +00:00
{
final grabbed:Array<BitmapData> = [];
final texturePool:Array<TextureBase> = [];
final bgTexture:TextureBase;
final bgBitmap:BitmapData;
final bgFrame:FlxFrame;
2023-12-11 17:23:33 +00:00
final customBlendShader:RuntimeCustomBlendShader;
final customBlendFilter:ShaderFilter;
var filtersApplied:Bool = false;
var bgItemCount:Int = 0;
2024-02-13 06:38:11 +00:00
public var shouldDraw:Bool = true;
// Used to identify the camera during debugging.
final id:String = 'unknown';
public function new(id:String = 'unknown', x:Int = 0, y:Int = 0, width:Int = 0, height:Int = 0, zoom:Float = 0)
2023-09-23 18:59:24 +00:00
{
super(x, y, width, height, zoom);
this.id = id;
2023-09-23 18:59:24 +00:00
bgTexture = pickTexture(width, height);
bgBitmap = FixedBitmapData.fromTexture(bgTexture);
bgFrame = new FlxFrame(new FlxGraphic('', null));
bgFrame.parent.bitmap = bgBitmap;
bgFrame.frame = new FlxRect();
2023-12-11 17:23:33 +00:00
customBlendShader = new RuntimeCustomBlendShader();
customBlendFilter = new ShaderFilter(customBlendShader);
2023-09-23 18:59:24 +00:00
}
/**
* Grabs the camera screen and returns it as a `BitmapData`. The returned bitmap
2023-12-11 17:23:33 +00:00
* will not be referred by the camera so, changing it will not affect the scene.
* The returned bitmap **will be reused in the next frame**, so the content is available
* only in the current frame.
* @param applyFilters if this is `true`, the camera's filters will be applied to the grabbed bitmap,
* and the camera's filters will be disabled until the beginning of the next frame
* @param isolate if this is `true`, sprites to be rendered will only be rendered to the grabbed bitmap,
* and the grabbed bitmap will not include any previously rendered sprites
2023-09-23 18:59:24 +00:00
* @return the grabbed bitmap data
*/
2023-12-11 17:23:33 +00:00
public function grabScreen(applyFilters:Bool, isolate:Bool = false):BitmapData
2023-09-23 18:59:24 +00:00
{
final texture = pickTexture(width, height);
final bitmap = FixedBitmapData.fromTexture(texture);
2023-12-11 17:23:33 +00:00
squashTo(bitmap, applyFilters, isolate);
grabbed.push(bitmap);
2023-09-23 18:59:24 +00:00
return bitmap;
}
2023-12-11 17:23:33 +00:00
/**
* Applies the filter immediately to the camera. This will be done independently from
* the camera's filters. This method can only be called after the first `grabScreen`
* in the frame.
* @param filter the filter
*/
public function applyFilter(filter:BitmapFilter):Void
{
if (grabbed.length == 0)
{
FlxG.log.error('grab screen before you can apply a filter!');
return;
}
BitmapDataUtil.applyFilter(bgBitmap, filter);
}
2024-02-12 21:49:36 +00:00
function squashTo(bitmap:BitmapData, applyFilters:Bool, isolate:Bool, clearScreen:Bool = false):Void
2023-09-23 18:59:24 +00:00
{
2023-12-11 17:23:33 +00:00
if (applyFilters && isolate)
{
FlxG.log.error('cannot apply filters while isolating!');
}
if (filtersApplied && applyFilters)
{
FlxG.log.warn('filters already applied!');
}
2023-09-23 18:59:24 +00:00
static final matrix = new FlxMatrix();
// resize the background bitmap if needed
if (bgTexture.__width != width || bgTexture.__height != height)
{
2023-12-11 17:23:33 +00:00
BitmapDataUtil.resizeTexture(bgTexture, width, height);
2023-09-23 18:59:24 +00:00
bgBitmap.__resize(width, height);
bgFrame.parent.bitmap = bgBitmap;
}
// grab the bitmap
2023-12-11 17:23:33 +00:00
renderSkipping(isolate ? bgItemCount : 0);
2023-09-23 18:59:24 +00:00
bitmap.fillRect(bitmap.rect, 0);
matrix.setTo(1, 0, 0, 1, flashSprite.x, flashSprite.y);
if (applyFilters)
{
bitmap.draw(flashSprite, matrix);
2023-12-11 17:23:33 +00:00
flashSprite.filters = null;
filtersApplied = true;
2023-09-23 18:59:24 +00:00
}
else
{
final tmp = flashSprite.filters;
flashSprite.filters = null;
bitmap.draw(flashSprite, matrix);
flashSprite.filters = tmp;
}
2023-12-11 17:23:33 +00:00
if (!isolate)
{
// also copy to the background bitmap
bgBitmap.fillRect(bgBitmap.rect, 0);
bgBitmap.draw(bitmap);
}
2023-09-23 18:59:24 +00:00
2024-02-12 21:49:36 +00:00
if (clearScreen)
{
// clear graphics data
2024-02-13 06:38:11 +00:00
super.clearDrawStack();
canvas.graphics.clear();
2024-02-12 21:49:36 +00:00
}
2023-09-23 18:59:24 +00:00
// render the background bitmap
bgFrame.frame.set(0, 0, width, height);
matrix.setTo(viewWidth / width, 0, 0, viewHeight / height, viewMarginLeft, viewMarginTop);
drawPixels(bgFrame, matrix);
2023-12-11 17:23:33 +00:00
// count background draw items for future isolation
bgItemCount = 0;
{
var item = _headOfDrawStack;
while (item != null)
{
item = item.next;
bgItemCount++;
}
}
2023-09-23 18:59:24 +00:00
}
2023-12-11 17:23:33 +00:00
function renderSkipping(count:Int):Void
2023-09-23 18:59:24 +00:00
{
2023-12-11 17:23:33 +00:00
var item = _headOfDrawStack;
while (item != null)
{
if (--count < 0) item.render(this);
item = item.next;
}
}
override function drawPixels(?frame:FlxFrame, ?pixels:BitmapData, matrix:FlxMatrix, ?transform:ColorTransform, ?blend:BlendMode, ?smoothing:Bool = false,
?shader:FlxShader):Void
{
2024-02-13 06:38:11 +00:00
if (!shouldDraw) return;
2023-12-11 17:23:33 +00:00
if ( switch blend
{
case DARKEN | HARDLIGHT | LIGHTEN | OVERLAY: true;
case _: false;
})
{
// squash the screen
grabScreen(false);
// render without blend
super.drawPixels(frame, pixels, matrix, transform, null, smoothing, shader);
// get the isolated bitmap
final isolated = grabScreen(false, true);
// apply fullscreen blend
2024-02-13 08:32:20 +00:00
customBlendShader.blendSwag = blend;
customBlendShader.sourceSwag = isolated;
2023-12-11 17:23:33 +00:00
customBlendShader.updateViewInfo(FlxG.width, FlxG.height, this);
applyFilter(customBlendFilter);
}
else
{
super.drawPixels(frame, pixels, matrix, transform, blend, smoothing, shader);
}
2023-09-23 18:59:24 +00:00
}
override function destroy():Void
{
super.destroy();
disposeTextures();
}
override function clearDrawStack():Void
{
super.clearDrawStack();
// also clear grabbed bitmaps
for (bitmap in grabbed)
{
2023-09-23 19:09:19 +00:00
texturePool.push(bitmap.__texture);
bitmap.dispose(); // this doesn't release the texture
2023-09-23 18:59:24 +00:00
}
grabbed.clear();
2023-12-11 17:23:33 +00:00
// clear filters applied flag
filtersApplied = false;
bgItemCount = 0;
2023-09-23 18:59:24 +00:00
}
function pickTexture(width:Int, height:Int):TextureBase
{
// zero-sized textures will be problematic
width = width < 1 ? 1 : width;
height = height < 1 ? 1 : height;
if (texturePool.length > 0)
{
final res = texturePool.pop();
2023-12-11 17:23:33 +00:00
BitmapDataUtil.resizeTexture(res, width, height);
2023-09-23 18:59:24 +00:00
return res;
}
return Lib.current.stage.context3D.createTexture(width, height, BGRA, true);
}
function disposeTextures():Void
{
trace('disposing textures');
for (bitmap in grabbed)
{
bitmap.dispose();
}
grabbed.clear();
for (texture in texturePool)
{
texture.dispose();
}
texturePool.resize(0);
bgTexture.dispose();
bgBitmap.dispose();
}
}