多个移动对象,每个对象都附有文本

时间:2022-05-21 06:26:16

I've been playing with the interactive cubes and decided to add a click function, which leads to a specific link. Now I would like to add unique text on each block which is rendered as a material. For right now, I am using a single material, so I am thinking I need to create an array for all my possible materials, however whenever I reference that array in the parameters for building the mesh of my objects, I get a setHex() is undefined returned to me in the console. How can I attach a unique material to each cube?

我一直在玩互动立方体,并决定添加一个点击功能,这将导致一个特定的链接。现在我想在每个作为材料渲染的块上添加唯一文本。现在,我正在使用单一材质,所以我想我需要为所有可能的材质创建一个数组,但每当我在参数中引用该数组来构建我的对象的网格时,我得到一个setHex()未定义在控制台中返回给我。如何将独特材料附加到每个立方体?

<html lang="en">
  <head>
    <title>three.js webgl - interactive cubes</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
    <style>
      body {
        font-family: Monospace;
        background-color: #f0f0f0;
        margin: 0px;
        overflow: hidden;
      }

      div#stats{
        display:none;
      }

      p{
        position:absolute;
        top:500px;
        left:500px;
        }
    </style>
  </head>
  <body>
  <div><p>Hello!</p></div>
    <script src="../build/three.min.js"></script>
    <script src="js/renderers/Projector.js"></script>
    <script src='../build/threex.dynamictexture.js'></script>
    <script src="js/libs/stats.min.js"></script>

    <script>

      var container, stats;
      var camera, scene, raycaster, renderer;
      var mouse = new THREE.Vector2(), INTERSECTED;
      var radius = 100, theta = 0;




      init();
      animate();

      function init() {

        container = document.createElement( 'div' );
        document.body.appendChild( container );

        var info = document.createElement( 'div' );
        info.style.position = 'absolute';
        info.style.top = '10px';
        info.style.width = '100%';
        info.style.textAlign = 'center';
        // info.innerHTML = '<a href="http://threejs.org" target="_blank">three.js</a> webgl - interactive cubes';
        container.appendChild( info );

        camera = new THREE.PerspectiveCamera( 100, window.innerWidth / window.innerHeight, 1, 10000 );

        scene = new THREE.Scene();



        var light = new THREE.DirectionalLight( 0x6699CC, 0.5 );
        light.position.set( 1, 1, 1 ).normalize();
        scene.add( light );


        raycaster = new THREE.Raycaster();
        projector = new THREE.Projector();
        renderer = new THREE.WebGLRenderer();
        renderer.setClearColor( 0xf0f0f0 );
        renderer.setPixelRatio( window.devicePixelRatio );
        renderer.setSize( window.innerWidth, window.innerHeight );
        renderer.sortObjects = false;
        container.appendChild(renderer.domElement);





      var dynamicTexture = new THREEx.DynamicTexture(512,512);
      var dynamicTexture2 = new THREEx.DynamicTexture(512,512);
      dynamicTexture.context.font = "bold"+(0.2*512)+ "px Courier";
      dynamicTexture.texture.anisotropy = renderer.getMaxAnisotropy();
        dynamicTexture2.context.font = "bold"+(0.2*512)+ "px Courier";
      dynamicTexture2.texture.anisotropy = renderer.getMaxAnisotropy();

dynamicTexture2.clear('blue');
      dynamicTexture.drawTextCooked({
        text: 'alltheletters',
        lineHeight: 0.2,
      })



dynamicTexture.clear('gray');
      dynamicTexture.drawTextCooked({
        text: 'wow text text text',
        lineHeight: 0.2,
      })

  // var materialArray = [];
  //       materialArray.push(new THREE.MeshBasicMaterial({map: dynamicTexture2 }))   



   scene.add(object);


        for ( var i = 0; i < 5; i ++ ) {




   var object = new THREE.Mesh( geometry, material );
          object.position.x = Math.random() * 800 - 400;
          object.position.y = Math.random() * 800 - 400;
          object.position.z = Math.random() * 800 - 400;

          object.rotation.x = Math.random() * 2 * Math.PI;
          object.rotation.y = Math.random() * 2 * Math.PI;
          object.rotation.z = Math.random() * 2 * Math.PI;

          object.scale.x = Math.random() + 0.5;
          object.scale.y = Math.random() + 0.5;
          object.scale.z = Math.random() + 0.5;
  scene.add( object );

          var geometry = new THREE.BoxGeometry( 62, 62, 62 );
        var material = new THREE.MeshBasicMaterial({
          map: dynamicTexture.texture,
          color:Math.random() * 0xffffff,
          name: "box1",
        })

               switch (i) {
            case 0:
                object.userData = {
                    URL: "http://google.com"
                };
                break;
            case 1:
                object.userData = {
                    URL: "http://yahoo.com"
                };
                break;
            case 2:
                object.userData = {
                    URL: "http://msn.com"
                };
                break;
            case 3:
                object.userData = {
                    URL: "http://engadget.com"
                };
                break;
            case 4:
                object.userData = {
                    URL: "http://*.com"
                };
                break;
        }



        }



        stats = new Stats();
        stats.domElement.style.position = 'absolute';
        stats.domElement.style.top = '0px';
        container.appendChild( stats.domElement );









        document.addEventListener('mousedown', onDocumentMouseDown,false);
        document.addEventListener( 'mousemove', onDocumentMouseMove, false );

        //

        window.addEventListener( 'resize', onWindowResize, false );

      }



      function onWindowResize() {

        camera.aspect = window.innerWidth / window.innerHeight;
        camera.updateProjectionMatrix();

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

      }



      function onDocumentMouseMove( event ) {

        event.preventDefault();

        mouse.x = ( event.clientX / window.innerWidth ) * 2 - 1;
        mouse.y = - ( event.clientY / window.innerHeight ) * 2 + 1;

      }

      function onDocumentMouseDown(event) {
    event.preventDefault();
    var vector = new THREE.Vector3((event.clientX / window.innerWidth) * 2 -
        1, -(event.clientY / window.innerHeight) * 2 + 1, 0.5);
    projector.unprojectVector(vector, camera);
    var raycaster = new THREE.Raycaster(camera.position, vector.sub(camera.position)
        .normalize());
    var intersects = raycaster.intersectObjects(scene.children);
    if (intersects.length > 0) {
        window.open(intersects[0].object.userData.URL);
    }
}


      //

      function animate() {

        requestAnimationFrame( animate );

        render();
        stats.update();

      }



      function render() {

        theta += 0.1;

        camera.position.x = radius * Math.sin( THREE.Math.degToRad( theta ) );
        camera.position.y = radius * Math.sin( THREE.Math.degToRad( theta ) );
        camera.position.z = radius * Math.cos( THREE.Math.degToRad( theta ) );
        camera.lookAt( scene.position );

        camera.updateMatrixWorld();

        // find intersections

        raycaster.setFromCamera( mouse, camera );
     var intersects = raycaster.intersectObjects( scene.children ), material;

//   for(var p =0; p < intersects.length; p++){
// }



   if (intersects.length > 0) {


        if (INTERSECTED != intersects[0].object) {

            if (INTERSECTED){
                material = INTERSECTED.material;
                if(material.emissive){
                    material.emissive.setHex(INTERSECTED.currentHex);
                }
                else{
                    material.color.setHex(INTERSECTED.currentHex);
                }
            }  
            INTERSECTED = intersects[0].object;

            material = INTERSECTED.material;
            if(material.emissive){
                INTERSECTED.currentHex = INTERSECTED.material.emissive.getHex();
                material.emissive.setHex(0xff0000);
            }
            else{
                INTERSECTED.currentHex = material.color.getHex();
                material.color.setHex(0xff0000);
            }

            console.log(INTERSECTED.position);
        }

    }


     else {

        if (INTERSECTED){
            material = INTERSECTED.material;

            if(material.emissive){
                material.emissive.setHex(INTERSECTED.currentHex);
            }
            else
            {
                material.color.setHex(INTERSECTED.currentHex);
            }
        }

        INTERSECTED = null;

    }


        renderer.render( scene, camera );

      }

    </script>

  </body>
</html>

1 个解决方案

#1


0  

I don't think the problem is the array, its the object in the array. Try this:

我不认为问题是数组,它是数组中的对象。试试这个:

var materialArray = [];
  materialArray.push(new THREE.MeshBasicMaterial({
    map: dynamicTexture2.texture,
    color:Math.random() * 0xffffff,
    name: "box1",
  }));
  //...
  var object = new THREE.Mesh( geometry, materialArray[0] );

If you want to create multiple cubes with different materials, you'll need to create multiple objects, instead of reusing object. You can wrap all the object creation/positioning code in a function, and have the unique material be a parameter.

如果要创建具有不同材质的多个多维数据集,则需要创建多个对象,而不是重用对象。您可以将所有对象创建/定位代码包装在函数中,并将唯一材质作为参数。

#1


0  

I don't think the problem is the array, its the object in the array. Try this:

我不认为问题是数组,它是数组中的对象。试试这个:

var materialArray = [];
  materialArray.push(new THREE.MeshBasicMaterial({
    map: dynamicTexture2.texture,
    color:Math.random() * 0xffffff,
    name: "box1",
  }));
  //...
  var object = new THREE.Mesh( geometry, materialArray[0] );

If you want to create multiple cubes with different materials, you'll need to create multiple objects, instead of reusing object. You can wrap all the object creation/positioning code in a function, and have the unique material be a parameter.

如果要创建具有不同材质的多个多维数据集,则需要创建多个对象,而不是重用对象。您可以将所有对象创建/定位代码包装在函数中,并将唯一材质作为参数。