Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mesh light, Light texture #1490

Merged
merged 15 commits into from
Nov 1, 2023
91 changes: 67 additions & 24 deletions projects/FBX/MayaCamera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <zeno/utils/log.h>

#include <zeno/zeno.h>
#include <zeno/utils/eulerangle.h>
#include <zeno/utils/logger.h>
#include <zeno/extra/GlobalState.h>
#include <zeno/types/NumericObject.h>
Expand All @@ -17,10 +18,11 @@

#include "assimp/scene.h"

#include "magic_enum.hpp"
#include "Definition.h"
#include "json.hpp"

#include <memory>

#include <glm/vec4.hpp>
#include <glm/mat4x4.hpp>
#include <glm/glm.hpp>
Expand Down Expand Up @@ -243,9 +245,10 @@ ZENO_DEFNODE(CameraEval)({
{"FBX"},
});


struct LightNode : INode {
virtual void apply() override {
auto isL = get_input2<int>("islight");
auto isL = true; //get_input2<int>("islight");
auto inverdir = get_input2<int>("invertdir");
auto position = get_input2<zeno::vec3f>("position");
auto scale = get_input2<zeno::vec3f>("scale");
Expand All @@ -266,20 +269,41 @@ struct LightNode : INode {

intensity *= scaler;

std::string type = get_input2<std::string>(lightTypeKey);
auto typeEnum = magic_enum::enum_cast<LightType>(type).value_or(LightType::Diffuse);
auto typeOrder = magic_enum::enum_integer(typeEnum);

std::string shapeString = get_input2<std::string>(lightShapeKey);
auto shapeEnum = magic_enum::enum_cast<LightShape>(shapeString).value_or(LightShape::Plane);
auto shapeOrder = magic_enum::enum_integer(shapeEnum);

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

if (has_input("prim")) {
auto mesh = get_input<PrimitiveObject>("prim");

if (mesh->size() > 0) {
prim = mesh;
shapeEnum = LightShape::TriangleMesh;
shapeOrder = magic_enum::enum_integer(shapeEnum);
}
} else {

auto &verts = prim->verts;
auto &tris = prim->tris;

//if(shape == "Plane"){
auto start_point = zeno::vec3f(0.5, 0, 0.5);
float rm = 1.0f;
float cm = 1.0f;

glm::mat4 rotation = glm::mat4(1.0f);
glm::vec3 euler = glm::vec3(rotate[0], rotate[1], rotate[2]);
rotation = glm::rotate(rotation, euler.z, glm::vec3(0.0f, 0.0f, 1.0f));
rotation = glm::rotate(rotation, euler.y, glm::vec3(0.0f, 1.0f, 0.0f));
rotation = glm::rotate(rotation, euler.x, glm::vec3(1.0f, 0.0f, 0.0f));
auto order = get_input2<std::string>("EulerRotationOrder:");
auto orderTyped = magic_enum::enum_cast<EulerAngle::RotationOrder>(order).value_or(EulerAngle::RotationOrder::YXZ);

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);

// Plane Verts
for(int i=0; i<=1; i++){
Expand All @@ -305,7 +329,10 @@ struct LightNode : INode {
// Plane Indices
tris.emplace_back(zeno::vec3i(0, 3, 1));
tris.emplace_back(zeno::vec3i(3, 0, 2));
//}
}

auto &verts = prim->verts;
auto &tris = prim->tris;

auto &clr = prim->verts.add_attr<zeno::vec3f>("clr");
auto c = color * intensity;
Expand Down Expand Up @@ -340,26 +367,33 @@ struct LightNode : INode {
prim->userData().set2("color", std::move(color));
prim->userData().set2("intensity", std::move(intensity));

auto maxDistance = get_input2<float>("maxDistance");
prim->userData().set2("maxDistance", std::move(maxDistance));

auto visible = get_input2<int>("visible");
auto doubleside = get_input2<int>("doubleside");

std::string type = get_input2<std::string>(lightTypeKey);
auto typeEnum = magic_enum::enum_cast<LightType>(type).value_or(LightType::Diffuse);
auto typeOrder = magic_enum::enum_integer(typeEnum);

std::string shape = get_input2<std::string>(lightShapeKey);
auto shapeEnum = magic_enum::enum_cast<LightShape>(shape).value_or(LightShape::Plane);
auto shapeOrder = magic_enum::enum_integer(shapeEnum);
if (has_input2<std::string>("profile")) {
auto profile = get_input2<std::string>("profile");
prim->userData().set2("lightProfile", std::move(profile));
}
if (has_input2<std::string>("texturePath")) {
auto texture = get_input2<std::string>("texturePath");
prim->userData().set2("lightTexture", std::move(texture));

auto profile = get_input2<std::string>("profile");
auto gamma = get_input2<float>("textureGamma");
prim->userData().set2("lightGamma", std::move(gamma));
}

prim->userData().set2("type", std::move(typeOrder));
prim->userData().set2("shape", std::move(shapeOrder));

prim->userData().set2("visible", std::move(visible));
prim->userData().set2("doubleside", std::move(doubleside));
prim->userData().set2("lightProfile", std::move(profile));


auto visibleIntensity = get_input2<float>("visibleIntensity");
prim->userData().set2("visibleIntensity", std::move(visibleIntensity));

set_output("prim", std::move(prim));
}

Expand Down Expand Up @@ -406,22 +440,31 @@ ZENO_DEFNODE(LightNode)({
{"vec3f", "scale", "1, 1, 1"},
{"vec3f", "rotate", "0, 0, 0"},
{"vec4f", "quaternion", "1, 0, 0, 0"},

{"vec3f", "color", "1, 1, 1"},
{"float", "exposure", "0"},
{"float", "intensity", "1"},
{"bool", "islight", "1"},
{"bool", "invertdir", "1"},
{"float", "maxDistance", "-1.0" },

{"bool", "visible", "0"},
{"bool", "invertdir", "1"},
{"bool", "doubleside", "0"},
{"string", "profile", ""},

{"readpath", "profile"},
{"readpath", "texturePath"},
{"float", "textureGamma", "1.0"},

{"float", "visibleIntensity", "1.0"},
{"enum " + LightNode::lightShapeListString(), LightNode::lightShapeKey, LightNode::lightShapeDefaultString()},
{"enum " + LightNode::lightTypeListString(), LightNode::lightTypeKey, LightNode::lightTypeDefaultString()}
{"enum " + LightNode::lightTypeListString(), LightNode::lightTypeKey, LightNode::lightTypeDefaultString()},
{"PrimitiveObject", "prim"},
},
{
"prim"
},
{

{"enum " + EulerAngle::RotationOrderListString(), "EulerRotationOrder", EulerAngle::RotationOrderDefaultString()},
{"enum " + EulerAngle::MeasureListString(), "EulerAngleMeasure", EulerAngle::MeasureDefaultString()}
},
{"shader"},
});
Expand Down
2 changes: 1 addition & 1 deletion zeno/include/zeno/types/LightObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace zeno {
};

enum struct LightShape {
Plane=0u, Sphere=1u, Point=2u
Plane=0u, Sphere=1u, Point=2u, TriangleMesh=3u
};

enum LightConfigMask {
Expand Down
85 changes: 85 additions & 0 deletions zeno/include/zeno/utils/eulerangle.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
#pragma once

#include <map>

#include "magic_enum.hpp"
#include "glm/gtc/matrix_transform.hpp"

namespace EulerAngle
{

enum struct Measure {
Degree, Radians
};

enum struct RotationOrder {
XYZ, XZY,
YXZ, YZX,
ZXY, ZYX
};

static std::string MeasureDefaultString() {
auto name = magic_enum::enum_name(Measure::Radians);
return std::string(name);
}

static std::string MeasureListString() {
auto list = magic_enum::enum_names<Measure>();

std::string result;
for (auto& ele : list) {
result += " ";
result += ele;
}
return result;
}

static std::string RotationOrderDefaultString() {
auto name = magic_enum::enum_name(RotationOrder::YXZ);
return std::string(name);
}

static std::string RotationOrderListString() {
auto list = magic_enum::enum_names<RotationOrder>();

std::string result;
for (auto& ele : list) {
result += " ";
result += ele;
}
return result;
}

inline glm::mat4 rotate(RotationOrder order, Measure measure, const glm::vec3& angleXYZ) {

auto angles = angleXYZ;

if ( measure != Measure::Radians ) {
angles[0] = glm::radians(angleXYZ[0]);
angles[1] = glm::radians(angleXYZ[1]);
angles[2] = glm::radians(angleXYZ[2]);
}

auto orderName = magic_enum::enum_name(order);

// static const std::map<char, uint8_t> kv {
// {'X', 0}, {'Y', 1}, {'Z', 2}
// };

glm::mat4 rotation = glm::mat4(1.0f);

for (size_t i=0; i<3; ++i) {
auto K = orderName[i];

auto index = K - 'X'; //kv.at(k);
auto angle = angles[index];

auto axis = glm::vec3(0,0,0);
axis[index] = 1.0f;

rotation = glm::rotate(rotation, angle, axis);
}

return rotation;
}
}
45 changes: 35 additions & 10 deletions zeno/src/nodes/prim/SimpleGeometry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@
#endif
//#include <spdlog/spdlog.h>

#include <zeno/utils/eulerangle.h>

#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <glm/vec2.hpp>
#include <glm/vec3.hpp>
#include <glm/gtc/type_ptr.hpp>

#define ROTATE_COMPUTE \
auto gp = glm::vec3(p[0], p[1], p[2]); \
Expand Down Expand Up @@ -1009,9 +1010,18 @@ struct CreateTorus : zeno::INode {
auto rotate = get_input2<zeno::vec3f>("rotate");
glm::mat4 transform = glm::mat4 (1.0);
transform = glm::translate(transform, glm::vec3(position[0], position[1], position[2]));
transform = glm::rotate(transform, glm::radians(rotate[0]), glm::vec3(1, 0, 0));
transform = glm::rotate(transform, glm::radians(rotate[1]), glm::vec3(0, 1, 0));
transform = glm::rotate(transform, glm::radians(rotate[2]), glm::vec3(0, 0, 1));

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

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);

transform = transform * rotation;

auto n_transform = glm::transpose(glm::inverse(transform));
for(int i = 0; i < prim->verts.size(); i++){
auto p = prim->verts[i];
Expand Down Expand Up @@ -1051,7 +1061,10 @@ ZENDEFNODE(CreateTorus, {
{"bool", "quads", "0"},
},
{"prim"},
{},
{
{"enum " + EulerAngle::RotationOrderListString(), "EulerRotationOrder", "XYZ"},
{"enum " + EulerAngle::MeasureListString(), "EulerAngleMeasure", "Degree"}
},
{"create"},
});

Expand Down Expand Up @@ -1176,9 +1189,18 @@ struct CreateSphere : zeno::INode {

glm::mat4 transform = glm::mat4 (1.0);
transform = glm::translate(transform, glm::vec3(position[0], position[1], position[2]));
transform = glm::rotate(transform, glm::radians(rotate[0]), glm::vec3(1, 0, 0));
transform = glm::rotate(transform, glm::radians(rotate[1]), glm::vec3(0, 1, 0));
transform = glm::rotate(transform, glm::radians(rotate[2]), glm::vec3(0, 0, 1));

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

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);

transform = transform * rotation;

transform = glm::scale(transform, glm::vec3(scale[0],scale[1],scale[2]) * radius);

auto n_transform = glm::transpose(glm::inverse(transform));
Expand Down Expand Up @@ -1286,7 +1308,10 @@ ZENDEFNODE(CreateSphere, {
{"bool", "SphereRT", "0"}
},
{"prim"},
{},
{
{"enum " + EulerAngle::RotationOrderListString(), "EulerRotationOrder", "XYZ"},
{"enum " + EulerAngle::MeasureListString(), "EulerAngleMeasure", "Degree"}
},
{"create"},
});

Expand Down
Loading
Loading