Using Dat GUI alongside Three.js to generate 3D text geometry based on user input, with real-time updates

Trying to utilize the combination of Dat GUI and Three.js to allow for user inputted text to generate 3D text geometry that updates in real time. Successfully achieved control over the x, y, and z positions, as well as displaying the text input box.

Struggling to identify the issue with the text input functionality, any assistance would be greatly appreciated.

Here is what has been accomplished so far: and here is the code dealing with the 3D text geometry and dat GUI:

var theText = "FEED ME";
var hash = document.location.hash.substr( 1 );
  if ( hash.length !== 0 ) { theText = hash; }

  var text3d = new THREE.TextGeometry( theText, {
    size: 80,
    height: 80,
    curveSegments: 2,
    font: "helvetiker",
    weight: "bold"
  });
  text3d.computeBoundingBox();

  var centerOffset = -0.5 * ( text3d.boundingBox.max.x - text3d.boundingBox.min.x );

  var textMaterial = new THREE.MeshNormalMaterial;
  text = new THREE.Mesh( text3d, textMaterial );
  text.position.x = centerOffset;
  text.position.y = 0;
  text.position.z = 0;
  // text.position.z = Math.tan( Date.now() * 2 ) * 20;
  text.rotation.x = 0;
  text.rotation.y = Math.PI * 2;
  parent = new THREE.Object3D();
  parent.add( text );
  scene.add( parent );

  var axes = new THREE.AxisHelper();
  scene.add(axes);


  gui = new dat.GUI();

  parameters = 
  {
    x: 0, y: 30, z: 0,
    color: "#ff0000", // color (change "#" to "0x")
    theText: "",
    opacity: 1, 
    visible: true,
    material: "Phong",
    reset: function() { resetText() }
  };

  var folder1 = gui.addFolder('text');
  var line1 = folder1.add( parameters, 'theText');
  // var line2 = folder1.add( text, '').step(1).listen();
  // var line3 = folder1.add( text, '' ).step(1).listen();
  // folder1.open();

  line1.onChange(function(newValue)
    { theText = newValue});

  var folder2 = gui.addFolder('position');
  var textX = folder2.add( parameters, 'x' ).min(-400).max(200).step(1).listen();
  var textY = folder2.add( parameters, 'y' ).min(0).max(100).step(1).listen();
  var textZ = folder2.add( parameters, 'z' ).min(-200).max(200).step(1).listen();
  // folder2.open();

  var folder3 = gui.addFolder('size');

  textX.onChange(function(value) 
  {   text.position.x = value;   });
  textY.onChange(function(value) 
  {   text.position.y = value;   });
  textZ.onChange(function(value) 
  {   text.position.z = value;   });

Answer №1

Your input :

line1.onChange(function(newValue)
{ theText = newValue});

does not cause any changes to the appearance of the text on the visual mesh.

To update the mesh with the new text, you must reconfigure it using the following code :

line1.onChange(function(newValue)
{ 
    var text3d = new THREE.TextGeometry( newValue, {
        size: 80,
        height: 80,
        curveSegments: 2,
        font: "helvetiker",
        weight: "bold"
      });
      text3d.computeBoundingBox();
      text = new THREE.Mesh( text3d, textMaterial );
 });

Answer №2

Using the canvas and font libraries to pass the text variable, rather than generating it within THREE.TextGeometry. I have created a function to input values and create a new text mesh each time. Considering implementing an update or refresh function in the GUI for users to erase old meshes if needed.

line1.onFinishChange(function(newValue) {
  function createText() {
    var hash = document.location.hash.substr(1);
    if (hash.length !== 0) { newValue = hash; }
    var text3d = new THREE.TextGeometry(newValue, {
      size: 80,
      height: 80,
      curveSegments: 2,
      font: "helvetiker",
      weight: "bold"
    });
    text3d.computeBoundingBox();

    var centerOffset = -0.5 * (text3d.boundingBox.max.x - text3d.boundingBox.min.x);

    var textMaterial = new THREE.MeshNormalMaterial;
    text = new THREE.Mesh(text3d, textMaterial);
    text.position.x = centerOffset;
    text.position.y = 0;
    text.position.z = 0;
    text.rotation.x = 0;
    text.rotation.y = Math.PI * 2;
    parent = new THREE.Object3D();
    parent.add(text);
    scene.add(parent);

    var axes = new THREE.AxisHelper();
    scene.add(axes);
  }
  
  createText();
});

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 there a way in JavaScript to format an array's output so that numbers are displayed with only two decimal places?

function calculateTipAmount(bill) { var tipPercent; if (bill < 50 ) { tipPercent = .20; } else if (bill >= 50 && bill < 200){ tipPercent = .15; } else { tipPercent = .10; } return tipPercent * bill; } var bills = ...

using javascript to invoke php function

I encountered an issue when attempting to call a PHP function from JavaScript. The code I used is displayed below: <html> <head></head> <body> <script type="text/javascript" > function header() { <?php ...

When I click on my div, the color does not change as expected

Recently, I wrote a code snippet where there are 9 different div elements each assigned a ".spaces" class. The intention was to change the color of these divs upon clicking on them. However, I encountered an issue where only the first div changes its color ...

Error: The filter argument must be in object form

Currently I am in the process of developing a REST API with an endpoint for posting movies. The request body is expected to contain only the movie title, which needs to be validated for presence. Upon receiving the title, I need to fetch other movie detail ...

HTML table containing radio buttons styled with Font Awesome icons

I'm having some trouble getting a radio button with Font Awesome to work properly within an HTML table. Outside of the table, it functions as expected, but inside the table, it only seems to hide/show between the two states without displaying the chec ...

When incorporating a JS React component in TypeScript, an error may occur stating that the JSX element type 'MyComponent' is not a valid constructor function for JSX elements

Currently, I am dealing with a JavaScript legacy project that utilizes the React framework. Within this project, there are React components defined which I wish to reuse in a completely different TypeScript React project. The JavaScript React component is ...

Adding a custom validation function to the joi.any() method - the easy way!

Is there a way to enhance joi.any() with a new rule that can be applied to any existing type, such as joi.boolean() or joi.string()? I already know how to extend joi by creating a custom type but that doesn't allow me to combine the new type with exis ...

Ways to emphasize search outcomes in Flask/HTML?

I am currently working on creating a search box using HTML and the Flask framework in Python. Below is the layout I am working with: Layout My goal is to input text into the search box and have it highlighted within the text area on the same HTML page. F ...

Loop through the ng-repeat scope object in AngularJS

Is it possible to create rows of 3 using bootstrap and ng-repeat with data from a scope object without the loop repeating every three times due to ng-if? I am curious if something like <h4> {{yogurt[$index+1].name}} </h4> would work. Below is ...

Utilizing an unknown provider in AngularJS constants: a guide

Can anyone help me figure out what's going on with this code snippet? var app = angular.module('myApp', []); app.constant('_START_REQUEST_', '_START_REQUEST_'); app.constant('_END_REQUEST_&ap ...

Upgrading from version 3 to version 5 in d3 does not just update the visualization but also introduces a new one

I attempted to upgrade this d3 visualization from version 3 to version 5, but instead of updating within the existing visualization, it continues to add another visualization below. I included: d3.select(".node").selectAll("*").remove(); d3.select(". ...

Enlarging an image on canvas and creating a repeating pattern

My canvas size is 500px x 500px. I have a png image that matches the canvas size, which you can view here. I am looking to resize the image to 100px x 100px and then use it as part of a repeat pattern on the canvas. This is how I achieve this: // First d ...

Substitute numerical strings with actual numbers within an array

I need to transform an array from arr = ["step","0","instruction","1"] to newArr = ["step",0,"instruction",1] Below is the code I am using for this operation: newArr = arr.map((x) => { ...

Is there a way to simulate Pinia and vue-i18n simultaneously?

Exploring Vue 3's Composition API with a twist: The store.ts file import { ref, Ref } from 'vue'; import { defineStore } from 'pinia'; export const useStore = defineStore('store', () => { const isLoading: Ref<bo ...

Why is the decision made to simply remove an item? Is there an option to instead choose and delete the specific item?

I am facing an issue with my form where I have a function to dynamically add inputs using "append" and another button to remove the inputs added. When I use the "last-child" selector to delete the last generated div, the remove button stops working and I a ...

Tips for displaying <p> element upon hovering over a specific area of an SVG image

I created a map and converted it to SVG. I want to display the name of each neighborhood when a user hovers the mouse over it. I'm not sure how to achieve this or how to isolate the specific area of a neighborhood to make changes. Here's a scre ...

When you click on the button, the section will not be displayed

I'm facing an issue with my code. I have a set of five buttons and corresponding sections. When a button is clicked, the active and active-btn classes are supposed to be added to that button as well as the corresponding section element with the same i ...

Prevent ESLint from linting files with non-standard extensions

My .estintrc.yaml: parser: "@typescript-eslint/parser" parserOptions: sourceType: module project: tsconfig.json tsconfigRootDir: ./ env: es6: true browser: true node: true mocha: true plugins: - "@typescript-eslint" D ...

Retrieve information from a JSON file containing multiple JSON objects for viewing purposes

Looking for a solution to access and display specific elements from a JSON object containing multiple JSON objects. The elements needed are: 1) CampaignName 2) Start date 3) End date An attempt has been made with code that resulted in an error displayed ...

Struggling to get getInitialProps working in dynamic routes with Next.js?

I am encountering an issue. The return value from the getInitialProps function is not being passed to the parent component. However, when I console.log the values inside the getInitialProps function, they appear to be correct. Here is the code snippet: i ...