* Sets this vector to a uniformly random point on a unit sphere. * * @return {Vector3} A reference to this vector.
()
| 1232 | * @return {Vector3} A reference to this vector. |
| 1233 | */ |
| 1234 | randomDirection() { |
| 1235 | |
| 1236 | // https://mathworld.wolfram.com/SpherePointPicking.html |
| 1237 | |
| 1238 | const theta = Math.random() * Math.PI * 2; |
| 1239 | const u = Math.random() * 2 - 1; |
| 1240 | const c = Math.sqrt( 1 - u * u ); |
| 1241 | |
| 1242 | this.x = c * Math.cos( theta ); |
| 1243 | this.y = u; |
| 1244 | this.z = c * Math.sin( theta ); |
| 1245 | |
| 1246 | return this; |
| 1247 | |
| 1248 | } |
| 1249 | |
| 1250 | *[ Symbol.iterator ]() { |
| 1251 |