three.js實現的地球3D旋轉效果
分享一段程式碼例項,它利用three.js實現了地球的3D旋轉效果。
特別說明:由於網路問題,可能需要多等候幾秒鐘才能檢視完整效果。
程式碼例項如下:
[HTML] 純文字檢視 複製程式碼執行程式碼<!doctype html> <html> <head> <meta charset="utf-8"> <meta name="author" content="http://www.softwhy.com/" /> <title>螞蟻部落</title> <style> body { margin: 0; } #canvasBox { width: 100%; height: 99.5%; position: absolute; } </style> </head> <body> <div id="canvasBox"></div> <script src="https://cdn.bootcss.com/three.js/r76/three.min.js"></script> <script type="text/javascript" src="http://libs.baidu.com/jquery/1.11.1/jquery.min.js"></script> <script> var canvasBox = document.getElementById("canvasBox"); var W = $("#canvasBox").width(); var H = $("#canvasBox").height(); //隨瀏覽器視窗變化而變化 function onWindowResize() { camera.aspect = window.innerWidth / window.innerHeight; camera.updateProjectionMatrix(); renderer.setSize(window.innerWidth, window.innerHeight); } $(window).on("resize", function() { onWindowResize(); }) var num = 0; var scene, camera, renderer, light; function intScene() { scene = new THREE.Scene(); } function intCamera() { camera = new THREE.PerspectiveCamera(45, W / H, 1, 1000); camera.position.set(0, 0, 400); camera.up.set(0, 1, 0); camera.lookAt({ x: 0, y: 0, z: 0 }) } function intRender() { renderer = new THREE.WebGLRenderer({ antialias: true }); //renderer = new THREE.CanvasRenderer(); renderer.setSize(W, H); canvasBox.appendChild(renderer.domElement); } function intLight() { light = new THREE.DirectionalLight(0xffffff, 0.6); light.position.set(0, 0, 400); scene.add(light); } var xkbg, earth, texts; function intModels() { /* 宇宙背景 */ var yz_geometry = new THREE.SphereGeometry(500, 500, 50); var yz_material = new THREE.MeshPhongMaterial({ map: THREE.ImageUtils.loadTexture('demo/js/img/yuzhou.jpg'), side: THREE.DoubleSide }); xkbg = new THREE.Mesh(yz_geometry, yz_material); xkbg.position.set(0, 0, 0); scene.add(xkbg); //地球 var earth_geometry = new THREE.SphereGeometry(100, 50, 50); var earth_material = new THREE.MeshPhongMaterial({ map: THREE.ImageUtils.loadTexture('demo/js/img/earthj.jpg'), side: THREE.DoubleSide }); earth = new THREE.Mesh(earth_geometry, earth_material); earth.position.set(0, 0, 0); xkbg.add(earth); } var mouseX, mouseY, isMove = true; //自轉 function zizhuan() { if (isMove) { requestAnimationFrame(zizhuan); xkbg.rotation.y -= 0.002; renderer.render(scene, camera); } } //拖拽 document.onmousedown = function(e) { isMove = false; mouseX = e.pageX; mouseY = e.pageY; } document.onmousemove = function rt(e) { if (!isMove) { var disX = e.pageX - mouseX; var disY = e.pageY - mouseY; requestAnimationFrame(zizhuan); xkbg.rotation.x = xkbg.rotation.x > 0.8 ? 0.8 : xkbg.rotation.x; xkbg.rotation.x = xkbg.rotation.x < -0.8 ? -0.8 : xkbg.rotation.x; xkbg.rotation.x += disY * 0.00005 * Math.PI; xkbg.rotation.y += disX * 0.0001 * Math.PI; renderer.render(scene, camera); } } document.onmouseup = function() { console.log(xkbg.rotation.y) isMove = true; setTimeout(function() { zizhuan(); }, 2000) } //滑動滑鼠讓地球放大縮小 function intsScale() { $(document).on('mousewheel DOMMouseScroll', function(ev) { var e = ev || event; e.preventDefault(); var value = e.originalEvent.wheelDelta || -e.originalEvent.detail; var delta = Math.max(-1, Math.min(1, value)); if (delta == 1) { num++; num = num > 10 ? 10 : num; } else { num--; num = num < -15 ? -15 : num; } camera.position.set(0, 0, 400 + num * 10); }); } function intStart() { intScene(); intCamera(); intRender(); intLight(); intModels(); zizhuan(); intsScale(); renderer.render(scene, camera); } intStart(); </script> </body> </html>
相關文章
- three.js實現的3D球狀拖動旋轉效果JS3D
- canvas實現太陽、地球和月亮旋轉效果Canvas
- Flash遮罩做地球旋轉效果遮罩
- CSS3實現的3D旋轉效果CSSS33D
- | / - 的旋轉效果實現(轉)
- css3實現的3D立體旋轉效果CSSS33D
- css3實現的立方體3D旋轉效果CSSS33D
- CSS 3D旋轉效果CSS3D
- SVG實現的圓環旋轉效果SVG
- iOS實現3D旋轉iOS3D
- 如何實現圖片的3D旋轉,而且是不停旋轉?3D
- javascript 3D旋轉滾動效果JavaScript3D
- canvas實現的旋轉太極圖效果Canvas
- 使用Three.js實現神奇的3D文字懸浮效果JS3D
- css3實現的旋轉的陀螺效果CSSS3
- 滑鼠懸浮div實現旋轉效果
- 簡單的css3頭像旋轉與3D旋轉效果CSSS33D
- 3D旋轉效果程式碼例項3D
- CSS3 3d旋轉魔方效果CSSS33D
- css3實現的立方體旋轉效果CSSS3
- 滑鼠懸浮實現環形旋轉效果
- HTML+CSS實現太極旋轉效果HTMLCSS
- css3實現環狀旋轉效果CSSS3
- css3實現的旋轉載入等待效果CSSS3
- 使用 three.js 中的 CSS3DRenderer 實現 3d 卡片的效果JSCSSS33D
- CSS3邊框旋轉動畫實現效果CSSS3動畫
- jQuery和css3實現的摩天輪旋轉效果jQueryCSSS3
- CSS3實現的小球旋轉載入等待效果CSSS3
- javascript元素3D旋轉效果程式碼例項JavaScript3D
- CSS3立方體3D旋轉效果CSSS33D
- 好玩的WPF第四彈:用Viewport2DVisual3D實現3D旋轉效果View3D
- CSS3實現的3D旋轉程式碼例項CSSS33D
- css3實現的旋轉魔方效果程式碼例項CSSS3
- css3實現的loadding旋轉載入效果CSSS3
- canvas實現的旋轉的太極圖效果程式碼例項Canvas
- three.js實現炫酷的3d影院JS3D
- css地球月亮太陽環繞旋轉CSS
- 基於HT for Web向量實現3D葉輪旋轉Web3D