Glowing beams of light emitted by point light in three.js

Is there a way to visualize light rays from a point light in a Three.js scene without affecting the entire scene with fog color?

 var width = $('#g_pre_emo').width();

 var scene = new THREE.Scene();
 scene.fog = new THREE.Fog(0xffff00, 0, 10);
 var camera = new THREE.PerspectiveCamera(50, width / 500, 0.1, 1000);
 var renderer = new THREE.WebGLRenderer({antialias: false});
 renderer.setSize(width, 500);

 $('#g_pre_emo').append(renderer.domElement);


 var intensity = 2.5;
 var distance = 5;
 var decay = 2.0;

 var light = new THREE.AmbientLight(0x404040); // soft white light
 scene.add(light);

renderer.shadowMap.Enabled = true;
renderer.setClearColor(0xffffff, 1);
var happyGroup = new THREE.Object3D();
var sadGroup = new THREE.Object3D();
var angryGroup = new THREE.Object3D();
var relaxedGroup = new THREE.Object3D();

scene.add(happyGroup);
scene.add(sadGroup);
scene.add(angryGroup);
scene.add(relaxedGroup);
var c1 = 0xffff00;


var sphere = new THREE.SphereGeometry(0.25, 16, 8);
light1 = new THREE.PointLight(c1, intensity, distance, decay);
light1.add(new THREE.Mesh(sphere, new THREE.MeshBasicMaterial({color: c1})));
scene.add(light1);

happyGroup.position.set(-3, 3, 0);
sadGroup.position.set(3, 3, 0);
angryGroup.position.set(-3, -3, 0);
relaxedGroup.position.set(3, -3, 0);


var happyGeometry = new THREE.SphereGeometry(1, 50, 50);
var happyMaterial = new THREE.MeshNormalMaterial();
var happySphere = new THREE.Mesh(happyGeometry, happyMaterial);
scene.add(happySphere);
happyGroup.add(happySphere);

var sadGeometry = new THREE.SphereGeometry(1, 50, 50);
var sadMaterial = new THREE.MeshNormalMaterial();
var sadSphere = new THREE.Mesh(sadGeometry, sadMaterial);
scene.add(sadSphere);
sadGroup.add(sadSphere);

var angryGeometry = new THREE.SphereGeometry(1, 50, 50);
var angryMaterial = new THREE.MeshNormalMaterial();
var angrySphere = new THREE.Mesh(angryGeometry, angryMaterial);
scene.add(angrySphere);
angryGroup.add(angrySphere);

var relaxedGeometry = new THREE.SphereGeometry(1, 50, 50);
var relaxedMaterial = new THREE.MeshNormalMaterial();
var relaxedSphere = new THREE.Mesh(relaxedGeometry, relaxedMaterial);
scene.add(relaxedSphere);
relaxedGroup.add(relaxedSphere);


renderer.gammaInput = true;
renderer.gammaOutput = true;
camera.position.z = 15;

function render() {
    renderer.render(scene, camera);
}
 ;
render();
animate();
function animate() {
    requestAnimationFrame(animate);

}

If I execute the above code, I am unable to observe any fog in the scene. You can find the code snippet here: https://jsfiddle.net/bqv5ynbo/1/

Answer №1

You may want to consider utilizing the VolumetricSpotlightMaterial developed by Jerome Etienne.

In my experience, it performed effectively with Three.js r71, although I have not tested it with more recent updates.

Check out this example for usage demonstration.

Answer №2

The phenomenon of 'Light Rays' does not occur naturally.
Usually, light rays are generated through the use of sophisticated pixel shaders (such as stencil/shadow projection over illuminated areas or volumetric fog) or in simpler scenarios by creating basic alpha-blended polygons with 'ray' textures.

A point light is essentially a type of illumination that does not involve directional computations during its shading process. Instead, calculations are based solely on distance.

Best of luck!

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Troubleshooting problem with updating a record in the MongoDB using Node.js and

Currently, I am developing a REST API using Node, Express, and MongoDB. I have successfully implemented fetch, create, and delete functions but facing issues with the update function. Every time I attempt to test it using Postman, the code hangs, the serve ...

The proper way to retrieve data using getServerSideProps

Encountering an issue with Next.js: Upon reaching pages/users, the following error is displayed: ./node_modules/mongodb/lib/cmap/auth/gssapi.js:4:0 Module not found: Can't resolve 'dns' Import trace for requested module: ./node_modules/mon ...

Fix issue with nested form in Rails 3.0.9 where remove_fields and add field link functionalities are not functioning properly

I've been watching Ryan Bates' nested_forms episodes 1 & 2 on RailsCasts, successfully implementing the functionality in one project without any issues. However, in a new project using the same reference, the remove and add field functionalit ...

Arrange the items in an array that are equal to a single object within the array

My Fetch request is returning an array of objects structured like this: [ {"id":11,"name":"test1","day":"MON","level":2}, {"id":13,"name":"test2","day" ...

Component built in React

I'm a newcomer to Jest and JavaScript in general. Recently, I've been working with a js code that involves various components, and now I need to incorporate a Log Out option for the website. Although I have a Log Out component ready, I'm un ...

Automatically close the popup each time it is displayed (using jQuery/JavaScript)

I am having issues with auto-closing my popup each time I open it. The current code I have only closes the popup the first time, requiring me to refresh the browser in order to auto-close it again. Can someone please assist me in writing a new code that ...

What could be causing the value of response.name to be undefined in this situation?

Despite extensively searching through various StackOverflow threads, none of the suggested solutions seemed to work for my specific scenario. Upon analyzing the response using console.log(response), I received an "Object Object" message. I am puzzled as to ...

TensorflowJS Error: The property 'fetch' cannot be read as it is undefined

I am working on an Angular 7 application and attempting to load the mobilenet model by following the instructions in this example. To do this, I first installed tensorflowjs using the command npm install @tensorflow/tfjs (based on the steps provided in th ...

Struggling with jquery .append issue

Here is the HTML code snippet: <div class="expand"> <div class="title" id="SectionTitle2">Academics</div> <input type="button" onclick="showTitleForm('2');" name="editTitle2" value="Edit Title"> <form id="titleForm2" m ...

Evolving from Angular 1.x to Angular 5: The Transition Journey

Seeking guidance on migrating a large web-app from Angular 1.4 to Angular 5. I have scoured through various tutorials but haven't found the right answer yet. If anyone has already achieved this milestone, please share your insights. Currently, I have ...

What could be the reason for the variable not being defined?

Initially, I have the following function: $.getJSON( 'getTerminalinsideCircle.json', { centerLatitude: adressMarker.getPosition().lat(), centerLongitude: ...

"Error: Discord Js encounters an issue trying to access the titles property of an

Having trouble with a random number generator in my discord bot. Whenever someone enters +nhr, it may work or display an error message in the console: TypeError: Cannot read property 'titles' of undefined and Unhandled promise rejection. Thi ...

Exploring the differences between two CSS style attributes using JQuery

Can someone help me with a quick question about CSS? I need to compare two style attributes, specifically margin-left. If the margin-left is less than 500px, I don't want to make any changes. However, if it's greater than 500px, I want to add ano ...

Retrieve solely the text content from a JavaScript object

Is there a way to extract only the values associated with each key in the following object? const params = [{"title":"How to code","author":"samuel","category":"categoery","body":"this is the body"}] I'm struggling to figure out how to achieve this. ...

How to prevent v-menu from overlapping a navbar in Vue.js

Exploring the examples on the main page of Vuetify, we come across the v-menu component showcased in detail. Check it out here: https://vuetifyjs.com/en/components/menus/#accessibility If you activate any of the buttons to open the v-menu and then scroll ...

Switching Primary Menu Selection When Scrolling Through Various Menu Options

I need help with changing the active class of links on my single page scrolling website. I found some useful code snippets for smooth scrolling and updating the active class on scroll or click events: $(document).ready(function () { $(document).on(" ...

Is Chrome launching a list of links in separate windows instead of tabs?

As I create a customized start page for myself, I've encountered a challenge. I want to include a link above all sections that, when clicked, will open all pages in a new tab. However, I'm facing an issue where only the first link opens in a new ...

Is there a way to set the content to be hidden by default in Jquery?

Can anyone advise on how to modify the provided code snippet, sourced from (http://www.w3schools.com/jquery/tryit.asp?filename=tryjquery_hide_show), so that the element remains hidden by default? <!DOCTYPE html> <html> <head> <scrip ...

Creating new rows dynamically with jQuery

In my current setup, there is a table with one row and two columns - a textbox and a button: $(function() { var i = 1; $('#add').click(function() { i++; $('#dyn').append('<tr id="row' + i + '">&l ...

What is the best way to eliminate excess space on the right side in Bootstrap 4 while in mobile view?

I'm struggling to understand why this layout is not responsive on mobile devices, triggering a bottom scroll bar at around 616px. I'm looking for a solution to hide the scroll bar at least until 414px for iPhones and other smartphones. I've ...