Skip to content

Commit 10bbfd2

Browse files
refactored cast.js to new components
1 parent c3d9b7a commit 10bbfd2

File tree

1 file changed

+47
-72
lines changed

1 file changed

+47
-72
lines changed

com/cast.js

Lines changed: 47 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,8 @@ AFRAME.registerComponent('cast', {
44
},
55

66
requires: {
7-
dom: "./com/dom.js", // interpret .dom object
8-
xd: "./com/xd.js", // allow switching between 2D/3D
9-
html: "https://unpkg.com/aframe-htmlmesh@2.1.0/build/aframe-html.js", // html to AFRAME
10-
winboxjs: "https://unpkg.com/winbox@0.2.82/dist/winbox.bundle.min.js", // deadsimple windows: https://nextapps-de.github.io/winbox
11-
winboxcss: "https://unpkg.com/winbox@0.2.82/dist/css/winbox.min.css", //
7+
dom: "com/dom.js",
8+
window: "com/window.js",
129
},
1310

1411
dom: {
@@ -23,7 +20,7 @@ AFRAME.registerComponent('cast', {
2320

2421
init: function () { },
2522

26-
getInstallables: function(){
23+
etInstallables: function(){
2724
const installed = document.querySelector('[launcher]').components.launcher.system.getLaunchables()
2825
return this.data.comps.map( (c) => {
2926
return installed[c] ? null : c
@@ -37,77 +34,54 @@ AFRAME.registerComponent('cast', {
3734
if( this.el.sceneEl.renderer.xr.isPresenting ){
3835
this.el.sceneEl.exitVR() // *FIXME* we need a gui
3936
}
40-
const el = document.querySelector('body');
41-
const cropTarget = await CropTarget.fromElement(el);
42-
const stream = await navigator.mediaDevices.getDisplayMedia();
43-
const [track] = stream.getVideoTracks();
44-
this.track = track
45-
this.stream = stream
46-
this.createWindow()
47-
}
37+
this.el.object3D.quaternion.copy( AFRAME.scenes[0].camera.quaternion ) // face towards camera
38+
// instance this component
39+
this.el.setAttribute("dom","")
40+
},
41+
42+
DOMready: function(){
43+
this.setupCast();
44+
this.el.setAttribute("html-as-texture-in-xr", `domid: #${this.el.uid}; faceuser: true`)
45+
},
46+
4847
},
4948

50-
createWindow: async function(){
49+
setupCast: async function(){
5150
let s = await AFRAME.utils.require(this.requires)
5251

53-
// instance this component
54-
const instance = this.el.cloneNode(false)
55-
this.el.sceneEl.appendChild( instance )
56-
instance.setAttribute("dom", "")
57-
instance.setAttribute("xd", "") // allows flipping between DOM/WebGL when toggling XD-button
58-
instance.setAttribute("visible", AFRAME.utils.XD() == '3D' ? 'true' : 'false' )
59-
instance.setAttribute("position", AFRAME.utils.XD.getPositionInFrontOfCamera(0.5) )
60-
instance.setAttribute("grabbable","")
61-
instance.object3D.quaternion.copy( AFRAME.scenes[0].camera.quaternion ) // face towards camera
62-
instance.track = this.track
63-
instance.stream = this.stream
64-
65-
const setupWindow = () => {
66-
instance.dom.style.display = 'none'
67-
68-
const video = instance.dom.querySelector('video')
69-
video.addEventListener( "loadedmetadata", function () {
70-
let width = Math.round(window.innerWidth*0.4)
71-
let factor = width / this.videoWidth
72-
let height = Math.round(this.videoHeight * factor)
73-
new WinBox("Casting Tab",{
74-
width,
75-
height,
76-
x:"center",
77-
y:"center",
78-
id: instance.uid, // important hint for html-mesh
79-
root: document.querySelector("#overlay"),
80-
mount: instance.dom,
81-
onclose: () => { instance.dom.style.display = 'none'; return false; },
82-
oncreate: () => {
83-
84-
// instance.setAttribute("html",`html:#${instance.uid}; cursor:#cursor`)
85-
}
86-
});
87-
instance.dom.style.display = '' // show
88-
89-
// hint grabbable's obb-collider to track the window-object
90-
instance.components['obb-collider'].data.trackedObject3D = 'components.html.el.object3D.children.0'
91-
instance.components['obb-collider'].update()
92-
})
93-
video.srcObject = instance.stream
94-
video.play()
95-
96-
this.createVideoTexture.apply(instance)
97-
}
98-
setTimeout( () => setupWindow(), 10 ) // give new components time to init
99-
},
52+
const video = this.el.dom.querySelector('video')
53+
54+
video.addEventListener( "loadedmetadata", () => {
55+
56+
let width = Math.round(window.innerWidth*0.4)
57+
let factor = width / video.videoWidth
58+
let height = Math.round( video.videoHeight * factor)
59+
60+
const createVideoTexture = () => {
61+
const texture = new THREE.VideoTexture( video );
62+
texture.colorSpace = THREE.SRGBColorSpace;
63+
const geometry = new THREE.PlaneGeometry( 16, 9 );
64+
geometry.scale( 0.2, 0.2, 0.2 );
65+
const material = new THREE.MeshBasicMaterial( { map: texture } );
66+
const mesh = new THREE.Mesh( geometry, material );
67+
mesh.lookAt( AFRAME.scenes[0].camera.position );
68+
this.el.object3D.add(mesh)
69+
}
70+
71+
createVideoTexture.apply(this)
72+
//this.el.setAttribute("window", `title: casting tab; uid: ${this.el.uid}; attach: #overlay; dom: #${this.el.dom.id}; width:${width}; height: ${height}`)
73+
})
74+
75+
const el = document.querySelector('body');
76+
const cropTarget = await CropTarget.fromElement(el);
77+
const stream = await navigator.mediaDevices.getDisplayMedia();
78+
const [track] = stream.getVideoTracks();
79+
this.track = track
80+
this.stream = stream
81+
82+
video.srcObject = this.stream
83+
video.play()
10084

101-
createVideoTexture: function(){
102-
console.dir(this)
103-
const texture = new THREE.VideoTexture( video );
104-
texture.colorSpace = THREE.SRGBColorSpace;
105-
const geometry = new THREE.PlaneGeometry( 16, 9 );
106-
geometry.scale( 0.2, 0.2, 0.2 );
107-
const material = new THREE.MeshBasicMaterial( { map: texture } );
108-
const mesh = new THREE.Mesh( geometry, material );
109-
mesh.lookAt( this.sceneEl.camera.position );
110-
this.object3D.add(mesh)
11185
},
11286

11387
manifest: { // HTML5 manifest to identify app to xrsh
@@ -116,6 +90,7 @@ AFRAME.registerComponent('cast', {
11690
"icons": [
11791
{
11892
"src": "https://css.gg/cast.svg",
93+
"src": "data:image/svg+xml;base64,PHN2ZwogIHdpZHRoPSIyNCIKICBoZWlnaHQ9IjI0IgogIHZpZXdCb3g9IjAgMCAyNCAyNCIKICBmaWxsPSJub25lIgogIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIKPgogIDxwYXRoCiAgICBkPSJNMjAgNkg0VjhIMlY2QzIgNC44OTU0MyAyLjg5NTQzIDQgNCA0SDIwQzIxLjEwNDYgNCAyMiA0Ljg5NTQzIDIyIDZWMThDMjIgMTkuMTA0NiAyMS4xMDQ2IDIwIDIwIDIwSDE1VjE4SDIwVjZaIgogICAgZmlsbD0iY3VycmVudENvbG9yIgogIC8+CiAgPHBhdGgKICAgIGQ9Ik0yIDEzQzUuODY1OTkgMTMgOSAxNi4xMzQgOSAyMEg3QzcgMTcuMjM4NiA0Ljc2MTQyIDE1IDIgMTVWMTNaIgogICAgZmlsbD0iY3VycmVudENvbG9yIgogIC8+CiAgPHBhdGggZD0iTTIgMTdDMy42NTY4NSAxNyA1IDE4LjM0MzEgNSAyMEgyVjE3WiIgZmlsbD0iY3VycmVudENvbG9yIiAvPgogIDxwYXRoCiAgICBkPSJNMiA5QzguMDc1MTMgOSAxMyAxMy45MjQ5IDEzIDIwSDExQzExIDE1LjAyOTQgNi45NzA1NiAxMSAyIDExVjlaIgogICAgZmlsbD0iY3VycmVudENvbG9yIgogIC8+Cjwvc3ZnPg==",
11994
"type": "image/svg+xml",
12095
"sizes": "512x512"
12196
}

0 commit comments

Comments
 (0)