- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我是 three.js 的新手,我在实现以下效果时遇到了问题:渲染 2 个场景并将它们与混合着色器混合。第一个 TexturePass 没问题,但第二个像频闪仪一样闪烁。结果可以在这里看到:https://codepen.io/anon/pen/jYKqyj?editors=0010#0
这是代码:
THREE.Custom = THREE.Custom || {};
// Taken from https://github.com/stemkoski/stemkoski.github.com/blob/master/Three.js/js/shaders/AdditiveBlendShader.js
THREE.Custom.AdditiveShader = {
uniforms: {
tDiffuse: { type: "t", value: null },
tAdd: { type: "t", value: null },
fCoeff: { type: "f", value: 1.0 }
},
vertexShader: [
"varying vec2 vUv;",
"void main() {",
"vUv = uv;",
"gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
"}"
].join("\n"),
fragmentShader: [
"uniform sampler2D tDiffuse;",
"uniform sampler2D tAdd;",
"uniform float fCoeff;",
"varying vec2 vUv;",
"void main() {",
"vec4 texel = texture2D( tDiffuse, vUv );",
"vec4 add = texture2D( tAdd, vUv );",
"gl_FragColor = texel + add * fCoeff, 1.0;",
"}"
].join("\n")
};
var TestBlending = (function (){
var canvasWidth, canvasHeight, canvasRatio;
var clock = new THREE.Clock();
var container, camera, scene1, scene2, renderer;
var stats, statsDisplay = true;
var box1, box2;
var composerFinal, composerScene1, composerScene2, composerBlending, renderTarget;
var composerDebugScene1, composerDebugScene2;
var delta;
function createScene1 () {
scene1 = new THREE.Scene();
scene1.name = "scene1";
var light = new THREE.DirectionalLight( 0xffffff, 1 );
light.position.set( 0, 10, 20 );
light.name = "light1";
scene1.add(light);
var material = new THREE.MeshPhongMaterial( { color: 0xcc00cc } );
var geometry = new THREE.BoxGeometry( 1, 1, 1 );
box1 = new THREE.Mesh( geometry, material );
scene1.add( box1 );
}
function createScene2 () {
scene2 = new THREE.Scene();
scene2.name = "scene2";
var light = new THREE.DirectionalLight( 0xffffff, 1 );
light.position.set( 0, 10, 20 );
light.name = "light2";
scene2.add(light);
var material = new THREE.MeshPhongMaterial( { color: 0x33cc33 } );
var geometry = new THREE.BoxGeometry( 1, 1, 1 );
box2 = new THREE.Mesh( geometry, material );
scene2.add( box2 );
}
function init() {
container = document.getElementById('test_blending');
updateCanvasSize();
// RENDERER
renderer = new THREE.WebGLRenderer( {
antialias: true,
alpha: true
} );
renderer.setPixelRatio( (window.devicePixelRatio) ? window.devicePixelRatio : 1 );
renderer.setSize(canvasWidth, canvasHeight);
renderer.setClearColor( new THREE.Color(0x0), 0 );
renderer.autoClear = false;
// CAMERA
camera = new THREE.PerspectiveCamera( 10, canvasRatio, 1, 10000 );
camera.position.set(0, 0, 30);
camera.lookAt(new THREE.Vector3(0,0,0));
createScene1();
createScene2();
window.addEventListener( 'resize', onWindowResize, false );
}
function updateCanvasSize () {
canvasWidth = window.innerWidth;
canvasHeight = window.innerHeight;
canvasRatio = canvasWidth / canvasHeight;
}
function initProcessingPasses () {
var parameters = {
minFilter: THREE.LinearFilter,
magFilter: THREE.LinearFilter,
format: THREE.RGBAFormat,
stencilBuffer: true
};
renderTarget = new THREE.WebGLRenderTarget( canvasWidth, canvasHeight, parameters );
processing = { pass: {}, texture: {} };
processing.pass.clear = new THREE.ClearPass();
processing.pass.renderScene1 = new THREE.RenderPass( scene1, camera );
processing.pass.renderScene1.clear = true;
processing.pass.renderScene1.renderToScreen = false;
processing.pass.renderScene1.clearDepth = true;
processing.pass.renderScene2 = new THREE.RenderPass( scene2, camera );
processing.pass.renderScene2.clear = true;
processing.pass.renderScene2.renderToScreen = false;
processing.pass.renderScene2.clearDepth = true;
processing.pass.blending = new THREE.ShaderPass( THREE.Custom.AdditiveShader );
//processing.pass.blending.needsSwap = true;
processing.pass.blending.clear = true;
processing.pass.blending.renderToScreen = false;
processing.pass.output = new THREE.ShaderPass( THREE.CopyShader );
processing.pass.output.renderToScreen = true;
setPostProcessingPasses();
}
function setPostProcessingPasses () {
console.log('setPostProcessingPasses');
composerScene1 = null;
composerScene1 = new THREE.EffectComposer( renderer, renderTarget );
composerScene1.addPass( processing.pass.clear );
composerScene1.addPass( processing.pass.renderScene1 );
processing.texture.renderedScene1 = new THREE.TexturePass(composerScene1.renderTarget2.texture);
composerScene2 = null;
composerScene2 = new THREE.EffectComposer( renderer, renderTarget );
composerScene2.addPass( processing.pass.clear );
composerScene2.addPass( processing.pass.renderScene2 );
processing.texture.renderedScene2 = new THREE.TexturePass(composerScene2.renderTarget2.texture);
composerBlending = null;
composerBlending = new THREE.EffectComposer( renderer, renderTarget );
composerBlending.addPass( processing.pass.clear );
composerBlending.addPass( processing.texture.renderedScene1 );
processing.pass.blending.uniforms[ 'tAdd' ].value = composerScene2.renderTarget2.texture;
composerBlending.addPass( processing.pass.blending );
processing.texture.renderedBlending = new THREE.TexturePass(composerBlending.renderTarget2.texture);
composerFinal = null;
composerFinal = new THREE.EffectComposer( renderer, renderTarget );
composerFinal.addPass( processing.pass.clear );
composerFinal.addPass( processing.texture.renderedBlending );
composerFinal.addPass( processing.pass.output );
composerDebugScene1 = new THREE.EffectComposer( renderer, renderTarget );
composerDebugScene1.addPass( processing.pass.clear );
composerDebugScene1.addPass( processing.texture.renderedScene1 );
composerDebugScene1.addPass( processing.pass.output );
composerDebugScene2 = new THREE.EffectComposer( renderer, renderTarget );
composerDebugScene2.addPass( processing.pass.clear );
composerDebugScene2.addPass( processing.texture.renderedScene2 );
composerDebugScene2.addPass( processing.pass.output );
}
function addStats (container) {
stats = new Stats();
if (statsDisplay) container.appendChild( stats.dom );
}
function addToDOM() {
var canvas = container.getElementsByTagName('canvas');
if (canvas.length>0) {
container.removeChild(canvas[0]);
}
container.appendChild( renderer.domElement );
addStats(container);
}
function animate() {
window.requestAnimationFrame(animate);
var time = performance.now() * 0.001;
var cDelta = clock.getDelta();
box1.rotation.y = time / 5;
box1.rotation.x = 10;
box2.rotation.z = time / -5;
box2.rotation.x = time / -5;
box2.rotation.y = 5;
stats.update();
render();
}
function render() {
delta = clock.getDelta();;
renderer.setViewport( 0, 0, canvasWidth, canvasHeight );
composerScene1.render(delta);
composerScene2.render(delta);
composerBlending.render(delta);
composerFinal.render(delta);
// DEBUG
renderer.setViewport( 0, 0, canvasWidth/3, canvasHeight/3 );
composerDebugScene1.render(delta);
renderer.setViewport( canvasWidth/3*2, canvasHeight/3*2, canvasWidth/3, canvasHeight/3 );
composerDebugScene2.render(delta);
}
function onWindowResize() {
camera.aspect = canvasRatio;
camera.updateProjectionMatrix();
renderer.setSize( canvasWidth, canvasHeight );
composerScene1.setSize( canvasWidth, canvasHeight );
composerScene2.setSize( canvasWidth, canvasHeight );
composerBlending.setSize( canvasWidth, canvasHeight );
composerFinal.setSize( canvasWidth, canvasHeight );
}
return {
init: function () {
init();
initProcessingPasses();
addToDOM();
animate();
}
}
})();
TestBlending.init();
如有任何帮助,我们将不胜感激。谢谢
最佳答案
主要问题是 processing.pass.renderScene1
的目标纹理被添加了两次。它作为单独的传递添加到 composerBlending
中,也用于 processing.pass.blending
。
创建一个不使用默认漫反射纹理的混合着色器tDiffuse
:
THREE.Custom.AdditiveShader = {
uniforms: {
tBase: { type: "t", value: null },
tAdd: { type: "t", value: null },
fCoeff: { type: "f", value: 1.0 }
},
vertexShader: [
"varying vec2 vUv;",
"void main() {",
"vUv = uv;",
"gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
"}"
].join("\n"),
fragmentShader: [
"uniform sampler2D tBase;",
"uniform sampler2D tAdd;",
"uniform float fCoeff;",
"varying vec2 vUv;",
"void main() {",
"vec4 texel = texture2D( tBase, vUv );",
"vec4 add = texture2D( tAdd, vUv );",
"gl_FragColor = texel + add * fCoeff;",
"}"
].join("\n")
};
并将由场景 channel 创建的 2 个纹理手动设置为混合 channel 的 unifor 采样器变量。
composerBlending = new THREE.EffectComposer( renderer, renderTarget );
processing.pass.blending.uniforms[ 'tBase' ].value = composerScene1.renderTarget2.texture;
processing.pass.blending.uniforms[ 'tAdd' ].value = composerScene2.renderTarget2.texture;
composerBlending.addPass( processing.pass.blending );
查看代码片段:
THREE.Custom = THREE.Custom || {};
// Taken from https://github.com/stemkoski/stemkoski.github.com/blob/master/Three.js/js/shaders/AdditiveBlendShader.js
THREE.Custom.AdditiveShader = {
uniforms: {
tBase: { type: "t", value: null },
tAdd: { type: "t", value: null },
fCoeff: { type: "f", value: 1.0 }
},
vertexShader: [
"varying vec2 vUv;",
"void main() {",
"vUv = uv;",
"gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
"}"
].join("\n"),
fragmentShader: [
"uniform sampler2D tBase;",
"uniform sampler2D tAdd;",
"uniform float fCoeff;",
"varying vec2 vUv;",
"void main() {",
"vec4 texel = texture2D( tBase, vUv );",
"vec4 add = texture2D( tAdd, vUv );",
"gl_FragColor = texel + add * fCoeff;",
"}"
].join("\n")
};
var TestBlending = (function (){
var canvasWidth, canvasHeight, canvasRatio;
var clock = new THREE.Clock();
var container, camera, scene1, scene2, renderer;
var stats, statsDisplay = true;
var box1, box2;
var composerFinal, composerScene1, composerScene2, composerBlending, renderTarget;
var composerDebugScene1, composerDebugScene2;
var delta;
function createScene1 () {
scene1 = new THREE.Scene();
scene1.name = "scene1";
var light = new THREE.DirectionalLight( 0xffffff, 1 );
light.position.set( 0, 10, 20 );
light.name = "light1";
scene1.add(light);
var material = new THREE.MeshPhongMaterial( { color: 0xcc00cc } );
var geometry = new THREE.BoxGeometry( 1, 1, 1 );
box1 = new THREE.Mesh( geometry, material );
scene1.add( box1 );
}
function createScene2 () {
scene2 = new THREE.Scene();
scene2.name = "scene2";
var light = new THREE.DirectionalLight( 0xffffff, 1 );
light.position.set( 0, 10, 20 );
light.name = "light2";
scene2.add(light);
var material = new THREE.MeshPhongMaterial( { color: 0x33cc33 } );
var geometry = new THREE.BoxGeometry( 1, 1, 1 );
box2 = new THREE.Mesh( geometry, material );
scene2.add( box2 );
}
function init() {
container = document.getElementById('test_blending');
updateCanvasSize();
// RENDERER
renderer = new THREE.WebGLRenderer( {
antialias: true,
alpha: true
} );
renderer.setPixelRatio( (window.devicePixelRatio) ? window.devicePixelRatio : 1 );
renderer.setSize(canvasWidth, canvasHeight);
renderer.setClearColor( new THREE.Color(0x0), 0 );
renderer.autoClear = false;
// CAMERA
camera = new THREE.PerspectiveCamera( 10, canvasRatio, 1, 100 );
camera.position.set(0, 0, 30);
camera.lookAt(new THREE.Vector3(0,0,0));
createScene1();
createScene2();
window.addEventListener( 'resize', onWindowResize, false );
}
function updateCanvasSize () {
canvasWidth = window.innerWidth;
canvasHeight = window.innerHeight;
canvasRatio = canvasWidth / canvasHeight;
}
function initProcessingPasses () {
var parameters = {
minFilter: THREE.LinearFilter,
magFilter: THREE.LinearFilter,
format: THREE.RGBAFormat,
stencilBuffer: true,
};
var parameters2 = {
minFilter: THREE.LinearFilter,
magFilter: THREE.LinearFilter,
format: THREE.RGBAFormat,
stencilBuffer: true,
};
renderTarget = new THREE.WebGLRenderTarget( canvasWidth, canvasHeight, parameters );
processing = { pass: {}, texture: {} };
processing.pass.clear = new THREE.ClearPass();
processing.pass.renderScene1 = new THREE.RenderPass( scene1, camera );
processing.pass.renderScene1.clear = true;
processing.pass.renderScene1.renderToScreen = false;
processing.pass.renderScene1.clearDepth = true;
processing.pass.renderScene2 = new THREE.RenderPass( scene2, camera );
processing.pass.renderScene2.clear = true;
processing.pass.renderScene2.renderToScreen = false;
processing.pass.renderScene2.clearDepth = true;
processing.pass.blending = new THREE.ShaderPass( THREE.Custom.AdditiveShader );
processing.pass.blending.renderToScreen = false;
processing.pass.blending.clear = true;
processing.pass.output = new THREE.ShaderPass( THREE.CopyShader );
processing.pass.output.needsSwap = true;
processing.pass.output.renderToScreen = true;
setPostProcessingPasses();
}
function setPostProcessingPasses () {
console.log('setPostProcessingPasses');
composerScene1 = new THREE.EffectComposer( renderer, renderTarget );
composerScene1.addPass( processing.pass.clear );
composerScene1.addPass( processing.pass.renderScene1 );
processing.texture.renderedScene1 = new THREE.TexturePass(composerScene1.renderTarget2.texture);
composerScene2 = new THREE.EffectComposer( renderer, renderTarget );
composerScene2.addPass( processing.pass.clear );
composerScene2.addPass( processing.pass.renderScene2 );
processing.texture.renderedScene2 = new THREE.TexturePass(composerScene2.renderTarget2.texture);
composerBlending = new THREE.EffectComposer( renderer, renderTarget );
processing.pass.blending.uniforms[ 'tBase' ].value = composerScene1.renderTarget2.texture;
processing.pass.blending.uniforms[ 'tAdd' ].value = composerScene2.renderTarget2.texture;
composerBlending.addPass( processing.pass.blending );
processing.texture.renderedBlending = new THREE.TexturePass(composerBlending.renderTarget2.texture);
composerFinal = new THREE.EffectComposer( renderer, renderTarget );
composerFinal.addPass( processing.texture.renderedBlending );
composerFinal.addPass( processing.pass.output );
composerDebugScene1 = new THREE.EffectComposer( renderer, renderTarget );
composerDebugScene1.addPass( processing.pass.clear );
composerDebugScene1.addPass( processing.texture.renderedScene1 );
composerDebugScene1.addPass( processing.pass.output );
composerDebugScene2 = new THREE.EffectComposer( renderer, renderTarget );
composerDebugScene2.addPass( processing.pass.clear );
composerDebugScene2.addPass( processing.texture.renderedScene2 );
composerDebugScene2.addPass( processing.pass.output );
}
function addStats (container) {
//stats = new Stats();
//if (statsDisplay) container.appendChild( stats.dom );
}
function addToDOM() {
container.appendChild( renderer.domElement );
addStats(container);
}
function animate() {
window.requestAnimationFrame(animate);
var time = performance.now() * 0.001;
var cDelta = clock.getDelta();
box1.rotation.y = time / 5;
box1.rotation.x = 10;
box2.rotation.z = time / -5;
box2.rotation.x = time / -5;
box2.rotation.y = 5;
//stats.update();
render();
}
function render() {
delta = clock.getDelta();
renderer.setViewport( 0, 0, canvasWidth, canvasHeight );
composerScene1.render(delta);
composerScene2.render(delta);
composerBlending.render(delta);
composerFinal.render(delta);
// DEBUG
renderer.setViewport( 0, 0, canvasWidth/3, canvasHeight/3 );
composerDebugScene1.render(delta);
renderer.setViewport( canvasWidth/3*2, canvasHeight/3*2, canvasWidth/3, canvasHeight/3 );
composerDebugScene2.render(delta);
}
function onWindowResize() {
updateCanvasSize();
camera.aspect = canvasRatio;
camera.updateProjectionMatrix();
renderer.setSize( canvasWidth, canvasHeight );
composerScene1.setSize( canvasWidth, canvasHeight );
composerScene2.setSize( canvasWidth, canvasHeight );
composerBlending.setSize( canvasWidth, canvasHeight );
composerFinal.setSize( canvasWidth, canvasHeight );
}
return {
init: function () {
init();
initProcessingPasses();
addToDOM();
animate();
}
}
})();
TestBlending.init();
<script src="https://threejs.org/build/three.min.js"></script>
<!--script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/89/three.min.js"></script-->
<script src="https://threejs.org/examples/js/controls/OrbitControls.js"></script>
<script src="https://threejs.org/examples/js/shaders/CopyShader.js"></script>
<script src="https://threejs.org/examples/js/postprocessing/EffectComposer.js"></script>
<script src="https://threejs.org/examples/js/postprocessing/ClearPass.js"></script>
<script src="https://threejs.org/examples/js/postprocessing/RenderPass.js"></script>
<script src="https://threejs.org/examples/js/postprocessing/ShaderPass.js"></script>
<script src="https://threejs.org/examples/js/postprocessing/TexturePass.js"></script>
<div id="test_blending"></div>
关于javascript - Three.js - EffectComposer - 混合场景闪烁,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48260278/
websocket的用途/场景 先总结:高即时性服务,比如聊天室的群聊,server顺序收到了张三,李四的消息,立即就推送给王五,不能让王五等半天。 Ajax也可以一秒一刷,让王五去问张三说话没,如果
前端的工作过程里,本地开发、提供测试环境,总得有个用着顺手的服务器软件,这个场景里nginx很流行。 介绍两个好用的配置项:rewrite try_files @xxxx rewrite 比较
我有一个场景的两个不同角度的 2 个视频文件,我想重建场景的 3D 估计。它类似于 3D 传感器的作用(例如 Kinect、PrimeSense)。我正在寻找一个库,甚至是一个完善的机器视觉算法,以便
我已阅读RebaseProject页面并尝试了一个不平凡的例子(不是对一个完整的分支进行 rebase )。这与 rebase D 的情况类似我场景B。 这是rebase之前的情况: default
有没有办法将我的场景保存在 JavaFx 应用程序中单独的 Java 文件中?我尝试过这样的事情: public class MyApp extends Application { pri
我有这样的场景:用户想要查看大量有关自己的信息。例如:年龄、姓名、地位、收入、工作、爱好、 child 的名字、妻子的名字、酋长的名字、祖父/祖母的名字。大约 50 个变量。他可以选择任何变量来显示信
我希望有人能帮助我解决这个问题:我有一个包含条目的表。我想执行查询并根据模式获取得分最高的记录。模式将是:如果我的话按原样出现,那么该条目的分数将是最高的。如果该单词出现在句子中,则该条目的分数将低于
我正在尝试在我的应用程序委托(delegate)方法中实现一些逻辑。了解当前正在运行哪种场景将非常有帮助。 [[CCDirector sharedDirector] runningScene] 返回当
好的,这是一个有趣的。我有 2 个表:tbl_notes、tbl_notes_categories 简单地说,tbl_notes 有一个 categoryid,我将 2 个表与该 ID 相关联。所以,
我有一个使用并行运行的 Specflow、selenium、NUnit 的测试解决方案在 AssemblyInfo 中添加了这个:[程序集:Parallelizable(ParallelScope.F
我正在尝试弄清楚如何在 SpriteKit 中添加更多场景。如果我在 GameViewController 中使用 SpriteKit 生成的行 if let scene = GameScene.un
目录 1、业务背景 2、场景分析 3、流程设计 1、业务流程 2、导入流程
我是 Unity 的新手,所以修复起来可能非常简单。我使用了一个 3D Google SketchUp 模型,我想让玩家环顾模型。 super 简单。 我添加了 3D 平面,添加了相机并更新了设置以支
我需要标记要跳过的某些测试。但是,有些测试是参数化的,我只需要能够跳过某些场景。 我使用 py.test -m "hermes_only" 调用测试或 py.test -m "not hermes_o
我已经开始使用 SpecFlow 并想知道是否可以在规范之间重用场景 基本上我的想法是这样的(我可能从根本上是错误的:)) 我编写了一项功能来验证导航。 功能:导航 I should be able
在编写验证输入表单上的信息的 BDD 场景时,您将如何列出规则。 选项是: 1) 每个规则一个场景 2)场景大纲,每个领域和规则的例子 我们如何说某些不在特定字符集中的无效内容,例如: 鉴于我输入了一
我们如何使用 StoryQ 来测试预期出现异常的场景? 最佳答案 就实际代码而言,在测试代码的 .Then 部分,您需要创建一个 Action 或 Func 来确定正在测试的内容,然后在代码的 .Th
完成快速初学者努力通过点击按钮向场景添加节点。 我知道我可以使用点击手势来获取点击坐标并执行点击测试,然后在点击的 3D 空间中放置一个对象。但是,我想在设备屏幕的中央显示一个球体或十字准线,当点击屏
如何在表格中传递空格? Background: Given the following books |Author |(here several spaces)
我正在尝试从 Eric Haines' Standard Procedural Database (SPD) 渲染“mount”场景,但折射部分就是不想配合。我已经尝试了所有我能想到的方法来修复它。
我是一名优秀的程序员,十分优秀!