123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <title>three.js webgl - morph targets - sphere</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="container"></div>
- <div id="info">
- <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - WebGL morph target example
- </div>
- <script type="module">
- import * as THREE from '../build/three.module.js';
- import { OrbitControls } from './jsm/controls/OrbitControls.js';
- import { GLTFLoader } from './jsm/loaders/GLTFLoader.js';
- var container;
- var camera, scene, renderer, clock;
- var mesh;
- var sign = 1;
- var speed = 0.5;
- init();
- animate();
- function init() {
- container = document.getElementById( 'container' );
- camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 0.2, 100 );
- camera.position.set( 0, 5, 5 );
- scene = new THREE.Scene();
- clock = new THREE.Clock();
- var light = new THREE.PointLight( 0xff2200, 0.7 );
- light.position.set( 100, 100, 100 );
- scene.add( light );
- light = new THREE.PointLight( 0x22ff00, 0.7 );
- light.position.set( - 100, - 100, - 100 );
- scene.add( light );
- light = new THREE.AmbientLight( 0x111111 );
- scene.add( light );
- var loader = new GLTFLoader();
- loader.load( 'models/gltf/AnimatedMorphSphere/glTF/AnimatedMorphSphere.gltf', function ( gltf ) {
- gltf.scene.traverse( function ( node ) {
- if ( node.isMesh ) mesh = node;
- } );
- mesh.material.morphTargets = true;
- mesh.rotation.z = Math.PI / 2;
- //mesh.material.visible = false;
- scene.add( mesh );
- //
- var pointsMaterial = new THREE.PointsMaterial( {
- size: 10,
- sizeAttenuation: false,
- map: new THREE.TextureLoader().load( 'textures/sprites/disc.png' ),
- alphaTest: 0.5,
- morphTargets: true
- } );
- var points = new THREE.Points( mesh.geometry, pointsMaterial );
- points.morphTargetInfluences = mesh.morphTargetInfluences;
- points.morphTargetDictionary = mesh.morphTargetDictionary;
- mesh.add( points );
- } );
- //
- renderer = new THREE.WebGLRenderer();
- renderer.setPixelRatio( window.devicePixelRatio );
- renderer.setSize( window.innerWidth, window.innerHeight );
- container.appendChild( renderer.domElement );
- //
- var controls = new OrbitControls( camera, renderer.domElement );
- controls.minDistance = 1;
- controls.maxDistance = 20;
- //
- window.addEventListener( 'resize', onWindowResize, false );
- document.addEventListener( 'visibilitychange', onVisibilityChange );
- }
- function onWindowResize() {
- camera.aspect = window.innerWidth / window.innerHeight;
- camera.updateProjectionMatrix();
- renderer.setSize( window.innerWidth, window.innerHeight );
- }
- function onVisibilityChange() {
- if ( document.hidden === true ) {
- clock.stop();
- } else {
- clock.start();
- }
- }
- function animate() {
- requestAnimationFrame( animate );
- render();
- }
- function render() {
- var delta = clock.getDelta();
- if ( mesh !== undefined ) {
- var step = delta * speed;
- mesh.rotation.y += step;
- mesh.morphTargetInfluences[ 1 ] = mesh.morphTargetInfluences[ 1 ] + step * sign;
- if ( mesh.morphTargetInfluences[ 1 ] <= 0 || mesh.morphTargetInfluences[ 1 ] >= 1 ) {
- sign *= - 1;
- }
- }
- renderer.render( scene, camera );
- }
- </script>
- </body>
- </html>
|