Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Thomas 5 · Feb 11, 2011 at 02:50 AM · runtimenormalmaps

Runtime Normal Map Import

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");
 }


Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
8

Answer by SophieH · Apr 16, 2011 at 08:11 PM

just spent a few hours trying to wrestle with this myself and finally got it to work (I think) so here y'are:

it seems when you import a normal map to unity, it intenally has the setup of RGB = y normal, A = x normal.

so when you load a texture at runtime, you should do something like (in C# sorry):

loadedTexture = www.texture;
normalTexture = new Texture2D(loadedTexture.width,loadedTexture.height,TextureFormat.ARGB32,false);
Color theColour = new Color();
for (int x=0; x<loadedTexture.width; x++){
for (int y=0; y<loadedTexture.height; y++){
   theColour.r = loadedTexture.GetPixel(x,y).g;
   theColour.g = theColour.r;
   theColour.b = theColour.r;
   theColour.a = loadedTexture.GetPixel(x,y).r;
   normalTexture.SetPixel(x,y, theColour);
}
}
normalTexture.Apply();

hope that helps :)

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image montblack · Jun 10, 2011 at 11:25 PM 0
Share

works great! TRAN$$anonymous$$S TOO $$anonymous$$UCH!

avatar image vagos21 · Feb 22, 2013 at 03:27 PM 0
Share

LOVE YOU!!!! i've been looking for the solution to this for months with no real solution! it even works on the shader side real-time :)

avatar image Enum · Jun 29, 2015 at 05:49 PM 0
Share

Thank you very much. Generating a normalmap-atlas for my tile-atlas would work much less convenient without your code.

avatar image Bunny83 · May 30, 2016 at 09:34 AM 0
Share

Since the question got already bumped i just want to say that this conversion is extremely inefficient. GetPixel has a bad performance. Also calling GetPixel two times per pixel doubles the overhead.

You should do something like this:

 loadedTexture = www.texture;
 normalTexture = new Texture2D(loadedTexture.width,loadedTexture.height,TextureFormat.ARGB32,false);
 Color32[] colours = loadedTexture.GetPixels32();
 for (int i=0; i < colours.Length; i++){
     Color32 c = colours[i];
     c.a = c.r;
     c.r = c.b = c.g;
     colours[i] = c;
 }
 normalTexture.SetPixels32(colours);
 normalTexture.Apply();

Get and SetPixels is way faster than reading one pixel at a time. Color32 is also a bit faster as it's a more compact format and the actual format used in memory.

avatar image laurachen Bunny83 · Aug 24, 2016 at 07:28 PM 0
Share

This works great on laptop/Editor, but does not work on iOS and Android. The result looks faded compared to the one with correct normal map. Below are the comparison between correct and non-correct one. normal works normal not work Is there chance that you might know why @Bunny83? I'm using Standard $$anonymous$$aterial. Any leads will be helpful. Thanks!

normal-work.png (514.4 kB)
normal-notwork.png (517.2 kB)
avatar image
0

Answer by tnageleweb · May 30, 2016 at 02:47 PM

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 [i].a = myColors [i].r;
         myColors [i].r =  myColors [i].g; 
         myColors [i].b =  myColors [i].g; 
         //myColors [i].g = myColors [i].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 [i].a = myColors [i].r;
         myColors [i].r = 0; 
         myColors [i].b = 0; 
         //myColors [i].g = myColors [i].g; 
   }
     normalMap.SetPixels (myColors);
     normalMap.Apply();


Hope that helps!

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by Michael_Swan · Jun 07 at 10:41 AM

Apparently you don't have to this anymore (2021+?) - Normal maps need to be linear rather than sRGB as described here: https://forum.unity.com/threads/runtime-normal-map-formats.1192042/

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

does Unity 3.5 support specularity and normal maps, when building an application to GL 1.1 android system? 0 Answers

Runtime normal maps by object distance 0 Answers

Runtime Loaded Bump/Normal Texture into "Bumped Diffuse" Shader 2 Answers

Normal map radiosity with Beast 1 Answer

How to get normal map of maya character to show up in Unity. 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges