#Whats the best way to fade in a 3d object with multiple materials?
11 messages · Page 1 of 1 (latest)
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!
Once i get the material count? Do i just use globaltween and start the tween? Do i still need to attach tween script on the sceneobject?
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 );```
hey man thanks for this! this works with model with. multiple material. but unfortunately the model im working with has multiple mesh render on 1 ibject, can i iterate through the mesh render within one object is that even psossible?
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.