Hey, what about receiving shadows for terrains generated from heightmap? I’ve tested some forks, and it seems to work with terrains that are generated with materials from editor, but in my case shader is used, to create a material, shaders do not appear, how to solve that?
I have tried setting receiveShadows to render/meshInstance, nothing changed
Switch to StandartMaterial does render shadows but all terrain becames white, seems like I need to define diffuses as a textures?
Hi @Newbie_Coder,
Are you using a custom shader? If that’s the case then lighting/shadows won’t work unless you write your own custom solution.
Try sharing some details or if possible an example project on your solution.
1 Like
this.shader = new pc.Shader(gd, shaderDefinition);
var material = new pc.Material();
material.setShader(this.shader);
material.setParameter('uTime', 0);
material.setParameter('sunColor', [2.0, 1.75, 1.65]);
material.setParameter('sunDirection', [-1.0, 0.2, 0.0]);
material.setParameter('horizonColor', [0.6, 0.7, 1]);
material.setParameter('zenithColor', [0.025, 0.1, 0.5]);
material.setParameter('uNormalMap', this.normalMap.resource);
material.setParameter('sandTexture', this.sandTexture.resource);
material.setParameter('grassTexture', this.grassTexture.resource);
// console.log(this.normalMap.resource);
material.setParameter('bumpTexture', heightTexture);
material.setParameter('bumpScale', bump);
Vert
attribute vec3 aPosition;
attribute vec2 aUv0;
attribute vec3 aNormal;
uniform sampler2D bumpTexture;
uniform float bumpScale;
uniform mat4 matrix_model;
uniform mat4 matrix_viewProjection;
varying float vAmount;
varying vec2 vUv0;
varying vec3 vWorldPos;
varying vec4 vProjectedPos;
void main()
{
vUv0 = aUv0;
vec4 bumpData = texture2D(bumpTexture, vUv0);
vAmount = bumpData.r; // assuming map is grayscale it doesn't matter if you use r, g, or b.
// move the position along the normal
vec4 worldPos = matrix_model * vec4(aPosition, 1.0);
vWorldPos = worldPos.xyz;
vProjectedPos = matrix_viewProjection * worldPos;
gl_Position = vProjectedPos;
}
Frag (Not full)
void main(void)
{
vec3 diffuse = vec3(0.008,0.294,0.525);
vec3 specular = vec3(0.0, 0.0, 0.0);
vec3 worldToEye = view_position - vWorldPos;
vec3 eyeDirection = normalize(worldToEye);
vec2 uv = vWorldPos.xz * 10.0; //Water normals size
vec2 uv2 = vWorldPos.xz * 1.0;
vec4 noise = getNoise(uv);
float dist = length(worldToEye);
float distortionFactor = max(dist / 100.0, 10.0);
vec3 surfaceNormal = normalize(noise.xzy * vec3(2.0, clamp(dist * 0.001, 1.0, 100.0), 2.0));
sunLight(surfaceNormal, eyeDirection, 100.0, 2.0, 0.5, diffuse, specular);
vec3 albedo = diffuse + specular;
albedo = aerialPerspective(albedo, dist, view_position, -eyeDirection);
vec4 water = (step(-0.2, vAmount) - step(0.01, vAmount)) * vec4(albedo, 1.0);
//z vec4 wetsand = (step(0.005, vAmount) - step(0.007, vAmount)) * vec4 (0.682, 0.561, 0.376, 1.0);
vec4 sand = (step(0.01, vAmount) - step(0.07, vAmount)) * texture2D(sandTexture, uv2 * 0.1 );
vec4 grass = (step(0.07, vAmount) - step(1.2, vAmount)) * texture2D(grassTexture, uv2 * 0.1 );
//gl_FragColor = vec4(water + wetsand + sand + grass, 1.0);
gl_FragColor = vec4(0.0, 0.0, 0.0, 1.0) + water + sand + grass; //, 1.0);
}
I should have thought about shaders before…