Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/369.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使用Three.js鼠标拾取_Javascript_Three.js_Mouse Picking - Fatal编程技术网

Javascript 如何使用Three.js鼠标拾取

Javascript 如何使用Three.js鼠标拾取,javascript,three.js,mouse-picking,Javascript,Three.js,Mouse Picking,我想将对象拾取添加到此代码中: var Three = new function () { this.scene = new THREE.Scene() this.camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 1, 1000) this.camera.position.set(20, 52, 20); this.camera.rotation.or

我想将对象拾取添加到此代码中:

var Three = new function () {
    this.scene = new THREE.Scene()

    this.camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 1, 1000)
    this.camera.position.set(20, 52, 20);
    this.camera.rotation.order = 'YXZ';
    this.camera.rotation.y = -Math.PI / 4;
    this.camera.rotation.x = Math.atan(-1 / Math.sqrt(2));
    this.camera.scale.addScalar(1);

    this.renderer = new THREE.WebGLRenderer()
    this.renderer.setSize(window.innerWidth, window.innerHeight);

    var ground = new THREE.Mesh(new THREE.PlaneBufferGeometry(436, 624), new THREE.MeshBasicMaterial({map: THREE.ImageUtils.loadTexture('/img/maps/1.png')}));
    ground.rotation.x = -Math.PI / 2; //-90 degrees around the x axis
    this.scene.add(ground);

    var light = new THREE.PointLight(0xFFFFDD);
    light.position.set(-1000, 1000, 1000);
    this.scene.add(light);

    var loader = new THREE.JSONLoader();
    this.loadCastle = function (color, x, y) {
        loader.load('/models/castle.json', getGeomHandler(color, x * 4 - 214, y * 4 - 309, 0.5));
    }
    this.init = function () {
        $('#game').append(Three.renderer.domElement);
        Three.render();
    }
    this.render = function () {
        requestAnimationFrame(Three.render);
        Three.renderer.render(Three.scene, Three.camera);
    };
}
我怎样才能用最简单的方法做到这一点

p、 美国。 只有使用“loadCastle”方法加载的castles网格,我希望能够拾取它们

编辑:

我试过这样的AlmazVildanov sugestion:

function getGeomHandler(color, x, y, scale) {
    return function (geometry) {
        var model = new THREE.Mesh(geometry, new THREE.MeshLambertMaterial({color: color}));
        model.scale.set(scale, scale, scale);
        model.position.set(x, 0, y);
        model.name = color + '_' + x + '_' + y;
        Three.scene.add(model);
        EventsControls.attach(model);
    };
}

EventsControls = new EventsControls(Three.camera, Three.renderer.domElement);
EventsControls.displacing = false;
EventsControls.onclick = function () {
    console.log('aaa')
    console.log(this.focused.name)
}
但当我点击模型时,并没有任何事情发生,控制台中并没有消息

编辑2:
对象拾取工作正常。已解决。

我的问题的解决方案是使用@AlmazVildanov EventsControl

查看@AlmazVildanov哪里可以获得EventsControl.js?@AlmazVildanov如果我有很多网格,但只有很少的网格需要拾取,我可以用你的库编程忽略它们吗?是的。带有代码
eventsControl.attach(模型)
事件控件分离(模型)