needDepthPrePass renders the whole mesh in black

Hello guys!

I’m starting to feel like an idiot here so if you have any clues what might be wrong please help me to solve the problem :stuck_out_tongue:

So:

  1. I load a GLB.
  2. I create two materials (one as main, one as backup) with the same function with different names so I call this function twice and I store the materials in a Map:
  private _createCustomWindowGlassMaterial(name: string) {
    const windowsConfig = this._sceneConfig.getWindows()

    const glassMaterial = new PBRMaterial(name, this.scene)
    glassMaterial.emissiveColor = new Color3(
      windowsConfig.windowTint.r,
      windowsConfig.windowTint.g,
      windowsConfig.windowTint.b
    )
    glassMaterial.alpha = windowsConfig.opacity
    glassMaterial.transparencyMode = Material.MATERIAL_ALPHABLEND
    glassMaterial.alphaMode = BABYLON.Engine.ALPHA_MULTIPLY
    glassMaterial.backFaceCulling = false
    // glassMaterial.needDepthPrePass = true
      return glassMaterial
  }
  1. I get the main material from the Map and assign it to some meshes. It’s all ok. Now if I set needDepthPrePass to true the meshes becomes black. However if I set the material of these meshes with the inspector to the second material the meshes are rendered correctly.

Logically it seems that I’m modifying the first material somewhere in my code because it is not working with the first one however working with the second one. But I am not aware of modifying the first material anywhere, I’ve triple (actually maybe 20 times already) checked my code, I’ve compared the two materials step by step, all the properties and they looks the same to me.

This is already driving me crazy :smiley: There must be something I am missing. Any hints what can cause a mesh being rendered black with this setup?

My codebase is already 20k+ lines but the material manipulation class is only a few hundred lines but despite of this I am not able to transfer the whole code to the playground. I’ve created a simple PG version but I couldn’t repro the issue. The difference between my code and the PG code is that I’m putting all my shared materials in a Map and getting them from there not from the scene but it must not cause any problems actually and I use an environment texture (not a HemisphericLight) and utilizing the DefaultRenderingPipeline in my app like I did it in the PG.

I’m running BJS 5.13.3. I can provide the PG link with the GLB (not public) for the BJS team if interested but as I said, I couldn’t repro the issue.

Thanks a lot!

:vulcan_salute:

R.

To be more precise on what am I doing:

  private _setupHDREnvironment(): void {
    const envConfig = this._sceneConfig.getHDREnvironment()
    if (!envConfig.enabled) {
      return
    }

    this.scene.ambientColor = fromConfigColor3(envConfig.ambientColor)

    if (envConfig.filename) {
      this.scene.environmentTexture = CubeTexture.CreateFromPrefilteredData(
        `${this._sceneConfig.getTextureDir()}${envConfig.filename}`,
        this.scene
      )
      this.scene.createDefaultSkybox(this.scene.environmentTexture, true, 3000, 0.3, false)
      this.scene.environmentIntensity = envConfig.intensity
    }
  }
  private _setupPipeline(camera: Camera): DefaultRenderingPipeline | undefined {
    const pipelineConfig = this._sceneConfig.getPipeline()
    if (!pipelineConfig.enabled) {
      return
    }

    const pipeline = new DefaultRenderingPipeline(
      'defaultPipeline', 
      this._sceneConfig.getHDREnvironment().enabled, 
      this.scene, 
      [camera] 
    )

    pipeline.samples = pipelineConfig.samples
    pipeline.fxaaEnabled = pipelineConfig.fxaaEnabled

    pipeline.imageProcessing.contrast = pipelineConfig.contrast
    pipeline.imageProcessing.exposure = pipelineConfig.exposure

    pipeline.imageProcessing.toneMappingEnabled = pipelineConfig.toneMappingEnabled
    pipeline.imageProcessing.toneMappingType = pipelineConfig.toneMappingType

    return pipeline
  }

Hey there! That’s a weird one :open_mouth: can you send the PG over my way? I’ll tag @Deltakosh too so when he’s back from FREEEEDOOOOM fireworks day he might have some idea about it on his wizard mind :thinking:

1 Like

@roland a repro would be really helpful, @CraigFeldspar do you have any idea ???

My bet would be on transparency.

1 Like

Hello Carol!
I’m still trying to repro the issue in the Playground but not luck yet. It doesn’t make sense for anyone to have a look at it since it’s working as expected. I’ll get back to you if I succeed to repro the issue. Thanks a lot!

1 Like

Hi!
So finally I’ve find the issue.

Calling

material.freeze()

will render the mesh black if needDepthPrePass === true.

Is there a limitation on freezing meshes with depthPrePass or is this a bug?

Thank you! :slight_smile:

R.

@carolhmj @sebavan @Deltakosh

1 Like

I would need to see it live in the PG to help

1 Like

I would think it is a limitation as the material is switching constantly with a depth prepass between 2 different instances.

2 Likes

Thanks buddy!