Utilizing Three.js Collada for loading and displaying several Collada objects within Three.js framework

I am struggling to load multiple objects with collada and the solutions provided on stack overflow are not working for me. While I was successful in loading with three.js export, collada is giving me trouble. I have shared my code below. Any help would be greatly appreciated. Thank you!

function o(){
    var loader = new  THREE.ColladaLoader();
    scene = new THREE.Scene();
    loader.options.convertUpAxis = true;
    loader.load('nn.dae', function (collada){
        dae = collada.scene;
        dae.scale.x = dae.scale.y = dae.scale.z = 3;
        //dae.updateMatrix();
        scene.add(dae);
        //console.log(scene);
    }); 
    loader.load('erer.dae', function (collada){
        dae1 = collada.scene;
        dae1.scale.x = dae1.scale.y = dae1.scale.z = 3;
        //dae1.updateMatrix();
        //scene.add(dae1);
        //console.log(scene);
    }); 

    init();
    animate();

}
o();
function init(){
    /*creates empty scene object and renderer*/
    camera =  new THREE.PerspectiveCamera(45, 600/400, .1, 500);
    renderer = new THREE.WebGLRenderer({antialias:true});

    renderer.setClearColor(0x000000);
    renderer.setSize(600, 400);
    renderer.shadowMapEnabled= true;
    renderer.shadowMapSoft = true;

    /*add controls*/

    camera.position.x = 5;
    camera.position.y = 9;
    camera.position.z = 42; 
    camera.lookAt(scene.position);

    /*datGUI controls object*/
    guiControls = new function(){
        this.rotationX  = 0.0;
        this.rotationY  = 0.0;
        this.rotationZ  = 0.0;

        this.lightX = 19;
        this.lightY = 47;
        this.lightZ = 19;
        this.intensity = 2.5;       
        this.distance = 373;
        this.angle = 1.6;
        this.exponent = 38;
        this.shadowCameraNear = 34;
        this.shadowCameraFar = 2635;
        this.shadowCameraFov = 68;
        this.shadowCameraVisible=false;
        this.shadowMapWidth=512;
        this.shadowMapHeight=512;
        this.shadowBias=0.00;
        this.shadowDarkness=0.11;       

    }
    /*adds spot light with starting parameters*/
    spotLight = new THREE.SpotLight(0xffffff);
    spotLight.castShadow = true;
    spotLight.position.set (20, 35, 40);
    spotLight.intensity = guiControls.intensity;        
    spotLight.distance = guiControls.distance;
    spotLight.angle = guiControls.angle;
    spotLight.exponent = guiControls.exponent;
    spotLight.shadowCameraNear = guiControls.shadowCameraNear;
    spotLight.shadowCameraFar = guiControls.shadowCameraFar;
    spotLight.shadowCameraFov = guiControls.shadowCameraFov;
    spotLight.shadowCameraVisible = guiControls.shadowCameraVisible;
    spotLight.shadowBias = guiControls.shadowBias;
    spotLight.shadowDarkness = guiControls.shadowDarkness;
    scene.add(spotLight);

    /*adds controls to scene*/

           $("#webGL-container").append(renderer.domElement);
    /*stats*/

}


function render() {    

    spotLight.position.x = guiControls.lightX;
    spotLight.position.y = guiControls.lightY;
    spotLight.position.z = guiControls.lightZ;

}

EDIT: I am trying to manipulate the scale of objects based on user input values from a form. I came across a technique where I load a collada file with two blender objects having different names. However, I am facing issues in changing the scale of both objects separately. The provided code only works for changing the scale of Cube, not Cube.001. Also, when I give a different name like 'dsds', the collada file fails to load. Here is the updated code snippet:

loader.options.convertUpAxis = true;
loader.load('vaddsi.dae', function (collada){
    dae = collada.scene;
    dae.scale.x = dae.scale.y = dae.scale.z = 3;
    dae.traverse(function (child){
        if (child.colladaId == "Cube.001"){
            child.traverse(function(e){
                e.castShadow = true;
                e.receiveShadow = true;
                e.scale.x=0.4;
                if (e.material instanceof THREE.Mesh){
                    //e.material.needsUpdate = true;
                }                  
            });
        }
        if (child.colladaId == "Cube"){
            child.traverse(function(e){
                e.scale.x=2;
                if (e.material instanceof THREE.Mesh){
                    e.material.needsUpdate = true;
                }  
            });
        }   
    });
    dae.updateMatrix();
    init();
    animate();
    console.log(scene);
}); 

Answer №1

function o(){
var loader = new  THREE.ColladaLoader(),
    loader2 = new THREE.ColladaLoader(),
    dae, dae1;
scene = new THREE.Scene();
loader.options.convertUpAxis = true;
loader.load('nn.dae', function (collada){
    dae = collada.scene;
    dae.scale.x = dae.scale.y = dae.scale.z = 3;
    //dae.updateMatrix();
    scene.add(dae);
    //console.log(scene);
}); 
loader2.load('erer.dae', function (collada){
    dae1 = collada.scene;
    dae1.scale.x = dae1.scale.y = dae1.scale.z = 3;
    //dae1.updateMatrix();
    scene.add(dae1);
    //console.log(scene);
}); 

init();
animate();

}

Don't forget to include the requestAnimationFrame() in the renderer to update the camera and scene.

If you are having trouble setting the scale, you can use this method:

    function o(){
var loader = new  THREE.ColladaLoader(),
    loader2 = new THREE.ColladaLoader(),
    dae, dae1;
scene = new THREE.Scene();
loader.options.convertUpAxis = true;
loader.load('nn.dae', function (collada){
    var StartTime = new Date();
    dae = collada.scene;
    scene.add(dae);
}, function(xhr){
   if(xhr.loaded >= xhr.total){
     var EndTime = new Date(), TotalTime;
     EndTime = StartTime - EndTime;
     TotalTime = Math.abs(EndTime);
     console.log("Object 1 loaded\nLoaded in: " + TotalTime + "ms.");
     setTimeout(function(){
       dae.scale.x = 3;
       dae.scale.y = 3;
       dae.scale.z = 3;
       console.log("Object 1 scale adjusted");
     }, TotalTime);
   }
}); 
loader2.load('erer.dae', function (collada){
    var StartTime = new Date();
    dae1 = collada.scene;
    scene.add(dae1);
}, function(xhr){
   if(xhr.loaded >= xhr.total){
     var EndTime = new Date(), TotalTime;
     EndTime = StartTime - EndTime;
     TotalTime = Math.abs(EndTime);

     console.log("Object 2 loaded\nLoaded in: " + TotalTime + "ms.");
     setTimeout(function(){
       dae1.scale.x = 3;
       dae1.scale.y = 3;
       dae1.scale.z = 3;
       console.log("Object 2 scale adjusted");
     }, TotalTime);
   }
}); 

init();
animate();

}

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

showing the angular response data in a HTML table layout within a specific div element

I'm currently using the angular-fullstack generator and I've received data in response to a GET request in the form of integer values (1, 2 5, 6 134, 245 236, 567 415, 234 and so on). I'd like to display these values in an HTML t ...

Implement a dynamic loading feature using AJAX, pushState, and Jekyll for a

In an effort to create a standard template for our studio's Jekyll sites development, I am aiming to implement page animation transitions. My plan involves loading different content from each page using AJAX and utilizing pushState to update the URL a ...

Is there a way to differentiate between a preflight request and the actual request?

Currently, I am utilizing CORS to transmit data to a server located on a different domain. Initially, the browser dispatches the preflight request to verify the server, followed by the main request. My goal is to identify when the request is in the "prefl ...

What could be causing the child of an ES6 imported object to return as undefined?

Here is the code snippet I am working with: import * as _routes from '../../routes' console.log('parent:', _routes) console.log('child:', _routes.breadcrumb) This code produces the following output: https://i.stack.imgur.co ...

Middleware GPS server - store location and transmit information to the server

After encountering a roadblock with my chosen GPS tracker service not supporting iframe plugins to share my car's position on my website, I've come up with the idea of creating a middleware server. The plan is to gather data from my GPS device, s ...

The sendKeys() method in Protractor is failing due to the element being hidden/not

Hi there! I am currently new to automated testing with protractorJS for an angularJS homepage. While the code I've written so far has been successful, I'm facing an issue where I'm unable to input keys into the search field. After running th ...

Obtain a collection of information from a web API by utilizing jQuery

Click on the following link to access live data from a web API: This is my first attempt at retrieving data from an API, so I may have missed some settings. To test the connection with the API, I included the following code in the HTML page: <div id= ...

Navigate to the correct page when the Button is clicked in a ReactJS/Django project

Embarking on my web development journey, I opted for django/Reactjs to build a social network platform. I created several API methods like account/view_user/ to retrieve a list of users and account/view_user/ for specific user attributes. In React, I cra ...

Move to the following array when clicking PHP (using Ajax, perhaps?)

How can I trigger php code when a button is clicked? (I understand the distinction between client-side and server-side. However, I believe I have encountered a similar scenario before.) Consider the following array : $test_id = [ 1 => '15 ...

Performing simultaneous updates to multiple records using CAML/jQuery in Sharepoint (Batch updates)

I am working on updating multiple records in Share Point using jQuery and CAML queries. While it's easy to update a single record with the current code, I need to handle 20 Products simultaneously for this particular project. Instead of looping throug ...

Glitch Uncovered in Excel Spreadsheet I Exported

I have a situation where I am working with an HTML table that includes both text and radio buttons. The issue arises when I attempt to export the table data along with the selected radio button values to Excel using the 'Export to Excel' button. ...

The object function router(req, res, next) is encountering an error as it does not contain the required method for handling the request

I am trying to add a new row to my MySQL database, but I encountered an error message. Here is the scenario: I have set up Passport and hjs in my project. I am passing form data from app.js to a JavaScript file where I aim to insert the data. Object funct ...

What is the best way to utilize the "useRouter" function to extract parameters from the URL within the getServerSideProps() method in Next.js?

How can I access the URL parameters within the getServerSideProps() method? Below is the snippet of my code. The objective of my code is to extract parameters from the URL, fetch data from the API, and render it on the front end. import { useRouter } from ...

Passing state from a parent component to a child component and then down to subsequent child components through props in React.js

Currently, all the data is static information. I am using a parent state which is an array containing 4 objects: function App(){ const [searchResults,setSearchResults] = useState([ { name: 'name1', artist: 'artist ...

Stop user from navigating to specific route using React-router

I am currently utilizing react-router with history useQueries(createHashHistory)(), and I have a requirement to restrict navigation to certain routes based on the route's configuration. The route configuration looks like this: <Route path="/" name ...

Is there a way to turn off the pinch-to-zoom trackpad gesture or disable the Ctrl+wheel zoom function on a webpage

Take a look at this example - While zooming in on the image by pressing ctrl + scroll, the image zooms but the page itself does not scale. Only the image is affected by the zoom. I am attempting to replicate this functionality on my Next.js page. I have ...

The challenge with our unique PHP/JS Analytics Solution

Here's an illustration of the code snippet for Google Analytics: <script type="text/javascript"> var _gaq = _gaq || []; _gaq.push(['_setAccount', 'userIDhere']); _gaq.push(['_trackPageview']); _gaq.push([&apos ...

Angular 2 with a jssor slider that adjusts seamlessly to different screen

After following the guidance provided in this answer on incorporating jssor into angular2, I have implemented the following JavaScript code snippet in a file and referenced it in angular-cli.json. jssor_1_slider_init = function() { var jssor_1_op ...

What is the best way to determine if a value stored in localStorage is present in an

After reading this particular response, I learned that I should utilize $.inArray. Following this advice, my code looks like this: var curPostId = $(".my_post_id").attr("data-id"); if($.inArray(curPostId, lines)) { $('#'+localStorage.getItem( ...

Tips for submitting a jQuery star rating:

I recently installed and added a plugin to one of my pages that transforms radio buttons into stars. While the form control works perfectly, I am facing an issue with submitting the form. The radio buttons are grouped together as stars but they do not subm ...