-
-
Save drakeirving/2677c2eefe353266bed0425fed8ef3c5 to your computer and use it in GitHub Desktop.
Motion Blur Effect
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
task MotionBlur(n, delay, frames, max_intensity){ | |
// n: number of image frames | |
// delay: delay in frames between images | |
// frames: duration of effect in frames | |
// max_intensity: intensity of effect; typically ranges between [0, 1] but can be set higher | |
let num = min(n, 10); | |
let intensity = max_intensity; | |
let textures = []; | |
let tex_objs = []; | |
ascent(i in 0..num){ | |
textures = textures ~ ["EffectBuffer"~itoa(i)]; | |
CreateRenderTarget(textures[i]); | |
tex_objs = tex_objs ~ [ makeTextureObject(textures[i]) ]; | |
} | |
function makeTextureObject(texture){ | |
let obj = ObjPrim_Create(OBJ_SPRITE_2D); | |
ObjRender_SetPosition(obj, 0, 0, 0); | |
ObjPrim_SetTexture(obj, texture); | |
ObjSprite2D_SetSourceRect(obj, 32, 16, 32+384, 16+448); | |
ObjSprite2D_SetDestRect(obj, 0, 0, 0+384, 0+448); | |
Obj_SetRenderPriorityI(obj, 79); | |
return obj; | |
} | |
let c = 0; | |
let duration = floor(frames/delay); | |
ascent(t in 0..duration){ | |
// render frame to current target | |
RenderToTextureA1(textures[c], 1, 69, true); | |
ascent(i in 0..num){ | |
Obj_SetRenderPriorityI( tex_objs[(c + i + 1) % num], 79 - i ); | |
ObjRender_SetAlpha( tex_objs[(c + i + 1) % num], intensity * 255 / ((num-i) + 1) ); | |
// With layers n, n-1, ..., 3, 2, 1, setting their respective opacities | |
// to 1/n, 1/(n-1), ..., 1/3, 1/2, 1, makes them all equally visible | |
} | |
// effect end transition | |
if(duration - t < num){ | |
intensity = max_intensity * (duration - t) / num; | |
} | |
c = (c + 1) % num; // switch to next target | |
loop(delay){yield;} | |
} | |
ascent(i in 0..length(tex_objs)){ Obj_Delete(tex_objs[i]); } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment