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
| import React, { useRef, Suspense } from "react" import { Canvas, useFrame, useThree, extend, useLoader } from 'react-three-fiber' import * as THREE from 'three' import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls' import sunIMage from "./images/sun_bg.jpg" import bgImage from "./images/starry_sky_bg.jpg" extend({ OrbitControls }) const starLitesConfig = [ { name: "水星", starLiteSize: 2, starLiteRadius: 34, speed: 0.02, imgUrl: require("./images/mercury_bg.png") }, { name: "金星", starLiteSize: 3, starLiteRadius: 38, speed: 0.018, imgUrl: require("./images/venus_bg.png") }, { name: "地球", starLiteSize: 3.2, starLiteRadius: 42.2, speed: 0.016, imgUrl: require("./images/earth_bg.png") }, { name: "火星", starLiteSize: 2.5, starLiteRadius: 47.1, speed: 0.014, imgUrl: require("./images/spark_bg.png") }, { name: "木星", starLiteSize: 35, starLiteRadius: 71, speed: 0.012, imgUrl: require("./images/jupiter_bg.png") }, { name: "土星", starLiteSize: 45, starLiteRadius: 110, speed: 0.01, imgUrl: require("./images/saturn_bg.png") }, { name: "天王星", starLiteSize: 17, starLiteRadius: 158, speed: 0.008, imgUrl: require("./images/uranus_bg.png") }, { name: "海王星", starLiteSize: 15, starLiteRadius: 188, speed: 0.006, imgUrl: require("./images/neptune_bg.png") } ] const Controls = () => { const { camera, gl } = useThree() const ref = useRef() useFrame(() => ref.current.update()) return <orbitControls ref={ref} args={[camera, gl.domElement]} /> }
function generateSprite(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 Sun() { const [texture] = useLoader(THREE.TextureLoader, [sunIMage]) const sun = useRef() useFrame(() => sun.current.rotation.y -= 0.01) return ( <group ref={sun}> <mesh> <sphereGeometry args={[30, 30, 30]} /> <meshBasicMaterial map={texture} /> </mesh> <sprite scale={[90, 90, 90]}> <spriteMaterial map={new THREE.CanvasTexture(generateSprite('253,111,7'))} blending={THREE.AdditiveBlending} /> </sprite> </group> ) }
function initSatellite({ starLiteSize, starLiteRadius, speed, imgUrl, rotation = { x: -Math.PI * 0.42, y: Math.PI * 0.02, z: 0 } }) { const [texture] = useLoader(THREE.TextureLoader, [imgUrl]) const ref = useRef(); useFrame(() => ref.current.rotation.z -= speed) return ( <group ref={ref} rotation={[rotation.x, rotation.y, rotation.z]} > <mesh > <ringGeometry args={[starLiteRadius, starLiteRadius + 0.1, 50]}/> <meshBasicMaterial /> </mesh> <mesh > <sphereGeometry args={[1, 1, 1]} /> <meshLambertMaterial /> </mesh> <sprite scale={[starLiteSize, starLiteSize, starLiteSize]} position={[starLiteRadius, 0, 0]} > <spriteMaterial map={texture}/> </sprite> </group> ) }
function StarLites(params) { return( <> {starLitesConfig.map((item,index) => initSatellite(item))} </> ) } export default function Deom(params) { return( <Canvas tabIndex={"none"} camera={{ position: [0, 0,1000], fov: 20,near: 0.1,far: 2000 }} style={{ background: `url(${bgImage}) 50% 50%`, with: "100vw", height: "100vh", outline: "none"}} > <Controls /> <Suspense fallback={null}> <Sun /> <StarLites /> </Suspense> </Canvas> ) }
|