`
18211103738
  • 浏览: 82799 次
  • 性别: Icon_minigender_1
社区版块
存档分类
最新评论

通过改变morphTargetInfluences实现物体变形

阅读更多

在three.js中morphTargets允许物体发生变形。如果该物体的geometry有 n 个顶点,那么MorphTargets允许你再指定 n 个, 2n 个, 3n 个甚至更多个顶点(比如,p⋅n 个),同时mesh对象提供一个数组morphTargetInfluences(公式中fj表示morphTargetInfluences[j]),具有 p 个元素,每个元素取值在0-1之间。渲染这个物体的时候,某个顶点 Vi 的位置其实变了,成了:

如果一个立方体有8个顶点,MorphTargets又指定了8个顶点,立方体的一个顶点为(1,1,1),而在MorphTargets中与之对应的顶点为(2,2,2),那么当morphTargetInfluences[0]为0.5的时候,实际渲染的时候该顶点的位置就成了(1.5,1.5,1.5)。这样做的好处是显而易见的,你可以通过简单地调整morphTargetInfluences数组来使物体形变,只要之前你设置好了。

 

效果:

 

代码:

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8"/>
		<title>Morph-Cube</title>
		<script src="js/three.min.js"></script>
		<style>body{background:#eee;margin:0;padding:0;overflow:hidden;}div{width:400px;height:250px;float:left;}</style>
	</head>
	<body>
		<div>
			Use controls to change morph target influneces:<br/>
			<!-- 通过mesh.morphTargetInfluences[0]的值实现变形 -->
			<input type="range" value="0" min="0" max="100" onchange="mesh.morphTargetInfluences[0]=this.value/100;" /><br/>
			<input type="range" value="0" min="0" max="100" onchange="mesh.morphTargetInfluences[1]=this.value/100;" /><br/>
			<input type="range" value="0" min="0" max="100" onchange="mesh.morphTargetInfluences[2]=this.value/100;" /><br/>
			<input type="range" value="0" min="0" max="100" onchange="mesh.morphTargetInfluences[3]=this.vlaue/100;" /><br/>
			<input type="range" value="0" min="0" max="100" onchange="mesh.morphTargetInfluences[4]=this.value/100;" /><br/>
			<input type="range" value="0" min="0" max="100" onchange="mesh.morphTargetInfluences[5]=this.value/100;" /><br/>
			<input type="range" value="0" min="0" max="100" onchange="mesh.morphTargetInfluences[6]=this.value/100;" /><br/>
			<input type="range" value="0" min="0" max="100" onchange="mesh.morphTargetInfluences[7]=this.value/100;" />
		</div>
		<script>
		var width = window.innerWidth/2, height = window.innerHeight/2;
		var scene,camera,renderer;
		var mesh;
		
		function init(){
			scene = new THREE.Scene();
			
			camera = new THREE.PerspectiveCamera(45, width/height, 1, 1000);
			camera.position.set(0, 0, 100);
			scene.add(camera);
			
			var geometry = new THREE.BoxGeometry(20, 20, 20);
			//var material = new THREE.MeshBasicMaterial({color:0xff0000});
			var material = new THREE.MeshPhongMaterial({color:0xff0000,morphTargets:true});		//变形目标属性设置为true(激活变形功能)
			/*向geometry中增加morphTargets*/
			for(var i=0; i<geometry.vertices.length; i++){
				var vertices = [];
				for(var j=0; j<geometry.vertices.length; j++){
					vertices.push(geometry.vertices[j].clone());
					if(j == 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});
			}
			console.log(geometry);
			console.log(material);
			mesh = new THREE.Mesh(geometry, material);
			mesh.scale.set(2, 2, 2);	//x/y/z放大到2倍
			scene.add(mesh);
			
			var light = new THREE.DirectionalLight(0xffffff);
			light.position.set(100, 100, 100);
			scene.add(light);
			
			renderer = new THREE.WebGLRenderer({anitialias:true});
			renderer.setSize(width, height);
			renderer.setClearColor(0xeeeeee, 1);
			//renderer.sortObjects = false;	//是否排列对象 默认是true
			document.body.appendChild(renderer.domElement);
		}
		
		function animate(){
			requestAnimationFrame(animate);
			
			mesh.rotation.x += 0.01;
			mesh.rotation.y += 0.01;
			renderer.render(scene, camera);
		}
		
		init();
		animate();
		</script>
	</body>
</html>

附注: 当前笔者使用的three.js版本是r69

  • 大小: 39.3 KB
  • 大小: 3.7 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics