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 &&
BogdanNagirniak marked this conversation as resolved Outdated

move this check to L573

move this check to L573
((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()
BogdanNagirniak marked this conversation as resolved Outdated
if (!view3d) {
  return false;
}
.....
return !(shading_settings == prev_settings);
``` if (!view3d) { return false; } ..... return !(shading_settings == prev_settings); ```
{
bool ret = false;
BogdanNagirniak marked this conversation as resolved Outdated

simplify by using add operator==() and copy constructor to ShadingSettings

ShadingSettings prev_settings(shading_settings);
shading_settings. ....
return shading_settings != prev_settings;
simplify by using add `operator==()` and copy constructor to ShadingSettings ``` ShadingSettings prev_settings(shading_settings); shading_settings. .... return shading_settings != prev_settings; ```
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;
BogdanNagirniak marked this conversation as resolved Outdated
    bool use_scene_lights = true;
    bool use_scene_world = true;
    std::string studiolight_name;
    float studiolight_rotation;
    floatstudiolight_intensity;
``` bool use_scene_lights = true; bool use_scene_world = true; std::string studiolight_name; float studiolight_rotation; floatstudiolight_intensity; ```
bool operator==(const ShadingSettings& other) {
BogdanNagirniak marked this conversation as resolved Outdated

move implementation to blender_scene_delegate.cc

move implementation to blender_scene_delegate.cc
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 &&
BogdanNagirniak marked this conversation as resolved Outdated

use scene_delegate_->shading_settings.use_scene_lights

use `scene_delegate_->shading_settings.use_scene_lights`

lights shouldn't be in available_objects, this can be reverted

lights shouldn't be in available_objects, this can be reverted
((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)
BogdanNagirniak marked this conversation as resolved Outdated

if (ob->type == OB_LAMP && !scene_delegate_->shading_settings.use_scene_lights)

`if (ob->type == OB_LAMP && !scene_delegate_->shading_settings.use_scene_lights)`
{
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"
BogdanNagirniak marked this conversation as resolved Outdated

shouldn't be needed

shouldn't be needed
#include "BKE_node.h"
@ -50,67 +49,79 @@ 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))
{
/* TODO: Create nodes parsing system */
bNode *output_node = ntreeShaderOutputNode(world->nodetree, SHD_OUTPUT_ALL);
blender::Span<bNodeSocket *> input_sockets = output_node->input_sockets();
bNodeSocket *input_socket = nullptr;
float intensity = 1.0f;
BogdanNagirniak marked this conversation as resolved Outdated
if (scene_delegate_.shading_settings.use_scene_world) {
   <previous code>
}
else {
  <new code>
}
``` if (scene_delegate_.shading_settings.use_scene_world) { <previous code> } else { <new code> } ```
float exposure = world->exposure;
pxr::GfVec3f color(1.0f, 1.0f, 1.0f);
pxr::SdfAssetPath texture_file;
for (auto socket : input_sockets) {
if (STREQ(socket->name, "Surface")) {
input_socket = socket;
break;
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);
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;
if (input_node->type != SH_NODE_BACKGROUND) {
return;
}
bNode *input_node = link->fromnode;
if (input_node->type != SH_NODE_BACKGROUND) {
return;
}
bNodeSocket color_input = input_node->input_by_identifier("Color");
bNodeSocket strength_input = input_node->input_by_identifier("Strength");
bNodeSocket color_input = input_node->input_by_identifier("Color");
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 *strength = strength_input.default_value_typed<float>();
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;
if (color_input_node->type == SH_NODE_TEX_IMAGE) {
NodeTexImage *tex = static_cast<NodeTexImage *>(color_input_node->storage);
Image *image = (Image *)color_input_node->id;
if (image) {
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);
if (!color_input.directly_linked_links().is_empty()) {
bNode *color_input_node = color_input.directly_linked_links()[0]->fromnode;
if (color_input_node->type == SH_NODE_TEX_IMAGE) {
NodeTexImage *tex = static_cast<NodeTexImage *>(color_input_node->storage);
Image *image = (Image *)color_input_node->id;
if (image) {
std::string image_path = cache_or_get_image_file(
image, scene_delegate_->context, &tex->iuser);
if (!image_path.empty()) {
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 {
float intensity = 1.0f;
if (LOOK_DEV_STUDIO_LIGHT_ENABLED(scene_delegate_->view3d)) {
if (scene_delegate_->view3d && !scene_delegate_->shading_settings.use_scene_world) {
BogdanNagirniak marked this conversation as resolved Outdated

thif if can be removed

thif `if` can be removed
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()