Is there a way to vary the length of vertices in three.js meshes using randomization?

Recently, I came across a mesmerizing mesh randomization effect in three.js that I really admired. I wanted to replicate it in my own codepen, so I took inspiration from the original source and made some adjustments (refer to line 56 of my codepen). However, despite my efforts, the desired distorted edges didn't come through, and instead, the code broke.

Specifically, this part seems to be causing the issue:

var noise = randomRange(1,5);
for(var i=0; i<Ico.vertices.length; i++){
      var v = Ico.vertices[i];
      v.x += -noise/2 + Math.random()*noise;
     v.y += -noise/2 + Math.random()*noise;
     v.z += -noise/2 + Math.random()*noise;
   };

I'm at a loss here. Have I overlooked something crucial? Any assistance or pointers would be highly valued.

Answer №1

Here is a recommended approach to solving your issue:

function generateRandomRange(min, max) {
    return Math.floor(Math.random() * (max - min + 1) + min);
}

// Geometry

var shape = new THREE.IcosahedronGeometry(25, 3);

var noiseLevel = generateRandomRange(1, 5);
for(var j = 0; j < shape.vertices.length; j++){
  var vertex = shape.vertices[j];
  vertex.x += -noiseLevel/2 + Math.random() * noiseLevel;
  vertex.y += -noiseLevel/2 + Math.random() * noiseLevel;
  vertex.z += -noiseLevel/2 + Math.random() * noiseLevel;
};

// Meshes

var polyhedron = new THREE.Mesh(shape, cyanMaterial);
polyhedron.rotation.z = 0.5;

Answer №2

Instead of

var Ico = new THREE.Mesh(
new THREE.IcosahedronGeometry(25,0), cyanMat);
Ico.rotation.z = 0.5;

replace it with

function randomRange(min,max) {
    return Math.floor(Math.random()*(max-min+1)+min);
}

var icoGeom = new THREE.IcosahedronGeometry(25,1);
var noise = randomRange(5,10);
console.log(icoGeom);
icoGeom.vertices.forEach(function(vertex){
  console.log(vertex);
  vertex.x += Math.random()*noise - noise/2;
  vertex.y += Math.random()*noise - noise/2;
  vertex.z += Math.random()*noise - noise/2;
});

also, don't forget to include the randomRange() function in your code.

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

fetching indexeddb information using the equivalent of a "WHERE IN (a,b)" query

I've been working on transitioning from websql to indexeddb, but I'm struggling to recreate the SELECT query: "SELECT * FROM tableA WHERE cid ='"+cid+"' AND hid IN("+hid+",1) ORDER BY hid DESC LIMIT 1"; function getMyData(e) { var ...

Ajax handling all tasks except for adding HTML elements

Having an issue with my basic "Load More on Scroll" AJAX function. The console is showing that the HTML is being sent back from the request, but for some reason, nothing is being rendered on the page. I must be missing something really simple here. $(wi ...

Steps to store radio button selections in local storage

I have a quiz that includes radio buttons, and I need to save the answers on my local storage. However, I am currently stuck and unsure of what else to do. I am learning, so please don't be too hard on me. Thank you! Here is the code I have written s ...

How to utilize JS variables for filtering an array in EJS?

Is there a way to filter my user array based on the "username" variable in JavaScript? On the server side: var users = data.filter(u => u.id.startsWith('user_')).map(u => u.value); // [{"username": "arin2115", "som ...

How to access selection range styles using JavaScript

It is common knowledge that we can retrieve the selection of text in JavaScript using the following method: var range = window.getSelection (); However, how can we obtain the style of this selection? For example, when I select bolded text or italicized ...

In Vue.js, the 'select' option is consistently set as the default choice

Encountering an unusual issue with a select field in vue.js. Desired outcome: a select field with no default option selected. In regular html, this is easily achievable. Here is the jsfiddle Link: https://jsfiddle.net/odsf3awr/ <select id="test" s ...

Efficiently handling multiple responses in Express.js per single request

Currently exploring Node.js and working on a bot utilizing Dialogflow. I am aiming to establish a straightforward process: Step 1: Dialogflow sends a POST request with parameter param1 Step 2: My application replies with a waiting message (e.g., "your re ...

React application failing to recognize environment variables (create-react-app)

I've been attempting to retrieve the environment variables within my React App, but for some reason, they're not being recognized. console.log(process.env) https://i.sstatic.net/Q71c3.jpg All of the variables are stored in a file named .env.dev ...

A nested DragSource component within a parent DragSource component in React DnD

In my project, I am working with a tree structure that consists of a root "Tree" component containing a list of root "TreeNodes". Each TreeNode can have an arbitrary number of children. Within the render method of the TreeNode component, I iterate over th ...

Asserting within a specific condition's scope in TypeScript

I am facing a similar situation, type Field1Type = { a: string; } type Field2Type = { b: string; c: number; } type ObjType = { field: Field1Type | Field2Type } const field = { b: "" c: 0 } const obj = { field } as ObjType i ...

Having Trouble with window.location in Chrome Browser

In my code, there is a JavaScript function that utilizes window.location. Surprisingly, it runs smoothly in Firefox and Internet Explorer, but encounters issues while running on Chrome. I have tried testing it on both Ubuntu Hardy and Windows Vista opera ...

Updating the state of an object within a mapping function

I've been struggling with this issue for two days now. Despite my efforts to find a solution online, I am still stuck and starting to believe that I might be missing something. The main functionality of the app is to click a button and watch an apple ...

Infuse the theme into the sx prop of MUI 5

The code snippet above was originally written using MUI v4: const useStyles = makeStyles(theme => ({ toolbarMargin: { ...theme.mixins.toolbar } })) To update this code to MUI v5 and utilize the sx prop, I attempted the following implementation: ...

Using 'if' conditions in Reactjs: A step-by-step guide

Working with Reactjs in the nextjs framework, I have received user data that includes the "category name (cat_name)" selected by the user. Now, I need to display that category in a dropdown menu. How can I achieve this? The current code snippet showcases ...

Exploring different ways to navigate JSON data dynamically and efficiently modify it

I have a JSON data structure resembling a map. var map = { level1 : { x : {name:'level1 x' , }, y : {name:'level1 y'} }, level2 : { x : {name:'level2 x&a ...

methods for controlling data in Datatables

Utilizing Datatables along with Vue.js to showcase data in table form. Below is the code snippet: computed: { job_table: function () { return { title: this.$t('commons.users'), headers:[ { text: &qu ...

What is the method for determining if a given point falls within a 3-dimensional cube?

I am currently seeking a method to determine whether a location is situated inside a rotated cube. To provide some context, I have access to the coordinates (x,y,z), rotation values (x,y,z), and dimensions (x,y,z). I am working with Javascript for this pr ...

Achieving consistent scroll position when utilizing the history.js library to navigate back with the click of a button

I need help implementing a feature that allows users to return to the same position on a webpage when clicking the back button. A common example is on ecommerce sites where if you scroll down, click on a product, then go back, you should be taken back to t ...

When using Lockdown.js with NPM, I encounter a blank file being returned

When using Lockdown.js for NPM, I encountered an issue where the command to generate the lockdown file resulted in an empty file. Here are the links for the dependencies: NPM lockdown git Here is a snippet from my package.json: { "name": "nw", "pri ...

"Exploring Mocha and Chai: Tips for Generating a Comprehensive List of Errors in Test Cases

I am currently working on a unit test to validate a list of elements. Example: arr = [ { element : "aaa", validation : false }, { element: "bbbb", validation: true }, { element: "ccc", validation: false } While running my unit test, I encountered ...