THREEJS辉光与景深特效

效果预览

辉光效果:

景深效果:

核心代码

辉光和景深尽量把场景背景设置为黑色,要不影响效果。

// 设置场景背景色为黑色
scene.background = new THREE.Color(0, 0, 0)

// 创建渲染通道
const renderPass= new RenderPass(scene, camera)

// 创建辉光bloom通道, 强度1.5,半径0,阈值0
const bloomPass = new UnrealBloomPass(new THREE.Vector2(window.innerWidth, window.innerHeight), 1.5, 0, 0)

// 创建景深参数
const params = {
   focus: 0, // 聚焦
   aspect: ts.camera.aspect,
   aperture: 0.000005, // 孔径
   maxblur: 1, 
   width: window.innerWidth,
   height: window.innerHeight
}

// 创建景深通道
const bokehPass = new BokehPass(scene, camera, params)

// 创建效果合成器
const composer = new EffectComposer(renderer)

// 添加通道,注意顺序
composer.addPass(renderPass)
composer.addPass(bloomPass)
composer.addPass(bokehPass)

// 别忘记在render中添加composer
function render() {
    renderer.render(scene, camera)
    composer.render()
    requestAnimationFrame(render)
    
}

在向composer添加通道时,要注意顺序,如果辉光和景深共存的情况下,景深要添加在辉光后面,否则先设置景深再设置辉光后,景深效果就没了。

  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
以下是一个简单的 Three.js 辉光代码示例: ```javascript // 创建一个用于渲染的场景 const scene = new THREE.Scene(); // 创建一个用于渲染的相机 const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000); camera.position.z = 5; // 创建一个用于渲染的渲染器 const renderer = new THREE.WebGLRenderer({ antialias: true }); renderer.setSize(window.innerWidth, window.innerHeight); document.body.appendChild(renderer.domElement); // 创建一个立方体 const geometry = new THREE.BoxGeometry(1, 1, 1); const material = new THREE.MeshBasicMaterial({ color: 0xffffff }); const cube = new THREE.Mesh(geometry, material); scene.add(cube); // 创建一个辉光效果 const glowGeometry = new THREE.BoxGeometry(1.2, 1.2, 1.2); const glowMaterial = new THREE.MeshBasicMaterial({ color: 0xffffff, transparent: true, opacity: 0.5 }); const glowMesh = new THREE.Mesh(glowGeometry, glowMaterial); glowMesh.position.set(cube.position.x, cube.position.y, cube.position.z); scene.add(glowMesh); // 渲染场景 function animate() { requestAnimationFrame(animate); // 使立方体旋转 cube.rotation.x += 0.01; cube.rotation.y += 0.01; // 使辉光效果跟随立方体移动 glowMesh.position.set(cube.position.x, cube.position.y, cube.position.z); renderer.render(scene, camera); } animate(); ``` 这个代码创建了一个立方体,并在它周围创建了一个半透明的辉光效果辉光效果的大小比立方体稍大,所以看起来有点像立方体发出了光。通过使立方体旋转,辉光效果会跟随立方体移动。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值