-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp4.js
140 lines (139 loc) · 4.51 KB
/
app4.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
(_ => {
'use strict';
const polyfill = new WebXRPolyfill();
const cvs = document.createElement('canvas');
const xrButton = new XRDeviceButton({
onRequestSession: device => device.requestSession({exclusive: true}).then(session => {
xrButton.setSession(session);
session.addEventListener('end', e => {
xrButton.setSession(null)
cvs.style.display = 'none'
});
start(session);
}),
onEndSession: session => session.end()
});
[cvs, xrButton.domElement].forEach(el => document.body.appendChild(el));
if ( navigator.xr ) {
navigator.xr.requestDevice()
.then(device => device.supportsSession({exclusive: true}).then(_ => xrButton.setDevice(device)));
}
const start = session => {
const start = isOK => {
if ( !isOK ) return console.log('error');
cvs.style.display = 'block'
const world = RedWorld();
const scene = RedScene(redGL);
const renderer = RedRenderer();
const camL = RedCamera(), camR = RedCamera();
camL.fov = 90
camR.fov = 90
// redGL.renderScale = 0.5
redGL.world = world;
renderer.world = redGL.world;
camL.autoUpdateMatrix = camR.autoUpdateMatrix = false;
camL.x = camL.y = camL.z = 10
camL.lookAt(0, 1, 0)
camR.x = camR.y = camR.z = 10
camL.lookAt(0, 1, 0)
const tUUID = +RedGL.makeUUID()
const tLeftViewName = 'left' + tUUID
const tRightViewName = 'right' + tUUID
world.addView(RedView(tLeftViewName, scene, camL));
RedView(tLeftViewName).setSize('50%', '100%');
RedView(tLeftViewName).setLocation('0%', '0%');
world.addView(RedView('right' + tUUID, scene, camR));
RedView(tRightViewName).setSize('50%', '100%');
RedView(tRightViewName).setLocation('50%', '0%');
////
// scene['postEffectManager'].addEffect(RedPostEffect_Bloom(redGL))
const effect = RedPostEffect_DoF(redGL)
// effect.blur = 24
effect.focusLength = 10
// scene['postEffectManager'].addEffect( effect)
let tMat = RedStandardMaterial(
redGL,
RedBitmapTexture(redGL, 'asset/crate.png'),
RedBitmapTexture(redGL, 'asset/normalTest.jpg'),
RedBitmapTexture(redGL, 'asset/brick_roughness.jpg')
)
let tGeo = RedSphere(redGL, 1,24,24,24)
let testDLight;
const setScene = function () {
let tMesh;
testDLight = RedDirectionalLight(redGL, '#00ff00')
testDLight.x = 3
testDLight.y = 3
testDLight.z = 3
scene.addLight(testDLight);
testDLight = RedDirectionalLight(redGL, '#ff00ff')
testDLight.x = -3
testDLight.y = 3
testDLight.z = 3
scene.addLight(testDLight);
tMat.shininess = 32
let i,j,k
let max = 4
i = max
while ( i-- ) {
j = max
while ( j-- ) {
k = max
while ( k-- ) {
tMesh = RedMesh( redGL, tGeo, tMat )
tMesh.x = (i - max/2) * 5
tMesh.y = (j - max/2) * 5
tMesh.z = (k - max/2) * 5
scene.addChild( tMesh )
}
}
}
// scene.grid = RedGrid(redGL);
scene.skyBox =
RedSkyBox(redGL, [
'asset/cubemap/posx.png',
'asset/cubemap/negx.png',
'asset/cubemap/posy.png',
'asset/cubemap/negy.png',
'asset/cubemap/negz.png',
'asset/cubemap/posz.png'
]);
}
setScene()
redGL.fullMode = false
session.baseLayer = new XRWebGLLayer(session, redGL.gl);
session.requestFrameOfReference('eyeLevel').then(frameOfRef => {
const onframe = (t, frame) => {
const session = frame.session;
const pose = frame.getDevicePose(frameOfRef);
t = performance.now()
if ( pose ) {
redGL.gl.bindFramebuffer(redGL.gl.FRAMEBUFFER, session.baseLayer.framebuffer);
redGL.gl.clear(redGL.gl.COLOR_BUFFER_BIT | redGL.gl.DEPTH_BUFFER_BIT);
for ( const view of frame.views ) {
const viewport = session.baseLayer.getViewport(view);
const cam = viewport.x == 0 ? camL : camR;
const viewName = viewport.x == 0 ? tLeftViewName : tRightViewName
RedView(viewName).setSize(viewport.width, viewport.height)
RedView(viewName).setLocation(viewport.x, viewport.y)
cam.perspectiveMTX = view.projectionMatrix;
cam.matrix = pose.getViewMatrix(view);
}
renderer.render(redGL, t);
}
let i = scene.children.length
let tMesh;
while ( i-- ) {
tMesh = scene.children[i]
}
testDLight.x = Math.sin(t / 500) * 10
testDLight.y = Math.cos(t / 500) * 10
testDLight.z = Math.cos(t / 500) * 10
session.requestAnimationFrame(onframe);
}
session.requestAnimationFrame(onframe);
});
};
const redGL = RedGL(document.querySelector('canvas'), start, {compatibleXRDevice: session.device});
};
})();