Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial WebXR Support #670

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions example/webxr/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<head>
<style> body { margin: 0; } </style>

<!-- <script src="//unpkg.com/3d-force-graph"></script> -->
<script src="../../dist/3d-force-graph.js"></script>
</head>

<body>
<!-- <div id="loadingIndicator">
Loading...
</div> -->

<div id="3d-graph"></div>

<script>
// window.onerror = (e) => {
// document.querySelector('#loadingIndicator').style.color = 'red';
// document.querySelector('#loadingIndicator').innerHTML = 'Error' + e;
// }

// Random tree
const N = 300;
const gData = {
nodes: [...Array(N).keys()].map(i => ({ id: i })),
links: [...Array(N).keys()]
.filter(id => id)
.map(id => ({
source: id,
target: Math.round(Math.random() * (id - 1))
}))
};

const Graph = ForceGraph3D()
(document.getElementById('3d-graph'))
// .nodeColor(() => "magenta")
.enableWebXR(true)
.graphData(gData);
</script>
</body>
22 changes: 12 additions & 10 deletions src/3d-force-graph.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const three = window.THREE
: { AmbientLight, DirectionalLight, Vector3 };

import { DragControls as ThreeDragControls } from 'three/examples/jsm/controls/DragControls.js';
import { XRButton as ThreeXRButton } from 'three/addons/webxr/XRButton.js';

import ThreeForceGraph from 'three-forcegraph';
import ThreeRenderObjects from 'three-render-objects';
Expand Down Expand Up @@ -124,6 +125,7 @@ export default Kapsule({
triggerUpdate: false
},
enableNodeDrag: { default: true, triggerUpdate: false },
enableWebXR: { default: true, triggerUpdate: false },
onNodeDrag: { default: () => {}, triggerUpdate: false },
onNodeDragEnd: { default: () => {}, triggerUpdate: false },
onNodeClick: { triggerUpdate: false },
Expand All @@ -148,20 +150,15 @@ export default Kapsule({
return this;
},
pauseAnimation: function(state) {
if (state.animationFrameRequestId !== null) {
cancelAnimationFrame(state.animationFrameRequestId);
state.animationFrameRequestId = null;
}
this.renderer().setAnimationLoop(null);
return this;
},

resumeAnimation: function(state) {
if (state.animationFrameRequestId === null) {
this._animationCycle();
}
this.renderer().setAnimationLoop(this._render);
return this;
},
_animationCycle(state) {
_render(state) {
if (state.enablePointerInteraction) {
// reset canvas cursor (override dragControls cursor)
this.renderer().domElement.style.cursor = null;
Expand All @@ -170,7 +167,6 @@ export default Kapsule({
// Frame cycle
state.forceGraph.tickFrame();
state.renderObjs.tick();
state.animationFrameRequestId = requestAnimationFrame(this._animationCycle);
},
scene: state => state.renderObjs.scene(), // Expose scene
camera: state => state.renderObjs.camera(), // Expose camera
Expand Down Expand Up @@ -420,8 +416,14 @@ export default Kapsule({

//

// config WebXR
if (state.enableWebXR) {
document.body.appendChild( ThreeXRButton.createButton( renderer ) );
renderer.xr.enabled = true;
}

// Kick-off renderer
this._animationCycle();
this.renderer().setAnimationLoop(this._render);
}
});

Expand Down