Hi, I got the same problem as Jan 18 in his `Runtime Loaded Bump/Normal Texture into "Bumped Diffuse" Shader`-topic.
When loading in a NormalMap via the WWW loader Unity just doesn't convert the texture to the Normal Map DXnm format. This makes it not possible for the Shaders to read the Normal Map properly.
So my question to the Unity Developers? Why is a conversion to the DXnm not possible?!
Regards,
Thomas
var DiffuseMap:Texture2D;
var NormalMap:Texture2D;
var HeightMap:Texture2D;
private var loader:WWW;
var PathDiffuse:String;
var PathNormal:String;
var PathHeight:String;
var textureImporter:TextureImporter;
function Start()
{
Shader.globalMaximumLOD=1000000;
DiffuseMap=new Texture2D(10,10,TextureFormat.ARGB32, false);
NormalMap=new Texture2D(10,10,TextureFormat.ARGB32, false);
HeightMap= new Texture2D(10,10,TextureFormat.ARGB32, false);
StartCoroutine(LoadTextures());
SetShader();
}
function LoadTextures ()
{
loader= new WWW(PathDiffuse);
yield loader;
loader.LoadImageIntoTexture(DiffuseMap);
loader = new WWW(PathNormal);
yield loader;
loader.LoadImageIntoTexture(NormalMap);
loader = new WWW(PathHeight);
yield loader;
loader.LoadImageIntoTexture(HeightMap);
print("Loaded");
}
function SetShader()
{
var m:Material;
m=new Material(Shader.Find("Bumped Diffuse"));
if(DiffuseMap!=null) m.SetTexture("_MainTex",DiffuseMap);
if(NormalMap!=null) m.SetTexture("_BumpMap",NormalMap);
//if(HeightMap!=null) m.SetTexture("_ParallaxMap",HeightMap); // Not using for Bumped Diffuse
renderer.material=m;
print("Shader set");
}
Thanks for this post and solution, that fix my issue!
However, instead of using double loop operations you might use a faster method as following:
Color [] myColors; // or define it globally
normalMap= new Texture2D( wwwTex.texture.width, wwwTex.texture.height, TextureFormat.ARGB32, true);
myColors= wwwTex.texture.GetPixels ();
for (int i = 0; i < myColors.Length; i++) {
myColors _.a = myColors *.r;*_
myColors .r = myColors .g; myColors .b = myColors .g; //myColors .g = myColors .g; * }* normalMap.SetPixels (myColors); normalMap.Apply(); ---------- That to get same suggested results above. But, to get exact results as unity you may need to clear R and B component as follows: ----------
Color [] myColors; // or define it globally normalMap= new Texture2D( wwwTex.texture.width, wwwTex.texture.height, TextureFormat.ARGB32, true); myColors= wwwTex.texture.GetPixels (); for (int i = 0; i < myColors.Length; i++) { myColors .a = myColors .r; _ myColors .r = 0; myColors .b = 0; //myColors .g = myColors .g; } normalMap.SetPixels (myColors); normalMap.Apply(); ---------- Hope that helps!_