-
Notifications
You must be signed in to change notification settings - Fork 44
/
basic-mesh.js
100 lines (87 loc) · 3.13 KB
/
basic-mesh.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import { Beam, ResourceTypes } from '../../../src/index.js'
import { LambertLighting } from '../../shaders/basic-lighting-shader.js'
import { BasicImage } from '../../shaders/image-filter-shaders.js'
import {
createBall,
createRect,
mergeGraphics,
} from '../../utils/graphics-utils.js'
import { createCamera } from '../../utils/camera.js'
import { create, rotate } from '../../utils/mat4.js'
const { VertexBuffers, IndexBuffer, Uniforms, Textures } = ResourceTypes
const canvas = document.querySelector('canvas')
canvas.height = document.body.offsetHeight
canvas.width = document.body.offsetWidth
const beam = new Beam(canvas)
const lightingShader = beam.shader(LambertLighting)
const imageShader = beam.shader(BasicImage)
const cameraMats = createCamera({ eye: [0, 0, 50] }, { canvas })
const matrices = beam.resource(Uniforms, cameraMats)
const light = beam.resource(Uniforms)
light.set('dirLight.direction', [0, 0, 1])
const ball = createBall()
const rect = createRect([0, 0, -3], 1, 5)
const graphics = mergeGraphics(ball, rect)
const graphicsBuffers = [
beam.resource(VertexBuffers, graphics.vertex),
beam.resource(IndexBuffer, graphics.index),
]
const target = beam.target(2048, 2048)
const textures = beam.resource(Textures)
// screen quad
const quad = createRect()
const quadBuffers = [
beam.resource(VertexBuffers, quad.vertex),
beam.resource(IndexBuffer, quad.index),
]
const render = () => {
beam.clear()
target.use(() => {
beam.draw(lightingShader, ...graphicsBuffers, matrices, light)
})
textures.set('img', target.texture)
beam.draw(imageShader, ...quadBuffers, textures)
// default draw to screen
// beam.clear().draw(lightingShader, ...graphicsBuffers, matrices, light)
}
render()
const $modelX = document.getElementById('model-x')
const $modelY = document.getElementById('model-y')
const $modelZ = document.getElementById('model-z')
;[$modelX, $modelY, $modelZ].forEach((input) => {
input.addEventListener('input', () => {
const [rx, ry, rz] = [$modelX.value, $modelY.value, $modelZ.value]
const modelMat = create()
rotate(modelMat, modelMat, (rx / 180) * Math.PI, [1, 0, 0])
rotate(modelMat, modelMat, (ry / 180) * Math.PI, [0, 1, 0])
rotate(modelMat, modelMat, (rz / 180) * Math.PI, [0, 0, 1])
matrices.set('modelMat', modelMat)
render()
})
})
const $dirX = document.getElementById('dir-x')
const $dirY = document.getElementById('dir-y')
const $dirZ = document.getElementById('dir-z')
;[$dirX, $dirY, $dirZ].forEach((input) => {
input.addEventListener('input', () => {
const [dx, dy, dz] = [$dirX.value, $dirY.value, $dirZ.value]
light.set('dirLight.direction', [dx, dy, dz])
render()
})
})
const $dirStrength = document.getElementById('dir-strength')
$dirStrength.addEventListener('input', () => {
light.set('dirLight.strength', $dirStrength.value)
render()
})
const $dirColor = document.getElementById('dir-color')
$dirColor.addEventListener('input', () => {
const hex = $dirColor.value
const rgb = [
parseInt(hex.slice(1, 3), 16) / 256,
parseInt(hex.slice(3, 5), 16) / 256,
parseInt(hex.slice(5, 7), 16) / 256,
]
light.set('dirLight.color', rgb)
render()
})