three.js加载.obj文件和贴图.mtl

使用three.js加载obj文件和贴图.mtl
<!DOCTYPE html>
<html lang="en">
	<head>
		<title>three.js webgl - OBJLoader2 basic usage</title>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">

		<style>
			body {
				font-family: Monospace;
				background-color: #000;
				color: #fff;
				margin: 0 0 0 0;
				padding: 0 0 0 0;
				border: none;
				cursor: default;
			}
			#info {
				color: #fff;
				position: absolute;
				top: 10px;
				width: 100%;
				text-align: center;
				z-index: 100;
				display:block;
			}
			#info a {
				color: #f00;
				font-weight: bold;
				text-decoration: underline;
				cursor: pointer
			}
			#glFullscreen {
				width: 100%;
				height: 100vh;
				min-width: 640px;
				min-height: 360px;
				position: relative;
				overflow: hidden;
				z-index: 0;
			}
			#example {
				width: 100%;
				height: 100%;
				top: 0;
				left: 0;
				background-color: #000000;
			}
			#feedback {
				color: darkorange;
			}
			#dat {
				user-select: none;
				position: absolute;
				left: 0;
				top: 0;
				z-Index: 200;
			}
		</style>

	</head>

	<body>
		<div id="glFullscreen">
			<canvas id="example"></canvas>
		</div>
		<div id="dat">

		</div>
		<div id="info">
			<a href="http://threejs.org" target="_blank" rel="noopener">three.js</a> - OBJLoader2 direct loader test
			<div id="feedback"></div>
		</div>

		<script src="js/Detector.js"></script>
		<script src="../build/three.js"></script>
		<script src="js/controls/TrackballControls.js"></script>
		<script src="js/loaders/MTLLoader.js"></script>
		<script src="js/libs/dat.gui.min.js"></script>

		<script src="js/loaders/LoaderSupport.js"></script>
		<script src="js/loaders/OBJLoader2.js"></script>
		<script>

			'use strict';

			var OBJLoader2Example = (function () {

				var Validator = THREE.LoaderSupport.Validator;

				function OBJLoader2Example( elementToBindTo ) {
					this.renderer = null;
					this.canvas = elementToBindTo;
					this.aspectRatio = 1;
					this.recalcAspectRatio();

					this.scene = null;
					this.cameraDefaults = {
						posCamera: new THREE.Vector3( 0.0, 175.0, 500.0 ),
						posCameraTarget: new THREE.Vector3( 0, 0, 0 ),
						near: 0.1,
						far: 10000,
						fov: 45
					};
					this.camera = null;
					this.cameraTarget = this.cameraDefaults.posCameraTarget;

					this.controls = null;
				}

				OBJLoader2Example.prototype.initGL = function () {
					this.renderer = new THREE.WebGLRenderer( {
						canvas: this.canvas,
						antialias: true,   //是否执行考锯齿
						autoClear: true
					} );
					this.renderer.setClearColor( 0x050505 );

					this.scene = new THREE.Scene();

					this.camera = new THREE.PerspectiveCamera( this.cameraDefaults.fov, this.aspectRatio, this.cameraDefaults.near, this.cameraDefaults.far );
					this.resetCamera();				//重置相机
					this.controls = new THREE.TrackballControls( this.camera, this.renderer.domElement );

					var ambientLight = new THREE.AmbientLight( 0x404040 );
					var directionalLight1 = new THREE.DirectionalLight( 0xC0C090 );	
					var directionalLight2 = new THREE.DirectionalLight( 0xC0C090 );

					directionalLight1.position.set( -100, -50, 100 );
					directionalLight2.position.set( 100, 50, -100 );

					this.scene.add( directionalLight1 );	//添加方向光
					this.scene.add( directionalLight2 );
					this.scene.add( ambientLight );		//添加自然光

					var helper = new THREE.GridHelper( 1200, 60, 0xFF4444, 0x404040 );//定义网格系统
					this.scene.add( helper );
				};

				OBJLoader2Example.prototype.initContent = function () {
					var modelName = 'female02';
					this._reportProgress( { detail: { text: 'Loading: ' + modelName } } );

					var scope = this;
					var objLoader = new THREE.OBJLoader2();
					var callbackOnLoad = function ( event ) {
						scope.scene.add( event.detail.loaderRootNode );
						console.log( 'Loading complete: ' + event.detail.modelName );
						scope._reportProgress( { detail: { text: '' } } );
					};

					var onLoadMtl = function ( materials ) {
						objLoader.setModelName( modelName );
						objLoader.setMaterials( materials );
						objLoader.getLogger().setDebug( true );
						objLoader.load( 'obj/female02/location_skybox.obj', callbackOnLoad, null, null, null, false );
					};
					objLoader.loadMtl( 'obj/female02/location_skybox.mtl', null, onLoadMtl );
				};

				OBJLoader2Example.prototype._reportProgress = function( event ) {
					var output = Validator.verifyInput( event.detail.text, '' );
					console.log( 'Progress: ' + output );
					document.getElementById( 'feedback' ).innerHTML = output;
				};

				OBJLoader2Example.prototype.resizeDisplayGL = function () {
					this.controls.handleResize();

					this.recalcAspectRatio();
					this.renderer.setSize( this.canvas.offsetWidth, this.canvas.offsetHeight, false );

					this.updateCamera();
				};

				OBJLoader2Example.prototype.recalcAspectRatio = function () {
					this.aspectRatio = ( this.canvas.offsetHeight === 0 ) ? 1 : this.canvas.offsetWidth / this.canvas.offsetHeight;
				};

				OBJLoader2Example.prototype.resetCamera = function () {
					this.camera.position.copy( this.cameraDefaults.posCamera );
					this.cameraTarget.copy( this.cameraDefaults.posCameraTarget );

					this.updateCamera();
				};

				OBJLoader2Example.prototype.updateCamera = function () {
					this.camera.aspect = this.aspectRatio;
					this.camera.lookAt( this.cameraTarget );
					this.camera.updateProjectionMatrix();//更新相机投影矩阵。参数更改后必须调用。
				};

				OBJLoader2Example.prototype.render = function () {
					if ( ! this.renderer.autoClear ) this.renderer.clear();
					this.controls.update();
					this.renderer.render( this.scene, this.camera );
				};

				return OBJLoader2Example;

			})();

			var app = new OBJLoader2Example( document.getElementById( 'example' ) );

			var resizeWindow = function () {
				app.resizeDisplayGL();
			};

			var render = function () {
				requestAnimationFrame( render );
				app.render();
			};

			window.addEventListener( 'resize', resizeWindow, false );

			console.log( 'Starting initialisation phase...' );
			app.initGL();
			app.resizeDisplayGL();
			app.initContent();

			render();

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

  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用three.js加载.obj和.mtl文件可以通过以下步骤完成: 1. 首先,你需要引入three.js文件,通过在HTML页面中添加如下代码来导入库文件: ``` <script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/110/three.min.js"></script> ``` 2. 创建一个用于显示3D模型的容器,可以是一个div元素或者canvas元素。例如: ``` <div id="canvas"></div> ``` 3. 在Javascript代码中,创建一个场景(scene)、摄像机(camera)和渲染器(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.getElementById("canvas").appendChild(renderer.domElement); ``` 4. 使用OBJLoader加载.obj文件MTLLoader加载.mtl文件。确保将路径指定为正确的模型文件路径。例如: ``` var objLoader = new THREE.OBJLoader(); var mtlLoader = new THREE.MTLLoader(); mtlLoader.load('model.mtl', function (materials) { materials.preload(); objLoader.setMaterials(materials); objLoader.load('model.obj', function (object) { scene.add(object); }); }); ``` 5. 设置摄像机的位置以及添加光源: ``` camera.position.z = 5; var light = new THREE.PointLight(0xFFFFFF, 1, 100); light.position.set(10, 10, 10); scene.add(light); ``` 6. 创建一个渲染函数,以及在每帧更新场景的方法: ``` function render() { requestAnimationFrame(render); renderer.render(scene, camera); } render(); ``` 以上代码可以让你使用three.js加载.obj和.mtl文件,并在页面上显示出3D模型。尽量准备好正确路径下的.obj和.mtl文件以及相关的纹理文件

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值