Loading external texture into secondary shader slot

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.

var url = "file://load_texture_diff.png";

function Update () {
	
	if (Input.GetButton ("Fire2")){
	
	
	renderer.material.mainTexture = new Texture2D(1024, 1024);


	new WWW(url).LoadImageIntoTexture(renderer.material.mainTexture);

	}


}

Thanks.

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

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.

Ok, I just tried this

var url = "file://load_texture_norm.png";


function Update () {
	
	var bumpMap : Texture;
	
	if (Input.GetButton ("Fire2")){
	
	renderer.material.SetTexture("_BumpMap", bumpMap) = new Texture2D(1024, 1024);


	new WWW(url).LoadImageIntoTexture(renderer.material.SetTexture("_BumpMap", bumpMap));

	}


}

and I am getting errors: "Assets/LoadBump.js(13) error BCE0049: Expression cannot be assigned to." and "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?

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. :slight_smile:

var url = "file://load_texture_norm.png"; 

function Update () { 
	if (Input.GetButton ("Fire2")) { 
		var bumpMap = new Texture2D(1024, 1024);
		new WWW(url).LoadImageIntoTexture(bumpMap);
		renderer.material.SetTexture("_BumpMap", bumpMap);
	}
}

Also you probably want GetButtonDown rather than GetButton.

–Eric

Ah, many thanks Eric. It’s starting to come together for me.