Toggle Material Parameters

I’m trying to access to float parameter of the material and use it for screen tap toggle. But, doesn’t work.

// -----JS CODE-----
//@input Asset.Material AlphaMat

var newAlpha = script.AlphaMat.mainPass.AlphaCut;
var toggle = false;
var min = -1; 
var max = 1;

// Enable full screen touches
global.touchSystem.touchBlocking = true;
script.api.togglenewAlpha = togglePar;

function togglePar() {
    toggle = !toggle;
    var end = toggle ? min : max;
    script.AlphaMat.mainPass.AlphaCut = newAlpha;
}

I think you would need to call this function in a behavior response for tap event.

1 Like

It says that api TogglePar is missing
and if I change it to my togglenewAlpha API then logger is clear but nothing happen

You would need to assign script.api.togglePar = togglePar to expose it.

1 Like

Thanks! Also, I found that I need t assign ‘script.AlphaMat.mainPass.AlphaCut’ to the ‘var end’
And now toggle is work, but, it just switching. How can I add a time to change between min and max value?

You would need to have the tap run a tween. The tween can set the AlphaCut material parameter

1 Like

The main thing why im trying to create this toggle script its cuz behavior and tween manager doesn’t have it ability


Ah, yeah it would be really nice to have a “reverse” or “toggle” command in that action list. I wonder if you could use the ping-pong loop type somehow to accomplish this. In any case, this does feel like a missing feature. You could submit a feature request to the support team. They have been super helpful to me.

This is something I ran into today. It’s a bit specific to my use case, but you could do something like this to set up a toggler.

//@input SceneObject[] tweenObjs
//@input string[] tweensState1
//@input string[] tweensState2

var bool = false
script.createEvent("TapEvent").bind(function(){
    bool = !bool
    if(bool){
        // run state 1 tweens
        global.tweenManager.startTween(script.getSceneObject(), script.tweensState1[0], function(){
            script.tweenObjs[0].enabled = false            
            script.tweenObjs[1].enabled = true            
            global.tweenManager.startTween(script.getSceneObject(), script.tweensState1[1])
        })
    }else{
        global.tweenManager.startTween(script.getSceneObject(), script.tweensState2[0], function(){
            script.tweenObjs[0].enabled = true
            script.tweenObjs[1].enabled = false
            global.tweenManager.startTween(script.getSceneObject(), script.tweensState2[1])
        })
    }
})
1 Like

Not sure I get how it works
I create main object with this script and create two tween that set material par. one from -1 to 1 and second 1 to -1. Then I add it in TweenObj. And set Tween State as the name of tween? Am I wrong?



In this script, I needed 4 individual tweens for two objects. Each object had two tweens each. One to transition in, and one to transition out. It is a sequential transition, so the first tween runs and then the second one runs afterward. If you just want to toggle the tween for one object, you can remove all of that array stuff and just use single parameters

1 Like

Ahhh, ok
So, I set up it:
First, I add two tweens that adjust material Par from min to max and from max to min values.


Then I Adjust the code:

//@input SceneObject tweenObjs
//@input string[] tweensState1
//@input string[] tweensState2

var bool = false
script.createEvent("TapEvent").bind(function(){
    bool = !bool
    if(bool){
        // run state 1 tweens
        global.tweenManager.startTween(script.getSceneObject(), script.tweensState1, function(){
            script.tweenObjs.enabled = false            
            global.tweenManager.startTween(script.getSceneObject(), script.tweensState1)
        })
    }else{
        global.tweenManager.startTween(script.getSceneObject(), script.tweensState2, function(){
            script.tweenObjs.enabled = true
            global.tweenManager.startTween(script.getSceneObject(), script.tweensState2)
        })
    }
})

And now its just a blinking on update, nothing happen on touch =(

Remove the array brackets from the @inputs

1 Like

Hmm. But how can I put the name of Tween?

Снимок экрана 2021-09-10 в 19.24.50

Did you define the type? It should be a string (but not a string array). You should be able to get single string input like this:

//@input string tweensState1

Seems like it was a bug. But, anyway when I screen tapping nothing happen =(

Try adding some print statements to see what is happening. Is the tap callback firing at all? Are the tween scripts on the same object as this script?

Do you need two tweens to play on every tap? You can probably remove the callback functions.

I’m not really sure what the problem is from this screenshot. Want to share the project?

Sure
I’m pretty sure it’s something minor =) as always happen to me

Also I notice that if I turn off min and max float par in material graph logger show me all taps
but if its turn on its show me just one tap