当前位置:   article > 正文

three.js 向量叉乘cross

three.js 向量叉乘cross

效果

代码:

  1. <template>
  2. <div>
  3. <el-container>
  4. <el-main>
  5. <div class="box-card-left">
  6. <div id="threejs"></div>
  7. <div>
  8. </div>
  9. </div>
  10. </el-main>
  11. </el-container>
  12. </div>
  13. </template>
  14. <script>
  15. // 引入轨道控制器扩展库OrbitControls.js
  16. import { OrbitControls } from "three/examples/jsm/controls/OrbitControls.js";
  17. import { GLTFLoader } from "three/examples/jsm/loaders/GLTFLoader.js";
  18. // 效果制作器
  19. import { EffectComposer } from "three/examples/jsm/postprocessing/EffectComposer.js";
  20. // 渲染通道
  21. import { RenderPass } from "three/examples/jsm/postprocessing/RenderPass.js";
  22. // 发光描边OutlinePass
  23. import { OutlinePass } from "three/examples/jsm/postprocessing/OutlinePass.js";
  24. import {
  25. CSS2DObject,
  26. CSS2DRenderer,
  27. } from "three/examples/jsm/renderers/CSS2DRenderer.js";
  28. import TWEEN from "@tweenjs/tween.js";
  29. import {
  30. CSS3DObject,
  31. CSS3DSprite,
  32. CSS3DRenderer,
  33. } from "three/examples/jsm/renderers/CSS3DRenderer.js";
  34. // TextGeometry 是一个附加组件,必须显式导入。 three/examples/jsm/geometries
  35. import { TextGeometry } from "three/examples/jsm/geometries/TextGeometry.js";
  36. // FontLoader 是一个附加组件,必须显式导入。
  37. import { FontLoader } from "three/examples/jsm/loaders/FontLoader.js";
  38. export default {
  39. data() {
  40. return {
  41. scene: null, // 场景对象
  42. camera: null, // 相机对象
  43. group: null, // 组对象
  44. person: null, // 人对象
  45. renderer: null, // 渲染器对象
  46. a: new this.$three.Vector3(50, 0, 0),
  47. b: new this.$three.Vector3(30, 0, 30),
  48. };
  49. },
  50. created() {},
  51. mounted() {
  52. this.name = this.$route.query.name;
  53. this.init();
  54. },
  55. methods: {
  56. goBack() {
  57. this.$router.go(-1);
  58. },
  59. /**
  60. * 如何判断物体是在人的前方还是后方
  61. * 思路:借助两个单位向量的点乘结果来判断的;
  62. */
  63. init() {
  64. // 创建场景对象
  65. this.scene = new this.$three.Scene();
  66. // 创建辅助坐标轴对象
  67. const axesHelper = new this.$three.AxesHelper(10);
  68. this.scene.add(axesHelper);
  69. // 创建相机对象
  70. this.camera = new this.$three.PerspectiveCamera(60,1,0.01,2000);
  71. this.camera.position.set(3,3,3);
  72. this.camera.lookAt(0,0,0);
  73. this.createArrowHelper(this.a);
  74. this.createArrowHelper(this.b);
  75. this.crossVectorsFn();
  76. // 创建渲染器对象
  77. this.renderer = new this.$three.WebGLRenderer();
  78. this.renderer.setSize(1000,800);
  79. this.renderer.render(this.scene, this.camera);
  80. window.document.getElementById("threejs").appendChild(this.renderer.domElement);
  81. const controls = new OrbitControls(this.camera, this.renderer.domElement);
  82. controls.addEventListener("change", () => {
  83. this.renderer.render(this.scene, this.camera);
  84. })
  85. },
  86. // 创建箭头用于显示向量的位置
  87. createArrowHelper(dir) {
  88. let arrow = new this.$three.ArrowHelper(dir.normalize(), new this.$three.Vector3(0,0,0), 2, 0xffffff);
  89. this.scene.add(arrow);
  90. },
  91. crossVectorsFn() {
  92. // const c = new this.$three.Vector3();
  93. // c.crossVectors(this.b, this.a);
  94. let c = this.b.clone().cross(this.a);
  95. this.createArrowHelper(c);
  96. }
  97. },
  98. };
  99. </script>
  100. <style lang="less" scoped>
  101. .box-card-left {
  102. display: flex;
  103. align-items: flex-start;
  104. flex-direction: row;
  105. width: 100%;
  106. .box-right {
  107. img {
  108. width: 500px;
  109. user-select: none;
  110. }
  111. }
  112. }
  113. </style>

 

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

闽ICP备14008679号