RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题 / 1525086
Accepted
timob256
timob256
Asked:2023-06-11 03:13:52 +0000 UTC2023-06-11 03:13:52 +0000 UTC 2023-06-11 03:13:52 +0000 UTC

three.js 库中不显示文字

  • 772

我正在尝试显示文本。这是对附加文本的对象的调用:

  const earthGeometry1 =  new THREE.SphereGeometry( 0, 0, 0 );
            const earthMaterial1=0;
            earth1 = new THREE.Mesh( earthGeometry1, earthMaterial1 );
            scene.add( earth1 );

然后是正文:

const earthDiv1 = document.createElement( 'div' );
            earthDiv1.className = 'label';
            earthDiv1.textContent = 'Earth';
            earthDiv1.style.backgroundColor = 'transparent';

            const earthLabel1 = new CSS2DObject( earthDiv );
            earthLabel1.position.set( 1.5 * EARTH_RADIUS, 0, 0 );
            earthLabel1.center.set( 0, 1 );
            earth1.add( earthLabel1 );
            earthLabel.layers.set( 0 );

            const earthMassDiv1 = document.createElement( 'div' );
            earthMassDiv1.className = 'label';
            earthMassDiv1.textContent = '5.97237e24 kg';
            earthMassDiv1.style.backgroundColor = 'transparent';

            const earthMassLabel1 = new CSS2DObject( earthMassDiv );
            earthMassLabel1.position.set( 1.5 * EARTH_RADIUS, 0, 0 );
            earthMassLabel1.center.set( 0, 0 );
            earth1.add( earthMassLabel1 );
            earthMassLabel1.layers.set( 1 );

但由于某种原因,文本未显示。

这是整个代码:

body{
  overflow: hidden;
  margin: 0;
}
<script async src="https://ga.jspm.io/npm:es-module-shims@1.6.3/dist/es-module-shims.js" crossorigin="anonymous"></script>
<script type="importmap">
  {
    "imports": {
      "three": "https://unpkg.com/three@0.152.0/build/three.module.js",
      "three/addons/": "https://unpkg.com/three@0.152.0/examples/jsm/"
    }
  }
</script>
<script type="module">

import * as THREE from 'three';

        import { OrbitControls } from 'three/addons/controls/OrbitControls.js';

        import Stats from 'three/addons/libs/stats.module.js';

       import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
       
       import { CSS2DRenderer, CSS2DObject } from 'three/addons/renderers/CSS2DRenderer.js';

        let gui;

        let camera, scene, renderer, labelRenderer;

        const layers = {

            'Toggle Name': function () {

                camera.layers.toggle( 0 );

            },
            'Toggle Mass': function () {

                camera.layers.toggle( 1 );

            },
            'Enable All': function () {

                camera.layers.enableAll();

            },

            'Disable All': function () {

                camera.layers.disableAll();

            }

        };
        
        const clock = new THREE.Clock();
        const textureLoader = new THREE.TextureLoader();

        let moon;
        let earth1;

        init();
        animate();

        function init() {

            const EARTH_RADIUS = 1;
            const MOON_RADIUS = 0.27;

            camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 0.1, 200 );
            camera.position.set( 10, 5, 20 );
            camera.layers.enableAll();

            scene = new THREE.Scene();
            scene.background = new THREE.Color( 0x94afb5 );

            const dirLight = new THREE.DirectionalLight( 0xffffff, 1 );
            dirLight.position.set( 0, 0, 1 );
            dirLight.layers.enableAll();
            scene.add( dirLight );

            //const axesHelper = new THREE.AxesHelper( 5 );
            //axesHelper.layers.enableAll();
            //scene.add( axesHelper );

            //
            const earthGeometry1 =  new THREE.SphereGeometry( 0, 0, 0 );
            const earthMaterial1=0;
            earth1 = new THREE.Mesh( earthGeometry1, earthMaterial1 );
            scene.add( earth1 );
            
            const earthGeometry = new THREE.SphereGeometry( EARTH_RADIUS, 16, 16 );
              const earthMaterial = new THREE.MeshPhongMaterial( {
                specular: 0x333333,
                shininess: 5,
                map: textureLoader.load( 'textures/planets/earth_atmos_2048.jpg' ),
                specularMap: textureLoader.load( 'textures/planets/earth_specular_2048.jpg' ),
                normalMap: textureLoader.load( 'textures/planets/earth_normal_2048.jpg' ),
                normalScale: new THREE.Vector2( 0.85, 0.85 )
            } ); 
            earthMaterial.map.colorSpace = THREE.SRGBColorSpace;
            const earth = new THREE.Mesh( earthGeometry, earthMaterial );
            scene.add( earth );

            const moonGeometry = new THREE.SphereGeometry( MOON_RADIUS, 16, 16 );
            const moonMaterial = new THREE.MeshPhongMaterial( {
                shininess: 5,
                map: textureLoader.load( 'textures/planets/moon_1024.jpg' )
            } );
            moonMaterial.map.colorSpace = THREE.SRGBColorSpace;
            moon = new THREE.Mesh( moonGeometry, moonMaterial );
            scene.add( moon );
            
            //--------------Создаем объект-------------------
const points = [];
points.push(new THREE.Vector3(-10, 10, 0));
points.push(new THREE.Vector3(10, 10, 0));
points.push(new THREE.Vector3(10, -10, 0));
points.push(new THREE.Vector3(-10, -10, 0));
points.push(new THREE.Vector3(-10, 10, 0));
const osxyMaterial = new THREE.LineBasicMaterial({ color: 0x7457bd, linewidth: 1 });
const osxyGeometry = new THREE.BufferGeometry().setFromPoints(points);
const osxy = new THREE.Line(osxyGeometry, osxyMaterial);
scene.add(osxy);

const points1 = [];
points1.push(new THREE.Vector3(0, -10, 10));//Указываем вектор первой точки
points1.push(new THREE.Vector3(0, 10, 10));//Указываем вектор второй точки
points1.push(new THREE.Vector3(0, 10, -10));
points1.push(new THREE.Vector3(0, -10, -10));
points1.push(new THREE.Vector3(0, -10, 10));
const osxzMaterial = new THREE.LineBasicMaterial({ color: 0x3d803d, linewidth: 1 });
const osxzGeometry = new THREE.BufferGeometry().setFromPoints(points1);
const osxz = new THREE.Line(osxzGeometry, osxzMaterial);
scene.add(osxz);

const points2 = []
points2.push(new THREE.Vector3(-10, 0, 10));//Указываем вектор первой точки
points2.push(new THREE.Vector3(10, 0, 10));//Указываем вектор второй точки
points2.push(new THREE.Vector3(10, 0, -10));
points2.push(new THREE.Vector3(-10, 0, -10));
points2.push(new THREE.Vector3(-10, 0, 10));
const osyzMaterial = new THREE.LineBasicMaterial({ color: 0xab5641, linewidth: 1 })
const osyzGeometry = new THREE.BufferGeometry().setFromPoints(points2)
const osyz = new THREE.Line(osyzGeometry, osyzMaterial)
scene.add(osyz) 

            //

            earth.layers.enableAll();
            moon.layers.enableAll();

            const earthDiv = document.createElement( 'div' );
            earthDiv.className = 'label';
            earthDiv.textContent = 'Earth';
            earthDiv.style.backgroundColor = 'transparent';

            const earthLabel = new CSS2DObject( earthDiv );
            earthLabel.position.set( 1.5 * EARTH_RADIUS, 0, 0 );
            earthLabel.center.set( 0, 1 );
            earth.add( earthLabel );
            earthLabel.layers.set( 0 );

            const earthMassDiv = document.createElement( 'div' );
            earthMassDiv.className = 'label';
            earthMassDiv.textContent = '5.97237e24 kg';
            earthMassDiv.style.backgroundColor = 'transparent';

            const earthMassLabel = new CSS2DObject( earthMassDiv );
            earthMassLabel.position.set( 1.5 * EARTH_RADIUS, 0, 0 );
            earthMassLabel.center.set( 0, 0 );
            earth.add( earthMassLabel );
            earthMassLabel.layers.set( 1 );
            
            //-
            const earthDiv1 = document.createElement( 'div' );
            earthDiv1.className = 'label';
            earthDiv1.textContent = 'Earth';
            earthDiv1.style.backgroundColor = 'transparent';

            const earthLabel1 = new CSS2DObject( earthDiv );
            earthLabel1.position.set( 1.5 * EARTH_RADIUS, 0, 0 );
            earthLabel1.center.set( 0, 1 );
            earth1.add( earthLabel1 );
            earthLabel.layers.set( 0 );

            const earthMassDiv1 = document.createElement( 'div' );
            earthMassDiv1.className = 'label';
            earthMassDiv1.textContent = '5.97237e24 kg';
            earthMassDiv1.style.backgroundColor = 'transparent';

            const earthMassLabel1 = new CSS2DObject( earthMassDiv );
            earthMassLabel1.position.set( 1.5 * EARTH_RADIUS, 0, 0 );
            earthMassLabel1.center.set( 0, 0 );
            earth1.add( earthMassLabel1 );
            earthMassLabel1.layers.set( 1 );
//-

            const moonDiv = document.createElement( 'div' );
            moonDiv.className = 'label';
            moonDiv.textContent = 'Moon';
            moonDiv.style.backgroundColor = 'transparent';

            const moonLabel = new CSS2DObject( moonDiv );
            moonLabel.position.set( 1.5 * MOON_RADIUS, 0, 0 );
            moonLabel.center.set( 0, 1 );
            moon.add( moonLabel );
            moonLabel.layers.set( 0 );

            const moonMassDiv = document.createElement( 'div' );
            moonMassDiv.className = 'label';
            moonMassDiv.textContent = '7.342e22 kg';
            moonMassDiv.style.backgroundColor = 'transparent';

            const moonMassLabel = new CSS2DObject( moonMassDiv );
            moonMassLabel.position.set( 1.5 * MOON_RADIUS, 0, 0 );
            moonMassLabel.center.set( 0, 0 );
            moon.add( moonMassLabel );
            moonMassLabel.layers.set( 1 );

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

            labelRenderer = new CSS2DRenderer();
            labelRenderer.setSize( window.innerWidth, window.innerHeight );
            labelRenderer.domElement.style.position = 'absolute';
            labelRenderer.domElement.style.top = '0px';
            document.body.appendChild( labelRenderer.domElement );

            const controls = new OrbitControls( camera, labelRenderer.domElement );
            controls.minDistance = 5;
            controls.maxDistance = 100;

            //

            window.addEventListener( 'resize', onWindowResize );

            initGui();

        }

        function onWindowResize() {

            camera.aspect = window.innerWidth / window.innerHeight;

            camera.updateProjectionMatrix();

            renderer.setSize( window.innerWidth, window.innerHeight );

            labelRenderer.setSize( window.innerWidth, window.innerHeight );

        }


        function animate() {

            requestAnimationFrame( animate );

            const elapsed = clock.getElapsedTime();

            moon.position.set( Math.sin( elapsed ) * 5, 0, Math.cos( elapsed ) * 5 );

            renderer.render( scene, camera );
            labelRenderer.render( scene, camera );

        }

        //

        function initGui() {

            gui = new GUI();

            gui.title( 'Camera Layers' );

            gui.add( layers, 'Toggle Name' );
            gui.add( layers, 'Toggle Mass' );
            gui.add( layers, 'Enable All' );
            gui.add( layers, 'Disable All' );

            gui.open();

        }

    </script>

gui
  • 1 1 个回答
  • 16 Views

1 个回答

  • Voted
  1. Best Answer
    timob256
    2023-06-13T19:03:23Z2023-06-13T19:03:23Z

    曾是

    const earthLabel1 = new CSS2DObject(earthDiv);
    const earthMassLabel1 = new CSS2DObject(earthMassDiv);
    

    它变成了

    const earthLabel1 = new CSS2DObject(earthDiv1);
    const earthMassLabel1 = new CSS2DObject(earthMassDiv1);
    

    在此处输入图像描述

    • 1

相关问题

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    我看不懂措辞

    • 1 个回答
  • Marko Smith

    请求的模块“del”不提供名为“default”的导出

    • 3 个回答
  • Marko Smith

    "!+tab" 在 HTML 的 vs 代码中不起作用

    • 5 个回答
  • Marko Smith

    我正在尝试解决“猜词”的问题。Python

    • 2 个回答
  • Marko Smith

    可以使用哪些命令将当前指针移动到指定的提交而不更改工作目录中的文件?

    • 1 个回答
  • Marko Smith

    Python解析野莓

    • 1 个回答
  • Marko Smith

    问题:“警告:检查最新版本的 pip 时出错。”

    • 2 个回答
  • Marko Smith

    帮助编写一个用值填充变量的循环。解决这个问题

    • 2 个回答
  • Marko Smith

    尽管依赖数组为空,但在渲染上调用了 2 次 useEffect

    • 2 个回答
  • Marko Smith

    数据不通过 Telegram.WebApp.sendData 发送

    • 1 个回答
  • Martin Hope
    Alexandr_TT 2020年新年大赛! 2020-12-20 18:20:21 +0000 UTC
  • Martin Hope
    Alexandr_TT 圣诞树动画 2020-12-23 00:38:08 +0000 UTC
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5