three.js实现地球 外部扫描的着色器

three.js实现地球 外部扫描的着色器

https://threehub.cn/#/codeMirror?navigation=ThreeJS&classify=shader&id=earthScan

bash 复制代码
import * as THREE from 'three'
import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls.js'
import { GUI } from 'three/examples/jsm/libs/lil-gui.module.min.js'

const box = document.getElementById('box')

const scene = new THREE.Scene()

const camera = new THREE.PerspectiveCamera(50, box.clientWidth / box.clientHeight, 0.1, 1000)

camera.position.set(0, 8, 8)

const renderer = new THREE.WebGLRenderer({ antialias: true, alpha: true, logarithmicDepthBuffer: true })

renderer.setSize(box.clientWidth, box.clientHeight)

box.appendChild(renderer.domElement)

const controls = new OrbitControls(camera, renderer.domElement)

controls.enableDamping = true

window.onresize = () => {

  renderer.setSize(box.clientWidth, box.clientHeight)

  camera.aspect = box.clientWidth / box.clientHeight

  camera.updateProjectionMatrix()

}

const earthGeometry = new THREE.SphereGeometry(2.5, 32, 16)

const earthMaterial = new THREE.MeshBasicMaterial({ map: new THREE.TextureLoader().load(`https://file.threehub.cn/` + 'threeExamples/shader/earth1.jpg') })

const earth = new THREE.Mesh(earthGeometry, earthMaterial)

scene.add(earth)

const geometry = new THREE.SphereGeometry(3, 32, 16)

const material = new THREE.ShaderMaterial({

  uniforms: {

    iTime: { value: 0.0 },

    pointNum: { value: new THREE.Vector2(64, 32) },

    uColor: { value: new THREE.Color('#bbd9ec') }

  },

  transparent: true,

  vertexShader: `
    varying vec2 vUv;
    void main(){
    vUv=uv;
    gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
  }`,

  fragmentShader: `
    float PI = acos(-1.0);
    uniform vec3 uColor;
    uniform vec2 pointNum;
    uniform float iTime;                        
    varying vec2 vUv;
    void main(){
    vec2 uv = vUv+ vec2(0.0, iTime);
      float current = abs(sin(uv.y * PI) );             
    if(current < 0.99) {      
      current=current*0.5;
    }
    float d = distance(fract(uv * pointNum), vec2(0.5, 0.5));
    if(d > current*0.2 ) {
       discard;
    } else {
       gl_FragColor =vec4(uColor,current);
    }
  }`

})

const folder = new GUI()

folder.addColor(material.uniforms.uColor, 'value')

folder.add(material.uniforms.pointNum.value, 'x', 1, 128).name('pointNumX')

folder.add(material.uniforms.pointNum.value, 'y', 1, 128).name('pointNumY')

const sphere = new THREE.Mesh(geometry, material)

scene.add(sphere)

animate()

function animate() {

  earth.rotation.y += 0.002

  material.uniforms.iTime.value += 0.002

  requestAnimationFrame(animate)

  controls.update()

  renderer.render(scene, camera)

}

/**
 * 名称: 地球扫描
 * 作者: 优雅永不过时 https://github.com/z2586300277
*/
相关推荐
万少4 分钟前
记第一次鸿蒙应用上架之旅:一场略带遗憾的旅途
前端·harmonyos
鹏多多6 分钟前
H5开发避坑!解决Safari浏览器的video会覆盖z-index:1的绝对定位元素
前端·javascript·vue.js
恋猫de小郭14 分钟前
来了解一下,为什么你的 Flutter WebView 在 iOS 26 上有点击问题?
android·前端·flutter
charlie11451419117 分钟前
CSS学习笔记5:CSS 盒模型 & Margin 注意事项
前端·css·笔记·学习·教程
CodeSheep17 分钟前
稚晖君公司的最新工资和招人标准
前端·后端·程序员
亿元程序员22 分钟前
今天我去面试游戏开发,说我回答得不全面...
前端
一只小阿乐35 分钟前
vue3封装alert 提示组件 仿element-plus
前端·javascript·vue.js·vue3
IT_陈寒1 小时前
SpringBoot实战避坑指南:我在微服务项目中总结的12条高效开发经验
前端·人工智能·后端
华洛1 小时前
解读麦肯锡报告:Agent落地的六大经验教训
前端·javascript·产品经理
艾小码1 小时前
还在重复造轮子?掌握这7个原则,让你的Vue组件复用性飙升!
前端·javascript·vue.js