Including 2D text onto a cube using elements from an array. Countless items

I have developed a script that generates approximately 10,000 boxes using the following code:

var c=[];
c.push([13,0,3021,98,'01391A']);
c.push([13,102,3021,98,'01391W']);
c.push([13,204,3021,98,'01391Y']);
c.push([13,306,3021,98,'01391Z']);
c.push([13,0,3069,98,'01392A']);
c.push([13,102,3069,98,'01392W']);
...
    var materials = [];
    var materials2 = [];
    var materials3 = [];
    var totalGeom = new THREE.Geometry();
    var totalGeom2 = new THREE.Geometry();
    var totalGeom3 = new THREE.Geometry();


for (var i in c)
{

    var cubeGeom = new THREE.CubeGeometry( 48, c[i][3] , 40 );
    var cubeMat = new THREE.MeshBasicMaterial(  { color: 0xF3F4EC  });
    materials.push(cubeMat);

    var cubeMesh = new THREE.Mesh( cubeGeom, cubeMat );
    cubeMesh.position.set(c[i][0] + 24, ((c[i][1]) + ((c[i][3]) / 2)), c[i][2] + 20);

    var outlineMaterial2 = new THREE.MeshBasicMaterial( { color: 0xCCCFBC,side: THREE.BackSide} );
    materials2.push(outlineMaterial2);
    var outlineMesh2 = new THREE.Mesh( cubeGeom, outlineMaterial2 );
    outlineMesh2.position = cubeMesh.position;
    outlineMesh2.scale.multiplyScalar(1.05);

    THREE.GeometryUtils.merge(totalGeom, cubeMesh);
    THREE.GeometryUtils.merge(totalGeom2, outlineMesh2);

}
var total = new THREE.Mesh(totalGeom, new THREE.MeshFaceMaterial(materials));
var total2 = new THREE.Mesh(totalGeom2, new THREE.MeshFaceMaterial(materials2));
scene.add( total ); 
scene.add( total2 );

The current functionality works well, but I am looking for a way to label each box on the side with its respective name. Although I can add 2D text to the scene, I'm unsure about the best approach for adding labels to each box.

For example, for '01391Y', I would like to label the side of the box as '391Y'.

Answer №1

When it comes to performance, I'm not sure which approach would be more efficient. One option is to add a 2dText geometry for each box and place it on the side, while another is to draw text onto a canvas and use the canvas as a texture...

Here's an example using 2dText, although you may have already figured that out:

Alternatively, how about something like this?: https://jsfiddle.net/EthanHermsey/u1gyzroL/2/

 var canvas = document.createElement('canvas');
canvas.width = canvas.height = 128;
var ctx = canvas.getContext("2d");

//cube background
ctx.fillStyle='#F3F4EC';
ctx.fillRect(0, 0, 128, 128);

//black text
ctx.fillStyle='black';
ctx.font = "30px Arial";
ctx.fillText("B391Y", 10, 50);

let canvasTexture = new THREE.CanvasTexture( canvas );
let material = new THREE.MeshBasicMaterial({ map: canvasTexture });
var cubeMesh = new THREE.Mesh( cubeGeom, material );

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

What is the best way to create a smooth transition for a bootstrap navbar in chrome, toggling from right to left on

I have successfully modified the bootstrap navbar to toggle from right to left instead of top to bottom using the following code: For HTML- <nav class="navbar navbar-inverse" role="navigation" style="height: 55px; padding-top: 2px; background-color: # ...

Interactions between a service and a directive: interdependence or triggering of events

Issue: managing multiple directives that interact with a service or factory to communicate and log actions with a server. Should I establish dependencies between them? angular.module('someModule', []) .directive('someDir', ['l ...

Transferring data between different elements in a React application

I am currently learning React and facing some challenges in terms of passing data between components. Even after reviewing various tutorials and blogs, I am still struggling to make things work. Within my project, I have two child components named Body-c ...

Submitting a form within AJAX-powered tabs (using the Twitter Bootstrap framework)

I have encountered an issue while trying to submit a form that is located within tabs. The content of these tabs is generated through AJAX. My problem arises when I submit the form - the page refreshes and loads the "default" tab, causing the PHP function ...

Using form.submit() alongside preventDefault() will not yield the desired outcome

My login form needs to either close the lightbox or update the error box based on the success of the login attempt. I suspect the issue lies with the onclick="formhash(this.form, this.form.password);" which is a JavaScript function that hashes a passwor ...

Unable to make colors appear in HTML5 canvas using .fillStyle

Trying my hand at canvas for the first time to create a game. I have an image displaying, but strangely the fillStyle method doesn't seem to be working as expected (the canvas background remains white in Google Chrome). Just a note that in my code, t ...

Find distinct elements in an array of objects

Imagine you have an array filled with different objects: var itemsArray = [ {name: "apple", color: "red", weight: "100g"}, {name: "banana", color: "yellow", weight: "120g"}, {name: "apple", color: "red", weight: "100g"}, {name: "banana", color: "y ...

Utilizing Ionic to seamlessly integrate Firebase into a factory, maintaining separation of controllers and services within distinct files

I'm struggling with setting up a firebase factory for use in my controllers. Currently, this is how my code appears: index.html ... <!-- integrating firebase --> <script src="lib/firebase/firebase.js"></script> <script src="lib/ ...

The JavaScript file specified in the script tag's src attribute was successfully downloaded, but it did not execute as expected after being fetched

I've implemented an ajax call using jQuery in the following manner: $.ajax({ type: 'GET', url: 'edit.htm', success: function(data){ container.html(data); }, }); After making the ajax call, the data returned includes s ...

Issue encountered while installing semantic-ui-react and semantic-ui-css with React using npm

Issue encountered during the semantic UI installation via npm install Any solutions for this problem? ...

Executing API call utilizing the Request module within a node.js application

In my node.js app, I have a request call that looks like this: request({ url:chanURL, qs:chanProperties}, function(err, response, body) { if(err) { console.log(err); return; } body = JSON.parse(body); (function (body) { Objec ...

What is the best way to post an image using nodejs and express?

Recently, I've been working on a CMS for a food automation system and one feature I want to incorporate is the ability to upload pictures of different foods: <form method="post" enctype="multipart/form-data" action="/upload"> <td>< ...

Adjust checkbox based on selection from <select> option

In my scenario, I have a selection process where the ID of the <option> element is sent to a .php file. Afterwards, based on some database checks, I need to dynamically check or uncheck a checkbox. Everything seems to be in place except for this last ...

Develop interactive div elements with the help of JavaScript/jQuery

Exploring dynamic div creation using javascript or jquery and looking for guidance. <div id="clickable"> <button class="start">Default</button> <button class="random">Randon</button> <button class="gradient"> ...

How can I connect an image to a link in Vue.js?

I am having trouble linking to another URL when I click on an image using Vue. It should be possible to link by clicking on the images, but it seems to not be working. If anyone can offer assistance, I would greatly appreciate it. Below is what my code ...

Unpredictable order of replies retrieved using $http.get

I am struggling to create a function that sends multiple requests to the server based on the number of Ids in the idArray. The issue I am encountering is that the data being pushed into the dataArray does not align with the corresponding Ids of the idArr ...

Mystery Surrounding MP3 Playback in ASP.NET

Currently, I am utilizing an ASP.NET server side control to play MP3 files on my website. While I could utilize JavaScript or Flash controls for this purpose, the issue I'm facing is that I want the music to only play once (at the start of the site) a ...

The straightforward splitting of a string is yielding an object rather than an array

Attempting a simple string split in NodeJS is resulting in an unexpected outcome where it returns an object instead of an array. var mytext = "a,b,c,d,e,f,g,h,i,j,k"; var arr = mytext.split(","); console.log(typeof mytext); <======= output string conso ...

Establishing a connection between JavaScript and MySQL within an HTML document

Recently, I started using node.js's node-mysql driver for MySQL and decided to write this piece of code: var mysql = require("mysql"); var connection = mysql.createConnection({ host : "localhost", user : "root", ...

In Ember.js, where should I place the initialization code for a controller? I attempted to set it up in the routes

With my experience working with ember.js and highcharts, I have come across some examples that were too simplistic for me to grasp how to set up the chart objects and render them properly. I have explored initializers and understand the significance of ro ...