What are some ways to enhance the texture of BufferGeometry faces?

I have designed a bufferGeometry comprising 5 planes measuring 100x25 with two triangles each.


function createGeometry() {
  var geometry = new THREE.PlaneGeometry(100, 25, 1);
  return geometry;
}

function createScene() {

    var bufferGeometry = new THREE.BufferGeometry();
    var radius = 125;
    var count = 5;

    var positions = [];
    var normals = [];
    var colors = [];

    var vector = new THREE.Vector3();
    var color = new THREE.Color(0xffffff);
    var heartGeometry = createGeometry();
    var geometry = new THREE.Geometry();

    var step = 0;
    for (var i = 1; i <= count; i++) {

        geometry.copy(heartGeometry);

        const y = i * 30
        geometry.translate(-100, y, 0);

        geometry.faces.forEach(function (face) {
            positions.push(geometry.vertices[face.a].x);
            positions.push(geometry.vertices[face.a].y);
            positions.push(geometry.vertices[face.a].z);
            positions.push(geometry.vertices[face.b].x);
            positions.push(geometry.vertices[face.b].y);
            positions.push(geometry.vertices[face.b].z);
            positions.push(geometry.vertices[face.c].x);
            positions.push(geometry.vertices[face.c].y);
            positions.push(geometry.vertices[face.c].z);

            normals.push(face.normal.x);
            normals.push(face.normal.y);
            normals.push(face.normal.z);
            normals.push(face.normal.x);
            normals.push(face.normal.y);
            normals.push(face.normal.z);
            normals.push(face.normal.x);
            normals.push(face.normal.y);
            normals.push(face.normal.z);

            colors.push(color.r);
            colors.push(color.g);
            colors.push(color.b);
            colors.push(color.r);
            colors.push(color.g);
            colors.push(color.b);
            colors.push(color.r);
            colors.push(color.g);
            colors.push(color.b);

        });
    }
    bufferGeometry.addAttribute('position', new THREE.Float32BufferAttribute(positions, 3));
    bufferGeometry.addAttribute('normal', new THREE.Float32BufferAttribute(normals, 3));
    bufferGeometry.addAttribute('color', new THREE.Float32BufferAttribute(colors, 3));


    var material = new THREE.MeshBasicMaterial({
        vertexColors: THREE.VertexColors,
        side: THREE.FrontSide
    });

    var mesh = new THREE.Mesh(bufferGeometry, material);
    scene.add(mesh);

}

How can I add text to each plane instead of coloring them? Specifically, I want to display 1,2,3,4,5 in the center of each plane.

To accomplish this, follow these steps:

  • Generate texture from canvas
  • Change the material to Shader Material with map:texture
  • Add uvs to bufferGeometry.

What is the relationship between uvs and textures?

I have provided a texture creation function below:

//not sure for each character or one texture as a single texture
function createTexture(ch){
  
  var fontSize = 20;
  var c = document.createElement('canvas');
  c.width = 100;
  c.height = 25;
  var ctx = c.getContext('2d');
  ctx.font = fontSize+'px Monospace';
  ctx.fillText(ch, c.width/2, c.height/2);

  var texture = new THREE.Texture(c);
  texture.flipY = false;
  texture.needsUpdate = true;

  return texture;
}

Access the full demo code HERE

EDIT

Priority is given to performance for this project. Adding separate meshes for each text will impact screen performance. Looking for suggestions on using a single mesh similar to my example.

The technique mentioned here seems promising, yet requires further understanding.

Answer №1

To achieve the desired output, it is essential to duplicate the original uv channel of the .faceVertexUvs attribute from the THREE.Geometry, following a similar method as with vertex coordinates and normal vectors:

for ( var i = 1, l = count; i <= l; i++ ) {

        geometry.copy( heartGeometry );

        const y = i * 30
        geometry.translate(-100, y, 0);

        geometry.faces.forEach(function (face) {
            let f = [face.a, face.b, face.c];
            for (let i=0; i < 3; ++i) {
                positions.push(...geometry.vertices[f[i]].toArray());
                normals.push(...face.normal.toArray());
                colors.push(...color.toArray());
            }
        });

        geometry.faceVertexUvs[0].forEach(function (faceUvs) {
            for (let i=0; i < 3; ++i) {
                uv.push(...faceUvs[i].toArray());
            }
        });
    }

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

Is it possible to have Vue.js code within a v-for loop in such a way that it executes every time the v-for loop runs?

<div class="questions" v-for="(q,index) in questions " :key="index" {{this.idx+=1}} > <h3 > {{q.content}}</h3> <h3> A) {{q.a}} </h3> <h3> B) {q.b}} </h3> ...

"Underscores in an array of primary keys serve as markers for

I want to filter a list of objects using underscore based on their primary key being included in a specific array of primary keys. list = [object{pk: 1}, object{pk: 2}, object{pk: 3}] primary_key_list = [1,2] The desired outcome is to return [object{pk: ...

Toggling D3 JS graphs when radio buttons are clicked is not working as expected

Working on integrating a D3 SVG chart with jQuery to trigger radio buttons displaying different graphs, using a helpful answer from here. Fiddle : http://jsfiddle.net/k3WJN/13/ Data is fetched from various URLs to populate log count by day and week. Belo ...

The touch events are not detected on Pixi.js when buttons are placed on a stage that has been both scaled and

Currently, I am developing a game using pixi js. In order to ensure that the game appears consistent on all screens, I have implemented the following scaling logic: this.scale = Math.max(this.viewWidth, this.viewHeight) / (2048 * this.ratio); Additionall ...

Meteor: The call stack has surpassed its maximum size limit

I attempted to perform an action that I have done several times before without encountering any errors. My goal is to retrieve all documents where the X field matches the value Y in my Meteor app: JS: (template helper) 'friendPictures' : funct ...

Exploring event propagation in React components

Can event bubbling be tested in React? For instance: Class Foo extends React.Component { doSomething() { console.log('bubble'); } render() { return ( <div onClick={this.doSomething}> ...

Loop through ng-options using ng-repeat that are linked to the same ng-model

This situation may seem odd, but I am willing to adjust the API Data if necessary. My requirement is to have multiple dropdowns sharing the same ng-model. The Question: Why does ng-model become unique when using the same variable name? How can I ensu ...

Previewing posts on a single page can be done by following a few

Hello there! I have written some code in React.js I am trying to display my blog posts on a single page when the user clicks on the "read more" button. I am fetching this data from a news API and I want to show each post based on its specific ID, which i ...

Combining Laravel and VueJs for a Multi-Page Application (MPA)

I have recently developed a website using a combination of Laravel and VueJs. However, I am facing some confusion regarding the most suitable routing architecture to implement. Currently, my application has the following setup: The routing system is man ...

Guidance on Applying Texture to a Targeted Area of a 3D Model in Three.js

Currently immersed in a Three.js project, I find myself faced with the challenge of projecting a texture onto a particular surface of a 3D model. Despite loading the model through GLTFLoader and possessing the necessary texture, my attempts at using Raycas ...

Utilizing AngularJS to dynamically inject HTML content into $scope

In my possession are the following files: index.html //includes instructions for passing arguments to the btnClick function in app.js <div ng-bind-html="currentDisplay"></div> app.js app.factory('oneFac', function ($http){ var htm ...

Preventing the display of any object items that include a period character in AngularJS

There seems to be an issue with a JSON item not displaying correctly in AngularJS. error = { "name": [ "The name field is required." ], "salary.salary_id": [ "The salary.salary id field is required." ] } When attempting to ...

How can I troubleshoot email validation issues in Vue.js?

<button type="submit" class="register-button" :class="(isDisabled) ? '' : 'selected'" :disabled='isDisabled' v-on:click=" isFirstScreen ...

Exploring the option of showcasing multiple json_encode data on a pie chart

Hey there! I'm currently utilizing Chart.js to generate a pie chart by retrieving data from the database: <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.4.0/Chart.min.js"></script> <script> var ctx = document.getE ...

Trigger an event once a script is called in an external HTML file loaded through Ajax

My webpage features an Ajax Loaded Div, within which a Jquery plugin loads my Google Spreadsheet. I am struggling to add event listeners to detect when the spreadsheet is fully loaded due to the unpredictable loading time and the limitations of listening f ...

Display the specific outcome solely from an array using AJAX

I possess an array of objects in JSON format as shown below: [{"id":"0","NameInData":"","name":"","type":"mf","greeting":"Bonjour","message":"GENERIC: Lorem ipsum."}, {"id":"1","NameInData":"alexis","name":"Alexis","type":"mf","greeting":"Bonjour Alexis", ...

How can I design a tooltip window using HTML, jQuery, or other elements to create a box-like effect?

As someone who is new to front end development, I am facing a challenge with my web app. The requirement is that when a user hovers over an image, I need a 'box' to open nearby and display a pie chart. Although I have managed to get the hover fu ...

Incorporate controllers dynamically into your Angular application

Currently, I am incorporating AngularJS into a Backbone.js application. The scenario is as follows: I have one controller (let's call it controller1) which is added to the Angular app from a Backbone view (referred to as view1). Subsequently, I introd ...

Effortless method to share an image and leave a comment on a user's Facebook wall using the

I am trying to efficiently share an image and comment from my website to a Facebook user's own wall. Despite searching through multiple resources and Facebook API documentation, I am struggling to find a simple solution for this basic task. Facebook ...

Tips for extracting specific values from JavaScript using jQuery in ASP to be utilized in the C# code behind section

I want to extract the selected values from a multiselect jQuery and use them in the code behind in C#. The script sources can be found at the following link: https://github.com/nobleclem/jQuery-MultiSelect Below is the ASP code I am using: <div class ...