- Home /
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"
}
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.
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.
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 ;)
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.
Your answer
![](https://koobas.hobune.stream/wayback/20220613105430im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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
How to determine shader center? 0 Answers