Skip to content

Commit

Permalink
Geometries: Implemented es6 default parameters.
Browse files Browse the repository at this point in the history
  • Loading branch information
mrdoob committed Nov 22, 2020
1 parent 94db93d commit 00f2725
Show file tree
Hide file tree
Showing 17 changed files with 33 additions and 92 deletions.
8 changes: 2 additions & 6 deletions src/geometries/CircleBufferGeometry.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Vector2 } from '../math/Vector2.js';

class CircleBufferGeometry extends BufferGeometry {

constructor( radius, segments, thetaStart, thetaLength ) {
constructor( radius = 1, segments = 8, thetaStart = 0, thetaLength = Math.PI * 2 ) {

super();

Expand All @@ -18,11 +18,7 @@ class CircleBufferGeometry extends BufferGeometry {
thetaLength: thetaLength
};

radius = radius || 1;
segments = segments !== undefined ? Math.max( 3, segments ) : 8;

thetaStart = thetaStart !== undefined ? thetaStart : 0;
thetaLength = thetaLength !== undefined ? thetaLength : Math.PI * 2;
segments = Math.max( 3, segments );

// buffers

Expand Down
3 changes: 2 additions & 1 deletion src/geometries/ConeBufferGeometry.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ import { CylinderBufferGeometry } from './CylinderBufferGeometry.js';

class ConeBufferGeometry extends CylinderBufferGeometry {

constructor( radius, height, radialSegments, heightSegments, openEnded, thetaStart, thetaLength ) {
constructor( radius = 1, height = 1, radialSegments = 8, heightSegments = 1, openEnded = false, thetaStart = 0, thetaLength = Math.PI * 2 ) {

super( 0, radius, height, radialSegments, heightSegments, openEnded, thetaStart, thetaLength );

this.type = 'ConeBufferGeometry';

this.parameters = {
Expand Down
14 changes: 3 additions & 11 deletions src/geometries/CylinderBufferGeometry.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Vector2 } from '../math/Vector2.js';

class CylinderBufferGeometry extends BufferGeometry {

constructor( radiusTop, radiusBottom, height, radialSegments, heightSegments, openEnded, thetaStart, thetaLength ) {
constructor( radiusTop = 1, radiusBottom = 1, height = 1, radialSegments = 8, heightSegments = 1, openEnded = false, thetaStart = 0, thetaLength = Math.PI * 2 ) {

super();
this.type = 'CylinderBufferGeometry';
Expand All @@ -23,16 +23,8 @@ class CylinderBufferGeometry extends BufferGeometry {

const scope = this;

radiusTop = radiusTop !== undefined ? radiusTop : 1;
radiusBottom = radiusBottom !== undefined ? radiusBottom : 1;
height = height || 1;

radialSegments = Math.floor( radialSegments ) || 8;
heightSegments = Math.floor( heightSegments ) || 1;

openEnded = openEnded !== undefined ? openEnded : false;
thetaStart = thetaStart !== undefined ? thetaStart : 0.0;
thetaLength = thetaLength !== undefined ? thetaLength : Math.PI * 2;
radialSegments = Math.floor( radialSegments );
heightSegments = Math.floor( heightSegments );

// buffers

Expand Down
2 changes: 1 addition & 1 deletion src/geometries/DodecahedronBufferGeometry.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { PolyhedronBufferGeometry } from './PolyhedronBufferGeometry.js';

class DodecahedronBufferGeometry extends PolyhedronBufferGeometry {

constructor( radius, detail ) {
constructor( radius = 1, detail = 0 ) {

const t = ( 1 + Math.sqrt( 5 ) ) / 2;
const r = 1 / t;
Expand Down
2 changes: 1 addition & 1 deletion src/geometries/IcosahedronBufferGeometry.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { PolyhedronBufferGeometry } from './PolyhedronBufferGeometry.js';

class IcosahedronBufferGeometry extends PolyhedronBufferGeometry {

constructor( radius, detail ) {
constructor( radius = 1, detail = 0 ) {

const t = ( 1 + Math.sqrt( 5 ) ) / 2;

Expand Down
7 changes: 2 additions & 5 deletions src/geometries/LatheBufferGeometry.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { MathUtils } from '../math/MathUtils.js';

class LatheBufferGeometry extends BufferGeometry {

constructor( points, segments, phiStart, phiLength ) {
constructor( points, segments = 12, phiStart = 0, phiLength = Math.PI * 2 ) {

super();

Expand All @@ -19,15 +19,12 @@ class LatheBufferGeometry extends BufferGeometry {
phiLength: phiLength
};

segments = Math.floor( segments ) || 12;
phiStart = phiStart || 0;
phiLength = phiLength || Math.PI * 2;
segments = Math.floor( segments );

// clamp phiLength so it's in range of [ 0, 2PI ]

phiLength = MathUtils.clamp( phiLength, 0, Math.PI * 2 );


// buffers

const indices = [];
Expand Down
2 changes: 1 addition & 1 deletion src/geometries/OctahedronBufferGeometry.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { PolyhedronBufferGeometry } from './PolyhedronBufferGeometry.js';

class OctahedronBufferGeometry extends PolyhedronBufferGeometry {

constructor( radius, detail ) {
constructor( radius = 1, detail = 0 ) {

const vertices = [
1, 0, 0, - 1, 0, 0, 0, 1, 0,
Expand Down
20 changes: 4 additions & 16 deletions src/geometries/PlaneBufferGeometry.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Float32BufferAttribute } from '../core/BufferAttribute.js';

class PlaneBufferGeometry extends BufferGeometry {

constructor( width, height, widthSegments, heightSegments ) {
constructor( width = 1, height = 1, widthSegments = 1, heightSegments = 1 ) {

super();
this.type = 'PlaneBufferGeometry';
Expand All @@ -15,30 +15,25 @@ class PlaneBufferGeometry extends BufferGeometry {
heightSegments: heightSegments
};

width = width || 1;
height = height || 1;

const width_half = width / 2;
const height_half = height / 2;

const gridX = Math.floor( widthSegments ) || 1;
const gridY = Math.floor( heightSegments ) || 1;
const gridX = Math.floor( widthSegments );
const gridY = Math.floor( heightSegments );

const gridX1 = gridX + 1;
const gridY1 = gridY + 1;

const segment_width = width / gridX;
const segment_height = height / gridY;

// buffers
//

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

// generate vertices, normals and uvs

for ( let iy = 0; iy < gridY1; iy ++ ) {

const y = iy * segment_height - height_half;
Expand All @@ -58,8 +53,6 @@ class PlaneBufferGeometry extends BufferGeometry {

}

// indices

for ( let iy = 0; iy < gridY; iy ++ ) {

for ( let ix = 0; ix < gridX; ix ++ ) {
Expand All @@ -69,17 +62,13 @@ class PlaneBufferGeometry extends BufferGeometry {
const c = ( ix + 1 ) + gridX1 * ( iy + 1 );
const d = ( ix + 1 ) + gridX1 * iy;

// 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 ) );
Expand All @@ -89,5 +78,4 @@ class PlaneBufferGeometry extends BufferGeometry {

}


export { PlaneBufferGeometry };
5 changes: 1 addition & 4 deletions src/geometries/PolyhedronBufferGeometry.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Vector2 } from '../math/Vector2.js';

class PolyhedronBufferGeometry extends BufferGeometry {

constructor( vertices, indices, radius, detail ) {
constructor( vertices, indices, radius = 1, detail = 0 ) {

super();

Expand All @@ -18,9 +18,6 @@ class PolyhedronBufferGeometry extends BufferGeometry {
detail: detail
};

radius = radius || 1;
detail = detail || 0;

// default buffer data

const vertexBuffer = [];
Expand Down
12 changes: 3 additions & 9 deletions src/geometries/RingBufferGeometry.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Vector3 } from '../math/Vector3.js';

class RingBufferGeometry extends BufferGeometry {

constructor( innerRadius, outerRadius, thetaSegments, phiSegments, thetaStart, thetaLength ) {
constructor( innerRadius = 0.5, outerRadius = 1, thetaSegments = 8, phiSegments = 1, thetaStart = 0, thetaLength = Math.PI * 2 ) {

super();

Expand All @@ -20,14 +20,8 @@ class RingBufferGeometry extends BufferGeometry {
thetaLength: thetaLength
};

innerRadius = innerRadius || 0.5;
outerRadius = outerRadius || 1;

thetaStart = thetaStart !== undefined ? thetaStart : 0;
thetaLength = thetaLength !== undefined ? thetaLength : Math.PI * 2;

thetaSegments = thetaSegments !== undefined ? Math.max( 3, thetaSegments ) : 8;
phiSegments = phiSegments !== undefined ? Math.max( 1, phiSegments ) : 1;
thetaSegments = Math.max( 3, thetaSegments );
phiSegments = Math.max( 1, phiSegments );

// buffers

Expand Down
4 changes: 1 addition & 3 deletions src/geometries/ShapeBufferGeometry.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { ShapeUtils } from '../extras/ShapeUtils.js';

class ShapeBufferGeometry extends BufferGeometry {

constructor( shapes, curveSegments ) {
constructor( shapes, curveSegments = 12 ) {

super();
this.type = 'ShapeBufferGeometry';
Expand All @@ -14,8 +14,6 @@ class ShapeBufferGeometry extends BufferGeometry {
curveSegments: curveSegments
};

curveSegments = curveSegments || 12;

// buffers

const indices = [];
Expand Down
14 changes: 3 additions & 11 deletions src/geometries/SphereBufferGeometry.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Vector3 } from '../math/Vector3.js';

class SphereBufferGeometry extends BufferGeometry {

constructor( radius, widthSegments, heightSegments, phiStart, phiLength, thetaStart, thetaLength ) {
constructor( radius = 1, widthSegments = 8, heightSegments = 6, phiStart = 0, phiLength = Math.PI * 2, thetaStart = 0, thetaLength = Math.PI ) {

super();
this.type = 'SphereBufferGeometry';
Expand All @@ -19,16 +19,8 @@ class SphereBufferGeometry extends BufferGeometry {
thetaLength: thetaLength
};

radius = radius || 1;

widthSegments = Math.max( 3, Math.floor( widthSegments ) || 8 );
heightSegments = Math.max( 2, Math.floor( heightSegments ) || 6 );

phiStart = phiStart !== undefined ? phiStart : 0;
phiLength = phiLength !== undefined ? phiLength : Math.PI * 2;

thetaStart = thetaStart !== undefined ? thetaStart : 0;
thetaLength = thetaLength !== undefined ? thetaLength : Math.PI;
widthSegments = Math.max( 3, Math.floor( widthSegments ) );
heightSegments = Math.max( 2, Math.floor( heightSegments ) );

const thetaEnd = Math.min( thetaStart + thetaLength, Math.PI );

Expand Down
2 changes: 1 addition & 1 deletion src/geometries/TetrahedronBufferGeometry.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { PolyhedronBufferGeometry } from './PolyhedronBufferGeometry.js';

class TetrahedronBufferGeometry extends PolyhedronBufferGeometry {

constructor( radius, detail ) {
constructor( radius = 1, detail = 0 ) {

const vertices = [
1, 1, 1, - 1, - 1, 1, - 1, 1, - 1, 1, - 1, - 1
Expand Down
4 changes: 1 addition & 3 deletions src/geometries/TextBufferGeometry.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@ import { ExtrudeBufferGeometry } from './ExtrudeBufferGeometry.js';

class TextBufferGeometry extends ExtrudeBufferGeometry {

constructor( text, parameters ) {

parameters = parameters || {};
constructor( text, parameters = {} ) {

const font = parameters.font;

Expand Down
9 changes: 3 additions & 6 deletions src/geometries/TorusBufferGeometry.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Vector3 } from '../math/Vector3.js';

class TorusBufferGeometry extends BufferGeometry {

constructor( radius, tube, radialSegments, tubularSegments, arc ) {
constructor( radius = 1, tube = 0.4, radialSegments = 8, tubularSegments = 6, arc = Math.PI * 2 ) {

super();
this.type = 'TorusBufferGeometry';
Expand All @@ -17,11 +17,8 @@ class TorusBufferGeometry extends BufferGeometry {
arc: arc
};

radius = radius || 1;
tube = tube || 0.4;
radialSegments = Math.floor( radialSegments ) || 8;
tubularSegments = Math.floor( tubularSegments ) || 6;
arc = arc || Math.PI * 2;
radialSegments = Math.floor( radialSegments );
tubularSegments = Math.floor( tubularSegments );

// buffers

Expand Down
10 changes: 3 additions & 7 deletions src/geometries/TorusKnotBufferGeometry.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Vector3 } from '../math/Vector3.js';

class TorusKnotBufferGeometry extends BufferGeometry {

constructor( radius, tube, tubularSegments, radialSegments, p, q ) {
constructor( radius = 1, tube = 0.4, tubularSegments = 64, radialSegments = 8, p = 2, q = 3 ) {

super();
this.type = 'TorusKnotBufferGeometry';
Expand All @@ -18,12 +18,8 @@ class TorusKnotBufferGeometry extends BufferGeometry {
q: q
};

radius = radius || 1;
tube = tube || 0.4;
tubularSegments = Math.floor( tubularSegments ) || 64;
radialSegments = Math.floor( radialSegments ) || 8;
p = p || 2;
q = q || 3;
tubularSegments = Math.floor( tubularSegments );
radialSegments = Math.floor( radialSegments );

// buffers

Expand Down
7 changes: 1 addition & 6 deletions src/geometries/TubeBufferGeometry.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Vector3 } from '../math/Vector3.js';

class TubeBufferGeometry extends BufferGeometry {

constructor( path, tubularSegments, radius, radialSegments, closed ) {
constructor( path, tubularSegments = 64, radius = 1, radialSegments = 8, closed = false ) {

super();
this.type = 'TubeBufferGeometry';
Expand All @@ -18,11 +18,6 @@ class TubeBufferGeometry extends BufferGeometry {
closed: closed
};

tubularSegments = tubularSegments || 64;
radius = radius || 1;
radialSegments = radialSegments || 8;
closed = closed || false;

const frames = path.computeFrenetFrames( tubularSegments, closed );

// expose internals
Expand Down

0 comments on commit 00f2725

Please sign in to comment.