【问题标题】:Three.JS updating camera breaks shader?三.JS 更新相机会破坏着色器?
【发布时间】:2019-11-30 22:59:30
【问题描述】:

我正在从 https://github.com/stemkoski/stemkoski.github.com/blob/master/Three.js/Bubble.html 复制气泡着色器,但代码已过时,并且示例会折射其背后的任何内容:

我的是黑色的。我在 animate() 中收到的警告(以及我相信的问题所在):

animate() {
    this.controls.update();

    this.renderer.render(this.scene, this.camera);

    this.sphere.visible = false;
    //this.refractSphereCamera.updateCubeMap( this.renderer, this.scene );
    this.refractSphereCamera.update();
    this.sphere.visible = true;

    //this.renderer.render( this.scene, this.camera );
    requestAnimationFrame(this.animate.bind(this));
  }

this.refractSphereCamera.updateCubeMap( this.renderer, this.scene ); 是控制台给我的原始行:

THREE.CubeCamera:.updateCubeMap() 现在是 .update()。

这是实际的着色器/球体:

this.refractSphereCamera = new THREE.CubeCamera( 0.1, 5000, 512 );
    this.scene.add( this.refractSphereCamera );

    var fShader = THREE.FresnelShader;

    var fresnelUniforms =
    {
        "mRefractionRatio": { type: "f", value: 1.02 },
        "mFresnelBias":     { type: "f", value: 0.1 },
        "mFresnelPower":    { type: "f", value: 2.0 },
        "mFresnelScale":    { type: "f", value: 1.0 },
        "tCube":            { type: "t", value: this.refractSphereCamera.renderTarget } //textureCube
    };

    // create custom material for the shader
    var customMaterial = new THREE.ShaderMaterial(
    {
        uniforms:       fresnelUniforms,
        vertexShader:   fShader.vertexShader,
        fragmentShader: fShader.fragmentShader
    }   );

    var sphereGeometry = new THREE.SphereGeometry( 100, 64, 32 );
    this.sphere = new THREE.Mesh( sphereGeometry, customMaterial);
    this.sphere.position.set(0, 50, 100);
    this.scene.add(this.sphere);

    this.refractSphereCamera.position.set(this.sphere.position);

我可以将一个纹理立方体输入到着色器中,它可以工作,但我不仅希望天空盒,而且希望球体后面的所有对象都发生折射。这里有什么问题?

如果我更改为 .update(),一切都会变黑

其他警告是:

THREE.WebGLRenderer.setTextureCube:不要使用立方体渲染目标作为 纹理。请改用他们的 .texture 属性。

【问题讨论】:

  • 你能发布一些工作代码吗?这里似乎没有任何问题,但它没有显示你如何渲染背景。为了进行健全性检查,您能否关闭天空盒,将背景清除为非黑色的东西,然后渲染飞机?
  • stemkoski.github.io/Three.js/Bubble.html - 这是有效的。我这样做了——如果我输入纹理立方体,它不会在折射中渲染它后面的任何对象,否则它不会渲染黑色。但是看到我上面也遇到的错误

标签: javascript html three.js shader


【解决方案1】:

尝试将您的.far 增加到 5,001 或 10,000。该示例似乎将天空盒渲染为场景内的立方体,如果您的相机剪辑为 5,000 并且立方体的大小相同,那么很可能它只是被剪辑在您的相机后面。

【讨论】:

  • 不,试过了,仍然是黑色的。如果它相关,我也会收到上面发布的错误 - THREE.WebGLRenderer.setTextureCube:不要使用立方体渲染目标作为纹理。改用他们的 .texture 属性。
  • 我认为不应该那么重要,因为它会自动回退,但可能会改变(您可以尝试提供.texture)。正如我在另一条评论中所说,清除背景并将平面渲染到立方体贴图中将是调试此问题的一个很好的步骤,因为您将天空盒排除在等式之外。如果您可以反射一些 3d 对象和自定义颜色背景,则可以继续尝试渲染天空盒...
  • 我试过把其他物体放在球体的后面,还有地板,球体仍然是全黑的。接下来你会建议什么,代码明智?
  • renderer.setClearColor( 0xff0000, 1)
  • 在最近的版本中这也可能需要手动调用:threejs.org/docs/#api/en/renderers/… 除此之外,您可以通过加载立方体纹理(而不是渲染到一个)来验证着色器是否工作。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-05
  • 2017-03-28
  • 2017-06-30
  • 2020-12-18
  • 2017-08-07
  • 1970-01-01
相关资源
最近更新 更多