It seems like I might have to take the longer route to solve this issue, but let's give it a shot...
I'm facing a challenge when applying CSS to control the opacity of a specific HTML element that acts as a container for a Three.JS scene. In this scenario, there are multiple elements, each serving as containers for their respective scenes. The problem arises when the CSS attributes (even when applied inline) intended for one specific scene-containing element end up being applied to all elements containing scenes, rather than just the targeted one. This phenomenon occurs not only with opacity but with any post-applied CSS attribute.
The reason behind this workaround is that, based on my research, there's no direct method to set opacity on a Three.JS group object that houses multiple meshes. I am attempting - in theory - to avoid defining every material with transparency enabled and then recursively updating all meshes within a Three.JS Group object for a fade in/out animation.
Some of these group objects contain numerous meshes. Instead of individually updating the opacity of each mesh within a Three.JS Group object, my intention was/is to create separate scenes for different animations, allowing for customizable levels of transparency, and simply adjusting the opacity property of the HTML element containing that particular animation.
I've experimented with using both single and multiple cameras without success. I also attempted nesting the containers under an additional element and setting CSS on the parent element, but encountered the same issue. While I haven't explored the option of using multiple renderers, my research indicates potential performance concerns and context limitations associated with this approach. Furthermore, the render loop has "autoClear" disabled to ensure all scenes render simultaneously.
Below is the HTML structure. Note that the first element includes an inline style setting opacity to 0.5, while the second element has no inline styling:
<div class="three-js-container" id="scene-container-1" style="opacity:0.5;"></div>
<div class="three-js-container" id="scene-container-2"></div>
Below is the corresponding Javascript code:
/* Only one renderer instance is created */
var universalRenderer = new THREE.WebGLRenderer({antialias: true, alpha:true});
/* references to all containers are made */
var containerForScene1 = document.getElementById("scene-container-1");
var containerForScene2 = document.getElementById("scene-container-2");
/* two different cameras are created */
var cameraForScene1 = new THREE.PerspectiveCamera(50, window.innerWidth / window.innerHeight, 0.001, 1000);
var cameraForScene2 = new THREE.PerspectiveCamera(50, window.innerWidth / window.innerHeight, 0.001, 1000);
/* two different scenes are created, one for each element container */
var scene1 = new THREE.Scene();
scene1.userData.element = containerForScene1;
var scene2 = new THREE.Scene();
scene2.userData.element = containerForScene2;
/* the renderer is applied to both scene containers */
containerForScene1.appendChild(universalRenderer.domElement);
containerForScene2.appendChild(universalRenderer.domElement);
Upon playing both animations, both scenes appear at half-opacity instead of just the intended first scene. Why does CSS styling applied to one HTML scene-containing element affect all other scene-containing elements? Must I resort to manually controlling mesh opacity?
Appreciate your insights.