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 98 additions and 57 deletions
Showing only changes of commit 3a348ff5c3 - Show all commits

View File

@ -3,8 +3,10 @@
#include <pxr/imaging/hd/light.h> #include <pxr/imaging/hd/light.h>
#include <pxr/imaging/hd/material.h> #include <pxr/imaging/hd/material.h>
#include <pxr/imaging/hd/renderDelegate.h>
#include <pxr/usd/usdLux/tokens.h> #include <pxr/usd/usdLux/tokens.h>
#include <pxr/imaging/hdSt/tokens.h> #include <pxr/imaging/hdSt/tokens.h>
#include <pxr/base/gf/rotation.h>
#include "glog/logging.h" #include "glog/logging.h"
@ -449,7 +451,15 @@ GfMatrix4d BlenderSceneDelegate::GetTransform(SdfPath const& id)
HdRenderIndex &index = GetRenderIndex(); HdRenderIndex &index = GetRenderIndex();
if (index.GetSprim(HdPrimTypeTokens->domeLight, id)) { if (index.GetSprim(HdPrimTypeTokens->domeLight, id)) {
return GfMatrix4d().SetIdentity(); GfMatrix4d transform = world_data.transform();
if (world_data.has_data(UsdLuxTokens->orientToStageUpAxis)) {
transform *= GfMatrix4d(GfRotation(GfVec3d(1.0, 0.0, 0.0), -90), GfVec3d());
}
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 (index.GetRenderDelegate()->GetRendererDisplayName() == "RPR") {
transform *= GfMatrix4d(GfRotation(GfVec3d(1.0, 0.0, 0.0), -180), GfVec3d());
transform *= GfMatrix4d(GfRotation(GfVec3d(0.0, 0.0, 1.0), 90.0), GfVec3d());
}
BogdanNagirniak marked this conversation as resolved
Review

move this to WorldData::transform()

move this to WorldData::transform()
return transform;
} }
return objects[id].transform(); return objects[id].transform();

View File

@ -17,12 +17,10 @@
#include "BKE_node.h" #include "BKE_node.h"
#include "BKE_node_runtime.hh" #include "BKE_node_runtime.hh"
#include "BKE_image.h" #include "BKE_image.h"
#include "BKE_image_save.h"
#include "BKE_appdir.h"
#include "NOD_shader.h" #include "NOD_shader.h"
#include "BLI_path_util.h"
#include "world.h" #include "world.h"
#include "../utils.h"
/* TODO : add custom tftoken "transparency"? */ /* TODO : add custom tftoken "transparency"? */
@ -32,15 +30,16 @@ using namespace std;
namespace blender::render::hydra { namespace blender::render::hydra {
WorldData::WorldData() WorldData::WorldData()
: shading(nullptr), : b_context(nullptr),
shading(nullptr),
world(nullptr) world(nullptr)
{ {
} }
BogdanNagirniak marked this conversation as resolved
Review

(World *world, bContext *context)

`(World *world, bContext *context)`
WorldData::WorldData(View3DShading *shading, World *world, BL::Context *b_context) WorldData::WorldData(View3DShading *shading, World *world, BL::Context *b_context)
: shading(shading), : b_context(b_context),
world(world), shading(shading),
b_context(b_context) world(world)
{ {
set_as_world(); set_as_world();
} }
@ -63,7 +62,7 @@ TfToken WorldData::prim_type()
GfMatrix4d WorldData::transform() GfMatrix4d WorldData::transform()
{ {
return GfMatrix4d(); return GfMatrix4d().SetIdentity();
} }
VtValue &WorldData::get_data(TfToken const &key) VtValue &WorldData::get_data(TfToken const &key)
@ -85,8 +84,9 @@ void WorldData::set_as_world()
{ {
data.clear(); data.clear();
if (world->use_nodes) { data[UsdLuxTokens->orientToStageUpAxis] = true;
BogdanNagirniak marked this conversation as resolved
Review

move to constructor

move to constructor
if (world->use_nodes) {
bNode *output_node = ntreeShaderOutputNode(world->nodetree, SHD_OUTPUT_ALL); bNode *output_node = ntreeShaderOutputNode(world->nodetree, SHD_OUTPUT_ALL);
bNodeSocket input_socket = output_node->input_by_identifier("Surface"); bNodeSocket input_socket = output_node->input_by_identifier("Surface");
bNodeLink const *link = input_socket.directly_linked_links()[0]; bNodeLink const *link = input_socket.directly_linked_links()[0];
@ -105,44 +105,19 @@ void WorldData::set_as_world()
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 *ima = (Image *)color_input_node->id; Image *image = (Image *)color_input_node->id;
if (image) {
Main *bmain = CTX_data_main((bContext *)b_context->ptr.data);
Scene *scene = CTX_data_scene((bContext *)b_context->ptr.data);
if (ima) {
ReportList reports; ReportList reports;
ImageSaveOptions opts; ImageSaveOptions opts;
Main *bmain = CTX_data_main((bContext *)b_context->ptr.data);
if (BKE_image_save_options_init(&opts,
bmain,
CTX_data_scene((bContext *)b_context->ptr.data),
ima,
&tex->iuser,
false,
false)) {
char tempfile[FILE_MAX];
string image_name;
if (ima->source == IMA_SRC_GENERATED) {
image_name.append(ima->id.name + 2).append(".png");
}
else {
image_name = ima->filepath == NULL ?
std::filesystem::path(ima->filepath).filename().string() :
ima->id.name + 2;
}
BLI_path_join(tempfile,
sizeof(tempfile),
BKE_tempdir_session(),
image_name.c_str());
STRNCPY(opts.filepath, tempfile);
opts.im_format.imtype = R_IMF_IMTYPE_PNG; opts.im_format.imtype = R_IMF_IMTYPE_PNG;
opts.save_copy = true;
if (BKE_image_save(&reports, bmain, ima, &tex->iuser, &opts)) { string cached_image_path = cache_image(bmain, scene, image, &tex->iuser, &opts, &reports);
data[HdLightTokens->textureFile] = SdfAssetPath(tempfile, tempfile); if (!cached_image_path.empty()) {
} data[HdLightTokens->textureFile] = SdfAssetPath(cached_image_path, cached_image_path);
BKE_image_save_options_free(&opts);
} }
} }
} }

View File

@ -33,17 +33,15 @@ public:
bool has_data(pxr::TfToken const &key); bool has_data(pxr::TfToken const &key);
void update_world(); void update_world();
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
BL::Context *b_context;
View3DShading *shading; View3DShading *shading;
World *world; World *world;
BL::Context *b_context;
private: private:
std::map<pxr::TfToken, pxr::VtValue> data; std::map<pxr::TfToken, pxr::VtValue> data;
void set_as_world(); void set_as_world();
void set_as_shading(); void set_as_shading();
std::string get_image_filepath(const bNode *tex_node);
}; };
BogdanNagirniak marked this conversation as resolved
Review

remove unused methods

remove unused methods
template<class T> template<class T>

View File

@ -2,9 +2,18 @@
* Copyright 2011-2022 Blender Foundation */ * Copyright 2011-2022 Blender Foundation */
#include <sstream> #include <sstream>
#include <filesystem>
#include <pxr/base/tf/stringUtils.h>
#include "BKE_appdir.h"
#include "BKE_image_save.h"
#include "BLI_string.h"
#include "BLI_path_util.h"
#include "utils.h" #include "utils.h"
using namespace pxr;
using namespace std; using namespace std;
DagerD marked this conversation as resolved
Review

remove this

remove this
namespace blender::render::hydra { namespace blender::render::hydra {
@ -26,11 +35,51 @@ string formatDuration(chrono::milliseconds millisecs)
ss << to_string(m / 1min) << ":"; ss << to_string(m / 1min) << ":";
if (s < 10s) if (s < 10s)
ss << "0"; ss << "0";
ss << to_string(s/1s) << ":"; ss << to_string(s / 1s) << ":";
if (millisecs < 10ms) if (millisecs < 10ms)
ss << "0"; ss << "0";
ss << to_string(millisecs/1ms/10); ss << to_string(millisecs / 1ms / 10);
return ss.str(); return ss.str();
} }
DagerD marked this conversation as resolved
Review

add {...}

add {...}
string cache_image(Main *bmain,
Scene *scene,
Image *image,
ImageUser *iuser,
ImageSaveOptions *opts,
ReportList *reports)
{
const string default_format = ".png";
char tempfile[FILE_MAX];
if (!BKE_image_save_options_init(opts, bmain, scene, image, iuser, true, false)) {
BKE_image_save_options_free(opts);
return "";
}
string image_name;
if (image->source == IMA_SRC_GENERATED) {
image_name = TfMakeValidIdentifier(image_name.append(image->id.name + 2));
}
else {
image_name = image->filepath == NULL ? image->filepath : image->id.name + 2;
image_name = std::filesystem::path(image_name).filename().replace_extension().string();
image_name = TfMakeValidIdentifier(image_name);
}
image_name.append(default_format);
BLI_path_join(tempfile, sizeof(tempfile), BKE_tempdir_session(), image_name.c_str());
STRNCPY(opts->filepath, tempfile);
if (BKE_image_save(reports, bmain, image, iuser, opts)) {
BKE_image_save_options_free(opts);
return tempfile;
};
BKE_image_save_options_free(opts);
return "";
}
} // namespace blender::render::hydra } // namespace blender::render::hydra

View File

@ -6,8 +6,17 @@
#include <chrono> #include <chrono>
#include <string> #include <string>
#include "BKE_image.h"
#include "BKE_image_save.h"
namespace blender::render::hydra { namespace blender::render::hydra {
std::string formatDuration(std::chrono::milliseconds secs); std::string formatDuration(std::chrono::milliseconds secs);
std::string cache_image(Main *bmain,
Scene *scene,
Image *image,
ImageUser *iuser,
ImageSaveOptions *opts,
ReportList *reports);
} // namespace blender::render::hydra } // namespace blender::render::hydra