Three.js中显示坐标轴、平面、球体、四方体
生活随笔
收集整理的這篇文章主要介紹了
Three.js中显示坐标轴、平面、球体、四方体
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
場景
Three.js入門和搭建HelloWorld:
https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/119149625
在上面已經(jīng)能實現(xiàn)顯示坐標軸和球體的基礎(chǔ)上。
怎樣顯示立方體和平面。
?
注:
博客:
https://blog.csdn.net/badao_liumang_qizhi
關(guān)注公眾號
霸道的程序猿
獲取編程相關(guān)電子書、教程推送與免費下載。
實現(xiàn)
創(chuàng)建坐標軸對象并添加進場景
??????? // 創(chuàng)建坐標軸對象var axes = new THREE.AxisHelper(20);//將坐標軸添加進場景scene.add(axes);創(chuàng)建平面對象并設(shè)置平面繞X軸旋轉(zhuǎn)90度
??????? // 創(chuàng)建平面,并定義平面的尺寸var planeGeometry = new THREE.PlaneGeometry(60, 20);//創(chuàng)建一個基本材質(zhì),并設(shè)置顏色var planeMaterial = new THREE.MeshBasicMaterial({color: 0xcccccc});//把兩個對象合并到Mesh網(wǎng)格對象var plane = new THREE.Mesh(planeGeometry, planeMaterial);// 設(shè)置平面繞x軸旋轉(zhuǎn)90度plane.rotation.x = -0.5 * Math.PI;// 設(shè)置平面的坐標位置plane.position.x = 15;plane.position.y = 0;plane.position.z = 0;// 將平面添加進場景scene.add(plane);創(chuàng)建立方體并添加進場景
??????? // 創(chuàng)建一個立方體var cubeGeometry = new THREE.BoxGeometry(4, 4, 4);//設(shè)置材質(zhì)//把線框?qū)傩詗ireframe設(shè)置為truevar cubeMaterial = new THREE.MeshBasicMaterial({color: 0xff0000, wireframe: true});//合并var cube = new THREE.Mesh(cubeGeometry, cubeMaterial);// 設(shè)置立方體坐標cube.position.x = 20;cube.position.y = 12;cube.position.z = 2;// 將立方體添加進場景scene.add(cube);創(chuàng)建球體并添加進場景
??????? // 創(chuàng)建一個球體var sphereGeometry = new THREE.SphereGeometry(4, 20, 20);//把線框?qū)傩詗ireframe設(shè)置為truevar sphereMaterial = new THREE.MeshBasicMaterial({color: 0x7777ff, wireframe: true});var sphere = new THREE.Mesh(sphereGeometry, sphereMaterial);// 設(shè)置球體的坐標sphere.position.x = 20;sphere.position.y = 4;sphere.position.z = 2;// 添加進場景scene.add(sphere);完整的實現(xiàn)代碼
<!DOCTYPE html><html><head><title>第一個場景的使用</title><script type="text/javascript" src="./js/three.js"></script><style>body {/* 將邊距設(shè)置為0,溢出設(shè)置為隱藏,以實現(xiàn)全屏顯示 */margin: 0;overflow: hidden;}</style> </head> <body><!-- 顯示的div --> <div id="WebGL-output"> </div><script type="text/javascript">// 初始化的方法function init() {// 創(chuàng)建一個場景,它將包含我們所有的元素,如物體,攝像機和燈光var scene = new THREE.Scene();// 創(chuàng)建一個相機,它定義了我們正在看的地方var camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 1000);// 創(chuàng)建渲染器并設(shè)置大小var renderer = new THREE.WebGLRenderer();//將renderer的背景色設(shè)置為接近白色renderer.setClearColorHex();renderer.setClearColor(new THREE.Color(0xEEEEEE));//設(shè)置大小renderer.setSize(window.innerWidth, window.innerHeight);// 創(chuàng)建坐標軸對象var axes = new THREE.AxisHelper(20);//將坐標軸添加進場景scene.add(axes);// 創(chuàng)建平面,并定義平面的尺寸var planeGeometry = new THREE.PlaneGeometry(60, 20);//創(chuàng)建一個基本材質(zhì),并設(shè)置顏色var planeMaterial = new THREE.MeshBasicMaterial({color: 0xcccccc});//把兩個對象合并到Mesh網(wǎng)格對象var plane = new THREE.Mesh(planeGeometry, planeMaterial);// 設(shè)置平面繞x軸旋轉(zhuǎn)90度plane.rotation.x = -0.5 * Math.PI;// 設(shè)置平面的坐標位置plane.position.x = 15;plane.position.y = 0;plane.position.z = 0;// 將平面添加進場景scene.add(plane);// 創(chuàng)建一個立方體var cubeGeometry = new THREE.BoxGeometry(4, 4, 4);//設(shè)置材質(zhì)//把線框?qū)傩詗ireframe設(shè)置為truevar cubeMaterial = new THREE.MeshBasicMaterial({color: 0xff0000, wireframe: true});//合并var cube = new THREE.Mesh(cubeGeometry, cubeMaterial);// 設(shè)置立方體坐標cube.position.x = 20;cube.position.y = 12;cube.position.z = 2;// 將立方體添加進場景scene.add(cube);// 創(chuàng)建一個球體var sphereGeometry = new THREE.SphereGeometry(4, 20, 20);//把線框?qū)傩詗ireframe設(shè)置為truevar sphereMaterial = new THREE.MeshBasicMaterial({color: 0x7777ff, wireframe: true});var sphere = new THREE.Mesh(sphereGeometry, sphereMaterial);// 設(shè)置球體的坐標sphere.position.x = 20;sphere.position.y = 4;sphere.position.z = 2;// 添加進場景scene.add(sphere);// 定義相機的坐標,即懸掛在場景的上方camera.position.x = -30;camera.position.y = 40;camera.position.z = 30;//為了確保相機能夠拍攝到這些物體,使用lookat函數(shù)指向場景的中心camera.lookAt(scene.position);// 將renderer的輸出掛接到HTML終點div元素document.getElementById("WebGL-output").appendChild(renderer.domElement);// 渲染場景renderer.render(scene, camera);}window.onload = init;</script> </body> </html>?
總結(jié)
以上是生活随笔為你收集整理的Three.js中显示坐标轴、平面、球体、四方体的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Three.js入门和搭建HelloWo
- 下一篇: Docker中宿主机与容器之间互传文件(