-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add click handler and two more examples
- Loading branch information
Showing
21 changed files
with
410 additions
and
177 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<title>three.js</title> | ||
<script type="module" crossorigin src="/locar.js/assets/03-ar-objects-D3M74PcG.js"></script> | ||
<link rel="modulepreload" crossorigin href="/locar.js/assets/locar.es-D2pHxP8J.js"> | ||
</head> | ||
<body> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<title>three.js</title> | ||
<script type="module" crossorigin src="/locar.js/assets/04-api-communication-B3laukjY.js"></script> | ||
<link rel="modulepreload" crossorigin href="/locar.js/assets/locar.es-D2pHxP8J.js"> | ||
</head> | ||
<body> | ||
</body> | ||
</html> |
2 changes: 1 addition & 1 deletion
2
docs/assets/01-helloworld-B_XJysGc.js → docs/assets/01-helloworld-BK54cGeW.js
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
2 changes: 1 addition & 1 deletion
2
docs/assets/02-gps-and-sensors-Bz5hD0d7.js → docs/assets/02-gps-and-sensors-La4_GML4.js
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
332 changes: 166 additions & 166 deletions
332
docs/assets/locar.es-K2XRG_mr.js → docs/assets/locar.es-D2pHxP8J.js
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<title>three.js</title> | ||
<script type='module' src='src/main.js'></script> | ||
</head> | ||
<body> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import * as THREE from 'three'; | ||
import * as LocAR from 'locar'; | ||
|
||
const camera = new THREE.PerspectiveCamera(80, window.innerWidth/window.innerHeight, 0.001, 1000); | ||
const renderer = new THREE.WebGLRenderer(); | ||
renderer.setSize(window.innerWidth, window.innerHeight); | ||
const scene = new THREE.Scene(); | ||
|
||
|
||
document.body.appendChild(renderer.domElement); | ||
|
||
|
||
window.addEventListener("resize", e => { | ||
renderer.setSize(window.innerWidth, window.innerHeight); | ||
camera.aspect = window.innerWidth / window.innerHeight; | ||
camera.updateProjectionMatrix(); | ||
}); | ||
|
||
const locar = new LocAR.LocationBased(scene, camera); | ||
|
||
const cam = new LocAR.WebcamRenderer(renderer); | ||
|
||
|
||
let firstPosition = true; | ||
const oneDegAsRad = THREE.MathUtils.degToRad(1.0); | ||
|
||
const indexedObjects = { }; | ||
|
||
const cube = new THREE.BoxGeometry(20, 20, 20); | ||
|
||
let mouseDown = false, initX; | ||
let curRotation = 0.0; | ||
|
||
const clickHandler = new LocAR.ClickHandler(renderer); | ||
|
||
renderer.domElement.addEventListener("mousedown", e=> { | ||
mouseDown = true; | ||
initX = e.clientX; | ||
}); | ||
|
||
renderer.domElement.addEventListener("mousemove", e=> { | ||
if(mouseDown) { | ||
curRotation += e.clientX > initX ? oneDegAsRad*10: -oneDegAsRad*10; | ||
if(curRotation > Math.PI) { | ||
curRotation -= 2*Math.PI; | ||
} else if(curRotation < -Math.PI) { | ||
curRotation += 2*Math.PI; | ||
} | ||
camera.rotation.set(0, curRotation, 0); | ||
} | ||
}); | ||
|
||
renderer.domElement.addEventListener("mouseup", e=> { | ||
mouseDown = false; | ||
|
||
}); | ||
|
||
locar.on("gpsupdate", async(pos, distMoved) => { | ||
|
||
// Even if you have static AR objects, as opposed to objects from an | ||
// API, you must wait until the first GPS update before adding the | ||
// objects. This is because the internal x, y and z coordinates of each | ||
// object are relative to the initial position, and if we do not yet | ||
// have a GPS position, the initial position will be unknown. | ||
if(firstPosition) { | ||
firstPosition = false; | ||
const guildhall = new THREE.Mesh( | ||
cube, | ||
new THREE.MeshBasicMaterial({color: 0x00ffff}) | ||
); | ||
const oneills = new THREE.Mesh( | ||
cube, | ||
new THREE.MeshBasicMaterial({color: 0xff0000}) | ||
); | ||
|
||
locar.add(guildhall, -1.406392, 50.908042, 0, { "name": "Guildhall"} ); | ||
locar.add(oneills, -1.404340, 50.907330, 0, { "name": "O'Neills"} ); | ||
} | ||
|
||
}); | ||
locar.fakeGps(-1.404555, 50.908015); | ||
|
||
renderer.setAnimationLoop(animate); | ||
|
||
function animate() { | ||
cam.update(); | ||
const objects = clickHandler.raycast(camera, scene); | ||
if(objects.length) { | ||
alert(`This is ${objects[0].object.properties.name}`); | ||
} | ||
renderer.render(scene, camera); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<title>three.js</title> | ||
<script type='module' src='src/main.js'></script> | ||
</head> | ||
<body> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import * as THREE from 'three'; | ||
import * as LocAR from 'locar'; | ||
|
||
const camera = new THREE.PerspectiveCamera(80, window.innerWidth/window.innerHeight, 0.001, 1000); | ||
const renderer = new THREE.WebGLRenderer(); | ||
renderer.setSize(window.innerWidth, window.innerHeight); | ||
const scene = new THREE.Scene(); | ||
|
||
|
||
document.body.appendChild(renderer.domElement); | ||
|
||
|
||
window.addEventListener("resize", e => { | ||
renderer.setSize(window.innerWidth, window.innerHeight); | ||
camera.aspect = window.innerWidth / window.innerHeight; | ||
camera.updateProjectionMatrix(); | ||
}); | ||
|
||
const locar = new LocAR.LocationBased(scene, camera); | ||
|
||
const deviceControls = new LocAR.DeviceOrientationControls(camera); | ||
|
||
const cam = new LocAR.WebcamRenderer(renderer); | ||
|
||
|
||
let firstPosition = true; | ||
|
||
const indexedObjects = { }; | ||
|
||
const cube = new THREE.BoxGeometry(20, 20, 20); | ||
|
||
const clickHandler = new LocAR.ClickHandler(renderer); | ||
|
||
locar.on("gpsupdate", async(pos, distMoved) => { | ||
|
||
if(firstPosition || distMoved > 100) { | ||
|
||
const response = await fetch(`https://hikar.org/webapp/map?bbox=${pos.coords.longitude-0.02},${pos.coords.latitude-0.02},${pos.coords.longitude+0.02},${pos.coords.latitude+0.02}&layers=poi&outProj=4326`); | ||
const pois = await response.json(); | ||
|
||
pois.features.forEach ( poi => { | ||
if(!indexedObjects[poi.properties.osm_id]) { | ||
const mesh = new THREE.Mesh( | ||
cube, | ||
new THREE.MeshBasicMaterial({color: 0xff0000}) | ||
); | ||
|
||
locar.add(mesh, poi.geometry.coordinates[0], poi.geometry.coordinates[1], 0, poi.properties); | ||
indexedObjects[poi.osm_id] = mesh; | ||
} | ||
}); | ||
firstPosition = false; | ||
} | ||
|
||
}); | ||
locar.startGps(); | ||
|
||
renderer.setAnimationLoop(animate); | ||
|
||
function animate() { | ||
cam.update(); | ||
deviceControls.update(); | ||
const objects = clickHandler.raycast(scene, camera); | ||
if(objects.length) { | ||
alert(`This is ${objects[0].object.properties.name}`); | ||
} | ||
renderer.render(scene, camera); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
import { LocationBased } from "./src/location-based.js"; | ||
import { WebcamRenderer } from "./src/webcam-renderer.js"; | ||
import { DeviceOrientationControls } from "./src/device-orientation-controls.js"; | ||
import { ClickHandler } from './src/click-handler.js'; | ||
|
||
export { LocationBased, WebcamRenderer, DeviceOrientationControls }; | ||
export { LocationBased, WebcamRenderer, DeviceOrientationControls, ClickHandler }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import * as THREE from 'three'; | ||
|
||
class ClickHandler { | ||
|
||
constructor(renderer) { | ||
this.raycaster = new THREE.Raycaster(); | ||
this.normalisedMousePosition = new THREE.Vector2(null, null); | ||
renderer.domElement.addEventListener("click", e => { | ||
this.normalisedMousePosition.set ( | ||
((e.clientX / renderer.domElement.clientWidth) * 2) - 1, | ||
-((e.clientY / renderer.domElement.clientHeight) * 2) + 1 | ||
); | ||
}); | ||
} | ||
|
||
|
||
raycast(camera, scene) { | ||
if(this.normalisedMousePosition.x !== null && this.normalisedMousePosition.y !== null) { | ||
this.raycaster.setFromCamera(this.normalisedMousePosition, camera); | ||
const objects = this.raycaster.intersectObjects(scene.children, false); | ||
this.normalisedMousePosition.set(null, null); | ||
return objects; | ||
} | ||
return []; | ||
} | ||
} | ||
|
||
export { ClickHandler }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters