- Home /
Runtime Loaded Bump/Normal Texture into "Bumped Diffuse" Shader
I've been working on a RUNTIME .OBJ importer, all is going well with building mesh, normals and uv's, even turning quads to tri's etc.
I've become stumped on getting a RUNTIME imported BumpMap texture to work correctly with the "Bumped Diffuse" Standard shader.
I've first tried altering the Texture2d from color, greyscale and included a found function to convert it to a NVidea type Normal Map at runtime with no results
I tried changing texture spec from DXT1,DXT5,ARG32,RGB24 to no gain, even swapping different channels about for fun, and compressing the textures at runtime. I know the inbuilt editor importer converts normal maps to DXTnm, but this is not supported under .TextureFormat. syntax.
I have included a function solvetangents that generates tangents for the mesh and puts them to mesh.tangents which belongs to the mesh attached to the meshfilter of GO.
My only conclusion is that I have to use some other shader which would be a shame, or am not passing something correctly, does anyone have an idea why this will theoretically not work or how I could get this to work?
edit: Here's a simplied and much stripped version of my problem:-
using UnityEngine; using System.Collections;
public class tester : MonoBehaviour {
public Texture2D BumpTexture;
public Texture2D DiffTexture;
public string objPath;
public string PathDif;
public string PathBump;
private WWW loader;
void Start() {
StartCoroutine (Loader(objPath));
}
public IEnumerator Load(string objPath) {
//....CODE...
loader = new WWW(PathDif);
yield return loader;
loader.LoadImageIntoTexture(DiffTexture);
loader = new WWW(PathBump);
yield return loader;
loader.LoadImageIntoTexture(BumpTexture);
//....CODE...
Material m;
m=new Material(Shader.Find("Bumped Diffuse"));
m.SetColor("_Color", new Color(0.5,0.5,0.5,1.0)); //example only
if(DiffTexture!=null) m.SetTexture("_MainTex",DiffTexture);
if(BumpTexture!=null) m.SetTexture("_BumpMap",BumpTexture);
//....CODE...
gameObject.AddComponent(typeof(MeshFilter));
gameObject.AddComponent(typeof(MeshRenderer));
//meshes generated, added, uv, normals recalculated, solved tangents
gameObject.renderer.material = m;
//result bump map doesn't look good
}
}
See comment below for links to other parts of earlier code.
Many thanks in anticipation, Jon.
Can you post some code for us to review here? I'm curious how you are doing the actual assignment of the bumpmap texture to the shader.
Thanks for the interest Dave, I won't post the full code i've been modding bartok's obj code from the Everyday3d site (I reposted some code back on his site last friday) you can see some of my earlier modifications here: http://pastebin.com/yqQjyHUg and here: http://pastebin.com/YPSYPi73 And i've posted a stripped simplified bit of the code i'm working in the original post above.
By transferring the material created onto a generated primitive sphere, I could see that the normal map generator, dxt5 compression and the Unity Bumped Diffuse shader were absolutely fine, what isn't working fine is the tangent space calculation on the object, code found on the forum, so i'm going to have to figure out why my mesh is getting its tangents in a twist, possibly something to do with either having sub-meshes, having to invert faces or inverting z-axis on vertex import??
Answer by Jon-Martin · Jan 24, 2011 at 12:22 PM
The solvetangents c# script on this page had an a tiny error:-
Calculating Tangents: Unity Answers
I posted the correction on that page in the comments underneath, thankyou to Dave A for the kind responses.
Thanks a lot Jon! I could fix my problem in the loader implementation I'm working too thanks to your pointer/hints about the tangents, since at first I tough the problem was at the Normal texture loader method.
Answer by DaveA · Jan 18, 2011 at 08:42 PM
If time is money, someone is selling such a thing in the Asset store for $15. I don't know how good it is.
Thanks for the idea, but my importer works well bar this normal mapping, not sure if this importer will parse an obj material file as well as i'd like it too.
Almost forgot, I also have to release all the project source code for free under GPL as i am bound by an educational bid, so couldn't use a commercial script as we'd have to disclose the code. Thanks though all the same.
Your answer
Follow this Question
Related Questions
Is it possible to adjust normal map strength at runtime? 1 Answer
How to fix a object intersection problem? 0 Answers
How to update texture during runtime to display damage? 0 Answers
Moss/Snow texture on arbitrarily rotated doodads. 1 Answer
Applying decals / textures to uneven surfaces runtime 1 Answer