BLEN-335: Export environment light #1

Merged
Bogdan Nagirniak merged 13 commits from BLEN-335 into hydra-render 2023-02-17 14:46:46 +01:00
5 changed files with 240 additions and 8 deletions
Showing only changes of commit f095e0683e - Show all commits

View File

@ -20,6 +20,7 @@ set(INC
../../../../intern/guardedalloc
../../makesdna
../../makesrna
../../nodes
../../blenlib
../../depsgraph
../../blenkernel
@ -66,6 +67,8 @@ set(SRC
sceneDelegate/scene.cc
sceneDelegate/material.h
sceneDelegate/material.cc
sceneDelegate/world.h
sceneDelegate/world.cc
)
set(LIB

View File

@ -50,10 +50,32 @@ void BlenderSceneDelegate::update_material(Material *material)
}
}
void BlenderSceneDelegate::add_world(View3DShading &view3DShading, World &world)
{
SdfPath world_light_id = world_id(&view3DShading);
LOG(INFO) << "Add world: " << world_light_id;
if (world_data.shading != &view3DShading || world_data.world != &world) {
world_data = WorldData(&view3DShading, &world);
GetRenderIndex().InsertSprim(HdPrimTypeTokens->domeLight, this, world_light_id);
}
else {
world_data.update_world();
DagerD marked this conversation as resolved
Review

world_data = nullptr;

`world_data = nullptr;`
GetRenderIndex().GetChangeTracker().MarkSprimDirty(world_light_id, HdLight::AllDirty);
DagerD marked this conversation as resolved
Review

if (world_data) ...

if (world_data) ...
}
}
bool BlenderSceneDelegate::GetVisible(SdfPath const &id)
{
DagerD marked this conversation as resolved
Review
if (!world_data) {
world_data = make_unique....
insertSprim
}
else {
world_data = make_unique....
changetracker
}
``` if (!world_data) { world_data = make_unique.... insertSprim } else { world_data = make_unique.... changetracker } ```
ObjectData *obj_data = object_data(id);
LOG(INFO) << "GetVisible: " << id.GetAsString() << " " << obj_data->is_visible();
LOG(INFO) << "GetVisible: " << id.GetAsString();
HdRenderIndex &index = GetRenderIndex();
if (index.GetSprim(HdPrimTypeTokens->domeLight, id)) {
return true;
}
return obj_data->is_visible();
}
@ -207,6 +229,15 @@ SdfPath BlenderSceneDelegate::material_id(Material *material)
return GetDelegateID().AppendElementString(str);
}
SdfPath BlenderSceneDelegate::world_id(View3DShading *view3DShading)
{
/* Making id of material in form like M_<pointer in 16 hex digits format>. Example:
* W_000002074e812088 */
char str[32];
snprintf(str, 32, "W_%016llx", (uint64_t)view3DShading);
return GetDelegateID().AppendElementString(str);
}
bool BlenderSceneDelegate::supported_object(Object *object)
BogdanNagirniak marked this conversation as resolved
Review

return GetDelegateID().AppendElementString("World");

return GetDelegateID().AppendElementString("World");
{
return object->type == OB_MESH ||
@ -229,6 +260,10 @@ void BlenderSceneDelegate::Populate(BL::Depsgraph &b_deps, View3D *v3d)
/* Export initial objects */
update_collection();
World *world = (World *)b_depsgraph->scene().world().ptr.data;
add_world(view3d->shading, *world);
is_populated = true;
return;
}
@ -277,6 +312,12 @@ void BlenderSceneDelegate::Populate(BL::Depsgraph &b_deps, View3D *v3d)
}
continue;
}
if (id.is_a(&RNA_World)) {
World *world = (World *)b_depsgraph->scene().world().ptr.data;
add_world(view3d->shading, *world);
continue;
}
}
if (do_update_collection) {
@ -397,6 +438,12 @@ GfMatrix4d BlenderSceneDelegate::GetTransform(SdfPath const& id)
{
LOG(INFO) << "GetTransform: " << id.GetAsString();
HdRenderIndex &index = GetRenderIndex();
if (index.GetSprim(HdPrimTypeTokens->domeLight, id)) {
return GfMatrix4d().SetIdentity();
}
return objects[id].transform();
}
@ -404,6 +451,16 @@ VtValue BlenderSceneDelegate::GetLightParamValue(SdfPath const& id, TfToken cons
{
LOG(INFO) << "GetLightParamValue: " << id.GetAsString() << " [" << key.GetString() << "]";
VtValue ret;
HdRenderIndex &index = GetRenderIndex();
if (index.GetSprim(HdPrimTypeTokens->domeLight, id)) {
BogdanNagirniak marked this conversation as resolved
Review

add TODO comment like: implement this check via render settings

add TODO comment like: implement this check via render settings
if (world_data.has_data(key)) {
ret = world_data.get_data(key);
}
}
BogdanNagirniak marked this conversation as resolved
Review

move this to WorldData::transform()

move this to WorldData::transform()
else {
ObjectData *obj_data = object_data(id);
DagerD marked this conversation as resolved
Review

revert this block

revert this block
if (obj_data) {
if (obj_data->has_data(key)) {
@ -414,6 +471,7 @@ VtValue BlenderSceneDelegate::GetLightParamValue(SdfPath const& id, TfToken cons
ret = 1.0f;
}
}
}
BogdanNagirniak marked this conversation as resolved
Review

else if (id == world_id()) {....}

else if (id == world_id()) {....}
return ret;
}

View File

@ -15,6 +15,7 @@
#include "RNA_blender_cpp.h"
#include "object.h"
#include "world.h"
using namespace pxr;
@ -49,6 +50,8 @@ private:
void update_material(Material *material);
void update_collection();
void update_visibility();
void add_world(View3DShading &view3DShading, World &world);
SdfPath world_id(View3DShading *view3DShading);
private:
BL::Depsgraph *b_depsgraph;
@ -56,6 +59,7 @@ private:
bool is_populated;
ObjectDataMap objects;
MaterialDataMap materials;
WorldData world_data;
};
BogdanNagirniak marked this conversation as resolved
Review

make is as unique_ptr, implement add/remove world

make is as unique_ptr, implement add/remove world
} // namespace blender::render::hydra

View File

@ -0,0 +1,116 @@
/* SPDX-License-Identifier: Apache-2.0
* Copyright 2011-2022 Blender Foundation */
#include <pxr/base/vt/array.h>
#include <pxr/base/gf/vec2f.h>
#include <pxr/imaging/hd/light.h>
#include <pxr/imaging/hd/tokens.h>
#include <pxr/usd/usdLux/tokens.h>
#include "DNA_node_types.h"
#include "BKE_node.h"
#include "BKE_node_runtime.hh"
#include "NOD_shader.h"
#include "world.h"
/* TODO : add tftoken "transparency" and RPR specific? */
using namespace pxr;
namespace blender::render::hydra {
WorldData::WorldData()
: shading(nullptr),
world(nullptr)
{
}
WorldData::WorldData(View3DShading *shading, World *world)
: shading(shading),
world(world)
{
set_as_world();
}
std::string WorldData::name()
{
BogdanNagirniak marked this conversation as resolved
Review

(World *world, bContext *context)

`(World *world, bContext *context)`
return "";
}
int WorldData::type()
{
//return object->type;
return 0;
}
TfToken WorldData::prim_type()
{
return HdPrimTypeTokens->domeLight;
}
GfMatrix4d WorldData::transform()
{
return GfMatrix4d();
}
VtValue &WorldData::get_data(TfToken const &key)
{
return data[key];
}
bool WorldData::has_data(TfToken const &key)
{
return data.find(key) != data.end();
}
void WorldData::update_world()
{
set_as_world();
}
void WorldData::set_as_world()
{
if (world->use_nodes) {
bNode *output_node = ntreeShaderOutputNode(world->nodetree, SHD_OUTPUT_ALL);
bNodeSocket input_socket = output_node->input_by_identifier("Surface");
bNodeLink const *link = input_socket.directly_linked_links()[0];
bNode *input_node = link->fromnode;
bNodeSocket color_input = input_node->input_by_identifier("Color");
bNodeSocket strength_input = input_node->input_by_identifier("Strength");
float const *color = color_input.default_value_typed<float>();
float const *strength = strength_input.default_value_typed<float>();
data[HdLightTokens->intensity] = strength[1];
BogdanNagirniak marked this conversation as resolved
Review

move to constructor

move to constructor
data[HdLightTokens->exposure] = 1.0f;
data[HdLightTokens->color] = GfVec3f(color[0], color[1], color[2]);
//blender::Span<bNode *> world_nodes = world->nodetree->nodes_by_type("ShaderNodeOutputWorld");
}
else {
DagerD marked this conversation as resolved
Review

string const &

`string const &`
data[HdLightTokens->intensity] = 1.0f;
data[HdLightTokens->exposure] = world->exposure;
data[HdLightTokens->color] = GfVec3f(
world->horr,
world->horg,
world->horb
);
}
}
void WorldData::set_as_shading()
{
data[HdLightTokens->intensity] = 1.0f;
data[HdLightTokens->exposure] = 1.0f;
data[HdLightTokens->color] = GfVec3f(
shading->single_color[0],
shading->single_color[1],
shading->single_color[2]
);
}
} // namespace blender::render::hydra

View File

@ -0,0 +1,51 @@
/* SPDX-License-Identifier: Apache-2.0
* Copyright 2011-2022 Blender Foundation */
#pragma once
#include <map>
#include <pxr/base/gf/matrix4d.h>
#include <pxr/usd/sdf/path.h>
#include <pxr/base/vt/value.h>
#include "pxr/base/tf/staticTokens.h"
#include "DNA_view3d_types.h"
#include "DNA_world_types.h"
namespace blender::render::hydra {
class WorldData {
public:
WorldData();
WorldData(View3DShading *shading, World *world);
std::string name();
int type();
pxr::TfToken prim_type();
pxr::GfMatrix4d transform();
pxr::VtValue &get_data(pxr::TfToken const &key);
template<class T>
const T &get_data(pxr::TfToken const &key);
bool has_data(pxr::TfToken const &key);
void update_world();
View3DShading *shading;
BogdanNagirniak marked this conversation as resolved
Review

no need update_world() just recreate it in scene delegate

no need update_world() just recreate it in scene delegate
World *world;
private:
std::map<pxr::TfToken, pxr::VtValue> data;
void set_as_world();
void set_as_shading();
};
template<class T>
const T &WorldData::get_data(pxr::TfToken const &key)
BogdanNagirniak marked this conversation as resolved
Review

remove unused methods

remove unused methods
{
return get_data(key).Get<T>();
}
} // namespace blender::render::hydra