0

I am attempting to change the opacity of an object in the update function but I am not able to call on the object by name. It always returns undefined.

This code is a fragment. There are many cars using the same function. I commented, "IS THIS CORRECT?", for the questionable lines. Please comment if clarification is needed.

// ***Program starts at the bottom

// initialize the scene
function init() {
    var scene = new THREE.Scene();
    var camera = new THREE.PerspectiveCamera(45, window.innerWidth/window.innerHeight, 1, 1000);
    camera.position.set(-71, 29, 103);

    getCarAnim('Car1');
    scene.add(parentCar1);
    update(renderer, scene, camera);
    return scene;
}

// importing the car model
function getCarAnim(path) {
    var objLoader = new THREE.OBJLoader();
    objLoader.load('/resource/3D Models/' + path + '.obj', function (obj) {
        obj.traverse(function (child) {
        if (child instanceof THREE.Mesh) {
            child.material = carMaterial;
            child.material.transparent = true;
            if (path == 'Car1') { // IS THIS CORRECT?
                child.name = "Car1";
            }
        }
    });

    if (path == 'Car1') {
        parentCar1.add(obj);
        parentCar1.position.x = 15;
        parentCar1.position.y = 0.9;
        parentCar1.position.z = 3;
    }
}

// rendering/updating the frame to screen
function update (renderer, scene, camera) {
    renderer.render(scene, camera);
    if (parentCar1.position.x <= 15) {
        parentCar1.getObjectByName("Car1").material.opacity = .2; // IS THIS CORRECT?
    }
    requestAnimationFrame(function() {
        update(renderer, scene, camera);
    })
}

//**** program starts here

// This variable is the parent of the imported car model.
// It allows an imported object to be called upon
// so it can be animated in the update function.
var parentCar1 = new THREE.Object3D;

var carMaterial = new THREE.MeshLambertMaterial({color: 'rgb(152,152,152)'});
var scene = init();
Puka
  • 1,485
  • 1
  • 14
  • 33
Alex Brandt
  • 73
  • 1
  • 1
  • 6
  • @AlexBrandit what is `parentCar1`? I don't see you've assign value to it. In `getCarAnim` you just added another object to it (if condition `path == 'Car1'` is `true`). – Vadi Feb 22 '19 at 14:12
  • parentCar1 is assigned a THREE.Object3D at the bottom of the code. I do this to be able to call upon the imported car model outside of the objLoader – Alex Brandt Feb 22 '19 at 14:27
  • `objLoader.load` returns results of loading inside a callback function, but `update` function depends on the results of `objLoader.load` and runs outside of callback `objLoader.load`. Try to move it inside callback – Vadi Feb 22 '19 at 15:17
  • Are you saying move the line parentCar1.getObjectByName("Car1").material.opacity = .2; inside the callback of objLoader.load? This does work and makes the opacity to .2. However, my plan is to animate the opacity and I thought the point of getObjectByName() method was to be able to access the car1 object anywhere in the code. – Alex Brandt Feb 22 '19 at 15:48
  • -Are you saying move the line- I meant `update` method, because as it's in the code, in update method you're trying to access object 'Car1' that will be available in the `scene` only after adding it to parentCar1, but it will be added after some time. But `update` in code is called immediately. I'm not saying this is the only reason it doesn't work. – Vadi Feb 22 '19 at 17:21

0 Answers0