查看内容

大瞳引擎2.0版本--模型预览案例

  1. //Controls.js
  2. this.BoxObject01 = new TONG.Mesh(); //获取当前场景对象
  3. this.BoxObject02 = new TONG.Mesh();
  4. this.BoxObject03 = new TONG.Mesh();
  5. this.BoxObject04 = new TONG.Mesh();
  6. var cameraPosition = new TONG.Vector3();
  7. var boxObjectArray = []; //定义数组
  8. var count = 0;
  9. var directionMove = new TONG.Vector3();
  10. var isCanBack = false;
  11. var isMoving = false;
  12. var rotationSpeed =0;
  13. function start()
  14. {
  15. cameraPosition = this.camera.position; //获取camera对象的位置
  16. boxObjectArray.push(this.BoxObject01);
  17. boxObjectArray.push(this.BoxObject02);
  18. boxObjectArray.push(this.BoxObject03);
  19. boxObjectArray.push(this.BoxObject04); //将对象放入数组当中
  20.  
  21. }
  22.  
  23. function update(e)
  24. { rotationSpeed +=0.05;
  25. if(cameraPosition.distanceTo(boxObjectArray[count].position)>5 && isCanBack){
  26. cameraPosition =cameraPosition.subVectors(cameraPosition,directionMove);
  27. isMoving =true;
  28. }
  29. else if(!isCanBack && cameraPosition.distanceTo(boxObjectArray[count].position)<22){ //检测与对象之间的最短距离要大于22
  30. cameraPosition =cameraPosition.addVectors(directionMove,cameraPosition);
  31. isMoving =true;
  32. }
  33. else{ isMoving = false;}
  34. //scene refresh();
  35. boxObjectArray[count].rotation.y = rotationSpeed;
  36. }
  37. function mouseup(){
  38. if(!isMoving){
  39. isCanBack = !isCanBack;
  40. directionMove = DirectionNormalize(cameraPosition,boxObjectArray[count].position); //获取单位向量
  41. if(!isCanBack){
  42. count++;
  43. if(count==4){
  44. count = 0;
  45. }
  46. }
  47. }
  48. }
  49. function DirectionNormalize(vec2,vec3){ //获取单位向量
  50. var vector3Object = new TONG.Vector3();
  51. var vecNormalize = vector3Object.subVectors(vec2,vec3);
  52. return vecNormalize.normalize();
  53. }
分享: