Is it possible to retrieve a list of controllers that are connected to an object in Three.js?

Is there a method in Three.js to retrieve the list of controllers linked to an object? Or perhaps just one controller linked to that object. I have connected a controller to an object, but I do not want the transformController to always be visible on it. Even though I tried setting trnasformController.enabled = false, it did not seem to work. When I detached it, everything seemed fine, but now I need a way to navigate back to the controllers attached to the object if needed. Is there a way to achieve this and detach it when necessary? My goal is to display the controller only when the object is clicked for modification purposes. Thanks in advance.

https://i.sstatic.net/btD55.png

Answer №1

Check out this code snippet that demonstrates attaching the transform control when an object is selected and detaching it upon pressing the Esc key.

Javascript:

var renderer
var saphi_mesh
var control
var controls
var pickable = []

function init() {
    container = document.createElement("div")
    document.body.appendChild(container)

    camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 1, 1000)
    camera.position.z = 500
    camera.position.y = 50

    scene = new THREE.Scene()

    var ambientLight = new THREE.AmbientLight(0xbbbbbb);
    scene.add(ambientLight);
    var gridXZ = new THREE.GridHelper(100, 10, 0xff0000, 0xffffff);
    scene.add(gridXZ);

    var geometry = new THREE.BoxGeometry(20, 20, 20);

    var cube1 = new THREE.Mesh(geometry, new THREE.MeshLambertMaterial({
        color: 0xff1234
    }));
    cube1.position.y = 50;
    cube1.name = "cube1";

    scene.add(cube1);
    pickable.push(cube1);

    renderer = new THREE.WebGLRenderer()
    renderer.setSize(window.innerWidth, window.innerHeight)
    control = new THREE.TransformControls(camera, renderer.domElement);
    pickable.push(control);
    control.addEventListener('change', render);
    container.appendChild(renderer.domElement)
    renderer.domElement.addEventListener("mousedown", onClick, false)
    window.addEventListener("keydown", keydown, false)
}

function animate() {
    requestAnimationFrame(animate)
    render()
}

function render() {
    renderer.render(scene, camera)
}

function keydown(event) {
    if (event.keyCode == 27) {
        scene.remove(control);
    }
}

function onClick(event) {
    x = (event.clientX / window.innerWidth) * 2 - 1;
    y = -(event.clientY / window.innerHeight) * 2 + 1;
    dir = new THREE.Vector3(x, y, -1)
    dir.unproject(camera)

    ray = new THREE.Raycaster(camera.position, dir.sub(camera.position).normalize())

    var intersects = ray.intersectObjects(pickable)
    if (intersects.length > 0) {
        var SELECTED = intersects[0].object;
        scene.add(control);
        control.attach(SELECTED);
    }
}

init()
animate()

Here's a live example on jsfiddle: http://jsfiddle.net/qZh59/3/

I trust this will be useful to you...

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

JavaScript: Learn how to simultaneously open two links from the current webpage

I am trying to create a browser bookmark that will open a link with the id: window.getElementById('updateOk').click() followed by opening a small window with another button: document.getElementsByClassName('rmit-save-details btn btn-inlin ...

Trouble with linkage in jQuery for AJAX requests in an external .js document

My asp.net application has a master file that includes the jquery.js file. On another page that uses this master page, I have my own jquery code. I attempted to move this code to an external file and then include it in my .aspx file. While most functions ...

How do I dynamically generate a new ngController in AngularJS?

Is it possible to dynamically create new ngControllers without using ngRepeat in the template? The scenario is that the user may never instantiate a particular ngController. Here's an example of the current template: <div ng-controller="ViewUsers ...

Executing React's useEffect hook twice

As I work on developing an API using express.js, I have implemented an authentication system utilizing JWT tokens for generating refresh and access tokens. During testing with Jest, Supertest, and Postman, everything appears to be functioning correctly. O ...

Troubleshooting API URL parameter problems

In my content management system (CMS), I am utilizing Laravel as a web API and AngularJS for making requests. An iframe is being used to call services with a direct link using the trusted src function. The issue I'm facing is that I cannot employ a s ...

Adding a <tr> tag to an HTML table using JQuery and AJAX in the context of Django framework step by step

I am currently navigating the world of Javascript, Jquery, and Ajax requests and I'm encountering a challenge with how my scripts are executing. My homepage contains a lengthy list of items (over 1200) that need to be displayed. Previously, I loaded ...

What is the best way to utilize recently added modules in React if they are not listed in the package.json "dependencies" section?

[I have updated my question to provide more details] As a newcomer to working with React, I may be asking a basic question. Recently, I installed several modules and will use one (example: @react-google-maps/api) for clarification. In my PC's termin ...

Creating HTML for an email by iterating through a JavaScript object

I need assistance with iterating through a JavaScript object where each key:value pair is displayed on separate lines in the email body using Appscript. I believe it might need to be converted to an HTML string but I'm not entirely sure. The code snip ...

Where have I gone astray? (Basic Announcer Bot on Discord)

Just a heads up, I haven't really delved into colds much before, just once or twice previously. The starting Bot I downloaded is from this link, but whenever I try to run it in the command prompt, I encounter this issue: C:\Users\aer\D ...

Customize the pagination template in ui-bootstrap to better suit your needs

I have customized the pagination template in AngularJS to display pagination. Here is the modified HTML code: <ul uib-pagination ng-model="source_pagination.current" template-url="pagination.html" total-items="source_pagination.total_pages" items-per-p ...

Utilizing JavaScript to update the content of a React page

Recently, I came across an API using Swagger for documentation. Swagger generates a ReactJs webpage to document and test the API endpoints. However, my lack of knowledge in React architecture has led to a problem: Prior to accessing any endpoint, an authe ...

THREE.js displays incorrect animations when importing JSON models exported from Blender

My walk animation created in Blender looks great during playback: https://i.sstatic.net/giJZH.gif However, when I export it to THREE.js using the exporter tool, the animation appears distorted on the browser: https://i.sstatic.net/5XdJX.gif The geometr ...

Leveraging AngularJS and PhoneGap for seamless Facebook login/SDK integration even without the need for a server

In the process of developing a web application that will be deployed with Phonegap, I am utilizing NodeJS on the backend and AngularJS on the frontend, incorporating Facebook authentication. Currently, the Node server is running at localhost:3000 (will eve ...

Tips on adding iterated items to an array with comma separation following each item in JavaScript

My code below is attempting to iterate through domains and display them as comma-separated values. However, even when directly assigning arr = selectedOption.EmailDomainlist, the domains are not separated by commas in the output. <div> Allowed domain ...

Determine the dimensions of Expo's React Native Over the Air (OTA) update

Is there a method to determine the size of the required download for an OTA update before existing deployed apps (e.g. v1) retrieve it following the publication of a new managed Expo app (e.g. v2)? ...

I must align a bootstrap modal(dialog) based on the opener's position

Using a bootstrap modal for the settings dialog in an angularJS app, but facing an issue where it opens in the center of the page by default instead of relative to the opener. Looking for a solution to keep it positioned correctly when scrolling. Code: M ...

Error: The 'document' object is not recognized in this context (React)

I attempted to display my main page using routes but I am still encountering the same ReferenceError. Below is my code snippet: import React from "react"; import ReactDOM from "react-dom/client"; import App from "../components/app ...

Deactivating Bootstrap Modal in Angular

Looking for advice on managing a Bootstrap Modal in Angular 7 I have a Form inside a Bootstrap Modal that I need to reset when the modal is closed (by clicking outside of it). Despite searching on Google, I haven't been able to find a solution. Any ...

Deno -> What steps should I take to ensure my codes run smoothly without any errors?

Trying to import locally Experimenting with Deno v1.6.0 in a testing environment Attempting local imports using Deno language Local directory structure: . └── src └── sample ├── hello_world.ts ├── httpRequest. ...

SailsJS - handling blueprint routes prior to configuration of routes

I am trying to configure a route in my config/routes.js file as shown below '*' : { controller: 'CustomRoutes', action: 'any', skipAssets:true } The CustomRoutes controller is responsible for handling custom routes. Th ...