Skip to content

⬅️ Back to Table of Contents

📄 RingGeometry.js

📊 Analysis Summary

Metric Count
🔧 Functions 2
🧱 Classes 1
📦 Imports 4
📊 Variables & Constants 15

📚 Table of Contents

🛠️ File Location:

📂 src/geometries/RingGeometry.js

📦 Imports

Name Source
BufferGeometry ../core/BufferGeometry.js
Float32BufferAttribute ../core/BufferAttribute.js
Vector2 ../math/Vector2.js
Vector3 ../math/Vector3.js

Variables & Constants

Name Type Kind Value Exported
indices any[] let/var []
vertices any[] let/var []
normals any[] let/var []
uvs any[] let/var []
radius number let/var innerRadius
radiusStep number let/var ( ( outerRadius - innerRadius ) / phiSegments )
vertex Vector3 let/var new Vector3()
uv Vector2 let/var new Vector2()
segment number let/var thetaStart + i / thetaSegments * thetaLength
thetaSegmentLevel number let/var j * ( thetaSegments + 1 )
segment number let/var i + thetaSegmentLevel
a number let/var segment
b number let/var segment + thetaSegments + 1
c number let/var segment + thetaSegments + 2
d number let/var segment + 1

Functions

RingGeometry.copy(source: any): this

Parameters:

  • source any

Returns: this

Calls:

  • super.copy
  • Object.assign
Code
copy( source ) {

        super.copy( source );

        this.parameters = Object.assign( {}, source.parameters );

        return this;

    }

RingGeometry.fromJSON(data: any): RingGeometry

JSDoc:

/**
     * Factory method for creating an instance of this class from the given
     * JSON object.
     *
     * @param {Object} data - A JSON object representing the serialized geometry.
     * @return {RingGeometry} A new instance.
     */

Parameters:

  • data any

Returns: RingGeometry

Code
static fromJSON( data ) {

        return new RingGeometry( data.innerRadius, data.outerRadius, data.thetaSegments, data.phiSegments, data.thetaStart, data.thetaLength );

    }

Classes

RingGeometry

Class Code
class RingGeometry extends BufferGeometry {

    /**
     * Constructs a new ring geometry.
     *
     * @param {number} [innerRadius=0.5] - The inner radius of the ring.
     * @param {number} [outerRadius=1] - The outer radius of the ring.
     * @param {number} [thetaSegments=32] - Number of segments. A higher number means the ring will be more round. Minimum is `3`.
     * @param {number} [phiSegments=1] - Number of segments per ring segment. Minimum is `1`.
     * @param {number} [thetaStart=0] - Starting angle in radians.
     * @param {number} [thetaLength=Math.PI*2] - Central angle in radians.
     */
    constructor( innerRadius = 0.5, outerRadius = 1, thetaSegments = 32, phiSegments = 1, thetaStart = 0, thetaLength = Math.PI * 2 ) {

        super();

        this.type = 'RingGeometry';

        /**
         * Holds the constructor parameters that have been
         * used to generate the geometry. Any modification
         * after instantiation does not change the geometry.
         *
         * @type {Object}
         */
        this.parameters = {
            innerRadius: innerRadius,
            outerRadius: outerRadius,
            thetaSegments: thetaSegments,
            phiSegments: phiSegments,
            thetaStart: thetaStart,
            thetaLength: thetaLength
        };

        thetaSegments = Math.max( 3, thetaSegments );
        phiSegments = Math.max( 1, phiSegments );

        // buffers

        const indices = [];
        const vertices = [];
        const normals = [];
        const uvs = [];

        // some helper variables

        let radius = innerRadius;
        const radiusStep = ( ( outerRadius - innerRadius ) / phiSegments );
        const vertex = new Vector3();
        const uv = new Vector2();

        // generate vertices, normals and uvs

        for ( let j = 0; j <= phiSegments; j ++ ) {

            for ( let i = 0; i <= thetaSegments; i ++ ) {

                // values are generate from the inside of the ring to the outside

                const segment = thetaStart + i / thetaSegments * thetaLength;

                // vertex

                vertex.x = radius * Math.cos( segment );
                vertex.y = radius * Math.sin( segment );

                vertices.push( vertex.x, vertex.y, vertex.z );

                // normal

                normals.push( 0, 0, 1 );

                // uv

                uv.x = ( vertex.x / outerRadius + 1 ) / 2;
                uv.y = ( vertex.y / outerRadius + 1 ) / 2;

                uvs.push( uv.x, uv.y );

            }

            // increase the radius for next row of vertices

            radius += radiusStep;

        }

        // indices

        for ( let j = 0; j < phiSegments; j ++ ) {

            const thetaSegmentLevel = j * ( thetaSegments + 1 );

            for ( let i = 0; i < thetaSegments; i ++ ) {

                const segment = i + thetaSegmentLevel;

                const a = segment;
                const b = segment + thetaSegments + 1;
                const c = segment + thetaSegments + 2;
                const d = segment + 1;

                // faces

                indices.push( a, b, d );
                indices.push( b, c, d );

            }

        }

        // build geometry

        this.setIndex( indices );
        this.setAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) );
        this.setAttribute( 'normal', new Float32BufferAttribute( normals, 3 ) );
        this.setAttribute( 'uv', new Float32BufferAttribute( uvs, 2 ) );

    }

    copy( source ) {

        super.copy( source );

        this.parameters = Object.assign( {}, source.parameters );

        return this;

    }

    /**
     * Factory method for creating an instance of this class from the given
     * JSON object.
     *
     * @param {Object} data - A JSON object representing the serialized geometry.
     * @return {RingGeometry} A new instance.
     */
    static fromJSON( data ) {

        return new RingGeometry( data.innerRadius, data.outerRadius, data.thetaSegments, data.phiSegments, data.thetaStart, data.thetaLength );

    }

}

Methods

copy(source: any): this
Code
copy( source ) {

        super.copy( source );

        this.parameters = Object.assign( {}, source.parameters );

        return this;

    }
fromJSON(data: any): RingGeometry
Code
static fromJSON( data ) {

        return new RingGeometry( data.innerRadius, data.outerRadius, data.thetaSegments, data.phiSegments, data.thetaStart, data.thetaLength );

    }