Skip to Content
defold-typescript libraries
paweljarosz

paweljarosz/defold-fake-real-glass/fake_real_glass

A multipass glass rendering library for Defold with screen-space double refraction, thickness-based distortion, optional color absorption and local cubemap reflection probes. Supports lighting from Defold light components and configurable reflection modes that update probes every frame, capture once or use pre-rendered cubemaps. Includes a reusable Lua render module, glass materials and example scenes with glass spheres, a wine glass and planar mirror reflections. Integrates with custom render pipelines and requires Defold 1.14.0 or newer.

  • GitHub: paweljarosz/defold-fake-real-glass — pinned to v1.1
    1. Pick a release from paweljarosz/defold-fake-real-glass releases and add its Source code (zip) URL (or a packaged .zip asset, if the library ships one) to game.project under [project] dependencies, then Fetch Libraries in the Defold editor.
    2. Run bunx @defold-typescript/cli resolve to materialize its types.
    3. Import it under a namespace alias of your choice:
      import * as fake_real_glass from "fake_real_glass"

Functions

create(options?: FakeRealGlassOptions): FakeRealGlassState

Create the per-render-script state for the fake glass pipeline.

This allocates Lua-side state immediately. Screen-sized render targets are created by resize() and recreated automatically when sizes change.

The returned table is intended to be stored by one render script instance. It tracks probe settings, render targets, and the constant buffer used when drawing visible glass objects.

Parameters

  • options?: FakeRealGlassOptions — Optional module setup overrides for tags, probe defaults, and materials.

Returns

  • FakeRealGlassState

resize(glass: FakeRealGlassState | undefined, width: number, height: number)

Resize screen-sized render targets to match the current window size.

Call this from init() and when the window size changes.

If probe rendering is enabled, this also ensures the cubemap exists, but does not force it to be redrawn immediately.

Parameters

  • glass: FakeRealGlassState | undefined — Module state previously returned by create().
  • width: number — New window width in pixels.
  • height: number — New window height in pixels.

final(glass?: FakeRealGlassState | undefined)

Release all render targets owned by the module.

Call this from the host render script's final() function to avoid leaking the scene capture, back-face capture, and probe-face render targets.

Parameters

  • glass?: FakeRealGlassState | undefined — Module state previously returned by create().

on_message(glass: FakeRealGlassState | undefined, message_id: Hash, message: FakeRealGlassMessage): boolean

Handle probe-control messages sent to @render:.

Returns true if the message belonged to this module and was handled. That lets the host render script early-return without processing the same message twice.

Parameters

  • glass: FakeRealGlassState | undefined — Module state previously returned by create().
  • message_id: Hash — Incoming render message id.
  • message: FakeRealGlassMessage — Probe message payload.

Returns

  • boolean

draw(glass: FakeRealGlassState | undefined, params: FakeRealGlassDrawParams)

Run the fake glass passes for the current frame.

The host render script should:

  1. draw its normal opaque world to the default framebuffer
  2. call this function before GUI rendering

This function may:

  1. re-render the six probe faces
  2. capture the opaque scene into scene_rt
  3. capture the glass back faces into backface_rt
  4. draw the visible front-face glass to the default framebuffer

Parameters

  • glass: FakeRealGlassState | undefined — Module state previously returned by create().
  • params: FakeRealGlassDrawParams — Per-frame camera, target-size, clear-buffer, and world-redraw inputs.

Variables

MSG_SET_PROBE_POSITION: Hash

Legacy message id that moves only the probe center and marks the probe dirty.

MSG_SET_PROBE_SETTINGS: Hash

Message id for updating one or more probe settings in a single call.

PROBE_MODE_DISABLED: FakeRealGlassProbeMode

Numeric constant for disabled probe rendering.

PROBE_MODE_CAPTURE_ONCE: FakeRealGlassProbeMode

Numeric constant for capture-once probe rendering.

PROBE_MODE_EVERY_FRAME: FakeRealGlassProbeMode

Numeric constant for per-frame probe rendering.

PROBE_MODE: FakeRealGlassProbeModeTable

Named probe mode enum for gameplay/render code.

Types

FakeRealGlassProbeModeTable

FakeRealGlassProbeModeTable.DISABLED: FakeRealGlassProbeMode

Do not render probe faces; keep the material's fallback probe textures bound.

FakeRealGlassProbeModeTable.CAPTURE_ONCE: FakeRealGlassProbeMode

Render the six probe faces only while the probe is marked dirty.

FakeRealGlassProbeModeTable.EVERY_FRAME: FakeRealGlassProbeMode

Render the six probe faces every frame for fully dynamic reflections.

FakeRealGlassConstantBuffer

FakeRealGlassConstantBuffer.view: Matrix4

Current camera view matrix used by the front-face shader.

FakeRealGlassConstantBuffer.view_proj: Matrix4

Current camera view-projection matrix used to reproject world points back to screen UVs.

FakeRealGlassConstantBuffer.camera_position: Vector4

World-space camera position; the shader uses the xyz components.

FakeRealGlassConstantBuffer.render_state: Vector4

Runtime state flags; x = 1 means the live capture textures are currently bound.

FakeRealGlassConstantBuffer.probe_center: Vector4

World-space local probe center; the shader uses the xyz components.

FakeRealGlassConstantBuffer.probe_extents: Vector4

Local probe half-extents for box-projected reflections; the shader uses the xyz components.

FakeRealGlassConstantBuffer.probe_params: Vector4

Probe sampling parameters; x = 1 / probe_size, y = max blur radius in texels.

FakeRealGlassProbeFace

FakeRealGlassProbeFace.face: number

Cubemap face enum selected when binding the target.

FakeRealGlassProbeFace.forward: Vector3

Forward axis used when capturing this face.

FakeRealGlassProbeFace.up: Vector3

Up axis used when capturing this face.

FakeRealGlassOptions

FakeRealGlassOptions.glass_tag: string

Material tag used for visible glass models. Default: "glass".

FakeRealGlassOptions.glass_predicate: Opaque<"userdata">

Optional prebuilt glass predicate. Overrides glass_tag.

FakeRealGlassOptions.backface_material: string

Name of the .render material entry used for the back-face override pass.

FakeRealGlassOptions.probe_position: Vector3

Initial probe center in world space.

FakeRealGlassOptions.probe_extents: Vector3

Initial local probe half-extents for box projection.

FakeRealGlassOptions.probe_size: number

Initial size of each probe face in pixels.

FakeRealGlassOptions.probe_far: number

Optional probe far plane override.

FakeRealGlassOptions.probe_near: number

Optional probe near plane override.

FakeRealGlassOptions.probe_mode: FakeRealGlassProbeMode

Initial probe update mode.

FakeRealGlassOptions.probe_max_blur_texels: number

Maximum blur radius, in texels, for rough probe sampling.

FakeRealGlassState

FakeRealGlassState.glass_predicate: Opaque<"userdata">

Predicate used to draw the visible glass objects in both back-face and front-face passes.

FakeRealGlassState.backface_material: string

Name of the .render material entry enabled during the hidden back-face pass.

FakeRealGlassState.probe_position: Vector3

World-space center of the local reflection probe.

FakeRealGlassState.probe_extents: Vector3

Half-extents of the local box used for box-projected reflections.

FakeRealGlassState.probe_size: number

Resolution, in pixels, of each of the six probe faces.

FakeRealGlassState.probe_near: number

Near clip plane used while rendering probe faces.

FakeRealGlassState.probe_far: number

Far clip plane used while rendering probe faces.

FakeRealGlassState.probe_projection: Matrix4

Shared 90-degree projection matrix used by every probe face.

FakeRealGlassState.probe_mode: FakeRealGlassProbeMode

Current reflection probe update policy.

FakeRealGlassState.probe_dirty: boolean

Whether the probe needs to be re-rendered before it can be reused.

FakeRealGlassState.probe_max_blur_texels: number

Maximum blur radius, in texels, used when rough surfaces sample the probe.

FakeRealGlassState.rt_width: number

Width of the window-sized scene/back-face render targets.

FakeRealGlassState.rt_height: number

Height of the window-sized scene/back-face render targets.

FakeRealGlassState.scene_rt: Opaque<"userdata"> | undefined

Render target containing the opaque scene color used for refraction sampling.

FakeRealGlassState.backface_rt: Opaque<"userdata"> | undefined

Render target containing back-face normals/depth for thickness and exit-ray reconstruction.

FakeRealGlassState.probe_rt: Opaque<"userdata"> | undefined

Cubemap render target containing all six probe faces.

FakeRealGlassState.glass_constants: FakeRealGlassConstantBuffer

Constant buffer populated just before the visible glass draw.

FakeRealGlassMessage

FakeRealGlassMessage.position: Vector3

Probe center in world space.

FakeRealGlassMessage.extents: Vector3

Probe half-extents used for box projection.

FakeRealGlassMessage.size: number

Probe face size in pixels.

FakeRealGlassMessage.far: number

Probe far plane override.

FakeRealGlassMessage.mode: FakeRealGlassProbeMode

Probe update mode.

FakeRealGlassMessage.rebuild: boolean

Force a new probe capture in capture-once mode.

FakeRealGlassDrawParams

FakeRealGlassDrawParams.view: Matrix4

Camera view matrix.

FakeRealGlassDrawParams.projection: Matrix4

Camera projection matrix.

FakeRealGlassDrawParams.window_width: number

Current window width in pixels.

FakeRealGlassDrawParams.window_height: number

Current window height in pixels.

FakeRealGlassDrawParams.clear_buffers: LuaTable

Render clear buffer table, reused for probe and scene captures.

FakeRealGlassDrawParams.draw_world: FakeRealGlassWorldDrawFn

Callback that redraws the opaque world into the currently bound target.

FakeRealGlassDrawParams.output_target: Opaque<"userdata">

Destination for the visible glass pass; default is the backbuffer.

FakeRealGlassDrawParams.capture_probe: boolean

Set false to reuse the current probe for an additional view.

FakeRealGlassDrawParams.flip_culling: boolean

Reverse face culling for a mirrored camera.

On this page

Functionscreate(options?: FakeRealGlassOptions): FakeRealGlassStateresize(glass: FakeRealGlassState | undefined, width: number, height: number)final(glass?: FakeRealGlassState | undefined)on_message(glass: FakeRealGlassState | undefined, message_id: Hash, message: FakeRealGlassMessage): booleandraw(glass: FakeRealGlassState | undefined, params: FakeRealGlassDrawParams)VariablesMSG_SET_PROBE_POSITION: HashMSG_SET_PROBE_SETTINGS: HashPROBE_MODE_DISABLED: FakeRealGlassProbeModePROBE_MODE_CAPTURE_ONCE: FakeRealGlassProbeModePROBE_MODE_EVERY_FRAME: FakeRealGlassProbeModePROBE_MODE: FakeRealGlassProbeModeTableTypesFakeRealGlassProbeModeTableFakeRealGlassProbeModeTable.DISABLED: FakeRealGlassProbeModeFakeRealGlassProbeModeTable.CAPTURE_ONCE: FakeRealGlassProbeModeFakeRealGlassProbeModeTable.EVERY_FRAME: FakeRealGlassProbeModeFakeRealGlassConstantBufferFakeRealGlassConstantBuffer.view: Matrix4FakeRealGlassConstantBuffer.view_proj: Matrix4FakeRealGlassConstantBuffer.camera_position: Vector4FakeRealGlassConstantBuffer.render_state: Vector4FakeRealGlassConstantBuffer.probe_center: Vector4FakeRealGlassConstantBuffer.probe_extents: Vector4FakeRealGlassConstantBuffer.probe_params: Vector4FakeRealGlassProbeFaceFakeRealGlassProbeFace.face: numberFakeRealGlassProbeFace.forward: Vector3FakeRealGlassProbeFace.up: Vector3FakeRealGlassOptionsFakeRealGlassOptions.glass_tag: stringFakeRealGlassOptions.glass_predicate: Opaque<"userdata">FakeRealGlassOptions.backface_material: stringFakeRealGlassOptions.probe_position: Vector3FakeRealGlassOptions.probe_extents: Vector3FakeRealGlassOptions.probe_size: numberFakeRealGlassOptions.probe_far: numberFakeRealGlassOptions.probe_near: numberFakeRealGlassOptions.probe_mode: FakeRealGlassProbeModeFakeRealGlassOptions.probe_max_blur_texels: numberFakeRealGlassStateFakeRealGlassState.glass_predicate: Opaque<"userdata">FakeRealGlassState.backface_material: stringFakeRealGlassState.probe_position: Vector3FakeRealGlassState.probe_extents: Vector3FakeRealGlassState.probe_size: numberFakeRealGlassState.probe_near: numberFakeRealGlassState.probe_far: numberFakeRealGlassState.probe_projection: Matrix4FakeRealGlassState.probe_mode: FakeRealGlassProbeModeFakeRealGlassState.probe_dirty: booleanFakeRealGlassState.probe_max_blur_texels: numberFakeRealGlassState.rt_width: numberFakeRealGlassState.rt_height: numberFakeRealGlassState.scene_rt: Opaque<"userdata"> | undefinedFakeRealGlassState.backface_rt: Opaque<"userdata"> | undefinedFakeRealGlassState.probe_rt: Opaque<"userdata"> | undefinedFakeRealGlassState.glass_constants: FakeRealGlassConstantBufferFakeRealGlassMessageFakeRealGlassMessage.position: Vector3FakeRealGlassMessage.extents: Vector3FakeRealGlassMessage.size: numberFakeRealGlassMessage.far: numberFakeRealGlassMessage.mode: FakeRealGlassProbeModeFakeRealGlassMessage.rebuild: booleanFakeRealGlassDrawParamsFakeRealGlassDrawParams.view: Matrix4FakeRealGlassDrawParams.projection: Matrix4FakeRealGlassDrawParams.window_width: numberFakeRealGlassDrawParams.window_height: numberFakeRealGlassDrawParams.clear_buffers: LuaTableFakeRealGlassDrawParams.draw_world: FakeRealGlassWorldDrawFnFakeRealGlassDrawParams.output_target: Opaque<"userdata">FakeRealGlassDrawParams.capture_probe: booleanFakeRealGlassDrawParams.flip_culling: boolean