Skip to content
GitLab
Explore
Projects
Groups
Snippets
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
Esguerra Montana
Web AR
Commits
74e48dbc
Commit
74e48dbc
authored
1 year ago
by
Esguerra Montana
Browse files
Options
Download
Email Patches
Plain Diff
Update index.html
parent
0b9d5be5
Pipeline
#9423
passed with stage
in 8 seconds
Changes
1
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
public/index.html
+74
-65
public/index.html
with
74 additions
and
65 deletions
+74
-65
public/index.html
+
74
-
65
View file @
74e48dbc
...
@@ -4,7 +4,7 @@
...
@@ -4,7 +4,7 @@
<meta
charset=
"UTF-8"
>
<meta
charset=
"UTF-8"
>
<meta
name=
"viewport"
<meta
name=
"viewport"
content=
"width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"
>
content=
"width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"
>
<title>
T
orus
Demo
</title>
<title>
T
etrahedron
Demo
</title>
<!-- three.js -->
<!-- three.js -->
<script
src=
"https://unpkg.com/three@0.126.0/build/three.js"
></script>
<script
src=
"https://unpkg.com/three@0.126.0/build/three.js"
></script>
...
@@ -13,7 +13,7 @@
...
@@ -13,7 +13,7 @@
<!-- Starting an immersive WebXR session requires user interaction.
<!-- Starting an immersive WebXR session requires user interaction.
We start this one with a simple button. -->
We start this one with a simple button. -->
<button
onclick=
"activateXR()"
>
Start T
orus
</button>
<button
onclick=
"activateXR()"
>
Start T
etrahedron
</button>
<script>
<script>
async
function
activateXR
()
{
async
function
activateXR
()
{
// Add a canvas element and initialize a WebGL context that is compatible with WebXR.
// Add a canvas element and initialize a WebGL context that is compatible with WebXR.
...
@@ -23,70 +23,79 @@ async function activateXR() {
...
@@ -23,70 +23,79 @@ async function activateXR() {
const
scene
=
new
THREE
.
Scene
();
const
scene
=
new
THREE
.
Scene
();
// Color of the torus geometry.
// Add lighting to the scene
const
material
=
new
THREE
.
MeshPhysicalMaterial
({
color
:
0xff0000
,
roughness
:
0.5
,
metalness
:
0.5
,
clearcoat
:
1.0
,
clearcoatRoughness
:
0.1
});
const
light
=
new
THREE
.
DirectionalLight
(
0xffffff
,
1
);
light
.
position
.
set
(
1
,
1
,
1
).
normalize
();
// Create the torus and add it to the demo scene.
scene
.
add
(
light
);
const
torus
=
new
THREE
.
Mesh
(
new
THREE
.
TetrahedronGeometry
(
1
),
material
);
//position the next geometry (sphere)
const
ambientLight
=
new
THREE
.
AmbientLight
(
0x404040
);
// soft white light
torus
.
position
.
set
(
0
,
0
,
-
1
);
scene
.
add
(
ambientLight
);
// Add the mesh to the scene
scene
.
add
(
torus
);
// Color of the tetrahedron geometry.
const
material
=
new
THREE
.
MeshPhysicalMaterial
({
color
:
0xff0000
,
roughness
:
0.5
,
metalness
:
0.5
,
clearcoat
:
1.0
,
clearcoatRoughness
:
0.1
});
// Set up the WebGLRenderer, which handles rendering to the session's base layer.
const
renderer
=
new
THREE
.
WebGLRenderer
({
// Create the tetrahedron and add it to the demo scene.
alpha
:
true
,
const
tetrahedron
=
new
THREE
.
Mesh
(
new
THREE
.
TetrahedronGeometry
(
1
),
material
);
preserveDrawingBuffer
:
true
,
canvas
:
canvas
,
// Position the tetrahedron
context
:
gl
tetrahedron
.
position
.
set
(
0
,
0
,
-
1
);
});
renderer
.
autoClear
=
false
;
// Add the mesh to the scene
scene
.
add
(
tetrahedron
);
// The API directly updates the camera matrices.
// Disable matrix auto updates so three.js doesn't attempt
// Set up the WebGLRenderer, which handles rendering to the session's base layer.
// to handle the matrices independently.
const
renderer
=
new
THREE
.
WebGLRenderer
({
const
camera
=
new
THREE
.
PerspectiveCamera
();
alpha
:
true
,
camera
.
matrixAutoUpdate
=
false
;
preserveDrawingBuffer
:
true
,
canvas
:
canvas
,
// Initialize a WebXR session using "immersive-ar".
context
:
gl
const
session
=
await
navigator
.
xr
.
requestSession
(
"
immersive-ar
"
);
});
session
.
updateRenderState
({
renderer
.
autoClear
=
false
;
baseLayer
:
new
XRWebGLLayer
(
session
,
gl
)
});
// The API directly updates the camera matrices.
// Disable matrix auto updates so three.js doesn't attempt
// A 'local' reference space has a native origin that is located
// to handle the matrices independently.
// near the viewer's position at the time the session was created.
const
camera
=
new
THREE
.
PerspectiveCamera
();
const
referenceSpace
=
await
session
.
requestReferenceSpace
(
'
local
'
);
camera
.
matrixAutoUpdate
=
false
;
// Create a render loop that allows us to draw on the AR view.
// Initialize a WebXR session using "immersive-ar".
const
onXRFrame
=
(
time
,
frame
)
=>
{
const
session
=
await
navigator
.
xr
.
requestSession
(
"
immersive-ar
"
);
// Queue up the next draw request.
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
);
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
);
}
}
</script>
</script>
</body>
</body>
...
...
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
Menu
Explore
Projects
Groups
Snippets