threejs-3D酷炫地球

<template>
  <div class="home">
    <div class="canvas-container" ref="screenDom" />
  </div>
</template>

<script setup>
import * as THREE from 'three';
import { onMounted, reactive, ref } from "vue";
import { gsap } from 'gsap';
import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls';
let screenDom = ref(null)
let progress = ref(0)

const lon2xyz = (R, longitude, latitude) => {
  let lon = (longitude * Math.PI) / 180; // 轉弧度值
  const lat = (latitude * Math.PI) / 180; // 轉弧度值
  lon = -lon; // js坐標系z坐標軸對應經(jīng)度-90度涛目,而不是90度

  // 經(jīng)緯度坐標轉球面坐標計算公式
  const x = R * Math.cos(lat) * Math.cos(lon);
  const y = R * Math.sin(lat);
  const z = R * Math.cos(lat) * Math.sin(lon);
  // 返回球面坐標
  return new THREE.Vector3(x, y, z);
};

onMounted(() => {
  //創(chuàng)建場景
  let scene = new THREE.Scene();
  // 創(chuàng)建相機
  let camera = new THREE.PerspectiveCamera(
    45,
    window.innerWidth / window.innerHeight,
    0.1,
    100000
  );
  camera.position.set(0, 50, 300)
  //創(chuàng)建渲染器
  let renderer = new THREE.WebGLRenderer({ antialias: true })
  renderer.setSize(window.innerWidth, window.innerHeight)
  //將畫布添加到頁面中
  screenDom.value.appendChild(renderer.domElement)

  //創(chuàng)建控制器
  let controls = new OrbitControls(camera, renderer.domElement)
  controls.autoRotate = true; //自動旋轉

  //創(chuàng)建星空背景
  scene.background = new THREE.Color(0x030311);
  //使用點材質(zhì)創(chuàng)建星空效果
  const vertices = [];
  for (let i = 0; i < 500; i++) {
    const vertex = new THREE.Vector3();
    vertex.x = Math.random() * 800 - 400;
    vertex.y = Math.random() * 800 - 400;
    vertex.z = Math.random() * 800 - 400;
    vertices.push(vertex.x, vertex.y, vertex.z);
  }

  //星空效果
  let starsGeometry = new THREE.BufferGeometry();
  starsGeometry.setAttribute(
    "position",
    new THREE.BufferAttribute(new Float32Array(vertices), 3)
  );

  //加載點材質(zhì)紋理
  const starsTexture = new THREE.TextureLoader().load('./images/stars.png')
  const starsMaterial = new THREE.PointsMaterial({
    map:starsTexture,
    size: 2,
    sizeAttenuation: true, //尺寸衰減
    color: 0x44d76cf,
    transparent: true, //透明度
    opacity: 1
  })
  let stars = new THREE.Points(starsGeometry, starsMaterial);
  scene.add(stars);
  
  // 創(chuàng)建地球
  let earthTexture = new THREE.TextureLoader().load('./images/map.jpg');
  let earthGeometry = new THREE.SphereGeometry(50,32,32)
  
  let earthMaterial = new THREE.MeshBasicMaterial({
    map:earthTexture
  })
  let earth = new THREE.Mesh(earthGeometry,earthMaterial)
  scene.add(earth)
  // 創(chuàng)建發(fā)光的地球
  let lightEarthGeometry = new THREE.SphereGeometry(53,32,32)
  let lightTexture = new THREE.TextureLoader().load('./images/earth.jpg');
  
  let lightEarthMaterial = new THREE.MeshBasicMaterial({
    map: lightTexture,
    alphaMap: lightTexture,
    blending: THREE.AdditiveBlending,
    transparent: true,
  })
  let lightEarth = new THREE.Mesh(lightEarthGeometry, lightEarthMaterial)
  scene.add(lightEarth)

  // 添加地球外發(fā)光精靈
  let spriteTexture = new THREE.TextureLoader().load('./images/glow.png');
  let spriteMaterial = new THREE.SpriteMaterial({
    map: spriteTexture,
    transparent: true,
    color:0x4d76cf,
    depthWrite:false,
    depthTest:false,
    blending: THREE.AdditiveBlending
  })
  let sprite = new THREE.Sprite(spriteMaterial);
  sprite.scale.set(155, 155, 0);
  scene.add(sprite);
  //內(nèi)發(fā)光
  let spriteTexture1 = new THREE.TextureLoader().load('./images/innerGlow.png');
  let spriteMaterial1 = new THREE.SpriteMaterial({
    map: spriteTexture1,
    transparent: true,
    color:0x4d76cf,
    depthWrite:false,
    depthTest:false,
    blending: THREE.AdditiveBlending
  })
  let sprite1 = new THREE.Sprite(spriteMaterial1);
  sprite1.scale.set(128, 128, 0);
  scene.add(sprite1);
  // let scale = new THREE.Vector3(1, 1, 1);

  for (let i = 0; i < 30; i++) {
     // 實現(xiàn)光柱
     let lightPillarTexture = new THREE.TextureLoader().load(
      "./images/light_column.png"
     );
     let lightPillarGeometry = new THREE.PlaneGeometry(3, 20);
     let lightPillarMaterial = new THREE.MeshBasicMaterial({
      color:0xffffff,
      map:lightPillarTexture,
      alphaMap:lightPillarTexture,
      transparent: true,
      blending: THREE.AdditiveBlending,
      side:THREE.DoubleSide,
      depthWrite:false
     })
     let lightPillar = new THREE.Mesh(lightPillarGeometry, lightPillarMaterial);
     lightPillar.add(lightPillar.clone().rotateY(Math.PI / 2));

     //創(chuàng)建波紋擴散效果
     let circlePlane = new THREE.PlaneGeometry(6,6); 
     let circleTexture = new THREE.TextureLoader().load("./images/label.png");
     let circleMaterial = new THREE.MeshBasicMaterial({
      color:0xffffff,
      map:circleTexture,
      transparent: true,
      blending: THREE.AdditiveBlending,
      depthWrite:false,
      side:THREE.DoubleSide,
     })
     let circleMesh = new THREE.Mesh(circlePlane,circleMaterial);
     circleMesh.rotation.x = -Math.PI /2;
     circleMesh.position.set(0,-7,0);
     lightPillar.add(circleMesh);

     gsap.to(circleMesh.scale, {
      duration: 1 + Math.random() * 0.5,
      x: 2,
      y: 2,
      z: 2,
      repeat: -1,
      delay: Math.random() * 0.5,
      yoyo: true,
      ease: "power2.inOut",
    });
    // 設置光柱的位置
    let lat = Math.random() * 180 - 90;
    let lon = Math.random() * 360 - 180;
    let position = lon2xyz(60, lon, lat);
    lightPillar.position.set(position.x, position.y, position.z);

    lightPillar.quaternion.setFromUnitVectors(
      new THREE.Vector3(0, 1, 0),
      position.clone().normalize()
    )
    scene.add(lightPillar)
  }
  
  // 繞地球運行的月球
  let moonTexture = new THREE.TextureLoader().load("./images/moon.jpg");
  let moonGeometry = new THREE.SphereGeometry(5,32,32)
  let moonMaterial = new THREE.MeshBasicMaterial({
    map:moonTexture,
    emissive:0xffffff,
    emissiveMap :moonTexture
  })
  let moon = new THREE.Mesh(moonGeometry,moonMaterial)
  moon.position.set(150,0,0)
  scene.add(moon)
  // 創(chuàng)建月球環(huán)
    let moonRingTexture = new THREE.TextureLoader().load(
    "./images/moon_ring.png"
  );
  let moonRingMaterial = new THREE.MeshBasicMaterial({
    map: moonRingTexture,
    transparent: true,
    blending: THREE.AdditiveBlending,
    side: THREE.DoubleSide,
    depthWrite: false,
    opacity: 0.5,
  });
  let moonRingGeometry = new THREE.RingGeometry(145, 155, 64);
  let moonRing = new THREE.Mesh(moonRingGeometry, moonRingMaterial);
  moonRing.rotation.x = -Math.PI / 2;
  scene.add(moonRing);

  let time = {
    value: 0
  };
  gsap.to(time, {
    value: 1,
    duration: 10,
    repeat: -1,
    ease: "linear",
    //月球每次旋轉位置更新位置
    onUpdate: () => {
      moon.position.x = 150 * Math.cos(time.value * Math.PI * 2);
      moon.position.z = 150 * Math.sin(time.value * Math.PI * 2);
      moon.rotation.y = time.value * Math.PI * 8;
    },
  });

  function render() {
    controls.update();
    requestAnimationFrame(render);
    renderer.render(scene, camera);
  }
  render();

  THREE.DefaultLoadingManager.onProgress = function (item, loaded, total) {
    console.log(item, loaded, total);
    progress.value = new Number((loaded / total) * 100).toFixed(2)
  }
})
</script>
<style>
* {
  margin: 0;
  padding: 0;
}
body {
  background-color: #000;
}
.canvas-container {
  width: 100vw;
  height: 100vh;
}
</style>
image.png
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末研铆,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子甜熔,更是在濱河造成了極大的恐慌,老刑警劉巖溶其,帶你破解...
    沈念sama閱讀 216,744評論 6 502
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件凝垛,死亡現(xiàn)場離奇詭異,居然都是意外死亡窗悯,警方通過查閱死者的電腦和手機区匣,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,505評論 3 392
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來蒋院,“玉大人沉颂,你說我怎么就攤上這事≡梦郏” “怎么了铸屉?”我有些...
    開封第一講書人閱讀 163,105評論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長切端。 經(jīng)常有香客問我彻坛,道長,這世上最難降的妖魔是什么踏枣? 我笑而不...
    開封第一講書人閱讀 58,242評論 1 292
  • 正文 為了忘掉前任昌屉,我火速辦了婚禮,結果婚禮上茵瀑,老公的妹妹穿的比我還像新娘间驮。我一直安慰自己,他們只是感情好马昨,可當我...
    茶點故事閱讀 67,269評論 6 389
  • 文/花漫 我一把揭開白布竞帽。 她就那樣靜靜地躺著,像睡著了一般鸿捧。 火紅的嫁衣襯著肌膚如雪屹篓。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,215評論 1 299
  • 那天匙奴,我揣著相機與錄音堆巧,去河邊找鬼。 笑死,一個胖子當著我的面吹牛谍肤,可吹牛的內(nèi)容都是我干的啦租。 我是一名探鬼主播,決...
    沈念sama閱讀 40,096評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼荒揣,長吁一口氣:“原來是場噩夢啊……” “哼刷钢!你這毒婦竟也來了?” 一聲冷哼從身側響起乳附,我...
    開封第一講書人閱讀 38,939評論 0 274
  • 序言:老撾萬榮一對情侶失蹤内地,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后赋除,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體阱缓,經(jīng)...
    沈念sama閱讀 45,354評論 1 311
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,573評論 2 333
  • 正文 我和宋清朗相戀三年举农,在試婚紗的時候發(fā)現(xiàn)自己被綠了荆针。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,745評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡颁糟,死狀恐怖航背,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情棱貌,我是刑警寧澤玖媚,帶...
    沈念sama閱讀 35,448評論 5 344
  • 正文 年R本政府宣布,位于F島的核電站婚脱,受9級特大地震影響今魔,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜障贸,卻給世界環(huán)境...
    茶點故事閱讀 41,048評論 3 327
  • 文/蒙蒙 一错森、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧篮洁,春花似錦涩维、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,683評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至锋叨,卻和暖如春垄分,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背娃磺。 一陣腳步聲響...
    開封第一講書人閱讀 32,838評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留叫倍,地道東北人偷卧。 一個月前我還...
    沈念sama閱讀 47,776評論 2 369
  • 正文 我出身青樓豺瘤,卻偏偏與公主長得像,于是被迫代替她去往敵國和親听诸。 傳聞我的和親對象是個殘疾皇子坐求,可洞房花燭夜當晚...
    茶點故事閱讀 44,652評論 2 354

推薦閱讀更多精彩內(nèi)容