Easily add a fake depth effect to your 2D platformer or top-down games made with KAPLAY.js.
(Designed for platformers; top-down requires some amount of kludgery to work.)
pnpm add @r47onfire/kaplay-slice3dThen in your kaplay options:
import kaplay from "kaplay";
import slice3d from "@r47onfire/kaplay-slice3d";
// ...other imports...
const K = kaplay({
// ...other options...
plugins: [
// ...other plugins...
slice3d
]
});set3DEnabled(x)andis3DEnabled()- set or get the state of 3D depth rendering. You can expose this as a setting in your game, if the player wants to turn it off.
- New option:
useDepth. Set this to true and the sprite will become affected by its z-value, and render with depth. - New option:
hideWhenDepthDisabled. Set this to true, and the sprite will skip rendering, if the 3D effect is disabled withset3DEnabled(false).
This is a simple helper component that creates (as children) a bunch of stacked sprites, in order to build a "block" that is rendered as the stack of slices.
The layers argument is a list of objects:
spriteandopt- these get passed to thespritecomponent (which is the patched one above, not the vanillasprite).dz(default 0) - the z-offset of the layer, which is relative to the parent's z value and will be updated as well if the parent's is changed.offset(defaultvec2(0, 0)) - the position offset of the layer origin relative to the parent.
camera3d- Controls the 3D perspective rendering depth. The camera is positioned to face directly towards the screen and cannot be rotated, however, it can be moved.camera3d.offset- the x/y offset relative to the KAPLAY camera (getCamPos()andsetCamPos()) to use when calculating the perspective warp.camera3d.z- the z-value, or how far the camera is from the screen plane. The FOV of the camera is controlled by this, and is implicitly defined as such that stuff at z=0 will be rendered in the exact same position and scale as they would without this plugin.camera3d.fade- controls background fog. This is the negative z-distance that will get 100% fog, with z=0 being no fog.camera3d.fadeColor- the color that objects'colorvalue is lerp'ed towards based on the amount of fog they are affected by. If the object doesn't have thecolor()component, it won't be affected by this.camera3d.fadeOpacity- the amount by which to reduce the object'sopacityvalue by based on the amount of fog they are affected by. (This will never increase the amount of opacity, unless you set this to something greater than 1.) If the object doesn't have theopacity()component, it won't be affected by this.