alibaba/tofu.js

Name: tofu.js

Owner: Alibaba

Description: The official web framework for WebAR.

Created: 2018-03-05 02:36:22.0

Updated: 2018-05-24 16:29:19.0

Pushed: 2018-05-23 09:29:13.0

Homepage:

Size: 114461

Language: JavaScript

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

tofu.js ? The 3D building framework

The official web framework for UC WebAR.

Introduction

tofu.js is a helper framework for building AR VR 3D application.

Getting Started
AR Case
rt { Mesh, BoxGeometry, MeshLambertMaterial } from 'three';
rt { ARViewer, ARGlue } from 'tofu.js';

t width = window.innerWidth;
t height = window.innerHeight;
t arViewer = new ARViewer({
nvas: '#webar', // `required` pass a `canvas-dom` or `css-selector`
dth,
ight,


ew a `AR-Glue`, it was glue container, can paste some 3d-model to video stream
t arGlue = new ARGlue({
me: 'glue-name',

dd a glue to scene
ewer.add(arGlue);

ssume `demoModel` was a 3d-model to tracker video stream
t demoModel = new Mesh(
w BoxGeometry(1, 1, 1),
w MeshLambertMaterial({ color: 0xffffff })

dd a 3d-model to `AR-Glue`
ue.add(demoModel);

et camera matrix, on get camera internal parameters
ewer.updateCameraMatrix(matrix);

et markers poses, on detector markers' poses
ewer.updatePoses(poses);

tart render
ewer.start();
XR Case
rt { Mesh, BoxGeometry, MeshLambertMaterial } from 'three';
rt { XRViewer } from 'tofu.js';
t xrViewer = new XRViewer({
nvas: '#webgl',
v: 75,
dth: WIDTH,
ight: HEIGHT,
ar: 0.01,
r: 3000,
mode: true, // turn on vr-mode ?
sensor: true, // turn on vr-sensor ?
Enable: true, // turn on after effect ?


reate a cube
t cube = new Mesh(
w BoxGeometry(1, 1, 1),
w MeshLambertMaterial({ color: 0xffffff })


ewer.add(cube);

tart render
ewer.start();
Animation Case

tofu.js add an animator patch for three.js, so you can start a animation very easy, this animator built-in support animate motion and runners type animations.

animate
t timeline = cube.animate({
om: { 'position.x': 0 }, // optional, play an animation start with this pose
: { 'position.x': 0 }, // play an animation to this pose
ration: 1000, // animation duration
peat: 1, // do you need repeat once?
finite: true, // you want infinite repeat this animation?
ternate: true, // you want repeat with alternate, just like yoyo?
se: Tween.Bounce.Out, // you want to use which timing function?

line.on('update', () => {}); // call on each update
line.on('complete', () => {}); // call on complete animation

line.pause(); // pause animation progress
line.restart(); // restart animation progress, use with pause
line.stop(); // stop animation to end, will trigger onComplete callback
line.cancle(); // cancle animation right now, will not trigger onComplete callback
line.timeScale = 0.5; // set timeScale, get a Slow motion?just like speed * 0.5
motion
t timeline = cube.motion({
th: curve, // should instance of Curve
ngthMode: false, // whether move with length-mode or not
tachTangent: false, // whether display-object should attach tangent or not
ration: 1, // animation duration
peat: 1, // do you need repeat once?
finite: true, // you want infinite repeat this animation?
ternate: true, // you want repeat with alternate, just like yoyo?
se: Tween.Bounce.Out, // you want to use which timing function?

imeline same with before
runners
t timeline = cube.runners({
nners: [], // combine animation, support `animate`?`motion`
peat: 1, // do you need repeat once?
finite: true, // you want infinite repeat this animation?
se: Tween.Bounce.Out, // you want to use which timing function?

imeline same with before
Interaction Case
ind interaction-event for cube, like click?touchstart and so on
.on('click', function(ev) {});
.on('touchstart', function(ev) {});
.on('mousedown', function(ev) {});
nd so on ...

you can also listen on parent-node or any display-tree node,source event will bubble up along with display-tree.you can stop the bubble-up by invoke ev.stopPropagation function.

AE Case
rt { BloomPass, FilmPass, FocusShader } from 'tofu.postprogressing.js';
rt { XRViewer, RenderPass, ShaderPass } from 'tofu.js';
t xrViewer = new XRViewer({
nvas: '#webgl',
v: 75,
dth: WIDTH,
ight: HEIGHT,
ar: 0.01,
r: 3000,
Enable: true, // turn on after effect ?


t renderModel = new RenderPass();
t effectBloom = new BloomPass(1.05);
t effectFilm = new FilmPass(0.15, 0.15, 1500, false);
t effectFocus = new ShaderPass(FocusShader);
ctFocus.uniforms["screenWidth"].value = WIDTH;
ctFocus.uniforms["screenHeight"].value = HEIGHT;
ctFocus.renderToScreen = true;

ewer.composer.addPass(renderModel);
ewer.composer.addPass(effectBloom);
ewer.composer.addPass(effectFilm);
ewer.composer.addPass(effectFocus);

ewer.start();
Documentation

run http-server and go to ./docs directory

run http
Examples

run http-server and go to ./examples directory

run http

This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.