WebGL Solar System

本文介绍了一个使用Three.js库实现的太阳系行星运行模拟项目。该模拟不仅展现了各行星围绕太阳公转的效果,还实现了自转功能,并允许用户通过鼠标交互选择行星以显示详细信息。

点击图片转到我的51cto博客查看


附源代码:

<!doctype html>
<html lang="en">
<head>
    <title>webgl solar system</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
    <style>
        body {
            color: #61443e;
            font-family:Monospace;
            font-size:13px;
            text-align:center;

            background-color: #aaccff;
            margin: 0px;
            overflow: hidden;
        }

        #info {
            color: #ffffff;
            position: absolute;
            top: 0px; width: 100%;
            padding: 5px;
        }

        a {

            color: yellow;
        }

        #oldie {
            background:rgb(0,0,50) !important;
            color:#fff !important;
        }

    </style>
</head>
<body>

<div id="container"></div>

<script src="Three.js"></script>

<script src="Detector.js"></script>
<script src="Stats.js"></script>
<script>

    if ( ! Detector.webgl ) {

        Detector.addGetWebGLMessage();
        document.getElementById( 'container' ).innerHTML = "";

    }

    var container, stats;

    var camera, controls, scene, renderer, projector;

    var mouse = new THREE.Vector2();

    var objects = [], SELECTED;

    var clock = new THREE.Clock();

    var starsArray = ["Sun","Mercury","Venus","Earth","Mars","Jupiter","Saturn","Uranus","Neptune","Pluto"];
    var stars = {
        Sun:{OrbitTime:99999, RotationTime:99999, Radius:100, InitAngle:0, texture:"Data/sun.png", position:new THREE.Vector3(0, 0, 0),rotaionRadius : 0,desc:""},
        Mercury:{OrbitTime:87.969, RotationTime:1407.6, Radius:20, InitAngle:30, texture:"Data/mercury.png", position:new THREE.Vector3(200, 0, 0),rotaionRadius : 500,desc:""},
        Venus:{OrbitTime:224.7, RotationTime:243 * 23.9, Radius:30, InitAngle:90, texture:"Data/venus.png", position:new THREE.Vector3(300, 0, 0),rotaionRadius : 600,desc:""},
        Earth:{OrbitTime:365.25636, RotationTime:23.9, Radius:50, InitAngle:120, texture:"Data/earth.png", position:new THREE.Vector3(400, 0, 0),rotaionRadius : 700,desc:""},
        Mars:{OrbitTime:687, RotationTime:24.6, Radius:25, InitAngle:250, texture:"Data/mars.png", position:new THREE.Vector3(500, 0, 0),rotaionRadius : 800,desc:""},
        Jupiter:{OrbitTime:11.86 * 365.25636, RotationTime:9.9, Radius:90, InitAngle:200, texture:"Data/jupiter.png", position:new THREE.Vector3(700, 0, 0),rotaionRadius : 900,desc:""},
        Saturn:{OrbitTime:29.5 * 365.25636, RotationTime:10.67, Radius:80, InitAngle:160, texture:"Data/saturn.png", position:new THREE.Vector3(900, 0, 0),rotaionRadius : 1000,desc:""},
        Uranus:{OrbitTime:84 * 365.25636, RotationTime:15.6, Radius:60, InitAngle:270, texture:"Data/uranus.png", position:new THREE.Vector3(1000, 0, 0),rotaionRadius : 1100,desc:""},
        Neptune:{OrbitTime:164.8 * 365.25636, RotationTime:18.4, Radius:55, InitAngle:60, texture:"Data/neptune.png", position:new THREE.Vector3(1100, 0, 0),rotaionRadius : 1200,desc:""},
        Pluto:{OrbitTime:248 * 365.25636, RotationTime:6.38, Radius:10, InitAngle:150, texture:"Data/pluto.png", position:new THREE.Vector3(1200, 0, 0),rotaionRadius : 1300,desc:""}
    };

    var earthObject;

    init();
    animate();

    function init() {

        container = document.getElementById( 'container' );

        scene = new THREE.Scene();
        //scene.fog = new THREE.FogExp2( 0xaaccff, 0.0007 );

        camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 1, 20000 );
        camera.position.x = 0;
        camera.position.y = 50;
        camera.position.z = 200;
        scene.add( camera );

        controls = new THREE.FirstPersonControls( camera );
        controls.movementSpeed = 300;
        controls.lookSpeed = 0.01;
        controls.lon = 300;

        scene.add( new THREE.AmbientLight( 0x777777 ) );

        var sun_light = new THREE.PointLight( 0xffffff,1,3000);
        sun_light.position.set( 500, 0, 0 );
        scene.add( sun_light );
        sun_light = new THREE.PointLight( 0xffffff,1,3000);
        sun_light.position.set( -500, 0, 0 );
        scene.add( sun_light );
        sun_light = new THREE.PointLight( 0xffffff,1,3000);
        sun_light.position.set( 0, 500, 0 );
        scene.add( sun_light );
        sun_light = new THREE.PointLight( 0xffffff,1,3000);
        sun_light.position.set( 0, -500, 0 );
        scene.add( sun_light );

        var star,materials,starObject;
        for(var i=0;i<starsArray.length;i++){
            eval("star = stars."+starsArray[i]+";");
            if(starsArray[i] == "Sun"){
                materials = new THREE.MeshPhongMaterial( { ambient: 0xffffff,specular:0xffffff,shininess:3000, map: THREE.ImageUtils.loadTexture( star.texture ) } );
            }else{
                materials = new THREE.MeshLambertMaterial( { ambient: 0xffffff, map: THREE.ImageUtils.loadTexture( star.texture ) } );
            }

            starObject = new THREE.Mesh( new THREE.SphereGeometry(star.Radius,64,32), materials );
            starObject.position.set(star.position.x,star.position.y,star.position.z);
            scene.add( starObject );
            star.object = starObject;
            objects.push( starObject );

            var smileyShape = new THREE.Shape();
            smileyShape.moveTo( 0, 0 );
            smileyShape.arc( 0, 0, star.rotaionRadius, 0, Math.PI*2, false );
            var circlePoints = smileyShape.createPointsGeometry(20);
            var line = new THREE.Line( circlePoints, new THREE.LineBasicMaterial( { color: 0xff0000, linewidth: 2 } ) );
            scene.add( line );
        }

        var object = new THREE.AxisHelper();
        object.position.set( 0, 0, 0 );
        object.scale.x = object.scale.y = object.scale.z = 2;
        scene.add( object );

        renderer = new THREE.WebGLRenderer( { clearColor: 0x000000, clearAlpha: 1 } );
        renderer.setSize( window.innerWidth, window.innerHeight );

        container.innerHTML = "";

        container.appendChild( renderer.domElement );

        stats = new Stats();
        stats.domElement.style.position = 'absolute';
        stats.domElement.style.top = '0px';
        container.appendChild( stats.domElement );

        projector = new THREE.Projector();
        document.addEventListener( 'mouseup', onDocumentMouseUp, false );
    }

    function onDocumentMouseUp( event ) {
        event.preventDefault();
        mouse.x = ( event.clientX / window.innerWidth ) * 2 - 1;
        mouse.y = - ( event.clientY / window.innerHeight ) * 2 + 1;

        var vector = new THREE.Vector3( mouse.x, mouse.y, 0.5 );
        projector.unprojectVector( vector, camera );

        var ray = new THREE.Ray( camera.position, vector.subSelf( camera.position ).normalize() );
        var intersects = ray.intersectObjects( objects );

        if ( intersects.length > 0 ) {

            SELECTED = intersects[ 0 ].object;
            var star;
            for(var i=0;i<starsArray.length;i++){
                eval("star = stars."+starsArray[i]+";");
                if(star.object == SELECTED){
                    alert(starsArray[i]+"\n"+star.desc+"\n"+SELECTED.position.x +"," + SELECTED.position.y + "," + SELECTED.position.z);
                    break;
                }
            }
        }
    }

    function animate() {

        requestAnimationFrame( animate );

        render();
        stats.update();

    }

    function render() {

        var delta = clock.getDelta(),
                time = clock.getElapsedTime() * 10;

        controls.update( delta );
        var star;
        for(var i=0;i<starsArray.length;i++){
            eval("star = stars."+starsArray[i]+";");
            //var theta =  time % 1000 / 1000 * 2 *3.1415926;
            var rotationTheta = 2 * 3.1415926 / star.RotationTime * time ;
            var orbitTheta = 2 * 3.1415926 / star.OrbitTime * time / 24;
            var pos = star.object.position;
            pos.x = star.rotaionRadius * Math.cos(orbitTheta+star.InitAngle);
            pos.y = star.rotaionRadius * Math.sin(orbitTheta+star.InitAngle);
            star.object.position.set(pos.x,pos.y,pos.z);
            star.object.rotation.set(1.5, rotationTheta,0);
        }
        renderer.render( scene, camera );
    }

</script>

</body>
</html>

对比获取资源文件:

    var stars = {
        Sun:{OrbitTime:99999, RotationTime:99999, Radius:100, InitAngle:0, texture:"http://derekliu.blog.51cto.com/attachment/201205/4479510_1336016383.png", position:new THREE.Vector3(0, 0, 0),rotaionRadius : 0,desc:""},
        Mercury:{OrbitTime:87.969, RotationTime:1407.6, Radius:20, InitAngle:30, texture:"http://derekliu.blog.51cto.com/attachment/201205/4479510_1336016270.png", position:new THREE.Vector3(200, 0, 0),rotaionRadius : 500,desc:""},
        Venus:{OrbitTime:224.7, RotationTime:243 * 23.9, Radius:30, InitAngle:90, texture:"http://derekliu.blog.51cto.com/attachment/201205/4479510_1336016343.png", position:new THREE.Vector3(300, 0, 0),rotaionRadius : 600,desc:""},
        Earth:{OrbitTime:365.25636, RotationTime:23.9, Radius:50, InitAngle:120, texture:"http://derekliu.blog.51cto.com/attachment/201205/4479510_1336016015.png", position:new THREE.Vector3(400, 0, 0),rotaionRadius : 700,desc:""},
        Mars:{OrbitTime:687, RotationTime:24.6, Radius:25, InitAngle:250, texture:"http://derekliu.blog.51cto.com/attachment/201205/4479510_1336016017.png", position:new THREE.Vector3(500, 0, 0),rotaionRadius : 800,desc:""},
        Jupiter:{OrbitTime:11.86 * 365.25636, RotationTime:9.9, Radius:90, InitAngle:200, texture:"http://derekliu.blog.51cto.com/attachment/201205/4479510_1336016016.png", position:new THREE.Vector3(700, 0, 0),rotaionRadius : 900,desc:""},
        Saturn:{OrbitTime:29.5 * 365.25636, RotationTime:10.67, Radius:80, InitAngle:160, texture:"http://derekliu.blog.51cto.com/attachment/201205/4479510_1336016341.png", position:new THREE.Vector3(900, 0, 0),rotaionRadius : 1000,desc:""},
        Uranus:{OrbitTime:84 * 365.25636, RotationTime:15.6, Radius:60, InitAngle:270, texture:"http://derekliu.blog.51cto.com/attachment/201205/4479510_1336016342.png", position:new THREE.Vector3(1000, 0, 0),rotaionRadius : 1100,desc:""},
        Neptune:{OrbitTime:164.8 * 365.25636, RotationTime:18.4, Radius:55, InitAngle:60, texture:"http://derekliu.blog.51cto.com/attachment/201205/4479510_1336016271.png", position:new THREE.Vector3(1100, 0, 0),rotaionRadius : 1200,desc:""},
        Pluto:{OrbitTime:248 * 365.25636, RotationTime:6.38, Radius:10, InitAngle:150, texture:"http://derekliu.blog.51cto.com/attachment/201205/4479510_1336016271.png", position:new THREE.Vector3(1200, 0, 0),rotaionRadius : 1300,desc:""}
    };


代码下载地址: https://pan.quark.cn/s/b4a8e0160cfc 齿轮与轴系零件在机械设备中扮演着至关重要的角色,它们负责实现动力传输、调整运动形态以及承受工作载荷等核心功能。 在机械工程的设计实践中,齿轮和轴系的设计是一项关键的技术任务,其内容涵盖了材料选用、构造规划、承载能力分析等多个技术层面。 下面将系统性地介绍《齿轮及轴系零件结构设计指导书》中的核心知识点。 一、齿轮设计1. 齿轮种类:依据齿廓轮廓的不同,齿轮可划分为直齿齿轮、斜齿轮以及人字齿轮等类别,各类齿轮均具有特定的性能特点与适用工况,能够满足多样化的工作环境与载荷需求。 2. 齿轮规格参数:模数大小、压力角数值、齿数数量、分度圆尺寸等是齿轮设计的基础数据,这些参数直接决定了齿轮的物理尺寸与运行性能。 3. 齿轮材质选用:齿轮材料的确定需综合评估其耐磨损性能、硬度水平以及韧性表现,常用的材料包括铸铁、钢材、铝合金等。 4. 齿轮强度验证:需进行齿面接触应力分析与齿根弯曲应力分析,以确保齿轮在实际运行过程中不会出现过度磨损或结构破坏。 5. 齿轮加工工艺:涉及切削加工、滚齿加工、剃齿加工、淬火处理等工艺流程,工艺方案的选择将直接影响齿轮的加工精度与使用寿命。 二、轴设计1. 轴的分类方式:依据轴在机械装置中的功能定位与受力特点,可将轴划分为心轴、转轴以及传动轴等类型。 2. 轴的材料选择:通常采用钢材作为轴的材料,例如碳素结构钢或合金结构钢,特殊需求时可选用不锈钢材料或轻质合金材料。 3. 轴的构造规划:需详细考虑轴的轴向长度、截面直径、键槽布置、轴承安装位置等要素,以满足轴的强度要求、刚度要求以及稳定性要求。 4. 轴的强度验证:需进行轴的扭转强度分析与弯曲强度分析,以防止轴在运行过程中发生塑性变形...
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值