3MFLoader with textures

Hello, the model contains textures, unfortunately amber is only displayed in black?
What can I do?

test1.3mf (109.9 KB)

I use the weggl_loader_3mf_materials.html example.

<!DOCTYPE html>
<html lang="en">
	<head>
		<title>three.js webgl - 3MF</title>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
		<link type="text/css" rel="stylesheet" href="main.css">
		<style>
			body {
				background-color: #a0a0a0;
			}
		</style>
	</head>
	<body>
		<div id="info">
			<a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> -
			<a href="http://3mf.io" target="_blank" rel="noopener">3MF</a> file with materials
		</div>

		<script type="importmap">
			{
				"imports": {
					"three": "../build/three.module.js",
					"three/addons/": "./jsm/"
				}
			}
		</script>

		<script type="module">

			import * as THREE from 'three';

			import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
			import { ThreeMFLoader } from 'three/addons/loaders/3MFLoader.js';

			let camera, scene, renderer;

			init();

			function init() {

				scene = new THREE.Scene();
				scene.background = new THREE.Color( 0xa0a0a0 );
				scene.fog = new THREE.Fog( 0xa0a0a0, 10, 500 );

				camera = new THREE.PerspectiveCamera( 35, window.innerWidth / window.innerHeight, 1, 500 );
				camera.position.set( - 50, 40, 50 );
				scene.add( camera );

				//

				const hemiLight = new THREE.HemisphereLight( 0xffffff, 0x8d8d8d, 3 );
				hemiLight.position.set( 0, 100, 0 );
				scene.add( hemiLight );

				const dirLight = new THREE.DirectionalLight( 0xffffff, 3 );
				dirLight.position.set( - 0, 40, 50 );
				dirLight.castShadow = true;
				dirLight.shadow.camera.top = 50;
				dirLight.shadow.camera.bottom = - 25;
				dirLight.shadow.camera.left = - 25;
				dirLight.shadow.camera.right = 25;
				dirLight.shadow.camera.near = 0.1;
				dirLight.shadow.camera.far = 200;
				dirLight.shadow.mapSize.set( 1024, 1024 );
				scene.add( dirLight );

				// scene.add( new THREE.CameraHelper( dirLight.shadow.camera ) );

				//

				const manager = new THREE.LoadingManager();

				const loader = new ThreeMFLoader( manager );
				loader.load( './models/3mf/test1.3mf', function ( object ) {

					
					//object.rotation.set( - Math.PI / 2, 0, 0 ); // z-up conversion
					object.scale.set(0.1,0.1,0.1);

					object.traverse( function ( child ) {

						child.castShadow = true;

					} );

					scene.add( object );

				} );

				//

				manager.onLoad = function () {

					render();

				};

				//

				const ground = new THREE.Mesh( new THREE.PlaneGeometry( 1000, 1000 ), new THREE.MeshPhongMaterial( { color: 0xcbcbcb, depthWrite: false } ) );
				ground.rotation.x = - Math.PI / 2;
				ground.position.y = 11;
				ground.receiveShadow = true;
				scene.add( ground );

				//

				renderer = new THREE.WebGLRenderer( { antialias: true } );
				renderer.setPixelRatio( window.devicePixelRatio );
				renderer.setSize( window.innerWidth, window.innerHeight );
				renderer.shadowMap.enabled = true;
				renderer.shadowMap.type = THREE.PCFSoftShadowMap;
				document.body.appendChild( renderer.domElement );

				//

				const controls = new OrbitControls( camera, renderer.domElement );
				controls.addEventListener( 'change', render );
				controls.minDistance = 50;
				controls.maxDistance = 200;
				controls.enablePan = false;
				controls.target.set( 0, 20, 0 );
				controls.update();

				window.addEventListener( 'resize', onWindowResize );

				render();

			}

			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>

Hello, I found it myself.

The file is searching for textures incorrectly.
In 3MFLoader.js, line 139 must be changed as follows.

} else if ( file.match( /^3D\/Textures.*\/.*/ ) ) {

Wo kann man das als Verbesserung melden?

1 Like

Do you think you can manage to file a PR here: GitHub - mrdoob/three.js: JavaScript 3D Library.

Hi @metrizzi and @Mugen87 …

I tested two 3mf files… the truck.3mf file responds with colors and the test1.3mf comes without colors… changing as requested by @metrizzi, both responded with colors.

I couldn’t see the texture for the truck logo and one other 3MF model wouldn’t load a texture.

This seems to work for all 3MF files I tested and would suggest to @Mugen87 to actually file a PR since it would probably be the least painful way:

} else if ( file.match( /^3D\/Textures?.*/ ) ) {

1 Like