Skip to content

Geometry ​

Until now we created an index and vertex buffer and called the appropriet methods on a pass instance to configure and issue a draw command. This can be grouped in a Geometry instance to offload the geometry related pass composition to that class.

So instead of doing

ts
pass.setIndexBuffer(iBuf)
pass.setVertexBuffer(vBuf)
pass.setPrimitiveType(primitiveType)
pass.drawIndexed(indexCount, instanceCount, indexOffset, baseVertex, instanceOffset)

we can just call

ts
pass.render(geometry)

and can do it repeatedly

ts
import {
  Color,
  createDevice,
  CullState,
  Device,
  FrameContext,
  Geometry,
  PlatformId,
  vertexLayout,
} from '@gglib/graphics'
import {
  DEGREE_TO_RAD,
  mat4$initLookAt,
  mat4$initPerspectiveFieldOfView,
  mat4$initRotationY,
  mat4$invert,
  mat4Identity,
  vec3,
} from '@gglib/math'

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

  const geometry = new Geometry(device, {
    primitiveType: 'triangle-list',
    vertexBuffer: [
      {
        layout: vertexLayout({
          position: 'float32x3',
        }),
        // prettier-ignore
        data: new Float32Array([
        -0.5, -0.5, -0.5,
        0.5, -0.5, -0.5,
        0.5, 0.5, -0.5,
        -0.5, 0.5, -0.5,
        -0.5, -0.5, 0.5,
        0.5, -0.5, 0.5,
        0.5, 0.5, 0.5,
        -0.5, 0.5, 0.5,
      ]),
      },
    ],
    indexBuffer: {
      indexType: 'uint32',
      // prettier-ignore
      data: new Uint32Array([
        4, 5, 6, 4, 6, 7, // front  (+Z)
        1, 0, 3, 1, 3, 2, // back   (-Z)
        0, 4, 7, 0, 7, 3, // left   (-X)
        5, 1, 2, 5, 2, 6, // right  (+X)
        3, 7, 6, 3, 6, 2, // top    (+Y)
        0, 1, 5, 0, 5, 4, // bottom (-Y)
      ]),
    },
  })

  const shader = device.createShaderModule({
    glsl: { vertex: glslVS, fragment: glslFS },
    wgsl: { source: wgsl },
  })

  const world = mat4Identity()
  const view = mat4Identity()
  const projection = mat4Identity()
  const cameraPosition = vec3(0, 1, 2)

  const pass = device.renderPass
  function frame(ctx: FrameContext) {
    pass.setClearColor(0, Color.CornflowerBlue)
    pass.clear()

    if (!shader.isValid) {
      return
    }

    mat4$initRotationY(world, ctx.time * 25 * DEGREE_TO_RAD)
    mat4$initLookAt(view, cameraPosition, vec3.Zero, vec3.UnitY)
    mat4$invert(view)
    mat4$initPerspectiveFieldOfView(projection, 50 * DEGREE_TO_RAD, device.output.aspectRatio, 0.1, 100, device.ndcMinZ)

    shader.program.set('uWorld', world)
    shader.program.set('uView', view)
    shader.program.set('uProjection', projection)
    shader.program.commit()

    pass.setCullState(CullState.CullBack)
    pass.setProgram(shader.program)
    pass.render(geometry)

    pass.resolve()
    pass.submit()
    pass.flush()
  }

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

const glslVS = /*glsl*/ `
  #version 300 es
  in vec3 position;
  out vec3 color;
  uniform mat4 uWorld;
  uniform mat4 uView;
  uniform mat4 uProjection;

  void main(void) {
    color = position + vec3(0.5);
    gl_Position = uProjection * uView * uWorld * vec4(position, 1.0);
  }
`
const glslFS = /*glsl*/ `
  #version 300 es
  precision highp float;
  in vec3 color;
  out vec4 fragColor;
  void main(void) {
    fragColor = vec4(color, 1.0);
  }
`

const wgsl = /*wgsl*/ `

  @group(0) @binding(0) var<uniform> uWorld : mat4x4f;
  @group(0) @binding(1) var<uniform> uView : mat4x4f;
  @group(0) @binding(2) var<uniform> uProjection : mat4x4f;

  struct VertexInput {
    @location(0) position : vec3f,
  };
  struct VertexOutput {
    @builtin(position) Position : vec4f,
    @location(0) color : vec3f,
  };

  @vertex
  fn vs(input: VertexInput) -> VertexOutput {
    var output : VertexOutput;
    output.color = input.position + vec3f(0.5);
    output.Position = uProjection * uView * uWorld * vec4f(input.position, 1.0);
    return output;
  }

  @fragment
  fn fs(input: VertexOutput) -> @location(0) vec4<f32> {
    return vec4f(input.color, 1.0);
  }
`

Released under the MIT License.