0

当我以这种方式创建多边形时,我正在使用 three.js 创建可点击的多边形

var geo = new THREE.Geometry();
            geo.vertices.push(new THREE.Vector3(0.3, 0.3, 0.5));
            geo.vertices.push(new THREE.Vector3(0.3, 0.4, 0.5));
            geo.vertices.push(new THREE.Vector3(0.4, 0.4, 0.5));
            geo.vertices.push(new THREE.Vector3(0.6, 0.35, 0.5));
            geo.vertices.push(new THREE.Vector3(0.4, 0.3, 0.5));                

            for (var face = 0 ; face < 5 - 2; face++) {
                // this makes a triangle fan, from the first +Y point around
                geo.faces.push(new THREE.Face3(0, face + 1, face + 2));
            }

            var mesh = new THREE.Mesh(geo, new THREE.MeshBasicMaterial({ color: Math.random() * 0xffffff, opacity: 0.5 }));
            geo.computeFaceNormals();

            layer.add(mesh);
            objects.push(mesh);

它显示,但多边形不可点击。如果我以这种方式创造

var geometry = new THREE.CubeGeometry(0.02, 0.02, 0.02);
            var object = new THREE.Mesh(geometry, new THREE.MeshBasicMaterial({ color: Math.random() * 0xffffff, opacity: 0.5 }));

            object.position.x = 0.5;
            object.position.y = 0.5;
            object.position.z = 0.5;

            layer.add(object);
            objects.push(object);

一切正常,立方体是可点击的,但我需要一个多边形。点击事件方法

function onDocumentMouseClick(event) {
        layerMap.update();
        var vector = new THREE.Vector3((event.clientX / window.innerWidth) * 2 - 1, -(event.clientY / window.innerHeight) * 2 + 1, 0.5);
        var ray = projector.pickingRay(vector, camera);
        var intersects = ray.intersectObjects(objects);
        if (intersects.length > 0) {
            intersects[0].object.material.color.setHex(Math.random() * 0xffffff);
        }
    }

我有太多不同的多边形如何创建可点击的多边形?

4

2 回答 2

0

You have to use the intersectTriangle() function of the THREE.Ray class. If object is your THREE.Mesh loop over your triangles and check for intersections like this:

function onDocumentMouseClick(event) 
{

    var vector = new THREE.Vector3((event.clientX / window.innerWidth) * 2 - 1, -(event.clientY / window.innerHeight) * 2 + 1, 0.5);
    var ray = projector.pickingRay(vector, camera);
    var currentTriangle;
    var currentIntersection, chosenIntersection;
    var currentDistance, chosenDistance;
    var distanceVector;

    chosenIntersection = null;
    chosenDistance = Infinity;

    var vertices = object.geometry.vertices;

    for (var i = 0; i < object.geometry.faces.length; i++)
    {
      currentTriangle = object.geometry.faces[i];
      currentIntersection = ray.intersectTriangle( vertices[ currentTriangle.a ], vertices[ currentTriangle.b ], vertices[ currentTriangle.c ], true );

      if( currentIntersection !== null )
      {
        // The following code checks if a found intersection is closer to the camera than a previous one.
        distanceVector.subVectors(currentIntersection, ray.origin);
        currentDistance = distanceVector.length();

        if( currentDistance < chosenDistance)
        {
          chosenIntersection = currentIntersection;
          chosenDistance = currentDistance;
        }
      }   
   }
}
于 2013-09-14T13:34:45.477 回答
0

You have two errors.

Your OrthographicCamera constructor args are incorrect -- it is upside down, and the near plane is behind the camera.

//this.camera = new THREE.OrthographicCamera(0, 1, 0, 1, -3000, 3000);
this.camera = new THREE.OrthographicCamera(0, 1, 1, 0, 1, 3000);

The winding order on your geometry is clockwise; it should be counter-clockwise (CCW).

//geo.faces.push(new THREE.Face3(0, face + 1, face + 2));
geo.faces.push(new THREE.Face3(0, face + 2, face + 1));

Tip: debug with the non-minified version of three.js. Also, download a local copy for your use.

three.js r.60

于 2013-09-14T14:53:52.127 回答