• threejs开发太阳系案例


    先看效果:

    1. <script>
    2. import Drawer from "@/components/Drawer.vue";
    3. // 引入轨道控制器扩展库OrbitControls.js
    4. import { OrbitControls } from "three/examples/jsm/controls/OrbitControls.js";
    5. import { onlyEnterMoney } from "@/utils/fn.js";
    6. export default {
    7. components: { Drawer },
    8. data() {
    9. return {
    10. radio1: "",
    11. radio2: "",
    12. name: "",
    13. x: 0,
    14. y: 0,
    15. z: 0,
    16. cameraX: 200,
    17. cameraY: 200,
    18. cameraZ: 200,
    19. scene: null,
    20. camera: null,
    21. renderer: null,
    22. mesh: null,
    23. mesh_moon: null,
    24. mesh_sun: null,
    25. geometry: null,
    26. group1: null,
    27. group2: null,
    28. axis: null,
    29. group1_arr: [],
    30. group2_arr: [],
    31. };
    32. },
    33. created() {},
    34. mounted() {
    35. this.name = this.$route.query.name;
    36. this.init();
    37. },
    38. methods: {
    39. goBack() {
    40. this.$router.go(-1);
    41. },
    42. init() {
    43. // 1,创建场景对象
    44. this.scene = new this.$three.Scene();
    45. // 2,创建组对象
    46. this.group1 = new this.$three.Group();
    47. this.group2 = new this.$three.Group();
    48. // 3,调用封装方法创建mesh
    49. this.mesh_sun = this.mesh_meth();
    50. const mesh = this.mesh_meth(60,0x0ACDE9);
    51. this.mesh_moon = this.mesh_meth(30,0xF5E904);
    52. this.mesh_moon.position.set(150,0,0);
    53. this.group1.add(mesh, this.mesh_moon);
    54. this.group1.position.set(400,0,0);
    55. this.group2.add(this.mesh_sun, this.group1);
    56. this.scene.add(this.group2);
    57. // 4,创建辅助坐标轴对象
    58. const axesHelper = new this.$three.AxesHelper(220);
    59. this.scene.add(axesHelper);
    60. // 5,创建 正交相机 对象;
    61. // 在这种投影模式下,无论物体距离相机距离远还是近,在最终渲染的图片中物体的大小都保持不变
    62. this.camera = new this.$three.OrthographicCamera(-1000,1000,1000,-1000,1,2000);
    63. // this.camera = new this.$three.PerspectiveCamera(50,1,-1300,1300);
    64. this.camera.position.set(300,300,300);// 设置相机位置
    65. // this.camera.position.set(700,700,800);// 设置相机位置
    66. this.camera.lookAt(0,0,0); // 设置相机朝向的位置
    67. this.scene.add(this.camera);
    68. this.renderer = new this.$three.WebGLRenderer();
    69. this.renderer.setSize(1000,800);
    70. this.renderer.render(this.scene,this.camera);
    71. document.getElementById("threejs").appendChild(this.renderer.domElement);
    72. // 9, 创建相机空间轨道控制器
    73. const controls = new OrbitControls(this.camera, this.renderer.domElement);
    74. controls.addEventListener('change', () => {
    75. this.renderer.render(this.scene,this.camera);
    76. });
    77. // 声明一个三维向量来表示某个坐标
    78. const worldPosition = new this.$three.Vector3();
    79. this.mesh_moon.getWorldPosition(worldPosition);
    80. console.log('世界坐标',worldPosition);
    81. console.log('本地坐标',this.mesh_moon.position);
    82. // 创建可视化mesh1的局部坐标系
    83. const mesh1_axesHelper = new this.$three.AxesHelper(150);
    84. // this.mesh_moon.add(mesh1_axesHelper);
    85. this.renderFun1();
    86. },
    87. mesh_meth(r=100,color= 0xF90E49) {
    88. // 2,创建球缓冲结合体对象
    89. const box_geometry = new this.$three.SphereGeometry(r);
    90. // 3,创建Toon网格材质对象;一种实现卡通着色的材质。
    91. // const material = new this.$three.MeshToonMaterial({color:color});
    92. const material = new this.$three.MeshBasicMaterial({color:color});
    93. return new this.$three.Mesh(box_geometry, material);
    94. },
    95. renderFun1() {
    96. this.group2.rotateY(0.01);
    97. this.mesh_moon.rotateY(0.05);
    98. this.group1.rotateY(0.03);
    99. this.renderer.render(this.scene, this.camera);
    100. window.requestAnimationFrame(this.renderFun1)
    101. },
    102. getRandomColor() {
    103. let c1 = this.random(0, 255);
    104. let c2 = this.random(0, 255);
    105. let c3 = this.random(0, 255);
    106. return new this.$three.Color("rgb(" + c1 + ", " + c2 + ", " + c3 + ")");
    107. },
    108. /**
    109. * 产生随机整数,包含下限值,但不包括上限值
    110. * @param {Number} lower 下限
    111. * @param {Number} upper 上限
    112. * @return {Number} 返回在下限到上限之间的一个随机整数
    113. */
    114. random(lower, upper) {
    115. return Math.floor(Math.random() * (upper - lower)) + lower;
    116. },
    117. },
    118. };
    119. script>
    120. <style lang="less" scoped>
    121. .msg {
    122. padding: 20px;
    123. text-align: left;
    124. display: flex;
    125. justify-content: flex-start;
    126. flex-wrap: wrap;
    127. .span {
    128. margin: 0 30px 30px 0;
    129. // white-space: nowrap;
    130. }
    131. .p {
    132. text-align: left;
    133. }
    134. }
    135. .box-card-left {
    136. display: flex;
    137. align-items: flex-start;
    138. flex-direction: row;
    139. width: 100%;
    140. .box-right {
    141. padding-left: 20px;
    142. }
    143. }
    144. style>

  • 相关阅读:
    论文解读:Sadeepcry:使用自我注意和自动编码器网络的蛋白质结晶倾向预测的深度学习框架
    Oracle 和 mysql 增加字段SQL
    ​“债务飙升!美国一天内增加2750亿美元,金融震荡的前奏已拉开帷幕!”
    Highcharts 标示区曲线图;Highcharts 对数图表;Highcharts 时间间隔图表
    Python try except else或finally异常处理
    云原生之容器化:Docker三剑客之Docker Swarm
    如何用Virtualbox搭建一个虚拟机
    PTA程序辅助设计平台—2023年软件设计综合实践_4(数组及字符串)
    C++ Reference: Standard C++ Library reference: C Library: cstring: memcpy
    神经系统图 基本结构图,大脑神经网络结构图片
  • 原文地址:https://blog.csdn.net/yinge0508/article/details/132830022