Skip to content

Commit

Permalink
Updated BabylonJS example and gettingstarted projects
Browse files Browse the repository at this point in the history
  • Loading branch information
atomarch authored Apr 16, 2019
1 parent 90d37aa commit 070291b
Show file tree
Hide file tree
Showing 18 changed files with 267 additions and 473 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Built entirely using standards-compliant JavaScript and WebGL, 8th Wall Web is a
# Examples

* [A-Frame Examples](https://github.com/8thwall/web/tree/master/examples/aframe)(Recommended to start)
* [Babylon.js Examples](https://github.com/8thwall/web/tree/master/examples/babylonjs)
* [three.js Examples](https://github.com/8thwall/web/tree/master/examples/threejs)
* [Camera Pipeline Examples](https://github.com/8thwall/web/tree/master/examples/camerapipeline)
* [Amazon Sumerian Setup](https://github.com/8thwall/web/tree/master/gettingstarted/xrsumerian)
12 changes: 10 additions & 2 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ Example 8th Wall Web projects, by 3D framework

* [Capture a photo](https://github.com/8thwall/web/tree/master/examples/aframe/capturephoto) - This example allows the user to capture photo evidence of a UFO abduction.

After following the steps above, you should have fully functional versions of:

Tap to place | Toss Object | Portal | Manipulate | Capture a photo
:----------: | :---------: | :----: | :--------: | :-------------:
![tapplace-screenshot](../images/screenshot-tap.jpg) | ![toss-screenshot](../images/screenshot-toss.jpg) | ![portal-screenshot](../images/screenshot-portal.jpg) | ![manipulate-screenshot](../images/screenshot-manipulate.jpg) | ![capture-screenshot](../images/screenshot-capture.jpg)
Expand All @@ -34,6 +32,16 @@ or scan on phone:<br> ![QR1](../images/qr-placeground.png) | or scan on phone:<b
| [Try Demo (mobile)](https://apps.8thwall.com/8thWall/aframe_artgallery) | [Try Demo (mobile)](https://apps.8thwall.com/8thWall/aframe_flyer)
| or scan on phone:<br> ![QR1](../images/qr-artgallery.png) | or scan on phone:<br> ![QR1](../images/qr-flyer.png)

## Babylon.js

* [Tap to place](https://github.com/8thwall/web/tree/master/examples/babylonjs/placeground) - This interactive example allows the user to grow trees on the ground by tapping. This showcases raycasting, instantiating objects, importing 3D models, and animation.

Tap to place
:----------:
![tapplace-threejs-screenshot](../images/screenshot-tap.jpg)
[Try Demo (mobile)](https://apps.8thwall.com/8thWall/babylonjs_placeground)
or scan on phone:<br> ![QR1](../images/qr-babylonjs-placeground.png)


## three.js

Expand Down
11 changes: 11 additions & 0 deletions examples/babylonjs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# 8th Wall Web Examples - Babylon.js

Example 8th Wall Web projects using Babylon.js:

* [Tap to place](https://github.com/8thwall/web/tree/master/examples/babylonjs/placeground) - This interactive example allows the user to grow trees on the ground by tapping. This showcases raycasting, instantiating objects, importing 3D models, and animation.

Tap to place
:----------:
![tapplace-screenshot](../../images/screenshot-tap.jpg)
[Try Demo (mobile)](https://apps.8thwall.com/8thWall/babylonjs_placeground)
or scan on phone:<br> ![QR1](../../images/qr-babylonjs-placeground.png)
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,33 +1,30 @@
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>8th Wall Web: babylon.js</title>
<link rel="stylesheet" type="text/css" href="index.css">

<!-- Babylon.js -->
<script src="https://preview.babylonjs.com/babylon.js"></script>
<script src="https://preview.babylonjs.com/loaders/babylonjs.loaders.min.js"></script>

<!-- Javascript tweening engine -->
<script src="//cdnjs.cloudflare.com/ajax/libs/tween.js/16.3.5/Tween.min.js"></script>

<!-- XR Extras - provides utilities like load screen, almost there, and error handling.
See github.com/8thwall/web/xrextras -->
<script src="//cdn.8thwall.com/web/xrextras/xrextras.js"></script>

<!-- 8thWall Web - Replace the app key here with your own app key -->
<script async src="//apps.8thwall.com/xrweb?appKey=XXXXXXXX"></script>

<!-- Experimental babylonjs module for 8th Wall Web -->
<script src="xr-babylonjs.js"></script>

<!-- client code -->
<script src="index.js"></script>

</head>
<body>
<canvas id="renderCanvas"></canvas>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>8th Wall Web: babylon.js</title>
<link rel="stylesheet" type="text/css" href="index.css">

<!-- Babylon.js -->
<script src="https://preview.babylonjs.com/babylon.js"></script>
<script src="https://preview.babylonjs.com/loaders/babylonjs.loaders.min.js"></script>

<!-- Javascript tweening engine -->
<script src="//cdnjs.cloudflare.com/ajax/libs/tween.js/16.3.5/Tween.min.js"></script>

<!-- XR Extras - provides utilities like load screen, almost there, and error handling.
See github.com/8thwall/web/xrextras -->
<script src="//cdn.8thwall.com/web/xrextras/xrextras.js"></script>

<!-- 8thWall Web - Replace the app key here with your own app key -->
<script async src="//apps.8thwall.com/xrweb?appKey=XXXXXXXX"></script>

<!-- client code -->
<script src="index.js"></script>

</head>
<body>
<canvas id="renderCanvas"></canvas>
</body>
</html>
124 changes: 124 additions & 0 deletions examples/babylonjs/placeground/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
const modelRootURL = './' // Directory where 3D model lives
const modelFile = 'tree.glb' // 3D model to spawn at tap
const startScale = new BABYLON.Vector3(0.01, 0.01, 0.01) // Initial scale value for our model
const endScale = new BABYLON.Vector3(0.05, 0.05, 0.05) // Ending scale value for our model
const animationMillis = 750 // Animate over 0.75 seconds

let surface, engine, scene, camera

// Populates some object into an XR scene and sets the initial camera position.
const initXrScene = ({ scene, camera }) => {
console.log('initXrScene')

const directionalLight = new BABYLON.DirectionalLight("DirectionalLight", new BABYLON.Vector3(0, -1, 1), scene)
directionalLight.intensity = 1.0

const ground = BABYLON.Mesh.CreatePlane('ground', 100, scene)
ground.rotation.x = Math.PI / 2
ground.material = new BABYLON.StandardMaterial("groundMaterial", scene)
ground.material.alpha = 0
surface = ground

// Set the initial camera position relative to the scene we just laid out. This must be at a
// height greater than y=0.
camera.position = new BABYLON.Vector3(0,3,5)
}

const placeObjectTouchHandler = (e) => {
// console.log('placeObjectTouchHandler')
// Call XrController.recenter() when the canvas is tapped with two fingers. This resets the
// AR camera to the position specified by XrController.updateCameraProjectionMatrix() above.
if (e.touches.length == 2) {
XR.XrController.recenter()
}

if (e.touches.length > 2) {
return
}

// If the canvas is tapped with one finger and hits the "surface", spawn an object.

const pickResult = scene.pick(e.touches[0].clientX, e.touches[0].clientY)
if (pickResult.hit && pickResult.pickedMesh == surface) {

const gltf = BABYLON.SceneLoader.LoadAssetContainer(
modelRootURL,
modelFile,
scene,
function (container) { // onSuccess
const scale = Object.assign({}, startScale)
const yRot = Math.random() * 360
for (i = 0; i < container.meshes.length; i++) {
container.meshes[i]._position.x = pickResult.pickedPoint.x
container.meshes[i]._position.z = pickResult.pickedPoint.z
container.meshes[i]._rotation.y = yRot
container.meshes[i]._scaling.x = scale.x
container.meshes[i]._scaling.y = scale.y
container.meshes[i]._scaling.z = scale.z
}
// Adds all elements to the scene
container.addAllToScene()

new TWEEN.Tween(scale)
.to(endScale, animationMillis)
.easing(TWEEN.Easing.Elastic.Out) // Use an easing function to make the animation smooth.
.onUpdate(() => {
for (i = 0; i < container.meshes.length; i++) {
container.meshes[i]._scaling.x = scale.x
container.meshes[i]._scaling.y = scale.y
container.meshes[i]._scaling.z = scale.z
}
})
.start() // Start the tween immediately.
},
function (xhr) { //onProgress
console.log(`${(xhr.loaded / xhr.total * 100 )}% loaded`)
},
function (error) { //onError
console.log('Error loading model')
},
)
}
}

const startScene = () => {
const canvas = document.getElementById('renderCanvas')

engine = new BABYLON.Engine(canvas, true, { stencil: true, preserveDrawingBuffer: true })
engine.enableOfflineSupport = false

scene = new BABYLON.Scene(engine)
camera = new BABYLON.FreeCamera('camera', new BABYLON.Vector3(0, 0, 0), scene)

initXrScene({ scene, camera }) // Add objects to the scene and set starting camera position.

// Connect the camera to the XR engine and show camera feed
camera.addBehavior(XR.Babylonjs.xrCameraBehavior())

canvas.addEventListener('touchstart', placeObjectTouchHandler, true) // Add touch listener.

engine.runRenderLoop(() => {
// Enable TWEEN animations.
TWEEN.update(performance.now())
scene.render()
})

window.addEventListener('resize', () => {
engine.resize()
})
}

const onxrloaded = () => {
XR.addCameraPipelineModules([ // Add camera pipeline modules.
XRExtras.AlmostThere.pipelineModule(), // Detects unsupported browsers and gives hints.
XRExtras.FullWindowCanvas.pipelineModule(), // Modifies the canvas to fill the window.
XRExtras.Loading.pipelineModule(), // Manages the loading screen on startup.
XRExtras.RuntimeError.pipelineModule(), // Shows an error image on runtime error.
])

startScene()
}

// Show loading screen before the full XR library has been loaded.
const load = () => { XRExtras.Loading.showLoading({onxrloaded}) }
window.onload = () => { window.XRExtras ? load() : window.addEventListener('xrextrasloaded', load) }
File renamed without changes.
135 changes: 0 additions & 135 deletions experimental/babylonjs/examples/placeground/index.js

This file was deleted.

Loading

0 comments on commit 070291b

Please sign in to comment.