setSession method

Future<void> setSession(
  1. XRSession? value
)

Implementation

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

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

    final attributes = (gl.getContextAttributes() as JSAny).dartify() as Map;
    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.gl.gl, layerInit.jsify() );

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

    }
    else {
      isMultisample = attributes['antialias'];
      dynamic depthFormat;

      if (attributes['depth'] != null) {
        clearStyle = WebGL.DEPTH_BUFFER_BIT;
        if ( attributes['stencil'] != null) clearStyle |= WebGL.STENCIL_BUFFER_BIT;
        depthStyle = attributes['stencil'] != null? WebGL.DEPTH_STENCIL_ATTACHMENT : WebGL.DEPTH_ATTACHMENT;
        depthFormat = attributes['stencil'] != null? WebGL.DEPTH24_STENCIL8 : WebGL.DEPTH_COMPONENT24;
      }

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

      glBinding = XRWebGLBinding( session!, gl.gl.gl );
      glProjLayer = glBinding?.createProjectionLayer( projectionlayerInit.jsify() );
      glFramebuffer = gl.createFramebuffer();

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

      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 );
      }
    }

    setFoveation( foveation  );
    customReferenceSpace = null;

    referenceSpace = (await (session?.requestReferenceSpace( referenceSpaceType ).toDart)) as XRReferenceSpace;

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

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