Source: primitives/tubeLines.js

import * as THREE from 'three/webgpu';
import { extractVertices, getTubeLinesGeometry } from '../utilities';
import { ZincObject } from './zincObject';

/**
 * Provides an object which stores lines.
 * This is created when a valid json file containing lines is read into a {@link Zinc.Scene}
 * object.
 *
 * @class
 * @author Alan Wu
 * @return {TubeLines}
 */
const TubeLines = function () {
  ZincObject.call(this);
  this.isTubeLines = true;
  let dataIn = {};
  let geometryConfig = { radius: 1, radialSegments: 8, smooth: false };

  /**
   * Create the line segements using geometry and material.
   *
   * @param {THREE.Geomtry} geometryIn - Geometry of lines to be rendered.
   * @param {THREE.Material} materialIn - Material to be set for the lines.
   * @param {Object} options - Provide various options
   * @param {Boolean} options.localTimeEnabled - A flag to indicate either the lines is
   * time dependent.
   * @param {Boolean} options.localMorphColour - A flag to indicate either the colour is
   * time dependent.
   */
  this.createLineSegment = (geometryIn, materialIn, options) => {
    if (geometryIn && materialIn) {
      dataIn = { geometryIn, materialIn, options };
      const { radius, radialSegments, smooth } = geometryConfig;
      const geometry = getTubeLinesGeometry(
        extractVertices(geometryIn),
        radius,
        radialSegments,
        smooth,
      );
      const material = new THREE.MeshStandardMaterial({ color: materialIn.color });
      const mesh = new THREE.Mesh(geometry, material);
      this.setMesh(mesh, options.localTimeEnabled, options.localMorphColour);
    }
  };

  /**
   * Return the settings used for creating the tube line.
   *
   */
  this.getGeometryConfig = () => {
    return geometryConfig;
  };

  /**
   * Set the width for the lines.
   *
   * @param {Number} width - Width of the lines.
   */
  this.setWidth = (width) => {
    this.requestRender();
    if (this.morph && this.morph.material) {
      this.morph.material.linewidth = width;
      this.morph.material.needsUpdate = true;
    }
  };

  /**
   * Set the opacity of this Geometry. This function will also set the transparent
   * according to the provided alpha value.
   *
   * @param {Number} alpah - Alpha value to set for this geometry,
   * can be any value between from 0 to 1.0.
   */
  this.setAlpha = function (alpha) {
    this.requestRender();
    let mesh = this.getMorph();
    mesh.material.opacity = alpha;
    mesh.material.transparent = alpha < 1.0;
    mesh.material.depthWrite = alpha > 0.5;
  };

  /**
   * Set the wireframe mode for this geometry.
   * @param {Boolean} wireframe
   */
  this.setWireframe = (wireframe) => {
    this.requestRender();
    let mesh = this.getMorph();
    mesh.material.wireframe = wireframe;
  };

  /**
   * Update tube radius/radialSegments value
   *
   * @param {Float} radius The radius of the tube.
   * @param {Integer} radialSegments The number of segments that make up the cross-section.
   */
  this.setTubeLines = (radius, radialSegments) => {
    this.requestRender();
    if (radius && radialSegments) {
      const { geometryIn } = dataIn;
      let mesh = this.getMorph();
      mesh.geometry.dispose();

      geometryConfig = Object.assign(geometryConfig, { radius, radialSegments });
      mesh.geometry = getTubeLinesGeometry(
        extractVertices(geometryIn),
        radius,
        radialSegments,
        geometryConfig.smooth,
      );
    }
  };
};

TubeLines.prototype = Object.create(ZincObject.prototype);
export { TubeLines };