setSession method

Future<void> setSession(
  1. dynamic value
)

Implementation

Future<void> setSession ( value ) async{
  session = value;

  if ( session != null ) {
    session.addEventListener( 'select', onSessionEvent );
    session.addEventListener( 'selectstart', onSessionEvent );
    session.addEventListener( 'selectend', onSessionEvent );
    session.addEventListener( 'squeeze', onSessionEvent );
    session.addEventListener( 'squeezestart', onSessionEvent );
    session.addEventListener( 'squeezeend', onSessionEvent );
    session.addEventListener( 'end', onSessionEnd );
    session.addEventListener( 'inputsourceschange', onInputSourcesChange );

    final attributes = gl.getContextAttributes();

    if ( attributes.xrCompatible != true ) {
      //await gl.makeXRCompatible();
    }

    if ( session.renderState.layers == null ) {
      final layerInit = {
        'antialias': attributes.antialias,
        'alpha': attributes.alpha,
        'depth': attributes.depth,
        'stencil': attributes.stencil,
        'framebufferScaleFactor': framebufferScaleFactor
      };

      glBaseLayer = XRWebGLLayer( session, gl, layerInit );

      session.updateRenderState( {'baseLayer': glBaseLayer } );

    }
    else if ( gl is WebGLRenderingContext ) {
      // Use old style webgl layer because we can't use MSAA
      // WebGL2 support.

      final layerInit = {
        'antialias': true,
        'alpha': attributes.alpha,
        'depth': attributes.depth,
        'stencil': attributes.stencil,
        'framebufferScaleFactor': framebufferScaleFactor
      };

      glBaseLayer = XRWebGLLayer( session, gl, layerInit );

      session.updateRenderState( { 'layers': [ glBaseLayer ] } );

    } else {
      isMultisample = attributes.antialias;
      dynamic depthFormat;

      if ( attributes.depth ) {

        clearStyle = WebGL.DEPTH_BUFFER_BIT;

        if ( attributes.stencil ) clearStyle |= WebGL.STENCIL_BUFFER_BIT;

        depthStyle = attributes.stencil ? WebGL.DEPTH_STENCIL_ATTACHMENT : WebGL.DEPTH_ATTACHMENT;
        depthFormat = attributes.stencil ? WebGL.DEPTH24_STENCIL8 : WebGL.DEPTH_COMPONENT24;

      }

      final projectionlayerInit = {
        'colorFormat': attributes.alpha ? WebGL.RGBA8 : WebGL.RGB8,
        'depthFormat': depthFormat,
        'scaleFactor': framebufferScaleFactor
      };

      glBinding = XRWebGLBinding( session, gl );

      glProjLayer = glBinding.createProjectionLayer( projectionlayerInit );

      glFramebuffer = gl.createFramebuffer();

      session.updateRenderState( { 'layers': [ glProjLayer ] } );

      if ( isMultisample ) {

        glMultisampledFramebuffer = gl.createFramebuffer();
        glColorRenderbuffer = gl.createRenderbuffer();
        gl.bindRenderbuffer( WebGL.RENDERBUFFER, glColorRenderbuffer );
        gl.renderbufferStorageMultisample(
          WebGL.RENDERBUFFER,
          4,
          WebGL.RGBA8,
          glProjLayer.textureWidth,
          glProjLayer.textureHeight );
        state.bindFramebuffer( WebGL.FRAMEBUFFER, glMultisampledFramebuffer );
        gl.framebufferRenderbuffer( WebGL.FRAMEBUFFER, WebGL.COLOR_ATTACHMENT0, WebGL.RENDERBUFFER, glColorRenderbuffer );
        gl.bindRenderbuffer( WebGL.RENDERBUFFER, null );

        if ( depthFormat != null ) {

          glDepthRenderbuffer = gl.createRenderbuffer();
          gl.bindRenderbuffer( WebGL.RENDERBUFFER, glDepthRenderbuffer );
          gl.renderbufferStorageMultisample( WebGL.RENDERBUFFER, 4, depthFormat, glProjLayer.textureWidth, glProjLayer.textureHeight );
          gl.framebufferRenderbuffer( WebGL.FRAMEBUFFER, depthStyle, WebGL.RENDERBUFFER, glDepthRenderbuffer );
          gl.bindRenderbuffer( WebGL.RENDERBUFFER, null );

        }

        state.bindFramebuffer( WebGL.FRAMEBUFFER, null );
      }
    }

    referenceSpace = await session.requestReferenceSpace( referenceSpaceType );

    animation.setContext( session );
    animation.start();

    isPresenting = true;
    dispatchEvent( Event(type: 'sessionstart' ) );
  }
}