Changing Shapes - Three.Js

I've been working on a code that allows users to customize dimensions and see it displayed using new THREE.Geometry(). Although this might not be relevant to everyone, I'm still new to Three.js and trying to figure things out.

The main issue I'm facing is:

  • I am struggling to incorporate geometry.morphTargets into my code. It seems like I'm not quite sure how to use it effectively.

Here's the snippet of my code:

//custom Object height and width
    customHeightWidth(customWidth, customHeight);

       function customHeightWidth(width, height){

        material = new THREE.MeshBasicMaterial({
        map: THREE.ImageUtils.loadTexture(uploadedFile),
        side: THREE.DoubleSide,
        overdraw: true,
        wireframe: false
        });
//objects
combined = new THREE.PlaneGeometry(width, height, 30, 10);

geometry = new THREE.Geometry();

geometry.name: "target1", vertices.push( new THREE.Vector3( -(width), height, 0 ) );
geometry.name: "target2", vertices.push( new THREE.Vector3( -(width), -(height), 0 ) );
geometry.name: "target3", vertices.push( new THREE.Vector3( width, -(height), 0 ) );

geometry.computeBoundingSphere();
geometry.faces.push( new THREE.Face3( 0, 1, 2 ) );

geometry1 = new THREE.Geometry();

geometry1.vertices.push( new THREE.Vector3( width, height, 0 ) );
geometry1.vertices.push( new THREE.Vector3( -(width), height, 0 ) );
geometry1.vertices.push( new THREE.Vector3( width, -(height), 0 ) );
geometry1.computeBoundingSphere();

geometry1.faces.push( new THREE.Face3( 0, 1, 2 ) );

// 1st box
var mesh1 = new THREE.Mesh(geometry);
var mesh2 = new THREE.Mesh(geometry1);

//activating the meshs
THREE.GeometryUtils.merge(combined, mesh1);
THREE.GeometryUtils.merge(combined, mesh2);


mesh = new THREE.Mesh(combined, material);
this.scene.add(mesh);
};
}

var animate = function() {
requestAnimationFrame(animate);
//mesh.rotation.x += 0.01; 
//mesh.rotation.y -= 0.006;
renderer.render(scene, camera);
}

init();
animate();

Answer №1

I suggest taking a closer look at how the JSONLoader is used to load morph targets into the Geometry.morphTargets array within the Geometry class.

function parseMorphing( scale ) {

    if ( json.morphTargets !== undefined ) {

        var i, l, v, vl, dstVertices, srcVertices;

        for ( i = 0, l = json.morphTargets.length; i < l; i ++ ) {

            geometry.morphTargets[ i ] = {};
            geometry.morphTargets[ i ].name = json.morphTargets[ i ].name;
            geometry.morphTargets[ i ].vertices = [];

            dstVertices = geometry.morphTargets[ i ].vertices;
            srcVertices = json.morphTargets [ i ].vertices;

            for( v = 0, vl = srcVertices.length; v < vl; v += 3 ) {

                var vertex = new THREE.Vector3();
                vertex.x = srcVertices[ v ] * scale;
                vertex.y = srcVertices[ v + 1 ] * scale;
                vertex.z = srcVertices[ v + 2 ] * scale;

                dstVertices.push( vertex );

            }

        }

    }

Answer №2

//Exploring morphTargets

for(i=0;i<geometry.morphTargets.length;i++) {
morph = geometry.morphTargets[i].vertices;
return morph;
}

//Loading the morphTargets

loadMorphTargets = function (morph) {
        if (morph !== undefined) {
            var i, l, v, vl, dstVertices, srcVertices;
            //for (i = 0, l = morphTargets.length; i < l; i++) {
            geometry.morphTargets[i] = {};
            geometry.morphTargets[i].name = morph.name;
            geometry.morphTargets[i].vertices = [];
            var dstVertices = geometry.morphTargets[i].vertices;
            var srcVertices = morph.vertices;
            for (v = 0, vl = srcVertices.length; v < vl; v += 3) {
                var vertex = new THREE.Vector3();
                vertex.x = srcVertices[ v ];
                vertex.y = srcVertices[ v + 1 ];
                vertex.z = srcVertices[ v + 2 ];
                dstVertices.push(vertex);
            }
        }
    };

This information may come in handy.

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

Tips for increasing the number of inputs within a form using <script> elements

I am currently working on a form within the script tags and I would like to include additional input fields before submitting. However, the submit button seems to be malfunctioning and I suspect that there may be an issue with how I am accessing it in my c ...

The MUI persistent drawer navigation bar becomes dysfunctional when accessing a specific route

Exploring the MUI library for the first time, I successfully created a navigation bar that functions properly for one route (Dashboard). However, when attempting to implement it on the candidate route, it collapses as shown in this Screengrab of collapsed ...

Retrieve data from API using ReactJS and passing parameters to the request

I am trying to retrieve information from the API based on the data I am sending, but encountering two issues: Initially, I hardcoded the information being sent to the API (year and term) as it should return the data I am trying to store, however, it is ...

Can you explain the slow parameter feature in Mocha?

While configuring mochaOpts in Protractor, one of the parameters we define is 'slow'. I'm unsure of the purpose of this parameter. I attempted adjusting its value but did not observe any impact on the test execution time. mochaOpts: { re ...

How can I prevent anchors from performing any action when clicked in React?

My dilemma involves this HTML element: <a href onClick={() => fields.push()}>Add Email</a> The purpose of the href attribute is to apply Bootstrap styles for link styling (color, cursor). The issue arises when clicking on the element caus ...

Cannot locate module using absolute paths in React Native with Typescript

I recently initiated a new project and am currently in the process of setting up an absolute path by referencing this informative article: https://medium.com/geekculture/making-life-easier-with-... Despite closely following the steps outlined, I'm en ...

Identifying whether a Property in an Object is another Object

I'm planning to utilize JSON for stringifying the value of a property within an object, which will then be stored as a Tag on Google Calendar using Google Apps Script. The value in question is actually a double-nested Object (look at extraAttributes w ...

Looking for guidance on integrating Forms with VueX and script setup? Are you utilizing v-model in your implementation?

UPDATE: I'm contemplating whether or not to abandon VueX entirely due to its outdated status, with Pinia being the preferred choice nowadays. Can someone confirm this? https://stackblitz.com/edit/vue-script-setup-with-vuex-hmrk5d?file=src/store.ts ...

"Automate the process of manual content duplication with JavaScript's for each replacement

Seeking a solution to automate the selection process without writing individual JS scripts for every input. For instance, having 10 double inputs (total of 20 inputs) and utilizing the each() function or other methods by only declaring selectors. Find th ...

Mastering the art of using either promises or callbacks properly

Attempting to retrieve the result of a variable after completing all asynchronous processes in a function. I've discovered that I need to use promises for this, so I spent today learning about them. I've implemented promises in my function and c ...

Is there a way to combine arrays with varying input values?

I am facing an issue where I have multiple text fields with the same name and class but with different values, each accompanied by a button. I am using Javascript to extract the value from each field by clicking on its respective button. I am able to store ...

assigning a value to a JavaScript variable using server-side code

Seeking guidance on setting a JavaScript value from backend code. Here is the current JS code snippet: gts.push(['google_base_offer_id', 'ITEM_PRODUCT_SEARCH_ID']); My attempt involves using this line of code: gts.push(['google ...

Creating a circular shape with a radius that can be adjusted

I'm trying to create an expanding bubble-like circle of various colors when clicked on a blank page. The circle should continue increasing in size each time it is clicked, only stopping when the mouse click is released. I am looking to use HTML, CSS, ...

Make sure all of the inner tags are properly contained within their respective containers

In my explanatory bubble, I aim to include text within the explain-header div as a container for my explanations: /*Explain Bubble*/ .explain-container { position: relative; overflow: hidden; max-width: 70vw; max-height: 50vh; background-c ...

Clicking on the checkbox will trigger the corresponding table column to disappear

Upon clicking the filter icon in the top right corner, a menu will open. Within that menu, there are table header values with checkboxes. When a checkbox for a specific value is selected, the corresponding table column should be hidden. I have already impl ...

Is it possible to launch a browser window using javascript?

I need to open a new Internet Explorer browser window and I am currently using the following code: window.open("http://jsc.simfatic-solutions.com", "mywindow"); However, I would like to know how can I open a new IE window with a fresh session? ...

During the initialization of the first page, the script is run prior to the loading of images and DOM

Hey there, I'm encountering an issue with script execution. I'm utilizing JQuarry to load images in load_images() and then running the activate_images() function, which adds the "data-lightbox" attribute and sets the class to "active". The proble ...

"Discover the steps to seamlessly display office documents on a webpage, similar to the functionality of

I am working on a project that involves using Node.js for the backend and AngularJS for the frontend. I need to be able to open Office files (.doc, .ppt, etc.) directly on my webpage. These files are stored on an Amazon S3 server. I am looking to implemen ...

Displaying and hiding content with a single button

I'm working on a slide-in content feature that appears from the left when clicking on the bars icon. Initially, I set it up with separate buttons for opening and closing the content by moving the page left and right. Now, I'm attempting to achiev ...

Enhancing jQuery Rating Plugin

Currently, I am working on customizing the jQuery Bar Rating System Plugin. You can view an example of the plugin by visiting this link: . The rating system on my end will resemble Example D. Rather than having the plugin based on user input, my goal is to ...