gpt4 book ai didi

javascript - THREE.js 透明度和 EffectComposer

转载 作者:行者123 更新时间:2023-11-28 07:45:30 27 4
gpt4 key购买 nike

我正在尝试以某种方式组合纹理 channel 以保留 Alpha channel 。我已经尝试过或确信这一点。

  1. 渲染器将 alpha 设置为 true
  2. 渲染器具有许多不同的 setClearColor 设置。
  3. 效果 channel 使用的全屏四边形上的 Material 的透明度设置为 true
  4. 纹理实际上是透明的
  5. 我用于组合的着色器使用 Alpha channel

如果您注释掉其他纹理/ channel ,所有纹理/ channel 都会正确绘制,我只是无法让 PNG1.png 与 gits.jpg 混合。

var width = window.innerWidth;
var height = window.innerHeight;
var updateFcts = [];

var masterRenderer = new THREE.WebGLRenderer({
alpha: true,
autoClear: false
});
masterRenderer.setSize( window.innerWidth, window.innerHeight );
masterRenderer.setClearColor ( 0xFFFFFF, 1.0);
document.body.insertBefore( masterRenderer.domElement, document.body.firstChild);

var parameters = { minFilter: THREE.LinearFilter, magFilter: THREE.LinearFilter, format: THREE.RGBAFormat, stencilBuffer: false };
var renderTarget = new THREE.WebGLRenderTarget( width, height, parameters );
var masterComposer = new THREE.EffectComposer(masterRenderer, renderTarget);

var gitsTexture = THREE.ImageUtils.loadTexture( "images/gits.jpg" );
var pngTexture = THREE.ImageUtils.loadTexture( "images/PNG1.png" );

// declare passes
var passes = {};
passes.toScreen = new THREE.ShaderPass(THREE.CopyShader); passes.toScreen.renderToScreen = true;
passes.gitsTexturePass = new THREE.TexturePass(gitsTexture);
passes.gitsTexturePass.material.transparent = true;
passes.pngTexturePass = new THREE.TexturePass(pngTexture);
passes.pngTexturePass.material.transparent = true;

//add passes
masterComposer.addPass(passes.gitsTexturePass);
masterComposer.addPass(passes.pngTexturePass);
masterComposer.addPass(passes.toScreen);

// render the webgl

updateFcts.push(function(delta,now){
masterComposer.render();
})


//////////////////////////////////////////////////////////////////////////////////
// handle resize //
//////////////////////////////////////////////////////////////////////////////////

function onResize(){
width = window.innerWidth;
height = window.innerHeight;
// notify the renderer of the size change
masterRenderer.setSize( window.innerWidth, window.innerHeight );
// update the camera
camera.aspect = window.innerWidth / window.innerHeight
camera.updateProjectionMatrix();
resizeFcts.forEach(function(resizeFn){resizeFn()})
}

window.addEventListener('resize', onResize, false)

//////////////////////////////////////////////////////////////////////////////////
// loop runner //
//////////////////////////////////////////////////////////////////////////////////
var lastTimeMsec= null
requestAnimationFrame(function animate(nowMsec){
// keep looping
requestAnimationFrame( animate );
// measure time
lastTimeMsec = lastTimeMsec || nowMsec-1000/60
var deltaMsec = Math.min(200, nowMsec - lastTimeMsec)
lastTimeMsec = nowMsec
// call each update function
updateFcts.forEach(function(updateFn){
updateFn(deltaMsec/1000, nowMsec/1000)
})
})

最佳答案

解决方案:

You could try { premultipliedAlpha: false } in the renderer constructor, but you will need to be careful about blending modes if you do so.

此评论解决了我最初的问题。

<小时/>

我无法弄清楚我的问题到底出在哪里。我最好的猜测是,当使用 CopyShader 时,您的纹理会“预乘”。我找到了一种解决方法,通过编写一个着色器来实现我想要的效果,该着色器将 alpha 乘回来。

    fragmentShader: [


"uniform sampler2D tBase;",
"uniform sampler2D tAdd;",
"uniform float amount;",

"varying vec2 vUv;",

"void main() {",

"vec4 t1 = texture2D( tBase, vUv );",
"vec4 t2 = texture2D( tAdd, vUv );",
"gl_FragColor = (t1 * (1.0 - t2.a))+(t2 * t2.a);",

"}"

].join("\n")

关于javascript - THREE.js 透明度和 EffectComposer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27492603/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com