How to add and remove different light types at run time in three.js?
I have some checkboxes, each representing a light type and I want to add a certain light type to the scene when its checkbox is checked and remove the light when unchecked.
I tried: scene.remove(light) and light.visible = false, but did not work.
With WebGLRenderer, if you change the number of lights, or types of lights, you need to set material.needsUpdate = true.
A better option is to set the light intensity to zero.
For more information, see the Wiki article How to Update Things.
three.js r.116
To hide/show light use:
light.visible = false; //or true
and set needsUpdate for all your materials to true.
material.needsUpdate = true;
I have all materials inside one object which properties are materials objects, so i have.
for (var material in materials) {
if (materials.hasOwnProperty(material)) {
materials[material].needsUpdate = true;
}
}
That will allow you to see all updates. Before you do needsUpdate trick, you will see nothing in most cases.
It worked with me like this:
render(){
// some other code ....
//light
if($('#dLight').is(':checked')){
dLight.position.set($('#light-x').slider('value'),$('#light-y').slider('value'),$('#light-z').slider('value')).normalize();
dLight.intensity = $('#light-intensity-id').slider('value');
dLight.color.setHex('0x' + $('#light-color').val());
scene.add(dLight);
}else{
scene.remove(dLight);
}
if($('#pLight').is(':checked')){
pLight.position.set($('#light-x').slider('value'),$('#light-y').slider('value'),$('#light-z').slider('value'));
pLight.intensity = $('#light-intensity-id').slider('value');
pLight.color.setHex('0x' + $('#light-color').val());
scene.add(pLight);
}else{
scene.remove(pLight);
}
if($('#sLight').is(':checked')){
sLight.position.set($('#light-x').slider('value'),$('#light-y').slider('value'),$('#light-z').slider('value'));
sLight.intensity = $('#light-intensity-id').slider('value');
sLight.color.setHex('0x' + $('#light-color').val());
scene.add(sLight);
}else{
scene.remove(sLight);
}
if($('#aLight').is(':checked')){
aLight.position.set($('#light-x').slider('value'),$('#light-y').slider('value'),$('#light-z').slider('value'));
aLight.color.setHex('0x' + $('#light-color').val());
scene.add(aLight);
}else{
scene.remove(aLight);
}
}
Related
I'm trying to assign a new material to an object, but when I assign a new (color) map, the object renders as white, and the AO and shadows no longer show up. It's as if the emissive attribute is 100%. I can change the color attribute (e.g. 'red' or 'blue'), ao, normal, etc. without issues. The glb loaded in already has a working material with a color map and ao, but I want to be able to replace it.
I'm using 8th Wall with A-Frame, but I've registered the following as a custom Three.js component.
const customMat = {
schema: {}, // will pass textures via aframe later
init() {
this.el.addEventListener('model-loaded', (e) => {
const material = new THREE.MeshStandardMaterial()
const texLoader = new THREE.TextureLoader()
texLoader.crossOrigin = ''
const mapColor = texLoader.load('assets/cover_color.jpg')
const mapAO = texLoader.load('assets/cover_ao.jpg')
material.map = mapColor // makes everything 100% white likes it's emissive
// material.color = new THREE.Color('red') // works fine no problem
material.aoMap = mapAO
material.aoMapIntensity = 1
e.detail.model.traverse((mesh) => {
if (mesh.isMesh) {
mesh.material = material
mesh.material.needsUpdate = true // not sure if needed
}
})
})
},
}
export {customMat}
Any suggestions would be much appreciated. I've tried this with primitive geometry too, but the same issue occurs. I don't seem to be able to modify the existing material's attributes either, so maybe my approach is fundamentally wrong.
Has anyone had any luck implementing bloom effects or other post-processing effects from https://threejs.org/docs/#examples/en/postprocessing/EffectComposer into a scene?
I’ve come across this a few times and tried to use it but can’t seem to make it work with the latest version of Aframe https://github.com/wizgrav/aframe-effects
I know it's not possible in VR yet and depends on Three work, but is there a way to use them if I'm not intending on my scene being used in VR or AR mode?
If you don't need VR or AR mode, you can use the effect composer "directly" (check out Don McCurdys gist):
Create a THREE.EffectComposer object,
add passes, and
call the composers render on each renderloop .
The "hacky" part is in 3, because you need to override a-frames default behavior.
// assuming this is a component
init: function() {
const renderer = this.sceneEl.renderer;
this.composer = new THREE.EffectComposer(renderer);
// add some passes ..
// (...)
// override `render`
this.bind();
},
// we need to keep the timestamps for the composer.render() function
tick: function (t, dt) {
this.t = t;
this.dt = dt;
},
// Bind the EffectComposer to the A-Frame render loop.
bind: function () {
const renderer = this.sceneEl.renderer;
const render = renderer.render;
const system = this;
let isDigest = false;
renderer.render = function () {
if (isDigest) {
render.apply(this, arguments);
} else {
isDigest = true;
system.composer.render(system.dt);
isDigest = false;
}
};
Check it out in this glitch;
As mentioned, all credit to Don and his gist.
Keep in mind - a-frame does not have most stuff from the threejs examples, so you may need to include them separately (like i did with the passes and shaders in my glitch)
e.g. a box should be Blending Mode,
but when it run perfectly in aframe debug inspector,
but it does not work in the 8thwall WebAR Camera,
since the "camera" plane also Addiative become White within once second.
Can this be solve?
it eventually turn out all Blank White,
i was using "modify-material" code from 8thwall example
<a-scene
xrweb="disableWorldTracking: true"
xrextras-almost-there
xrextras-loading
xrextras-runtime-error>
AFRAME.registerComponent('modify-materials', {
init: function() {
...
if (node.name.indexOf('p_TBSPart') !== -1) {
node.material.transparent = true;
//node.material.depthWrite: false;
node.material.blending = THREE.AdditiveBlending;
console.log("blend:" + node.material.blending+";<br/>");
} else {
node.material.transparent = false;
node.material.blending = THREE.NormalBlending;
console.log("b_:" + node.material.blending+";<br/>");
}
..
</a-scene>
Thank you
--
TBSFred
I'm working on some screen space shaders using THREE.JS and GLSL. I'd like to both render a "diffuse" buffer with per object materials/textures AND render an "ID" buffer where each object has a unique color for object edge detection. This would also be really useful for object picking as some of the THREE.JS examples show.
I am already using Scene.overrideMaterial to render normal buffers (and some other cool stuff) but haven't figured out a way to do something similar for an ID buffer. Is there something like this that exists? Seems useful enough that it would be likely. Have other people developed solutions to this problem? Maybe by mapping an objects UUID to a color value?
-----> EDIT
Here is my attempt using this example:
https://threejs.org/examples/webgl_interactive_instances_gpu.html
I'm not concerned with selection at this point, regardless this isn't working. I think I may be setting up onBeforeRender() incorrectly. Any help on this?
Thanks!
initPieces = function(geometry){
...
//set id Color to unique color
//for picking will use setHex(i) method
m.userData.idColor = material.color;
...
m.onBeforeRender = function (){
if(Viewer._scene.overrideMaterial){
if(Viewer._scene.overrideMaterial._name == "id"){
var updateList = [];
var u = scene.overrideMaterial.uniforms;
//picking color in user data
var d = this.userData;
//Is this just equivalent to checking whether this is the picking material?
if(u.idColor){
u.idColor.value = (d.idColor);
// u.needsUpdate = true;
updateList.push("idColor");
}
//Don't understand what this is doing
//Throws "WebGL INVALID_OPERATION: uniformMatrix4fv: location is not from current program"
if (updateList.length){
var materialProperties = renderer.properties.get(material);
if( materialProperties.program){
var gl = renderer.getContext();
var p = materialProperties.program;
gl.useProgram( p.program );
var pu = p.getUniforms();
updateList.forEach(function(name){
pu.setValue( gl, name, u[name].value);
});
}
}
}
}
}
// Doesn't show onBeforeRender set as I expected?
console.log(m);
...
}
...
}
...
function render(scene, camera){
...
scene.overrideMaterial = getMaterial("id");
renderer.render(scene, camera, getTarget("id"));
scene.overrideMaterial = null;
...
}
I am using THREE.JS rev 49.
My program needs to update a mesh by changing it's geometry.
Unfortunately the display does not seem to update.
Here is my code :
// theObject is an array of associatives :
// {
// object1: {mesh: undefined/THREE.mesh, mat: THREE.Material, geo: THREE.Geometry}
// object2: {mesh: undefined/THREE.mesh, mat: THREE.Material, geo: THREE.Geometry}
// ...
// }
// In my function, theObject[i].mesh geometry must change to be theObject[i].geo.
for(i in theObjects) {
//*
if ( theObjects[i].mesh == undefined) {
theObjects[i].mesh = new THREE.Mesh(theObjects[i].geo, theObjects[i].mat);
theObjects[i].mesh.geometry.dynamic = true;
theObjects[i].geo.verticesNeedUpdate = true;
scenePostsurgery.add(theObjects[i].mesh);
} else
theObjects[i].mesh.geometry.vertices = theObjects[i].geo.vertices;
}
Do I have to add something else ?
/Oragon
If I understood correctly you are updating vertices here:
else{
theObjects[i].mesh.geometry.vertices = theObjects[i].geo.vertices;
}
Try to change this code to :
else{
theObjects[i].mesh.geometry.dynamic = true;
theObjects[i].mesh.geometry.vertices = theObjects[i].geo.vertices;
theObjects[i].mesh.geometry.verticesNeedUpdate = true;
}
In if(){} you create a mesh and in else{} you update so dynamic = true and verticesNeedUpdate = true you need to set to mesh which is in else{}.
When changing the entire geometry, I think the easiest way is to remove the old one (scene.remove(geometry), then add the new one (scene.add(geometry)). I think the cost of modifying the mesh and geometry parameters and properties is the same as adding a new one, although adding is much easier and saves a lot of headache!