Using Three.js to display a texture mapping on a specific section of an object

What is the most efficient way to display texture on the center/left/right side of a 2D object?

I have experimented with manipulating the offset and repetition of the texture without success, and I have found no answers elsewhere.

In addition, I attempted the following code snippet:

        someButton.onClick(function () {

            var geom = editor.selected.geometry;
            var uvs = geom.faceVertexUvs[0]; 

            var face0 = uvs[0];
            face0[0] = new THREE.Vector2(0,1);
            face0[1] = new THREE.Vector2(0,0.2);
            face0[2] = new THREE.Vector2(0.5,1);

            var face1 = uvs[1];
            face1[0] = new THREE.Vector2(0,0.2);
            face1[1] = new THREE.Vector2(0.5,0.2);
            face1[2] = new THREE.Vector2(0.5,1);

            geom.uvsNeedUpdate = true;

    });

Unfortunately, this method did not yield the desired results.

For instance, if the object is a 2D square and I wish for the koala bear picture to be centered, it should appear like this:

Koala Bear in the Center of Square

I am looking to alter the texture position dynamically. Thank you in advance.

Answer №1

After much brainstorming, I've devised a clever solution. Feel free to check it out:

someButton.onClick(function () {

    var obj = editor.selected;
    var width = obj.geometry.parameters.width;
    var height = obj.geometry.parameters.height;
    var geometry = new THREE.PlaneGeometry(width,height, 3, 3);
    var material = obj.material;

    material.map.wrapS = material.map.wrapT = THREE.RepeatWrapping;
    material.map.repeat.x = material.map.repeat.y = 3;

    var materials = [];
    materials.push(material);
    materials.push(new THREE.MeshBasicMaterial({}));

    for (var i = 0; i < 18; i++) {
        if (i == 8 || i == 9) geomtery.faces[i].materialIndex = 0;
        else geomtery.faces[i].materialIndex = 1;
    }

    obj.geomtery = geomtery;
    obj.material = new THREE.MeshFaceMaterial(materials);
    editor.signals.materialChanged.dispatch(obj);

});

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

Generating a personalized list of objects from JSON information

My json objects array contains the following data: [{"SensorID":2,"Temperature":34.0,"Humidity":56.0,"Carbon":87.0,"DateTime":"2017-03-08T10:07:00","ID":10},{"SensorID":2,"Temperature":34.0,"Humidity":43.0,"Carbon":87.0,"DateTime":"2017-03-09T12:00:00","I ...

Can you explain the distinction between employing express.urlencoded() with extended set to true as opposed to setting it to false along with performing manual JSON stringify/parse calls?

Within my NodeJS/Express server, I am faced with the decision of setting extended to either true or false for the urlencoded middleware: app.use(express.urlencoded({ extended: true/false })); I have come to understand that when set to false, it signifies ...

Displaying Data in Table Using Ajax Request

I'm working on a project that involves creating an HTML table from an ajax request pulling SharePoint list items. The screenshot provided demonstrates how it functions and what it displays after the button is clicked. However, I am looking for a way t ...

Implement a new method called "defer" to an array that will be resolved at a later time using Promise.all()

I need to manage a queue of DB calls that will be executed only once the connection is established. The DB object is created and stored as a member of the module upon connection. DB Module: var db = { localDb: null, connectLocal: (dbName) => { ...

Send the response from Facebook to the flash callback on the external interface

I attempted to transfer the response from a Facebook API request obtained through the FB JS-SDK to my flash application using external interface. However, I am having trouble identifying the type of response as it displays [object object] when printed in t ...

Transferring data from JavaScript to ASP.NET (via AJAX)

I'm trying to figure out how to pass variables from JavaScript to a textbox. I have a variable in JavaScript but I am unsure how to transfer it to the textbox. I have heard that using AJAX makes it easy, but I don't know how to implement it. I th ...

Activate $digest when a variable is modified by a node.js function

I am currently working on developing an application using node-webkit and AngularJS. Everything was going smoothly until I tried to make Angular watch over a "legacy variable" using the method outlined in this answer, which led to an Error: [$rootScope:inp ...

The issue with the AngularJS filter seems to be arising specifically when applied to

My AngularJS filter isn't functioning properly when used with an Object. Here's the View: <input type="text" ng-model="searchKeyUser.$" placeholder="Search Keys" class="form-control"><br> <ul class="list-group"> <li cla ...

Bootstrapvalidator does not function properly with select2.js

My code is not validating the select field. What could be causing this issue? Can anyone provide a solution? Apologies for my poor English, and thank you in advance for your response. Here is my form: <form name="form_tambah" class="form_tambah"> ...

Comparing two arrays of objects and updating the first object when a matching value is found in the second object: A guide

Trying to update the first array based on matching data from the second array. Encounter an error with forEach loop. First array: const data = [{ id: 1, name: "Alice" }, { id: 2, name: "Bob" }, { id: 3, name: "Charlie" } ] S ...

In JavaScript, you can verify if a specific <input> element is an input-field

With the introduction of HTML5, a variety of new input types have been added: <input /> Is there a way to detect whether an input field is a text field (such as date, time, email, text, etc.) without explicitly specifying each type? I would like t ...

How can VueJS manipulate state with Mutation?

I have set up a Vuex Store that returns data on headers and desserts. The desserts object includes a property called display, which is initially set to false. In my project, I am using a component called Row within another component named Table. The Row co ...

Executing a jQuery code within a WordPress theme

I have been attempting to implement the following script within the header.php file of a Wordpress website: <script> jQuery(function($) { jQuery('[href=#membership]').attr( 'data-menu-top', '500' ); }); ...

Utilizing Internationalization in Socket.io

I currently utilize the i18n-2 package for Internationalization in my project by implementing it as follows: router.get('/route', function (req, res, next) { res.redirect('/route/?lang=' + req.i18n.getLocale()); }); Now, ...

Implementing a class addition on focus event using Angular 2

Currently, I am in the process of upgrading an Angular 1 application to Angular 2 and encountering an issue with one of my existing directives. The task at hand is straightforward. When an input field is focused, a class should be added (md-input-focus) a ...

How to prevent VueJS observer from monitoring a temporary variable

My VueJS and Observer objects are causing me trouble. I am encountering an issue where I assign a part of my object to a temporary variable for later use, update the original part with new data, and then revert it back to its original state after 8 seconds ...

Issue detected: Code coverage functionality in Jest is not functioning as expected

When attempting to use jest --coverage --watch, the goal is to only see coverage related to the specific component being tested and to track increased coverage as more tests are added. However, despite successfully running modified tests, the resulting cov ...

CSS grid challenges on a massive scale

My CSS grid timeline is currently generating around 1300 divs, causing performance issues. Is there a way to style or interact with the empty nodes without rendering all of them? I also want each cell to be clickable and change color on hover. Any suggest ...

Unfortunately, CSS3 PIE is not compatible with border-radius and box-shadow properties

I created a basic HTML/CSS code for testing purposes, but I'm having trouble getting the library to function properly. I've placed the .htc, .php and .js files in the same directory as index.html, but it just won't work. Check out the code ...

Stop the sudden jump when following a hashed link using jQuery

Below is the code snippet I am working with: $( document ).ready(function() { $( '.prevent-default' ).click(function( event ) { event.preventDefault(); }); }); To prevent the window from jumping when a hashed anchor ...