three 實現繞物體旋轉,衛星繞星球旋轉

1.先看看效果javascript

 

2.若是是二維的話,能夠根據變化角度結合三角函數就能計算從x和y座標;css

無奈,若是仍是使用座標改變的方式,博主不會三維數學啊,計算不出x,y和z座標啊。html

還好,過了好長時間,博主終於在網上找到了解決的方法,java

就是把中心點,圓環(圓軌)和衛星三者組合成一體,再改變旋轉角度canvas

 

3.代碼以下數組

<!DOCTYPE html>  
<html lang="en">  
<head>  
    <meta charset="UTF-8">  
    <title>TestRound</title>  
    <style type="text/css">  
        html, body {  
            width: 100%;  
            height: 100%;  
            margin: 0;  
            background-color: black;  
        }  
  
        div#box {  
            width: 100%;  
            height: 100%;  
        }  
    </style>  
    <script type="text/javascript" src="../../js/resource/js/three/three.js"></script>  
</head>  
<body onload="initThree()">  
<div id="box"></div>  
</body>  
<script>  
    var renderer, camera, scene;//渲染器,相加,場景  
    var Earth, satellites = [];//地球,衛星(數組)  
  
    function initThree() {  
        var dom = document.getElementById("box");  
  
        scene = new THREE.Scene();  
        camera = new THREE.PerspectiveCamera(20, dom.clientWidth / dom.clientHeight, 1, 1000);  
        camera.position.set(0, 0, 400);//設置相機位置  
        renderer = new THREE.WebGLRenderer({  
            alpha: true,  
            antialias: true  
        });  
        renderer.setSize(dom.clientWidth, dom.clientHeight);//設置窗口尺寸  
        dom.appendChild(renderer.domElement);  
  
        var sunTexture = THREE.ImageUtils.loadTexture('../../js/resource/img/map_world.png', {}, function () {  
            renderer.render(scene, camera);  
        });  
  
        //地球  
        Earth = new THREE.Mesh(new THREE.SphereGeometry(20, 30, 30), new THREE.MeshBasicMaterial({  
            map: sunTexture  
        })); //材質設定  
  
        var satellite = new THREE.Sprite(new THREE.SpriteMaterial({  
            map: new THREE.CanvasTexture(generateSprite('196,233,255')),  
            blending: THREE.AdditiveBlending  
        }));  
        satellite.scale.x = satellite.scale.y = satellite.scale.z = 60;  
        scene.add(satellite);//添加發光,讓地球有發光的樣式  
        scene.add(Earth);  
  
        //添加衛星  
        satellites.push(initSatellite(5, 28, {x: -Math.PI * 0.35, y: Math.PI * 0.25, z: 0}, 0.021, scene));  
        satellites.push(initSatellite(5, 25, {x: -Math.PI * 0.35, y: -Math.PI * 0.2, z: 0}, 0.022, scene));  
        satellites.push(initSatellite(5, 29, {x: -Math.PI * 0.35, y: Math.PI * 0.05, z: 0}, 0.023, scene));  
  
        render();  
  
    }  
  
      
    /**  
     * 返回一個衛星和軌道的組合體  
     * @param satelliteSize 衛星的大小  
     * @param satelliteRadius 衛星的旋轉半徑  
     * @param rotation 組合體的x,y,z三個方向的旋轉角度  
     * @param speed 衛星運動速度  
     * @param scene 場景  
     * @returns {{satellite: THREE.Mesh, speed: *}} 衛星組合對象;速度  
     */  
    var initSatellite = function (satelliteSize, satelliteRadius, rotation, speed, scene) {  
  
        var track = new THREE.Mesh(new THREE.RingGeometry(satelliteRadius, satelliteRadius + 0.05, 50, 1), new THREE.MeshBasicMaterial());  
        var centerMesh = new THREE.Mesh(new THREE.SphereGeometry(1, 1, 1), new THREE.MeshLambertMaterial()); //材質設定  
        var satellite = new THREE.Sprite(new THREE.SpriteMaterial({  
            map: new THREE.CanvasTexture(generateSprite('196,233,255')),  
            blending: THREE.AdditiveBlending  
        }));  
        satellite.scale.x = satellite.scale.y = satellite.scale.z = satelliteSize;  
        satellite.position.set(satelliteRadius, 0, 0);  
  
        var pivotPoint = new THREE.Object3D();  
        pivotPoint.add(satellite);  
        pivotPoint.add(track);  
        centerMesh.add(pivotPoint);  
        centerMesh.rotation.set(rotation.x, rotation.y, rotation.z);  
        scene.add(centerMesh);  
        return {satellite: centerMesh, speed: speed};  
    };  
  
    /**  
     * 實現發光星星  
     * @param color 顏色的r,g和b值,好比:「123,123,123」;  
     * @returns {Element} 返回canvas對象  
     */  
    var generateSprite = function (color) {  
        var canvas = document.createElement('canvas');  
        canvas.width = 16;  
        canvas.height = 16;  
        var context = canvas.getContext('2d');  
        var gradient = context.createRadialGradient(canvas.width / 2, canvas.height / 2, 0, canvas.width / 2, canvas.height / 2, canvas.width / 2);  
        gradient.addColorStop(0, 'rgba(' + color + ',1)');  
        gradient.addColorStop(0.2, 'rgba(' + color + ',1)');  
        gradient.addColorStop(0.4, 'rgba(' + color + ',.6)');  
        gradient.addColorStop(1, 'rgba(0,0,0,0)');  
        context.fillStyle = gradient;  
        context.fillRect(0, 0, canvas.width, canvas.height);  
        return canvas;  
    };  
      
  
    function render() {  
        renderer.render(scene, camera);  
        Earth.rotation.y -= 0.01;  
        for (var i = 0; i < satellites.length; i++) {  
            satellites[i].satellite.rotation.z -= satellites[i].speed;  
        }  
        requestAnimationFrame(render);  
    }  
  
</script>  
  
</html>  
相關文章
相關標籤/搜索