How to use the Sphere function from three

Find comprehensive JavaScript three.Sphere code examples handpicked from public code repositorys.

three.Sphere is a class in the Three.js library that represents a 3D sphere geometry.

150
151
152
153
154
155
156
157
158
159
 */
bindLightTransform: function (offset, phi, theta, obj, dirLight) {
  // Computing boundingSphere
  const bb = new THREE.Box3().setFromObject(obj);
  const center = bb.getCenter(new THREE.Vector3());
  const bsphere = bb.getBoundingSphere(new THREE.Sphere(center));
  const sphericalPoint = new THREE.Spherical(
    bsphere.radius + offset,
    phi,
    theta
fork icon15
star icon10
watch icon0

+ 4 other calls in file

66
67
68
69
70
71
72
73
74
75
const _new_b = new THREE.Vector3();
const _new_c = new THREE.Vector3();
const _axis = new THREE.Vector3();
const _v = new THREE.Vector3();
const _rel_eye = new THREE.Vector3();
const sceneSphere = new THREE.Sphere();
const _tempEye = new THREE.Vector3();
let ndcControl = undefined;
let maxDist = 0;
const viewports = {
fork icon9
star icon3
watch icon3

How does three.Sphere work?

The three.Sphere class in Three.js represents a 3D sphere with a center position and a radius, and provides methods for calculating intersections with other objects and testing whether a point is contained within the sphere. When creating a new Sphere instance, you provide a center point vector and a radius value, and can optionally specify the number of segments used to create the sphere geometry.

118
119
120
121
122
123
124
125
126
    sizeX = spacingsX.data.reduce((p, v) => p + v, 0);
    sizeY = spacingsY.data.reduce((p, v) => p + v, 0);
    sizeZ = spacingsZ.data.reduce((p, v) => p + v, 0);
}

geometry.boundingSphere = new THREE.Sphere(
    new THREE.Vector3(0.5 * sizeX, 0.5 * sizeY, 0.5 * sizeZ),
    new THREE.Vector3(0.5 * sizeX, 0.5 * sizeY, 0.5 * sizeZ).length(),
);
fork icon4
star icon13
watch icon2

+ 4 other calls in file

2216
2217
2218
2219
2220
2221
2222
2223
2224
  }
}

geometry.boundingBox = box;

var sphere = new THREE.Sphere();

box.getCenter(sphere.center);
sphere.radius = box.min.distanceTo(box.max) / 2;
fork icon3
star icon7
watch icon8

+ 14 other calls in file

Ai Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// Import the necessary components from three.js
import * as THREE from "three";

// Define the properties of the sphere
const radius = 1;
const widthSegments = 32;
const heightSegments = 16;

// Create the sphere geometry
const sphereGeometry = new THREE.SphereGeometry(
  radius,
  widthSegments,
  heightSegments
);

// Create a material for the sphere
const sphereMaterial = new THREE.MeshBasicMaterial({ color: 0xffffff });

// Create a mesh from the geometry and material
const sphereMesh = new THREE.Mesh(sphereGeometry, sphereMaterial);

In this example, SphereGeometry is used to create a sphere mesh geometry by passing in its radius, widthSegments, and heightSegments. A MeshBasicMaterial is also created to give the sphere its white color. Finally, a Mesh is created by combining the sphere geometry and material, which can then be added to a three.js scene for rendering.

4006
4007
4008
4009
4010
4011
4012
4013
4014
    box.expandByVector( maxDisplacement );

}

geometry.boundingBox = box;
const sphere = new THREE.Sphere();
box.getCenter( sphere.center );
sphere.radius = box.min.distanceTo( box.max ) / 2;
geometry.boundingSphere = sphere;
fork icon2
star icon10
watch icon0

2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
            }
        }
        box.expandByVector(maxDisplacement);
    }
    geometry.boundingBox = box;
    const sphere = new three.Sphere();
    box.getCenter(sphere.center);
    sphere.radius = box.min.distanceTo(box.max) / 2;
    geometry.boundingSphere = sphere;
}
fork icon2
star icon7
watch icon0

1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
this._tbRadius = this.calculateTbRadius(this.camera);
if (this.adjustNearFar) {
    const cameraDistance = this.camera.position.distanceTo(this._gizmos.position);
    const bb = new three_1.Box3();
    bb.setFromObject(this._gizmos);
    const sphere = new three_1.Sphere();
    bb.getBoundingSphere(sphere);
    const adjustedNearPosition = Math.max(this._nearPos0, sphere.radius + sphere.center.length());
    const regularNearPosition = cameraDistance - this._initialNear;
    const minNearPos = Math.min(adjustedNearPosition, regularNearPosition);
fork icon1
star icon3
watch icon0

145
146
147
148
149
150
151
152
153

#positionCamera(padding = 1.2) {
	const boundingBox = new THREE.Box3().setFromObject(this.#parent);
	const center = boundingBox.getCenter(new THREE.Vector3());
	const boundingSphere = boundingBox.getBoundingSphere(
		new THREE.Sphere(center),
	);
	const maxDimension = boundingSphere.radius * 2 * padding;
	const size = boundingBox.getSize(new THREE.Vector3());
fork icon0
star icon3
watch icon0

118
119
120
121
122
123
124
125
126
}

function MeshLineRaycast(raycaster, intersects) {
  var inverseMatrix = new THREE.Matrix4()
  var ray = new THREE.Ray()
  var sphere = new THREE.Sphere()
  var interRay = new THREE.Vector3()
  var geometry = this.geometry
  // Checking boundingSphere distance to ray
fork icon0
star icon1
watch icon1

2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873


    box.expandByVector(maxDisplacement);
  }


  geometry.boundingBox = box;
  var sphere = new _three.Sphere();
  box.getCenter(sphere.center);
  sphere.radius = box.min.distanceTo(box.max) / 2;
  geometry.boundingSphere = sphere;
}
fork icon0
star icon1
watch icon0

92
93
94
95
96
97
98
99
100
101
102
103
104
var panVector = new THREE.Vector3(); // Save last transformation


var lastPosition = new THREE.Vector3();
var lastQuaternion = new THREE.Quaternion(); // Tangent sphere to ellipsoid


var pickSphere = new THREE.Sphere();
var pickingPoint = new THREE.Vector3(); // Sphere intersection


var intersection = new THREE.Vector3(); // Set to true to enable target helper

fork icon0
star icon1
watch icon0

190
191
192
193
194
195
196
197
198
199

   return function computeBoundingSphere() {

     if ( this.boundingSphere === null ) {

this.boundingSphere = new THREE.Sphere();

     }

     if ( this.boundingBox === null ) {
fork icon0
star icon0
watch icon1

+ 16 other calls in file

Other functions in three

Sorted by popularity

function icon

three.Vector3 is the most popular function in three (22341 examples)