TextGeometry in Three JS is designed to always face the user

Here is the source code I have been working on. My main goal is to make sure that TextGeometry is always facing the camera. Is this possible?

Code:

      var stats;
      var camera, controls, scene, renderer;

      init();
      render();

      function init() {
      scene = new THREE.Scene();
      var ambient = new THREE.AmbientLight( 0xFFFFFF );
      scene.add( ambient );

      var container = document.getElementById('container');

      renderer = new THREE.WebGLRenderer({antialias: 1 });

      renderer.shadowMap.enabled = true;

      renderer.setPixelRatio( window.devicePixelRatio );
      renderer.setSize( window.innerWidth, window.innerHeight);
      renderer.setClearColor(0x013A65);

      container.appendChild( renderer.domElement );

      camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 1, 5000 );
      camera.position.set(0,0,0);

      controls = new THREE.OrbitControls( camera, renderer.domElement );
      controls.addEventListener( 'change', render );
      controls.enableKeys = false;
      controls.enableZoom = true;
      controls.minDistance = 2000;
      controls.maxDistance = 3500;
      controls.maxPolarAngle = Math.PI/2; 

      // world
      var onError = function ( xhr ) { };
      THREE.Loader.Handlers.add( /\.dds$/i, new THREE.DDSLoader() );
      var mtlLoader = new THREE.MTLLoader();
      mtlLoader.setPath( './assets/3d/' );
      mtlLoader.load( 'A.mtl',           function( materials ) {
        materials.preload();
        var objLoader = new THREE.OBJLoader();
        objLoader.setMaterials( materials );
        mtlLoader.setPath( './assets/3d/' );
        objLoader.load( './assets/3d/A.obj',             function ( object ) {
        object.position.x = 0;
        object.position.y = 0;
        object.position.z = 0;
        scene.add( object );
        }, onError );
      });

      //Text
      var loader = new THREE.FontLoader();
      loader.load( './fonts/Open_Sans_Regular.json', function ( font ) {
        var textGeometry = new THREE.TextGeometry( "Test", {font: font, size: 22, height: 3, curveSegments: 1});var textMaterial = new THREE.MeshPhongMaterial({ color: 0xFFFFFF, specular: 0xFFFFFF });var mesh1 = new THREE.Mesh( textGeometry, textMaterial );mesh1.position.x = -200;mesh1.position.y = 250;mesh1.position.z = 725;scene.add( mesh1);});   

      // lights
      light = new THREE.DirectionalLight( 0xFFFFFF );
      light.position.set( 1, 1, 1 );
      scene.add( light );

      window.addEventListener( 'resize', onWindowResize, true );
    }

      function onWindowResize() {
        camera.aspect = window.innerWidth / window.innerHeight;
        camera.updateProjectionMatrix();
        renderer.setSize( window.innerWidth, window.innerHeight );
      }

      function animate() {
        requestAnimationFrame( animate );
        stats.update();
        render();
      }

      function render() {
        renderer.render( scene, camera );
      }

I have attempted using mesh.lookAt(camera.position) inside the render function, but unfortunately, it has not been successful.

Warm Regards.

Answer №1

In my opinion, one way to ensure the visibility of text regardless of camera position is by placing it inside a div container with absolute positioning using CSS, instead of text geometry. You can refer to this link for more information:

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

Retrieve the unique payment ID generated from the database and present it on the frontend

Greetings, I am currently working on integrating a payment processor and I require the automated generation of the corresponding payment ID in the backend. On the frontend side, I have implemented JS to request data from the backend, but I'm facing ...

Pass the form data to the next page with javascript in HTML

While working on a website for a power plant, I encountered some issues that require assistance. The main problem is that our client does not want to set up a database on their server. This means I can only use html, javascript, and a bit of php. There is ...

Having issues with the jQuery toggle functionality

var resultsList = $("#test"); resultsList.text("Hello. This is jQuery!"); var tB = jQuery("#toggleButton"); tB.on("click", function() { resultsList.toggle(400); }); The syntax appears to be correct as there are no errors reported in the browser cons ...

JavaScript implementation of the results sorting

I have 2 sql queries to calculate the turnover for each semester. Results from query 1: { "LRU": [ "RADOME", "RADOME", "ATSU", "MFC", "FWC", "Unspecified", "AZE", "ECP", "CMM", "ECP" ], "Client": [ 17346, ...

Retrieving status code without reliance on a back-end language (maybe through JavaScript?)

My new service offers a solution for error pages in web apps by providing a code snippet that can be easily copied and pasted into the error page templates, similar to Google Analytics. The status code is embedded in a hidden input within the installatio ...

Creating a modal form with jQuery in ASP.NET

I'm fairly new to ASP.NET development and have been able to work on simple tasks so far. However, I now have a more complex requirement that I'm struggling with. My goal is to create a modal form that pops up when a button is clicked in order to ...

Determine the presence of an HTML data attribute within any of the parent elements

I am attempting to locate an HTML data attribute on one of the ancestors of a given element, baseElement, and retrieve its value if found. In the case where the attribute is present in multiple ancestors, I am interested in the one closest to baseElement. ...

Using JavaScript to transform radio buttons into checkboxes

I have a grouping of radio buttons and a checkbox displayed on the page as shown below <html> <head> <title>Languages</title> <script type="text/javascript"> </script> </head> <body> <spa ...

How can I prevent users from selecting text when they right click using JavaScript?

My custom context menu on canvas works well, but I'm facing an issue with Text elements. When I try to right-click on a text element, it triggers text selection as if double-clicking. I attempted to use the following code: document.addEventListener(& ...

How can I extract just the initial 2 letters of a country name using AmCharts maps?

Having trouble with Amcharts maps. I have a map that displays countries as United States, but I only want them to show as US. Is there a country formatter available for this issue? Any help is appreciated. ...

Suggestions on how to refactor redundant code in various peer AngularJS controllers for handling $intervals

In my compact single-page application, I have implemented multiple tabs to display operational status information for different applications. Each tab is associated with a controller that creates $interval objects to make AJAX calls to fetch status data fr ...

Step-by-step guide to implementing a datepicker textfield with Vuetify 3

I'm currently exploring Vuetify 3 and aiming to implement a textfield that serves as a datepicker. For reference, you can find a similar example in the Vuetify 2 documentation here. Unfortunately, the Vuetify 3 docs do not yet include an example like ...

Loading animation vanishes prior to the second ajax request being made

Whenever an ajax call is initiated, I display a loading animation that stops once the ajax call is completed. This works perfectly when there's only one ajax call in a function. However, if there are two ajax calls within the same function, the loadin ...

Trouble with displaying pagination within an iframe

I am facing an issue with the pagination in iframe where the height of the iframe does not increase according to the content when I navigate to the next page. Please see the issue demonstrated here: http://fsportal.us-east-1.elasticbeanstalk.com usernam ...

Exploring the properties of a file directory

As I try to access the d attribute of a path that was generated using Javascript, the output of printing the path element appears as follows: path class=​"coastline" d="M641.2565741281438,207.45837080935186L640.7046722156485,207.0278378856494L640.698 ...

Sorting by Date in JavaScript

My goal is to filter out the elements in an array that have a date (converted from string) greater than a specific date. _this.downloadData.forEach(_d => _d.LogTime = _d.LogTime.toString()); console.log(_this.downloadData.filter(x=>new Date(x.LogTi ...

Highcharts failing to connect the dots with lines between data points

UPDATE: After some investigation, it turns out that the issue stemmed from inconsistent intervals. By following the solution outlined in this post, the problem was easily resolved. I have implemented highcharts to create a chart, where the initial data po ...

What is the best way to monitor and record the height of a div element in a React application?

Exploring the Height of a Div I am interested in monitoring the height of a div element so that I can dynamically adjust distances based on varying screen widths. The animation should respond to changes in screen width, such as when text stacks and the he ...

The imported `theme` as `theme` was not visible in the file './theme'. The only possible export in the file is `default`

Encountering an error while attempting to export the theme from the index.js file. Below is the snippet of code present in the index.js file. import { createTheme } from "@mui/material"; import shadows from "./shadows"; import typograph ...

Default Filter for Lookup Dialog in Dynamics CRM 2011

I am attempting to customize a lookup dialog to default to a specific entity type. While working on the Connection form, I am checking the entity type of record1id and attempting to use that information to set the defaulttype attribute on record2id. My c ...