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 Em3rgency · Jul 03, 2013 at 08:21 PM · shadercgcustom-shader

Modify custom shader to add bump and normal maps

I loathe to ask a "please do it for me" but I'm really stumped here. Shaders are a complete mystery and CG just baffles me.

I found this shader elsewhere on the internet. It makes sure an object's (like a wall) textures are set to world coordinates rather than object coordinates, so that I can place proceduraly generated objects and not worry about their textures matching. And it works like a charm. Now I would like to add normal and bump maps to it. I tried following unity's documentation on the subject, but I just get a ton of errors I can't even begin to comprehend. Here is the current shader text, the comments are me trying to add in a bump map. Help?

 Shader "Custom/World UV" 
 {
     Properties 
     {
         _Color ("Main Color", Color) = (1,1,1,1)
         _MainTex ("Texture (RGB)", 2D) = "surface" {} 
         //_BumpMap ("Bumpmap", 2D) = "bump" {}
         _Scale ("Texture Scale", Float) = 0.1
 
     }
 
     SubShader 
     {
         Tags { "RenderType"="Opaque" }
         
         CGPROGRAM
         #pragma surface surf Lambert
         
         struct Input 
         {
             float3 worldNormal;
             float3 worldPos;
         };
         
         sampler2D _MainTex;
         //sampler2D _BumpMap;
         float4 _Color;
         float _Scale;
 
         
         void surf (Input IN, inout SurfaceOutput o) 
         {
             float2 UV;
             fixed4 c;
             //fixed4 d;
             
             if(abs(IN.worldNormal.x)>0.5) 
             {
                 UV = IN.worldPos.yz; // side
                 c = tex2D(_MainTex, UV* _Scale);
                 //d = tex2D(_BumpMap, UV* _Scale);
             } 
             else if(abs(IN.worldNormal.z)>0.5) 
             {
                 UV = IN.worldPos.xy; // front
                 c = tex2D(_MainTex, UV* _Scale);
                 //d = tex2D(_BumpMap, UV* _Scale);
             } 
             else 
             {
                 UV = IN.worldPos.xz; // top
                 c = tex2D(_MainTex, UV* _Scale);
                 //d = tex2D(_BumpMap, UV* _Scale);
             }
             
             o.Albedo = c.rgb * _Color;
             //o.Normal = UnpackNormal (d);
         }
         ENDCG
     } 
     Fallback "VertexLit"
 }


Comment
Add comment · Show 3
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 Em3rgency · Jul 04, 2013 at 07:12 PM 0
Share

Cmon guys, noone? If nobody knows how to modify this shader to add a bump map, maybe someone could at least make heads or tails of these errors? This is what i get when I uncomment my lines.

alt text

avatar image Benproductions1 · Jul 05, 2013 at 04:08 AM 0
Share

Very few people know how to make shaders. It seems you have not posted all the code. The errors are on lines 124-126.... I only see up till line 62.

avatar image Em3rgency · Jul 05, 2013 at 05:10 AM 0
Share

No this is all $$anonymous$$Y code. The rest is the automatically added code (a few thousands lines, I might add) for the compiler. Its throwing errors because of something in my code. Clearly, you don't know anything about shaders in unity ;)

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by dsnettleton · Jul 08, 2013 at 11:47 PM

First of all, I would move

c = tex2D(_MainTex, UV* _Scale); d = tex2D(_BumpMap, UV* _Scale);

outside of the branching if statement, since it does not change. Second of all, you've got what looks like an NVidia Cg shader in the middle of your unity shader, so you really can't fault the unity crew for not debugging for you. Best to stick to Unity's ShaderLab language. Fewer moving parts. Especially if you're not using an NVidia card. I'm not sure how Unity handles the conversion, but I'm positive that their language will be best for cross-platform consistency.

As for your actual code, normal maps are usually done using tangent space, which you're not computing here. But that won't keep your results from compiling; just from looking good. I haven't used Cg myself, but it seems pretty similar to glsl. I would get rid of those warnings first. On line 56, try changing c.rgb to c.rgba. Also, make sure you've got a properly encoded image file for your bumpmap loaded into the editor. Finally, notice that cg shaders in unity utilize half3 and half4 data types. I haven't done the research, so I'm not sure where to use these, but maybe you should.

Sorry I can't be more helpful, but I don't see anything else wrong with your code (except that I don't know where UnpackNormal came from), so perhaps you could try an NVidia forum for more help, or try rewriting in ShaderLab.

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 mavv · Aug 06, 2019 at 10:22 AM

Anyone figured it out? :)

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

18 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

Related Questions

How to force the compilation of a shader in Unity? 5 Answers

ShaderLab on Unity3d Indie 2 Answers

Which way to write a shader in Unity is the best? 1 Answer

Get access to previous pass from CG 2 Answers

Pass ShaderLab properties to Standard cginc 2 Answers


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