forked from mrdoob/three.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
JSM: port glTF loader example to use ES6 modules
- Loading branch information
Showing
2 changed files
with
157 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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,156 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<title>three.js webgl - glTF loader (module version)</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: 0px; | ||
overflow: hidden; | ||
} | ||
#info { | ||
color: #fff; | ||
position: absolute; | ||
top: 10px; | ||
width: 100%; | ||
text-align: center; | ||
z-index: 100; | ||
display:block; | ||
} | ||
#info a { | ||
color: #75ddc1; | ||
font-weight: bold; | ||
} | ||
</style> | ||
</head> | ||
|
||
<body> | ||
<div id="info"> | ||
<a href="http://threejs.org" target="_blank" rel="noopener">three.js</a> - GLTFLoader (module version)<br /> | ||
Battle Damaged Sci-fi Helmet by | ||
<a href="https://sketchfab.com/theblueturtle_" target="_blank" rel="noopener">theblueturtle_</a><br /> | ||
</div> | ||
|
||
<script src="js/WebGL.js"></script> | ||
<script src="js/libs/stats.min.js"></script> | ||
|
||
<script type="module"> | ||
import { | ||
CubeTextureLoader, | ||
PerspectiveCamera, | ||
Scene, | ||
WebGLRenderer | ||
} from "../build/three.module.js"; | ||
|
||
import { OrbitControls } from './jsm/controls/OrbitControls.js'; | ||
import { GLTFLoader } from './jsm/loaders/GLTFLoader.js'; | ||
import { PMREMGenerator } from './jsm/pmrem/PMREMGenerator.js'; | ||
import { PMREMCubeUVPacker } from './jsm/pmrem/PMREMCubeUVPacker.js'; | ||
|
||
if ( WEBGL.isWebGLAvailable() === false ) { | ||
|
||
document.body.appendChild( WEBGL.getWebGLErrorMessage() ); | ||
|
||
} | ||
|
||
var container, stats, controls; | ||
var camera, scene, renderer; | ||
|
||
init(); | ||
animate(); | ||
|
||
function init() { | ||
|
||
container = document.createElement( 'div' ); | ||
document.body.appendChild( container ); | ||
|
||
camera = new PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 0.25, 20 ); | ||
camera.position.set( - 1.8, 0.9, 2.7 ); | ||
|
||
controls = new OrbitControls( camera ); | ||
controls.target.set( 0, - 0.2, - 0.2 ); | ||
controls.update(); | ||
|
||
scene = new Scene(); | ||
|
||
var urls = [ 'posx.jpg', 'negx.jpg', 'posy.jpg', 'negy.jpg', 'posz.jpg', 'negz.jpg' ]; | ||
var loader = new CubeTextureLoader().setPath( 'textures/cube/Bridge2/' ); | ||
loader.load( urls, function ( texture ) { | ||
|
||
var pmremGenerator = new PMREMGenerator( texture ); | ||
pmremGenerator.update( renderer ); | ||
|
||
var pmremCubeUVPacker = new PMREMCubeUVPacker( pmremGenerator.cubeLods ); | ||
pmremCubeUVPacker.update( renderer ); | ||
|
||
var envMap = pmremCubeUVPacker.CubeUVRenderTarget.texture; | ||
|
||
// model | ||
|
||
var loader = new GLTFLoader().setPath( 'models/gltf/DamagedHelmet/glTF/' ); | ||
loader.load( 'DamagedHelmet.gltf', function ( gltf ) { | ||
|
||
gltf.scene.traverse( function ( child ) { | ||
|
||
if ( child.isMesh ) { | ||
|
||
child.material.envMap = envMap; | ||
|
||
} | ||
|
||
} ); | ||
|
||
scene.add( gltf.scene ); | ||
|
||
} ); | ||
|
||
pmremGenerator.dispose(); | ||
pmremCubeUVPacker.dispose(); | ||
|
||
scene.background = texture; | ||
|
||
} ); | ||
|
||
renderer = new WebGLRenderer( { antialias: true } ); | ||
renderer.setPixelRatio( window.devicePixelRatio ); | ||
renderer.setSize( window.innerWidth, window.innerHeight ); | ||
renderer.gammaOutput = true; | ||
container.appendChild( renderer.domElement ); | ||
|
||
window.addEventListener( 'resize', onWindowResize, false ); | ||
|
||
// stats | ||
stats = 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 ); | ||
|
||
renderer.render( scene, camera ); | ||
|
||
stats.update(); | ||
|
||
} | ||
|
||
</script> | ||
|
||
</body> | ||
</html> |