forked from mekanoe/electron-openvr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
60 lines (45 loc) · 1.4 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
const { app, BrowserWindow } = require('electron')
const vr = require('node-openvr')
const VROverlay = require('node-openvr/overlay')
app.disableHardwareAcceleration()
class VRWindow extends BrowserWindow {
constructor (opts, ...args) {
opts.frame = false
opts.transparent = true
opts.show = false
opts.webPreferences = {
...(opts.webPreferences || {}),
offscreen: opts.vr.devMode || true
}
const vrOpts = opts.vr
opts.vr = undefined
super(opts, ...args)
this.__vrHasNewFrame = true
this.__setupOverlay(vrOpts)
}
__setupOverlay ({ key, name = 'Electron VR App', fps = 60, iconPath, system = null }) {
this.__vrSystem = system || vr.system.VR_Init(vr.EVRApplicationType.VRApplication_Overlay)
this.overlay = new VROverlay({ system: this.__vrSystem, key: key, name: name, iconPath: iconPath })
this.webContents.setFrameRate(fps)
this.webContents.on('paint', (...args) => {
this.__vrDraw()
})
this.webContents.on('dom-ready', () => {
this.overlay.show()
this.__vrDraw()
setTimeout(() => {
this.__vrDraw()
}, 1000)
})
}
__vrDraw (force = false) {
this.webContents.capturePage((image) => {
const buf = image.toBitmap()
if (buf.length === 0) {
return
}
this.overlay.setTextureFromBuffer(buf, { ...image.getSize() })
})
}
}
module.exports = {VRWindow}