【问题标题】:Setting the position of a text geometry?设置文本几何的位置?
【发布时间】:2021-07-17 00:40:41
【问题描述】:

我查看了堆栈溢出和谷歌,发现了如何将文本几何居中,但这不是我想要做的。

我有一个场景,其中只有一段文字显示“在此购买!”。使用three.js网站中的文档和示例here,经过一番努力,我能够做到这一点。由于我在函数中创建了几何图形,因此在查找如何引用该网格时遇到了一些麻烦,并且我花了几个小时才知道将其名称设置为字符串,以便可以从不同的父/子级别访问它.

我现在不能做的是将文本偏移一些任意数量的单位。我尝试将其降低 5 个单位。无论我如何尝试它都不起作用。我要么设法使文本几何消失,要么我的整个场景都是黑色的。

这是我的代码...

我的基本场景设置工作正常,我会将其包含在此处,但请随意跳过,因为我很确定这与问题无关...

import './style.css'
import * as THREE from 'three';
import { OrbitControls } from 'https://cdn.jsdelivr.net/npm/three@0.117.0/examples/jsm/controls/OrbitControls.js';
import TWEEN from 'https://cdn.jsdelivr.net/npm/@tweenjs/tween.js@18.5.0/dist/tween.esm.js';

//BASIC SCENE SETUP
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(window.devicePixelRatio);
document.body.appendChild(renderer.domElement);

//LIGHTS (POINT AND AMBIENT)
const pointLight = new THREE.PointLight(0xFFFFFF);
pointLight.position.set(5, 5, 5);
const ambientLight = new THREE.AmbientLight(0xFFFFFF);
scene.add(pointLight, ambientLight);

//RESIZE WINDOW
window.addEventListener('resize', () => {
    camera.aspect = window.innerWidth / window.innerHeight;
    camera.updateProjectionMatrix();
    renderer.setSize(window.innerWidth, window.innerHeight);
    render();
}, false);

//ORBIT CONTROLS
const controls = new OrbitControls(camera, renderer.domElement);
controls.minDistance = 5;
controls.maxDistance = 70;
controls.enablePan = false;
controls.enableRotate = false;
controls.enableZoom = false;
controls.target.set(0,0,-1);
camera.position.setZ(25);

window.addEventListener("click", (event) => {
        onClick(event);
    })

window.addEventListener("mousemove", onMouseMove);
    
var animate = function() {
    requestAnimationFrame(animate);
    controls.update();
    render();
    TWEEN.update();
        
};
    
function render() {
    renderer.render(scene, camera);
}
    
animate();

这是我的文本对象代码......

var loaderF = new THREE.FontLoader();
    loaderF.load( 'https://threejs.org/examples/fonts/optimer_regular.typeface.json', function ( font ) {
        var geometry = new THREE.TextGeometry( 'Buy Here!', {
            font: font,
            size: 2.3,
            height: 0.1,
            curveSegments: 15,
            bevelEnabled: true,
            bevelThickness: 0.5,
            bevelSize: 0.31,
            bevelSegments: 7
        } );
        geometry.center();
        var material = new THREE.MeshLambertMaterial({color: 0x686868});
        var mesh = new THREE.Mesh( geometry, material );
        mesh.name = "bhText"
        scene.add( mesh );
        mesh.userData = { URL: "http://google.com"};
    } );

这是我尝试过的......

在“var geometry ({...});”下我输入了....

geometry.position.setX(-5);

但是文本对象完全消失了所以我尝试了

geometry.position.setX = -5;

但没有区别,所以我尝试取出

geometry.center();

但结果相同。

然后我尝试使用

mesh.position.x = -5;

有 AND 没有

geometry.center();

但同样,它们都只是让我的文本对象消失。

所以现在我尝试通过在包含的所有内容的外部键入以下代码来从函数外部设置位置

loaderF.load('https.....', function (font){var geometry = .....})

使用我学到的参考......

scene.getObjectByName("bhText").position.x(-5);

但这会使我的整个场景变成空白(黑色)。所以我尝试了类似的变体

scene.getObjectByName("bhText").position.x = -5;
scene.getObjectByName("bhText").position.setX(-5);
scene.getObjectByName("bhText").position.setX = -5;
mesh.position.setX = -5;// I was pretty sure this wasn't going to work since I wasn't 
                        //using the mesh name specifically for when it's inside something
                        //I can't reach because of parent-child relations 

然后再次尝试使用 AND 不使用的每一个

geometry.center();

但它们都让我的场景变黑了。

我只想将它向下移动几个单位。嘘。

谁能告诉我在我的代码中哪里可以设置文本几何图形的位置?谢谢。

【问题讨论】:

    标签: javascript text three.js position


    【解决方案1】:

    我只想将它向下移动几个单位。

    在这种情况下使用mesh.position.y = - 5;。更改x 坐标会将网格向左或向右移动。这是基于您的代码的完整实时示例:

    const scene = new THREE.Scene();
    
    const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
    camera.position.set( 0, 0, 10 );
    
    const renderer = new THREE.WebGLRenderer({
      antialias: true
    });
    renderer.setSize(window.innerWidth, window.innerHeight);
    renderer.setPixelRatio(window.devicePixelRatio);
    document.body.appendChild(renderer.domElement);
    
    const pointLight = new THREE.PointLight(0xFFFFFF);
    pointLight.position.set(5, 5, 5);
    
    const ambientLight = new THREE.AmbientLight(0xFFFFFF);
    scene.add(pointLight, ambientLight);
    
    const loader = new THREE.FontLoader();
    loader.load('https://threejs.org/examples/fonts/optimer_regular.typeface.json', function(font) {
      const geometry = new THREE.TextGeometry('Buy Here!', {
        font: font,
        size: 2,
        height: 0.5
      });
      geometry.center();
      const material = new THREE.MeshLambertMaterial({
        color: 0x686868
      });
      const mesh = new THREE.Mesh(geometry, material);
      mesh.position.y = - 1; // FIX
      
      mesh.name = "bhText"
      scene.add(mesh);
      
      renderer.render(scene, camera);
      
    });
    body {
          margin: 0;
    }
    <script src="https://cdn.jsdelivr.net/npm/three@0.130.1/build/three.min.js"></script>

    【讨论】:

    • 我使用 x 坐标只是因为我想向任何方向移动它,因为它不起作用。问题一定是其他问题,因为我尝试了你建议的方法,但是当我今天阅读你的消息时,我什至做了一个 ctrl+z 来查看当我输入 mesh.position.y=-2; (我最初希望它降低 2 个单位)几天前,它奏效了。所以我之前一定是正确的,还有别的原因。我仍然不知道,令人沮丧的是这种情况发生的频率。感谢您花时间帮助我。
    • 我有一个问题是,如果我想稍后在我的代码中从 if 函数中触发该动作怎么办?那时我不能使用“网格”,因为我的屏幕变黑了。我尝试使用 scene.getObjectByName("bhText") 来引用它,但这不起作用。无论我在代码中的哪个位置,是否有适当的方法来引用它?
    • 一个简单的解决方案是在onLoad() 回调之外声明mesh 变量。例如,在您声明 renderer 变量的同一范围内。然后,您应该能够在代码中的任何位置使用该引用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多