question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

WebGLRenderer: Difference between Scene.background and setClearColor()

See original GitHub issue

Using Scene.background and WebGLRenderer.setClearColor() produces the same output in the following demos:

Scene.background: https://jsfiddle.net/xkf3o8uw/5/ setClearColor(): https://jsfiddle.net/xkf3o8uw/4/

However, when setting autoClear to false, the behavior is different:

Scene.background: https://jsfiddle.net/0o38d67m/ setClearColor(): https://jsfiddle.net/xkf3o8uw/2/

Scene.background still works because of this line in Scene.background:

https://github.com/mrdoob/three.js/blob/a4f742c7d7b61f3efae147235efb0b5bc73e6e03/src/renderers/webgl/WebGLBackground.js#L52

What is the reason for this discrepancy? Does it just mean Scene.background used with a color should be decoupled of autoClear settings?

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:1
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
WestLangleycommented, Aug 16, 2021

Do you mind mentioning them^^?

Sure.

renderer.setClearColor( 0xff0000, 0.5 ); // the behavior is as-expected
  • supports alpha
  • ignores viewport
  • ignores outputEncoding
  • ignores tonemapping
  • honors scissor
scene.background = new THREE.Color( 0xff0000 ); // it currently behaves similar to clear color
  • does not support alpha
  • ignores viewport
  • ignores outputEncoding
  • ignores tonemapping
  • honors scissor
scene.background = new THREE.DataTexture( new Uint8Array( [ 255, 0, 0 ] ), 1, 1, THREE.RGBFormat );
  • does not support alpha
  • honors viewport
  • honors outputEncoding
  • honors tonemapping
  • honors scissor
  • honors encoding

I think that is all correct…

And for consistency, the scene background color should be assumed to be in linear colorspace. Currently it is an unusual special case.

1reaction
wmcmurraycommented, Jul 28, 2022

+1 That forceClear = true got me real confused this morning, I didn’t expect Scene.background to still clear the color even if autoClear === false 😕 ! For that reason, I ended up not using Scene.background at all.

Read more comments on GitHub >

github_iconTop Results From Across the Web

WebGLRenderer#setClearColor – three.js docs
The WebGL renderer displays your beautifully crafted scenes using WebGL. ... to use this if dealing with huge differences in scale in a...
Read more >
Transparent background with three.js - Stack Overflow
If you want a transparent background in three.js, you need pass in the alpha parameter to the WebGLRenderer constructor.
Read more >
Scene: background, fog and shadow. · Issue #17420 - GitHub
This is the current thinking: Background Taking #16900 and #17199 into account... scene.background = new ... setClearColor() and renderer.
Read more >
Define a Transparent Background in ThreeJS - DevSamples
Setting a Clear Background in a ThreeJS Scene. Copy renderer = new THREE.WebGLRenderer({ alpha: true, antialias: true }); // Second argument is for...
Read more >
Three.js Multiple Canvases Multiple Scenes
The solution is one canvas that fills the viewport in the background and some other element to ... return {scene, camera, elem};; };...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found