首页 > 解决方案 > 如何在三个js中的按钮上添加功能

问题描述

三JS中已经有写好的程序了。我想在按钮点击事件上添加动画功能。以及如何在内部窗口中设置按钮。我想在按钮单击事件上调用所有动画。请帮忙谢谢。

相同的代码在这个三个js的链接中

代码在这里

<!DOCTYPE html>
<html lang="en">
	<head>
		<title>Skinning and morphing</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">
		<style>
			body {
				color: #222545;
			}

			a {
				color: #2fa1d6;
			}

			p {
				max-width: 600px;
				margin-left: auto;
				margin-right: auto;
				padding: 0 2em;
			}
		</style>
	</head>
	

	<body>
		
	<div class="btn-group">
	<button id="walking" class="button" onclick="">Standing</button>
	<button id="Idle" class="button" onclick="">Idle</button>
	<button id="Standing" class="button" onclick="" >Walking</button>
	<button id="Death" class="button" onclick="" >Death</button>
	</div>
	

		<script type="module">
		

			import * as THREE from './js/three.module.js';

			import Stats from './js/stats.module.js';
			import { GUI } from './js/dat.gui.module.js';

			import { GLTFLoader } from './js/GLTFLoader.js';

			var container, stats, clock, gui, mixer, actions, activeAction, previousAction;
			var camera, scene, renderer, model, face;

			var api = { state: 'Death' };

			init();
			animate();

			function init() {

				container = document.createElement( 'div' );
				document.body.appendChild( container );

				camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 0.25, 100 );
				camera.position.set( - 5, 3, 10 );
				camera.lookAt( new THREE.Vector3( 0, 2, 0 ) );

				scene = new THREE.Scene();
				scene.background = new THREE.Color( 0xe0e0e0 );
				scene.fog = new THREE.Fog( 0xe0e0e0, 20, 100 );

				clock = new THREE.Clock();

				// lights

				var light = new THREE.HemisphereLight( 0xffffff, 0x444444 );
				light.position.set( 0, 20, 0 );
				scene.add( light );

				light = new THREE.DirectionalLight( 0xffffff );
				light.position.set( 0, 20, 10 );
				scene.add( light );

				// ground

				var mesh = new THREE.Mesh( new THREE.PlaneBufferGeometry( 2000, 2000 ), new THREE.MeshPhongMaterial( { color: 0x999999, depthWrite: false } ) );
				mesh.rotation.x = - Math.PI / 2;
				scene.add( mesh );

				var grid = new THREE.GridHelper( 200, 40, 0x000000, 0x000000 );
				grid.material.opacity = 0.2;
				grid.material.transparent = true;
				scene.add( grid );

				// model

				var loader = new GLTFLoader();
				loader.load( 'models/RobotExpressive.glb', function ( gltf ) {

					model = gltf.scene;
					scene.add( model );

					createGUI( model, gltf.animations );

				}, undefined, function ( e ) {

					console.error( e );

				} );

				renderer = new THREE.WebGLRenderer( { antialias: true } );
				renderer.setPixelRatio( window.devicePixelRatio );
				renderer.setSize( window.innerWidth, window.innerHeight );
				renderer.outputEncoding = THREE.sRGBEncoding;
				container.appendChild( renderer.domElement );

				window.addEventListener( 'resize', onWindowResize, false );

				// stats
				stats = new Stats();
				container.appendChild( stats.dom );

			}

			function createGUI( model, animations ) {

				var states = [ 'Idle', 'Walking', 'Running', 'Dance', 'Death', 'Sitting', 'Standing' ];
				var emotes = [ 'Jump', 'Yes', 'No', 'Wave', 'Punch', 'ThumbsUp' ];

				gui = new GUI();

				mixer = new THREE.AnimationMixer( model );

				actions = {};

				for ( var i = 0; i < animations.length; i ++ ) {

					var clip = animations[ i ];
					var action = mixer.clipAction( clip );
					actions[ clip.name ] = action;

					if ( emotes.indexOf( clip.name ) >= 0 || states.indexOf( clip.name ) >= 4 ) {

						action.clampWhenFinished = true;
						action.loop = THREE.LoopOnce;

					}

				}

				// states

				var statesFolder = gui.addFolder( 'States' );

				var clipCtrl = statesFolder.add( api, 'state' ).options( states );

				clipCtrl.onChange( function () {

					fadeToAction( api.state, 0.5 );

				} );

				statesFolder.open();

				// emotes

				var emoteFolder = gui.addFolder( 'Emotes' );

				function createEmoteCallback( name ) {

					api[ name ] = function () {

						fadeToAction( name, 0.2 );

						mixer.addEventListener( 'finished', restoreState );

					};

					emoteFolder.add( api, name );

				}

				function restoreState() {

					mixer.removeEventListener( 'finished', restoreState );

					fadeToAction( api.state, 0.2 );

				}

				for ( var i = 0; i < emotes.length; i ++ ) {

					createEmoteCallback( emotes[ i ] );

				}

				emoteFolder.open();

				// expressions

				face = model.getObjectByName( 'Head_2' );

				var expressions = Object.keys( face.morphTargetDictionary );
				var expressionFolder = gui.addFolder( 'Expressions' );

				for ( var i = 0; i < expressions.length; i ++ ) {

					expressionFolder.add( face.morphTargetInfluences, i, 0, 1, 0.01 ).name( expressions[ i ] );

				}

				activeAction = actions[ 'Death' ];
				activeAction.play();

				expressionFolder.open();

			}

			function fadeToAction( name, duration ) {  //want to add this animated function on click event
			

				previousAction = activeAction;
				activeAction = actions[ name ];

				if ( previousAction !== activeAction ) {

					previousAction.fadeOut( duration );

				}

				activeAction
					.reset()
					.setEffectiveTimeScale( 1 )
					.setEffectiveWeight( 1 )
					.fadeIn( duration )
					.play();

			}
			document.getElementById("Death").addEventListener("click", fadeToAction('Death',0.5));


			function onWindowResize() {

				camera.aspect = window.innerWidth / window.innerHeight;
				camera.updateProjectionMatrix();

				renderer.setSize( window.innerWidth, window.innerHeight );

			}

			//

			function animate() {

				var dt = clock.getDelta();

				if ( mixer ) mixer.update( dt );

				requestAnimationFrame( animate );

				renderer.render( scene, camera );

				stats.update();

			}

		</script>

	</body>
</html>

请帮助我如何做到这一点。非常感谢。

标签: javascriptthree.jstween.js

解决方案


Three.js 使用dat.gui作为它的 gui。

一个简单的按钮,只要按下它就会运行一个功能,就像这样。

function buttonFunction() {
    //do button 
    //stuff here.
}

var params = {
    ClickMe: buttonFunction
};

var gui = new GUI();
var folder = gui.addFolder('My folder');

folder.add(params, 'ClickMe');

folder.open();

推荐阅读