Three.js fbx文件导入

源码 下载
https://download.csdn.net/download/qq_34206863/10859181
https://download.csdn.net/download/qq_34206863/12065026
其实可以先看看官方案例
你想要的东西 官方里面都有,就看你找不找得到了。。。。
话不多说
我们先看看这个例子吧

正文更新时间:2019年11月27日



	
		three.js webgl - FBX loader
		
		
		
	

	
		
three.js - FBXLoader
Character and animation from Mixamo

首先我们找到这个例子,然后对其解析,化为自己的知识

import * as THREE from '../build/three.module.js'; 
import Stats from './jsm/libs/stats.module.js';
import { OrbitControls } from './jsm/controls/OrbitControls.js';
import { FBXLoader } from './jsm/loaders/FBXLoader.js';

我们看到引入的js库有以上几类

  1. three.js的模块库
  2. stats的模块库
  3. 鼠标控制相机(Camera)库
  4. FBXLoader库

FBXLoader.js库,是fbx文件的加载库
所以,我们这个时候应该看看源码。

/**
 * @author Kyle-Larson https://github.com/Kyle-Larson
 * @author Takahiro https://github.com/takahirox
 * @author Lewy Blue https://github.com/looeee
 *
 * Loader loads FBX file and generates Group representing FBX scene.
 * Requires FBX file to be >= 7.0 and in ASCII or >= 6400 in Binary format
 * Versions lower than this may load but will probably have errors
 *
 * Needs Support:
 *  Morph normals / blend shape normals
 *
 * FBX format references:
 * 	https://wiki.blender.org/index.php/User:Mont29/Foundation/FBX_File_Structure
 * 	http://help.autodesk.com/view/FBX/2017/ENU/?guid=__cpp_ref_index_html (C++ SDK reference)
 *
 * 	Binary format specification:
 *		https://code.blender.org/2013/08/fbx-binary-file-format-specification/
 */

恩,一来就看到fbx的格式要求
看正文

( function () {
//构造函数
	function FBXLoader( manager ) {
		Loader.call( this, manager );
	}
	//对fbx对象的配置处理
	FBXLoader.prototype = Object.assign( Object.create( Loader.prototype ), {...});
		//读取
		load: function ( url, onLoad, onProgress, onError ) {
			var self = this;
			var path = ( self.path === '' ) ? LoaderUtils.extractUrlBase( url ) : self.path;
			var loader = new FileLoader( this.manager );
			loader.setPath( self.path );
			loader.setResponseType( 'arraybuffer' );
			loader.load( url, function ( buffer ) {
				try {
					onLoad( self.parse( buffer, path ) );
				} catch ( error ) {
					setTimeout( function () {
						if ( onError ) onError( error );
						self.manager.itemError( url );
					}, 0 );
				}
			}, onProgress, onError );
		},
		//解析
		parse: function ( FBXBuffer, path ) {
			if ( isFbxFormatBinary( FBXBuffer ) ) {
				fbxTree = new BinaryParser().parse( FBXBuffer );
			} else {
				var FBXText = convertArrayBufferToString( FBXBuffer );
				if ( ! isFbxFormatASCII( FBXText ) ) {
					throw new Error( 'THREE.FBXLoader: Unknown format.' );
				}
				if ( getFbxVersion( FBXText ) < 7000 ) {
					throw new Error( 'THREE.FBXLoader: FBX version not supported, FileVersion: ' + getFbxVersion( FBXText ) );
				}
				fbxTree = new TextParser().parse( FBXText );
			}
			// console.log( fbxTree );

			var textureLoader = new TextureLoader( this.manager ).setPath( this.resourcePath || path ).setCrossOrigin( this.crossOrigin );

			return new FBXTreeParser( textureLoader, this.manager ).parse( fbxTree );

		}

Three.js fbx文件导入_第1张图片
这就是它的介绍,至于源码有兴趣的看看吧。

然后我们开始了解如何构造的

	//容器
	container = document.createElement( 'div' );
	document.body.appendChild( container );
	//相机(充当你的眼睛)
	camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 2000 );
	camera.position.set( 100, 200, 300 );//位置
	//场景
	scene = new THREE.Scene();
	scene.background = new THREE.Color( 0xa0a0a0 );//背景色
	scene.fog = new THREE.Fog( 0xa0a0a0, 200, 1000 );//雾化
	//灯光
	light = new THREE.HemisphereLight( 0xffffff, 0x444444 );//没有光线,将啥也看不到
	light.position.set( 0, 200, 0 );
	scene.add( light );

	light = new THREE.DirectionalLight( 0xffffff );
	light.position.set( 0, 200, 100 );
	light.castShadow = true;
	light.shadow.camera.top = 180;
	light.shadow.camera.bottom = - 100;
	light.shadow.camera.left = - 120;
	light.shadow.camera.right = 120;
	scene.add( light );

	// scene.add( new CameraHelper( light.shadow.camera ) );

	// ground 广场,整个大地
	var mesh = new THREE.Mesh( new THREE.PlaneBufferGeometry( 2000, 2000 ), new THREE.MeshPhongMaterial( { color: 0x999999, depthWrite: false } ) );//材质
	mesh.rotation.x = - Math.PI / 2;
	mesh.receiveShadow = true;
	scene.add( mesh );
	//格子
	var grid = new THREE.GridHelper( 2000, 20, 0x000000, 0x000000 );
	grid.material.opacity = 0.2;
	grid.material.transparent = true;
	scene.add( grid );

	// model 模型
	var loader = new FBXLoader();
	loader.load( 'models/fbx/ball.fbx', function ( object ) {
		//获取动画
		mixer = new THREE.AnimationMixer( object );
		var action = mixer.clipAction( object.animations[ 0 ] );
		action.play();//播放
		object.traverse( function ( child ) {
			if ( child.isMesh ) {//材质
				child.castShadow = true;
				child.receiveShadow = true;
			}
		} );
		
		scene.add( object );
	} );
	//渲染
	renderer = new THREE.WebGLRenderer( { antialias: true } );
	renderer.setPixelRatio( window.devicePixelRatio );
	renderer.setSize( window.innerWidth, window.innerHeight );
	renderer.shadowMap.enabled = true;
	container.appendChild( renderer.domElement );

	controls = new OrbitControls( camera, renderer.domElement );
	controls.target.set( 0, 100, 0 );
	controls.update();

	window.addEventListener( 'resize', onWindowResize, false );//尺寸变换
	// stats
	stats = new Stats();//左上角那一坨
	container.appendChild( stats.dom );

好了,看不看得懂,不要紧,要懂得基本操作就好了,是在不行,自己去例子里面现学现卖。当然懂了最好
最后我们实际运行一下。
Three.js fbx文件导入_第2张图片
ok,over.
对了这些代码需要在服务器上执行,没有服务器?你的ide没有自带么?(webstorm)


更新时间------2019年11月27日

你可能感兴趣的:(html,three.js,WebGL)