如何在Three.js中使用以下动画制作camera.zoom [英] How to animate camera.zoom in three.js with

查看:122
本文介绍了如何在Three.js中使用以下动画制作camera.zoom的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用正交摄影机对Three.js中的摄影机动画进行动画处理.没有动画,这非常简单.我只需要设置 camera.zoom = 1 .但是我想用tween.js制作动画.

How to animate the zoom of camera in three.js with orthigraphic camera. Without animation it is very simple. I only have to set camera.zoom = 1. But I want to animate it with tween.js.

有我的代码笔: http://codepen.io/anon/pen/yVOOLj?editors=1111

var scene = new THREE.Scene();
var camera = new THREE.OrthographicCamera(640 / -2, 640 / 2, 380 / 2, 380 / -2, -5000, 5000);
camera.position.set(100,100,100);
camera.zoom = 0.1;


var renderer = new THREE.WebGLRenderer();
renderer.setSize( 640, 380 );
renderer.setClearColor("#FFF");
$("#canvas").append( renderer.domElement );

var geometry = new THREE.BoxBufferGeometry( 100, 100, 100 );
var material = new THREE.MeshBasicMaterial( {color: 0x00ff00} );
var cube = new THREE.Mesh( geometry, material );
scene.add( cube );

var originAxes = new THREE.AxisHelper(1200);
scene.add(originAxes);	

var controls = new THREE.OrbitControls(camera, renderer.domElement, renderer, scene);

animate();

function animate(){
  camera.updateProjectionMatrix();
  requestAnimationFrame( animate );
  TWEEN.update();
  render();
}

function render() {
  renderer.render(scene, camera);
};


$("#withoutAnim").click(function () {
  camera.zoom = 1;    
});

$("#withAnim").click(function () { 
  var tween = new TWEEN.Tween(0).to(1,  500);
  tween.onUpdate(function () {
    camera.zoom = 0.10;
  });
  tween.start();
  
});

body { margin: 0; }
#canvas {float: left; width: 640px; height: 380px; border: 1px solid #d3d3d3 }
.button { border: 1px solid black; cursor: pointer; width: 230px; height: 20px;}
.button:hover{background: blue; color: white;}

<script src="http://sole.github.io/tween.js/build/tween.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<script src="http://threejs.org/examples/js/controls/OrbitControls.js"></script>
<script src="http://threejs.org/build/three.min.js"></script>

<div id="canvas">
<div id="withoutAnim" class="button" >working zoom without animation</div>
<div id="withAnim" class="button" >zoom with animation - not working</div>

推荐答案

最好先这样写(不带全局变量):

It's better to put it like that then (without global variables):

$("#withAnim").click(function() {
  var zoom = {
    value: camera.zoom // from current zoom (no matter if it's more or less than 1)
  };
  var zoomEnd = {
    value: 1 // to the zoom of 1
  };
  var tween = new TWEEN.Tween(zoom).to(zoomEnd, 500); // duration of tweening is 0.5 second
  tween.onUpdate(function() {
    camera.zoom = zoom.value;
  });
  tween.start();
});

jsfiddle 示例

这篇关于如何在Three.js中使用以下动画制作camera.zoom的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆