Skip to content

Instantly share code, notes, and snippets.

@detain
Created May 12, 2023 19:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save detain/af1e59c20129ddb770300a86f9366bcc to your computer and use it in GitHub Desktop.
Save detain/af1e59c20129ddb770300a86f9366bcc to your computer and use it in GitHub Desktop.
three.js noters and stuff

To create a three.js page that loads a 3D model, you'll need to do the following steps:

  1. Create an HTML file that includes the necessary scripts and libraries.
<!DOCTYPE html>
<html>
  <head>
    <title>Three.js Model Loader</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <script src="https://cdn.jsdelivr.net/npm/three@0.133.2/build/three.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/three@0.133.2/examples/js/loaders/MTLLoader.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/three@0.133.2/examples/js/loaders/OBJLoader.js"></script>
  </head>
  <body>
    <div id="model-container"></div>
    <script src="js/main.js"></script>
  </body>
</html>

In this example, we're using the latest version of three.js, as well as the MTLLoader and OBJLoader scripts from the examples folder.

  1. Create a main.js file that loads the model and adds it to the scene.
// Initialize the scene, camera and renderer
var scene = new THREE.Scene();
var camera = new THREE.PerspectiveCamera( 75, window.innerWidth/window.innerHeight, 0.1, 1000 );
var renderer = new THREE.WebGLRenderer();
renderer.setSize( window.innerWidth, window.innerHeight );
document.body.appendChild( renderer.domElement );

// Load the materials and geometry of the model
var mtlLoader = new THREE.MTLLoader();
mtlLoader.load( 'model.mtl', function( materials ) {
  materials.preload();

  var objLoader = new THREE.OBJLoader();
  objLoader.setMaterials( materials );
  objLoader.load( 'model.obj', function ( object ) {
    scene.add( object );
  });
});

// Add lights to the scene
var ambientLight = new THREE.AmbientLight( 0xcccccc, 0.4 );
scene.add( ambientLight );

var pointLight = new THREE.PointLight( 0xffffff, 0.8 );
camera.add( pointLight );
scene.add( camera );

// Set the position of the camera
camera.position.z = 5;

// Render the scene
function animate() {
  requestAnimationFrame( animate );
  renderer.render( scene, camera );
}
animate();

In this example, we're using MTLLoader to load the materials of the model, and OBJLoader to load the geometry. Once both the materials and geometry have been loaded, we add the object to the scene. We're also adding ambient and point lights to the scene and positioning the camera to view the object.

  1. Place the model files and images in the correct location within your project directory.

Your project directory should look something like this:

project/
  index.html
  js/
    main.js
  model/
    model.mtl
    model.obj
  img/
    texture.jpg
  1. Open the index.html file in your browser to view the loaded 3D model.

That's it! You should now have a three.js page that loads your 3D model.

Sure, here's an example code snippet on how to load a model with three.js using the provided files:

// create a new scene
var scene = new THREE.Scene();

// create a new camera
var camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 0.1, 1000 );
camera.position.z = 5;

// create a new renderer
var renderer = new THREE.WebGLRenderer();
renderer.setSize( window.innerWidth, window.innerHeight );
document.body.appendChild( renderer.domElement );

// load in the material
var mtlLoader = new THREE.MTLLoader();
mtlLoader.load( 'model.mtl', function( materials ) {
    materials.preload();

    // load in the object using the materials
    var objLoader = new THREE.OBJLoader();
    objLoader.setMaterials( materials );
    objLoader.load( 'model.obj', function ( object ) {
        // apply textures to the object
        object.traverse( function ( child ) {
            if ( child instanceof THREE.Mesh ) {
                child.material.map = THREE.ImageUtils.loadTexture( 'texture.jpg' );
            }
        } );

        // add the object to the scene
        scene.add( object );
    } );
} );

Make sure to replace the filenames with the appropriate ones for your files [3], and adjust the camera position and renderer size to fit your needs. Additionally, if your model has more than one texture file, you'll need to adjust the code to apply the correct texture to each object.

To create a Three.js page that loads a model, you will need to include the Three.js library in your HTML file and write the JavaScript code to load the model.

Here is an example code that you can modify to fit your specific requirements:

<!DOCTYPE html>
<html>
<head>
	<title>My Three.js Model</title>
	<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r125/three.min.js"></script>
</head>
<body>

	<script>
		// Initialize the scene, camera, and renderer
		var scene = new THREE.Scene();
		var camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
		var renderer = new THREE.WebGLRenderer();
		renderer.setSize(window.innerWidth, window.innerHeight);
		document.body.appendChild(renderer.domElement);

		// Load the model
		var loader = new THREE.MTLLoader();
		loader.load('model.mtl', function(materials) {
			materials.preload();
			var objLoader = new THREE.OBJLoader();
			objLoader.setMaterials(materials);
			objLoader.load('model.obj', function(object) {
				scene.add(object);
				// Position the camera to see the model
				object.position.set(0, 0, 0);
				camera.position.z = 5;
			});
		});

		// Add lighting to the scene
		var pointLight = new THREE.PointLight(0xffffff);
		pointLight.position.set(10, 10, 10);
		scene.add(pointLight);

		// Render the scene
		function animate() {
			requestAnimationFrame(animate);
			renderer.render(scene, camera);
		}
		animate();

	</script>
</body>
</html>

In the example code, replace 'model.obj' and 'model.mtl' with the filenames of your model files. Make sure all the files are in the same directory as your HTML file.

You may also need to specify an image for the model's texture by providing the path to the image file in the MTL file. Make sure the path to the image file is correct.

The code above should display your model in the browser window once you open the HTML document.

To create a Three.js page that loads the model, you will need to follow these steps:

  1. Download the Three.js library and include it in your HTML file.
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"></script>
  1. Create a new scene, camera, and renderer.
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
const renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
  1. Load the model and textures.
const loader = new THREE.MTLLoader();
loader.load('model.mtl', function (materials) {
    materials.preload();
    const objLoader = new THREE.OBJLoader();
    objLoader.setMaterials(materials);
    objLoader.load('model.obj', function (object) {
        scene.add(object);
    });
});

const textureLoader = new THREE.TextureLoader();
textureLoader.load('texture.jpg', function (texture) {
    // Apply texture to object or material
});
  1. Add lights to the scene.
const ambientLight = new THREE.AmbientLight(0xffffff, 0.5);
scene.add(ambientLight);

const directionalLight = new THREE.DirectionalLight(0xffffff, 0.5);
directionalLight.position.set(0, 1, 0);
scene.add(directionalLight);
  1. Update the camera position and render the scene.
camera.position.z = 5;

function animate() {
    requestAnimationFrame(animate);
    renderer.render(scene, camera);
}
animate();

Putting it all together, your code should look something like this:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>Three.js Model Loader</title>
    </head>
    <body>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"></script>
        <script src="https://threejs.org/examples/js/loaders/MTLLoader.js"></script>
        <script src="https://threejs.org/examples/js/loaders/OBJLoader.js"></script>
        <script>
            const scene = new THREE.Scene();
            const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
            const renderer = new THREE.WebGLRenderer();
            renderer.setSize(window.innerWidth, window.innerHeight);
            document.body.appendChild(renderer.domElement);

            const loader = new THREE.MTLLoader();
            loader.load('model.mtl', function (materials) {
                materials.preload();
                const objLoader = new THREE.OBJLoader();
                objLoader.setMaterials(materials);
                objLoader.load('model.obj', function (object) {
                    scene.add(object);
                });
            });

            const textureLoader = new THREE.TextureLoader();
            textureLoader.load('texture.jpg', function (texture) {
                // Apply texture to object or material
            });

            const ambientLight = new THREE.AmbientLight(0xffffff, 0.5);
            scene.add(ambientLight);

            const directionalLight = new THREE.DirectionalLight(0xffffff, 0.5);
            directionalLight.position.set(0, 1, 0);
            scene.add(directionalLight);

            camera.position.z = 5;

            function animate() {
                requestAnimationFrame(animate);
                renderer.render(scene, camera);
            }
            animate();
        </script>
    </body>
</html>

Make sure to replace `model

To load the model in a three.js page, you can use the following code:

// create a scene
var scene = new THREE.Scene();

// create a camera
var camera = new THREE.PerspectiveCamera(75, window.innerWidth/window.innerHeight, 0.1, 1000);
camera.position.z = 5;

// create a renderer
var renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);

// load the model
var mtlLoader = new THREE.MTLLoader();
mtlLoader.load('model.mtl', function(materials) {
    materials.preload();

    var objLoader = new THREE.OBJLoader();
    objLoader.setMaterials(materials);
    objLoader.load('model.obj', function(object) {
        scene.add(object);
    });
});

// add lighting to the scene
var light = new THREE.AmbientLight(0xffffff);
scene.add(light);

// render the scene
function animate() {
    requestAnimationFrame(animate);
    renderer.render(scene, camera);
}
animate();

Make sure to replace model.mtl and model.obj with the correct filenames for your files [3], and to include all necessary texture maps in the MTL file. Additionally, you can add lighting and other visual effects to the scene as desired.

To load a 3D model with textures into a Three.js scene, you can use the MTLLoader and OBJLoader classes. Here's an example HTML file that loads a model and its texture images:

<!DOCTYPE html>
<html>
<head>
	<title>Three.js Model Loading Example</title>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">

	<!-- include Three.js library -->
	<script src="https://cdn.jsdelivr.net/npm/three@0.131.2/build/three.min.js"></script>

	<!-- include MTLLoader and OBJLoader -->
	<script src="https://cdn.jsdelivr.net/npm/three@0.131.2/examples/js/loaders/MTLLoader.js"></script>
	<script src="https://cdn.jsdelivr.net/npm/three@0.131.2/examples/js/loaders/OBJLoader.js"></script>

	<style>
		body { margin: 0; }
		canvas { display: block; }
	</style>
</head>
<body>

	<!-- create a canvas element to render the scene -->
	<canvas id="canvas"></canvas>

	<script>
		// create the Three.js scene
		var scene = new THREE.Scene();
		var camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
		var renderer = new THREE.WebGLRenderer({ canvas: document.getElementById("canvas"), antialias: true });
		renderer.setSize(window.innerWidth, window.innerHeight);

		// create a light source to illuminate the scene
		var light = new THREE.DirectionalLight(0xffffff, 1);
		light.position.set(0, 1, 0);
		scene.add(light);

		// load the model and its texture images
		var mtlLoader = new THREE.MTLLoader();
		mtlLoader.load('model.mtl', function(materials) {
			materials.preload();
			var objLoader = new THREE.OBJLoader();
			objLoader.setMaterials(materials);
			objLoader.load('model.obj', function(object) {
				scene.add(object);
			});
		});

		// adjust the camera position to view the scene
		camera.position.z = 5;

		// render the scene
		function animate() {
			requestAnimationFrame(animate);
			renderer.render(scene, camera);
		}
		animate();
	</script>

</body>
</html>

Make sure to update the paths to the model files (model.mtl and model.obj) in the MTLLoader and OBJLoader calls. You can also adjust the camera position and light source to best showcase your model.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment