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

Question How can I keep the main texture of the character when adding a new shader?

Discussion in 'Shader Graph' started by Chocolade, Jun 13, 2020.

  1. Chocolade

    Chocolade

    Joined:
    Jun 19, 2013
    Posts:
    932
    I created a pbr shader graph. The problem is when I change the material to use the new shader the character texture is changing too. Is there a way to keep the original texture with the new shader ?

    This is a screenshot of the original material using with the character with the original shader :



    This is the original textures :



    This is the character mesh inspector settings with the script attached to it :



    This is the new shader settings in the inspector :



    This is the script :

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class NewBehaviourScript : MonoBehaviour
    {
    public Shader _shader;

    // Start is called before the first frame update
    void Start()
    {
    Store();
    }

    // Update is called once per frame
    void Update()
    {

    }

    private void Store()
    {
    Texture tex = gameObject.GetComponent<Renderer>().material.GetTexture("_MainTex");
    gameObject.GetComponent<Renderer>().material.shader = _shader;
    gameObject.GetComponent<Renderer>().material.SetTexture("_MainTex", tex);
    }
    }
    When running the game now it's changing to the new shader but it's not keeping the original textures :

    I tried to change the line in the script the last line in the Store function to this : but it didn't change much.

    gameObject.GetComponent<Renderer>().material.SetTexture("vanguard_diffuse", tex);
    This glowing lines is fine this is the effect the new shader should do but I want it to do it on the original textures like in the first screenshots.



    The new shader using pbr and hdr and other stuff to make the character slowly disappear and show back again with the glowing color on the edges. I can't figure out how to use it on the character with the original textures.

    I just did this tutorial and it's working fine but I want it to work now on the original textures of the character.

     
    marcospgp likes this.
  2. LandonTownsend

    LandonTownsend

    Unity Technologies

    Joined:
    Aug 21, 2019
    Posts:
    35
    If you want a value to carry over after switching to your shadergraph shader:
    1. Click the three dots in the upper corner and switch to debug
    upload_2020-6-16_9-52-26.png
    Figure out which of the "saved properties" corresponds to the texture or value you want to keep
    upload_2020-6-16_9-53-23.png
    ("_BaseMap" is the main diffuse texture for instance)

    Then in your shader graph, make sure the reference name of your property matches that name (the display name doesn't matter)
    upload_2020-6-16_9-55-18.png

    If you do this for all the texture, float and color properties you want to carry over, they will stay the same when you switch the material to your shadergraph. Unfortunately I don't think there is a way to do that for the tiling and offsets for your textures so you will have to do that manually.

    Remember to switch back from "debug" to "normal" when you're done (using the three dots in the upper right) so your material inspector goes back to normal.
     
  3. perholmes

    perholmes

    Joined:
    Dec 29, 2017
    Posts:
    296
    Dynamite answer!
     
  4. xx12animedude12xx

    xx12animedude12xx

    Joined:
    Mar 4, 2020
    Posts:
    2
    LEGENDARY! Thank you! Saved me hours of dragging and dropping