Is it possible to duplicate the geometrical shape within the scene?

I'm currently experimenting with Three.js to recreate a flower as part of my learning process for transformations.

Below is the code I used to generate the stamen, stem, and petals. My goal is to have multiple petals stemming from the same Petal mesh, but I am only able to achieve one petal (rotated).

            // stamen
            var stamen_geometry = new THREE.SphereGeometry(0.7,32,32);
            var stamen_material = new THREE.MeshBasicMaterial( { color: 0x00fff0 } );
            stamen = new THREE.Mesh( stamen_geometry, stamen_material );
            stamen.position.set(0,2,0);
            scene.add( stamen );

            // stem
            var stem_geometry = new THREE.CylinderGeometry(0.3, 0.3, 3);
            var stem_material = new THREE.MeshBasicMaterial( { color: 0x00ff00 } );
            stem = new THREE.Mesh( stem_geometry, stem_material );
            scene.add( stem );

            
            var pivot = new THREE.Object3D();      
      

            //petal
            var petal_geometry = new THREE.CylinderGeometry(0, 0.3, 3);
            var petal_material = new THREE.MeshBasicMaterial({ color: 0xff0000 });
            petal = new THREE.Mesh( petal_geometry, petal_material );
            petal.rotation.z = 90 * Math.PI/180;
            petal.position.set(1.5,2,0);
            pivot.add( petal );
            
            for (var i = 0; i < 10; i++) {
                pivot.rotation.y = i*45*Math.PI/180;
                scene.add (pivot);
            }

If anyone has any suggestions or tips on how to achieve multiple petals from the same mesh, please let me know. Thanks!

Answer №1

scene.add (pivot);

To ensure uniqueness, you must generate a new instance of THREE.Mesh for each petal. Currently, the same 3D object is being repeatedly added to the scene.

Here's the simplified code:

var petal_geometry = new THREE.CylinderGeometry(0, 0.3, 3);
var petal_material = new THREE.MeshBasicMaterial({ color: 0xff0000 });

for (var i = 0; i < 10; i++) {
    var petal = new THREE.Mesh(petal_geometry, petal_material);
    petal.rotation.y = i*45*Math.PI/180;
    scene.add(petal);
}

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

Guide to generating dynamic multiple fields in AngularJS

I have an AngularJS app where I am adding multiple dynamic fields within a form. Here is how I have tried to do it: The JavaScript file looks like this: $scope.commonData = [{ 'a': '', 'b': '', }]; $scope. ...

Is it possible to integrate a jQuery function within PHP code when encountering a "is not defined"

I am looking to implement the jQuery-confirm dialog in place of using a JavaScript alert generated by PHP. However, when I execute the following code, I encounter the following error message: $ is not defined echo "<script>$.alert({title: ' ...

angularjs currency conversion tool

Is it possible to choose only 3-4 currency values from a drop-down list, where the selected value will determine the base URL for fetching JSON data? For instance, if I select USD as the first value, the JSON data should be retrieved from . ...

What are the best practices for establishing a secure SignalR client connection?

While tackling this issue may not be solely related to SignalR, it's more about approaching it in the most efficient way. In C#, creating a singleton of a shared object is achievable by making it static and utilizing a lock to prevent multiple threads ...

Get the image from the express route

Currently, I am running an express server with the following route: exports.getUserFile = function (req, resp) { let filePath = path.join(__dirname, 'storage', req.params.fileName); resp.download(filePath); }); } Within my web app ...

Is it possible to modify the CSS styles for a specific portion of an input value?

Currently, I am in the process of developing a form using React.js where a specific input must match the label written above it. Here is an example: https://i.stack.imgur.com/S2wOV.png If there happens to be a typo, the text should turn red like this: h ...

The command "npm run build:css " is not functioning properly, but when I execute the script independently, it works fine

Recently, while working on a program using npm script on my Mac system, I encountered some issues. Despite having installed node-sass globally, running "npm run build:css" did not work as expected. The content of my package.json file can be viewed here. Th ...

I have a specific resolution in mind where I want to run jQuery code during window scrolling

I have a jQuery code that I want to run only when the resolution is greater than 950px. If not, I risk losing the responsive logo. My goal is to create a navigation bar similar to the one on the Lenovo homepage. $(document).ready(function(){ //left ...

Having trouble implementing String.prototype in my factory

Currently, I am working on incorporating a function mentioned in this answer. String.prototype.supplant = function (o) { return this.replace(/{([^{}]*)}/g, function (a, b) { var r = o[b]; return typeof r === 's ...

How can you deduce the type from a different property in Typescript?

I have encountered obstacles in my development process and need assistance overcoming them. Currently, I am trying to configure TObject.props to only accept 'href' or 'download' if the condition TObject.name = 'a' is met, and ...

What is the method for invoking an express middleware function that triggers a file download?

Currently, I am delving into Express and experimenting with middleware. My goal is to initiate a file download when accessing the root route while sending out a "Hello World" message. My attempts so far have been: function initiateDownload(req, res, next) ...

Ways to attach dynamic methods within a v-for loop

I am currently working on a situation where I need to dynamically assign methods to elements that are being generated by a template using a v-for loop. These methods will be assigned based on the value of a particular variable, specifically the 'btn.m ...

The JAVASCRIPT function fails to interpret echoed HTML Element generated by PHP

My HTML elements are generated from PHP as most content is fetched from the database. For example: <?php ... while($row = $sqlQry->fetch_object()) { echo "<li class='lstOption' id='opt$row->name' data-value ...

AngularJS NG-Grid displaying incorrect value for select cell

I'm working on creating a table with a column that needs to be selected based on a value received from the server. The server sends me 4 as the value, but the first option is always selected instead. $scope.lotteryOptions = { data: 'myDa ...

Getting the dimensions of an image when clicking on a link

Trying to retrieve width and height of an image from this link. <a id="CloudThumb_id_1" class="cloud-zoom-gallery" rel="useZoom: 'zoom1', smallImage: 'http://www.example.com/598441_l2.jpg'" onclick="return theFunction();" href="http ...

Steps to deactivate a JavaScript function once the page has undergone a Page.IsPostBack event

My current setup involves a simple div with the display set to none. Upon page load, I use $("#MyDiv").show(); to display the div after a delay, allowing users to enter information into the form and submit it using an asp.net button. After submitting the ...

Encountering a Zone.js error when trying to load an Angular 7 app using ng serve, preventing the application from loading

Scenario: Yesterday, I decided to upgrade my Angular app from version 5.2.9 to 6 and thought it would be a good idea to go all the way to 7 while I was at it. It ended up taking the whole day and required numerous changes to multiple files, mostly due to R ...

The columnFilter plugin in Datatables is failing to initialize

I have a pre-existing table that needs to be customized and initialized properly. <table id="currencies-table" class="table table-striped table-bordered table-hover form-data-table"> <thead> <tr> <th style="width: 10px;" ...

Listen up, Javascript keyboard input recorder

I am aiming for the search bar to pop up when you type. The code below is functioning, but I am facing an issue where the search bar pops up even when typing in a different input field, such as the comment input. Is it feasible for the search bar not to ...

A guide on installing a npm dependency module from a local registry domain

I have successfully published a module on my own custom registry domain, located at , and I am able to publish updates to the module there. Unfortunately, I am encountering an issue with dependencies within my published module. For example: "dependencies" ...