get bounding sphere of a scene

by jscastro76

HTML

<script id="heightmapFragmentShader" type="x-shader/x-fragment">

			#include <common>

			uniform vec2 mousePos;
			uniform float mouseSize;
			uniform float viscosityConstant;
			uniform float heightCompensation;

			void main()	{

				vec2 cellSize = 1.0 / resolution.xy;

				vec2 uv = gl_FragCoord.xy * cellSize;

				// heightmapValue.x == height from previous frame
				// heightmapValue.y == height from penultimate frame
				// heightmapValue.z, heightmapValue.w not used
				vec4 heightmapValue = texture2D( heightmap, uv );

				// Get neighbours
				vec4 north = texture2D( heightmap, uv + vec2( 0.0, cellSize.y ) );
				vec4 south = texture2D( heightmap, uv + vec2( 0.0, - cellSize.y ) );
				vec4 east = texture2D( heightmap, uv + vec2( cellSize.x, 0.0 ) );
				vec4 west = texture2D( heightmap, uv + vec2( - cellSize.x, 0.0 ) );

				// https://web.archive.org/web/20080618181901/http://freespace.virgin.net/hugo.elias/graphics/x_water.htm

				float newHeight = ( ( north.x + south.x + east.x + west.x ) * 0.5 - heightmapValue.y ) * viscosityConstant;

				// Mouse influence
				float mousePhase = clamp( length( ( uv - vec2( 0.5 ) ) * BOUNDS - vec2( mousePos.x, - mousePos.y ) ) * PI / mouseSize, 0.0, PI );
				newHeight += ( cos( mousePhase ) + 1.0 ) * 0.28;

				heightmapValue.y = heightmapValue.x;
				heightmapValue.x = newHeight;

				gl_FragColor = heightmapValue;

			}

		</script>

		<!-- This is just a smoothing 'compute shader' for using manually: -->
		<script id="smoothFragmentShader" type="x-shader/x-fragment">

			uniform sampler2D smoothTexture;

			void main()	{

				vec2 cellSize = 1.0 / resolution.xy;

				vec2 uv = gl_FragCoord.xy * cellSize;

				// Computes the mean of texel and 4 neighbours
				vec4 textureValue = texture2D( smoothTexture, uv );
				textureValue += texture2D( smoothTexture, uv + vec2( 0.0, cellSize.y ) );
				textureValue += texture2D( smoothTexture, uv + vec2( 0.0, - cellSize.y ) );
				textureValue += texture2D(...

CSS

body {
			background-color: #fff;
			margin: 0px;
			overflow: hidden;
		}

JavaScript

import * as THREE from 'https://threejs.org/build/three.module.js';

			import Stats from 'https://threejs.org/examples/jsm/libs/stats.module.js';
			import {
			  GUI
			} from 'https://threejs.org/examples/jsm/libs/dat.gui.module.js';

			import {
			  GPUComputationRenderer
			} from 'https://threejs.org/examples/jsm/misc/GPUComputationRenderer.js';
			import {
			  SimplexNoise
			} from 'https://threejs.org/examples/jsm/math/SimplexNoise.js';

			// Texture width for simulation
			const WIDTH = 128;

			// Water size in system units
			const BOUNDS = 512;
			const BOUNDS_HALF = BOUNDS * 0.5;

			let container, stats;
			let camera, scene, renderer;
			let mouseMoved = false;
			const mouseCoords = new THREE.Vector2();
			const raycaster = new THREE.Raycaster();

			let waterMesh;
			let meshRay;
			let gpuCompute;
			let heightmapVariable;
			let waterUniforms;
			let smoothShader;
			let readWaterLevelShader;
			let readWaterLevelRenderTarget;
			let readWaterLevelImage;
			const waterNormal = new THREE.Vector3();

			const NUM_SPHERES = 5;
			const spheres = [];
			let spheresEnabled = true;

			const simplex = new SimplexNoise();

			init();
			animate();

			function init() {

			  container = document.createElement('div');
			  document.body.appendChild(container);

			  camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 1, 3000);
			  camera.position.set(0, 200, 350);
			  camera.lookAt(0, 0, 0);

			  scene = new THREE.Scene();

			  const sun = new THREE.DirectionalLight(0xFFFFFF, 1.0);
			  sun.position.set(300, 400, 175);
			  scene.add(sun);

			  const sun2 = new THREE.DirectionalLight(0x40A040, 0.6);
			  sun2.position.set(-100, 350, -200);
			  scene.add(sun2);

			  renderer = new THREE.WebGLRenderer();
			  renderer.setPixelRatio(window.devicePixelRatio);
			  renderer.setSize(window.innerWidth, window.innerHeight);
			  container.appendChild(renderer.domElement);

			  stats = new Stats();
			 ...