当前位置:   article > 正文

ThreeJs 导入外部三维模型,并实现鼠标滚动放大缩小旋转效果_threejs怎么旋转导入模型

threejs怎么旋转导入模型

  1. let i = 0;
  2. function init() {
  3. // create a scene, that will hold all our elements such as objects, cameras and lights.
  4. var scene = new THREE.Scene();
  5. // create a camera, which defines where we're looking at.
  6. var camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 1000);
  7. var renderer = new THREE.WebGLRenderer();
  8. var axes = new THREE.AxesHelper(20);
  9. var controls = new THREE.TrackballControls(camera); //创建场景旋转缩放事件
  10. camera.position.set(-30, 40, 30);
  11. camera.lookAt(scene.position);
  12. renderer.setClearColor(new THREE.Color(0xcccccc)); // 设置渲染面板颜色
  13. renderer.setSize(window.innerWidth, window.innerHeight); // 设置渲染面板长宽
  14. // // show axes in the screen
  15. // 显示三维坐标轴
  16. scene.add(axes);
  17. controls = new THREE.TrackballControls(camera); //创建场景旋转缩放事件
  18. controls.rotateSpeed = 2.5;
  19. controls.zoomSpeed = 1.2;
  20. controls.panSpeed = 0.8;
  21. controls.noZoom = false;
  22. controls.noPan = false;
  23. controls.staticMoving = true;
  24. controls.dynamicDampingFactor = 0.3;
  25. // create a render and set the size
  26. // 设置渲染面板属性
  27. // create the ground plane
  28. // var planeGeometry = new THREE.PlaneGeometry(60, 20);
  29. // var planeMaterial = new THREE.MeshBasicMaterial({
  30. // color: 0xAAAAAA
  31. // });
  32. // var plane = new THREE.Mesh(planeGeometry, planeMaterial);
  33. // // rotate and position the plane
  34. // plane.rotation.x = -0.5 * Math.PI;
  35. // plane.position.set(15, 0, 0);
  36. // // add the plane to the scene
  37. // scene.add(plane);
  38. // create a cube
  39. // position the cube
  40. // cube.position.set(-4, 3, 0);
  41. // add the cube to the scene
  42. // create a sphere
  43. // var sphereGeometry = new THREE.SphereGeometry(4, 20, 20);
  44. // var sphereMaterial = new THREE.MeshBasicMaterial({
  45. // color: 0x7777FF,
  46. // wireframe: true
  47. // });
  48. // var sphere = new THREE.Mesh(sphereGeometry, sphereMaterial);
  49. // // position the sphere
  50. // sphere.position.set(20, 4, 2);
  51. // // add the sphere to the scene
  52. // scene.add(sphere);
  53. // position and point the camera to the center of the scene
  54. // add the output of the renderer to the html element
  55. // render the scene
  56. let addT = null;
  57. let redt = null;
  58. let timeAdd = null;
  59. let timeDel = null;
  60. let step = 1;
  61. let frequ = 100;
  62. // 数值增加到制定数字
  63. function add (dis) {
  64. clearInterval(timeDel);
  65. timeAdd = setInterval(() => {
  66. if (i < dis) {
  67. i++;
  68. intCub();
  69. } else {
  70. clearInterval(timeAdd);
  71. // del(0);
  72. }
  73. console.log(i);
  74. }, frequ);
  75. };
  76. // 数值减少到制定数字
  77. function del (dis) {
  78. clearInterval(timeAdd);
  79. timeDel = setInterval(() => {
  80. if (i > dis) {
  81. i--;
  82. intCub();
  83. } else {
  84. val = dis;
  85. clearInterval(timeDel);
  86. add(50)
  87. }
  88. console.log(i);
  89. }, frequ);
  90. };
  91. function intCub () {
  92. let random = parseInt(1 + (4 - 1) * (Math.random())); // 随机数用于正方体的长宽高
  93. let randomC = parseInt(1 + (2 - 1) * (Math.random())); // 随机数用于球形的半径
  94. let colorRandomNum = parseInt(1 + (7 - 1) * (Math.random())); // 随机数用于赋值后续的物体的材质颜色
  95. let randomColor = [0xF7CE18, 0x2550EC, 0x57E10C, 0xEB6F0A, 0xEB0AE9, 0x820745, 0x8D11D8];
  96. // 配置灯光
  97. let light = new THREE.AmbientLight(0x820745);
  98. light.position.set(100, 100, 200);
  99. // 生成正方体
  100. var cubeGeometry = new THREE.BoxGeometry(random, random, random); // 长宽高
  101. // 给正方体网格添加材质
  102. var cubeMaterial = new THREE.MeshBasicMaterial({
  103. color: randomColor[colorRandomNum],
  104. wireframe: false // 是否显示网格状态
  105. });
  106. var cube = new THREE.Mesh(cubeGeometry, cubeMaterial); // 将材质贴到模型上
  107. // 生成原型
  108. var sphereGeometry = new THREE.SphereGeometry(randomC, 200, 200); // 半径和网格数,网格数表示球体的粗糙程度
  109. var sphereMaterial = new THREE.MeshBasicMaterial({
  110. color: randomColor[colorRandomNum],
  111. wireframe: false // 是否显示网格状态
  112. });
  113. var sphere = new THREE.Mesh(sphereGeometry, sphereMaterial); // 将材质贴到模型上
  114. // position the sphere
  115. // 设置球体的位置
  116. sphere.position.set(parseInt(-10 + (25 - 1) * (Math.random())), parseInt(-10 + (25 - 1) * (Math.random())), parseInt(-10 + (25 - 1) * (Math.random())));
  117. // 设置正方体的位置
  118. cube.position.set(parseInt(-10 + (25 - 1) * (Math.random())), parseInt(-10 + (25 - 1) * (Math.random())), parseInt(-10 + (25 - 1) * (Math.random())));
  119. // add the sphere to the scene
  120. // 将贴好材质的模型和灯光添加到场景
  121. scene.add(sphere);
  122. scene.add(cube);
  123. scene.add(light);
  124. //声明raycaster和mouse变量
  125. var raycaster = new THREE.Raycaster();
  126. var mouse = new THREE.Vector2();
  127. // 绑定点击事件
  128. function onMouseClick( event ) {
  129. //通过鼠标点击的位置计算出raycaster所需要的点的位置,以屏幕中心为原点,值的范围为-1到1.
  130. mouse.x = ( event.clientX / window.innerWidth ) * 2 - 1;
  131. mouse.y = - ( event.clientY / window.innerHeight ) * 2 + 1;
  132. // 通过鼠标点的位置和当前相机的矩阵计算出raycaster
  133. raycaster.setFromCamera( mouse, camera );
  134. // 获取raycaster直线和所有模型相交的数组集合
  135. var intersects = raycaster.intersectObjects(scene.children);
  136. console.log(intersects);
  137. //将所有的相交的模型的颜色设置为红色,如果只需要将第一个触发事件,那就数组的第一个模型改变颜色即可
  138. for ( var i = 0; i < intersects.length; i++ ) {
  139. intersects[i].object.material.color.set(0x000000);
  140. let msg = JSON.stringify(intersects[i]);
  141. document.querySelector('.msg .content').innerHTML = msg;
  142. }
  143. // alert('我点击了对象,对象信息已经在控制台打印出来');
  144. renderer.render(scene, camera); // 渲染场景中的模型
  145. }
  146. // // window.removeEventListener('click', onMouseClick);
  147. // document.onmousedown = function(event) {
  148. // document.onmousemove = function () {
  149. // controls.update();
  150. // renderer.render(scene, camera); // 渲染场景中的模型
  151. // }
  152. // }
  153. // document.onmouseup = function(event) {
  154. // document.onmousemove = null
  155. // }
  156. document.getElementById("webgl-output").appendChild(renderer.domElement);
  157. // 定时鼠标点击移动或者滚轮滚动的时候要触发渲染事件,画面是不会跟着放大缩小旋转
  158. setInterval(() => {
  159. controls.update();
  160. renderer.render(scene, camera); // 渲染场景中的模型
  161. }, 5);
  162. // renderer.render(scene, camera); // 渲染场景中的模型
  163. /**************加载模型************** */
  164. var loader = new THREE.OBJLoader();//在init函数中,创建loader变量,用于导入模型
  165. let i = 0.04;
  166. let step = 0.02;
  167. let v = 0;
  168. loader.load(
  169. // 资源链接
  170. 'http://10.1.252.90:8080/src/chapter-01/models/man.obj',
  171. // 资源加载完成后的回调函数
  172. function (object) {
  173. console.log(object);
  174. object.position.set(0, 0, 0);
  175. object.rotation.z = 3.1415927; // 纠正导入
  176. var ms = new THREE.MeshBasicMaterial({
  177. color: 0xcccccc,
  178. wireframe: true // 是否显示网格状态
  179. });
  180. scene.add(object);
  181. renderer.render(scene, camera); // 渲染场景中的模型
  182. window.addEventListener('click', onMouseClick, false);
  183. // // var sphere = new THREE.Mesh(object, ms); // 将材质贴到模型上
  184. setInterval(() => {
  185. i += step;
  186. object.rotation.y = i;
  187. scene.add(object);
  188. // renderer.render(scene, camera); // 渲染场景中的模型
  189. }, 10);
  190. }
  191. );
  192. /**************加载模型************** */
  193. };
  194. // 运行渲染
  195. add (10);
  196. intCub();
  197. }
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>Example 01.02 - First Scene</title>
  5. <meta charset="UTF-8" />
  6. <script type="text/javascript" charset="UTF-8" src="../../libs/three/three.js"></script>
  7. <script type="text/javascript" charset="UTF-8" src="../../libs/three/loaders/OBJLoader.js"></script>
  8. <script type="text/javascript" charset="UTF-8" src="../../libs/three/controls/TrackballControls.js"></script>
  9. <script type="text/javascript" charset="UTF-8" src="../../libs/three/controls/DragControls.js"></script>
  10. <link rel="stylesheet" href="../../css/default.css">
  11. <style>
  12. #render{
  13. position: fixed;
  14. left: 20px;
  15. top: 30px;
  16. height: 40px;
  17. width: 120px;
  18. }
  19. .msg{
  20. position: absolute;
  21. left: 10px;
  22. top: 10px;
  23. height: 600px;
  24. min-height: 550px;
  25. overflow:scroll;
  26. width: 500px;
  27. background: #fff;
  28. border: 2px solid #eee;
  29. }
  30. h3{
  31. padding: 0;
  32. margin: 0;
  33. text-align: center;
  34. }
  35. </style>
  36. </head>
  37. <body>
  38. <!-- Div which will hold the Output -->
  39. <div id="webgl-output"></div>
  40. <div class="msg">
  41. <h3>鼠标点击的对象信息</h3>
  42. <div class="content"></div>
  43. </div>
  44. <script type="text/javascript" src="./js/01-02.js"></script>
  45. <!-- Javascript code that runs our Three.js examples -->
  46. <script type="text/javascript">
  47. (function () {
  48. // your page initialization code here
  49. // the DOM will be available here
  50. init();
  51. })();
  52. </script>
  53. </body>
  54. </html>

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

闽ICP备14008679号