Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Animated Decal Projector / Decal

Discussion in 'Shaders' started by elettrozero, Feb 1, 2019.

  1. elettrozero

    elettrozero

    Joined:
    Jun 19, 2016
    Posts:
    216
    Hi,
    waiting for the ability of creating a custom Decal shader via Shader Graph I'm trying to animate the texture of a Decal Projector via script.

    Code (CSharp):
    1. projector.m_Material.SetTexture("_MainText", [...]);
    No error but nothing moves...where am I wrong?
     
  2. ecreators

    ecreators

    Joined:
    Oct 23, 2016
    Posts:
    7
    Hi @elettrozero.
    The DecalProjector is not on the next level. You change the material as you like and then reassign that material to the projector.material property. But remember: any new instantiated Material will incease the material count and used RAM.
    I did this to solve this

    Code (CSharp):
    1.  
    2. // field
    3. private Material initialMaterial; // to avoid trying to delete asset material that will show up an error in console
    4.  
    5. // or Awake ...
    6. void Start() {
    7.    this.initialMaterial = projector.material;
    8.    this.projector.material = this.initialMaterial; // info: projector is a DecalProjector in this script ...
    9. }
    10.  
    11. void Update() {
    12.    // change material as you like: example code ...
    13.    this.initialMaterial.SetColor("_BaseColor", Color.red);
    14.  
    15.    var materialToDelete = this.initialMaterial;
    16.    this.initialMaterial = new Material(this.initialMaterial);
    17.    // this will show up you material change in the decal projector
    18.    this.projector.material = this.initialMaterial;
    19.    Object.Destroy(materialToDelete); // free memory and reduce material count
    20. }
    This should help.
    Regarding this known issue. Unity Team has it on the TODO List, but it is low prioritized to my last infos.
    I hope this problem will be solved in a future unity version as well.