Skip to content

DDS ​

ts
import { AssetType, ContentLoader } from '@gglib/content'
import {
  CommonMaterial,
  IblSampler,
  skyboxMaterial,
  SkyboxMaterial,
  TonemapEffect,
  TonemapOperator,
} from '@gglib/effects'
import { MouseInput } from '@gglib/game'
import {
  BlendState,
  boxGeometry,
  Color,
  createDevice,
  CullState,
  DepthState,
  FrameContext,
  Mesh,
  PlatformId,
  TextureUsage,
  TRUE,
} from '@gglib/graphics'
import { DDS, GLTF } from '@gglib/loaders'
import {
  BoundingSphere,
  DEGREE_TO_RAD,
  mat4$initLookAt,
  mat4$initPerspectiveFieldOfView,
  mat4$invert,
  mat4Identity,
  mat4Premultiply,
  vec3,
  vec3$add,
  vec3$initSpherical,
} from '@gglib/math'
import { Model } from '@gglib/model'

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

  const pass = device.renderPass
  const msaaColor = device.createRenderTarget({ sampleCount: 4 })
  const msaaDepth = device.createDepthTarget({ sampleCount: 4, format: 'depth24plus' })
  const color = device.createRenderTarget({ usage: TextureUsage.TextureBinding })
  const fxTonemap = await new TonemapEffect(device).compiled

  GLTF.Loader.registerExtension(GLTF.KhrMaterialsSpecular)
  GLTF.Loader.registerExtension(GLTF.KhrMaterialsEmissiveStrength)
  GLTF.Loader.registerExtension(GLTF.KhrMaterialsPbrSpecularGlossinessHandler)
  GLTF.Loader.registerExtension(GLTF.KhrMaterialsIor)
  const content = new ContentLoader(device)
  content.registerLoader(GLTF.Loader)
  content.registerLoader(DDS.Loader)
  content.registerCreator(AssetType.Material, (ctx, options) => {
    const material = new CommonMaterial(ctx.device, options)
    material.UseIBL = TRUE
    material.IblBrdfMap = iblSampler.lutMapGGX
    material.IblLambertianMap = iblSampler.envMapLambert
    material.IblEnvironmentMap = iblSampler.envMapGGX
    material.IblMipCount = iblSampler.envMapGGX.mipLevelCount
    material.IblIntensity = 1

    return material
  })

  let model: Model
  let sphere: BoundingSphere = BoundingSphere.create()

  async function loadContent() {
    const cubemap = await content.loadTexture('/textures/formats/cubemap.dds')
    iblSampler.update(cubemap)

    model = await content.loadModel('/models/gltf/shader-ball.glb')
    sphere = model.boundingSphere.copy()

    const colorMap = await content.loadTexture('/textures/formats/ice_base.dds')
    const normalMap = await content.loadTexture('/textures/formats/ice_normals.dds')
    const ormMap = await content.loadTexture('/textures/formats/ice_orm.dds')
    for (const mesh of model.meshes) {
      for (const material of mesh.materials) {
        const mtl = material as CommonMaterial
        mtl.BaseMap = colorMap
        mtl.UseBaseMap = TRUE
        mtl.NormalMap = normalMap
        mtl.UseNormalMap = TRUE
        mtl.OcclusionMap = ormMap
        mtl.UseOcclusionMap = TRUE
        mtl.MetallicRoughnessMap = ormMap
        mtl.UseMetallicRoughnessMap = TRUE
        mtl.Ior = 1.3
        mtl.Roughness = 0.5
        mtl.Metallic = 0.5
      }
    }
  }
  loadContent()

  const mouse = new MouseInput()

  const skybox = new Mesh(device, {
    geometries: [boxGeometry(device, { invert: true })],
    materials: [
      skyboxMaterial(device, {
        blur: 0,
        cubemap: iblSampler.envMapGGX,
        intensity: 1,
      }),
    ],
    parts: [
      {
        geometryIndex: 0,
        materialIndex: 0,
      },
    ],
  })

  const world = mat4Identity()
  const camera = {
    theta: 0,
    phi: 90,
    fow: 45,
    distance: 1,
    position: vec3(),
    view: mat4Identity(),
    projection: mat4Identity(),
  }

  function updateCamera() {
    mouse.update()
    if (mouse.leftButtonIsPressed) {
      camera.theta -= mouse.dxNormalized * 360
      camera.phi -= mouse.dyNormalized * 180
    }
    if (mouse.middleButtonIsPressed) {
      camera.distance += mouse.dyNormalized * 2
      camera.distance = Math.max(0.1, camera.distance)
    }

    // prettier-ignore
    vec3$initSpherical(camera.position,
      camera.phi * DEGREE_TO_RAD,
      camera.theta * DEGREE_TO_RAD,
      camera.distance * sphere.radius * 2,
    )
    vec3$add(camera.position, sphere.center)

    mat4$initLookAt(camera.view, camera.position, sphere.center, vec3.UnitY)
    mat4$invert(camera.view)
    mat4$initPerspectiveFieldOfView(
      camera.projection,
      camera.fow * DEGREE_TO_RAD,
      device.output.aspectRatio,
      0.01,
      1000,
      device.ndcMinZ,
    )
  }

  function updateModel(model: Model) {
    model.update(world)
  }

  function drawModel(model: Model) {
    for (const node of model.sceneNodes) {
      const mesh = model.meshes[node.data?.mesh!]
      if (!mesh) {
        continue
      }

      for (const material of mesh.materials) {
        const mtl = material as CommonMaterial
        mtl.World = node.world
        mtl.View = camera.view
        mtl.Projection = camera.projection
        mtl.CameraPosition = camera.position
      }
    }

    model.draw()
  }

  function drawSky() {
    const material = skybox.materials[0] as SkyboxMaterial
    material.ViewProjection = mat4Premultiply(camera.view, camera.projection)
    material.Blur = 0.5
    skybox.draw()
  }

  function frame(ctx: FrameContext) {
    msaaColor.resizeToMatch(device.output)
    msaaDepth.resizeToMatch(device.output)
    color.resizeToMatch(device.output)

    pass.setRenderTarget(0, msaaColor, 0, 0, color)
    pass.setDepthTarget(msaaDepth)
    pass.setCullState(CullState.CullBack)
    pass.setDepthState(DepthState.LessEqual)
    pass.setRenderBlend(0, BlendState.Alpha)
    pass.setClearColor(0, Color.TransparentBlack)
    pass.clear()

    if (model) {
      updateCamera()
      drawSky()
      updateModel(model)
      drawModel(model)
    }
    pass.submit()
    pass.resolve()
    pass.flush()

    fxTonemap.srgb = true
    fxTonemap.operator = TonemapOperator.PBR_NEUTRAL
    fxTonemap.textureIn = color
    fxTonemap.textureOut = device.output
    fxTonemap.render(pass)
    pass.flush()
  }

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

Released under the MIT License.