Search Unity

Loading external texture into secondary shader slot

Discussion in 'Scripting' started by Obscurity, Apr 25, 2008.

  1. Obscurity

    Obscurity

    Joined:
    Nov 13, 2007
    Posts:
    203
    I may have just missed this in my search so forgive me if it's elsewhere.

    Does anyone know how to load an image into, for example, the bumpmap slot in a material using the Bumped Specular shader? In the docs all I could find was how to apply one to the main texture. Any clue? Or is this an impossibility with basic scripting? Below is my simple test script for applying a local external texture, as an example. It works fine but I'd also like to swap out the normal map too somehow.

    Code (csharp):
    1.  
    2. var url = "file://load_texture_diff.png";
    3.  
    4. function Update () {
    5.    
    6.     if (Input.GetButton ("Fire2")){
    7.    
    8.    
    9.     renderer.material.mainTexture = new Texture2D(1024, 1024);
    10.  
    11.  
    12.     new WWW(url).LoadImageIntoTexture(renderer.material.mainTexture);
    13.  
    14.     }
    15.  
    16.  
    17. }
    18.  
    19.  
    Thanks.
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    mainTexture is a shortcut for SetTexture("_MainTex", someTexture); So if you want to replace something other than _MainTex, you'd look in the shader to see what it's called. Bumpmaps are usually, as you might expect, called "_BumpMap".

    --Eric
     
  3. Obscurity

    Obscurity

    Joined:
    Nov 13, 2007
    Posts:
    203
    Thanks, Eric. Yeah I saw that in the docs right after posting, of course. I'll update with my results in case anyone else does a forum search and runs across this thread.
     
  4. Obscurity

    Obscurity

    Joined:
    Nov 13, 2007
    Posts:
    203
    Ok, I just tried this

    Code (csharp):
    1.  
    2.  
    3. var url = "file://load_texture_norm.png";
    4.  
    5.  
    6. function Update () {
    7.    
    8.     var bumpMap : Texture;
    9.    
    10.     if (Input.GetButton ("Fire2")){
    11.    
    12.     renderer.material.SetTexture("_BumpMap", bumpMap) = new Texture2D(1024, 1024);
    13.  
    14.  
    15.     new WWW(url).LoadImageIntoTexture(renderer.material.SetTexture("_BumpMap", bumpMap));
    16.  
    17.     }
    18.  
    19.  
    20. }
    21.  
    and I am getting errors:
    Code (csharp):
    1. "Assets/LoadBump.js(13) error BCE0049: Expression cannot be assigned to."
    and
    Code (csharp):
    1. "Assets/LoadBump.js(15) error BCE0017: The best overload for the method 'UnityEngine.WWW.LoadImageIntoTexture(UnityEngine.Texture2D)' is not compatible with the argument list '(void)'."
    What am I doing wrong here?
     
  5. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    You have the right idea, but mostly you're just getting the order a bit wrong. Also, I know the docs say that using .mainTexture is "the same" as using Get/SetTexture with a _MainTex name, but it's actually a reference to that texture. Whereas SetTexture is a function that performs an action and doesn't return anything ("void"), not a refererence to the texture. Hence the error message...understandable confusion but it will all make sense eventually. :)

    Code (csharp):
    1. var url = "file://load_texture_norm.png";
    2.  
    3. function Update () {
    4.     if (Input.GetButton ("Fire2")) {
    5.         var bumpMap = new Texture2D(1024, 1024);
    6.         new WWW(url).LoadImageIntoTexture(bumpMap);
    7.         renderer.material.SetTexture("_BumpMap", bumpMap);
    8.     }
    9. }
    Also you probably want GetButtonDown rather than GetButton.

    --Eric
     
  6. Obscurity

    Obscurity

    Joined:
    Nov 13, 2007
    Posts:
    203
    Ah, many thanks Eric. It's starting to come together for me.