Creating random objects that span multiple directions in three.js involves utilizing the Math.random() function to generate

My goal is to generate a sphere in Three.js by constructing multiple cylinders and then rotating them in various directions. I attempted to iterate through 2 pie settings to adjust the rotation axis, but unfortunately, it did not yield the desired outcome. Can anyone provide guidance on how to proceed?

var cylinder;

var cylinderMaterial    = new THREE.MeshPhongMaterial(
    { color: 0x5500DD,
        specular: 0xD1F5FD,
        shininess: 100 } );

var cylinderGeo = new THREE.CylinderGeometry( 3, 3, 500, 32 );

var cylinder = new THREE.Mesh( cylinderGeo, cylinderMaterial );


var untransformedCylinder = cylinder.clone();
console.log("Untransformed cylinder matrix:")
console.log(untransformedCylinder.matrix); 
scene.add(untransformedCylinder);


var maxCorner = new THREE.Vector3(  1, 1, 1 );
var minCorner = new THREE.Vector3( -1, -1, -1 );

var cylAxis = new THREE.Vector3().subVectors( maxCorner, minCorner );


cylAxis.normalize();

var theta = Math.acos( cylAxis.y );

var rotationAxis = new THREE.Vector3(1, 0, 0);

rotationAxis.normalize();

cylinder.matrixAutoUpdate = false;

cylinder.matrix.makeRotationAxis( rotationAxis, theta );

console.log("Theta: " + theta);
console.log("  cos: " + Math.cos(theta));
console.log("  sin: " + Math.sin(theta));

console.log("Transformed cylinder matrix:")
console.log(cylinder.matrix); 

scene.add( cylinder );

Answer №1

One important step is to include the line

cylinder.setRotationFromMatrix(cylinder.matrix);
directly after calling
cylinder.matrix.makeRotationAxis( rotationAxis, theta );

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

Having trouble retrieving text using getText() or getAttribute("innerHTML")

getAttribute but struggling to retrieve the text associated with each combobox. I need to access the text content of every combobox. <small> <input type="checkbox" checked="checked" value="on" name="irOperations[0]"/> Account Activity < ...

What is the best way to trigger an API call using AJAX whenever the page loads and at regular intervals using `setInterval()` function?

New to coding and seeking guidance: I have a basic AJAX feature in my project that triggers a GET API request every 3 seconds: <script> $(document).ready( function() { setInterval(function() { $.get(&apos ...

Activate a jQuery plugin on elements that are dynamically generated

Here is the code snippet I am using: $(".address-geocomplete").geocomplete({ country: "US", details: ".details", detailsAttribute: "data-geo" }); When these elements are loaded with the document, everything works smoothly. However, there seem ...

What steps can be taken to stop the update function from inserting data into MongoDB from a Meteor application

Within my Meteor method, I have implemented a function to check for existing documents. If the document is not found, it gets inserted into the database. However, if it is found during a second attempt, the document is updated and a new one is also inserte ...

How can you retrieve the `categoryIds` key within an object that holds an array of mongodb's `ObjectId(s)` as its value?

In my Node.js code, I have the following: var getQuestionsByUserId = function (config) { var query = { _id: ObjectId(String(config.userId)) }; var projection = { categoryIds: true, _id: false }; var respondWithCategories = function (error, doc ...

What methods do platforms such as SketchFab, Pix4D, and DroneDeploy use to efficiently transfer large .OBJ files over the network to achieve quick rendering speeds?

There are various 3D viewer services such as SketchFab, Pix4D, DroneDeploy, that can efficiently send large 3D models over a network with fast rendering times. For instance, downloading a 70mb model from Pix4D directly can take a significant amount of ti ...

The filter function is failing to return the initial element of the array, contradicting what is displayed in the console.log output

When given two arrays of integers, I aim to create a new array containing only the unique values from both input arrays. There are many methods for achieving this, but I'm curious as to why my current approach is not producing the desired result. cons ...

three.js BoundingBoxHelper that excludes diagonal lines

When I look at my BoundingBox, I see triangles inside the box that I don't want to show. How can I remove those lines? Thanks. var bbox = new THREE.BoundingBoxHelper(object3d, new THREE.Color(0xFF9900)); bbox.visible = false; bbox.name = ...

Exploring Nested Event Listeners in NodeJS

I'm confused about why passing an argument to the event emitter with nested event listeners streams all values. Does it have to pass through the upper level 'join' listener? Is variable information stored somewhere? var events = require(&ap ...

The JQuery script is not producing any results

After integrating a script into my website template, it is not functioning as expected. I suspect there may be a conflict with JavaScript, but upon inspecting with firebug, I am unable to identify any abnormalities. Here is the link for reference: Link ...

Remove a value from Javascript, leaving it as undefined (null)

Encountering an issue when attempting to remove an element fetched from my API call, as the deleted element is leaving behind an "undefined" value. var nutrientsLink = "http://api.nal.usda.gov/ndb/list?format=json&lt=n&sort=n&api_key=JX ...

Exclusive Modal Pop-Up for First-Time Visitors - Utilizing Bootstrap, PHP, and JavaScript

Seeking assistance from the community as I have exhausted my online research efforts. I am currently working on implementing a welcome bootstrap modal in a php environment. Despite being new to php and js, I have managed to make it pop up only once using ...

Guide on toggling the expansion and collapse of antd TreeSelect items when a button is clicked

I recently started using the antd library to develop my website. I have successfully implemented the TreeSelect component to display nested options. However, I am currently facing an issue: I am attempting to expand and collapse TreeSelect items when a b ...

How to designate a default selection value using local array data source in Select2.js version 4.0?

If I am using the select2.js v4 plugin with a local array data source, how can I set the default selected value? For instance, consider the following code: var data_names = [{ id: 0, text: "Henri", }, { id: 1, text: "John", }, { id: 2, text: ...

The Ultimate Guide to Automatically Updating JSON File URLs

I am currently working on a project where I need to retrieve data from a URL using the $.getJSON method. I am interested in finding a way to continuously update this data and replace it in the HTML without needing to manually refresh the page. Although I h ...

Discover how to efficiently load and display a JSON array or object using JavaScript

I am new to learning about json and d3, having just started a few hours ago. While I have basic knowledge of javascript, I need help with loading a json file and displaying all the arrays and objects on the console using d3. I tried to do it myself but unf ...

Having trouble with the page redirection issue? Here's how you can troubleshoot and resolve

My goal is to restrict access to both the user home and admin dashboard, allowing access only when logged in. Otherwise, I want to redirect users to the login or admin login page. import { NextResponse } from 'next/server' import { NextRequest } ...

Tips for creating multiple files using nodejs and express

I am currently working on developing a personalized code editor that consists of 3 textareas: html, css, and javascript. The objective is to save the data from each textarea into individual files. With the help of express and nodejs, I have successfully m ...

what is the method to extract the value of a JSON object nested within another JSON object

Can someone please assist me? "_attachments": { "kiran.jpg": { "content_type": "image/jpeg", "revpos": 6, "digest": "md5-mEsoX4ljN1iJlF2bX1Lw2g==", "length": 4601, "stub": true } } I ...

React.js JSX side by side components

When running the code below, I encounter an exception related to parsing in React. How can I resolve this error where JSX elements must be wrapped in an enclosing tag? Error: Line 48: Parsing error: Adjacent JSX elements must be wrapped in an enclosin ...