Не выводится текст в библиотеке three.js

Рейтинг: 0Ответов: 1Опубликовано: 10.06.2023

Пытаюсь вывести текст. Вот вызов объекта к которому крепится текст :

  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>

Ответы

▲ 1Принят

Было

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

Стало

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

введите сюда описание изображения