threejs深拷貝

 

		this.deepClone = (object) =>{

			let target = null;
			if(object.type === 'Mesh' || object.type === 'Sprite'){
				target =  new THREE.Mesh(object.geometry.clone(false), object.material.clone(false) );
			}else{
				target = object.clone( false );
			}
			
			target.name = object.name;
			copyProperty(target, object);

			if(object.children && object.children.length > 0){

				object.children.forEach( child => {
					target.add( this.deepClone(child) );
				});
			}


			return target; 
		}


		function copyProperty(target, source){

			target.position.x = source.position.x;
			target.position.y = source.position.y;
			target.position.z = source.position.z;

			target.rotation.x = source.rotation.x;
			target.rotation.y = source.rotation.y;
			target.rotation.z = source.rotation.z;

			target.scale.x = source.scale.x;
			target.scale.y = source.scale.y;
			target.scale.z = source.scale.z;
		}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章