Skip to content

Commit

Permalink
Fix terrain shader on windows (#542)
Browse files Browse the repository at this point in the history
* baseline

* fix terrain shader for windows

* fix terrain

* update

* restore

* support chinese path

* skip missing light true
  • Loading branch information
QuanyiLi committed Nov 3, 2023
1 parent 845af2d commit 81ce062
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 27 deletions.
3 changes: 2 additions & 1 deletion metadrive/engine/core/terrain.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,8 @@ def _generate_collision_mesh(self, heightfield_img, height_scale):
mesh = np.flipud(mesh)
mesh = cv2.resize(mesh, (mesh.shape[0] + 1, mesh.shape[1] + 1))
path_to_store = self.PATH.joinpath("run_time_map_mesh.png")
cv2.imwrite(str(path_to_store), mesh)
cv2.imencode('.png', mesh)[1].tofile(path_to_store)
# cv2.imwrite(str(path_to_store), mesh)
if sys.platform.startswith("win"):
path_to_store = AssetLoader.windows_style2unix_style(path_to_store)
p = PNMImage(Filename(str(path_to_store)))
Expand Down
2 changes: 1 addition & 1 deletion metadrive/envs/scenario_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
filter_overlapping_car=True, # If in one frame a traffic vehicle collides with ego car, it won't be created.
even_sample_vehicle_class=True, # to make the scene more diverse
default_vehicle_in_traffic=False,
skip_missing_light=False,
skip_missing_light=True,
static_traffic_object=True,

# ===== Agent config =====
Expand Down
50 changes: 25 additions & 25 deletions metadrive/shaders/terrain.frag.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ uniform struct {
vec4 ambient;
} p3d_LightModel;

uniform sampler2D p3d_Texture0;
uniform vec3 wspos_camera;

// asset
Expand Down Expand Up @@ -76,8 +75,8 @@ vec3 project(mat4 mvp, vec3 p) {
}


vec3 get_normal(sampler2D normal_tex, sampler2D rough_tex, float tex_ratio, mat3 tbn){
vec3 normal = texture(normal_tex, terrain_uv * tex_ratio).rgb*2.0-1.0;
vec3 get_normal(vec3 diffuse, sampler2D normal_tex, sampler2D rough_tex, float tex_ratio, mat3 tbn){
vec3 normal = normalize(texture(normal_tex, terrain_uv * tex_ratio).rgb*2.0-1.0);
normal = normalize(tbn * normal);
return normal;
}
Expand All @@ -98,8 +97,27 @@ void main() {
vec3 tangent = normalize(vec3(1, 0, h_u1 - h_u0));
vec3 binormal = normalize(vec3(0, 1, h_v1 - h_v0));
vec3 terrain_normal = normalize(cross(tangent, binormal));
// tbn is calulated in world space
vec3 normal = normalize(p3d_NormalMatrix * terrain_normal);
// normal.x *= -1;

mat3 tbn = mat3(tangent, binormal, terrain_normal);
vec3 shading = vec3(0.0);

// Calculate the shading of each light in the scene
for (int i = 0; i < p3d_LightSource.length(); ++i) {
vec3 diff = p3d_LightSource[i].position.xyz - vtx_pos * p3d_LightSource[i].position.w;
vec3 light_vector = normalize(diff);
vec3 light_shading = clamp(dot(normal, light_vector), 0.0, 1.0) * p3d_LightSource[i].color;
// If PSSM is not used, use the shadowmap from the light
// This is deeply ineficient, it's only to be able to compare the rendered shadows
if (!use_pssm) {
vec4 projected = projecteds[i];
// Apply a bias to remove some of the self-shadow acne
projected.z -= fixed_bias * 0.01 * projected.w;
light_shading *= textureProj(p3d_LightSource[i].shadowMap, projected);
}
shading += light_shading;
}

// get the color and terrain normal in world space
vec3 diffuse;
Expand All @@ -116,39 +134,21 @@ void main() {
// Semantics for value 4
diffuse = texture(white_tex, terrain_uv * road_tex_ratio).rgb;
}
tex_normal_world = get_normal(road_normal, road_rough, road_tex_ratio, tbn);
tex_normal_world = get_normal(diffuse, road_normal, road_rough, road_tex_ratio, tbn);
}
else{

// texture splatting, mixing ratio can be determined via rgba, no grass here
diffuse = texture(grass_tex, terrain_uv * grass_tex_ratio).rgb;
tex_normal_world = get_normal(grass_normal, grass_rough, grass_tex_ratio, tbn);
tex_normal_world = get_normal(diffuse, grass_normal, grass_rough, grass_tex_ratio, tbn);
}

// vec3 terrain_normal_view = normalize(tex_normal_world);

vec3 shading = vec3(0.0);

// Calculate the shading of each light in the scene
for (int i = 0; i < p3d_LightSource.length(); ++i) {
vec3 diff = p3d_LightSource[i].position.xyz - vtx_pos * p3d_LightSource[i].position.w;
vec3 light_vector = normalize(diff);
vec3 light_shading = clamp(dot(normalize(p3d_NormalMatrix * tex_normal_world), light_vector), 0.0, 1.0) * p3d_LightSource[i].color;
// If PSSM is not used, use the shadowmap from the light
// This is deeply ineficient, it's only to be able to compare the rendered shadows
if (!use_pssm) {
vec4 projected = projecteds[i];
// Apply a bias to remove some of the self-shadow acne
projected.z -= fixed_bias * 0.01 * projected.w;
light_shading *= textureProj(p3d_LightSource[i].shadowMap, projected);
}
shading += light_shading;
}

// static shadow
vec3 light_dir = normalize(light_direction);
shading *= max(0.0, dot(tex_normal_world, light_dir));
// shading += vec3(0.07, 0.07, 0.1);
shading += vec3(0.07, 0.07, 0.1);

// dynamic shadow
if (use_pssm) {
Expand Down

0 comments on commit 81ce062

Please sign in to comment.