Hello,
Trying to implement bloom in three js using three.js/webgl_postprocessing_unreal_bloom_selective.html at 191a1ef699a5d53de2cd645c667e7fee184a18fd · mrdoob/three.js · GitHub
I’m getting a large red plane in front with black everywhere else , or, if finalComposer.addPass( finalPass ); is disabled, it just looks normal, without any bloom effect.
Is there a minimum three js version needed? I believe I’m using 129.
Thank you,
Jim
Here are the links to the failed and working screenshots:
normal view, with bloom off: https://scontent-lga3-1.xx.fbcdn.net/v/t39.30808-6/291789922_10220929781941029_8123652435574873713_n.jpg?_nc_cat=108&ccb=1-7&_nc_sid=8bfeb9&_nc_ohc=LN3CISKowAgAX9qWE4J&tn=i8S_vLP6o8fXBrEm&_nc_ht=scontent-lga3-1.xx&oh=00_AT9QFOagZlPGgUpflExR4gIQVyX4yoflCRT6qu3TjclzVw&oe=62C741A8
fail to red and black, with bloom on: https://scontent-lga3-1.xx.fbcdn.net/v/t39.30808-6/291171692_10220929760420491_7843779091932940070_n.jpg?_nc_cat=100&ccb=1-7&_nc_sid=8bfeb9&_nc_ohc=LoayV9RA3iwAX85zlWf&_nc_ht=scontent-lga3-1.xx&oh=00_AT-5dB4pF9U7WAmZrwrzl6cdfaYLpzHkRNxCC2C8UxONMg&oe=62C81FE7
Below is the relevant code, using a php cache and window vars since render, animate, renderBloom and other functions are loaded first in this case. Don’t use orbit controls here.
// Tween.js script here - doesn't show correctly
// script type="module" - doesn't show correctly
$cache .= 'import * as THREE from "../three.js/build/three.module.js";'; // CURRENT, working
$cache .= 'import { VRButton } from "../three.js/examples/jsm/webxr/VRButton.js";'; // CURRENT, working
$cache .= 'import { XRControllerModelFactory } from "../three.js/examples/jsm/webxr/XRControllerModelFactory.js";'; // CURRENT, working
$cache .= 'import { XRHandModelFactory } from "../three.js/examples/jsm/webxr/XRHandModelFactory.js";'; // CURRENT, working
$cache .= 'import {GLTFLoader} from "../three.js/examples/jsm/loaders/GLTFLoader.js";'; // CURRENT, working
$cache .= 'import {OBJLoader} from "../three.js/examples/jsm/loaders/OBJLoader.js";'; // CURRENT, working
$cache .= 'import {MTLLoader} from "../three.js/examples/jsm/loaders/MTLLoader.js";'; // CURRENT, working
// bloom, new
if ($bloom_strength) {
//$cache .= 'import Stats from "../three.js/examples/jsm/libs/stats.module.js";';
$cache .= 'import { GUI } from "../three.js/examples/jsm/libs/dat.gui.module.js";';
$cache .= 'import { EffectComposer } from "../three.js/examples/jsm/postprocessing/EffectComposer.js";';
//$cache .= 'import { EffectComposer } from "https://threejs.org/examples/jsm/postprocessing/EffectComposer.js";';
$cache .= 'import { RenderPass } from "../three.js/examples/jsm/postprocessing/RenderPass.js";';
$cache .= 'import { ShaderPass } from "../three.js/examples/jsm/postprocessing/ShaderPass.js";';
$cache .= 'import { UnrealBloomPass } from "../three.js/examples/jsm/postprocessing/UnrealBloomPass.js";';
$cache .= 'const ENTIRE_SCENE = 0, BLOOM_SCENE = 1;'; // orig
$cache .= 'window.bloomLayer = new THREE.Layers();';
$cache .= 'window.bloomLayer.set( BLOOM_SCENE );';
$cache .= 'const params = {';
$cache .= 'exposure: 1,';
$cache .= 'bloomStrength: '.$bloom_strength.',';
$cache .= 'bloomThreshold: 0,';
$cache .= 'bloomRadius: 0,';
$cache .= 'scene: "Scene with Glow"';
$cache .= '};';
$cache .= 'window.darkMaterial = new THREE.MeshBasicMaterial( { color: "black" } );';
$cache .= 'window.materials = {};';
}
// try
$cache .= 'window.GLTFLoader = GLTFLoader;';
$cache .= 'window.THREE = THREE;';
$cache .= 'window.OBJLoader = OBJLoader;';
$cache .= 'window.MTLLoader = MTLLoader;';
$cache .= 'window.VRButton = VRButton;';
$cache .= 'window.XRControllerModelFactory = XRControllerModelFactory;';
$cache .= 'window.XRHandModelFactory = XRHandModelFactory;';
// try save these also to window
if ($bloom_strength) {
$cache .= 'window.EffectComposer = EffectComposer;';
$cache .= 'window.RenderPass = RenderPass;';
$cache .= 'window.UnrealBloomPass = UnrealBloomPass;';
$cache .= 'window.bloomStrength = '.$bloom_strength.';';
}
...
// scene
$cache .= 'window.scene = new window.THREE.Scene();';
$cache .= 'window.scene.background = new window.THREE.Color( 0x444444 );';
$cache .= 'window.camera = new window.THREE.PerspectiveCamera( 80, window.innerWidth / window.innerHeight, 0.1, 1000 );'; // for outdoors
...
// renderer
$cache .= 'window.renderer = new window.THREE.WebGLRenderer({ antialias: true });';
$cache .= 'window.renderer.setPixelRatio( window.devicePixelRatio );';
$cache .= 'window.renderer.setSize( window.innerWidth, window.innerHeight );';
if ($bloom_strength)
$cache .= 'window.renderer.toneMapping = THREE.ReinhardToneMapping;';
$cache .= 'window.renderer.outputEncoding = window.THREE.sRGBEncoding;';
$cache .= 'window.renderer.xr.enabled = true;';
$cache .= 'window.renderer.xr.setFramebufferScaleFactor(2.0);';
$cache .= 'window.container.appendChild( window.renderer.domElement );';
}
...
// load object to bloom, cylinder in this case
} elseif ($entity_tag == 'a-cylinder' && $radius_vr && $height) {
$cache .= 'var geometry = new window.THREE.CylinderGeometry( '.$radius_vr.', '.$radius_vr.', '.$height.' );';
$cache .= 'var material = new window.THREE.MeshBasicMaterial( { color: 0x'.substr($color, 1).' } );';
$cache .= 'var mesh = new window.THREE.Mesh( geometry, material );';
if ($x)
$cache .= 'mesh.position.x = '.$x.';';
if ($y)
$cache .= 'mesh.position.y = '.$y.';';
if ($z)
$cache .= 'mesh.position.z = '.$z.';';
if ($r)
$cache .= 'mesh.rotation.x = '.deg2rad($r).';';
if ($ry)
$cache .= 'mesh.rotation.y = '.deg2rad($ry).';';
if ($rz)
$cache .= 'mesh.rotation.z = '.deg2rad($rz).';';
if ($entity_id)
$cache .= 'mesh.name = "'.$entity_id.'";';
if ($vr_parent) {
$cache .= 'var vrParentList = window.scene.getObjectByName("'.$vr_parent.'");';
$cache .= 'if(vrParentList) {';
$cache .= 'vrParentList.add(mesh);';
$cache .= '}';
} else
$cache .= 'window.scene.add( mesh);';
// try bloom here, new, try non-gltf, believe not working either
if ($door_bloom > 0) {
$cache .= 'mesh.layers.enable( BLOOM_SCENE );';
}
}
...
$cache .= set_up_rig($userid, $x, $y, $z, 'return', '', $threejs, $user_y, $user_ry, $vr_parent, $userid_2nd);
// call new fn; try skip js open; nc odd
$cache .= set_up_camera($userid, $x, $y, $z, 'return', $threejs, $http_prefix, $open_inline, $user_ry, $vr_parent, $userid_2nd);
if($bloom_strength) {
$cache .= 'const renderScene = new RenderPass( window.scene, window.camera );';
$cache .= 'const bloomPass = new UnrealBloomPass( new THREE.Vector2( window.innerWidth, window.innerHeight ), 1.5, 0.4, 0.85 );';
$cache .= 'bloomPass.threshold = params.bloomThreshold;';
$cache .= 'bloomPass.strength = params.bloomStrength;';
$cache .= 'bloomPass.radius = params.bloomRadius;';
$cache .= 'window.bloomComposer = new EffectComposer( window.renderer );';
$cache .= 'window.bloomComposer.renderToScreen = false;';
$cache .= 'window.bloomComposer.addPass( renderScene );';
$cache .= 'window.bloomComposer.addPass( bloomPass );';
$cache .= 'const finalPass = new ShaderPass(';
$cache .= 'new THREE.ShaderMaterial( {';
$cache .= 'uniforms: {';
$cache .= 'baseTexture: { value: null },';
$cache .= 'bloomTexture: { value: window.bloomComposer.renderTarget2.texture }';
$cache .= '},';
$cache .= 'defines: {}';
$cache .= '} ), "baseTexture"';
$cache .= ');';
$cache .= 'finalPass.needsSwap = true;';
$cache .= 'window.finalComposer = new EffectComposer( window.renderer );';
$cache .= 'window.finalComposer.addPass( renderScene );';
// all red
$cache .= 'window.finalComposer.addPass( finalPass );';
}
$cache .= 'animate();';
// js fns
function animate() {
// test ok window.bloomStrength jsa35794: 10
//alert("window.bloomStrength jsa35794: " + window.bloomStrength);
window.renderer.setAnimationLoop( render );
}
function render() {
var indexTip2Pos = window.hand2.joints[ "index-finger-tip" ].position;
// carry out and tween move
if ( TWEEN) TWEEN.update();
// dolly (rig) move, new 2021-08-29 14:25:02 per https://stackoverflow.com/questions/62476426/webxr-controllers-for-button-pressing-in-three-js
dollyMove();
// render scene with bloom - not working, disabling for now
if (window.bloomStrength) {
renderBloom( true );
// render the entire scene, then render bloom scene on top - test w/o, fail to black
window.finalComposer.render();
//return;
//break;
} else
window.renderer.render( window.scene, window.camera ); // working desktop, non vr in hallway
}
function renderBloom( mask ) {
if ( mask === true ) {
// does darken everything if not restored
window.scene.traverse( darkenNonBloomed );
window.bloomComposer.render();
window.scene.traverse( restoreMaterial );
} else {
window.camera.layers.set( BLOOM_SCENE );
window.bloomComposer.render();
window.camera.layers.set( ENTIRE_SCENE );
}
}
// partial php fns - within set up camera:
$cache .= 'window.camera = new window.THREE.PerspectiveCamera( 80, window.innerWidth / window.innerHeight, 0.1, 1000 );'; // for outdoors