Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove hard code. #12

Open
github-actions bot opened this issue Sep 15, 2021 · 0 comments
Open

Remove hard code. #12

github-actions bot opened this issue Sep 15, 2021 · 0 comments
Labels

Comments

@github-actions
Copy link

Remove hard code.

// TODO Remove hard code.

      this.handleResize();
      this.updateCameraPlanes();
   }
}
/** @constant */
VirtualInputRenderer.MIN_CAMERA_PLANES_DISTANCE = 0.5;
/** @type {VirtualInputRenderer[]} */
VirtualInputRenderer.instances = [];

class PhotometricStereoRenderer extends VirtualInputRenderer {
   /**
    * @public
    * @param {HTMLCanvasElement} uiCanvas
    * @param {string} modelUrl
    * @param {{width: number, height: number}} renderDimensions
    */
   constructor(
      uiCanvas,
      modelUrl = "./test-datasets/models/monkey.glb",
      renderDimensions = { width: 300, height: 300 }
   ) {
      super(uiCanvas, modelUrl, renderDimensions);
   }

   /**
    * @override
    * @protected
    */
   async initialize() {
      if (this.initialized || (!this.initialized && this.initializing)) {
         return;
      }
      this.initializing = true;

      super.initialize();

      this.lights = new Array(8);
      this.lightHelpers = new Array(8);

      for (let i = 0; i < 8; i++) {
         // TODO Remove hard code.
         this.lights[i] = new THREE.PointLight("white", 0.25);
         this.lights[i].castShadow = true;
         // TODO Remove hard code.
         this.lights[i].shadow.mapSize.width = 512 * 2;
         // TODO Remove hard code.
         this.lights[i].shadow.mapSize.height = 512 * 2;
         // TODO Remove hard code.
         this.lightHelpers[i] = new THREE.PointLightHelper(this.lights[i], 0.2);
         this.scene.add(this.lights[i]);
         this.scene.add(this.lightHelpers[i]);
      }

      this.initialized = true;
      this.initializing = false;
   }

   /**
    * @override
    * @public
    * @returns {Promise<HTMLImageElement[]>}
    */
   async render() {
      this.renderId++;
      const renderId = this.renderId;

      await this.initialize();

      if (this.isRenderObsolete(renderId)) return;

      const lightCount = this.lights.length;
      const renderPromises = [];

      for (let i = 0; i < lightCount; i++) {
         this.lightHelpers[i].visible = false;
      }
      this.cameraHelper.visible = false;

      for (let i = 0; i < lightCount; i++) {
         for (let i = 0; i < lightCount; i++) {
            this.lights[i].visible = false;
         }
         this.lights[i].visible = true;
         // TODO Remove hard code.
         this.lights[i].intensity = 25 * (this.cameraDistance / 8);

         if (this.isRenderObsolete(renderId)) return;

         this.renderer.render(this.scene, this.camera);
         const renderImageDataUrl = this.renderer.domElement.toDataURL();

         renderPromises.push(
            new Promise((resolve) => {
               setTimeout(() => {
                  const image = new Image();
                  image.addEventListener("load", () => {
                     resolve(image);
                  });
                  image.src = renderImageDataUrl;
               });
            })
         );
      }

      for (let i = 0; i < lightCount; i++) {
         this.lights[i].visible = true;
         this.lightHelpers[i].visible = true;
         // TODO Remove hard code.
         this.lights[i].intensity = 0.25;
      }
      this.cameraHelper.visible = true;

      this.updateCameraPlanes();
      this.uiRenderer.render(this.scene, this.uiCamera);

      return Promise.all(renderPromises);
   }

   /**
    * @public
    * @param {number} lightPolarAngleDeg
    */
   async setLightPolarAngleDeg(lightPolarAngleDeg) {
      await this.initialize();
      this.lightPolarAngleDeg = lightPolarAngleDeg;
      this.updateLightPositions();
   }

   /**
    * @override
    * @public
    * @param {number} lightDistance
    */
   async setLightDistance(lightDistance) {
      await this.initialize();

      this.lightDistance = lightDistance;
      for (let i = 0; i < this.lights.length; i++) {
         this.lights[i].distance = this.lightDistance * 2;
      }
      this.updateLightPositions();
   }

   async updateLightPositions() {
      await this.initialize();

      const correctedLightPolarDegree = 360 - this.lightPolarAngleDeg;

      /**
       * @param {THREE.Light} light
       * @param {number} lightAzimuthalDegree
       */
      const setSingleLightAzimuthalAngle = (light, lightAzimuthalDegree) => {
         let lightVector = new THREE.Vector3(this.lightDistance, 0, 0);

         let lightPolarRotationAxis = new THREE.Vector3(0, 1, 0).normalize();
         lightVector.applyAxisAngle(
            lightPolarRotationAxis,
            correctedLightPolarDegree * (Math.PI / 180)
         );

         const lightRotation = lightAzimuthalDegree * (Math.PI / 180);
         const lightRotationAxis = new THREE.Vector3(0, 0, 1).normalize();
         lightVector.applyAxisAngle(lightRotationAxis, lightRotation);

         light.position.set(lightVector.x, lightVector.y, lightVector.z);
      };

      const lightCount = this.lights.length;
      for (let i = 0; i < lightCount; i++) {
         setSingleLightAzimuthalAngle(this.lights[i], i * (360 / lightCount));
      }
   }
}

class SphericalGradientRenderer extends VirtualInputRenderer {
   /**
    * @public
    * @param {HTMLCanvasElement} uiCanvas
    * @param {string} modelUrl
    * @param {{width: number, height: number}} renderDimensions
    */
   constructor(
      uiCanvas,
      modelUrl = "./test-datasets/models/monkey.glb",
      renderDimensions = { width: 300, height: 300 }
   ) {
      super(uiCanvas, modelUrl, renderDimensions);
   }
}

f39176ee8b4b393b919c0a86b9e008ac8a522b6e

@github-actions github-actions bot added the todo label Sep 15, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

0 participants