agentscript
Version:
AgentScript Model in Model/View architecture
87 lines (73 loc) • 2.85 kB
HTML
<html>
<head>
<title>camera3d</title>
</head>
<body>
<script type="module">
import * as util from 'https://code.agentscript.org/src/utils.js'
import Color from 'https://code.agentscript.org/src/Color.js'
import ThreeDraw from 'https://code.agentscript.org/src/ThreeDraw.js'
import Animator from 'https://code.agentscript.org/src/Animator.js'
import Model from 'https://code.agentscript.org/models/Camera3DModel.js'
import dat from 'https://code.agentscript.org/vendor/dat.gui.js'
const isPixel = t => t.breed.name === 'pixels'
const uvToColor = t => {
const { u, v } = t
const r = Math.sqrt(u ** 2 + v ** 2)
return Color.toTypedColor(`hsl(${(r * 240) % 240}, 100%, 50%)`)
}
const drawOptions = {
turtlesMesh: { meshClass: 'Obj3DMesh', useAxes: true },
turtlesShape: t => (isPixel(t) ? 'Cube' : 'Dart'),
turtlesColor: t =>
isPixel(t) ? uvToColor(t) : Color.toTypedColor('red'),
turtlesSize: t => (isPixel(t) ? 0.35 : 3),
linksColor: l => uvToColor(l.end0),
}
const model = new Model()
await model.startup()
model.setup()
const view = new ThreeDraw(model, {
div: document.body,
// turtles: { meshClass: 'Obj3DMesh' },
drawOptions,
})
util.toWindow({ util, model, view })
const anim = new Animator(
() => {
model.step()
view.draw()
},
-1, //500,
30
)
const gui = runGui(model, view)
function runGui(model, view) {
const gui = new dat.GUI()
// Helper for adding variables to gui.
// "listen" to make reset values to appear in gui menu
// "onChange" to have model update camera for each change.
const guiAddVal = (obj, name, start, stop, step) => {
gui.add(obj, name, start, stop, step)
.listen()
.onChange(val => {
model.moveCamera()
})
}
guiAddVal(model, 'heading', -180, 180, 5)
guiAddVal(model, 'pitch', -180, 180, 5)
guiAddVal(model, 'roll', -180, 180, 5)
guiAddVal(model, 'headingDelta', 0, 5, 1)
guiAddVal(model, 'pitchDelta', 0, 5, 1)
guiAddVal(model, 'rollDelta', 0, 5, 1)
guiAddVal(model, 'sphereRadius', 0, 16, 1)
guiAddVal(model, 'fieldOfView', 1, 180, 1)
gui.add(model, 'toggleLinks')
gui.add(model, 'reset')
gui.add(anim, 'toggle')
return gui
}
</script>
<div id="modelDiv"></div>
</body>
</html>