| 1 |
<!DOCTYPE html> |
| 2 |
<html lang="en"> |
| 3 |
<head> |
| 4 |
<title>Filament Parquet</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 |
const iblfile = 'default_env/default_env_ibl.ktx'; |
| 20 |
const skyfile = 'default_env/default_env_skybox.ktx'; |
| 21 |
|
| 22 |
Filament.init([ |
| 23 |
'parquet.filamat', |
| 24 |
'shader_ball.filamesh', |
| 25 |
'floor_ao_roughness_metallic.png', |
| 26 |
'floor_basecolor.jpg', |
| 27 |
'floor_normal.png', |
| 28 |
iblfile, skyfile |
| 29 |
], () => { |
| 30 |
window.VertexAttribute = Filament.VertexAttribute; |
| 31 |
window.AttributeType = Filament.VertexBuffer$AttributeType; |
| 32 |
window.PrimitiveType = Filament.RenderableManager$PrimitiveType; |
| 33 |
window.IndexType = Filament.IndexBuffer$IndexType; |
| 34 |
window.Fov = Filament.Camera$Fov; |
| 35 |
window.LightType = Filament.LightManager$Type; |
| 36 |
window.app = new App(document.getElementsByTagName('canvas')[0]); |
| 37 |
}); |
| 38 |
|
| 39 |
class App { |
| 40 |
constructor(canvas) { |
| 41 |
this.canvas = canvas; |
| 42 |
const engine = this.engine = Filament.Engine.create(this.canvas); |
| 43 |
this.scene = engine.createScene(); |
| 44 |
|
| 45 |
const sunlight = Filament.EntityManager.get().create(); |
| 46 |
Filament.LightManager.Builder(LightType.SUN) |
| 47 |
.color([0.98, 0.92, 0.89]) |
| 48 |
.intensity(100000.0) |
| 49 |
.direction([0.6, -1.0, -0.8]) |
| 50 |
.castShadows(true) |
| 51 |
.sunAngularRadius(1.9) |
| 52 |
.sunHaloSize(10.0) |
| 53 |
.sunHaloFalloff(80.0) |
| 54 |
.build(engine, sunlight); |
| 55 |
this.scene.addEntity(sunlight); |
| 56 |
|
| 57 |
const indirectLight = this.ibl = engine.createIblFromKtx(iblfile); |
| 58 |
this.scene.setIndirectLight(indirectLight); |
| 59 |
|
| 60 |
const radians = 1.0; |
| 61 |
indirectLight.setRotation(mat3.fromRotation(mat3.create(), radians, [0, 1, 0])) |
| 62 |
indirectLight.setIntensity(10000); |
| 63 |
|
| 64 |
const skybox = engine.createSkyFromKtx(skyfile); |
| 65 |
this.scene.setSkybox(skybox); |
| 66 |
|
| 67 |
const material = engine.createMaterial('parquet.filamat'); |
| 68 |
const matinstance = material.createInstance(); |
| 69 |
|
| 70 |
const sampler = new Filament.TextureSampler( |
| 71 |
Filament.MinFilter.LINEAR_MIPMAP_LINEAR, |
| 72 |
Filament.MagFilter.LINEAR, |
| 73 |
Filament.WrapMode.REPEAT); |
| 74 |
|
| 75 |
const ao = engine.createTextureFromPng('floor_ao_roughness_metallic.png'); |
| 76 |
const basecolor = engine.createTextureFromJpeg('floor_basecolor.jpg', {'srgb': true}); |
| 77 |
const normal = engine.createTextureFromPng('floor_normal.png'); |
| 78 |
matinstance.setTextureParameter('aoRoughnessMetallic', ao, sampler) |
| 79 |
matinstance.setTextureParameter('baseColor', basecolor, sampler) |
| 80 |
matinstance.setTextureParameter('normal', normal, sampler) |
| 81 |
|
| 82 |
const mesh = engine.loadFilamesh('shader_ball.filamesh', matinstance); |
| 83 |
this.shaderball = mesh.renderable; |
| 84 |
this.scene.addEntity(mesh.renderable); |
| 85 |
|
| 86 |
this.swapChain = engine.createSwapChain(); |
| 87 |
this.renderer = engine.createRenderer(); |
| 88 |
this.camera = engine.createCamera(Filament.EntityManager.get().create()); |
| 89 |
this.view = engine.createView(); |
| 90 |
this.view.setCamera(this.camera); |
| 91 |
this.view.setScene(this.scene); |
| 92 |
this.resize(); |
| 93 |
this.render = this.render.bind(this); |
| 94 |
this.resize = this.resize.bind(this); |
| 95 |
window.addEventListener('resize', this.resize); |
| 96 |
window.requestAnimationFrame(this.render); |
| 97 |
} |
| 98 |
|
| 99 |
render() { |
| 100 |
const radians = Date.now() / 1000; |
| 101 |
const transform = mat4.fromRotation(mat4.create(), radians, [0, 1, 0]); |
| 102 |
const tcm = this.engine.getTransformManager(); |
| 103 |
const inst = tcm.getInstance(this.shaderball); |
| 104 |
tcm.setTransform(inst, transform); |
| 105 |
inst.delete(); |
| 106 |
this.renderer.render(this.swapChain, this.view); |
| 107 |
window.requestAnimationFrame(this.render); |
| 108 |
} |
| 109 |
|
| 110 |
resize() { |
| 111 |
const dpr = window.devicePixelRatio; |
| 112 |
const width = this.canvas.width = window.innerWidth * dpr; |
| 113 |
const height = this.canvas.height = window.innerHeight * dpr; |
| 114 |
this.view.setViewport([0, 0, width, height]); |
| 115 |
const eye = [0, 1.8, 5], center = [0, 1, -1], up = [0, 1, 0]; |
| 116 |
this.camera.lookAt(eye, center, up); |
| 117 |
const aspect = width / height; |
| 118 |
const fov = aspect < 1 ? Fov.HORIZONTAL : Fov.VERTICAL; |
| 119 |
this.camera.setProjectionFov(45, aspect, 1.0, 10.0, fov); |
| 120 |
} |
| 121 |
} |
| 122 |
</script> |
| 123 |
</body> |
| 124 |
</html> |