Rename types to conform to mjr naming conventions.
PiperOrigin-RevId: 904459745 Change-Id: I9369bec34e72dc973df45f844fcf1a6cb08b26b1
This commit is contained in:
committed by
Copybara-Service
parent
2bc97d8408
commit
8a87a1efbf
@@ -247,10 +247,12 @@ void ImguiBridge::Update() {
|
||||
auto& renderable = renderables_[renderable_index];
|
||||
renderable->SetMesh(mesh, index_offset, command.ElemCount);
|
||||
|
||||
MaterialTextures textures;
|
||||
mjrMaterialTextures textures;
|
||||
mjr_defaultMaterialTextures(&textures);
|
||||
textures.color = textures_[command.GetTexID()].get();
|
||||
|
||||
MaterialParams properties;
|
||||
mjrMaterialParams properties;
|
||||
mjr_defaultMaterialParams(&properties);
|
||||
properties.scissor[0] = command.ClipRect.x;
|
||||
properties.scissor[1] = height - command.ClipRect.w;
|
||||
properties.scissor[2] = command.ClipRect.z - command.ClipRect.x;
|
||||
|
||||
@@ -14,20 +14,59 @@
|
||||
|
||||
#include "experimental/filament/filament/material.h"
|
||||
|
||||
#include <array>
|
||||
|
||||
#include <filament/Color.h>
|
||||
#include <filament/Material.h>
|
||||
#include <filament/MaterialInstance.h>
|
||||
#include <filament/RenderableManager.h>
|
||||
#include <filament/TextureSampler.h>
|
||||
#include <mujoco/mujoco.h>
|
||||
#include "experimental/filament/filament/math_util.h"
|
||||
#include "experimental/filament/filament/texture.h"
|
||||
#include "experimental/filament/filament/object_manager.h"
|
||||
|
||||
namespace mujoco {
|
||||
|
||||
template <int N>
|
||||
static void setf(float (&arr)[N], const std::array<float, N>& values) {
|
||||
for (int i = 0; i < N; ++i) {
|
||||
arr[i] = values[i];
|
||||
}
|
||||
}
|
||||
|
||||
void mjr_defaultMaterialTextures(mjrMaterialTextures* textures) {
|
||||
textures->color = nullptr;
|
||||
textures->normal = nullptr;
|
||||
textures->metallic = nullptr;
|
||||
textures->roughness = nullptr;
|
||||
textures->occlusion = nullptr;
|
||||
textures->orm = nullptr;
|
||||
textures->emissive = nullptr;
|
||||
textures->reflection = nullptr;
|
||||
}
|
||||
|
||||
void mjr_defaultMaterialParams(mjrMaterialParams* params) {
|
||||
setf(params->color, {1.f, 1.f, 1.f, 1.f});
|
||||
setf(params->segmentation_color, {1, 1, 1, 1});
|
||||
setf(params->uv_scale, {1, 1});
|
||||
setf(params->uv_offset, {0, 0});
|
||||
setf(params->scissor, {0, 0, 0, 0});
|
||||
|
||||
params->emissive = -1.0f;
|
||||
params->specular = -1.0f;
|
||||
params->glossiness = -1.0f;
|
||||
params->metallic = -1.0f;
|
||||
params->roughness = -1.0f;
|
||||
params->reflectance = 0.0f;
|
||||
params->tex_uniform = false;
|
||||
params->reflective = false;
|
||||
}
|
||||
|
||||
|
||||
void UpdateMaterialInstance(filament::MaterialInstance* instance,
|
||||
const MaterialParams& params,
|
||||
const MaterialTextures& textures,
|
||||
const mjrMaterialParams& params,
|
||||
const mjrMaterialTextures& textures,
|
||||
ObjectManager* object_mgr) {
|
||||
if (params.scissor[2] != 0 && params.scissor[3] != 0) {
|
||||
instance->setScissor(params.scissor[0], params.scissor[1],
|
||||
@@ -37,11 +76,11 @@ void UpdateMaterialInstance(filament::MaterialInstance* instance,
|
||||
const filament::Material* material = instance->getMaterial();
|
||||
if (material->hasParameter("BaseColorFactor")) {
|
||||
instance->setParameter("BaseColorFactor", filament::RgbaType::sRGB,
|
||||
params.color);
|
||||
ReadFloat4(params.color));
|
||||
}
|
||||
if (material->hasParameter("SegmentationColor")) {
|
||||
instance->setParameter("SegmentationColor", filament::RgbaType::LINEAR,
|
||||
params.segmentation_color);
|
||||
ReadFloat4(params.segmentation_color));
|
||||
}
|
||||
if (material->hasParameter("EmissiveFactor")) {
|
||||
instance->setParameter("EmissiveFactor", params.emissive);
|
||||
@@ -61,10 +100,10 @@ void UpdateMaterialInstance(filament::MaterialInstance* instance,
|
||||
params.roughness >= 0 ? params.roughness : 1.0f);
|
||||
}
|
||||
if (material->hasParameter("UvScale")) {
|
||||
instance->setParameter("UvScale", params.uv_scale);
|
||||
instance->setParameter("UvScale", ReadFloat3(params.uv_scale));
|
||||
}
|
||||
if (material->hasParameter("UvOffset")) {
|
||||
instance->setParameter("UvOffset", params.uv_offset);
|
||||
instance->setParameter("UvOffset", ReadFloat3(params.uv_offset));
|
||||
}
|
||||
if (material->hasParameter("Reflectance")) {
|
||||
instance->setParameter("Reflectance", params.reflectance);
|
||||
|
||||
@@ -17,49 +17,51 @@
|
||||
|
||||
#include <filament/Engine.h>
|
||||
#include <filament/MaterialInstance.h>
|
||||
#include <math/vec2.h>
|
||||
#include <math/vec3.h>
|
||||
#include <math/vec4.h>
|
||||
#include <mujoco/mujoco.h>
|
||||
#include "experimental/filament/filament/texture.h"
|
||||
#include "experimental/filament/filament/object_manager.h"
|
||||
|
||||
namespace mujoco {
|
||||
|
||||
// The textures that can be assigned to the drawable's material.
|
||||
struct MaterialTextures {
|
||||
const Texture* color = nullptr;
|
||||
const Texture* normal = nullptr;
|
||||
const Texture* metallic = nullptr;
|
||||
const Texture* roughness = nullptr;
|
||||
const Texture* occlusion = nullptr;
|
||||
const Texture* orm = nullptr;
|
||||
const Texture* emissive = nullptr;
|
||||
const Texture* reflection = nullptr;
|
||||
struct mjrMaterialTextures {
|
||||
const Texture* color;
|
||||
const Texture* normal;
|
||||
const Texture* metallic;
|
||||
const Texture* roughness;
|
||||
const Texture* occlusion;
|
||||
const Texture* orm;
|
||||
const Texture* emissive;
|
||||
const Texture* reflection;
|
||||
};
|
||||
|
||||
void mjr_defaultMaterialTextures(mjrMaterialTextures* textures);
|
||||
|
||||
// The parameters that can be applied to the drawable's material.
|
||||
struct MaterialParams {
|
||||
filament::math::float4 color = {1, 1, 1, 1};
|
||||
filament::math::float4 segmentation_color = {1, 1, 1, 1};
|
||||
filament::math::float2 tex_repeat = {1, 1};
|
||||
filament::math::float3 uv_scale = {1, 1, 1};
|
||||
filament::math::float3 uv_offset = {0, 0, 0};
|
||||
filament::math::float4 scissor = {0, 0, 0, 0};
|
||||
float specular = -1.0f;
|
||||
float glossiness = -1.0f;
|
||||
float metallic = -1.0f;
|
||||
float roughness = -1.0f;
|
||||
float emissive = -1.0f;
|
||||
float reflectance = 0.0f;
|
||||
bool tex_uniform = false;
|
||||
bool reflective = false;
|
||||
struct mjrMaterialParams {
|
||||
float color[4];
|
||||
float segmentation_color[4];
|
||||
float tex_repeat[2];
|
||||
float uv_scale[3];
|
||||
float uv_offset[3];
|
||||
float scissor[4];
|
||||
float specular;
|
||||
float glossiness;
|
||||
float metallic;
|
||||
float roughness;
|
||||
float emissive;
|
||||
float reflectance;
|
||||
mjtByte tex_uniform;
|
||||
mjtByte reflective;
|
||||
};
|
||||
|
||||
void mjr_defaultMaterialParams(mjrMaterialParams* params);
|
||||
|
||||
// Updates the material instances based on the currently set parameters and
|
||||
// textures.
|
||||
void UpdateMaterialInstance(filament::MaterialInstance* instance,
|
||||
const MaterialParams& params,
|
||||
const MaterialTextures& textures,
|
||||
const mjrMaterialParams& params,
|
||||
const mjrMaterialTextures& textures,
|
||||
ObjectManager* object_mgr);
|
||||
|
||||
} // namespace mujoco
|
||||
|
||||
@@ -41,7 +41,10 @@ void mjr_defaultRenderableParams(mjrRenderableParams* params) {
|
||||
}
|
||||
|
||||
Renderable::Renderable(ObjectManager* object_mgr, const mjrRenderableParams& params)
|
||||
: object_mgr_(object_mgr), params_(params) {}
|
||||
: object_mgr_(object_mgr), params_(params) {
|
||||
mjr_defaultMaterialParams(&material_params_);
|
||||
mjr_defaultMaterialTextures(&material_textures_);
|
||||
}
|
||||
|
||||
Renderable::~Renderable() noexcept {
|
||||
filament::Engine* engine = GetEngine();
|
||||
@@ -198,8 +201,8 @@ void Renderable::RemoveFromScene(filament::Scene* scene) {
|
||||
assigned_scene_ = nullptr;
|
||||
}
|
||||
|
||||
void Renderable::UpdateMaterial(const MaterialParams& params,
|
||||
const MaterialTextures& textures) {
|
||||
void Renderable::UpdateMaterial(const mjrMaterialParams& params,
|
||||
const mjrMaterialTextures& textures) {
|
||||
material_params_ = params;
|
||||
material_textures_ = textures;
|
||||
|
||||
@@ -237,11 +240,11 @@ void Renderable::AssignMaterial(DrawMode mode,
|
||||
}
|
||||
}
|
||||
|
||||
const MaterialParams& Renderable::GetMaterialParams() const {
|
||||
const mjrMaterialParams& Renderable::GetMaterialParams() const {
|
||||
return material_params_;
|
||||
}
|
||||
|
||||
const MaterialTextures& Renderable::GetMaterialTextures() const {
|
||||
const mjrMaterialTextures& Renderable::GetMaterialTextures() const {
|
||||
return material_textures_;
|
||||
}
|
||||
|
||||
@@ -374,7 +377,7 @@ ObjectManager::MaterialType Renderable::GetColorMaterialType() const {
|
||||
}
|
||||
|
||||
if (material_textures_.color == nullptr) {
|
||||
if (material_params_.color.a < 1.0f) {
|
||||
if (material_params_.color[3] < 1.0f) {
|
||||
return ObjectManager::kPhongColorFade;
|
||||
} else if (material_params_.reflective) {
|
||||
return ObjectManager::kPhongColorReflect;
|
||||
@@ -383,7 +386,7 @@ ObjectManager::MaterialType Renderable::GetColorMaterialType() const {
|
||||
}
|
||||
} else if (material_textures_.color->GetFilamentTexture()->getTarget() ==
|
||||
filament::Texture::Sampler::SAMPLER_CUBEMAP) {
|
||||
if (material_params_.color.a < 1.0f) {
|
||||
if (material_params_.color[3] < 1.0f) {
|
||||
return ObjectManager::kPhongCubeFade;
|
||||
} else if (material_params_.reflective) {
|
||||
return ObjectManager::kPhongCubeReflect;
|
||||
@@ -391,7 +394,7 @@ ObjectManager::MaterialType Renderable::GetColorMaterialType() const {
|
||||
return ObjectManager::kPhongCube;
|
||||
}
|
||||
} else if (has_texcoords) {
|
||||
if (material_params_.color.a < 1.0f) {
|
||||
if (material_params_.color[3] < 1.0f) {
|
||||
return ObjectManager::kPhong2dUvFade;
|
||||
} else if (material_params_.reflective) {
|
||||
return ObjectManager::kPhong2dUvReflect;
|
||||
@@ -399,7 +402,7 @@ ObjectManager::MaterialType Renderable::GetColorMaterialType() const {
|
||||
return ObjectManager::kPhong2dUv;
|
||||
}
|
||||
} else {
|
||||
if (material_params_.color.a < 1.0f) {
|
||||
if (material_params_.color[3] < 1.0f) {
|
||||
return ObjectManager::kPhong2dFade;
|
||||
} else if (material_params_.reflective) {
|
||||
return ObjectManager::kPhong2dReflect;
|
||||
|
||||
@@ -127,14 +127,14 @@ class Renderable {
|
||||
void SetDrawMode(DrawMode mode);
|
||||
|
||||
// Updates the parameters for the material.
|
||||
void UpdateMaterial(const MaterialParams& params,
|
||||
const MaterialTextures& textures);
|
||||
void UpdateMaterial(const mjrMaterialParams& params,
|
||||
const mjrMaterialTextures& textures);
|
||||
|
||||
// Returns the current material parameters.
|
||||
const MaterialParams& GetMaterialParams() const;
|
||||
const mjrMaterialParams& GetMaterialParams() const;
|
||||
|
||||
// Returns the current material textures.
|
||||
const MaterialTextures& GetMaterialTextures() const;
|
||||
const mjrMaterialTextures& GetMaterialTextures() const;
|
||||
|
||||
// Returns the filament Engine managing the renderables.
|
||||
filament::Engine* GetEngine();
|
||||
@@ -156,8 +156,8 @@ class Renderable {
|
||||
ObjectManager* object_mgr_;
|
||||
mjrRenderableParams params_;
|
||||
filament::MaterialInstance* instances_[kNumDrawModes] = {nullptr};
|
||||
MaterialParams material_params_;
|
||||
MaterialTextures material_textures_;
|
||||
mjrMaterialParams material_params_;
|
||||
mjrMaterialTextures material_textures_;
|
||||
DrawMode draw_mode_ = DrawMode::Color;
|
||||
filament::Scene* assigned_scene_ = nullptr;
|
||||
std::vector<Part> parts_;
|
||||
|
||||
@@ -351,8 +351,12 @@ static void UpdateGeomMaterial(Renderable& renderable, const mjvGeom& geom,
|
||||
const mjModel* model = model_objs->GetModel();
|
||||
|
||||
const bool use_segid_color = scene->flags[mjRND_IDCOLOR];
|
||||
MaterialParams params;
|
||||
params.color = ReadFloat4(geom.rgba);
|
||||
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];
|
||||
if (geom.type == mjGEOM_PLANE) {
|
||||
if (IsBehind(headpos, geom.pos, geom.mat)) {
|
||||
params.color[3] *= 0.3;
|
||||
@@ -360,7 +364,7 @@ static void UpdateGeomMaterial(Renderable& renderable, const mjvGeom& geom,
|
||||
params.reflective = false;
|
||||
} else {
|
||||
renderable.SetReceiveShadows(true);
|
||||
params.reflective = geom.reflectance > 0 && params.color.a == 1.0f;
|
||||
params.reflective = geom.reflectance > 0 && params.color[3] == 1.0f;
|
||||
}
|
||||
}
|
||||
renderable.SetLayerMask(geom.category);
|
||||
@@ -371,7 +375,8 @@ static void UpdateGeomMaterial(Renderable& renderable, const mjvGeom& geom,
|
||||
renderable.SetWireframe(scene->flags[mjRND_WIREFRAME]);
|
||||
}
|
||||
|
||||
MaterialTextures textures;
|
||||
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);
|
||||
@@ -392,7 +397,8 @@ static void UpdateGeomMaterial(Renderable& renderable, const mjvGeom& geom,
|
||||
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 = ReadFloat2(model->mat_texrepeat, geom.matid);
|
||||
params.tex_repeat[0] = model->mat_texrepeat[(geom.matid * 2) + 0];
|
||||
params.tex_repeat[1] = model->mat_texrepeat[(geom.matid * 2) + 1];
|
||||
}
|
||||
|
||||
if (geom.segid >= 0) {
|
||||
@@ -408,9 +414,9 @@ static void UpdateGeomMaterial(Renderable& 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.x = static_cast<float>(red) / 255.0f;
|
||||
params.segmentation_color.y = static_cast<float>(green) / 255.0f;
|
||||
params.segmentation_color.z = static_cast<float>(blue) / 255.0f;
|
||||
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;
|
||||
}
|
||||
|
||||
// UvScale only applies to objects that don't have explicit UV coordinates
|
||||
@@ -426,23 +432,23 @@ static void UpdateGeomMaterial(Renderable& renderable, const mjvGeom& geom,
|
||||
// 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.x = params.tex_repeat.x;
|
||||
params.uv_scale.y = params.tex_repeat.y;
|
||||
params.uv_scale[0] = params.tex_repeat[0];
|
||||
params.uv_scale[1] = params.tex_repeat[1];
|
||||
|
||||
if (geom.dataid >= 0 && geom.type != mjGEOM_PLANE) {
|
||||
if (geom.size[0] > mjMINVAL) {
|
||||
params.uv_scale.x /= geom.size[0];
|
||||
params.uv_scale[0] /= geom.size[0];
|
||||
}
|
||||
if (geom.size[1] > mjMINVAL) {
|
||||
params.uv_scale.y /= geom.size[1];
|
||||
params.uv_scale[1] /= geom.size[1];
|
||||
}
|
||||
}
|
||||
if (params.tex_uniform) {
|
||||
if (geom.size[0] > 0) {
|
||||
params.uv_scale.x *= geom.size[0];
|
||||
params.uv_scale[0] *= geom.size[0];
|
||||
}
|
||||
if (geom.size[1] > 0) {
|
||||
params.uv_scale.y *= geom.size[1];
|
||||
params.uv_scale[1] *= geom.size[1];
|
||||
}
|
||||
}
|
||||
const bool is_infinite_plane =
|
||||
@@ -452,11 +458,11 @@ static void UpdateGeomMaterial(Renderable& 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.x);
|
||||
GetPlaneTileSize(model, geom.matid, params.tex_repeat[0]);
|
||||
const float tile_size_y =
|
||||
GetPlaneTileSize(model, geom.matid, params.tex_repeat.y);
|
||||
params.uv_scale.x = 2.0f * plane_scale / tile_size_x;
|
||||
params.uv_scale.y = 2.0f * plane_scale / 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;
|
||||
}
|
||||
|
||||
// We want to do the equivalent of:
|
||||
@@ -464,17 +470,17 @@ static void UpdateGeomMaterial(Renderable& 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.x = 0.5f * params.uv_scale.x;
|
||||
params.uv_scale.y = -0.5f * params.uv_scale.y;
|
||||
params.uv_offset.x = -0.5f;
|
||||
params.uv_offset.y = -0.5f;
|
||||
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;
|
||||
} 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.x = 1.0f / (geom.size[0] ? geom.size[0] : 1.0f);
|
||||
params.uv_scale.y = 1.0f / (geom.size[1] ? geom.size[1] : 1.0f);
|
||||
params.uv_scale.z = 1.0f / (geom.size[2] ? geom.size[2] : 1.0f);
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -300,7 +300,7 @@ void SceneView::AddReflectiveRenderable(Renderable* renderable) {
|
||||
target->Prepare(viewport.width, viewport.height);
|
||||
|
||||
if (reflections_enabled_) {
|
||||
MaterialTextures textures = renderable->GetMaterialTextures();
|
||||
mjrMaterialTextures textures = renderable->GetMaterialTextures();
|
||||
textures.reflection = target->GetColorTexture();
|
||||
renderable->UpdateMaterial(renderable->GetMaterialParams(), textures);
|
||||
}
|
||||
@@ -332,7 +332,7 @@ void SceneView::EnableReflections() {
|
||||
|
||||
for (int i = 0; i < reflectives_.size(); ++i) {
|
||||
Renderable* renderable = reflectives_[i];
|
||||
MaterialTextures textures = renderable->GetMaterialTextures();
|
||||
mjrMaterialTextures textures = renderable->GetMaterialTextures();
|
||||
textures.reflection = reflect_targets_[i]->GetColorTexture();
|
||||
renderable->UpdateMaterial(renderable->GetMaterialParams(), textures);
|
||||
}
|
||||
@@ -341,7 +341,7 @@ void SceneView::EnableReflections() {
|
||||
void SceneView::DisableReflections() {
|
||||
reflections_enabled_ = false;
|
||||
for (Renderable* renderable : reflectives_) {
|
||||
MaterialTextures textures = renderable->GetMaterialTextures();
|
||||
mjrMaterialTextures textures = renderable->GetMaterialTextures();
|
||||
textures.reflection = nullptr;
|
||||
renderable->UpdateMaterial(renderable->GetMaterialParams(), textures);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user