-
-
Notifications
You must be signed in to change notification settings - Fork 35.8k
Examples: Add textured assets to webgl_loader_fbx
.
#31057
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,226 @@ | ||
<!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="https://threejs.org" target="_blank" rel="noopener">three.js</a> - FBXLoader<br /> | ||
Fbx with texture from blender suzanne | ||
</div> | ||
|
||
<script type="importmap"> | ||
{ | ||
"imports": { | ||
"three": "../build/three.module.js", | ||
"three/addons/": "./jsm/" | ||
} | ||
} | ||
</script> | ||
|
||
<script type="module"> | ||
|
||
import * as THREE from 'three'; | ||
|
||
import Stats from 'three/addons/libs/stats.module.js'; | ||
|
||
import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; | ||
import { FBXLoader } from 'three/addons/loaders/FBXLoader.js'; | ||
import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; | ||
|
||
const manager = new THREE.LoadingManager(); | ||
|
||
let camera, scene, renderer, stats, object, loader, guiMorphsFolder; | ||
let mixer; | ||
|
||
const clock = new THREE.Clock(); | ||
|
||
const params = { | ||
asset: 'monkey' | ||
}; | ||
|
||
const assets = [ | ||
'monkey', | ||
'monkey_embedded_texture' | ||
]; | ||
|
||
|
||
init(); | ||
|
||
function init() { | ||
|
||
const 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 ); | ||
|
||
const hemiLight = new THREE.HemisphereLight( 0xffffff, 0x444444, 5 ); | ||
hemiLight.position.set( 0, 200, 0 ); | ||
scene.add( hemiLight ); | ||
|
||
const dirLight = new THREE.DirectionalLight( 0xffffff, 5 ); | ||
dirLight.position.set( 0, 200, 100 ); | ||
dirLight.castShadow = true; | ||
dirLight.shadow.camera.top = 180; | ||
dirLight.shadow.camera.bottom = - 100; | ||
dirLight.shadow.camera.left = - 120; | ||
dirLight.shadow.camera.right = 120; | ||
scene.add( dirLight ); | ||
|
||
// scene.add( new THREE.CameraHelper( dirLight.shadow.camera ) ); | ||
|
||
// ground | ||
const mesh = new THREE.Mesh( new THREE.PlaneGeometry( 2000, 2000 ), new THREE.MeshPhongMaterial( { color: 0x999999, depthWrite: false } ) ); | ||
mesh.rotation.x = - Math.PI / 2; | ||
mesh.receiveShadow = true; | ||
scene.add( mesh ); | ||
|
||
const grid = new THREE.GridHelper( 2000, 20, 0x000000, 0x000000 ); | ||
grid.material.opacity = 0.2; | ||
grid.material.transparent = true; | ||
scene.add( grid ); | ||
|
||
loader = new FBXLoader( manager ); | ||
loadAsset( params.asset ); | ||
|
||
renderer = new THREE.WebGLRenderer( { antialias: true } ); | ||
renderer.setPixelRatio( window.devicePixelRatio ); | ||
renderer.setSize( window.innerWidth, window.innerHeight ); | ||
renderer.setAnimationLoop( animate ); | ||
renderer.shadowMap.enabled = true; | ||
container.appendChild( renderer.domElement ); | ||
|
||
const controls = new OrbitControls( camera, renderer.domElement ); | ||
controls.target.set( 0, 100, 0 ); | ||
controls.update(); | ||
|
||
window.addEventListener( 'resize', onWindowResize ); | ||
|
||
// stats | ||
stats = new Stats(); | ||
container.appendChild( stats.dom ); | ||
|
||
const gui = new GUI(); | ||
gui.add( params, 'asset', assets ).onChange( function ( value ) { | ||
|
||
loadAsset( value ); | ||
|
||
} ); | ||
|
||
guiMorphsFolder = gui.addFolder( 'Morphs' ).hide(); | ||
|
||
} | ||
|
||
function loadAsset( asset ) { | ||
|
||
loader.load( 'models/fbx/' + asset + '.fbx', function ( group ) { | ||
|
||
if ( object ) { | ||
|
||
object.traverse( function ( child ) { | ||
|
||
if ( child.material ) { | ||
|
||
const materials = Array.isArray( child.material ) ? child.material : [ child.material ]; | ||
materials.forEach( material => { | ||
|
||
if ( material.map ) material.map.dispose(); | ||
material.dispose(); | ||
|
||
} ); | ||
|
||
} | ||
|
||
if ( child.geometry ) child.geometry.dispose(); | ||
|
||
} ); | ||
|
||
scene.remove( object ); | ||
|
||
} | ||
|
||
// | ||
|
||
object = group; | ||
|
||
if ( object.animations && object.animations.length ) { | ||
|
||
mixer = new THREE.AnimationMixer( object ); | ||
|
||
const action = mixer.clipAction( object.animations[ 0 ] ); | ||
action.play(); | ||
|
||
} else { | ||
|
||
mixer = null; | ||
|
||
} | ||
|
||
guiMorphsFolder.children.forEach( ( child ) => child.destroy() ); | ||
guiMorphsFolder.hide(); | ||
|
||
object.traverse( function ( child ) { | ||
|
||
if ( child.isMesh ) { | ||
|
||
child.castShadow = true; | ||
child.receiveShadow = true; | ||
|
||
if ( child.morphTargetDictionary ) { | ||
|
||
guiMorphsFolder.show(); | ||
const meshFolder = guiMorphsFolder.addFolder( child.name || child.uuid ); | ||
Object.keys( child.morphTargetDictionary ).forEach( ( key ) => { | ||
|
||
meshFolder.add( child.morphTargetInfluences, child.morphTargetDictionary[ key ], 0, 1, 0.01 ); | ||
|
||
} ); | ||
|
||
} | ||
|
||
} | ||
|
||
} ); | ||
|
||
scene.add( object ); | ||
|
||
} ); | ||
|
||
} | ||
|
||
function onWindowResize() { | ||
|
||
camera.aspect = window.innerWidth / window.innerHeight; | ||
camera.updateProjectionMatrix(); | ||
|
||
renderer.setSize( window.innerWidth, window.innerHeight ); | ||
|
||
} | ||
|
||
// | ||
|
||
function animate() { | ||
|
||
const delta = clock.getDelta(); | ||
|
||
if ( mixer ) mixer.update( delta ); | ||
|
||
renderer.render( scene, camera ); | ||
|
||
stats.update(); | ||
|
||
} | ||
|
||
</script> | ||
|
||
</body> | ||
</html> |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.