时间:2023-08-29 03:30:01 | 来源:网站运营
时间:2023-08-29 03:30:01 来源:网站运营
教你用three.js写一个炫酷的3D登陆页面,纯干货!:该篇文章用到的主要技术:vue3、three.jsconst scene = new THREE.Scene()// 在场景中添加雾的效果,Fog参数分别代表‘雾的颜色’、‘开始雾化的视线距离’、刚好雾化至看不见的视线距离’scene.fog = new THREE.Fog(0x000000, 0, 10000)// 盒模型的深度const depth = 1400// 在场景中添加一个圆球盒模型// 1.创建一个立方体const geometry = new THREE.BoxGeometry(1000, 800, depth)// 2.加载纹理const texture = new THREE.TextureLoader().load('bg.png')// 3.创建网格材质(原料)const material = new THREE.MeshBasicMaterial({map: texture, side: THREE.BackSide})// 4.生成网格const mesh = new THREE.Mesh(geometry, material)// 5.把网格放入场景中scene.add(mesh)复制代码
// 1.创建环境光const ambientLight = new THREE.AmbientLight(0xffffff, 1)// 2.创建点光源,位于场景右下角const light_rightBottom = new THREE.PointLight(0x0655fd, 5, 0)light_rightBottom.position.set(0, 100, -200)// 3.把光源放入场景中scene.add(light_rightBottom)scene.add(ambientLight)复制代码
/** * 为了避免边缘变形,这里将fov角度设置小一些,距离拉远一些 * 固定视域角度,求需要多少距离才能满足完整的视野画面 * 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)复制代码
// 获取容器domconst container = document.getElementById('login-three-container')// 创建webgl渲染器实例const renderer = new THREE.WebGLRenderer({ antialias: true, alpha: true })// 设置渲染器画布的大小renderer.setSize(width, height)// 把画布实例(canvas)放入容器中container.appendChild(renderer.domElement)// 渲染器渲染场景renderer.render(scene, camera)复制代码
需要注意,这样创建出来的场景并没有动效,原因是这次渲染的仅仅只是这一帧的画面。为了让场景中的物体能动起来,我们需要使用requestAnimationFrame,所以我们可以写一个loop函数//动画刷新const loopAnimate = () => { requestAnimationFrame(loopAnimate) scene.rotateY(0.001) renderer.render(scene, camera)}loopAnimate()复制代码
// 加载纹理const texture = THREE.TextureLoader().load('earth_bg.png')// 创建网格材质const material = new THREE.MeshPhongMaterial({map: texture, blendDstAlpha: 1})// 创建几何球体const sphereGeometry = new THREE.SphereGeometry(50, 64, 32)// 生成网格const sphere = new THREE.Mesh(sphereGeometry, material)// 为了单独操作球体的运动效果,我们把球体放到一个组中const Sphere_Group = new THREE.Group()const Sphere_Group.add(sphere)// 设置该组(球体)在空间坐标中的位置const Sphere_Group.position.x = -400const Sphere_Group.position.y = 200const Sphere_Group.position.z = -200// 加入场景scene.add(Sphere_Group)// 使球能够自转,需要在loopAnimate中加上Sphere_Group.rotateY(0.001)复制代码
使地球自转// 渲染星球的自转const renderSphereRotate = () => { if (sphere) { Sphere_Group.rotateY(0.001) }}// 使球能够自转,需要在loopAnimate中加上const loopAnimate = () => { requestAnimationFrame(loopAnimate) renderSphereRotate() renderer.render(scene, camera)}复制代码
创建星星// 初始化星星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个节点 for (let i = 0; i < 500; i++) { /** * const x: number = Math.random() * 2 * width - width * 等价 * THREE.MathUtils.randFloatSpread(width) * _.random使用的是lodash库中的生成随机数 */ 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)) // 创建2种不同的材质的节点(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)复制代码
使星星运动// 渲染星星的运动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))) }}复制代码
星星的运动效果,实际就是沿着z轴从远处不断朝着相机位置移动,直到移出相机的位置时回到起点,不断重复这个操作。我们使用上帝视角,从x轴的左侧看去。// 创建曲线路径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)// 把创建好的路径加入场景中scene.add(tube)// 创建平面几何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)复制代码
现在有了云和曲线路径,我们需要将二者结合,让云按着路径进行运动let cloudProgress = 0let scaleSpeed = 0.0006let maxScale = 1let startScale = 0// 初始化云的运动函数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) } } }}复制代码
完成three.js有关效果关键词:登陆,干货