<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          教你用 three.js 寫一個炫酷的3D登陸頁面

          共 5698字,需瀏覽 12分鐘

           ·

          2021-11-01 15:01

          點擊上方?前端瓶子君,關(guān)注公眾號

          回復(fù)算法,加入前端編程面試算法每日一題群

          前言:

          大家好,我是xx傳媒嚴(yán)導(dǎo)(xx這兩個字請自行腦補) 。

          該篇文章用到的主要技術(shù):vue3、three.js

          我們先看看成品效果:

          高清大圖預(yù)覽(會有些慢):

          廢話不多說,直接進(jìn)入正題

          Three.js的基礎(chǔ)知識

          想象一下,在一個虛擬的3D世界中都需要什么?首先,要有一個立體的空間,其次是有光源,最重要的是要有一雙眼睛。下面我們就看看在three.js中如何創(chuàng)建一個3D世界吧!

          1. 創(chuàng)建一個場景
          2. 設(shè)置光源
          3. 創(chuàng)建相機(jī),設(shè)置相機(jī)位置和相機(jī)鏡頭的朝向
          4. 創(chuàng)建3D渲染器,使用渲染器把創(chuàng)建的場景渲染出來

          此時,你就通過three.js創(chuàng)建出了一個可視化的3D頁面,很簡單是吧!

          關(guān)于場景

          你可以為場景添加背景顏色,或創(chuàng)建一個盒模型(球體、立方體),給盒模型的內(nèi)部貼上圖片,再把相機(jī)放在這個盒模型內(nèi)部以達(dá)到模擬場景的效果。盒模型的方式多用于360度全景,比如房屋vr展示

          【登陸頁面】創(chuàng)建場景的例子:

          const?scene?=?new?THREE.Scene()
          //?在場景中添加霧的效果,F(xiàn)og參數(shù)分別代表‘霧的顏色’、‘開始霧化的視線距離’、剛好霧化至看不見的視線距離’
          scene.fog?=?new?THREE.Fog(0x000000,?0,?10000)
          //?盒模型的深度
          const?depth?=?1400
          //?在場景中添加一個圓球盒模型
          //?1.創(chuàng)建一個立方體
          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)格放入場景中
          scene.add(mesh)
          復(fù)制代碼

          關(guān)于光源

          為場景設(shè)置光源的顏色、強(qiáng)度,同時還可以設(shè)置光源的類型(環(huán)境光、點光源、平行光等)、光源所在的位置

          【登陸頁面】創(chuàng)建光源的例子:

          //?1.創(chuàng)建環(huán)境光
          const?ambientLight?=?new?THREE.AmbientLight(0xffffff,?1)
          //?2.創(chuàng)建點光源,位于場景右下角
          const?light_rightBottom?=?new?THREE.PointLight(0x0655fd,?5,?0)
          light_rightBottom.position.set(0,?100,?-200)
          //?3.把光源放入場景中
          scene.add(light_rightBottom)
          scene.add(ambientLight)
          復(fù)制代碼

          關(guān)于相機(jī)(重要)

          很重要的一步,相機(jī)就是你的眼睛。這里還會著重說明一下使用透視相機(jī)時可能會遇到的問題,我們最常用到的相機(jī)就是正交相機(jī)和透視相機(jī)了。

          正交相機(jī):無論物體距離相機(jī)距離遠(yuǎn)或者近,在最終渲染的圖片中物體的大小都保持不變。用于渲染2D場景或者UI元素是非常有用的。如圖:

          圖注解:

          1. 圖中紅色三角錐體是視野的大小
          2. 紅色錐體連著的第一個面是攝像機(jī)能看到的最近位置
          3. 從該面通過白色輔助線延伸過去的面是攝像機(jī)能看到的最遠(yuǎn)的位置

          透視相機(jī):被用來模擬人眼所看到的景象。它是3D場景的渲染中使用得最普遍的投影模式。如圖:

          我們在使用透視相機(jī)時,可能會遇到這種情況:邊緣處的物體會產(chǎn)生一定程度上的形變,原因是:透視相機(jī)是魚眼效果,如果視域越大,邊緣變形越大。為了避免邊緣變形,可以將fov角度設(shè)置小一些,距離拉遠(yuǎn)一些

          關(guān)于透視相機(jī)的幾個參數(shù),new THREE.PerspectiveCamera(fov, width / height, near, far)

          • fov(field of view) — 攝像機(jī)視錐體垂直視野角度
          • aspect(width / height) — 攝像機(jī)視錐體長寬比
          • near — 攝像機(jī)視錐體近端面
          • far — 攝像機(jī)視錐體遠(yuǎn)端面
          /**
          ?*?為了避免邊緣變形,這里將fov角度設(shè)置小一些,距離拉遠(yuǎn)一些
          ?*?固定視域角度,求需要多少距離才能滿足完整的視野畫面
          ?*?15度等于(Math.PI?/?12)
          ?*/

          const?container?=?document.getElementById('login-three-container')
          const?width?=?container.clientWidth
          const?height?=?container.clientHeight
          const?fov?=?15
          const?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ù)制代碼

          關(guān)于渲染器

          WebGL[1]渲染出你精心制作的場景。它會創(chuàng)建一個canvas進(jìn)行渲染

          【登陸頁面】創(chuàng)建渲染器的例子:

          //?獲取容器dom
          const?container?=?document.getElementById('login-three-container')
          //?創(chuàng)建webgl渲染器實例
          const?renderer?=?new?THREE.WebGLRenderer({?antialias:?true,?alpha:?true?})
          //?設(shè)置渲染器畫布的大小
          renderer.setSize(width,?height)
          //?把畫布實例(canvas)放入容器中
          container.appendChild(renderer.domElement)
          //?渲染器渲染場景
          renderer.render(scene,?camera)
          復(fù)制代碼

          需要注意,這樣創(chuàng)建出來的場景并沒有動效,原因是這次渲染的僅僅只是這一幀的畫面。為了讓場景中的物體能動起來,我們需要使用requestAnimationFrame,所以我們可以寫一個loop函數(shù)

          //動畫刷新
          const?loopAnimate?=?()?=>?{
          ????requestAnimationFrame(loopAnimate)
          ????scene.rotateY(0.001)
          ????renderer.render(scene,?camera)
          }
          loopAnimate()
          復(fù)制代碼

          完善效果

          創(chuàng)建一個左上角的地球

          //?加載紋理
          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)
          //?為了單獨操作球體的運動效果,我們把球體放到一個組中
          const?Sphere_Group?=?new?THREE.Group()
          const?Sphere_Group.add(sphere)
          //?設(shè)置該組(球體)在空間坐標(biāo)中的位置
          const?Sphere_Group.position.x?=?-400
          const?Sphere_Group.position.y?=?200
          const?Sphere_Group.position.z?=?-200
          //?加入場景
          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個節(jié)點
          ????for?(let?i?=?0;?i?500;?i++)?{
          ??????/**
          ???????*?const?x:?number?=?Math.random()?*?2?*?width?-?width
          ???????*?等價
          ???????*?THREE.MathUtils.randFloatSpread(width)
          ???????*?_.random使用的是lodash庫中的生成隨機(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é)點(500?*?2)
          ????for?(let?i?=?0;?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?/?2
          let?zprogress?=?particles_init_position
          let?zprogress_second?=?particles_init_position?*?2
          const?particles_first?=?initSceneStar(particles_init_position)
          const?particles_second?=?initSceneStar(zprogress_second)
          復(fù)制代碼

          使星星運動

          //?渲染星星的運動
          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???????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ù)制代碼

          星星的運動效果,實際就是沿著z軸從遠(yuǎn)處不斷朝著相機(jī)位置移動,直到移出相機(jī)的位置時回到起點,不斷重復(fù)這個操作。我們使用上帝視角,從x軸的左側(cè)看去。

          創(chuàng)建云以及運動軌跡

          //?創(chuàng)建曲線路徑
          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)建好的路徑加入場景中
          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)
          //?將云加入場景中
          scene.add(cloud)
          復(fù)制代碼

          現(xiàn)在有了云和曲線路徑,我們需要將二者結(jié)合,讓云按著路徑進(jìn)行運動

          使云運動

          let?cloudProgress?=?0
          let?scaleSpeed?=?0.0006
          let?maxScale?=?1
          let?startScale?=?0
          //?初始化云的運動函數(shù)
          const?cloudMove?=?()?=>?{
          ??if?(startScale?????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)效果

          最后,把cloudMove函數(shù)放入loopAnimate函數(shù)中即可實現(xiàn)云的運動。至此,該登錄頁所有與three.js有關(guān)的部分都介紹完了。剩下的月球地面、登錄框、宇航員都是通過定位和層級設(shè)置以及css3動畫實現(xiàn)的,這里就不進(jìn)行深入的討論了。

          上面的每個部分的代碼在連貫性并不完整,并且同登錄頁的完整代碼也有些許出入。上面更多是為了介紹每個部分的實現(xiàn)方式。完整代碼,我放在github上了,每行注釋幾乎都打上了,希望能給你入坑three.js帶來一些幫助,地址:https://github.com/Yanzengyong/threejs-login-view

          結(jié)語

          之前用react+three.js寫過一個3D可視化的知識圖譜,如果這篇對大家有幫助并且反響好的話,后續(xù)我會寫一篇如何使用three.js創(chuàng)建一個3D知識圖譜。

          最后,我認(rèn)為3D可視化的精髓其實在于設(shè)計,有好的素材、好的建模,能讓你的頁面效果瞬間提升N倍

          three.js官網(wǎng)[3]


          關(guān)于本文

          來源:KDDA_

          https://juejin.cn/post/7020571868314730532


          最后

          歡迎關(guān)注【前端瓶子君】??ヽ(°▽°)ノ?
          回復(fù)「算法」,加入前端編程源碼算法群,每日一道面試題(工作日),第二天瓶子君都會很認(rèn)真的解答喲!
          回復(fù)「交流」,吹吹水、聊聊技術(shù)、吐吐槽!
          回復(fù)「閱讀」,每日刷刷高質(zhì)量好文!
          如果這篇文章對你有幫助,在看」是最大的支持
          ?》》面試官也在看的算法資料《《
          “在看和轉(zhuǎn)發(fā)”就是最大的支持


          瀏覽 99
          點贊
          評論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  五月天人妻 | 99精品免费观看 | 日韩国产在线观看 | HEZ-502搭讪绝品人妻系列 | 麻豆av影院日韩影院 |