Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 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 Setsuki · Oct 11, 2013 at 07:38 AM · shadernormalsworldspacechanging shader

World Relative UVs with normalmaps

Hello ! I am currently using a shader allowing me to place my texture relative to the world. I simply put the textures used for the floor (normals pointing up) and for the walls (normals pointing elsewhere).

 struct Input 
 {
 float3 worldNormal;
 float3 worldPos;
 };


 void surf (Input IN, inout SurfaceOutput o) 
 {
 float2 UV;
 fixed4 c;

     if(abs(IN.worldNormal.x)>0.5) 
     {
         UV = IN.worldPos.zy; // side
         c = tex2D(_MainTexWall, UV* _Scale); // use WALLSIDE texture
     } 
     else if(abs(IN.worldNormal.z)>0.5) 
     {
         UV = IN.worldPos.xy; // front
         c = tex2D(_MainTexWall, UV* _Scale); // use WALL texture
     }
     else 
     {
         UV = IN.worldPos.xz; // top
         c = tex2D(_MainTexFlr, UV* _FlrScale); // use FLR texture
     }

 o.Albedo = c.rgb;
 }

I want to add normalmapping to this shader. Some of you might already know what my question is going to be since the problem is written in the doc :


float3 worldNormal - will contain world normal vector if surface shader does not write to o.Normal.

float3 worldNormal; INTERNAL_DATA - will contain world normal vector if surface shader writes to o.Normal. To get the normal vector based on per-pixel normal map, use WorldNormalVector (IN, o.Normal).


I can't read worldNormal as soon as I add
 o.Normal = something 


and

 struct Input 
 {
 float3 worldNormal;
 float3 worldPos;
     INTERNAL_DATA
 };


But using WorldNormalVector(IN, o.Normal) doesn't seem to do much if i haven't changed the value of o.Normal before...

I feel like in order to read my worldNormal, I need to write it myself, which doesn't seem possible since I can't read it...

So, does anybody have any idea so I can add normalmapping to my textures ?

Thanks a lot for your help !

Similar thread

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Setsuki · Oct 17, 2013 at 03:00 PM

Here is the solution by Orihaus in the comments of http://www.blog.radiator.debacle.us/2012/01/joys-of-using-world-space-procedural.html

 Shader "WorldUV/Bumped Diffuse" {
     Properties
     {
             _MainTex ("Base", 2D) = "white" {}
             _BumpMap ("Normalmap", 2D) = "bump" {}
             _Scale ("Scale", float) = 1.0
     }
    
     SubShader
     {
             Tags { "RenderType"="Opaque" }
             LOD 400
    
             CGPROGRAM
             #pragma surface surf Lambert
            
            
             struct Input
             {
                 float4 color : COLOR;
                     float2 uv_MainTex;
                     float2 uv_BumpMap;
                     float3 worldPos;
                     float3 worldNormal; INTERNAL_DATA
             };
            
             sampler2D _MainTex;
             sampler2D _BumpMap;
             fixed4 _Color;
            
             half _Scale;
            
             void surf (Input IN, inout SurfaceOutput o)
             {
                     float3 correctWorldNormal = WorldNormalVector(IN, float3( 0, 0, 1 ) );
                     float2 uv = IN.worldPos.zx;
                    
                     if( abs( correctWorldNormal.x ) > 0.5 ) uv = IN.worldPos.zy;
                     if( abs( correctWorldNormal.z ) > 0.5 ) uv = IN.worldPos.xy;
 
                     uv.x *= _Scale;
                     uv.y *= _Scale;
                    
                     fixed4 tex = tex2D( _MainTex, uv );
                     o.Albedo = IN.color * tex.rgb ;
                     o.Normal = UnpackNormal( tex2D( _BumpMap, uv ) );
             }
            
             ENDCG
     }
 FallBack "Bumped Specular"
 }
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

16 People are following this question.

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

Related Questions

How to extend a Material Shader so that it also maps the Normal Maps in WorldSpace? 1 Answer

Normals for Displacement Shader 0 Answers

What are normals? 1 Answer

Add a slider to bumped specular shader for normal strength? 3 Answers

Flipping normals with shader - zeroing out 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