当前位置:   article > 正文

three.js 匀速动画(向量表示速度)

three.js 匀速动画(向量表示速度)

效果:

代码:

  1. <template>
  2. <div>
  3. <el-container>
  4. <el-main>
  5. <div class="box-card-left">
  6. <div id="threejs" style="border: 1px solid red"></div>1. 匀速动画(向量表示速度)
  7. </div>
  8. </el-main>
  9. </el-container>
  10. </div>
  11. </template>
  12. <script>
  13. // 引入轨道控制器扩展库OrbitControls.js
  14. import { OrbitControls } from "three/examples/jsm/controls/OrbitControls.js";
  15. import { GLTFLoader } from "three/examples/jsm/loaders/GLTFLoader.js";
  16. // 效果制作器
  17. import { EffectComposer } from "three/examples/jsm/postprocessing/EffectComposer.js";
  18. // 渲染通道
  19. import { RenderPass } from "three/examples/jsm/postprocessing/RenderPass.js";
  20. // 发光描边OutlinePass
  21. import { OutlinePass } from "three/examples/jsm/postprocessing/OutlinePass.js";
  22. import {
  23. CSS2DObject,
  24. CSS2DRenderer,
  25. } from "three/examples/jsm/renderers/CSS2DRenderer.js";
  26. import TWEEN from "@tweenjs/tween.js";
  27. // TextGeometry 是一个附加组件,必须显式导入。 three/examples/jsm/geometries
  28. import { TextGeometry } from 'three/examples/jsm/geometries/TextGeometry.js';
  29. // FontLoader 是一个附加组件,必须显式导入。
  30. import { FontLoader } from "three/examples/jsm/loaders/FontLoader.js";
  31. export default {
  32. data() {
  33. return {
  34. textGeometry: null,
  35. group: null,
  36. camera: null,
  37. box: null,
  38. renderer: null,
  39. clock: null,
  40. sudu: null,
  41. mesh: null,
  42. group: null,
  43. flag: true
  44. };
  45. },
  46. created() {},
  47. mounted() {
  48. this.name = this.$route.query.name;
  49. this.init();
  50. },
  51. methods: {
  52. goBack() {
  53. this.$router.go(-1);
  54. },
  55. // 1. 匀速动画(向量表示速度)
  56. init() {
  57. // 创建场景对象
  58. this.scene = new this.$three.Scene();
  59. const axesHelper = new this.$three.AxesHelper(200);
  60. this.scene.add(axesHelper);
  61. const ambientLight = new this.$three.AmbientLight(0xffffff, 1);
  62. this.scene.add(ambientLight);
  63. this.clock = new this.$three.Clock();
  64. this.sudu = new this.$three.Vector3(0,0,10);//物体运动速度
  65. // 创建文本模型对象
  66. /**
  67. * TextGeometry(text, parameters)
  68. * text: 要显示的文字
  69. * parameters: {
  70. * font -- THREE.字体
  71. * size -- Float.大小
  72. * height -- Float.字体厚度, 默认是50
  73. * curveSegments — Integer. 曲线上点的数量. 默认值为12.
  74. bevelEnabled — Boolean. 是否打开斜面. 默认值为False.
  75. bevelThickness — Float. 文本斜面的深度. 默认值为10.
  76. bevelSize — Float. 斜面离轮廓的距离. 默认值为8.
  77. *
  78. * }
  79. */
  80. this.box = new this.$three.Box3();
  81. const fontLoader = new FontLoader();
  82. fontLoader.load("/fonts/Fontquan-XinYiJiXiangSong_Regular.json", font => {
  83. const mesh = this.createText(font, 0xFB0606);
  84. const mesh2 = this.createText(font,0xF84949);
  85. mesh2.rotateX(Math.PI);
  86. mesh2.translateY(-this.box.min.y + 10)
  87. mesh2.translateZ(-10)
  88. this.group = new this.$three.Group();
  89. this.group.add(mesh,mesh2);
  90. this.scene.add(this.group);
  91. this.camera = new this.$three.PerspectiveCamera();
  92. this.camera.position.set(0,30,500);
  93. this.camera.lookAt(0,0,0);
  94. this.renderer = new this.$three.WebGLRenderer();
  95. this.renderer.setSize(1000,800);
  96. this.renderer.render(this.scene, this.camera);
  97. window.document.getElementById("threejs").appendChild(this.renderer.domElement);
  98. const controls = new OrbitControls(this.camera, this.renderer.domElement);
  99. controls.addEventListener("change", e => {
  100. this.renderer.render(this.scene, this.camera);
  101. })
  102. this.loopFun();
  103. })
  104. },
  105. loopFun() {
  106. const spt = this.clock.getDelta() * 2;
  107. const dis = this.sudu.clone().multiplyScalar(spt);
  108. this.group.position.add(dis);
  109. this.renderer.render(this.scene, this.camera);
  110. requestAnimationFrame(this.loopFun);
  111. },
  112. createText(font, color) {
  113. const textGeometry = new TextGeometry('龙年大吉', { font: font, size:50, height: 10});
  114. const material = new this.$three.MeshLambertMaterial({color: color});
  115. const mesh = new this.$three.Mesh(textGeometry, material);
  116. this.box.expandByObject(mesh);
  117. const move_x = (this.box.max.x - this.box.min.x) / 2;
  118. mesh.translateX(-move_x)
  119. mesh.translateZ(-5)
  120. mesh.translateY(-this.box.min.y+ 0)
  121. return mesh;
  122. }
  123. },
  124. };
  125. </script>
  126. //
  127. <style lang="less" scoped>
  128. .box-card-left {
  129. display: flex;
  130. align-items: flex-start;
  131. flex-direction: row;
  132. width: 100%;
  133. .box-right {
  134. img {
  135. width: 500px;
  136. user-select: none;
  137. }
  138. }
  139. }
  140. </style>

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/76696
推荐阅读
相关标签
  

闽ICP备14008679号