Implement Viewport render with material preview #56

Merged
Bogdan Nagirniak merged 22 commits from BLEN-421 into hydra-render 2023-06-30 09:03:28 +02:00
4 changed files with 104 additions and 91 deletions
Showing only changes of commit ffb3b4a4f6 - Show all commits

View File

@ -205,8 +205,8 @@ void BlenderSceneDelegate::populate(Depsgraph *deps, bContext *cont)
check_updates();
}
else {
set_light_shading_settings(view3d);
set_world_shading_settings(view3d);
set_light_shading_settings();
set_world_shading_settings();
add_new_objects();
update_world();
}
@ -340,7 +340,7 @@ void BlenderSceneDelegate::update_objects(Object *object)
if (!ObjectData::is_supported(object)) {
return;
}
if (view3d && !V3D_USES_SCENE_LIGHTS(view3d) && object->type == OB_LAMP) {
if (!shading_settings.use_scene_lights && object->type == OB_LAMP) {
return;
}
pxr::SdfPath id = object_prim_id(object);
@ -426,11 +426,11 @@ void BlenderSceneDelegate::check_updates()
bool do_update_visibility = false;
bool do_update_world = false;
if (set_world_shading_settings(view3d)) {
if (set_world_shading_settings()) {
do_update_world = true;
}
if (set_light_shading_settings(view3d)) {
if (set_light_shading_settings()) {
if (shading_settings.use_scene_lights) {
add_new_objects(true);
}
@ -472,7 +472,7 @@ void BlenderSceneDelegate::check_updates()
} break;
case ID_WO: {
if (id->recalc & ID_RECALC_SHADING) {
if (shading_settings.use_scene_world && id->recalc & ID_RECALC_SHADING) {
do_update_world = true;
}
} break;
@ -591,7 +591,9 @@ void BlenderSceneDelegate::remove_unused_objects()
/* Remove unused objects */
objects_.remove_if([&](auto item) {
bool ret = !available_objects.contains(item.key.GetName());
if (!V3D_USES_SCENE_LIGHTS(view3d) && ((Object *)item.value->id)->type == OB_LAMP) {
if (!shading_settings.use_scene_lights &&
((Object *)item.value->id)->type == OB_LAMP)
{
ret = true;
}
if (ret) {
@ -667,7 +669,7 @@ void BlenderSceneDelegate::update_visibility()
ITER_END;
}
bool BlenderSceneDelegate::set_light_shading_settings(View3D const *view3d)
bool BlenderSceneDelegate::set_light_shading_settings()
{
bool ret = false;
if (view3d) {
@ -679,21 +681,16 @@ bool BlenderSceneDelegate::set_light_shading_settings(View3D const *view3d)
return ret;
}
bool BlenderSceneDelegate::set_world_shading_settings(View3D const *view3d)
bool BlenderSceneDelegate::set_world_shading_settings()
{
bool ret = false;
if (view3d) {
if (shading_settings.shading_flag != view3d->shading.flag ||
shading_settings.lookdev_light != view3d->shading.lookdev_light ||
shading_settings.rotation != view3d->shading.studiolight_rot_z ||
shading_settings.intensity != view3d->shading.studiolight_intensity)
{
shading_settings.shading_flag = view3d->shading.flag;
shading_settings.lookdev_light = view3d->shading.lookdev_light;
shading_settings.rotation = view3d->shading.studiolight_rot_z;
shading_settings.intensity = view3d->shading.studiolight_intensity;
ret = true;
}
ShadingSettings prev_settings(shading_settings);
shading_settings.use_scene_world = V3D_USES_SCENE_WORLD(view3d);
shading_settings.studiolight_name = view3d->shading.lookdev_light;
shading_settings.studiolight_rotation = view3d->shading.studiolight_rot_z;
shading_settings.studiolight_intensity = view3d->shading.studiolight_intensity;
ret = shading_settings != prev_settings;
}
return ret;
}

View File

@ -38,11 +38,27 @@ class BlenderSceneDelegate : public pxr::HdSceneDelegate {
};
struct ShadingSettings {
bool use_scene_lights = false;
short shading_flag;
std::string lookdev_light;
float rotation;
float intensity;
bool use_scene_lights = true;
bool use_scene_world = true;
std::string studiolight_name;
float studiolight_rotation;
float studiolight_intensity;
bool operator==(const ShadingSettings& other) {
return use_scene_lights == other.use_scene_lights &&
use_scene_world == other.use_scene_world &&
studiolight_name == other.studiolight_name &&
studiolight_rotation == other.studiolight_rotation &&
studiolight_intensity == other.studiolight_intensity;
}
bool operator!=(const ShadingSettings &other)
{
return use_scene_lights != other.use_scene_lights ||
use_scene_world != other.use_scene_world ||
studiolight_name != other.studiolight_name ||
studiolight_rotation != other.studiolight_rotation ||
studiolight_intensity != other.studiolight_intensity;
}
};
BlenderSceneDelegate(pxr::HdRenderIndex *parent_index,
@ -102,8 +118,8 @@ class BlenderSceneDelegate : public pxr::HdSceneDelegate {
void add_new_objects(bool only_lights = false);
void remove_unused_objects();
void update_visibility();
bool set_light_shading_settings(View3D const *view3d);
bool set_world_shading_settings(View3D const *view3d);
bool set_light_shading_settings();
bool set_world_shading_settings();
ObjectDataMap objects_;
MaterialDataMap materials_;
@ -111,10 +127,4 @@ class BlenderSceneDelegate : public pxr::HdSceneDelegate {
std::unique_ptr<WorldData> world_data_;
};
#define LOOK_DEV_STUDIO_LIGHT_ENABLED(v3d) \
((v3d) && (((v3d->shading.type == OB_MATERIAL) && \
((v3d->shading.flag & V3D_SHADING_SCENE_WORLD) == 0)) || \
((v3d->shading.type == OB_RENDER) && \
((v3d->shading.flag & V3D_SHADING_SCENE_WORLD_RENDER) == 0))))
} // namespace blender::render::hydra

View File

@ -272,7 +272,7 @@ void InstancerData::check_remove(Set<std::string> &available_objects)
light_instances_.remove_if([&](auto item) {
bool res = !available_objects.contains(item.key.GetName());
if (!V3D_USES_SCENE_LIGHTS(scene_delegate_->view3d) &&
if (!scene_delegate_->shading_settings.use_scene_lights &&
((Object *)item.value.data->id)->type == OB_LAMP)
{
res = true;
@ -358,10 +358,10 @@ void InstancerData::write_instances()
scene_delegate_->depsgraph, scene_delegate_->scene, (Object *)id);
LISTBASE_FOREACH (DupliObject *, dupli, lb) {
Object *ob = dupli->ob;
if (scene_delegate_->view3d && !V3D_USES_SCENE_LIGHTS(scene_delegate_->view3d) &&
if (!scene_delegate_->shading_settings.use_scene_lights &&
ob->type == OB_LAMP)
{
return;
continue;
}
if (!is_supported(ob) || !is_instance_visible(ob)) {
continue;

View File

@ -12,7 +12,6 @@
#include <pxr/usd/usdLux/tokens.h>
#include "BKE_context.h"
#include "DEG_depsgraph_query.h"
#include "DNA_node_types.h"
#include "BKE_node.h"
@ -50,11 +49,14 @@ void WorldData::init()
data_.clear();
data_[pxr::UsdLuxTokens->orientToStageUpAxis] = true;
eEvaluationMode deg_mode = DEG_get_mode(scene_delegate_->depsgraph);
if (world->use_nodes && ((deg_mode == DAG_EVAL_VIEWPORT && scene_delegate_->view3d &&
V3D_USES_SCENE_WORLD(scene_delegate_->view3d)) ||
deg_mode == DAG_EVAL_RENDER))
{
float intensity = 1.0f;
float exposure = world->exposure;
pxr::GfVec3f color(1.0f, 1.0f, 1.0f);
pxr::SdfAssetPath texture_file;
if (scene_delegate_->shading_settings.use_scene_world) {
if (world->use_nodes) {
/* TODO: Create nodes parsing system */
bNode *output_node = ntreeShaderOutputNode(world->nodetree, SHD_OUTPUT_ALL);
@ -84,10 +86,9 @@ void WorldData::init()
bNodeSocket strength_input = input_node->input_by_identifier("Strength");
float const *strength = strength_input.default_value_typed<float>();
float const *color = color_input.default_value_typed<float>();
data_[pxr::HdLightTokens->intensity] = strength[1];
data_[pxr::HdLightTokens->exposure] = 1.0f;
data_[pxr::HdLightTokens->color] = pxr::GfVec3f(color[0], color[1], color[2]);
float const *input_color = color_input.default_value_typed<float>();
intensity = strength[1];
color = pxr::GfVec3f(input_color[0], input_color[1], input_color[2]);
if (!color_input.directly_linked_links().is_empty()) {
bNode *color_input_node = color_input.directly_linked_links()[0]->fromnode;
@ -98,19 +99,29 @@ void WorldData::init()
std::string image_path = cache_or_get_image_file(
image, scene_delegate_->context, &tex->iuser);
if (!image_path.empty()) {
data_[pxr::HdLightTokens->textureFile] = pxr::SdfAssetPath(image_path, image_path);
texture_file = pxr::SdfAssetPath(image_path, image_path);
}
}
}
}
}
else {
float intensity = 1.0f;
if (LOOK_DEV_STUDIO_LIGHT_ENABLED(scene_delegate_->view3d)) {
intensity = 1.0f;
color = pxr::GfVec3f(world->horr, world->horg, world->horb);
}
if (texture_file.GetAssetPath().empty()) {
float fill_color[4] = {color[0], color[1], color[2], 1.0f};
std::string image_path = cache_image_color(fill_color);
texture_file = pxr::SdfAssetPath(image_path, image_path);
}
}
else {
if (scene_delegate_->view3d && !scene_delegate_->shading_settings.use_scene_world) {
StudioLight *sl = BKE_studiolight_find(scene_delegate_->view3d->shading.lookdev_light,
STUDIOLIGHT_ORIENTATIONS_MATERIAL_MODE);
if (sl != NULL && sl->flag & STUDIOLIGHT_TYPE_WORLD) {
data_[pxr::HdLightTokens->textureFile] = pxr::SdfAssetPath(sl->filepath, sl->filepath);
texture_file = pxr::SdfAssetPath(sl->filepath, sl->filepath);
transform *= pxr::GfMatrix4d(
pxr::GfRotation(pxr::GfVec3d(0.0, 0.0, -1.0),
RAD2DEGF(scene_delegate_->view3d->shading.studiolight_rot_z)),
@ -119,17 +130,12 @@ void WorldData::init()
intensity = scene_delegate_->view3d->shading.studiolight_intensity / 2;
}
}
data_[pxr::HdLightTokens->intensity] = intensity;
data_[pxr::HdLightTokens->exposure] = world->exposure;
data_[pxr::HdLightTokens->color] = pxr::GfVec3f(world->horr, world->horg, world->horb);
}
if (data_.find(pxr::HdLightTokens->textureFile) == data_.end()) {
pxr::GfVec3f c = data_[pxr::HdLightTokens->color].Get<pxr::GfVec3f>();
float color[4] = {c[0], c[1], c[2], 1.0f};
std::string image_path = cache_image_color(color);
data_[pxr::HdLightTokens->textureFile] = pxr::SdfAssetPath(image_path, image_path);
}
data_[pxr::HdLightTokens->intensity] = intensity;
data_[pxr::HdLightTokens->exposure] = exposure;
data_[pxr::HdLightTokens->color] = color;
data_[pxr::HdLightTokens->textureFile] = texture_file;
}
void WorldData::insert()