// Copyright (c) 2022 The Foundry Visionmongers Ltd. All Rights Reserved. #include "DDImage/Knobs.h" #include "DDImage/SourceGeomOp.h" #include "usg/geom/MeshPrim.h" #include "usg/geom/GeomTokens.h" #include "ndk/geo/utils/MeshUtils.h" #include using namespace DD::Image; namespace NukeExample { static const char* kTriangleClass = "GeoTriangle"; static const char* kHelp = "Creates a 3D @i;Triangle@n;"; class GeoTriangle : public SourceGeomOp { struct KnobSample : public GeomKnobSample { fdk::Vec3f pointA; fdk::Vec3f pointB; fdk::Vec3f pointC; } parms; public: class Engine : public SourceEngine { KnobSample parms; public: Engine(Op* parent) : SourceEngine(parent) {} void createPrims(usg::GeomSceneContext& context, const usg::Path& path) override { const auto kPointA = knob("point_a"); if (kPointA) { parms.pointA = kPointA.get(); } const auto kPointB = knob("point_b"); if (kPointB) { parms.pointB = kPointB.get(); } const auto kPointC = knob("point_c"); if (kPointC) { parms.pointC = kPointC.get(); } if (definesEditLayer()) { usg::LayerRef defineLayer = editLayer(); assert(defineLayer); ndk::MeshSample sample; usg::MeshPrim mesh = usg::MeshPrim::defineInLayer(defineLayer, path.appendChild("Triangle")); sample.addTri(0, 1, 2); sample.points = {parms.pointA, parms.pointB, parms.pointC}; sample.setMeshPrimFaceTopology(mesh); sample.setMeshPrimPointsAndBounds(mesh, true/*buildBoundsFromPoints*/); sample.setMeshPrimProperties(mesh, true/*setUvs*/, true/*setNormals*/); } } }; GeoTriangle(Node* node) : SourceGeomOp(node, BuildEngine()), _aPos(0, 0, 0), _bPos(1, 0, 0), _cPos(0, 1, 0) {} virtual const char* Class() const override { return kTriangleClass; } const char* node_help() const override { return kHelp; } virtual void knobs(Knob_Callback f) override { SourceGeomOp::knobs(f); // Set up the common SourceGeo knobs. XYZ_knob(f, &_aPos[0], "point_a", "a"); KnobDefinesGeometry(f); XYZ_knob(f, &_bPos[0], "point_b", "b"); KnobDefinesGeometry(f); XYZ_knob(f, &_cPos[0], "point_c", "c"); KnobDefinesGeometry(f); makeTransformKnob(f); } static Op* Build(Node* node) { return new GeoTriangle(node); } static const Op::Description description; private: fdk::Vec3f _aPos; fdk::Vec3f _bPos; fdk::Vec3f _cPos; }; const Op::Description GeoTriangle::description(kTriangleClass, GeoTriangle::Build); }