Skip to content

IBL Filter ​

ts
import { ContentLoader } from '@gglib/content'
import {
  IblBRDFLutEffect,
  IblDistributionFunction,
  IblFilterEffect,
  PanoramaToCubemapEffect,
  skyboxMaterial,
} from '@gglib/effects'
import {
  boxGeometry,
  Color,
  createDevice,
  FrameContext,
  PlatformId,
  SpriteBatch,
  Texture,
  TextureUsage,
} from '@gglib/graphics'
import { HDR } from '@gglib/loaders'
import { DEGREE_TO_RAD, mat4$initPerspectiveFieldOfView, mat4$rotateY, mat4Identity } from '@gglib/math'
import { mount } from 'tweak-ui'

const files = {
  Court: '/textures/hdr/footprint_court.hdr',
  Exterior: '/textures/hdr/cannon_exterior.hdr',
  Overcast: '/textures/hdr/overcast_puresky.hdr',
  Memorial: '/textures/hdr/memorial.hdr',
}
const params = {
  texture: files.Court,
  lutSampleCount: 512,
  lambertSampleCount: 2048,
  ggxSampleCount: 1024,
  charlieSampleCount: 64,
  intensity: 1,
  cubemap: 'cubemap' as DisplayCubemap,
  cubemapBlur: 0,
  frameTime: 0,
  fieldOfView: 60,
}

type DisplayCubemap = 'cubemap' | 'lambert' | 'ggx' | 'charlie'

export default async (canvas: HTMLCanvasElement, tools: HTMLElement, platform: PlatformId) => {
  const device = await createDevice({ canvas, platform, autosize: true }).ready

  const content = new ContentLoader(device)
  content.registerLoader(HDR.Loader)

  let texture: Texture
  const panel = mount(tools, (ui) => {
    loadTexture(params.texture)

    ui.group('Settings', { collapsible: true, defaultCollapsed: true }, () => {
      ui.group('Input', () => {
        ui.select(params, 'texture', {
          options: files,
          onChange: () => loadTexture(params.texture),
        })
      })

      ui.group('IBL Filter', () => {
        ui.scalar(params, 'lutSampleCount', { range: true, min: 32, max: 1024, step: 32 })
        ui.scalar(params, 'lambertSampleCount', { range: true, min: 32, max: 2048, step: 32 })
        ui.scalar(params, 'ggxSampleCount', { range: true, min: 32, max: 1024, step: 32 })
        ui.scalar(params, 'charlieSampleCount', { range: true, min: 32, max: 1024, step: 32 })
        ui.scalar(params, 'intensity', { range: true, min: 0, max: 10 })
      })

      ui.group('Cubemap', () => {
        ui.select(params, 'cubemap', {
          label: 'Display Cubemap',
          options: ['cubemap', 'lambert', 'ggx', 'charlie'] satisfies DisplayCubemap[],
        })
        ui.scalar(params, 'cubemapBlur', { label: 'Blur', range: true, min: 0, max: 1 })
        ui.scalar(params, 'fieldOfView', { range: true, min: 10, max: 120 })
      })
      ui.group('Performance', () => {
        ui.graph({
          rows: [
            {
              name: 'frameTime',
              sample: () => params.frameTime * 1000,
              fractionDigits: 2,
              smoothing: 0.9,
              min: 0,
              max: 50,
            },
          ],
        })
      })
    })
  })

  async function loadTexture(url: string) {
    texture = await content.loadTexture(url)
  }

  const spriteBatch = new SpriteBatch(device)
  const fxCubemap = new PanoramaToCubemapEffect(device)
  const fxIblBrdf = new IblBRDFLutEffect(device)
  const fxIblFilter = new IblFilterEffect(device)

  const geometry = boxGeometry(device)
  const material = skyboxMaterial(device)

  const pass = device.renderPass

  const cubemap = device.createRenderTarget({
    name: 'Cybemap Render Target',
    type: 'cube',
    mipLevelCount: 5,
    depth: 6,
    width: 512,
    height: 512,
    usage: TextureUsage.TextureBinding,
    format: 'rgba16float',
  })

  const rtScene = device.createRenderTarget({
    name: 'Scene Render Target',
    width: device.output.width,
    height: device.output.height,
    usage: TextureUsage.TextureBinding,
  })

  const lutGGX = device.createRenderTarget({
    width: 512,
    height: 512,
    format: 'rgba16float',
    usage: TextureUsage.TextureBinding,
  })
  const lutSheen = device.createRenderTarget({
    width: 512,
    height: 512,
    format: 'rgba16float',
    usage: TextureUsage.TextureBinding,
  })

  const envLambert = device.createRenderTarget({
    type: 'cube',
    width: 256,
    height: 256,
    depth: 6,
    format: 'rgba16float',
    mipLevelCount: 5,
    usage: TextureUsage.TextureBinding,
  })
  const envGGX = device.createRenderTarget({
    type: 'cube',
    width: 256,
    height: 256,
    depth: 6,
    format: 'rgba16float',
    mipLevelCount: 5,
    usage: TextureUsage.TextureBinding,
  })
  const envCharlie = device.createRenderTarget({
    type: 'cube',
    width: 256,
    height: 256,
    depth: 6,
    format: 'rgba16float',
    mipLevelCount: 5,
    usage: TextureUsage.TextureBinding,
  })

  const world = mat4Identity()
  const proj = mat4Identity()

  function frame(ctx: FrameContext) {
    params.frameTime = ctx.delta
    pass.setClearColor(0, Color.TransparentBlack)
    pass.clear()

    rtScene.resizeToMatch(device.output)
    mat4$rotateY(world, -10 * DEGREE_TO_RAD * ctx.delta)
    mat4$initPerspectiveFieldOfView(
      proj,
      params.fieldOfView * DEGREE_TO_RAD,
      rtScene.width / rtScene.height,
      0.1,
      100,
      device.ndcMinZ,
    )
    if (!texture || !fxIblBrdf.isValid || !fxIblFilter.isValid || !fxCubemap.isValid) {
      pass.flush()
      return
    }

    // generate cubemap from panorama
    fxCubemap.textureIn = texture
    fxCubemap.textureOut = cubemap
    fxCubemap.render(pass)
    cubemap.updateMipmaps()

    // generate GGX lookup table
    fxIblBrdf.textureOut = lutGGX
    fxIblBrdf.distribution = IblDistributionFunction.GGX
    fxIblBrdf.samples = params.lutSampleCount
    fxIblBrdf.render(pass)

    // generate Charlie lookup table
    fxIblBrdf.textureOut = lutSheen
    fxIblBrdf.distribution = IblDistributionFunction.CHARLIE
    fxIblBrdf.samples = params.lutSampleCount
    fxIblBrdf.render(pass)

    // setup common inputs for IBL filter
    fxIblFilter.cubemapIn = cubemap

    fxIblFilter.intensity = params.intensity

    // generate radiance cubemap
    fxIblFilter.cubemapOut = envLambert
    fxIblFilter.samples = params.lambertSampleCount
    fxIblFilter.distribution = IblDistributionFunction.LAMBERT
    fxIblFilter.render(pass)

    // generate irradiance cubemap
    fxIblFilter.cubemapOut = envGGX
    fxIblFilter.samples = params.ggxSampleCount
    fxIblFilter.distribution = IblDistributionFunction.GGX
    fxIblFilter.render(pass)

    // generate sheen cubemap
    fxIblFilter.cubemapOut = envCharlie
    fxIblFilter.samples = params.charlieSampleCount
    fxIblFilter.distribution = IblDistributionFunction.CHARLIE
    fxIblFilter.render(pass)

    // render cubemap to scene render target
    pass.flush()
    pass.setRenderTarget(0, rtScene)
    pass.setViewportState(0, 0, rtScene.width, rtScene.height)
    pass.setClearColor(0, Color.TransparentBlack)
    pass.clear()
    switch (params.cubemap) {
      case 'cubemap':
        material.EnvironmentMap = cubemap
        break
      case 'lambert':
        material.EnvironmentMap = envLambert
        break
      case 'ggx':
        material.EnvironmentMap = envGGX
        break
      case 'charlie':
        material.EnvironmentMap = envCharlie
        break
    }
    material.Blur = params.cubemapBlur
    material.MipCount = material.EnvironmentMap.mipLevelCount
    material.ViewProjection = proj
    material.ObjectModel = world
    material.effect.applyInputs(material.inputBlocks)
    material.effect.draw(pass, geometry)
    pass.submit()

    // present all render targets to screen
    pass.setRenderTarget(0, null)
    pass.setViewportState(0, 0, device.output.width, device.output.height)
    pass.setClearColor(0, Color.TransparentBlack)
    pass.clear()

    spriteBatch.begin()
    spriteBatch
      .next(rtScene)
      .source(0, 0, rtScene.width, rtScene.height)
      .flipY(device.isWebGL2 && rtScene.isRenderTarget)
      .destination(0, 0, device.output.width, device.output.height)

    // input panorama and lookup tables aside, ui is on the right
    const margin = 8
    const width = Math.min(device.output.width / 4, 320)
    let y = margin
    for (const tex of [texture, lutGGX, lutSheen]) {
      const height = (width * tex.height) / tex.width
      spriteBatch.next(tex).source(0, 0, tex.width, tex.height).destination(margin, y, width, height)
      y += height + margin
    }
    spriteBatch.draw()

    pass.flush()
  }

  device.schedule(frame)
  return () => {
    device.dispose()
    panel?.dispose()
  }
}

Released under the MIT License.