Custom Pipeline
ts
import { ContentLoader } from '@gglib/content'
import { CommonMaterial } from '@gglib/effects'
import { Color, createDevice, CullState, DepthState, FrameContext, PlatformId } from '@gglib/graphics'
import {
DEGREE_TO_RAD,
mat4$initPerspectiveFieldOfView,
mat4$initRotationY,
mat4$initTranslationXYZ,
mat4GetTranslation,
mat4Identity,
mat4Invert,
} from '@gglib/math'
import { Model } from '@gglib/model'
import { mount } from 'tweak-ui'
import { PixelsLoader } from './pixels-loader'
export default async (canvas: HTMLCanvasElement, tools: HTMLElement, platform: PlatformId) => {
const device = await createDevice({ canvas, platform, autosize: true }).ready
const content = new ContentLoader(device)
// The PixelsLoader class implements the registerable interface.
// We can just pass the class into the registry.
content.registerLoader(PixelsLoader)
// Hardcoded options that we can pick from for this demo
const assets = ['/megaman.pixels', '/sonic.pixels', '/mario.pixels']
const scene = {
model: null! as Model,
}
// loadModel will be called with one of the asset paths
function loadModel(path: string) {
content
.loadModel(path, {
// For demonstraion purposes enforce a type, that will be matched against all
// registered loaders. Not needed here, since the path already contains
// the .pixels extension which would be used otherwise
type: '.pixels',
})
.then((result) => {
scene.model = result
})
}
// load initial asset and mount the ui options
loadModel(assets[0])
const panel = mount(tools, (ui) => {
ui.select({ model: assets[0] }, 'model', {
options: assets,
onChange: (value) => loadModel(value),
})
})
// everything else is just common scene rendering procedure
const world = mat4Identity()
const view = mat4Identity()
const proj = mat4Identity()
const cam = mat4Identity()
const msaaDepth = device.createDepthTarget({
width: device.output.width,
height: device.output.height,
format: 'depth24plus-stencil8',
sampleCount: 4,
})
const msaaColor = device.createRenderTarget({
width: device.output.width,
height: device.output.height,
format: device.output.format,
sampleCount: 4,
})
const pass = device.renderPass
function frame(ctx: FrameContext) {
msaaColor.resizeToMatch(device.output)
msaaDepth.resizeToMatch(device.output)
let t = ctx.time
mat4$initTranslationXYZ(cam, 0, 0, 30)
mat4Invert(cam, view)
mat4$initPerspectiveFieldOfView(proj, 60 * DEGREE_TO_RAD, device.output.aspectRatio, 0.1, 100, device.ndcMinZ)
if (scene.model) {
mat4$initRotationY(world, t * DEGREE_TO_RAD * 25)
for (const mesh of scene.model.meshes) {
for (const material of mesh.materials) {
const mtl = material as CommonMaterial
mtl.World = world
mtl.View = view
mtl.Projection = proj
mat4GetTranslation(cam, mtl.CameraPosition)
}
}
}
pass.setRenderTarget(0, msaaColor, 0, 0, device.output)
pass.setDepthTarget(msaaDepth)
pass.setClearColor(0, Color.CornflowerBlue)
pass.setClearDepth(1)
pass.clear()
pass.setDepthState(DepthState.LessEqual)
pass.setCullState(CullState.CullBack)
if (scene.model) {
scene.model.draw()
}
pass.submit()
pass.resolve()
pass.flush()
}
device.schedule(frame)
return () => {
device.dispose()
panel.dispose()
}
}
ts
import { AssetContainer, AssetLoader, LoadContext } from '@gglib/content'
import MARIO from './mario.pixels'
import MEGAMAN from './megaman.pixels'
import { PixelsContainer } from './pixels-container'
import SONIC from './sonic.pixels'
export class PixelsLoader implements AssetLoader {
// the file extensions that this loader is able to handle
public static extensions = ['.pixels']
// optionally we could match against mime types
public static mimeTypes = []
// the loader factory that will be called once for every load request
public static create = () => new PixelsLoader()
public async load(url: string, context: LoadContext): Promise<AssetContainer> {
// Usually we would fetch the file from the network like this:
//
// const input = context.content.fetch(url, { responseType: 'text' }).then((response) => response.body)
//
// However, in this example we have hardcoded the pixel data
const input = {
'/mario.pixels': MARIO,
'/megaman.pixels': MEGAMAN,
'/sonic.pixels': SONIC,
}[url]
if (!input) {
throw new Error(`PixelsLoader: No data found for URL: ${url}`)
}
// The loader is only responsible for fetching data and creating an asset container.
// Here we just pass the loaded data to PixelsContainer for further processing
return new PixelsContainer(input)
}
}
ts
import { AssetContainer, AssetType, LoadContext } from '@gglib/content'
import { buildBox, Color, CommonMaterialProps, GeometryBuilder, MaterialOptions } from '@gglib/graphics'
import { mat4$initTranslationXYZ, mat4Identity } from '@gglib/math'
import { ModelOptions } from '@gglib/model'
export class PixelsContainer implements AssetContainer {
private data: string
public constructor(data: string) {
this.data = data
}
public count(type: AssetType<any, any>): number {
if (type === AssetType.Model) {
return 1
}
return 0
}
public load<T>(type: AssetType<T, any>, index: number, context: LoadContext): Promise<T>
public load<T>(type: AssetType, index: number, context: LoadContext): Promise<any> {
if (type !== AssetType.Model) {
throw new Error(`asset type not supported: ${type}`)
}
return parsePixels(this.data)
}
}
async function parsePixels(data: string): Promise<ModelOptions> {
// lookup map for colors
const colorMap: Record<string, Color> = {
0: Color.fromBytes(0, 0, 0, 255),
1: Color.fromBytes(255, 0, 0, 255),
2: Color.fromBytes(0, 255, 0, 255),
3: Color.fromBytes(255, 255, 0, 255),
4: Color.fromBytes(0, 0, 255, 255),
5: Color.fromBytes(255, 0, 255, 255),
6: Color.fromBytes(0, 255, 255, 255),
7: Color.fromBytes(255, 255, 255, 255),
}
const gap = 0.1
const colors: Color[] = []
const builders: GeometryBuilder[] = []
const materials: MaterialOptions[] = []
const transform = mat4Identity()
const rows = data.split('\n')
rows.forEach((row, y) => {
const cols = row.trim().split('')
return cols.forEach((col, x) => {
const color = colorMap[col]
if (!color) {
return
}
if (!colors.includes(color)) {
colors.push(color)
builders.push(new GeometryBuilder({ layout: [['position', 'normal', 'texture']] }))
materials.push({
properties: {
BaseColor: [color.r, color.g, color.b],
} satisfies CommonMaterialProps,
})
}
const index = colors.indexOf(color)
const builder = builders[index]
mat4$initTranslationXYZ(
transform,
x - cols.length / 2 + 0.5 + x * gap,
rows.length - y - rows.length / 2 + 0.5 - y * gap,
0,
)
builder.withTransform(transform, () => {
buildBox(builder, { size: 1 })
})
})
})
return {
meshes: [
{
materials: materials,
partImports: builders.map((it, index) => {
return {
geometry: it.toGeometryOptions()!,
materialIndex: index,
}
}),
},
],
nodes: [
{
mesh: 0,
},
],
scenes: [
{
nodes: [0],
},
],
scene: 0,
}
}
ts
export default `
| 11111 |
| 111111111 |
| 2223303 |
| 2323330333 |
| 23223330333|
| 233330000 |
| 333333 |
| 11411411 |
| 1114114111 |
|111144441111|
|331434434133|
|333444444333|
|334444444433|
| 444 444 |
| 222 222 |
|2222 2222|
`.trim()
ts
export default `
| 000 |
| 000660 |
| 04440660 |
| 0444440000 |
| 04444406640 |
| 064444440040 |
| 00064437774470 |
| 0066064377003070 |
| 06666604377003070 0000 |
| 046666604337773730000444000 |
| 0444606604300003006604666060|
| 0444040660333330666604444040|
| 04404406600000666660444000 |
| 044440666666600000 0000 |
| 044066666660 |
| 0044666660 |
| 044444440 |
| 0644444460 |
| 066644446660 |
| 04666600666440 |
| 00444660 044440 |
| 004444440 0444000 |
|044444440 044444440 |
|000000000 000000000 |
`.trim()
ts
export default `
| 000000000000 00 |
| 044444444444400040 |
| 04440344444444440 |
| 0440334444444440 |
| 0403444444444440 |
| 04444444477744440 |
| 044444444777774440 |
| 044444444477770470 |
|0000004444477770470 |
| 044444447770 000 |
| 0444443337777330 |
| 04444443333333330 |
| 00000000033333330 |
| 0300000000 |
| 030043330000 |
| 0307703330000 |
| 0077770330770 |
| 07777003070 |
| 077004000 |
| 004000 |
| 00401100 |
| 00110011700 |
| 011171001110 |
| 0117111101110|
| 0000000000000|
`.trim()