Revamping the vertices and UVs of DecalGeometry

I am currently experimenting with ThreeJS decals. I have successfully added a stunning decal to my sphere.

Below is the code snippet I am using to place the decal on my sphere. (Please disregard any custom classes mentioned in the code.)

// Creating the sphere
var mainMesh = new THREE.Mesh(
    new THREE.SphereGeometry(7, 16, 16),
    new THREE.MeshBasicMaterial({ color: 0x00a1fd })
);

// Defining the decal material
var decalMaterial = new THREE.MeshPhongMaterial({
    color               : 0xff0000,    
    specular            : 0x444444,
    map                 : TextureLoader.instance.getTexture('http://threejs.org/examples/textures/decal/decal-diffuse.png'),
    normalMap           : TextureLoader.instance.getTexture('http://threejs.org/examples/textures/decal/decal-normal.jpg'),
    normalScale         : new THREE.Vector2( 1, 1 ),
    shininess           : 30,
    transparent         : true,
    depthTest           : true,
    depthWrite          : false,
    polygonOffset       : true,
    polygonOffsetFactor : -4,
    wireframe           : false
});

// Creating the decal itself
var decal = new THREE.Mesh(
    new THREE.DecalGeometry(
        mainMesh,
        new THREE.Vector3(0, 2.5, 3),
        new THREE.Vector3(0, 0, 0),
        new THREE.Vector3(8, 8, 8),
        new THREE.Vector3(1, 1, 1)
    ),
    decalMaterial.clone()
);

// Adding mesh, decal, and helpers to the scene
scene.add(
    mainMesh,
    new THREE.HemisphereLight(0xffffff, 0, 1),
    decal,
    new THREE.WireframeHelper(decal, 0xffff00)
);

decal.add(new THREE.BoxHelper(decal, 0xffff00));

Now, I want to move this decal on my sphere and update the decal geometry accordingly.

However, when I call decal.geometry.computeDecal(), the decal mesh does not update. I am unable to find a solution to this issue.

function moveDecal()
{
    decal.translateX(1);
    decal.geometry.computeDecal();
};

As per the DecalGeometry class, the computeDecal function should update various elements like vertices, colors, UVs, etc.

this.computeDecal = function() {
    // [...]
    this.verticesNeedUpdate     = true;
    this.elementsNeedUpdate     = true;
    this.morphTargetsNeedUpdate = true;
    this.uvsNeedUpdate          = true;
    this.normalsNeedUpdate      = true;
    this.colorsNeedUpdate       = true;
};

Any assistance on this matter would be greatly appreciated! :D

PS: ThreeJS r80

Answer №1

If you want to modify the vertices in your geometry, there are some restrictions to keep in mind.

You have the ability to alter the value of a vertex component like so:

geometry.vertices[0].x += 1;

However, you cannot introduce new vertices by using the push method:

geometry.vertices.push(new THREE.Vector3(x, y, z)); // this operation is not permitted

Similarly, you are not permitted to replace the entire vertex array:

geometry.vertices = new_array; // this action is also restricted

Once the geometry has been rendered at least once, these operations become off-limits.

The same restrictions apply to other attributes, such as UVs.

For more detailed information, refer to this answer: verticesNeedUpdate in Three.js.

three.js r.80

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

Animation of disappearing blocks covering the entire screen

I am currently working on creating a slider with fading blocks animation similar to the one shown here. The challenge I am facing is making it fullscreen, where the height and width will be variable. This makes using the background-position trick ineffecti ...

Vue Application experiences issues resizing Three.js Animation in Canvas

Currently, I am deep diving into learning Three.js within a Vue.js component. Take a look at my code snippet: <script> import * as THREE from "three"; export default { name: "Scene", mounted() { this.initializeThree(); ...

Eliminate the JSON object within jqGrid's posted data

The web page I'm working on features Filters with two buttons that load data for jqGrid when clicked. Clicking 'Filter' generates a postData json object and sends it to the server, which is working perfectly. However, I'm facing an is ...

Rotate a sphere in three.js in order to align the mapped image of the globe with the GeoJSON data that is displayed in the same three

I have successfully implemented a globe in three.js with an image mapped onto the sphere. Furthermore, I am utilizing the ThreeGeoJSON library to visualize geojson data on top of the globe. However, the geographies from the geojson data do not align corr ...

Adjust the width of every column across numerous instances of ag-grid on a single webpage

I am facing an issue with Ag-grid tables on my webpage. I have multiple instances of Ag-grid table in a single page, but when I resize the browser window, the columns do not automatically adjust to the width of the table. We are using only one Ag-grid in ...

Challenges encountered with extracting information from a JSON dataset

When I make an Ajax POST request, the response I receive is structured like this: { "columns": [ "r" ], "data": [ [ { "extensions": {}, "start": "http://localhost:7474/db/data/node/27 ...

The property 1 cannot be added because the object is not extendable in React

Does anyone know what is causing the following issue? I am unable to insert a new object into a key object within my arrays of objects. For example, when I try to insert a new email at index 1 in the 'emails' array, it throws an error stating "ca ...

Adjust dimensions of images in Bee3D carousel

I recently utilized the Bee3d library available on the following Github link: https://github.com/lukeed/bee3d While I found everything to be truly impressive, I am curious if there is a way to adjust the image size. Whenever I attempt to change the image ...

Return to the previous page with different query parameters, not the same one

When it comes to reverting state location back by 1 step in Angular, we can utilize something along the lines of this.location.back();. This method works well unless the system redirects to the same URL but with different query parameters. In such cases, ...

Top method for developing a Wizard element within React

As I delved into learning React, my focus shifted towards creating a Wizard component. My initial vision was to use it in this way: <Wizard isVisible={this.state.isWizardVisible}> <Page1 /> <Page2 /> </Wizard> However, I e ...

Ensure both mouse clicks and pressing the enter key are functional for improved accessibility

Consider the following form with a tabindex property: <form id="settings-form"> <input type="text" /> <input type="submit" /> <a href="#" class="cancel-save" tabindex="0">cancel</a> </form> An action is bou ...

Is there a way to dynamically insert objects into an array from an ajax json request, in order to iterate through them later on?

I have a current code where I am trying to utilize data to create a new information window from the Google Maps API dynamically. Currently, I am pushing objects of different data types into an array, but I believe that might be the only issue here. How c ...

Under what circumstances would you need to manually specify the displayName of a React component?

After coming across an article (linked here: https://medium.com/@stevemao/do-not-use-anonymous-functions-to-construct-react-functional-components-c5408ec8f4c7) discussing the drawbacks of creating React components with arrow functions, particularly regardi ...

What is the best way to prevent users from entering a zero in the first position of a text box using JavaScript

Although I am aware this may be a duplicate issue, the existing solution does not seem to work for me. The field should accept values like: valid - 123,33.00, 100,897,99, 8000 10334 9800,564,88.36 invalid - 001, 0 ...

Saving the AJAX response object in a global variable - Important fields are not being retrieved

Currently, I am developing an asynchronous webpage in Grails wherein I invoke a controller and display the response as a D3.js network. To ensure further usability, I saved the object as a global variable. Despite the successful execution of the function u ...

I am attempting to incorporate a List View within a Scroll View, but they are simply not cooperating. My goal is to display a collection of items with additional text placed at the bottom

This is how it should appear: item item item item additional text here I am trying to create a layout where the list is in List View for benefits like virtual scrolling, but the entire layout needs to be within a Scroll View. I want to be able to con ...

Implementing Vue modal within a Laravel 5.2 foreach loop

I am facing a challenge with my Laravel blade template that uses a foreach loop to create a table with data. Each row has a link that triggers a modal when clicked. However, the issue is that clicking on any link activates the modal for every row instead o ...

Creating a form that can identify both letters and numbers using JavaScript

Is it possible to create an <input> field that can recognize both letters and numbers while disregarding spaces and capitalization? Here is my current progress, aiming for the feedback to display as "right" when 9 H 6 U 8 f is entered in the field, ...

Customize and adjust the default color for dark themes in Material-UI

When using Material-UI and switching to a dark theme, you may notice that some components change their color to #424242 while others change to #212121. This color inconsistency stems from the use of theme.palette.grey: theme.palette.grey[800]: '#424 ...

Set up global variables for components to access

Currently, I am working on a Laravel 8 project with vue-sweetalert2 integration. My goal is to set up the Toast behavior once and then be able to call it within various components. At the moment, my code looks like this: /js/components/Mypage.vue <scr ...