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

Three.js的基礎(chǔ)知識(shí)想象一下,在一個(gè)虛擬的3D世界中都需要什么?首先,要有一個(gè)立體的空間,其次是有光源,最重要" />

国产成人精品无码青草_亚洲国产美女精品久久久久∴_欧美人与鲁交大毛片免费_国产果冻豆传媒麻婆精东

所在位置: 首頁(yè) > 營(yíng)銷(xiāo)資訊 > 網(wǎng)站運(yùn)營(yíng) > 教你用three.js寫(xiě)一個(gè)炫酷的3D登陸頁(yè)面,純干貨!

教你用three.js寫(xiě)一個(gè)炫酷的3D登陸頁(yè)面,純干貨!

時(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.js

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

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

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

1.創(chuàng)建一個(gè)場(chǎng)景

2.設(shè)置光源

3.創(chuàng)建相機(jī),設(shè)置相機(jī)位置和相機(jī)鏡頭的朝向

4.創(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)始霧化的視線(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ù)制代碼

關(guān)于光源

為場(chǎng)景設(shè)置光源的顏色、強(qiáng)度,同時(shí)還可以設(shè)置光源的類(lèi)型(環(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)復(fù)制代碼

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

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

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

圖注解:

1.圖中紅色三角錐體是視野的大小

2.紅色錐體連著的第一個(gè)面是攝像機(jī)能看到的最近位置

3.從該面通過(guò)白色輔助線(xiàn)延伸過(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角度設(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ù)制代碼

關(guān)于渲染器

用WebGL渲染出你精心制作的場(chǎng)景。它會(huì)創(chuàng)建一個(gè)canvas進(jìn)行渲染

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

// 獲取容器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ù)制代碼

完善效果

創(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 = -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)建云以及運(yùn)動(dòng)軌跡

// 創(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)

使云運(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)效果

最后,把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)一些幫助,地址:https://github.com/Yanzengyong/threejs-login-view

結(jié)語(yǔ)

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

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

END!想要學(xué)習(xí)的小伙伴,建議收藏,如果對(duì)您的學(xué)習(xí)有所幫助,記得關(guān)注+點(diǎn)贊吧!

關(guān)鍵詞:登陸,干貨

74
73
25
news

版權(quán)所有? 億企邦 1997-2025 保留一切法律許可權(quán)利。

為了最佳展示效果,本站不支持IE9及以下版本的瀏覽器,建議您使用谷歌Chrome瀏覽器。 點(diǎn)擊下載Chrome瀏覽器
關(guān)閉