Creating ParticleCloud instances in THREE.js

Hello, I am new to Threejs and still learning the ropes, so please bear with me if this question sounds basic. I have not had much experience working with particles.

I am trying to figure out how to place points (particles) inside a custom geometry of text using Threejs. My goal is to create an effect where points are instanced within a geometry or text, and then explode outwards to their world positions. Any guidance on how to achieve this would be greatly appreciated.

I am aware of an example at , but I am having trouble understanding what is happening in the render loop.

Answer №1

This serves as a starting point rather than the final solution.

Points can be configured from any type of geometry, whether it's typical geometry or buffer geometry.

https://i.sstatic.net/m0ePz.png

Consider having a THREE.TextGeometry(). In this case, setting points from it would look like this:

textGeo = new THREE.TextGeometry("ABC", {
    font: font,
    size: 2,
    height: 0.25,
    curveSegments: 1,
    bevelEnabled: false
  });
  textGeo.computeBoundingBox();
  textGeo.computeVertexNormals();
  textGeo.center();

  fillWithPoints(textGeo, 1000); // populate our text geometry with 1000 random points

  textGeo.vertices.forEach(function(vertex){
    vertex.startPoint = vertex.clone(); // store initial position of a vertex
    vertex.direction = vertex.clone().normalize(); // set direction
  })
  textPoints = new THREE.Points(textGeo, new THREE.PointsMaterial({color: 0x00ff00, size: 0.1})); // only requiring geometry and THREE.PointsMaterial()
  scene.add(textPoints);

To determine if a random point is within our geometry, we project all faces of the text geometry into 2D (x,y) and check if the point (with its x,y coordinates) falls inside one of the projected triangles (faces):

function isPointInside(point, geometry) {
  var retVal = false;
  for (var i = 0; i < geometry.faces.length; i++) { //loop through faces
    face = geometry.faces[i];
    a = geometry.vertices[face.a];
    b = geometry.vertices[face.b];
    c = geometry.vertices[face.c];
    if (ptInTriangle(point, a, b, c)) {
      var retVal = true;
      break; // exit the loop if the point is in a projected triangle
    }
  }
  return retVal;
}

where

function ptInTriangle(p, p0, p1, p2) {
    // credits: http://jsfiddle.net/PerroAZUL/zdaY8/1/
    var A = 1/2 * (-p1.y * p2.x + p0.y * (-p1.x + p2.x) + p0.x * (p1.y - p2.y) + p1.x * p2.y);
  	var sign = A < 0 ? -1 : 1;
    var s = (p0.y * p2.x - p0.x * p2.y + (p2.y - p0.y) * p.x + (p0.x - p2.x) * p.y) * sign;
    var t = (p0.x * p1.y - p0.y * p1.x + (p0.y - p1.y) * p.x + (p1.x - p0.x) * p.y) * sign;

    return s > 0 && t > 0 && (s + t) < 2 * A * sign;
}

In the animation loop, we utilize the properties of a vertex (startPoint and direction):

textGeo.vertices.forEach(function(vertex){
    vertex.copy(vertex.startPoint).addScaledVector(vertex.direction, 5 + Math.sin(Date.now() * 0.001) * 5);
});
textGeo.verticesNeedUpdate = true; // crucial step after each rendering

jsfiddle example

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

Executing JavaScript upon inserting it into the HTML of a webpage

On a webpage, I have content that is first sent from the server at page load and then frequently updated via AJAX. To handle this, I currently use $( function () {} ) for binding and updating based on the initial server data. However, I also need a way to ...

Pressing a button within an HTML table to retrieve the corresponding value in that row

How can I click a button inside an HTML table, get the value on the same row and pass it to an input field called FullName? Thanks for your help! <td><?php echo $r['signatoryname'] ?></td> <td ...

Unable to utilize the resolved value received from a promise and returned from it

Within the code snippet below, I am retrieving a Table object from mysql/xdevapi. The getSchema() and getTable() methods return objects instead of promises. The purpose of this function is to return a fulfilled Table object that can be used synchronously i ...

What is the process for incorporating an additional input in HTML as you write?

I am looking to create a form with 4 input boxes similar to the layout below: <input type="text" name="txtName" value="Text 1" id="txt" /> <input type="text" name="txtName2" value="Text 2" id="txt" /> <input type="text" name="txtName3" valu ...

Twilio's phone calls are programmed to end after just 2 minutes

For the past week, I've been dealing with a frustrating issue where calls are being automatically disconnected after 2 minutes of recording. Here is the TwiML code: <Response> <Say voice="woman" language="en">Hii Welcome to our App</Sa ...

Utilizing custom i18n blocks for Vue 3 and Vuetify 3 to enhance locale messages

Looking to localize messages separately for each component? Check out this example for Vue 2. But how can it be done for Vue 3 and Vuetify 3? Here's what I've tried: package.json "dependencies": { "@mdi/font": "6.5 ...

Organize a list in AngularJS by breaking it down based on its headers and displaying them in

As someone who is new to angularJs, I am looking to convert an app to angularJs but I have encountered a roadblock: The custom accordion markup I am working with is as follows: <div class="accord_main_wrap" ng-controller="catController"> <di ...

Implementing Flash Messages with jQuery AJAX for Every Click Event

I've been working on integrating Ajax and PHP, successfully fetching data from the database. However, I'm facing an issue where the Ajax response is only displayed in the HTML for the first click. What I actually want is to show a "success/error" ...

Transferring contacts from Gmail, Yahoo, Hotmail, Facebook, and Twitter

Looking to streamline the process of importing contacts from various platforms like gmail, yahoo, hotmail, and facebook using Google API's. Are there any libraries available that can handle this task or should I dive into writing code for all the diff ...

What is the reason for the inclusion of [Circular] in the hash?

Below is the code snippet: db.query(str, arr, function selectCb(error, results, fields) { if (error) { return proceed(false, {errno:'010',message:error.message}, request); } var q = async.queue ...

Guide to accessing a method from a separate file with the help of an event bus

I'm working on CreateEntryStepper.vue where I have a button that needs to call a function in CreateEntryStepperImageUpload.vue when pressed. I understand that event busses need to be used, but I am unsure about what exactly needs to be passed and how ...

Using Angular 2 (Typescript) to export variables from a separate file

Within my Node.js application, I have a file named variables.js. This file contains variables that are used throughout the application in one central location: exports.var1= 'a'; exports.var2= 'b'; By requiring this file in another pa ...

The average duration for each API request is consistently recorded at 21 seconds

It's taking 21 seconds per request for snippet.json and images, causing my widget to load in 42 seconds consistently. That just doesn't seem right. Check out this code snippet below: <script type="text/javascript"> function fetchJSONFil ...

How can you use JavaScript to create hyperlinks for every occurrence of $WORD in a text without altering the original content?

I've hit a bit of a roadblock. I'm currently working with StockTwits data and their API requires linking 'cashtags' (similar to hashtags but using $ instead of #). The input data I have is This is my amazing message with a stock $sym ...

What is the best method to eliminate the 'Unsorted' selection from the sorting options in Material React Table?

Currently utilizing the "material-react-table" library within a React JS project. In alignment with my needs, seeking to eliminate the 'Unsorted' sorting option in Material React Table. Could someone assist me in addressing this? Your help is ...

Is there a way to export a specific portion of a destructuring assignment?

const { a, ...rest } = { a: 1, b: 2, c: 3 }; If I want to export only the rest object in TypeScript, how can I achieve that? ...

Determining the Similarity of jQuery Selectors' Selected Elements

I'm looking for a way to programmatically identify if two jQuery selectors have chosen the exact same element. My goal is to iterate over multiple divs and exclude one of them. This is what I envision: var $rows, $row, $row_to_exclude; $rows ...

Is there a way to determine if content is present within a given div element?

I have created a sample code where clicking on the "Next" button displays the next content of the page. However, if you click one more time on it when there is no content left, an empty page is shown. How can I determine whether content is available insi ...

Customize Link Appearance Depending on Current Section

Looking for a way to dynamically change the color of navigation links based on which section of the page a user is currently viewing? Here's an example setup: <div id="topNav"> <ul> <li><a href="#contact">Contac ...

Tips on maintaining and hiding the vertical scrollbar when a popup is open, alongside the navigation bar positioned at the top of the page

After reviewing this pen, my goal is to create a popup that maintains access to the navigation bar (hence avoiding Bootstrap's Modal). However, I am facing the challenge of keeping the scrollbar visible while preventing scrolling in the background whe ...