Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable MetaDrive support on macOS #794

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion metadrive/component/block/base_block.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def __init__(
self.side_texture.setWrapU(Texture.WM_repeat)
self.side_texture.setWrapV(Texture.WM_repeat)
self.side_texture.setMinfilter(SamplerState.FT_linear_mipmap_linear)
self.side_texture.setAnisotropicDegree(8)
self.side_texture.setAnisotropicDegree(1)
self.side_normal = self.loader.loadTexture(AssetLoader.file_path("textures", "sidewalk", "normal.png"))
# self.side_normal.set_format(Texture.F_srgb)
self.side_normal.setWrapU(Texture.WM_repeat)
Expand Down
8 changes: 7 additions & 1 deletion metadrive/component/sensors/rgb_camera.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from metadrive.constants import CamMask
from metadrive.constants import Semantics, CameraTagStateKey
from metadrive.third_party.simplepbr import _load_shader_str
from metadrive.utils.utils import is_mac


class RGBCamera(BaseCamera):
Expand Down Expand Up @@ -41,13 +42,18 @@ def _setup_effect(self):
fbprops.float_color = True
fbprops.set_rgba_bits(16, 16, 16, 16)
fbprops.set_depth_bits(24)
fbprops.set_multisamples(16)
if is_mac():
fbprops.set_multisamples(4)
else:
fbprops.set_multisamples(16)
self.scene_tex = p3d.Texture()
self.scene_tex.set_format(p3d.Texture.F_rgba16)
self.scene_tex.set_component_type(p3d.Texture.T_float)
self.tonemap_quad = self.manager.render_scene_into(colortex=self.scene_tex, fbprops=fbprops)
#
defines = {}
if is_mac():
defines["USE_330"] = True
#
post_vert_str = _load_shader_str('post.vert', defines)
post_frag_str = _load_shader_str('tonemap.frag', defines)
Expand Down
15 changes: 11 additions & 4 deletions metadrive/component/sensors/semantic_camera.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from panda3d.core import RenderState, LightAttrib, ColorAttrib, ShaderAttrib, TextureAttrib, FrameBufferProperties

from panda3d.core import RenderState, LightAttrib, ColorAttrib, ShaderAttrib, TextureAttrib, FrameBufferProperties, LColor, MaterialAttrib, Material
from metadrive.utils.utils import is_mac
from metadrive.component.sensors.base_camera import BaseCamera
from metadrive.constants import CamMask
from metadrive.constants import Semantics, CameraTagStateKey
Expand All @@ -14,7 +14,7 @@ def __init__(self, width, height, engine, *, cuda=False):
buffer_props.set_rgba_bits(8, 8, 8, 8)
buffer_props.set_depth_bits(8)
buffer_props.set_force_hardware(True)
buffer_props.set_multisamples(16)
buffer_props.set_multisamples(4 if is_mac() else 16)
buffer_props.set_srgb_color(False)
buffer_props.set_stereo(False)
buffer_props.set_stencil_bits(0)
Expand All @@ -39,13 +39,20 @@ def _setup_effect(self):
else:

if label == Semantics.PEDESTRIAN.label and not self.engine.global_config.get("use_bounding_box", False):
# PZH: This is a workaround fix to make pedestrians animated.
# rendering pedestrian with glasses, shoes, etc. [Synbody]
base_color = LColor(c[0] / 255, c[1] / 255, c[2] / 255, 1)
material = Material()
material.setDiffuse((base_color[0], base_color[1], base_color[2], 1))
material.setSpecular((0, 0, 0, 1))
material.setShininess(0)

cam.setTagState(
label,
RenderState.make(
# ShaderAttrib.makeOff(),
LightAttrib.makeAllOff(),
TextureAttrib.makeOff(),
MaterialAttrib.make(material),
ColorAttrib.makeFlat((c[0] / 255, c[1] / 255, c[2] / 255, 1)),
1
)
Expand Down
33 changes: 22 additions & 11 deletions metadrive/engine/core/engine_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,6 @@ class EngineCore(ShowBase.ShowBase):
DEBUG = False
global_config = None # global config can exist before engine initialization
loadPrcFileData("", "window-title {}".format(EDITION))
loadPrcFileData("", "framebuffer-multisample 1")
loadPrcFileData("", "multisamples 8")
loadPrcFileData("", "bullet-filter-algorithm groups-mask")
loadPrcFileData("", "audio-library-name null")
loadPrcFileData("", "model-cache-compressed-textures 1")
Expand All @@ -95,6 +93,15 @@ class EngineCore(ShowBase.ShowBase):
loadPrcFileData("", "garbage-collect-states 0")
loadPrcFileData("", "print-pipe-types 0")

if is_mac():
# latest macOS supported openGL version
loadPrcFileData("", "gl-version 4 1")
loadPrcFileData("", "framebuffer-multisample 1")
loadPrcFileData("", "multisamples 4")
else:
loadPrcFileData("", "framebuffer-multisample 1")
loadPrcFileData("", "multisamples 8")

# loadPrcFileData("", "allow-incomplete-render #t")
# loadPrcFileData("", "# even-animation #t")

Expand Down Expand Up @@ -160,9 +167,6 @@ def __init__(self, global_config):
# Disable useless camera capturing in none mode
self.global_config["show_interface"] = False

if is_mac() and (self.mode == RENDER_MODE_OFFSCREEN): # Mac don't support offscreen rendering
self.mode = RENDER_MODE_ONSCREEN

loadPrcFileData("", "win-size {} {}".format(*self.global_config["window_size"]))

if self.use_render_pipeline:
Expand Down Expand Up @@ -296,12 +300,19 @@ def __init__(self, global_config):
if self.global_config["daytime"] is not None:
self.render_pipeline.daytime_mgr.time = self.global_config["daytime"]
else:
self.pbrpipe = init(
msaa_samples=16,
use_hardware_skinning=True,
# use_normal_maps=True,
use_330=False
)
if is_mac():
self.pbrpipe = init(
msaa_samples=4,
# use_hardware_skinning=True,
use_330=True
)
else:
self.pbrpipe = init(
msaa_samples=16,
use_hardware_skinning=True,
# use_normal_maps=True,
use_330=False
)

self.sky_box = SkyBox(not self.global_config["show_skybox"])
self.sky_box.attach_to_world(self.render, self.physics_world)
Expand Down
10 changes: 3 additions & 7 deletions metadrive/engine/core/sky_box.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def __init__(self, pure_background: bool = False):
skybox_texture.set_magfilter(SamplerState.FT_linear)
skybox_texture.set_wrap_u(SamplerState.WM_repeat)
skybox_texture.set_wrap_v(SamplerState.WM_mirror)
skybox_texture.set_anisotropic_degree(16)
skybox_texture.set_anisotropic_degree(1)
skybox.set_texture(skybox_texture)

gles = ConfigVariableString("load-display").getValue()
Expand All @@ -40,12 +40,8 @@ def __init__(self, pure_background: bool = False):
AssetLoader.file_path("../shaders", "skybox_gles.frag.glsl")
)
else:
if is_mac():
vert_file = "skybox_mac.vert.glsl"
frag_file = "skybox_mac.frag.glsl"
else:
vert_file = "skybox.vert.glsl"
frag_file = "skybox.frag.glsl"
vert_file = "skybox.vert.glsl"
frag_file = "skybox.frag.glsl"
skybox_shader = Shader.load(
Shader.SL_GLSL, AssetLoader.file_path("../shaders", vert_file),
AssetLoader.file_path("../shaders", frag_file)
Expand Down
10 changes: 5 additions & 5 deletions metadrive/engine/core/terrain.py
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,7 @@ def _load_mesh_terrain_textures(self, engine, anisotropic_degree=16, filter_type
tex.set_wrap_v(v_wrap)
tex.setMinfilter(filter_type)
tex.setMagfilter(filter_type)
tex.setAnisotropicDegree(anisotropic_degree)
tex.setAnisotropicDegree(1)

# rock
self.rock_tex = self.loader.loadTexture(
Expand All @@ -464,7 +464,7 @@ def _load_mesh_terrain_textures(self, engine, anisotropic_degree=16, filter_type
tex.set_wrap_v(v_wrap)
tex.setMinfilter(filter_type)
tex.setMagfilter(filter_type)
tex.setAnisotropicDegree(anisotropic_degree)
tex.setAnisotropicDegree(1)

# # sidewalk
# self.side_tex = self.loader.loadTexture(AssetLoader.file_path("textures", "sidewalk", "color.png"))
Expand All @@ -478,7 +478,7 @@ def _load_mesh_terrain_textures(self, engine, anisotropic_degree=16, filter_type
# tex.set_wrap_v(v_wrap)
# tex.setMinfilter(filter_type)
# tex.setMagfilter(filter_type)
# tex.setAnisotropicDegree(anisotropic_degree)
# tex.setAnisotropicDegree(1)

# Road surface
# self.road_texture = self.loader.loadTexture(AssetLoader.file_path("textures", "sci", "new_color.png"))
Expand All @@ -496,10 +496,10 @@ def _load_mesh_terrain_textures(self, engine, anisotropic_degree=16, filter_type
tex.set_wrap_v(v_wrap)
tex.setMinfilter(filter_type)
tex.setMagfilter(filter_type)
tex.setAnisotropicDegree(anisotropic_degree)
tex.setAnisotropicDegree(1)
# self.road_texture_displacement = self.loader.loadTexture(AssetLoader.file_path("textures", "sci", "normal.jpg"))
# self.road_texture.setMinfilter(minfilter)
# self.road_texture.setAnisotropicDegree(anisotropic_degree)
# self.road_texture.setAnisotropicDegree(1)

# lane line
white_lane_line = PNMImage(1024, 1024, 4)
Expand Down
2 changes: 1 addition & 1 deletion metadrive/shaders/terrain.frag.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ void main() {
float radius = 0.001; // Sample radius
for(int x = -1; x <= 1; x++) {
for(int y = -1; y <= 1; y++) {
float depth_sample = texture2D(PSSMShadowAtlas, projected_coord.xy + vec2(x, y) * radius).r;
float depth_sample = texture(PSSMShadowAtlas, projected_coord.xy + vec2(x, y) * radius).r;
shadow_factor += step(ref_depth, depth_sample);
}
}
Expand Down
2 changes: 1 addition & 1 deletion metadrive/tests/vis_functionality/vis_rgb_cam.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
},
"interface_panel": ["dashboard", "rgb_camera"],
"manual_control": True,
"use_render": True,
"use_render": False,
"image_observation": True, # it is a switch telling metadrive to use rgb as observation
"norm_pixel": True, # clip rgb to range(0,1) instead of (0, 255)
# "pstats": True,
Expand Down
2 changes: 1 addition & 1 deletion metadrive/third_party/simplepbr/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ def _setup_tonemapping(self):

fbprops = p3d.FrameBufferProperties()
fbprops.float_color = True
fbprops.set_rgba_bits(16, 16, 16, 16)
# fbprops.set_rgba_bits(16, 16, 16, 16)
fbprops.set_depth_bits(24)
fbprops.set_multisamples(self.msaa_samples)
scene_tex = p3d.Texture()
Expand Down
10 changes: 5 additions & 5 deletions metadrive/third_party/simplepbr/shaders/simplepbr.frag.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -125,15 +125,15 @@ float diffuse_function(FunctionParamters func_params) {
}

void main() {
vec4 metal_rough = texture2D(p3d_TextureMetalRoughness, v_texcoord);
vec4 metal_rough = texture(p3d_TextureMetalRoughness, v_texcoord);
float metallic = clamp(p3d_Material.metallic * metal_rough.b, 0.0, 1.0);
float perceptual_roughness = clamp(p3d_Material.roughness * metal_rough.g, 0.0, 1.0);
float alpha_roughness = perceptual_roughness * perceptual_roughness;
vec4 base_color = p3d_Material.baseColor * v_color * p3d_ColorScale * texture2D(p3d_TextureBaseColor, v_texcoord);
vec4 base_color = p3d_Material.baseColor * v_color * p3d_ColorScale * texture(p3d_TextureBaseColor, v_texcoord);
vec3 diffuse_color = (base_color.rgb * (vec3(1.0) - F0)) * (1.0 - metallic);
vec3 spec_color = mix(F0, base_color.rgb, metallic);
#ifdef USE_NORMAL_MAP
vec3 n = normalize(v_tbn * (2.0 * texture2D(p3d_TextureNormal, v_texcoord).rgb - 1.0));
vec3 n = normalize(v_tbn * (2.0 * texture(p3d_TextureNormal, v_texcoord).rgb - 1.0));
#else
vec3 n = normalize(v_tbn[2]);
#endif
Expand All @@ -146,7 +146,7 @@ void main() {
#endif

#ifdef USE_EMISSION_MAP
vec3 emission = p3d_Material.emission.rgb * texture2D(p3d_TextureEmission, v_texcoord).rgb;
vec3 emission = p3d_Material.emission.rgb * texture(p3d_TextureEmission, v_texcoord).rgb;
#else
vec3 emission = vec3(0.0);
#endif
Expand Down Expand Up @@ -207,7 +207,7 @@ void main() {
float radius = 0.001; // Sample radius
for(int x = -1; x <= 1; x++) {
for(int y = -1; y <= 1; y++) {
float depth_sample = texture2D(PSSMShadowAtlas, projected_coord.xy + vec2(x, y) * radius).r;
float depth_sample = texture(PSSMShadowAtlas, projected_coord.xy + vec2(x, y) * radius).r;
shadow_factor += step(ref_depth, depth_sample);
}
}
Expand Down
12 changes: 3 additions & 9 deletions metadrive/third_party/simplepbr/shaders/tonemap.frag.glsl
Original file line number Diff line number Diff line change
@@ -1,24 +1,18 @@
#version 120
#version 330

uniform sampler2D tex;
uniform float exposure;

varying vec2 v_texcoord;

#ifdef USE_330
out vec4 o_color;
#endif

void main() {
vec3 color = texture2D(tex, v_texcoord).rgb;
vec3 color = texture(tex, v_texcoord).rgb;

color *= exposure;
color = max(vec3(0.0), color - vec3(0.004));
color = (color * (vec3(6.2) * color + vec3(0.5))) / (color * (vec3(6.2) * color + vec3(1.7)) + vec3(0.06));

#ifdef USE_330
o_color = vec4(color, 1.0);
#else
gl_FragColor = vec4(color, 1.0);
#endif
}
}
Loading