World Tracking — Babylon.js
Integración de Monolook World con Babylon.js para detectar superficies y anclar contenido 3D.
Requisitos
- Babylon.js cargado vía CDN (
window.BABYLON) o como módulo npm - License key de Monolook World
- HTTPS + Chrome en Android, o Monolook App / AppClip en iOS
HTML
html
<script src="https://cdn.babylonjs.com/babylon.js"></script>
<canvas id="scene"></canvas>Setup
js
import { Monolook } from 'monolook/world'
const BABYLON = window.BABYLON
const canvas = document.getElementById('scene')
const engine = new BABYLON.Engine(canvas, true, {
alpha: true,
premultipliedAlpha: false,
preserveDrawingBuffer: false,
stencil: true,
})
const scene = new BABYLON.Scene(engine)
scene.clearColor = new BABYLON.Color4(0.2, 0.2, 0.2, 1)
scene.useRightHandedSystem = true
const camera = new BABYLON.FreeCamera('camera', new BABYLON.Vector3(0, 1.2, 3), scene)
camera.minZ = 0.01
camera.maxZ = 100
camera.setTarget(BABYLON.Vector3.Zero())
const light = new BABYLON.HemisphericLight('light', new BABYLON.Vector3(0, 1, 0), scene)
light.intensity = 2.4
const cube = BABYLON.MeshBuilder.CreateBox('cube', { size: 0.35 }, scene)
cube.setEnabled(false)
const monolook = new Monolook({
adapter: 'babylon',
BABYLON,
engine,
scene,
camera,
domOverlayRoot: document.getElementById('dom_overlay'),
})
useRightHandedSystem = truees necesario para alinear correctamente las coordenadas.
Eventos e inicio
js
monolook.on('surfacefound', () => setStatus('Superficie detectada — toca para colocar'))
monolook.on('select', colocarCubo)
monolook.on('sessionend', resetTrasSession)
await monolook.start({ mode: 'surface', licenseKey: '<WORLD_LICENSE_KEY>' })
cube.setEnabled(false)
monolook.startTracking()
monolook.setRenderLoop(() => monolook.updateTracking())
await monolook.enterAR()Colocación
js
function colocarCubo() {
if (!monolook.isPresenting() || isPlaced) return
const pose = monolook.getIndicatorPose()
if (!pose?.visible) return
isPlaced = true
cube.position.set(pose.position.x, pose.position.y, pose.position.z)
cube.setEnabled(true)
monolook.pauseTracking()
monolook.setIndicatorVisible(false)
}Ejemplo completo
js
import { Monolook } from 'monolook/world'
const BABYLON = window.BABYLON
const canvas = document.getElementById('scene')
const engine = new BABYLON.Engine(canvas, true, { alpha: true, premultipliedAlpha: false, stencil: true })
const scene = new BABYLON.Scene(engine)
scene.clearColor = new BABYLON.Color4(0.2, 0.2, 0.2, 1)
scene.useRightHandedSystem = true
const camera = new BABYLON.FreeCamera('camera', new BABYLON.Vector3(0, 1.2, 3), scene)
camera.minZ = 0.01
camera.setTarget(BABYLON.Vector3.Zero())
new BABYLON.HemisphericLight('light', new BABYLON.Vector3(0, 1, 0), scene).intensity = 2.4
const cube = BABYLON.MeshBuilder.CreateBox('cube', { size: 0.35 }, scene)
cube.setEnabled(false)
const monolook = new Monolook({
adapter: 'babylon',
BABYLON, engine, scene, camera,
domOverlayRoot: document.getElementById('dom_overlay'),
})
let isPlaced = false
monolook.on('surfacefound', () => !isPlaced && setStatus('Superficie detectada — toca para colocar'))
monolook.on('select', colocarCubo)
monolook.on('sessionend', () => {
isPlaced = false
cube.setEnabled(true)
scene.render()
})
async function startAR() {
await monolook.start({ mode: 'surface', licenseKey: '<WORLD_LICENSE_KEY>' })
isPlaced = false
cube.setEnabled(false)
monolook.startTracking()
monolook.setRenderLoop(() => monolook.updateTracking())
await monolook.enterAR()
}
function colocarCubo() {
if (!monolook.isPresenting() || isPlaced) return
const pose = monolook.getIndicatorPose()
if (!pose?.visible) return
isPlaced = true
cube.position.set(pose.position.x, pose.position.y, pose.position.z)
cube.setEnabled(true)
monolook.pauseTracking()
monolook.setIndicatorVisible(false)
}
document.getElementById('startButton').addEventListener('click', startAR)
window.addEventListener('pointerup', colocarCubo)
engine.runRenderLoop(() => { if (!monolook.isPresenting()) scene.render() })