• 炫酷的宇宙空间


    Space globe - Three.js

    在这里插入图片描述

    <div id="canvas_container">div>
    
    
    
    • 1
    • 2
    • 3
    body {
        margin: 0;
        overflow: hidden;
        width: 100vw;
        height: 100vh;
        background-image: url("https://user-images.githubusercontent.com/26748614/96337246-f14d4580-1085-11eb-8793-a86d929e034d.jpg");
        background-size: cover;
        backdrop-filter:  brightness(50%);
    }
    
    canvas {
        display: block;
    }
    
    #canvas_container {
        width: 100%;
        height: 100vh;
    }
    
    button {
        position: absolute;
        bottom: 5%;
        left: 50%;
        transform: translateX(-50%);
        border: 1px solid white;
        border-radius: 5px;
        font-size: 0.9rem;
        padding: 0.5rem 0.9em;
        background: #000000;
        color: white;
        -webkit-font-smoothing: antialiased;
        font-weight: bold;
        cursor: pointer;
        transition: all .3s;
    }
    
    button:hover {
        background: #ffffff;
        color: #000000;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    let renderer,
    scene,
    camera,
    sphereBg,
    nucleus,
    stars,
    controls,
    container = document.getElementById("canvas_container"),
    timeout_Debounce,
    noise = new SimplexNoise(),
    cameraSpeed = 0,
    blobScale = 3;
    
    
    init();
    animate();
    
    
    function init() {
        scene = new THREE.Scene();
    
        camera = new THREE.PerspectiveCamera(55, window.innerWidth / window.innerHeight, 0.01, 1000)
        camera.position.set(0,0,230);
    
        const directionalLight = new THREE.DirectionalLight("#fff", 2);
        directionalLight.position.set(0, 50, -20);
        scene.add(directionalLight);
    
        let ambientLight = new THREE.AmbientLight("#ffffff", 1);
        ambientLight.position.set(0, 20, 20);
        scene.add(ambientLight);
    
        renderer = new THREE.WebGLRenderer({
            antialias: true,
            alpha: true
        });
        renderer.setSize(container.clientWidth, container.clientHeight);
        renderer.setPixelRatio(window.devicePixelRatio);
        container.appendChild(renderer.domElement);
    
        //OrbitControl
        controls = new THREE.OrbitControls(camera, renderer.domElement);
        controls.autoRotate = true;
        controls.autoRotateSpeed = 4;
        controls.maxDistance = 350;
        controls.minDistance = 150;
        controls.enablePan = false;
    
        const loader = new THREE.TextureLoader();
        const textureSphereBg = loader.load('https://i.ibb.co/4gHcRZD/bg3-je3ddz.jpg');
        const texturenucleus = loader.load('https://i.ibb.co/hcN2qXk/star-nc8wkw.jpg');
        const textureStar = loader.load("https://i.ibb.co/ZKsdYSz/p1-g3zb2a.png");
        const texture1 = loader.load("https://i.ibb.co/F8by6wW/p2-b3gnym.png");  
        const texture2 = loader.load("https://i.ibb.co/yYS2yx5/p3-ttfn70.png");
        const texture4 = loader.load("https://i.ibb.co/yWfKkHh/p4-avirap.png");
    
    
        /*  Nucleus  */   
        texturenucleus.anisotropy = 16;
        let icosahedronGeometry = new THREE.IcosahedronGeometry(30, 10);
        let lambertMaterial = new THREE.MeshPhongMaterial({ map: texturenucleus });
        nucleus = new THREE.Mesh(icosahedronGeometry, lambertMaterial);
        scene.add(nucleus);
    
    
        /*    Sphere  Background   */
        textureSphereBg.anisotropy = 16;
        let geometrySphereBg = new THREE.SphereBufferGeometry(150, 40, 40);
        let materialSphereBg = new THREE.MeshBasicMaterial({
            side: THREE.BackSide,
            map: textureSphereBg,
        });
        sphereBg = new THREE.Mesh(geometrySphereBg, materialSphereBg);
        scene.add(sphereBg);
    
    
        /*    Moving Stars   */
        let starsGeometry = new THREE.Geometry();
    
        for (let i = 0; i < 50; i++) {
            let particleStar = randomPointSphere(150); 
    
            particleStar.velocity = THREE.MathUtils.randInt(50, 200);
    
            particleStar.startX = particleStar.x;
            particleStar.startY = particleStar.y;
            particleStar.startZ = particleStar.z;
    
            starsGeometry.vertices.push(particleStar);
        }
        let starsMaterial = new THREE.PointsMaterial({
            size: 5,
            color: "#ffffff",
            transparent: true,
            opacity: 0.8,
            map: textureStar,
            blending: THREE.AdditiveBlending,
        });
        starsMaterial.depthWrite = false;  
        stars = new THREE.Points(starsGeometry, starsMaterial);
        scene.add(stars);
    
    
        /*    Fixed Stars   */
        function createStars(texture, size, total) {
            let pointGeometry = new THREE.Geometry();
            let pointMaterial = new THREE.PointsMaterial({
                size: size,
                map: texture,
                blending: THREE.AdditiveBlending,                      
            });
    
            for (let i = 0; i < total; i++) {
                let radius = THREE.MathUtils.randInt(149, 70); 
                let particles = randomPointSphere(radius);
                pointGeometry.vertices.push(particles);
            }
            return new THREE.Points(pointGeometry, pointMaterial);
        }
        scene.add(createStars(texture1, 15, 20));   
        scene.add(createStars(texture2, 5, 5));
        scene.add(createStars(texture4, 7, 5));
    
    
        function randomPointSphere (radius) {
            let theta = 2 * Math.PI * Math.random();
            let phi = Math.acos(2 * Math.random() - 1);
            let dx = 0 + (radius * Math.sin(phi) * Math.cos(theta));
            let dy = 0 + (radius * Math.sin(phi) * Math.sin(theta));
            let dz = 0 + (radius * Math.cos(phi));
            return new THREE.Vector3(dx, dy, dz);
        }
    }
    
    
    function animate() {
    
        //Stars  Animation
        stars.geometry.vertices.forEach(function (v) {
            v.x += (0 - v.x) / v.velocity;
            v.y += (0 - v.y) / v.velocity;
            v.z += (0 - v.z) / v.velocity;
    
            v.velocity -= 0.3;
    
            if (v.x <= 5 && v.x >= -5 && v.z <= 5 && v.z >= -5) {
                v.x = v.startX;
                v.y = v.startY;
                v.z = v.startZ;
                v.velocity = THREE.MathUtils.randInt(50, 300);
            }
        });
    
    
        //Nucleus Animation
        nucleus.geometry.vertices.forEach(function (v) {
            let time = Date.now();
            v.normalize();
            let distance = nucleus.geometry.parameters.radius + noise.noise3D(
                v.x + time * 0.0005,
                v.y + time * 0.0003,
                v.z + time * 0.0008
            ) * blobScale;
            v.multiplyScalar(distance);
        })
        nucleus.geometry.verticesNeedUpdate = true;
        nucleus.geometry.normalsNeedUpdate = true;
        nucleus.geometry.computeVertexNormals();
        nucleus.geometry.computeFaceNormals();
        nucleus.rotation.y += 0.002;
    
    
        //Sphere Beckground Animation
        sphereBg.rotation.x += 0.002;
        sphereBg.rotation.y += 0.002;
        sphereBg.rotation.z += 0.002;
    
        
        controls.update();
        stars.geometry.verticesNeedUpdate = true;
        renderer.render(scene, camera);
        requestAnimationFrame(animate);
    }
    
    
    
    /*     Resize     */
    window.addEventListener("resize", () => {
        clearTimeout(timeout_Debounce);
        timeout_Debounce = setTimeout(onWindowResize, 80);
    });
    function onWindowResize() {
        camera.aspect = container.clientWidth / container.clientHeight;
        camera.updateProjectionMatrix();
        renderer.setSize(container.clientWidth, container.clientHeight);
    }
    
    
    
    /*     Fullscreen btn     */
    // let fullscreen;
    // let fsEnter = document.getElementById('fullscr');
    // fsEnter.addEventListener('click', function (e) {
    //     e.preventDefault();
    //     if (!fullscreen) {
    //         fullscreen = true;
    //         document.documentElement.requestFullscreen();
    //         fsEnter.innerHTML = "Exit Fullscreen";
    //     }
    //     else {
    //         fullscreen = false;
    //         document.exitFullscreen();
    //         fsEnter.innerHTML = "Go Fullscreen";
    //     }
    // });
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
  • 相关阅读:
    Scrum敏捷开发:适应变化的核心能力
    简单工厂模式、工厂方法模式、抽象工厂模式
    消防设备电源监控系统在城市建筑中的应用
    2020 MIT6.s081 Lab: Multithreading
    FS2117 SOP8-EP 高效 DC-DC 同步整流升压转换器
    C++进阶-STL 常用算法列举
    【开发经验】gateway网关开发调试优先选择本地服务
    webp格式如何转换成jpg?
    基于粒子群优化算法在配电系统中优化电容器的配置以降低损耗(Matlab代码实现)
    全面吃透JAVA Stream流操作,让代码更加的优雅
  • 原文地址:https://blog.csdn.net/qq_52696618/article/details/132736815