時(shí)間:2023-08-29 03:30:01 | 來(lái)源:網(wǎng)站運(yùn)營(yíng)
時(shí)間:2023-08-29 03:30:01 來(lái)源:網(wǎng)站運(yùn)營(yíng)
教你用three.js寫(xiě)一個(gè)炫酷的3D登陸頁(yè)面,純干貨?。涸撈恼掠玫降闹饕夹g(shù):vue3、three.jsconst scene = new THREE.Scene()// 在場(chǎng)景中添加霧的效果,F(xiàn)og參數(shù)分別代表‘霧的顏色’、‘開(kāi)始霧化的視線(xiàn)距離’、剛好霧化至看不見(jiàn)的視線(xiàn)距離’scene.fog = new THREE.Fog(0x000000, 0, 10000)// 盒模型的深度const depth = 1400// 在場(chǎng)景中添加一個(gè)圓球盒模型// 1.創(chuàng)建一個(gè)立方體const geometry = new THREE.BoxGeometry(1000, 800, depth)// 2.加載紋理const texture = new THREE.TextureLoader().load('bg.png')// 3.創(chuàng)建網(wǎng)格材質(zhì)(原料)const material = new THREE.MeshBasicMaterial({map: texture, side: THREE.BackSide})// 4.生成網(wǎng)格const mesh = new THREE.Mesh(geometry, material)// 5.把網(wǎng)格放入場(chǎng)景中scene.add(mesh)復(fù)制代碼
// 1.創(chuàng)建環(huán)境光const ambientLight = new THREE.AmbientLight(0xffffff, 1)// 2.創(chuàng)建點(diǎn)光源,位于場(chǎng)景右下角const light_rightBottom = new THREE.PointLight(0x0655fd, 5, 0)light_rightBottom.position.set(0, 100, -200)// 3.把光源放入場(chǎng)景中scene.add(light_rightBottom)scene.add(ambientLight)復(fù)制代碼
/** * 為了避免邊緣變形,這里將fov角度設(shè)置小一些,距離拉遠(yuǎn)一些 * 固定視域角度,求需要多少距離才能滿(mǎn)足完整的視野畫(huà)面 * 15度等于(Math.PI / 12) */const container = document.getElementById('login-three-container')const width = container.clientWidthconst height = container.clientHeightconst fov = 15const distance = width / 2 / Math.tan(Math.PI / 12)const zAxisNumber = Math.floor(distance - depth / 2)const camera = new THREE.PerspectiveCamera(fov, width / height, 1, 30000)camera.position.set(0, 0, zAxisNumber)const cameraTarget = new THREE.Vector3(0, 0, 0)camera.lookAt(cameraTarget)復(fù)制代碼
// 獲取容器domconst container = document.getElementById('login-three-container')// 創(chuàng)建webgl渲染器實(shí)例const renderer = new THREE.WebGLRenderer({ antialias: true, alpha: true })// 設(shè)置渲染器畫(huà)布的大小renderer.setSize(width, height)// 把畫(huà)布實(shí)例(canvas)放入容器中container.appendChild(renderer.domElement)// 渲染器渲染場(chǎng)景renderer.render(scene, camera)復(fù)制代碼
需要注意,這樣創(chuàng)建出來(lái)的場(chǎng)景并沒(méi)有動(dòng)效,原因是這次渲染的僅僅只是這一幀的畫(huà)面。為了讓場(chǎng)景中的物體能動(dòng)起來(lái),我們需要使用requestAnimationFrame,所以我們可以寫(xiě)一個(gè)loop函數(shù)//動(dòng)畫(huà)刷新const loopAnimate = () => { requestAnimationFrame(loopAnimate) scene.rotateY(0.001) renderer.render(scene, camera)}loopAnimate()復(fù)制代碼
// 加載紋理const texture = THREE.TextureLoader().load('earth_bg.png')// 創(chuàng)建網(wǎng)格材質(zhì)const material = new THREE.MeshPhongMaterial({map: texture, blendDstAlpha: 1})// 創(chuàng)建幾何球體const sphereGeometry = new THREE.SphereGeometry(50, 64, 32)// 生成網(wǎng)格const sphere = new THREE.Mesh(sphereGeometry, material)// 為了單獨(dú)操作球體的運(yùn)動(dòng)效果,我們把球體放到一個(gè)組中const Sphere_Group = new THREE.Group()const Sphere_Group.add(sphere)// 設(shè)置該組(球體)在空間坐標(biāo)中的位置const Sphere_Group.position.x = -400const Sphere_Group.position.y = 200const Sphere_Group.position.z = -200// 加入場(chǎng)景scene.add(Sphere_Group)// 使球能夠自轉(zhuǎn),需要在loopAnimate中加上Sphere_Group.rotateY(0.001)復(fù)制代碼
使地球自轉(zhuǎn)// 渲染星球的自轉(zhuǎn)const renderSphereRotate = () => { if (sphere) { Sphere_Group.rotateY(0.001) }}// 使球能夠自轉(zhuǎn),需要在loopAnimate中加上const loopAnimate = () => { requestAnimationFrame(loopAnimate) renderSphereRotate() renderer.render(scene, camera)}復(fù)制代碼
創(chuàng)建星星// 初始化星星const initSceneStar = (initZposition: number): any => { const geometry = new THREE.BufferGeometry() const vertices: number[] = [] const pointsGeometry: any[] = [] const textureLoader = new THREE.TextureLoader() const sprite1 = textureLoader.load('starflake1.png') const sprite2 = textureLoader.load('starflake2.png') parameters = [ [[0.6, 100, 0.75], sprite1, 50], [[0, 0, 1], sprite2, 20] ] // 初始化500個(gè)節(jié)點(diǎn) for (let i = 0; i < 500; i++) { /** * const x: number = Math.random() * 2 * width - width * 等價(jià) * THREE.MathUtils.randFloatSpread(width) * _.random使用的是lodash庫(kù)中的生成隨機(jī)數(shù) */ const x: number = THREE.MathUtils.randFloatSpread(width) const y: number = _.random(0, height / 2) const z: number = _.random(-depth / 2, zAxisNumber) vertices.push(x, y, z) } geometry.setAttribute('position', new THREE.Float32BufferAttribute(vertices, 3)) // 創(chuàng)建2種不同的材質(zhì)的節(jié)點(diǎn)(500 * 2) for (let i = 0; i < parameters.length; i++) { const color = parameters[i][0] const sprite = parameters[i][1] const size = parameters[i][2] materials[i] = new THREE.PointsMaterial({ size, map: sprite, blending: THREE.AdditiveBlending, depthTest: true, transparent: true }) materials[i].color.setHSL(color[0], color[1], color[2]) const particles = new THREE.Points(geometry, materials[i]) particles.rotation.x = Math.random() * 0.2 - 0.15 particles.rotation.z = Math.random() * 0.2 - 0.15 particles.rotation.y = Math.random() * 0.2 - 0.15 particles.position.setZ(initZposition) pointsGeometry.push(particles) scene.add(particles) } return pointsGeometry}const particles_init_position = -zAxisNumber - depth / 2let zprogress = particles_init_positionlet zprogress_second = particles_init_position * 2const particles_first = initSceneStar(particles_init_position)const particles_second = initSceneStar(zprogress_second)復(fù)制代碼
使星星運(yùn)動(dòng)// 渲染星星的運(yùn)動(dòng)const renderStarMove = () => { const time = Date.now() * 0.00005 zprogress += 1 zprogress_second += 1 if (zprogress >= zAxisNumber + depth / 2) { zprogress = particles_init_position } else { particles_first.forEach((item) => { item.position.setZ(zprogress) }) } if (zprogress_second >= zAxisNumber + depth / 2) { zprogress_second = particles_init_position } else { particles_second.forEach((item) => { item.position.setZ(zprogress_second) }) } for (let i = 0; i < materials.length; i++) { const color = parameters[i][0] const h = ((360 * (color[0] + time)) % 360) / 360 materials[i].color.setHSL(color[0], color[1], parseFloat(h.toFixed(2))) }}復(fù)制代碼
星星的運(yùn)動(dòng)效果,實(shí)際就是沿著z軸從遠(yuǎn)處不斷朝著相機(jī)位置移動(dòng),直到移出相機(jī)的位置時(shí)回到起點(diǎn),不斷重復(fù)這個(gè)操作。我們使用上帝視角,從x軸的左側(cè)看去。// 創(chuàng)建曲線(xiàn)路徑const route = [ new THREE.Vector3(-width / 10, 0, -depth / 2), new THREE.Vector3(-width / 4, height / 8, 0), new THREE.Vector3(-width / 4, 0, zAxisNumber)]const curve = new THREE.CatmullRomCurve3(route, false)const tubeGeometry = new THREE.TubeGeometry(curve, 100, 2, 50, false)const tubeMaterial = new THREE.MeshBasicMaterial({ opacity: 0, transparent: true})const tube = new THREE.Mesh(tubeGeometry, tubeMaterial)// 把創(chuàng)建好的路徑加入場(chǎng)景中scene.add(tube)// 創(chuàng)建平面幾何const clondGeometry = new THREE.PlaneGeometry(500, 200)const textureLoader = new THREE.TextureLoader()const cloudTexture = textureLoader.load('cloud.png')const clondMaterial = new THREE.MeshBasicMaterial({ map: cloudTexture, blending: THREE.AdditiveBlending, depthTest: false, transparent: true})const cloud = new THREE.Mesh(clondGeometry, clondMaterial)// 將云加入場(chǎng)景中scene.add(cloud)復(fù)制代碼
現(xiàn)在有了云和曲線(xiàn)路徑,我們需要將二者結(jié)合,讓云按著路徑進(jìn)行運(yùn)動(dòng)let cloudProgress = 0let scaleSpeed = 0.0006let maxScale = 1let startScale = 0// 初始化云的運(yùn)動(dòng)函數(shù)const cloudMove = () => { if (startScale < maxScale) { startScale += scaleSpeed cloud.scale.setScalar(startScale) } if (cloudProgress > 1) { cloudProgress = 0 startScale = 0 } else { cloudProgress += speed if (cloudParameter.curve) { const point = curve.getPoint(cloudProgress) if (point && point.x) { cloud.position.set(point.x, point.y, point.z) } } }}復(fù)制代碼
完成three.js有關(guān)效果關(guān)鍵詞:登陸,干貨
客戶(hù)&案例
營(yíng)銷(xiāo)資訊
關(guān)于我們
客戶(hù)&案例
營(yíng)銷(xiāo)資訊
關(guān)于我們
微信公眾號(hào)
版權(quán)所有? 億企邦 1997-2025 保留一切法律許可權(quán)利。