File size: 10,023 Bytes
71d12ce
1
{"version":3,"file":"three-6dof.min.js","sources":["../src/components/constants.ts","../src/components/uniforms.ts","../src/components/viewer.ts"],"sourcesContent":["enum MeshDensity {\n  LOW = 64,\n  MEDIUM = 128,\n  HIGH = 256,\n  EXTRA_HIGH = 512,\n  EPIC = 1024,\n}\n\nenum Style {\n  WIRE = 0,\n  POINTS = 1,\n  MESH = 2,\n}\n\nenum TextureType {\n  TOP_BOTTOM,\n  SEPERATE,\n}\n\nclass Props {\n  public type: TextureType = TextureType.SEPERATE\n  public density: MeshDensity = MeshDensity.HIGH\n  public style: Style = Style.MESH\n  public displacement: number = 4.0\n  public radius: number = 6\n}\n\nexport { MeshDensity, Style, TextureType, Props }\n","export const Uniforms: object = {\n  colorTexture: {\n    type: 't',\n    value: null,\n  },\n  depthTexture: {\n    type: 't',\n    value: null,\n  },\n  time: {\n    type: 'f',\n    value: 0.0,\n  },\n  opacity: {\n    type: 'f',\n    value: 1.0,\n  },\n  pointSize: {\n    type: 'f',\n    value: 3.0,\n  },\n  debugDepth: {\n    type: 'f',\n    value: 0.0,\n  },\n  displacement: {\n    type: 'f',\n    value: 1.0,\n  },\n}\n","import {\n  Object3D,\n  ShaderMaterial,\n  BackSide,\n  Mesh,\n  Points,\n  SphereBufferGeometry,\n  Texture,\n  NearestFilter,\n  LinearFilter,\n  RGBFormat,\n} from './three'\n\n// @ts-ignore\nimport frag from '../shaders/sixdof.frag'\n// @ts-ignore\nimport vert from '../shaders/sixdof.vert'\n\nimport { Uniforms } from './uniforms'\nimport { Style, MeshDensity, TextureType, Props } from './constants'\n\nexport default class Viewer extends Object3D {\n  /** Default props if not provided */\n  private props: Props = new Props()\n\n  private static geometry: SphereBufferGeometry\n  private material: ShaderMaterial = new ShaderMaterial({\n    uniforms: Uniforms,\n    vertexShader: vert,\n    fragmentShader: frag,\n    transparent: true,\n    side: BackSide,\n  })\n\n  constructor(texture: Texture, depth?: Texture, props?: object) {\n    super()\n\n    /** Assign the user provided props, if any */\n    this.setProps(this.props, props)\n\n    // /** Add the compiler definitions needed to pick the right GLSL methods */\n    this.setShaderDefines(this.material, [TextureType[this.props.type]])\n\n    /**\n     * Create the geometry only once, it can be shared between instances\n     *  of the viewer since it's kept as a static class member\n     **/\n    if (!Viewer.geometry) {\n      Viewer.geometry = this.createSphereGeometry(\n        this.props.radius,\n        this.props.density,\n      )\n    }\n\n    /** Assign the textures and update the shader uniforms */\n    this.assignTexture(this.props.type, texture, depth)\n\n    /** Set the displacement using the public setter */\n    this.displacement = this.props.displacement\n\n    /** Create the Mesh/Points and add it to the viewer object */\n    super.add(this.createMesh(Viewer.geometry, this.material, this.props.style))\n  }\n\n  /** Small util to set the defines of the GLSL program based on textureType */\n  private setShaderDefines(\n    material: ShaderMaterial,\n    defines: Array<string>,\n  ): void {\n    defines.forEach(define => (material.defines[define] = ''))\n  }\n\n  /** Internal util to create buffer geometry */\n  private createSphereGeometry(\n    radius: number,\n    meshDensity: MeshDensity,\n  ): SphereBufferGeometry {\n    return new SphereBufferGeometry(radius, meshDensity, meshDensity)\n  }\n\n  /** Internal util to set viewer props from config object */\n  private setProps(viewerProps: Props, userProps?: object): void {\n    if (!userProps) return\n\n    /** Iterate over user provided props and assign to viewer props */\n    for (let prop in userProps) {\n      if (viewerProps[prop]) {\n        viewerProps[prop] = userProps[prop]\n      } else {\n        console.warn(\n          `THREE.SixDOF: Provided ${prop} in config but it is not a valid property and being ignored`,\n        )\n      }\n    }\n  }\n\n  /** Internal util to assign the textures to the shader uniforms */\n  private assignTexture(\n    type: TextureType,\n    colorTexture: Texture,\n    depthTexture?: Texture,\n  ): void {\n    /** Check wheter we are rendering top bottom or just single textures */\n    if (type === TextureType.SEPERATE) {\n      if (!depthTexture)\n        throw new Error(\n          'When using seperate texture type, depthmap must be provided',\n        )\n      this.depth = this.setDefaultTextureProps(depthTexture)\n    }\n\n    /** Assign the main texture */\n    this.texture = this.setDefaultTextureProps(colorTexture)\n  }\n\n  private setDefaultTextureProps(texture: Texture): Texture {\n    texture.minFilter = NearestFilter\n    texture.magFilter = LinearFilter\n    texture.format = RGBFormat\n    texture.generateMipmaps = false\n    return texture\n  }\n\n  /** An internal util to create the Mesh Object3D */\n  private createMesh(\n    geo: SphereBufferGeometry,\n    mat: ShaderMaterial,\n    style: Style,\n  ): Object3D {\n    switch (style) {\n      case Style.WIRE:\n        if (!this.material.wireframe) this.material.wireframe = true\n        return new Mesh(geo, mat)\n      case Style.MESH:\n        if (this.material.wireframe) this.material.wireframe = false\n        return new Mesh(geo, mat)\n      case Style.POINTS:\n        return new Points(geo, mat)\n    }\n  }\n\n  /** Toggle vieweing texture or depthmap in viewer */\n  public toggleDepthDebug(state?: boolean): void {\n    this.material.uniforms.debugDepth.value =\n      state != undefined ? state : !this.material.uniforms.debugDepth.value\n  }\n\n  /** Setter for displacement amount */\n  public set displacement(val: number) {\n    this.material.uniforms.displacement.value = val\n  }\n\n  /** Setter for depthmap uniform */\n  public set depth(map: Texture) {\n    this.material.uniforms.depthTexture.value = map\n  }\n\n  /** Setter for depthmap uniform */\n  public set texture(map: Texture) {\n    this.material.uniforms.colorTexture.value = map\n  }\n\n  /** Setter for the opacity */\n  public set opacity(val: number) {\n    this.material.uniforms.opacity.value = val\n  }\n\n  /** Setter for the point size */\n  public set pointSize(val: number) {\n    this.material.uniforms.pointSize.value = val\n  }\n\n  /** Getter for the current viewer props */\n  public get config(): Props {\n    return this.props\n  }\n\n  /** Getter for the opacity */\n  public get opacity(): number {\n    return this.material.uniforms.opacity.value\n  }\n\n  /** Getter for the point size */\n  public get pointSize(): number {\n    return this.material.uniforms.pointSize.value\n  }\n\n  /** Getter for displacement amount */\n  public get displacement(): number {\n    return this.material.uniforms.displacement.value\n  }\n\n  /** Getter for texture */\n  public get texture(): Texture {\n    return this.material.uniforms.colorTexture.value\n  }\n\n  /** Getter for the depth texture */\n  public get depth(): Texture {\n    return this.material.uniforms.opacity.value\n  }\n}\n"],"names":["MeshDensity","Style","TextureType","Uniforms","colorTexture","type","value","depthTexture","time","opacity","pointSize","debugDepth","displacement","Props","SEPERATE","density","HIGH","style","MESH","radius","Viewer","Object3D","constructor","texture","depth","props","material","ShaderMaterial","uniforms","vertexShader","vert","fragmentShader","frag","transparent","side","BackSide","setProps","this","setShaderDefines","geometry","createSphereGeometry","assignTexture","add","createMesh","defines","forEach","define","meshDensity","SphereBufferGeometry","viewerProps","userProps","prop","console","warn","Error","setDefaultTextureProps","minFilter","NearestFilter","magFilter","LinearFilter","format","RGBFormat","generateMipmaps","geo","mat","WIRE","wireframe","Mesh","POINTS","Points","toggleDepthDebug","state","undefined","val","map"],"mappings":"2OAAKA,EAQAC,EAMAC,uzGCdQC,EAAmB,CAC9BC,aAAc,CACZC,KAAM,IACNC,MAAO,MAETC,aAAc,CACZF,KAAM,IACNC,MAAO,MAETE,KAAM,CACJH,KAAM,IACNC,MAAO,GAETG,QAAS,CACPJ,KAAM,IACNC,MAAO,GAETI,UAAW,CACTL,KAAM,IACNC,MAAO,GAETK,WAAY,CACVN,KAAM,IACNC,MAAO,GAETM,aAAc,CACZP,KAAM,IACNC,MAAO,KD3BNN,EAAAA,gBAAAA,mBAAAA,gBAAAA,EAAAA,uBAAAA,EAAAA,mBAAAA,EAAAA,+BAAAA,EAAAA,qBAQAC,EAAAA,UAAAA,aAAAA,iBAAAA,EAAAA,qBAAAA,EAAAA,kBAMAC,EAAAA,gBAAAA,mBAAAA,6BAAAA,EAAAA,yBAKL,MAAMW,qBACGR,KAAoBH,cAAYY,cAChCC,QAAuBf,cAAYgB,UACnCC,MAAehB,QAAMiB,UACrBN,aAAuB,OACvBO,OAAiB,GEHX,MAAMC,UAAeC,WAalCC,YAAYC,EAAkBC,EAAiBC,gBAXvCA,MAAe,IAAIZ,OAGnBa,SAA2B,IAAIC,iBAAe,CACpDC,SAAUzB,EACV0B,aAAcC,EACdC,eAAgBC,EAChBC,aAAa,EACbC,KAAMC,kBAODC,SAASC,KAAKZ,MAAOA,QAGrBa,iBAAiBD,KAAKX,SAA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