mob-sakai / SoftMaskForUGUI

Enhance Unity UI (uGUI) with advanced soft-masking features to create more visually appealing effects!
https://github.com/mob-sakai/SoftMaskForUGUI
MIT License
1.97k stars 261 forks source link

Unable to modify custom shader's properties in scripts' update? #101

Closed udvaritibor closed 3 years ago

udvaritibor commented 3 years ago

I've managed to set up my shader's SoftMaskable version. But now I can't change the shader's properties at runtime.

Shader "Hidden/Custom/UI/Grayscale (SoftMaskable)"
{
    Properties
    {
        [PerRendererData] _MainTex ("Sprite Texture", 2D) = "white" {}
        _Color ("Tint", Color) = (1,1,1,1)

        _StencilComp ("Stencil Comparison", Float) = 8
        _Stencil ("Stencil ID", Float) = 0
        _StencilOp ("Stencil Operation", Float) = 0
        _StencilWriteMask ("Stencil Write Mask", Float) = 255
        _StencilReadMask ("Stencil Read Mask", Float) = 255

        _EffectAmount ("Effect Amount", Range (0, 1)) = 1.0
        _BrightnessAmount ("Brightness Amount", Range(0.0, 3)) = 1.0

        _ColorMask ("Color Mask", Float) = 15
    }
        ...

And this is how I set properties in Update (for a short period of time):

LevelImage.material.SetFloat("_EffectAmount", amount);

If I click on the Material in the object's Renderer itself, it shows the correct effect amount, but under the object, it displays the Hidden Soft Maskable material, with unchangeable default properties.

How could I fix this?

Thanks in advance! :)

mob-sakai commented 3 years ago

@udvaritibor

Please try to use CanvasRender.GetMaterial() API to get the material for rendering.

LevelImage.canvasRenderer.GetMaterial().SetFloat("_EffectAmount", amount);

Edit: I will add an API to get get the material for rendering.

103

udvaritibor commented 3 years ago

@mob-sakai For some reason CanvasRenderer.GetMaterial() returns null :\

mob-sakai commented 3 years ago

@udvaritibor Hmm... In the first frame, it may return null.

udvaritibor commented 3 years ago

@mob-sakai Thanks, yes, it's only null at the first frame. :) (In the last (now deleted) comment I said that it's constantly null, but it was null only due to an issue on my side)

mob-sakai commented 3 years ago

Use v1.0.0-preview.11 and Graphic.materialForRendering property.