【发布时间】:2019-07-10 21:50:20
【问题描述】:
考虑这个 three.js 示例
https://threejs.org/examples/?q=glt#webgl_loader_gltf
我正在尝试在鼠标单击事件中实现对象的动画(从对象当前所在的位置旋转/移动),我得到了这些但没有动画。
这是我添加的代码。我是否过度使用渲染?
document.addEventListener( 'mousedown', clickMe, false );
render();
function clickMe() {
rotation();
render();
}
var gltfModel;
function rotation() {
var rotationAnimation = 5 * (Math.PI / 180);
gltfModel.rotation.x += rotationAnimation;
render();
}
function render() {
renderer.render( scene, camera );
}
如果我添加函数rotation(); requestAnimationFrame( 旋转 );
function rotation() {
requestAnimationFrame( rotation );
var rotationAnimation = 5 * (Math.PI / 180);
gltfModel.rotation.x += rotationAnimation;
render();
}
gltfModel 一直循环旋转,每次点击速度都会翻倍
完整代码如下:
<script>
if ( WEBGL.isWebGLAvailable() === false ) {
document.body.appendChild( WEBGL.getWebGLErrorMessage() );
}
var container, stats, controls;
var camera, scene, renderer, light;
init();
animate();
function init() {
container = document.createElement( 'div' );
document.body.appendChild( container );
camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 0.25, 20 );
camera.position.set( - 1.8, 0.9, 2.7 );
controls = new THREE.OrbitControls( camera );
controls.target.set( 0, - 0.2, - 0.2 );
controls.update();
var urls = [ 'posx.jpg', 'negx.jpg', 'posy.jpg', 'negy.jpg', 'posz.jpg', 'negz.jpg' ];
var loader = new THREE.CubeTextureLoader().setPath( 'textures/cube/Bridge2/' );
var background = loader.load( urls );
scene = new THREE.Scene();
scene.background = background;
light = new THREE.HemisphereLight( 0xbbbbff, 0x444422 );
light.position.set( 0, 1, 0 );
scene.add( light );
// model
var loader = new THREE.GLTFLoader().setPath( 'models/gltf/DamagedHelmet/glTF/' );
loader.load( 'DamagedHelmet.gltf', function ( gltf ) {
gltf.scene.traverse( function ( child ) {
if ( child.isMesh ) {
child.material.envMap = background;
gltfModel = child;
}
} );
scene.add( gltf.scene );
}, undefined, function ( e ) {
console.error( e );
} );
renderer = new THREE.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 );
//MY LINE OF CODE
document.addEventListener( 'mousedown', clickMe, false );
render();
// stats
stats = new Stats();
container.appendChild( stats.dom );
}
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize( window.innerWidth, window.innerHeight );
}
//MY LINE OF CODE
///////////////////////////////////////
function clickMe() {
rotation();
render();
}
var gltfModel;
function rotation() {
var rotationAnimation = 5 * (Math.PI / 180);
gltfModel.rotation.x += rotationAnimation;
render();
}
///////////////////////////////////////
function animate() {
requestAnimationFrame( animate );
render();
stats.update();
}
function render() {
renderer.render( scene, camera );
}
</script>
可以用EventDispatcher来完成吗?如果是这样,怎么做? https://threejs.org/docs/#api/en/core/EventDispatcher
但我更喜欢第一种方法?
【问题讨论】:
标签: javascript function three.js jquery-animate