你的登錄界面不夠花里胡哨,3D 版本的來(lái)了
↓推薦關(guān)注↓
今天分享一篇用three.js 做的登錄網(wǎng)站,里面還用到了粒子特效,一個(gè)字就是酷炫??
前言:
該篇文章用到的主要技術(shù):vue3、three.js
我們先看看成品效果:

座機(jī)小圖預(yù)覽:

廢話不多說(shuō),直接進(jìn)入正題
Three.js的基礎(chǔ)知識(shí)
想象一下,在一個(gè)虛擬的3D世界中都需要什么?「首先,要有一個(gè)立體的空間,其次是有光源,最重要的是要有一雙眼睛」。下面我們就看看在three.js中如何創(chuàng)建一個(gè)3D世界吧!
創(chuàng)建一個(gè)場(chǎng)景 設(shè)置光源 創(chuàng)建相機(jī),設(shè)置相機(jī)位置和相機(jī)鏡頭的朝向 創(chuàng)建3D渲染器,使用渲染器把創(chuàng)建的場(chǎng)景渲染出來(lái)
此時(shí),你就通過(guò)three.js創(chuàng)建出了一個(gè)可視化的3D頁(yè)面,很簡(jiǎn)單是吧!
關(guān)于場(chǎng)景
你可以為場(chǎng)景添加背景顏色,或創(chuàng)建一個(gè)盒模型(球體、立方體),給盒模型的內(nèi)部貼上圖片,再把相機(jī)放在這個(gè)盒模型內(nèi)部以達(dá)到模擬場(chǎng)景的效果。盒模型的方式多用于360度全景,比如房屋vr展示
【登陸頁(yè)面】創(chuàng)建場(chǎng)景的例子:
const?scene?=?new?THREE.Scene()
//?在場(chǎng)景中添加霧的效果,F(xiàn)og參數(shù)分別代表‘霧的顏色’、‘開(kāi)始霧化的視線距離’、剛好霧化至看不見(jià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)
關(guān)于光源
為場(chǎng)景設(shè)置光源的顏色、強(qiáng)度,同時(shí)還可以設(shè)置光源的類型(環(huán)境光、點(diǎn)光源、平行光等)、光源所在的位置
【登陸頁(yè)面】創(chuàng)建光源的例子:
//?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)
關(guān)于相機(jī)(重要)
很重要的一步,相機(jī)就是你的眼睛。「這里還會(huì)著重說(shuō)明一下使用透視相機(jī)時(shí)可能會(huì)遇到的問(wèn)題」,我們最常用到的相機(jī)就是正交相機(jī)和透視相機(jī)了。
正交相機(jī):無(wú)論物體距離相機(jī)距離遠(yuǎn)或者近,在最終渲染的圖片中物體的大小都保持不變。「用于渲染2D場(chǎng)景或者UI元素是非常有用的」。如圖:
「圖注解:」
圖中紅色三角錐體是視野的大小 紅色錐體連著的第一個(gè)面是攝像機(jī)能看到的最近位置 從該面通過(guò)白色輔助線延伸過(guò)去的面是攝像機(jī)能看到的最遠(yuǎn)的位置

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

我們?cè)谑褂猛敢曄鄼C(jī)時(shí),可能會(huì)遇到這種情況:邊緣處的物體會(huì)產(chǎn)生一定程度上的形變,原因是:「透視相機(jī)是魚(yú)眼效果,如果視域越大,邊緣變形越大。為了避免邊緣變形,可以將fov角度設(shè)置小一些,距離拉遠(yuǎn)一些」
關(guān)于透視相機(jī)的幾個(gè)參數(shù),new THREE.PerspectiveCamera(fov, width / height, near, far)
fov(field of view) — 攝像機(jī)視錐體垂直視野角度 aspect(width / height) — 攝像機(jī)視錐體長(zhǎng)寬比 near — 攝像機(jī)視錐體近端面 far — 攝像機(jī)視錐體遠(yuǎn)端面
/**
?*?為了避免邊緣變形,這里將fov角度設(shè)置小一些,距離拉遠(yuǎn)一些
?*?固定視域角度,求需要多少距離才能滿足完整的視野畫(huà)面
?*?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)
關(guān)于渲染器
用WebGL渲染出你精心制作的場(chǎng)景。它會(huì)創(chuàng)建一個(gè)canvas進(jìn)行渲染
【登陸頁(yè)面】創(chuàng)建渲染器的例子:
//?獲取容器dom
const?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)
需要注意,這樣創(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()
完善效果
創(chuàng)建一個(gè)左上角的地球
//?加載紋理
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?=?-400
const?Sphere_Group.position.y?=?200
const?Sphere_Group.position.z?=?-200
//?加入場(chǎng)景
scene.add(Sphere_Group)
//?使球能夠自轉(zhuǎn),需要在loopAnimate中加上
Sphere_Group.rotateY(0.001)
使地球自轉(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)
}
創(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???????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)
使星星運(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???????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)))
????}
}
星星的運(yùn)動(dòng)效果,實(shí)際就是沿著z軸從遠(yuǎn)處不斷朝著相機(jī)位置移動(dòng),直到移出相機(jī)的位置時(shí)回到起點(diǎn),不斷重復(fù)這個(gè)操作。我們使用上帝視角,從x軸的左側(cè)看去,效果如下:

創(chuàng)建云以及運(yùn)動(dò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)建好的路徑加入場(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)
現(xiàn)在有了云和曲線路徑,我們需要將二者結(jié)合,讓云按著路徑進(jìn)行運(yùn)動(dòng)
使云運(yùn)動(dòng)
let?cloudProgress?=?0
let?scaleSpeed?=?0.0006
let?maxScale?=?1
let?startScale?=?0
//?初始化云的運(yùn)動(dòng)函數(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)
??????}
????}
??}
}
完成three.js有關(guān)效果
最后,把cloudMove函數(shù)放入loopAnimate函數(shù)中即可實(shí)現(xiàn)云的運(yùn)動(dòng)。至此,該登錄頁(yè)所有與three.js有關(guān)的部分都介紹完了。剩下的月球地面、登錄框、宇航員都是通過(guò)定位和層級(jí)設(shè)置以及css3動(dòng)畫(huà)實(shí)現(xiàn)的,這里就不進(jìn)行深入的討論了。
上面的每個(gè)部分的代碼在連貫性并不完整,并且同登錄頁(yè)的完整代碼也有些許出入。上面更多是為了介紹每個(gè)部分的實(shí)現(xiàn)方式。完整代碼,我放在github上了,每行注釋幾乎都打上了,希望能給你入坑three.js帶來(lái)一些幫助,地址:github.com/Yanzengyong…
結(jié)語(yǔ)
最后,我認(rèn)為3D可視化的精髓其實(shí)在于設(shè)計(jì),有好的素材、好的建模,能讓你的頁(yè)面效果瞬間提升N倍
作者:KDDA_
鏈接:https://juejin.cn/post/7020571868314730532
最近面試BAT,整理一份面試資料《前端面試BAT通關(guān)手冊(cè)》,覆蓋了前端技術(shù)、CSS、JavaScript、框架、?數(shù)據(jù)庫(kù)、數(shù)據(jù)結(jié)構(gòu)等等。 獲取方式:關(guān)注公眾號(hào)并回復(fù)?前端?領(lǐng)取,更多內(nèi)容陸續(xù)奉上。 明天見(jiàn)(??ω??)??
