Hi there,

I made a detail shader, it's working but lighting seems to change according to camera rotation. What could be wrong ?

`shader_type spatial;

uniform float texture1Res = 10; uniform float detailRes = 10;

uniform sampler2D texture1; uniform sampler2D texture1N; uniform sampler2D detail; uniform sampler2D detailN;

void fragment(){ //float grassval = texture(splatmap, UV).g; //float rockval = texture(splatmap, UV).b; //float dirtval = texture(splatmap, UV).r; //float grassHeight = max (grassval,rockval); //vec3 grasstex = texture(grass, UVgrassres).rgb grassHeight;

vec3 texture1Albedo = texture(texture1, UVtexture1Res).rgb 0.5; vec3 texture1Normal = texture(texture1N, UVtexture1Res).rgb 0.5;

vec3 detailAlbedo = texture(detail, UVdetailRes).rgb 0.5; vec3 detailNormal = texture(detailN, UVdetailRes).rgb 0.5;

//vec3 resultAlbedo = vertexColor.r > minvertexColor ? grasstex: rocktex;


vec3 resultAlbedo = texture1Albedo + detailAlbedo; ALBEDO = resultAlbedo;

vec3 resultNormal = texture1Normal + detailNormal; NORMAL = resultNormal;

ROUGHNESS =0.5;

}`

@Megalomaniak said: At a glance I'd say its because you are adding the normal map values together but you don't normalize the result.

http://blog.selfshadow.com/publications/blending-in-detail/

I tried the first example, but the lighting is not working like before.

`shader_type spatial;

uniform float grassres = 10; uniform float rockres = 10;

uniform sampler2D grass; uniform sampler2D rock; uniform sampler2D grassN; uniform sampler2D rockN;

varying vec4 vertexColor; varying vec3 vertexNormal;

void vertex() { vertexColor = COLOR; // make the normal the color vertexNormal = NORMAL; }

void fragment(){

vec3 grasstex = texture(grass, UVgrassres).rgb ; vec3 graaNtext = texture(grassN, UVgrassres).rgb *2.0 - 1.0;

vec3 rocktex = texture(rock, UVrockres).rgb ; vec3 rockNtex = texture(rockN, UVrockres).rgb *2.0 - 1.0;

//vec3 resultAlbedo = vertexColor.r > minvertexColor ? grasstex: rocktex;

float inverseMultiplier = 1.0 - vertexColor.r; inverseMultiplier = inverseMultiplier >0.01 ? inverseMultiplier : 0.01;

vec3 resultAlbedo = (grasstex inverseMultiplier) + (rocktexvertexColor.r);
ALBEDO = resultAlbedo;

vec3 nResult = normalize(graaNtext + rockNtex);

vec3 resultNormal = nResult *0.5 + 0.5 ;

NORMAL = resultNormal;

ROUGHNESS =0.5;

}`

Your normalmaps might themselves be off, try inverting channels.

@Megalomaniak said: Your normalmaps might themselves be off, try inverting channels.

A palm tree object has correct lighting on trunk normal map.

I assigned plam tree normal map for the custom shader normals, but the lighting is not working.

There must be a bug, the object whole lighting changes with camera rotation.

I'm not sure I even see it on the palm tree, you sure it isn't just reflections or HDR messing with you? Also make sure that textures that need to are tagged as sRGB and those that need to are linear.

@Megalomaniak said: I'm not sure I even see it on the palm tree, you sure it isn't just reflections or HDR messing with you? Also make sure that textures that need to are tagged as sRGB and those that need to are linear.

Something is wrong with ShaderMaterial.

One the image the palmtree uses SpatialMaterial , lighting is ok. Rock uses ShaderMaterial , the lighting is not working and changes as the editor viewport camera rotates.

You can download the project and test it

www85.zippyshare.com/v/3OdKHWff/file.html

Everytime you link one of these iffy upload sites I flinch, tbh. :# You sure you don't want to register a github or dropbox account or something? Anyways guess I'll just temporarily give the site permission to run js and then scan the archive before opening it. :/

Edit: Yep, as I originally thought you aren't calculating your normals correct. edit 2 coming up as soon as i have rewritten the whole shader for you.

edit2: simplest example, cutting out dealing with normals by simply using NORMALMAP:


shader_type spatial;

	

uniform float tex1res = 10;
uniform float tex2res = 10;

	

uniform sampler2D tex1Albedo;
uniform sampler2D tex1Normal;
uniform sampler2D tex2Albedo;
uniform sampler2D tex2Normal;

	

varying vec3 vN;

	

void vertex() {

	

	vN = NORMAL;

	

}

	

void fragment() {

	vec3 a1 = texture(tex1Albedo, UV * tex1res).rgb;
	vec3 n1 = texture(tex1Normal, UV * tex1res).rgb;

	

	vec3 a2 = texture(tex2Albedo, UV * tex2res).rgb;
	vec3 n2 = texture(tex2Normal, UV * tex2res).rgb;

	

	vec3 resultAlbedo = mix(a1, a2, 0.5);
	ALBEDO =  resultAlbedo;

	

	vec3 addNormals = vec3(n1 + n2);
	vec3 nResult = normalize(addNormals);

	

	NORMALMAP = nResult;
	ROUGHNESS = 0.5;
}

You are right about NORMALMAP , this is what i should have used.

5 years later