源码 下载
/download/qq_34206863/10859181
/download/qq_34206863/12065026
其实可以先看看官方案例
你想要的东西 官方里面都有,就看你找不找得到了。。。。
话不多说
我们先看看这个例子吧
正文更新时间:11月27日
<!DOCTYPE html><html lang="en"><head><title>three.js webgl - FBX loader</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"></head><body><div id="info"><a href="" target="_blank" rel="noopener">three.js</a> - FBXLoader<br />Character and animation from <a href="/" target="_blank" rel="noopener">Mixamo</a></div><script type="module">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';var container, stats, controls;var camera, scene, renderer, light;var clock = new THREE.Clock();var mixer;init();animate();function init() {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 ) );// groundvar 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 );// modelvar 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 );// statsstats = new Stats();container.appendChild( stats.dom );}function onWindowResize() {camera.aspect = window.innerWidth / window.innerHeight;camera.updateProjectionMatrix();renderer.setSize( window.innerWidth, window.innerHeight );}//function animate() {requestAnimationFrame( animate );var delta = clock.getDelta();if ( mixer ) mixer.update( delta );renderer.render( scene, camera );stats.update();}</script></body></html>
首先我们找到这个例子,然后对其解析,化为自己的知识
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库有以上几类
three.js的模块库stats的模块库鼠标控制相机(Camera)库FBXLoader库
FBXLoader.js库,是fbx文件的加载库
所以,我们这个时候应该看看源码。
/*** @author Kyle-Larson /Kyle-Larson* @author Takahiro /takahirox* @author Lewy Blue /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:* /index.php/User:Mont29/Foundation/FBX_File_Structure* /view/FBX//ENU/?guid=__cpp_ref_index_html (C++ SDK reference)** Binary format specification:*//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 );}
这就是它的介绍,至于源码有兴趣的看看吧。
然后我们开始了解如何构造的
//容器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 );//尺寸变换// statsstats = new Stats();//左上角那一坨container.appendChild( stats.dom );
好了,看不看得懂,不要紧,要懂得基本操作就好了,是在不行,自己去例子里面现学现卖。当然懂了最好
最后我们实际运行一下。
ok,over.
对了这些代码需要在服务器上执行,没有服务器?你的ide没有自带么?(webstorm)
更新时间------11月27日