Function buildTorusGeometry

  • Creates a torus-shaped SceneGeometry.

    This function generates a torus (doughnut shape) geometry by calculating the positions of vertices based on the specified parameters. It also calculates the normals and UV coordinates for each vertex. The resulting geometry can be used to render a torus mesh in 3D environments.

    To create a torus geometry, call the function with the desired configuration. For example:

    const torusGeometry = buildTorusGeometry({
    radius: 2,
    tube: 0.5,
    radialSegments: 36,
    tubeSegments: 24,
    arc: Math.PI * 2,
    center: [0, 0, 0]
    });

    This creates a torus with a radius of 2 units, a tube radius of 0.5 units, 36 radial segments, and 24 tubular segments, with a full circle arc.

    Parameters

    • cfg: {
          arc?: number;
          center?: number[];
          radialSegments?: number;
          radius?: number;
          tube?: number;
          tubeSegments?: number;
      } = ...

      Configuration object for generating the torus geometry.

      • Optionalarc?: number

        The length of the arc in radians, where Math.PI*2 represents a full circle. Default is a full circle.

        Returns a SceneGeometry object containing the torus geometry with the necessary positions, indices, and optional normals and UV coordinates for rendering.

        const torusGeometry = buildTorusGeometry({
        radius: 1.5,
        tube: 0.4,
        radialSegments: 24,
        tubeSegments: 16,
        arc: Math.PI * 2,
        center: [0, 0, 0]
        });
        • The geometry is created by iterating over both radial and tube segments, calculating the positions of vertices in 3D space, and connecting them with indices to form triangles.
        • The arc parameter defines how much of the torus is created. A full circle corresponds to Math.PI * 2, and any smaller value creates a partial torus.
        • The function calculates vertex normals using the difference between each vertex and the center of the torus.
      • Optionalcenter?: number[]

        A 3D point (array of 3 numbers) indicating the center position of the torus. Defaults to [0, 0, 0].

      • OptionalradialSegments?: number

        The number of radial segments (segments along the circular cross-section). Default is 32.

      • Optionalradius?: number

        The overall radius of the torus. This controls the distance from the center to the tube's center. Default is 1.

      • Optionaltube?: number

        The radius of the tube that makes up the torus. Default is 0.3.

      • OptionaltubeSegments?: number

        The number of tubular segments (segments around the tube). Default is 24.

    Returns GeometryArrays

    The geometry data for the torus, including positions, normals, and indices for rendering.