three.js - get object name with mouse click

前端 未结 2 1295
温柔的废话
温柔的废话 2020-12-16 08:45

I had loaded 3 external model with the name into my scene using json loader and now i want to get the name of the model/object by clicking it.

Below is the that i ha

2条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-16 09:11

    Try to make through this example. Look at messages in the console.

    
    
    EventsControls = new EventsControls( camera, renderer.domElement );
    
    EventsControls.attachEvent( 'onclick', function() {
    
        console.log( 'this.focused.name: ' + this.focused.name );
    
    });
    

    // if use drag and drop

    EventsControls.attachEvent( 'dragAndDrop', function () {
    
        this.container.style.cursor = 'move';
        this.focused.position.y = this.previous.y;
    
    });
    
    EventsControls.attachEvent( 'mouseOut', function () {
    
        this.container.style.cursor = 'auto';
    
    });
    
    var jsonLoader = new THREE.JSONLoader();
    jsonLoader.load( "models/Tux.js", addModelToScene ); 
    
    
    function addModelToScene( geometry, materials ) {
    
       var material = new THREE.MeshFaceMaterial( materials );
       model = new THREE.Mesh( geometry, material );
       model.scale.set( 10, 10, 10 ); model.name = 'Tux';
       model.rotation.x = -Math.PI/2; 
       model.position.set( 175, 45, 125 );
       scene.add( model ); 
       EventsControls.attach( model );
    
    }
    

提交回复
热议问题