123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <title>three.js webgl - animation - morph targets</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 { GUI } from './jsm/libs/dat.gui.module.js';
- import { OrbitControls } from './jsm/controls/OrbitControls.js';
- var container;
- var camera, scene, renderer, raycaster;
- var mesh, mouse = new THREE.Vector2();
- init();
- animate();
- function init() {
- container = document.getElementById( 'container' );
- camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 2000 );
- camera.position.z = 500;
- scene = new THREE.Scene();
- scene.background = new THREE.Color( 0x222222 );
- scene.fog = new THREE.Fog( 0x000000, 1, 15000 );
- var light = new THREE.PointLight( 0xffffff );
- light.position.z = 500;
- camera.add( light );
- scene.add( camera );
- var light = new THREE.AmbientLight( 0x111111 );
- scene.add( light );
- var geometry = new THREE.BoxGeometry( 100, 100, 100 );
- var material = new THREE.MeshLambertMaterial( { color: 0xff0000, morphTargets: true } );
- // construct 8 blend shapes
- for ( var i = 0; i < 8; i ++ ) {
- var vertices = [];
- for ( var v = 0; v < geometry.vertices.length; v ++ ) {
- vertices.push( geometry.vertices[ v ].clone() );
- if ( v === i ) {
- vertices[ vertices.length - 1 ].x *= 2;
- vertices[ vertices.length - 1 ].y *= 2;
- vertices[ vertices.length - 1 ].z *= 2;
- }
- }
- geometry.morphTargets.push( { name: "target" + i, vertices: vertices } );
- }
- geometry = new THREE.BufferGeometry().fromGeometry( geometry );
- mesh = new THREE.Mesh( geometry, material );
- scene.add( mesh );
- //
- var params = {
- influence1: 0,
- influence2: 0,
- influence3: 0,
- influence4: 0,
- influence5: 0,
- influence6: 0,
- influence7: 0,
- influence8: 0
- };
- var gui = new GUI();
- var folder = gui.addFolder( 'Morph Targets' );
- folder.add( params, 'influence1', 0, 1 ).step( 0.01 ).onChange( function ( value ) {
- mesh.morphTargetInfluences[ 0 ] = value;
- } );
- folder.add( params, 'influence2', 0, 1 ).step( 0.01 ).onChange( function ( value ) {
- mesh.morphTargetInfluences[ 1 ] = value;
- } );
- folder.add( params, 'influence3', 0, 1 ).step( 0.01 ).onChange( function ( value ) {
- mesh.morphTargetInfluences[ 2 ] = value;
- } );
- folder.add( params, 'influence4', 0, 1 ).step( 0.01 ).onChange( function ( value ) {
- mesh.morphTargetInfluences[ 3 ] = value;
- } );
- folder.add( params, 'influence5', 0, 1 ).step( 0.01 ).onChange( function ( value ) {
- mesh.morphTargetInfluences[ 4 ] = value;
- } );
- folder.add( params, 'influence6', 0, 1 ).step( 0.01 ).onChange( function ( value ) {
- mesh.morphTargetInfluences[ 5 ] = value;
- } );
- folder.add( params, 'influence7', 0, 1 ).step( 0.01 ).onChange( function ( value ) {
- mesh.morphTargetInfluences[ 6 ] = value;
- } );
- folder.add( params, 'influence8', 0, 1 ).step( 0.01 ).onChange( function ( value ) {
- mesh.morphTargetInfluences[ 7 ] = value;
- } );
- folder.open();
- //
- raycaster = new THREE.Raycaster();
- 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 = 400;
- controls.maxDistance = 1000;
- //
- window.addEventListener( 'resize', onWindowResize, false );
- document.addEventListener( 'click', onClick, false );
- }
- function onClick( event ) {
- event.preventDefault();
- mouse.x = ( event.clientX / window.innerWidth ) * 2 - 1;
- mouse.y = - ( event.clientY / window.innerHeight ) * 2 + 1;
- raycaster.setFromCamera( mouse, camera );
- var intersects = raycaster.intersectObject( mesh );
- if ( intersects.length > 0 ) {
- mesh.material.color.set( Math.random() * 0xffffff );
- }
- }
- function onWindowResize() {
- camera.aspect = window.innerWidth / window.innerHeight;
- camera.updateProjectionMatrix();
- renderer.setSize( window.innerWidth, window.innerHeight );
- }
- function animate() {
- requestAnimationFrame( animate );
- render();
- }
- function render() {
- mesh.rotation.y += 0.01;
- renderer.render( scene, camera );
- }
- </script>
- </body>
- </html>
|