Skip to content

Commit

Permalink
Sky Composer
Browse files Browse the repository at this point in the history
  • Loading branch information
iaomw committed May 31, 2024
1 parent 6190388 commit 56ac178
Show file tree
Hide file tree
Showing 23 changed files with 1,448 additions and 258 deletions.
2 changes: 1 addition & 1 deletion ui/zenoedit/launch/serialize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ using namespace JsonHelper;

QSet<QString> lightCameraNodes({
"CameraEval", "CameraNode", "CihouMayaCameraFov", "ExtractCameraData", "GetAlembicCamera","MakeCamera",
"LightNode", "BindLight", "ProceduralSky", "HDRSky",
"LightNode", "BindLight", "ProceduralSky", "HDRSky", "SkyComposer"
});

std::set<std::string> matNodeNames = {"ShaderFinalize", "ShaderVolume", "ShaderVolumeHomogeneous"};
Expand Down
15 changes: 15 additions & 0 deletions zeno/include/zeno/types/LightObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@
#include <zeno/core/IObject.h>
#include <zeno/utils/vec.h>

#else

#ifndef vec3f
#define vec3f vec3
#endif

#endif

namespace zeno {
Expand All @@ -23,6 +29,15 @@ namespace zeno {
LightConfigDoubleside = 2u
};

struct DistantLightData {
vec3f direction;
float angle;
vec3f color;
float intensity;

DistantLightData() = default;
};

#ifndef __CUDACC_RTC__

struct LightData {
Expand Down
27 changes: 27 additions & 0 deletions zeno/include/zeno/utils/pfm.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#pragma once

#include <vector>
#include <string>
#include <cstring>

#include <zeno/utils/format.h>
#include <zeno/utils/fileio.h>

namespace zeno {

static void write_pfm(const char* path, int w, int h, const float *pixel, bool mono=false) {
std::string header = zeno::format("PF\n{} {}\n-1.0\n", w, h);
char channel = 3;

if (mono) {
header = zeno::format("Pf\n{} {}\n-1.0\n", w, h);
channel = 1;
}

std::vector<char> data(header.size() + w * h * sizeof(float) * channel);
memcpy(data.data(), header.data(), header.size());
memcpy(data.data() + header.size(), pixel, w * h * sizeof(float) * channel);
zeno::file_put_binary(data, path);
}

}
2 changes: 1 addition & 1 deletion zeno/src/extra/GlobalComm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ namespace zeno {
std::vector<std::filesystem::path> cachepath(3);
std::unordered_set<std::string> lightCameraNodes({
"CameraEval", "CameraNode", "CihouMayaCameraFov", "ExtractCameraData", "GetAlembicCamera","MakeCamera",
"LightNode", "BindLight", "ProceduralSky", "HDRSky",
"LightNode", "BindLight", "ProceduralSky", "HDRSky", "SkyComposer"
});
std::set<std::string> matNodeNames = {"ShaderFinalize", "ShaderVolume", "ShaderVolumeHomogeneous"};

Expand Down
234 changes: 234 additions & 0 deletions zeno/src/nodes/ProcedrualSkyNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
#include <zeno/types/PrimitiveObject.h>
#include <zeno/types/UserData.h>
#include <zeno/utils/fileio.h>
#include <zeno/ListObject.h>
#include <tinygltf/json.hpp>

#include <zeno/utils/eulerangle.h>
#include <zeno/types/LightObject.h>


namespace zeno {
struct ProceduralSky : INode {
Expand Down Expand Up @@ -78,6 +84,234 @@ ZENDEFNODE(HDRSky, {
{"shader"},
});

struct DistantLightWrapper : IObject{
DistantLightData data;
};

struct DistantLight : INode {

virtual void apply() override {
auto dir2 = get_input2<zeno::vec2f>("Lat-Lon");
// dir2[0] = fmod(dir2[0], 180.f);
// dir2[1] = fmod(dir2[1], 180.f);
dir2 *= M_PIf / 180.f;

zeno::vec3f dir3;
dir3[1] = std::sin(dir2[0]);

dir3[2] = std::cos(dir2[0]) * std::cos(dir2[1]);
dir3[0] = std::cos(dir2[0]) * std::sin(dir2[1]);

//dir3 = zeno::normalize(dir3);

auto angleExtent = get_input2<float>("angleExtent");
angleExtent = zeno::clamp(angleExtent, 0.0f, 60.0f);

auto color = get_input2<zeno::vec3f>("color");
auto intensity = get_input2<float>("intensity");
intensity = fmaxf(0.0, intensity);

auto result = std::make_shared<DistantLightWrapper>();
result->data.direction = dir3;
result->data.angle = angleExtent;
result->data.color = color;
result->data.intensity = intensity;
set_output2("out", std::move(result) );
}
};

ZENDEFNODE(DistantLight, {
{
{"vec2f", "Lat-Lon", "45, 90"},
{"float", "angleExtent", "0.5"},
{"colorvec3f", "color", "1,1,1"},
{"float", "intensity", "1"}
},
{
{"out"},
},
{
},
{"shader"},
});

struct PortalLight : INode {
virtual void apply() override {

auto pos = get_input2<zeno::vec3f>("pos");
auto scale = get_input2<zeno::vec2f>("scale");
auto rotate = get_input2<zeno::vec3f>("rotate");
auto size = get_input2<int>("size");
size = std::max(size, 180);

scale = 0.5f * abs(scale);

auto order = get_input2<std::string>("EulerRotationOrder:");
auto orderTyped = magic_enum::enum_cast<EulerAngle::RotationOrder>(order).value_or(EulerAngle::RotationOrder::XYZ);

auto measure = get_input2<std::string>("EulerAngleMeasure:");
auto measureTyped = magic_enum::enum_cast<EulerAngle::Measure>(measure).value_or(EulerAngle::Measure::Radians);

glm::vec3 eularAngleXYZ = glm::vec3(rotate[0], rotate[1], rotate[2]);
glm::mat4 rotation = EulerAngle::rotate(orderTyped, measureTyped, eularAngleXYZ);

glm::mat4 transform(1.0f);

transform = glm::translate(transform, glm::vec3(pos[0], pos[1], pos[2]));
transform = transform * rotation;
transform = glm::scale(transform, glm::vec3(scale[0], 0.5 * (scale[0] + scale[1]), scale[1]));

auto prim = std::make_shared<zeno::PrimitiveObject>();
prim->verts->resize(8);

prim->verts[0] = zeno::vec3f(-1, 0, -1);
prim->verts[1] = zeno::vec3f(+1, 0, -1);
prim->verts[2] = zeno::vec3f(+1, 0, +1);
prim->verts[3] = zeno::vec3f(-1, 0, +1);

prim->verts[4] = zeno::vec3f(0, 0, 0);
prim->verts[5] = zeno::vec3f(0.5, 0, 0);
prim->verts[6] = zeno::vec3f(0, 0.5, 0);
prim->verts[7] = zeno::vec3f(0, 0, 0.5);

for (size_t i=0; i<prim->verts->size(); ++i) {
auto& ele = prim->verts[i];
auto ttt = transform * glm::vec4(ele[0], ele[1], ele[2], 1.0f);
prim->verts[i] = zeno::vec3f(ttt.x, ttt.y, ttt.z);
}

//prim->lines.attrs.clear();
prim->lines->resize(8);
prim->lines[0] = {0, 1};
prim->lines[1] = {1, 2};
prim->lines[2] = {2, 3};
prim->lines[3] = {3, 0};

prim->lines[4] = {4, 5};
prim->lines[5] = {4, 6};
prim->lines[6] = {4, 7};

auto& color = prim->verts.add_attr<zeno::vec3f>("clr");
color.resize(8);
color[0] = {1,1,1};
color[1] = {1,1,1};
color[2] = {1,1,1};
color[3] = {1,1,1};

color[4] = {1, 1, 1};
color[5] = {1, 0, 0};
color[6] = {0, 1, 0};
color[7] = {0, 0, 1};
//prim->lines.update();
prim->userData().set2("size", size);
set_output2("out", std::move(prim));
}
};

ZENDEFNODE(PortalLight, {
{
{"vec3f", "pos", "0,0,0"},
{"vec2f", "scale", "1, 1"},
{"vec3f", "rotate", "0,0,0"},
{"int", "size", "180"}
},
{
{"out"},
},
{
{"enum " + EulerAngle::RotationOrderListString(), "EulerRotationOrder", "XYZ"},
{"enum " + EulerAngle::MeasureListString(), "EulerAngleMeasure", "Degree"}
},
{"shader"},
});

struct SkyComposer : INode {
virtual void apply() override {

auto prim = std::make_shared<zeno::PrimitiveObject>();

if (has_input("dlights")) {
auto dlights = get_input<ListObject>("dlights")->get<DistantLightWrapper>();
if (dlights.empty()) {
throw zeno::makeError("Bad input for dlights");
}

prim->verts->resize(dlights.size());
auto& attr_rad = prim->verts.add_attr<float>("rad");
auto& attr_angle = prim->verts.add_attr<float>("angle");
auto& attr_color = prim->verts.add_attr<zeno::vec3f>("color");
auto& attr_inten = prim->verts.add_attr<float>("inten");

unsigned i = 0;
for (const auto& dlight : dlights) {

prim->verts[i] = dlight->data.direction;
attr_rad[i] = 0.0f;
attr_angle[i] = dlight->data.angle;
attr_color[i] = dlight->data.color;
attr_inten[i] = dlight->data.intensity;

++i;
}
}

if (has_input("portals")) {
auto portals = get_input<ListObject>("portals")->get<zeno::PrimitiveObject>();
if (portals.empty()) {
throw zeno::makeError("Bad input for portals");
}

using json = nlohmann::json;
std::vector<zeno::vec3f> raw(4 * portals.size());
std::vector<int> psizes(portals.size());

for (size_t i=0; i<portals.size(); ++i) {
auto &rect = portals[i];

auto p0 = rect->verts[0];
auto p1 = rect->verts[1];
auto p2 = rect->verts[2];
auto p3 = rect->verts[3];

/* p0 --- p1 */
/* --------- */
/* p3 --- p2 */

raw[4 * i + 0] = p0;
raw[4 * i + 1] = p1;
raw[4 * i + 2] = p2;
raw[4 * i + 3] = p3;

auto psize = rect->userData().get2<int>("size");
psizes[i] = psize;
}

json aux(raw);
prim->userData().set2("portals", std::move(aux.dump()));
prim->userData().set2("psizes", json(psizes).dump());
}

prim->userData().set2("SkyComposer", std::move(1));
prim->userData().set2("isRealTimeObject", std::move(1));
set_output2("out", std::move(prim));
}
};

ZENDEFNODE(SkyComposer, {
{

{"list", "dlights"},
{"list", "portals"}
},
{
{"out"},
},
{
{"enum SphereUnbounded", "proxy", "SphereUnbounded"},
},
{"shader"},
});

vec3f colorTemperatureToRGB(float temperatureInKelvins)
{
vec3f retColor;
Expand Down
2 changes: 0 additions & 2 deletions zenovis/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ file(GLOB_RECURSE source CONFIGURE_DEPENDS include/*.h src/*.cpp)
file(GLOB_RECURSE glad_source CONFIGURE_DEPENDS glad/include/*.h glad/src/*.c)
file(GLOB_RECURSE stbi_source CONFIGURE_DEPENDS stbi/include/*.h stbi/src/*.cpp stbi/src/*.c)

#OPTION(OPTIX_USE_20XX "turn on if on a 20xx gpu" OFF)

add_library(zenovis OBJECT ${source} ${glad_source} ${stbi_source})
target_link_libraries(zenovis PRIVATE ${CMAKE_DL_LIBS})
target_link_libraries(zenovis PUBLIC zeno)
Expand Down
2 changes: 1 addition & 1 deletion zenovis/include/zenovis/RenderEngine.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ struct Scene;
struct RenderEngine {
virtual void draw(bool record) = 0;
virtual void update() = 0;
virtual void cleanupOptix() = 0;
virtual void cleanupAssets() = 0;

virtual ~RenderEngine() = default;
};
Expand Down
2 changes: 1 addition & 1 deletion zenovis/src/Scene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ void Scene::cleanUpScene()
RenderEngine* pEngine = renderMan->getEngine();
if (pEngine) {
pEngine->update();
pEngine->cleanupOptix();
pEngine->cleanupAssets();
}
}

Expand Down
2 changes: 1 addition & 1 deletion zenovis/src/bate/RenderEngineBate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ struct RenderEngineBate : RenderEngine {
}
}

void cleanupOptix() override {
void cleanupAssets() override {

}
};
Expand Down
Loading

0 comments on commit 56ac178

Please sign in to comment.