Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions examples/jsm/objects/GroundedSkybox.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Mesh, MeshBasicMaterial, SphereGeometry, Vector3 } from 'three';
import { Mesh, MeshBasicMaterial, SphereGeometry, Vector3, Float16BufferAttribute } from 'three';

/**
* A ground-projected skybox.
Expand Down Expand Up @@ -36,7 +36,13 @@ class GroundedSkybox extends Mesh {

}

const geometry = new SphereGeometry( radius, 2 * resolution, resolution );
const geometry = new SphereGeometry(radius, 2 * resolution, resolution, 0, Math.PI * 2, 0, Math.PI,
{
position: Float16BufferAttribute,
uv: Float16BufferAttribute,
normal: null
}
);
geometry.scale( 1, 1, - 1 );

const pos = geometry.getAttribute( 'position' );
Expand All @@ -52,15 +58,15 @@ class GroundedSkybox extends Mesh {
const f =
tmp.y < y1 ? - height / tmp.y : ( 1 - tmp.y * tmp.y / ( 3 * y1 * y1 ) );
tmp.multiplyScalar( f );
tmp.toArray( pos.array, 3 * i );
pos.setXYZ(i, tmp.x, tmp.y, tmp.z);

}

}

pos.needsUpdate = true;

super( geometry, new MeshBasicMaterial( { map, depthWrite: false } ) );
super(geometry, new MeshBasicMaterial({ map, depthWrite: false, name: "GroundedSkybox" }));

}

Expand Down
34 changes: 34 additions & 0 deletions src/core/BufferAttribute.js
Original file line number Diff line number Diff line change
Expand Up @@ -653,6 +653,40 @@ class BufferAttribute {

}

/**
* Return this BufferAttribute with a different type.
*
* @param {Function} TargetType - BufferAttribute type to return.
* @return {BufferAttribute} Converted BufferAttribute.
*/
convert( TargetType ) {

if ( this.constructor === TargetType ) return this;

const target = new TargetType( this.count * this.itemSize, this.itemSize, this.normalized );

if ( target.isFloat16BufferAttribute )
target.normalized = false;

for ( let i = 0; i < this.count; ++ i ) {

switch ( this.itemSize ) {

case 1: target.setX( i, this.getX( i ) ); break;
case 2: target.setXY( i, this.getX( i ), this.getY( i ) ); break;
case 3: target.setXYZ( i, this.getX( i ), this.getY( i ), this.getZ( i ) ); break;
case 4: target.setXYZW( i, this.getX( i ), this.getY( i ), this.getZ( i ), this.getW( i ) ); break;

}

}

target.name = this.name;

return target;

}

/**
* Serializes the buffer attribute into JSON.
*
Expand Down
16 changes: 10 additions & 6 deletions src/geometries/SphereGeometry.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ class SphereGeometry extends BufferGeometry {
* @param {number} [phiLength=Math.PI*2] - The horizontal sweep angle size.
* @param {number} [thetaStart=0] - The vertical starting angle in radians.
* @param {number} [thetaLength=Math.PI] - The vertical sweep angle size.
* @param {Object} [attributes={}] - Attribute precision specifiers.
*/
constructor( radius = 1, widthSegments = 32, heightSegments = 16, phiStart = 0, phiLength = Math.PI * 2, thetaStart = 0, thetaLength = Math.PI ) {
constructor( radius = 1, widthSegments = 32, heightSegments = 16, phiStart = 0, phiLength = Math.PI * 2, thetaStart = 0, thetaLength = Math.PI, precision = { position: Float32BufferAttribute, uv: Float32BufferAttribute, normal: Float32BufferAttribute } ) {

super();

Expand All @@ -48,7 +49,8 @@ class SphereGeometry extends BufferGeometry {
phiStart: phiStart,
phiLength: phiLength,
thetaStart: thetaStart,
thetaLength: thetaLength
thetaLength: thetaLength,
precision: precision
};

widthSegments = Math.max( 3, Math.floor( widthSegments ) );
Expand Down Expand Up @@ -141,9 +143,11 @@ class SphereGeometry extends BufferGeometry {
// 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 ) );
this.setAttribute( 'position', new Float32BufferAttribute( vertices, 3 ).convert( precision.position ) );
if ( precision.normal !== null )
this.setAttribute( 'normal', new Float32BufferAttribute( normals, 3, true ).convert( precision.normal ) );
if ( precision.uv !== null )
this.setAttribute( 'uv', new Float32BufferAttribute( uvs, 2, true ).convert( precision.uv ) );

}

Expand All @@ -166,7 +170,7 @@ class SphereGeometry extends BufferGeometry {
*/
static fromJSON( data ) {

return new SphereGeometry( data.radius, data.widthSegments, data.heightSegments, data.phiStart, data.phiLength, data.thetaStart, data.thetaLength );
return new SphereGeometry( data.radius, data.widthSegments, data.heightSegments, data.phiStart, data.phiLength, data.thetaStart, data.thetaLength, data.precision );

}

Expand Down
Loading