| 1 |
<!DOCTYPE html> |
| 2 |
<html lang="en"> |
| 3 |
<head> |
| 4 |
<title>Filament Triangle</title> |
| 5 |
<meta charset="utf-8"> |
| 6 |
<meta name="viewport" content="width=device-width,user-scalable=no,initial-scale=1"> |
| 7 |
<link href="favicon.png" rel="icon" type="image/x-icon" /> |
| 8 |
<style> |
| 9 |
body { margin: 0; overflow: hidden; } |
| 10 |
canvas { touch-action: none; width: 100%; height: 100%; } |
| 11 |
</style> |
| 12 |
</head> |
| 13 |
<body> |
| 14 |
<canvas></canvas> |
| 15 |
<script src="filament.js"></script> |
| 16 |
<script src="gl-matrix-min.js"></script> |
| 17 |
<script> |
| 18 |
|
| 19 |
Filament.init([ 'nonlit.filamat' ], () => { |
| 20 |
window.VertexAttribute = Filament.VertexAttribute; |
| 21 |
window.AttributeType = Filament.VertexBuffer$AttributeType; |
| 22 |
window.Projection = Filament.Camera$Projection; |
| 23 |
window.app = new App(document.getElementsByTagName('canvas')[0]); |
| 24 |
}); |
| 25 |
|
| 26 |
class App { |
| 27 |
constructor(canvas) { |
| 28 |
this.canvas = canvas; |
| 29 |
const engine = this.engine = Filament.Engine.create(this.canvas); |
| 30 |
this.scene = engine.createScene(); |
| 31 |
this.triangle = Filament.EntityManager.get().create(); |
| 32 |
this.scene.addEntity(this.triangle); |
| 33 |
|
| 34 |
const TRIANGLE_POSITIONS = new Float32Array([ |
| 35 |
1, 0, |
| 36 |
Math.cos(Math.PI * 2 / 3), Math.sin(Math.PI * 2 / 3), |
| 37 |
Math.cos(Math.PI * 4 / 3), Math.sin(Math.PI * 4 / 3), |
| 38 |
]); |
| 39 |
|
| 40 |
const TRIANGLE_COLORS = new Uint32Array([0xffff0000, 0xff00ff00, 0xff0000ff]); |
| 41 |
|
| 42 |
this.vb = Filament.VertexBuffer.Builder() |
| 43 |
.vertexCount(3) |
| 44 |
.bufferCount(2) |
| 45 |
.attribute(VertexAttribute.POSITION, 0, AttributeType.FLOAT2, 0, 8) |
| 46 |
.attribute(VertexAttribute.COLOR, 1, AttributeType.UBYTE4, 0, 4) |
| 47 |
.normalized(VertexAttribute.COLOR) |
| 48 |
.build(engine); |
| 49 |
|
| 50 |
this.vb.setBufferAt(engine, 0, TRIANGLE_POSITIONS); |
| 51 |
this.vb.setBufferAt(engine, 1, TRIANGLE_COLORS); |
| 52 |
|
| 53 |
this.ib = Filament.IndexBuffer.Builder() |
| 54 |
.indexCount(3) |
| 55 |
.bufferType(Filament.IndexBuffer$IndexType.USHORT) |
| 56 |
.build(engine); |
| 57 |
|
| 58 |
this.ib.setBuffer(engine, new Uint16Array([0, 1, 2])); |
| 59 |
|
| 60 |
const mat = engine.createMaterial('nonlit.filamat'); |
| 61 |
const matinst = mat.getDefaultInstance(); |
| 62 |
Filament.RenderableManager.Builder(1) |
| 63 |
.boundingBox({ center: [-1, -1, -1], halfExtent: [1, 1, 1] }) |
| 64 |
.material(0, matinst) |
| 65 |
.geometry(0, Filament.RenderableManager$PrimitiveType.TRIANGLES, this.vb, this.ib) |
| 66 |
.build(engine, this.triangle); |
| 67 |
|
| 68 |
this.swapChain = engine.createSwapChain(); |
| 69 |
this.renderer = engine.createRenderer(); |
| 70 |
this.camera = engine.createCamera(Filament.EntityManager.get().create()); |
| 71 |
|
| 72 |
this.view = engine.createView(); |
| 73 |
this.view.setSampleCount(4); |
| 74 |
this.view.setCamera(this.camera); |
| 75 |
this.view.setScene(this.scene); |
| 76 |
|
| 77 |
this.renderer.setClearOptions({clearColor: [0.0, 0.1, 0.2, 1.0], clear: true}); |
| 78 |
|
| 79 |
this.resize(); |
| 80 |
this.render = this.render.bind(this); |
| 81 |
this.resize = this.resize.bind(this); |
| 82 |
window.addEventListener('resize', this.resize); |
| 83 |
window.requestAnimationFrame(this.render); |
| 84 |
} |
| 85 |
|
| 86 |
render() { |
| 87 |
const radians = Date.now() / 1000; |
| 88 |
const transform = mat4.fromRotation(mat4.create(), radians, [0, 0, 1]); |
| 89 |
const tcm = this.engine.getTransformManager(); |
| 90 |
const inst = tcm.getInstance(this.triangle); |
| 91 |
tcm.setTransform(inst, transform); |
| 92 |
inst.delete(); |
| 93 |
this.renderer.render(this.swapChain, this.view); |
| 94 |
window.requestAnimationFrame(this.render); |
| 95 |
} |
| 96 |
|
| 97 |
resize() { |
| 98 |
const dpr = window.devicePixelRatio; |
| 99 |
const width = this.canvas.width = window.innerWidth * dpr; |
| 100 |
const height = this.canvas.height = window.innerHeight * dpr; |
| 101 |
this.view.setViewport([0, 0, width, height]); |
| 102 |
const aspect = width / height; |
| 103 |
this.camera.setProjection(Projection.ORTHO, -aspect, aspect, -1, 1, 0, 1); |
| 104 |
} |
| 105 |
} |
| 106 |
</script> |
| 107 |
</body> |
| 108 |
</html> |