Skip to content
This repository has been archived by the owner on Nov 30, 2020. It is now read-only.

Commit

Permalink
chore: Split VR video player into its own component
Browse files Browse the repository at this point in the history
Moves all aframe stuff to its own component
  • Loading branch information
qdot committed Jul 5, 2018
1 parent af5bb62 commit 2e923f1
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 87 deletions.
110 changes: 110 additions & 0 deletions src/components/AframeVideoPlayer/AframeVideoPlayer.ts
@@ -0,0 +1,110 @@
import Vue from "vue";
import { Component, Model, Prop, Watch } from "vue-property-decorator";
import videojs from "video.js";
import "aframe";
const asc = require("aframe-stereo-component");

@Component({
})
export default class AframeVideoPlayer extends Vue {
@Prop({default: null})
private currentPlayer: videojs.Player | null;
private vrControlButton: videojs.Component | null = null;
private videoElementId: string | null = null;

public mounted() {
if (!AFRAME.components.hasOwnProperty("stereo")) {
AFRAME.registerComponent("stereo", asc.stereo_component);
AFRAME.registerComponent("stereocam", asc.stereocam_component);
}
const Button = videojs.getComponent("Button");
const OnVRClick = () => {
this.RunVRMode();
};
const VRButton = (videojs as any).extend(videojs.getComponent("Button"), {
// tslint:disable-next-line object-literal-shorthand
constructor: function() {
(Button as any).apply(this, arguments);
this.controlText("VR");
},
// tslint:disable-next-line object-literal-shorthand
handleClick: function() {
OnVRClick();
},
});

videojs.registerComponent("vrButton", VRButton);
if (this.currentPlayer) {
this.OnCurrentPlayerChange();
}
}

@Watch("currentPlayer")
public OnCurrentPlayerChange() {
this.vrControlButton =
this.currentPlayer!
.getChild("controlBar")!
.addChild("vrButton", { id: "vr-aframe-button", text: "VR" }, 12);
this.vrControlButton.el().innerHTML = this.vrControlButton.el().innerHTML + "<b>VR</b>";

const videoElement = this.currentPlayer!.el().querySelector("video");
if (videoElement === undefined || videoElement === null) {
console.log("Can't find video element for aframe setup?");
return;
}
this.videoElementId = videoElement.id;
}

public beforeDestroy() {
this.currentPlayer!.getChild("controlBar")!.removeChild(this.vrControlButton!);
this.vrControlButton = null;
}

private RunVRMode() {
const scene = document.querySelector("a-scene");
const camera = document.createElement("a-camera");
// Objects here need to be cast to any, otherwise typescript gets angry
// about not being able to discern their shape. So what was that anna was
// saying about these type systems being more trouble than they are
// help...
camera.setAttribute("position", "0 0 10");
camera.setAttribute("stereocam", { eye: "left" } as any);
scene.appendChild(camera);
const leftEye = document.createElement("a-entity");
leftEye.setAttribute("geometry", {
primitive: "sphere",
radius: 100,
segmentsHeight: 64,
segmentsWidth: 64,
} as any);
leftEye.setAttribute("scale", "-1 1 1");
leftEye.setAttribute("material", {
shader: "flat",
npot: true,
side: "back",
src: "#" + this.videoElementId} as any);
leftEye.setAttribute("stereo", {
eye: "left",
mode: "half"} as any);
scene.appendChild(leftEye);
const rightEye = document.createElement("a-entity");
rightEye.setAttribute("geometry", {
primitive: "sphere",
radius: 100,
segmentsHeight: 64,
segmentsWidth: 64,
} as any);
rightEye.setAttribute("scale", "-1 1 1");
rightEye.setAttribute("material", {
shader: "flat",
npot: true,
side: "back",
src: "#" + this.videoElementId} as any);
rightEye.setAttribute("stereo", {
eye: "right",
mode: "half"} as any);
scene.appendChild(rightEye);
document.querySelector("a-scene").enterVR();
}
}
Vue.component("aframe-video-player", AframeVideoPlayer);
7 changes: 7 additions & 0 deletions src/components/AframeVideoPlayer/AframeVideoPlayer.vue
@@ -0,0 +1,7 @@
<template>
<a-scene embedded vr-mode-ui="enabled: false">
</a-scene>
</template>

<script lang="ts" src="./AframeVideoPlayer.ts">
</script>
87 changes: 2 additions & 85 deletions src/components/VideoPlayer/VideoPlayer.ts
Expand Up @@ -2,13 +2,12 @@ import Vue from "vue";
import { Component, Model, Prop, Watch } from "vue-property-decorator";
const videoPlayer = require("vue-video-player").videoPlayer;
import videojs from "video.js";
import "aframe";
const asc = require("aframe-stereo-component");
const vrButton = require("./VRButton");
import AframeVideoPlayerComponent from "../AframeVideoPlayer/AframeVideoPlayer.vue";

@Component({
components: {
videoPlayer,
AframeVideoPlayerComponent,
},
})
export default class VideoPlayer extends Vue {
Expand Down Expand Up @@ -131,86 +130,4 @@ export default class VideoPlayer extends Vue {
this.runTimeUpdateLoop();
});
}

@Watch("vrMode")
private SetupVRMode() {
if (this.vrMode) {
if (!AFRAME.components.hasOwnProperty("stereo")) {
AFRAME.registerComponent("stereo", asc.stereo_component);
AFRAME.registerComponent("stereocam", asc.stereocam_component);
}
const Button = videojs.getComponent("Button");
const OnVRClick = () => {
this.RunVRMode();
};
const VRButton = (videojs as any).extend(videojs.getComponent("Button"), {
// tslint:disable-next-line object-literal-shorthand
constructor: function() {
(Button as any).apply(this, arguments);
this.controlText("VR");
},
// tslint:disable-next-line object-literal-shorthand
handleClick: function() {
OnVRClick();
},
});

videojs.registerComponent("vrButton", VRButton);
this.vrControlButton =
this.currentPlayer!
.getChild("controlBar")!
.addChild("vrButton", { id: "vr-aframe-button", text: "VR" }, 12);
this.vrControlButton.el().innerHTML = this.vrControlButton.el().innerHTML + "<b>VR</b>";
} else {
this.currentPlayer!.getChild("controlBar")!.removeChild(this.vrControlButton!);
this.vrControlButton = null;
}
}

private RunVRMode() {
const scene = document.querySelector("a-scene");
const camera = document.createElement("a-camera");
// Objects here need to be cast to any, otherwise typescript gets angry
// about not being able to discern their shape. So what was that anna was
// saying about these type systems being more trouble than they are
// help...
camera.setAttribute("position", "0 0 10");
camera.setAttribute("stereocam", { eye: "left" } as any);
scene.appendChild(camera);
const leftEye = document.createElement("a-entity");
leftEye.setAttribute("geometry", {
primitive: "sphere",
radius: 100,
segmentsHeight: 64,
segmentsWidth: 64,
} as any);
leftEye.setAttribute("scale", "-1 1 1");
leftEye.setAttribute("material", {
shader: "flat",
npot: true,
side: "back",
src: "#" + this.videoElementId} as any);
leftEye.setAttribute("stereo", {
eye: "left",
mode: "half"} as any);
scene.appendChild(leftEye);
const rightEye = document.createElement("a-entity");
rightEye.setAttribute("geometry", {
primitive: "sphere",
radius: 100,
segmentsHeight: 64,
segmentsWidth: 64,
} as any);
rightEye.setAttribute("scale", "-1 1 1");
rightEye.setAttribute("material", {
shader: "flat",
npot: true,
side: "back",
src: "#" + this.videoElementId} as any);
rightEye.setAttribute("stereo", {
eye: "right",
mode: "half"} as any);
scene.appendChild(rightEye);
document.querySelector("a-scene").enterVR();
}
}
5 changes: 3 additions & 2 deletions src/components/VideoPlayer/VideoPlayer.vue
Expand Up @@ -13,8 +13,9 @@
</video-player>
</v-flex>
<v-flex v-if="this.vrMode">
<a-scene embedded vr-mode-ui="enabled: false">
</a-scene>
<aframe-video-player
:currentPlayer="this.currentPlayer">
</aframe-video-player>
</v-flex>
</v-layout>
</div>
Expand Down

0 comments on commit 2e923f1

Please sign in to comment.