#Whats the best way to fade in a 3d object with multiple materials?

11 messages · Page 1 of 1 (latest)

sharp wind
#

I tried using TweenAlpha but it works only with simple object with 1 material, how would i do for multiple material within 1 object?

polar brook
#

Hi @sharp wind, depends on your scripting skills.
If you are able to script this I would just create a simple tween script and iterate over all the materials used on a mesh.
Get material count by using something like: script.yoursceneobject.getComponent("Component.MaterialMeshVisual").getMaterialsCount()

Else you could use TweenValue on the separate materials to "Set Material Parameter" On Update Callback.

Let me know if this helps!

sharp wind
polar brook
#

So here is an example I build:

Start a clean project, add the TweenManager (remove the example object) and add mesh Box.
Create 2 new pbr materials and set their blend mode to normal and add these to the box replaceing the one that is already there.
Create a new scene object, and add a TweenValue script with the name "materialvalue" and change the data type to Float and start at 1,00 and end at 0,00.
Disable the "Play Automatically" checkbox.
Add another Script component for the script below and add the box and main.

Let me know if this works for you!

//@input SceneObject main

//start the tween
global.tweenManager.startTween(script.main, "materialvalue", tweenComplete);

//sets alpha value on update event
function setAlpha(eventData)
{
    //get current tweenvalue
    var tweenValue = global.tweenManager.getGenericTweenValue( script.main, "materialvalue" );
    //checks amount of materials applied and changes all alpha values to current tween value
    for (let i = 0; i < script.box.getComponent("Component.MaterialMeshVisual").getMaterialsCount(); i++) {
        var currColor = script.box.getComponent("Component.MaterialMeshVisual").getMaterial(i).mainPass.baseColor;
        script.box.getComponent("Component.MaterialMeshVisual").getMaterial(i).mainPass.baseColor = new vec4(currColor.x, currColor.y, currColor.z, tweenValue)
          
    }
}

//stops updateevent
function tweenComplete(){
    event.enabled = false;
}

// Bind the function setAlpha to the event UpdateEvent
var event = script.createEvent("UpdateEvent");
event.bind( setAlpha );```
sharp wind
polar brook
#

Hi @sharp wind, yes this is possible by using getComponents("Component.MaterialMeshVisual") instead of getComponent("Component.MaterialMeshVisual")

script.box.getComponents("Component.MaterialMeshVisual").length will give you the amount of Render Mesh Visual components.
script.box.getComponents("Component.MaterialMeshVisual")[0] will give you the first one, etc.

If we utilise this we can just add a loop where it first checks all the components before diving into the component.

#

replace the setAlpha function with this one:

{
    //get current tweenvalue
    var tweenValue = global.tweenManager.getGenericTweenValue( script.main, "materialvalue" );
    //print( tweenValue.toString() );
    //checks amount of materials applied and changes all alpha values to current tween value
    print(script.box.getComponents("Component.MaterialMeshVisual").length);
    for (let j = 0; j < script.box.getComponents("Component.MaterialMeshVisual").length; j++) {
        for (let i = 0; i < script.box.getComponents("Component.MaterialMeshVisual")[j].getMaterialsCount(); i++) {
            var currColor = script.box.getComponents("Component.MaterialMeshVisual")[j].getMaterial(i).mainPass.baseColor;
            script.box.getComponents("Component.MaterialMeshVisual")[j].getMaterial(i).mainPass.baseColor = new vec4(currColor.x, currColor.y, currColor.z, tweenValue)
            //print(script.box.getComponent("Component.MaterialMeshVisual").getMaterial(i).mainPass.baseColor);    
        }
    }
}```
#

The "print" line is just to show the render mesh visual component amount as example and can be removed.

sharp wind
#

OMG i understand now it works!!!

#

thanks so mucch

#

hopefully it works if i make it into a global function so i can stop and start the tween from another script