Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
Volker Coors
Visualization
Commits
b6e48247
Commit
b6e48247
authored
1 year ago
by
Alfakhori
Browse files
Options
Download
Email Patches
Plain Diff
Update public/webXR/three.html
parent
a80bfe64
Pipeline
#7501
passed with stages
in 11 seconds
Changes
1
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
public/webXR/three.html
+47
-75
public/webXR/three.html
with
47 additions
and
75 deletions
+47
-75
public/webXR/three.html
+
47
-
75
View file @
b6e48247
...
...
@@ -16,81 +16,53 @@
<button
onclick=
"activateXR()"
>
Start Cube Demo
</button>
<script>
async
function
activateXR
()
{
// Add a canvas element and initialize a WebGL context that is compatible with WebXR.
const
canvas
=
document
.
createElement
(
"
canvas
"
);
document
.
body
.
appendChild
(
canvas
);
const
gl
=
canvas
.
getContext
(
"
webgl
"
,
{
xrCompatible
:
true
});
const
scene
=
new
THREE
.
Scene
();
// The cube will have a different color on each side.
const
materials
=
[
new
THREE
.
MeshBasicMaterial
({
color
:
0xff0000
}),
new
THREE
.
MeshBasicMaterial
({
color
:
0x0000ff
}),
new
THREE
.
MeshBasicMaterial
({
color
:
0x00ff00
}),
new
THREE
.
MeshBasicMaterial
({
color
:
0xff00ff
}),
new
THREE
.
MeshBasicMaterial
({
color
:
0x00ffff
}),
new
THREE
.
MeshBasicMaterial
({
color
:
0xffff00
})
];
// Create the cube and add it to the demo scene.
const
cube
=
new
THREE
.
Mesh
(
new
THREE
.
BoxBufferGeometry
(
0.5
,
0.5
,
0.5
),
materials
);
cube
.
position
.
set
(
0
,
0
,
-
1
);
scene
.
add
(
cube
);
// Set up the WebGLRenderer, which handles rendering to the session's base layer.
const
renderer
=
new
THREE
.
WebGLRenderer
({
alpha
:
true
,
preserveDrawingBuffer
:
true
,
canvas
:
canvas
,
context
:
gl
});
renderer
.
autoClear
=
false
;
// The API directly updates the camera matrices.
// Disable matrix auto updates so three.js doesn't attempt
// to handle the matrices independently.
const
camera
=
new
THREE
.
PerspectiveCamera
();
camera
.
matrixAutoUpdate
=
false
;
// Initialize a WebXR session using "immersive-ar".
const
session
=
await
navigator
.
xr
.
requestSession
(
"
immersive-ar
"
);
session
.
updateRenderState
({
baseLayer
:
new
XRWebGLLayer
(
session
,
gl
)
});
// A 'local' reference space has a native origin that is located
// near the viewer's position at the time the session was created.
const
referenceSpace
=
await
session
.
requestReferenceSpace
(
'
local
'
);
// Create a render loop that allows us to draw on the AR view.
const
onXRFrame
=
(
time
,
frame
)
=>
{
// Queue up the next draw request.
session
.
requestAnimationFrame
(
onXRFrame
);
// Bind the graphics framebuffer to the baseLayer's framebuffer
gl
.
bindFramebuffer
(
gl
.
FRAMEBUFFER
,
session
.
renderState
.
baseLayer
.
framebuffer
)
// Retrieve the pose of the device.
// XRFrame.getViewerPose can return null while the session attempts to establish tracking.
const
pose
=
frame
.
getViewerPose
(
referenceSpace
);
if
(
pose
)
{
// In mobile AR, we only have one view.
const
view
=
pose
.
views
[
0
];
const
viewport
=
session
.
renderState
.
baseLayer
.
getViewport
(
view
);
renderer
.
setSize
(
viewport
.
width
,
viewport
.
height
)
// Use the view's transform matrix and projection matrix to configure the THREE.camera.
camera
.
matrix
.
fromArray
(
view
.
transform
.
matrix
)
camera
.
projectionMatrix
.
fromArray
(
view
.
projectionMatrix
);
camera
.
updateMatrixWorld
(
true
);
// Render the scene with THREE.WebGLRenderer.
renderer
.
render
(
scene
,
camera
)
}
}
session
.
requestAnimationFrame
(
onXRFrame
);
// ------------------------------------------------
// BASIC SETUP
// ------------------------------------------------
// Create an empty scene
var
scene
=
new
THREE
.
Scene
();
// Create a basic perspective camera
var
camera
=
new
THREE
.
PerspectiveCamera
(
75
,
window
.
innerWidth
/
window
.
innerHeight
,
0.1
,
1000
);
camera
.
position
.
z
=
4
;
// Create a renderer with Antialiasing
var
renderer
=
new
THREE
.
WebGLRenderer
({
antialias
:
true
});
// Configure renderer clear color
renderer
.
setClearColor
(
"
#000000
"
);
// Configure renderer size
renderer
.
setSize
(
window
.
innerWidth
,
window
.
innerHeight
);
// Append Renderer to DOM
document
.
body
.
appendChild
(
renderer
.
domElement
);
// ------------------------------------------------
// FUN STARTS HERE
// ------------------------------------------------
// Create a Cube Mesh with basic material
var
geometry
=
new
THREE
.
BoxGeometry
(
1
,
1
,
1
);
var
material
=
new
THREE
.
MeshBasicMaterial
(
{
color
:
"
#433F81
"
}
);
var
cube
=
new
THREE
.
Mesh
(
geometry
,
material
);
// Add cube to Scene
scene
.
add
(
cube
);
// Render Loop
var
render
=
function
()
{
requestAnimationFrame
(
render
);
cube
.
rotation
.
x
+=
0.01
;
cube
.
rotation
.
y
+=
0.01
;
// Render the scene
renderer
.
render
(
scene
,
camera
);
};
render
();
}
</script>
...
...
This diff is collapsed.
Click to expand it.
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment