Object becomes hidden behind background when zooming out

Hello everyone!

I’m currently trying to make a minimal gltf viewer that loads from a hard-coded filepath. I can load my object just fine, but when I zoom out, it looks like the object gradually moves behind the background image and becomes hidden. Could someone help me figure out why?

I’m currently using examples/webgl_loader_gltf.html as a template and can view my object just file. So my code below almost an exact match – the key difference is just the object that I’m loading as well as the camera distance settings.

Here’s the object (and hdr) I’m observing this with:
crystal_structure.glb (238.3 KB)
quarry_01_1k.hdr (1.4 MB)

<!DOCTYPE html>
<html lang="en">
	<head>
		...
	</head>

	<body>
		<script type="module">

			import * as THREE from 'https://cdn.skypack.dev/three@0.132.2';
            
            import { OrbitControls } from 'https://cdn.skypack.dev/three@0.132.2/examples/jsm/controls/OrbitControls.js';
			import { TrackballControls } from 'https://cdn.skypack.dev/three@0.132.2/examples/jsm/controls/TrackballControls.js';
			import { GLTFLoader } from 'https://cdn.skypack.dev/three@0.132.2/examples/jsm/loaders/GLTFLoader.js';
			import { RGBELoader } from 'https://cdn.skypack.dev/three@0.132.2/examples/jsm/loaders/RGBELoader.js';
			import { RoughnessMipmapper } from 'https://cdn.skypack.dev/three@0.132.2/examples/jsm/utils/RoughnessMipmapper.js';

			let camera, scene, renderer;

			init();
			render();

			function init() {

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

				camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 0.25, 20 );
				camera.position.set( - 18, 6, 27 );

				scene = new THREE.Scene();

				new RGBELoader()
					.load( '/path/to//quarry_01_1k.hdr', function ( texture ) {

						texture.mapping = THREE.EquirectangularReflectionMapping;

						scene.background = texture;
						scene.environment = texture;

						render();

						// model

						// use of RoughnessMipmapper is optional
						const roughnessMipmapper = new RoughnessMipmapper( renderer );
                        
                        
                        
                        {% load static %}
						const loader = new GLTFLoader();
						loader.load( 'path/to/crytal_structure.glb', function ( gltf ) {

							gltf.scene.traverse( function ( child ) {

								if ( child.isMesh ) {

									roughnessMipmapper.generateMipmaps( child.material );

								}

							} );

							scene.add( gltf.scene );

							roughnessMipmapper.dispose();

							render();

						} );

					} );

				renderer = new THREE.WebGLRenderer( { antialias: true } );
				renderer.setPixelRatio( window.devicePixelRatio );
				renderer.setSize( window.innerWidth, window.innerHeight );
				renderer.toneMapping = THREE.ACESFilmicToneMapping;
				renderer.toneMappingExposure = 1;
				renderer.outputEncoding = THREE.sRGBEncoding;
				container.appendChild( renderer.domElement );

				const controls = new OrbitControls( camera, renderer.domElement );
				
				controls.addEventListener( 'change', render ); // use if there is no animation loop
				controls.zoomSpeed = 2;
				controls.minDistance = 2;
				controls.maxDistance = 15;
				controls.maxPolarAngle = Infinity;
				controls.update();

				window.addEventListener( 'resize', onWindowResize );

			}

			function onWindowResize() {

				camera.aspect = window.innerWidth / window.innerHeight;
				camera.updateProjectionMatrix();

				renderer.setSize( window.innerWidth, window.innerHeight );

				render();

			}

			//

			function render() {

				renderer.render( scene, camera );

			}

		</script>

	</body>
</html>

Thank you in advance for the help! I’ve spent an embarrassing amount of time trying to fix this lol.

-Jack

Hi!

Try to set far property of the camera greater than 20. For example, 1000:
camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 0.25, 1000 );

1 Like

Haha oh wow, that was easy. Thank you for the quick reply! That did the trick :smile:

You’re welcome :beers: