The text in Three.js fails to display

Hey there, I'm relatively new to Three.js and having some trouble getting basic text to render. Most of the solutions I found on StackOverflow seem outdated, so I thought I'd ask here. The code snippet below only results in a black screen. Any help would be greatly appreciated!

<html>
<head>
    <title>Text Test</title>
    <style>
        body { margin: 0; }
        canvas { width: 100%; height: 100% }
    </style>
</head>
<body>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/85/three.js"></script>
    <script>
        var scene = new THREE.Scene();
        var camera = new THREE.PerspectiveCamera( 75, window.innerWidth/window.innerHeight, 0.1, 1000 );

        var renderer = new THREE.WebGLRenderer();
        renderer.setSize( window.innerWidth, window.innerHeight );
        document.body.appendChild( renderer.domElement );


        var loader = new THREE.FontLoader();
        var font = loader.load(
            // resource URL
            'helvetiker_bold.typeface.json',
            // Function when resource is loaded
            function ( font ) {
                var geometry = new THREE.TextGeometry( 'Hello three.js!', {
                    font: font,
                    size: 80,
                    height: 5,
                    curveSegments: 12,
                    bevelEnabled: true,
                    bevelThickness: 10,
                    bevelSize: 8,
                    bevelSegments: 5
                } );
                var material = new THREE.MeshBasicMaterial( { color: 0x00ff00 } );
                var textMesh = new THREE.Mesh( geometry, material );

                scene.add( textMesh );

                camera.position.z = 5;

                var render = function () {
                    requestAnimationFrame( render );


                    renderer.render(scene, camera);
                };

                render();
            },
            // Function called when download progresses
            function ( xhr ) {
                console.log( (xhr.loaded / xhr.total * 100) + '% loaded' );
            },
            // Function called when download errors
            function ( xhr ) {
                console.log( 'An error happened' );
            }
        );



    </script>
</body>

Answer №1

It appears there are two potential issues.

A warning was mentioned regarding the extension 'GL_ARB_gpu_shader5' not being supported. This issue may be related to a bug in Firefox and Safari, as discussed here.

Additionally, your text seems oversized and your camera is too close. Consider adjusting the camera position with the following code:

camera.position.z = 500;

Answer №2

If the URL isn't working directly to load the Json file imports, you will need to first add the file path in the imports section and then specify the component name.

   var loader = new THREE.FontLoader();
   var font = loader.parse(helveticaRegular); 
   var textGeometry = new THREE.TextGeometry( 'Hello three.js!', 
   {font: font,
    size: 1,
    height: 0.1, 
    curveSegments: 20 } );
   var textMaterial = new THREE.MeshBasicMaterial( { color: 
   0xF00000 } ); 
   var textMesh = new THREE.Mesh( textGeometry, textMaterial ); 
   this.scene.add(textMesh);

I opted for the helveticaRegular Font file. To use your own font, download the TTF file and convert it to Json here:

Implement this code - it should effectively address your issue.

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

Challenges Faced when Connecting JavaScript to HTML

This is a snippet of my HTML where I link the .js file <!DOCTYPE html> <html> <head> <link rel="stylesheet" type="text/css" href="CSS/Style.css"> <title> Learning JavaScript </title& ...

@mui/x-date-pickers styling for the DatePicker component

Despite numerous attempts, I have been unsuccessful in styling the @mui/x-date-pickers <DatePicker/> component. I've experimented with various methods such as sx={{}}, style={{}}, makeStyles(), .css with the !important rule, renderInput={(param ...

Issue: The element [undefined] is not recognized as a valid child of the <Routes> component. Only <Route> or <React.Fragment> components are allowed as children of the <Routes

I am facing an issue while trying to upgrade react-router-dom from v5 to v6. The error message I receive is as follows: Error: [undefined] is not a <Route> component. All component children of <Routes> must be a <Route> or <React.Fragm ...

Why isn't my div displaying in JavaScript?

Currently working on a project for the Odin Project front-end web development. My task involves creating a grid using javascript/jQuery. I attempted to use the createElement method but have encountered an issue in making the div visible within the html. Am ...

Updating the text input value in a database with PHP upon button click

Here is a breakdown of what has been implemented so far: The database table is named adhar_card. Below is the structure image for reference (Adhard_card_id serves as the primary key). https://i.sstatic.net/8j4m6.jpg Clicking on the verify button will cha ...

Choosing HTML elements within nested DIV tags

In the process of creating a Hangman-style game using javascript/jQuery, I encountered an issue with selecting HTML content from virtual keyboard keys : <div class="square keyboard"> <div class="content"> <div class="table"> ...

Using Firebase collection inside an Angular constant

Currently, I am working on a Datatable using ng-bootstrap that utilizes a static array to display data. There are 3 main files involved: In the "country.ts" file, the necessary values are declared: export interface Country { id: number; name: string; ...

Executing a javascript file in a Java environment (Android)

Struggling with logging into a website from an android app? Look no further. In order to successfully submit the POST request with the necessary hashed values, you might need to delve into the website's javascript file. But what if tracing through it ...

Iterate through every row in the table in order to carry out a mathematical operation

I have created a jQuery function to add a table dynamically: $(document).on('click', '.add', function(){ var html = ''; html += '<tbody><tr>'; html += '<td><select name="product_name[]" cla ...

The Ajax request functions flawlessly on Mozilla but encounters issues on Chrome, causing confusion as it occasionally works

I am using a PHP file with a class and function to store data in the database, accessed via AJAX. While everything works smoothly in Mozilla, Chrome seems to be causing some issues. Strangely, sometimes it works fine, but other times it fails for no appare ...

Guide on integrating web api on android with javascript, jquery, and jsonp

I am attempting to create a button in my Android application that, when clicked, retrieves information from a web API and displays the results on my screen. Here is the code that performs the necessary tasks, but I need to integrate it into my Android pag ...

Discord.js messageCollector filtering options

I am looking to set up a MessageCollector with multiple users in Discord. Here is what I have so far: const collector = new Discord.MessageCollector(channel, m => m.author.id === "123456789" || m.author.id === "978654321" , { max: 2000, maxMa ...

Animating slides with CSS Keyframes and React, incorporating toggle slide fade out and slide fade (back) in

I am seeking a solution for toggling a box (div) with slide-out animation and then sliding back in animation (in reverse) upon button press. My requirement is to have no animations during the initial page render. While I can successfully slide the box to ...

Browser now replacing attribute translate="yes" with translate="translate"

We have encountered an issue with a translation library that is affecting the functionality of our page. <html lang="en" class="notranslate translated-ltr"> <meta name="google" content="notranslate"> As ...

The excessive memory usage of the WebGL renderer is causing frequent crashes in the browser

When loading JSON files containing object geometry, I am encountering memory consumption issues. For example, when loading a 200MB JSON file, it ends up using approximately 2.5GB of memory to render the data. This often leads to browser crashes or NS_OUT ...

Creating PDFs from DOCX files using NodeJS and TypeScript

I'm struggling to convert a DOCX file to a PDF in NodeJS using NestJS and TypeScript. I've tried several methods, but they all seem to fail: @nativedocuments/docx-wasm: NativeDocuments is not functioning as expected. word2pdf: This project is ar ...

The chai property "matchSnapshot" is not valid

https://i.sstatic.net/4wgqq.png After following the instructions provided at this link, I am now in the process of writing basic Jest tests for my React application that uses the Create-React-App starter kit. I came across a recommendation mentioned here ...

JAVASCRIPT ENCOUNTERS ISSUE WITH RANDOM TEXT GENERATION

Utilizing an array of words, we are in the process of generating random text. The goal is to shuffle and combine these words to form new "random sentences". However, we have encountered a minor issue - the addition of "undefined" at the beginning of each p ...

Avoiding repetitive clicking of buttons in jQuery—strategies for success

Hello, I am currently facing an issue where I need to reset the content of a div using an AJAX call when a user clicks a button. However, if the user clicks multiple times in quick succession, an error occurs. I would like to find a way to prevent users fr ...

After clicking, an created modal is displayed, then disappears on its own

After creating 2 Modals, I encountered an issue where the second modal with the class "category-modal" reverts back to display none when I click the "+" sign on the form. The first modal, with the class "modal," displays correctly when the add button is cl ...