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

View File

@ -38,11 +38,27 @@ class BlenderSceneDelegate : public pxr::HdSceneDelegate {
}; };
struct ShadingSettings { struct ShadingSettings {
bool use_scene_lights = false; bool use_scene_lights = true;
short shading_flag; bool use_scene_world = true;
std::string lookdev_light; std::string studiolight_name;
float rotation; float studiolight_rotation;
float intensity; 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, BlenderSceneDelegate(pxr::HdRenderIndex *parent_index,
@ -102,8 +118,8 @@ class BlenderSceneDelegate : public pxr::HdSceneDelegate {
void add_new_objects(bool only_lights = false); void add_new_objects(bool only_lights = false);
void remove_unused_objects(); void remove_unused_objects();
void update_visibility(); void update_visibility();
bool set_light_shading_settings(View3D const *view3d); bool set_light_shading_settings();
bool set_world_shading_settings(View3D const *view3d); bool set_world_shading_settings();
ObjectDataMap objects_; ObjectDataMap objects_;
MaterialDataMap materials_; MaterialDataMap materials_;
@ -111,10 +127,4 @@ class BlenderSceneDelegate : public pxr::HdSceneDelegate {
std::unique_ptr<WorldData> world_data_; 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 } // 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) { light_instances_.remove_if([&](auto item) {
bool res = !available_objects.contains(item.key.GetName()); 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) ((Object *)item.value.data->id)->type == OB_LAMP)
{ {
res = true; res = true;
@ -358,10 +358,10 @@ void InstancerData::write_instances()
scene_delegate_->depsgraph, scene_delegate_->scene, (Object *)id); scene_delegate_->depsgraph, scene_delegate_->scene, (Object *)id);
LISTBASE_FOREACH (DupliObject *, dupli, lb) { LISTBASE_FOREACH (DupliObject *, dupli, lb) {
Object *ob = dupli->ob; 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) ob->type == OB_LAMP)
{ {
return; continue;
} }
if (!is_supported(ob) || !is_instance_visible(ob)) { if (!is_supported(ob) || !is_instance_visible(ob)) {
continue; continue;

View File

@ -12,7 +12,6 @@
#include <pxr/usd/usdLux/tokens.h> #include <pxr/usd/usdLux/tokens.h>
#include "BKE_context.h" #include "BKE_context.h"
#include "DEG_depsgraph_query.h"
#include "DNA_node_types.h" #include "DNA_node_types.h"
#include "BKE_node.h" #include "BKE_node.h"
@ -50,67 +49,79 @@ void WorldData::init()
data_.clear(); data_.clear();
data_[pxr::UsdLuxTokens->orientToStageUpAxis] = true; 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))
{
/* TODO: Create nodes parsing system */
bNode *output_node = ntreeShaderOutputNode(world->nodetree, SHD_OUTPUT_ALL); float intensity = 1.0f;
blender::Span<bNodeSocket *> input_sockets = output_node->input_sockets(); float exposure = world->exposure;
bNodeSocket *input_socket = nullptr; pxr::GfVec3f color(1.0f, 1.0f, 1.0f);
pxr::SdfAssetPath texture_file;
for (auto socket : input_sockets) { if (scene_delegate_->shading_settings.use_scene_world) {
if (STREQ(socket->name, "Surface")) { if (world->use_nodes) {
input_socket = socket; /* TODO: Create nodes parsing system */
break;
bNode *output_node = ntreeShaderOutputNode(world->nodetree, SHD_OUTPUT_ALL);
blender::Span<bNodeSocket *> input_sockets = output_node->input_sockets();
bNodeSocket *input_socket = nullptr;
for (auto socket : input_sockets) {
if (STREQ(socket->name, "Surface")) {
input_socket = socket;
break;
}
}
if (!input_socket) {
return;
}
bNodeLink const *link = input_socket->directly_linked_links()[0];
if (input_socket->directly_linked_links().is_empty()) {
return;
} }
}
if (!input_socket) {
return;
}
bNodeLink const *link = input_socket->directly_linked_links()[0];
if (input_socket->directly_linked_links().is_empty()) {
return;
}
bNode *input_node = link->fromnode; bNode *input_node = link->fromnode;
if (input_node->type != SH_NODE_BACKGROUND) { if (input_node->type != SH_NODE_BACKGROUND) {
return; return;
} }
bNodeSocket color_input = input_node->input_by_identifier("Color"); bNodeSocket color_input = input_node->input_by_identifier("Color");
bNodeSocket strength_input = input_node->input_by_identifier("Strength"); bNodeSocket strength_input = input_node->input_by_identifier("Strength");
float const *strength = strength_input.default_value_typed<float>(); float const *strength = strength_input.default_value_typed<float>();
float const *color = color_input.default_value_typed<float>(); float const *input_color = color_input.default_value_typed<float>();
data_[pxr::HdLightTokens->intensity] = strength[1]; intensity = strength[1];
data_[pxr::HdLightTokens->exposure] = 1.0f; color = pxr::GfVec3f(input_color[0], input_color[1], input_color[2]);
data_[pxr::HdLightTokens->color] = pxr::GfVec3f(color[0], color[1], color[2]);
if (!color_input.directly_linked_links().is_empty()) { if (!color_input.directly_linked_links().is_empty()) {
bNode *color_input_node = color_input.directly_linked_links()[0]->fromnode; bNode *color_input_node = color_input.directly_linked_links()[0]->fromnode;
if (color_input_node->type == SH_NODE_TEX_IMAGE) { if (color_input_node->type == SH_NODE_TEX_IMAGE) {
NodeTexImage *tex = static_cast<NodeTexImage *>(color_input_node->storage); NodeTexImage *tex = static_cast<NodeTexImage *>(color_input_node->storage);
Image *image = (Image *)color_input_node->id; Image *image = (Image *)color_input_node->id;
if (image) { if (image) {
std::string image_path = cache_or_get_image_file( std::string image_path = cache_or_get_image_file(
image, scene_delegate_->context, &tex->iuser); image, scene_delegate_->context, &tex->iuser);
if (!image_path.empty()) { if (!image_path.empty()) {
data_[pxr::HdLightTokens->textureFile] = pxr::SdfAssetPath(image_path, image_path); texture_file = pxr::SdfAssetPath(image_path, image_path);
}
} }
} }
} }
} }
else {
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 { else {
float intensity = 1.0f; if (scene_delegate_->view3d && !scene_delegate_->shading_settings.use_scene_world) {
if (LOOK_DEV_STUDIO_LIGHT_ENABLED(scene_delegate_->view3d)) {
StudioLight *sl = BKE_studiolight_find(scene_delegate_->view3d->shading.lookdev_light, StudioLight *sl = BKE_studiolight_find(scene_delegate_->view3d->shading.lookdev_light,
STUDIOLIGHT_ORIENTATIONS_MATERIAL_MODE); STUDIOLIGHT_ORIENTATIONS_MATERIAL_MODE);
if (sl != NULL && sl->flag & STUDIOLIGHT_TYPE_WORLD) { 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( transform *= pxr::GfMatrix4d(
pxr::GfRotation(pxr::GfVec3d(0.0, 0.0, -1.0), pxr::GfRotation(pxr::GfVec3d(0.0, 0.0, -1.0),
RAD2DEGF(scene_delegate_->view3d->shading.studiolight_rot_z)), RAD2DEGF(scene_delegate_->view3d->shading.studiolight_rot_z)),
@ -119,17 +130,12 @@ void WorldData::init()
intensity = scene_delegate_->view3d->shading.studiolight_intensity / 2; 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()) { data_[pxr::HdLightTokens->intensity] = intensity;
pxr::GfVec3f c = data_[pxr::HdLightTokens->color].Get<pxr::GfVec3f>(); data_[pxr::HdLightTokens->exposure] = exposure;
float color[4] = {c[0], c[1], c[2], 1.0f}; data_[pxr::HdLightTokens->color] = color;
std::string image_path = cache_image_color(color); data_[pxr::HdLightTokens->textureFile] = texture_file;
data_[pxr::HdLightTokens->textureFile] = pxr::SdfAssetPath(image_path, image_path);
}
} }
void WorldData::insert() void WorldData::insert()