Simplify materials.
Merge shading model, material params, and material textures into a single "mjrMaterial" type. Remove redundant decor_line material. PiperOrigin-RevId: 911960222 Change-Id: Icce3772a8ea48467dfd610799c0c2f2ba752323e
This commit is contained in:
committed by
Copybara-Service
parent
d5c5a989f2
commit
abea65b0be
@@ -183,20 +183,25 @@ static void UpdateGeomMaterial(mjrRenderable* renderable, const mjvGeom& geom,
|
||||
const mjModel* model = model_objs->GetModel();
|
||||
|
||||
const bool use_segid_color = scene->flags[mjRND_IDCOLOR];
|
||||
mjrMaterialParams params;
|
||||
mjr_defaultMaterialParams(¶ms);
|
||||
params.color[0] = geom.rgba[0];
|
||||
params.color[1] = geom.rgba[1];
|
||||
params.color[2] = geom.rgba[2];
|
||||
params.color[3] = geom.rgba[3];
|
||||
mjrMaterial material;
|
||||
mjr_defaultMaterial(&material);
|
||||
|
||||
if (geom.category == mjCAT_DECOR) {
|
||||
material.decor_ux = true;
|
||||
}
|
||||
|
||||
material.color[0] = geom.rgba[0];
|
||||
material.color[1] = geom.rgba[1];
|
||||
material.color[2] = geom.rgba[2];
|
||||
material.color[3] = geom.rgba[3];
|
||||
if (geom.type == mjGEOM_PLANE) {
|
||||
if (IsBehind(headpos, geom.pos, geom.mat)) {
|
||||
params.color[3] *= 0.3;
|
||||
material.color[3] *= 0.3;
|
||||
mjrf_setRenderableReceiveShadows(renderable, false);
|
||||
params.reflective = false;
|
||||
material.reflective = false;
|
||||
} else {
|
||||
mjrf_setRenderableReceiveShadows(renderable, true);
|
||||
params.reflective = geom.reflectance > 0 && params.color[3] == 1.0f;
|
||||
material.reflective = geom.reflectance > 0 && material.color[3] == 1.0f;
|
||||
}
|
||||
}
|
||||
mjrf_setRenderableLayerMask(renderable, geom.category);
|
||||
@@ -207,30 +212,28 @@ static void UpdateGeomMaterial(mjrRenderable* renderable, const mjvGeom& geom,
|
||||
mjrf_setRenderableWireframe(renderable, scene->flags[mjRND_WIREFRAME]);
|
||||
}
|
||||
|
||||
mjrMaterialTextures textures;
|
||||
mjr_defaultMaterialTextures(&textures);
|
||||
if (geom.matid >= 0) {
|
||||
textures.color = model_objs->GetTexture(geom.matid, mjTEXROLE_RGB);
|
||||
textures.normal = model_objs->GetTexture(geom.matid, mjTEXROLE_NORMAL);
|
||||
textures.emissive = model_objs->GetTexture(geom.matid, mjTEXROLE_EMISSIVE);
|
||||
textures.orm = model_objs->GetTexture(geom.matid, mjTEXROLE_ORM);
|
||||
textures.metallic = model_objs->GetTexture(geom.matid, mjTEXROLE_METALLIC);
|
||||
textures.roughness =
|
||||
material.color_texture = model_objs->GetTexture(geom.matid, mjTEXROLE_RGB);
|
||||
material.normal_texture =
|
||||
model_objs->GetTexture(geom.matid, mjTEXROLE_NORMAL);
|
||||
material.emissive_texture =
|
||||
model_objs->GetTexture(geom.matid, mjTEXROLE_EMISSIVE);
|
||||
material.orm_texture = model_objs->GetTexture(geom.matid, mjTEXROLE_ORM);
|
||||
material.metallic_texture =
|
||||
model_objs->GetTexture(geom.matid, mjTEXROLE_METALLIC);
|
||||
material.roughness_texture =
|
||||
model_objs->GetTexture(geom.matid, mjTEXROLE_ROUGHNESS);
|
||||
textures.occlusion =
|
||||
material.occlusion_texture =
|
||||
model_objs->GetTexture(geom.matid, mjTEXROLE_OCCLUSION);
|
||||
}
|
||||
|
||||
params.reflectance = geom.reflectance;
|
||||
params.emissive = geom.emission;
|
||||
params.specular = geom.specular;
|
||||
params.glossiness = geom.shininess;
|
||||
material.reflectance = geom.reflectance;
|
||||
material.emissive = geom.emission;
|
||||
material.specular = geom.specular;
|
||||
material.glossiness = geom.shininess;
|
||||
if (geom.matid >= 0) {
|
||||
params.metallic = model->mat_metallic[geom.matid];
|
||||
params.roughness = model->mat_roughness[geom.matid];
|
||||
params.tex_uniform = model->mat_texuniform[geom.matid];
|
||||
params.tex_repeat[0] = model->mat_texrepeat[(geom.matid * 2) + 0];
|
||||
params.tex_repeat[1] = model->mat_texrepeat[(geom.matid * 2) + 1];
|
||||
material.metallic = model->mat_metallic[geom.matid];
|
||||
material.roughness = model->mat_roughness[geom.matid];
|
||||
}
|
||||
|
||||
if (geom.segid >= 0) {
|
||||
@@ -246,9 +249,9 @@ static void UpdateGeomMaterial(mjrRenderable* renderable, const mjvGeom& geom,
|
||||
const uint8_t red = (segmentation_color >> 0) & 0xff;
|
||||
const uint8_t green = (segmentation_color >> 8) & 0xff;
|
||||
const uint8_t blue = (segmentation_color >> 16) & 0xff;
|
||||
params.segmentation_color[0] = static_cast<float>(red) / 255.0f;
|
||||
params.segmentation_color[1] = static_cast<float>(green) / 255.0f;
|
||||
params.segmentation_color[2] = static_cast<float>(blue) / 255.0f;
|
||||
material.segmentation_color[0] = static_cast<float>(red) / 255.0f;
|
||||
material.segmentation_color[1] = static_cast<float>(green) / 255.0f;
|
||||
material.segmentation_color[2] = static_cast<float>(blue) / 255.0f;
|
||||
}
|
||||
|
||||
// UvScale only applies to objects that don't have explicit UV coordinates
|
||||
@@ -258,28 +261,33 @@ static void UpdateGeomMaterial(mjrRenderable* renderable, const mjvGeom& geom,
|
||||
// The material's `texuniform` and `texrepeat` parameters allow us to scale
|
||||
// the programmatic UVs.
|
||||
|
||||
if (textures.color) {
|
||||
if (mjrf_getSamplerType(textures.color) == mjTEXTURE_2D) {
|
||||
if (material.color_texture) {
|
||||
const bool tex_uniform = model->mat_texuniform[geom.matid];
|
||||
if (mjrf_getSamplerType(material.color_texture) == mjTEXTURE_2D) {
|
||||
// For 2D textures, `tex_repeat` specifies how many times the texture
|
||||
// image is repeated. The `tex_uniform` flag determines if the repetition
|
||||
// is applied at in object space (false) or in world space (true).
|
||||
params.uv_scale[0] = params.tex_repeat[0];
|
||||
params.uv_scale[1] = params.tex_repeat[1];
|
||||
float tex_repeat[2];
|
||||
tex_repeat[0] = model->mat_texrepeat[(geom.matid * 2) + 0];
|
||||
tex_repeat[1] = model->mat_texrepeat[(geom.matid * 2) + 1];
|
||||
material.uv_scale[0] = tex_repeat[0];
|
||||
material.uv_scale[1] = tex_repeat[1];
|
||||
|
||||
if (geom.dataid >= 0 && geom.type != mjGEOM_PLANE) {
|
||||
if (geom.size[0] > mjMINVAL) {
|
||||
params.uv_scale[0] /= geom.size[0];
|
||||
material.uv_scale[0] /= geom.size[0];
|
||||
}
|
||||
if (geom.size[1] > mjMINVAL) {
|
||||
params.uv_scale[1] /= geom.size[1];
|
||||
material.uv_scale[1] /= geom.size[1];
|
||||
}
|
||||
}
|
||||
if (params.tex_uniform) {
|
||||
|
||||
if (tex_uniform) {
|
||||
if (geom.size[0] > 0) {
|
||||
params.uv_scale[0] *= geom.size[0];
|
||||
material.uv_scale[0] *= geom.size[0];
|
||||
}
|
||||
if (geom.size[1] > 0) {
|
||||
params.uv_scale[1] *= geom.size[1];
|
||||
material.uv_scale[1] *= geom.size[1];
|
||||
}
|
||||
}
|
||||
const bool is_infinite_plane =
|
||||
@@ -289,11 +297,11 @@ static void UpdateGeomMaterial(mjrRenderable* renderable, const mjvGeom& geom,
|
||||
// re-centering in engine_vis_visualize.c.
|
||||
const float plane_scale = static_cast<float>(mjMAXPLANEGRID) / 2.0f;
|
||||
const float tile_size_x =
|
||||
GetPlaneTileSize(model, geom.matid, params.tex_repeat[0]);
|
||||
GetPlaneTileSize(model, geom.matid, tex_repeat[0]);
|
||||
const float tile_size_y =
|
||||
GetPlaneTileSize(model, geom.matid, params.tex_repeat[1]);
|
||||
params.uv_scale[0] = 2.0f * plane_scale / tile_size_x;
|
||||
params.uv_scale[1] = 2.0f * plane_scale / tile_size_y;
|
||||
GetPlaneTileSize(model, geom.matid, tex_repeat[1]);
|
||||
material.uv_scale[0] = 2.0f * plane_scale / tile_size_x;
|
||||
material.uv_scale[1] = 2.0f * plane_scale / tile_size_y;
|
||||
}
|
||||
|
||||
// We want to do the equivalent of:
|
||||
@@ -301,42 +309,34 @@ static void UpdateGeomMaterial(mjrRenderable* renderable, const mjvGeom& geom,
|
||||
// mjr_setf4(tplane, 0, -0.5 * scl.y, 0, -0.5);
|
||||
// glTexGenfv(GL_S, GL_OBJECT_PLANE, splane);
|
||||
// glTexGenfv(GL_T, GL_OBJECT_PLANE, tplane);
|
||||
params.uv_scale[0] = 0.5f * params.uv_scale[0];
|
||||
params.uv_scale[1] = -0.5f * params.uv_scale[1];
|
||||
params.uv_offset[0] = -0.5f;
|
||||
params.uv_offset[1] = -0.5f;
|
||||
material.uv_scale[0] = 0.5f * material.uv_scale[0];
|
||||
material.uv_scale[1] = -0.5f * material.uv_scale[1];
|
||||
material.uv_offset[0] = -0.5f;
|
||||
material.uv_offset[1] = -0.5f;
|
||||
} else {
|
||||
// For cube maps, if `tex_uniform` is true, then scale the texture so that
|
||||
// it covers a 1x1 area of world space rather than the area of the object.
|
||||
if (params.tex_uniform) {
|
||||
params.uv_scale[0] = 1.0f / (geom.size[0] ? geom.size[0] : 1.0f);
|
||||
params.uv_scale[1] = 1.0f / (geom.size[1] ? geom.size[1] : 1.0f);
|
||||
params.uv_scale[2] = 1.0f / (geom.size[2] ? geom.size[2] : 1.0f);
|
||||
if (tex_uniform) {
|
||||
material.uv_scale[0] = 1.0f / (geom.size[0] ? geom.size[0] : 1.0f);
|
||||
material.uv_scale[1] = 1.0f / (geom.size[1] ? geom.size[1] : 1.0f);
|
||||
material.uv_scale[2] = 1.0f / (geom.size[2] ? geom.size[2] : 1.0f);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Apply material multipliers from the model.
|
||||
params.emissive *= model_objs->GetEmissiveMultiplier();
|
||||
params.specular *= model_objs->GetSpecularMultiplier();
|
||||
params.glossiness *= model_objs->GetShininessMultiplier();
|
||||
material.emissive *= model_objs->GetEmissiveMultiplier();
|
||||
material.specular *= model_objs->GetSpecularMultiplier();
|
||||
material.glossiness *= model_objs->GetShininessMultiplier();
|
||||
|
||||
mjrf_setRenderableMaterial(renderable, ¶ms, &textures);
|
||||
mjrf_setRenderableMaterial(renderable, &material);
|
||||
}
|
||||
|
||||
UniquePtr<mjrRenderable> CreateGeomRenderable(
|
||||
const mjvGeom& geom, const mjvScene* scene, mjrfContext* ctx,
|
||||
ModelObjects* model_objs, const float headpos[3]) {
|
||||
mjrShadingModel shading_model = mjSHADING_MODEL_SCENE_OBJECT;
|
||||
if (geom.type == mjGEOM_LINE || geom.type == mjGEOM_LINEBOX) {
|
||||
shading_model = mjSHADING_MODEL_DECOR_LINES;
|
||||
} else if (geom.category == mjCAT_DECOR) {
|
||||
shading_model = mjSHADING_MODEL_DECOR;
|
||||
}
|
||||
|
||||
mjrRenderableParams params;
|
||||
mjr_defaultRenderableParams(¶ms);
|
||||
params.shading_model = shading_model;
|
||||
auto renderable = CreateRenderable(ctx, params);
|
||||
PrepareGeomMeshes(renderable.get(), geom, scene, model_objs);
|
||||
UpdateGeomMaterial(renderable.get(), geom, scene, model_objs, headpos);
|
||||
|
||||
Reference in New Issue
Block a user