Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Malvareth · Dec 01, 2018 at 09:05 PM · shadermaterialsshader programmingnormalsworldspace

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

Hey!

I am working on a Material Shader right now, which applys a texture onto a surface depending on world coordinates. A pretty good example for this approach is this video ([https://www.youtube.com/watch?v=CNWsN10KZCs][1])

I managed to get the mapping of the assigned texture right. However I'm struggling to add the normalmaps right. I tried different approaches, but nothing worked so far. Here is the code-snippet I'm working with:

 Shader "Custom/Test_WorldSpaceTexture" {
     Properties {
         _Color ("Color", Color) = (1,1,1,1)
         _MainTex ("Texture", 2D) = "white" {}
         _Glossiness ("Smoothness", Range(0,1)) = 0.5
         _Metallic ("Metallic", Range(0,1)) = 0.0
         _BumpMap ("Normal", 2D) = "bump" {}
     }
     SubShader {
         Tags { "RenderType"="Opaque" }
  
         CGPROGRAM
         #pragma surface surf Standard
         sampler2D _MainTex;
         sampler2D _BumpMap;
  
         struct Input {
             float2 uv_BumpMap;
             INTERNAL_DATA
             float3 worldNormal;
             float3 worldPos; };
 
         half _Glossiness;
         half _Metallic;
         fixed4 _Color;
  
         void surf (Input IN, inout SurfaceOutputStandard o) {
  
             if(abs(IN.worldNormal.y) > 0.5)
             {
                 o.Albedo = tex2D(_MainTex, IN.worldPos.xz) * _Color;
             }
             else if(abs(IN.worldNormal.x) > 0.5)
             {
                 o.Albedo = tex2D(_MainTex, IN.worldPos.yz) * _Color;
             }
             else
             {
                 o.Albedo = tex2D(_MainTex, IN.worldPos.yx) * _Color;
             }
  
             o.Emission = o.Albedo;
             o.Metallic = _Metallic;
             o.Smoothness = _Glossiness;
             //o.Normal = UnpackNormal (tex2D(_BumpMap, IN.uv_BumpMap)); 
         } 
         ENDCG
     }
     FallBack "Diffuse"
 }


Is there a simple way to extend the surf-method so that it will also map the normal map accordingly? If not, could you give me some suggestions that would point me in the right direction.

This problem seems trivial to me. But I wasn't able to figure it out so far. I'm pretty new and inexperienced with writing shaders. But I'm eager to learn new things. Help or Support would be highly appreciated!
[1]: https://www.youtube.com/watch?v=CNWsN10KZCs

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

Answer by Namey5 · Jan 09, 2019 at 12:11 PM

Seeing as you aren't blending the textures, this should be pretty simple to implement;

 struct Input 
 {
     float2 uv_BumpMap;
     float3 worldPos; 
     float3 worldNormal;
     INTERNAL_DATA
 };
  
 half _Glossiness;
 half _Metallic;
 fixed4 _Color;
   
 void surf (Input IN, inout SurfaceOutputStandard o) 
 {
     float3 worldNorm = WorldNormalVector (IN, float3 (0,0,1));    //You need to use this if you are writing to o.Normal
 
     if(abs(worldNorm.y) > 0.5)
     {
         o.Albedo = tex2D(_MainTex, IN.worldPos.xz) * _Color;
         o.Normal = UnpackNormal (tex2D (_BumpMap, IN.worldPos.xz));
     }
     else if(abs(worldNorm.x) > 0.5)
     {
         o.Albedo = tex2D(_MainTex, IN.worldPos.yz) * _Color;
         o.Normal = UnpackNormal (tex2D (_BumpMap, IN.worldPos.yz));
     }
     else
     {
         o.Albedo = tex2D(_MainTex, IN.worldPos.yx) * _Color;
         o.Normal = UnpackNormal (tex2D (_BumpMap, IN.worldPos.yx));
     }
   
     o.Emission = o.Albedo;
     o.Metallic = _Metallic;
     o.Smoothness = _Glossiness;
 } 
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

158 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 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 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 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 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 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 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 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 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 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

Shader to turn materials transparent based on y axis 0 Answers

World Relative UVs with normalmaps 1 Answer

See backside of a transparent emissive shader? 0 Answers

World Space material Stretching when object is rotated. 0 Answers

Displace and show normals in fragment shader 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