Skip to content

Commit

Permalink
working just not when close to object
Browse files Browse the repository at this point in the history
  • Loading branch information
Trevor Baron committed Aug 3, 2018
1 parent 64164c2 commit 10e379c
Show file tree
Hide file tree
Showing 5 changed files with 313 additions and 102 deletions.
12 changes: 12 additions & 0 deletions Tools/Gulp/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@
"anaglyph",
"stereoscopic",
"vr",
"labs",
"virtualJoystick",
"optimizations",
"highlights",
Expand Down Expand Up @@ -1233,6 +1234,17 @@
"stereoscopicInterlace.fragment"
]
},
"labs": {
"files": [
"../../src/Labs/babylon.magicWindowManager.js"
],
"dependUpon": [
"core",
"freeCamera"
],
"shaders": [
]
},
"vr": {
"files": [
"../../src/PostProcess/babylon.vrDistortionCorrectionPostProcess.js",
Expand Down
29 changes: 26 additions & 3 deletions src/Cameras/babylon.camera.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
private static _RIG_MODE_STEREOSCOPIC_OVERUNDER = 13;
private static _RIG_MODE_VR = 20;
private static _RIG_MODE_WEBVR = 21;
private static _RIG_MODE_CUSTOM = 22;

public static get PERSPECTIVE_CAMERA(): number {
return Camera._PERSPECTIVE_CAMERA;
Expand Down Expand Up @@ -69,6 +70,9 @@
public static get RIG_MODE_WEBVR(): number {
return Camera._RIG_MODE_WEBVR;
}
public static get RIG_MODE_CUSTOM(): number {
return Camera._RIG_MODE_CUSTOM;
}

public static ForceAttachControlToAlwaysPreventDefault = false;

Expand Down Expand Up @@ -155,6 +159,8 @@
public _alternateCamera: Camera;

public customRenderTargets = new Array<RenderTargetTexture>();
// TODO: is this the same as customRenderTargets?, from looking at it's uses, likely not
public _outputBuffer:Nullable<WebGLBuffer> = null;

// Observables
public onViewMatrixChangedObservable = new Observable<Camera>();
Expand All @@ -163,7 +169,12 @@
public onRestoreStateObservable = new Observable<Camera>();

// Cache
private _computedViewMatrix = Matrix.Identity();
/**
* @hidden
* When false, _computedViewMatrix will directly correspond to the camera's view matrix
*/
public _updateViewMatrix = true;
public _computedViewMatrix = Matrix.Identity();
public _projectionMatrix = new Matrix();
private _doNotComputeProjectionMatrix = false;
private _worldMatrix = Matrix.Identity();
Expand Down Expand Up @@ -463,6 +474,11 @@
}

public getViewMatrix(force?: boolean): Matrix {
// TODO this should be updated to mirror _doNotComputeProjectionMatrix
if(!this._updateViewMatrix){
return this._computedViewMatrix;
}

if (!force && this._isSynchronizedViewMatrix()) {
return this._computedViewMatrix;
}
Expand Down Expand Up @@ -713,7 +729,7 @@
return (<TargetCamera>this._rigCameras[1]).getTarget();
}

public setCameraRigMode(mode: number, rigParams: any): void {
public setCameraRigMode(mode: number, rigParams: {interaxialDistance?:number, rigCameras?: Array<Camera>, vrCameraMetrics?: VRCameraMetrics, vrDisplay?:VRDisplay, specs?: any, frameData?: any, parentCamera?: Camera}): void {
if (this.cameraRigMode === mode) {
return;
}
Expand All @@ -733,7 +749,14 @@
this._cameraRigParams.stereoHalfAngle = Tools.ToRadians(this._cameraRigParams.interaxialDistance / 0.0637);

// create the rig cameras, unless none
if (this.cameraRigMode !== Camera.RIG_MODE_NONE) {
if(this.cameraRigMode === Camera.RIG_MODE_CUSTOM){
// Set all rig cameras passed in on the camera (Used for webXR)
if(rigParams && rigParams.rigCameras){
(<Array<Camera>>rigParams.rigCameras).forEach((camera)=>{
this._rigCameras.push(camera);
})
}
}else if (this.cameraRigMode !== Camera.RIG_MODE_NONE) {
let leftCamera = this.createRigCamera(this.name + "_L", 0);
let rightCamera = this.createRigCamera(this.name + "_R", 1);
if (leftCamera && rightCamera) {
Expand Down
88 changes: 54 additions & 34 deletions src/Engine/babylon.engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -597,6 +597,16 @@

/** @hidden */
public _gl: WebGLRenderingContext;
/**
* @hidden
* TODO: frame should be of type XRFrame? once webXR spec is released as outlined here https://github.com/immersive-web/webxr/blob/master/explainer.md
*/
public _onBeforeRenderObservable = new Observable<{time:number, frame?:any}>()
/**
* @hidden
* TODO: should be of type, any object that supports requestAnimationFrame
*/
public _customRequester:any;
private _renderingCanvas: Nullable<HTMLCanvasElement>;
private _windowIsBackground = false;
private _webGLVersion = 1.0;
Expand Down Expand Up @@ -1778,15 +1788,18 @@
}
}

/** @hidden */
public _renderLoop(): void {
/** @hidden
* TODO: frame should be of type XRFrame? once webXR spec is released as outlined here https://github.com/immersive-web/webxr/blob/master/explainer.md
*/
public _renderLoop(time:any, frame:any): void {
if (!this._contextWasLost) {
var shouldRender = true;
if (!this.renderEvenInBackground && this._windowIsBackground) {
shouldRender = false;
}

if (shouldRender) {
this._onBeforeRenderObservable.notifyObservers({time: time, frame: frame});
// Start new frame
this.beginFrame();

Expand All @@ -1804,9 +1817,16 @@
if (this._activeRenderLoops.length > 0) {
// Register new frame
var requester = null;
if (this._vrDisplay && this._vrDisplay.isPresenting)
if(this._customRequester){
requester = this._customRequester;
}else if (this._vrDisplay && this._vrDisplay.isPresenting){
requester = this._vrDisplay;
this._frameHandler = Tools.QueueNewFrame(this._bindedRenderFunction, requester);
}
if(this._customRequester == 1){

}else{
this._frameHandler = Tools.QueueNewFrame(this._bindedRenderFunction, requester);
}
} else {
this._renderingQueueLaunched = false;
}
Expand Down Expand Up @@ -2221,8 +2241,8 @@

private bindUnboundFramebuffer(framebuffer: Nullable<WebGLFramebuffer>) {
if (this._currentFramebuffer !== framebuffer) {
this._gl.bindFramebuffer(this._gl.FRAMEBUFFER, framebuffer);
this._currentFramebuffer = framebuffer;
// this._gl.bindFramebuffer(this._gl.FRAMEBUFFER, framebuffer);
// this._currentFramebuffer = framebuffer;
}
}

Expand All @@ -2233,34 +2253,34 @@
* @param onBeforeUnbind defines a function which will be called before the effective unbind
*/
public unBindFramebuffer(texture: InternalTexture, disableGenerateMipMaps = false, onBeforeUnbind?: () => void): void {
this._currentRenderTarget = null;

// If MSAA, we need to bitblt back to main texture
var gl = this._gl;

if (texture._MSAAFramebuffer) {
gl.bindFramebuffer(gl.READ_FRAMEBUFFER, texture._MSAAFramebuffer);
gl.bindFramebuffer(gl.DRAW_FRAMEBUFFER, texture._framebuffer);
gl.blitFramebuffer(0, 0, texture.width, texture.height,
0, 0, texture.width, texture.height,
gl.COLOR_BUFFER_BIT, gl.NEAREST);
}

if (texture.generateMipMaps && !disableGenerateMipMaps && !texture.isCube) {
this._bindTextureDirectly(gl.TEXTURE_2D, texture, true);
gl.generateMipmap(gl.TEXTURE_2D);
this._bindTextureDirectly(gl.TEXTURE_2D, null);
}

if (onBeforeUnbind) {
if (texture._MSAAFramebuffer) {
// Bind the correct framebuffer
this.bindUnboundFramebuffer(texture._framebuffer);
}
onBeforeUnbind();
}

this.bindUnboundFramebuffer(null);
// this._currentRenderTarget = null;

// // If MSAA, we need to bitblt back to main texture
// var gl = this._gl;

// if (texture._MSAAFramebuffer) {
// gl.bindFramebuffer(gl.READ_FRAMEBUFFER, texture._MSAAFramebuffer);
// gl.bindFramebuffer(gl.DRAW_FRAMEBUFFER, texture._framebuffer);
// gl.blitFramebuffer(0, 0, texture.width, texture.height,
// 0, 0, texture.width, texture.height,
// gl.COLOR_BUFFER_BIT, gl.NEAREST);
// }

// if (texture.generateMipMaps && !disableGenerateMipMaps && !texture.isCube) {
// this._bindTextureDirectly(gl.TEXTURE_2D, texture, true);
// gl.generateMipmap(gl.TEXTURE_2D);
// this._bindTextureDirectly(gl.TEXTURE_2D, null);
// }

// if (onBeforeUnbind) {
// if (texture._MSAAFramebuffer) {
// // Bind the correct framebuffer
// this.bindUnboundFramebuffer(texture._framebuffer);
// }
// onBeforeUnbind();
// }

// this.bindUnboundFramebuffer(null);
}

/**
Expand Down
143 changes: 143 additions & 0 deletions src/Labs/babylon.magicWindowManager.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
module BABYLON {
/**
* The _Labs namespace will contain classes/functions that are not (!) backwards compatible.
* The APIs in all labs-related classes and configuration might change.
* Once stable, lab features will be moved to the publis API and configuration object.
*/
export namespace _Labs {
export class MagicWindowManger {
private _xrDevice:any;
private _xrSession:any;
private _gl:any;
private _camera:FreeCamera;
public tappedOb = new BABYLON.Observable<{}>();
constructor(public scene:BABYLON.Scene){
this._camera = new BABYLON.FreeCamera("camera1", new BABYLON.Vector3(0, 0, 0), scene);
this._camera.minZ = 0;
this._camera.rotationQuaternion = new Quaternion()
this._camera.setCameraRigMode(Camera.RIG_MODE_CUSTOM, { parentCamera: this._camera, rigCameras: [new TargetCamera("left", this._camera.position.clone(), this._camera.getScene())] });
this._camera.rigCameras[0]._updateViewMatrix = false;
this._camera.rigCameras[0].freezeProjectionMatrix()
this._camera.rigCameras[0].minZ = 0;
}
init(){
var xrNavigator:any = navigator;
if(!xrNavigator.xr){
console.log("xr not found");
return Promise.reject("xr not found");
}
var started = false;
return xrNavigator.xr.requestDevice()
.then((device:any)=>{
this._xrDevice = device;
document.onclick = ()=>{
this.tappedOb.notifyObservers({})
if(started){
// console.log(camera.position)
// console.log(camera.rotationQuaternion)
return
}
started = true
this.enterXR();

}
})
}
enterXR(){
var outputCanvas = document.createElement('canvas');
outputCanvas.style.cssText = "position:absolute; top:0px;left:0px;z-index:10;width:100%;height:100%"
var ctx = outputCanvas.getContext('xrpresent');
console.log("enterXR")
this._xrDevice.requestSession({
outputContext: ctx,
environmentIntegration: true,
}).then((session:any)=>{
this._xrSession = session;
document.body.appendChild(outputCanvas);
console.log("got sesssion")
this._gl = this.scene.getEngine()._gl;
return this._gl.setCompatibleXRDevice(this._xrSession.device)
}).then(()=>{
this._xrSession.baseLayer = new XRWebGLLayer(this._xrSession, this._gl);
console.log("created XR layer")
return this._xrSession.requestFrameOfReference('eye-level')
}).then((frameOfRef:any)=>{
this.scene.activeCamera = this._camera;
this.scene.autoClear = false;
this.scene.getEngine().getRenderingCanvas()!.style.cssText = "visibility:hidden"
this.scene.getEngine()._customRequester = 1//this._xrSession
var rendLoop = (time:any, frame:any)=>{
var renderInfo = {time: time, frame: frame};
if(!renderInfo.frame){
return;
}
var pose = renderInfo.frame.getDevicePose(frameOfRef);

if(pose){
renderInfo.frame.views.forEach((view:any, i:number)=> {

var viewport = this._xrSession.baseLayer.getViewport(renderInfo.frame.views[i])

Matrix.FromFloat32ArrayToRefScaled(pose.getViewMatrix(renderInfo.frame.views[i]), 0, 1, this._camera.rigCameras[i]._computedViewMatrix)
Matrix.FromFloat32ArrayToRefScaled(renderInfo.frame.views[i].projectionMatrix, 0, 1, this._camera.rigCameras[i]._projectionMatrix)
if (!this._camera.getScene().useRightHandedSystem) {
[2, 6, 8, 9, 14].forEach((num) => {
this._camera.rigCameras[i]._computedViewMatrix.m[num] *= -1;
});
[8, 9, 10, 11].forEach((num) => {
this._camera.rigCameras[i]._projectionMatrix.m[num] *= -1;
});
}
// TODO figure out how to set viewport using ratio like other places
this._camera.rigCameras[i].viewport.width = viewport.width;
this._camera.rigCameras[i].viewport.height = viewport.height;
this._camera.rigCameras[i].viewport.x = viewport.x;
this._camera.rigCameras[i].viewport.y = viewport.y;
//this._camera.rigCameras[i].getViewMatrix(); // TODO, is this needed? // TODO this causes a huge perf hit when getting clone to a mesh for some reason
this._camera.rigCameras[i]._outputBuffer = this._xrSession.baseLayer.framebuffer;
//console.log(this._camera.rigCameras[i].getViewMatrix().getTranslation())
this.scene._activeMeshesFrozen=true;
this.scene._renderForCamera(this._camera.rigCameras[i], undefined, true)
})
}

this._xrSession.requestAnimationFrame(rendLoop)
}
this._xrSession.requestAnimationFrame(rendLoop)
// this.scene.getEngine()._onBeforeRenderObservable.add((renderInfo)=>{
// if(!renderInfo.frame){
// return;
// }
// var pose = renderInfo.frame.getDevicePose(frameOfRef);

// if(pose){
// renderInfo.frame.views.forEach((view:any, i:number)=> {

// var viewport = this._xrSession.baseLayer.getViewport(renderInfo.frame.views[i])

// Matrix.FromFloat32ArrayToRefScaled(pose.getViewMatrix(renderInfo.frame.views[i]), 0, 1, this._camera.rigCameras[i]._computedViewMatrix)
// Matrix.FromFloat32ArrayToRefScaled(renderInfo.frame.views[i].projectionMatrix, 0, 1, this._camera.rigCameras[i]._projectionMatrix)
// if (!this._camera.getScene().useRightHandedSystem) {
// [2, 6, 8, 9, 14].forEach((num) => {
// this._camera.rigCameras[i]._computedViewMatrix.m[num] *= -1;
// });
// [8, 9, 10, 11].forEach((num) => {
// this._camera.rigCameras[i]._projectionMatrix.m[num] *= -1;
// });
// }
// // TODO figure out how to set viewport using ratio like other places
// this._camera.rigCameras[i].viewport.width = viewport.width;
// this._camera.rigCameras[i].viewport.height = viewport.height;
// this._camera.rigCameras[i].viewport.x = viewport.x;
// this._camera.rigCameras[i].viewport.y = viewport.y;
// this._camera.rigCameras[i].getViewMatrix(); // TODO, is this needed?
// this._camera.rigCameras[i]._outputBuffer = this._xrSession.baseLayer.framebuffer;
// //console.log(this._camera.rigCameras[i].getViewMatrix().getTranslation())
// })
// }
// });
});
}
}
}
}
Loading

0 comments on commit 10e379c

Please sign in to comment.