Parse more Shader stuff.

This commit is contained in:
Syoyo Fujita
2022-04-19 20:40:28 +09:00
parent ec45f14f59
commit a215530d81
8 changed files with 320 additions and 2 deletions

View File

@@ -375,6 +375,31 @@ std::string to_string(const Xform &xform, const uint32_t indent) {
return ss.str();
}
std::string to_string(const GeomCamera &camera, const uint32_t indent) {
std::stringstream ss;
ss << Indent(indent) << "def Camera \"" << camera.name << "\"\n";
ss << Indent(indent) << "(\n";
// args
ss << Indent(indent) << ")\n";
ss << Indent(indent) << "{\n";
// members
ss << Indent(indent) << " float2 clippingRange = " << camera.clippingRange << "\n";
ss << Indent(indent) << " float focalLength = " << camera.focalLength << "\n";
ss << Indent(indent) << " float horizontalAperture = " << camera.horizontalAperture << "\n";
ss << Indent(indent) << " float horizontalApertureOffset = " << camera.horizontalApertureOffset << "\n";
ss << Indent(indent) << " token projection = \"" << camera.projection << "\"\n";
ss << Indent(indent) << " float verticalAperture = " << camera.verticalAperture << "\n";
ss << Indent(indent) << " float verticalApertureOffset = " << camera.verticalApertureOffset << "\n";
//ss << print_predefined(camera, indent);
ss << Indent(indent) << "}\n";
return ss.str();
}
std::string to_string(const GeomSphere &sphere, const uint32_t indent) {
std::stringstream ss;
@@ -555,5 +580,45 @@ std::string to_string(const GeomCapsule &geom, const uint32_t indent) {
return ss.str();
}
std::string to_string(const LuxSphereLight &light, const uint32_t indent) {
std::stringstream ss;
ss << Indent(indent) << "def SphereLight \"" << light.name << "\"\n";
ss << Indent(indent) << "(\n";
// args
ss << Indent(indent) << ")\n";
ss << Indent(indent) << "{\n";
// members
ss << Indent(indent) << " color3f inputs:color = " << light.color << "\n";
ss << Indent(indent) << " float inputs:intensity = " << light.intensity << "\n";
ss << Indent(indent) << " float inputs:radius = " << light.radius << "\n";
ss << Indent(indent) << " float inputs:specular = " << light.specular << "\n";
ss << Indent(indent) << "}\n";
return ss.str();
}
std::string to_string(const Shader &shader, const uint32_t indent) {
std::stringstream ss;
ss << Indent(indent) << "def Shader \"" << shader.name << "\"\n";
ss << Indent(indent) << "(\n";
// args
ss << Indent(indent) << ")\n";
ss << Indent(indent) << "{\n";
// members
ss << Indent(indent) << " uniform token info:id = \"" << shader.info_id << "\"\n";
// TODO
//if (auto p = nonstd::get_if<PreviewSurface>(shader.value)) {
//}
ss << Indent(indent) << "}\n";
return ss.str();
}
} // tinyusdz

View File

@@ -121,6 +121,10 @@ std::string to_string(const GeomCapsule &geom, const uint32_t indent = 0);
std::string to_string(const GeomCone &geom, const uint32_t indent = 0);
std::string to_string(const GeomCylinder &geom, const uint32_t indent = 0);
std::string to_string(const GeomCube &geom, const uint32_t indent = 0);
std::string to_string(const GeomCamera &camera, const uint32_t indent = 0);
std::string to_string(const LuxSphereLight &light, const uint32_t indent = 0);
std::string to_string(const Shader &shader, const uint32_t indent = 0);
std::string to_string(const tinyusdz::AnimatableVisibility &v, const uint32_t );

View File

@@ -124,6 +124,7 @@ enum class Variability {
Invalid
};
// TODO: Use primvar::asset
struct AssetReference {
std::string asset_reference;
std::string prim_path;
@@ -1305,6 +1306,33 @@ struct UVCoords {
std::vector<uint32_t> indices; // UV indices. Usually varying
};
struct GeomCamera {
std::string name;
int64_t parent_id{-1}; // Index to parent node
//
// Properties
//
AnimatableExtent extent; // bounding extent(in local coord?).
AnimatableVisibility visibility{Visibility::Inherited};
Purpose purpose{Purpose::Default};
// TODO: Animatable?
Vec2f clippingRange{{0.1f, 100.0f}};
float focalLength{50.0f};
float horizontalAperture{36.0f};
float horizontalApertureOffset{0.0f};
std::string projection{"perspective"};
float verticalAperture{20.25f};
float verticalApertureOffset{0.0f};
// List of Primitive attributes(primvars)
// NOTE: `primvar:widths` are not stored here(stored in `widths`)
std::map<std::string, PrimAttrib> attribs;
};
struct GeomBoundable {
std::string name;
@@ -1551,6 +1579,35 @@ struct GeomPoints {
std::map<std::string, PrimAttrib> attribs;
};
struct LuxSphereLight
{
std::string name;
int64_t parent_id{-1}; // Index to xform node
//
// Predefined attribs.
//
// TODO: Support texture?
primvar::color3f color{};
float intensity{10.0f};
float radius{0.1f};
float specular{1.0f};
//
// Properties
//
AnimatableExtent extent; // bounding extent(in local coord?).
AnimatableVisibility visibility{Visibility::Inherited};
Purpose purpose{Purpose::Default};
// List of Primitive attributes(primvars)
// NOTE: `primvar:widths` may exist(in that ase, please ignore `widths`
// parameter)
std::map<std::string, PrimAttrib> attribs;
};
// BlendShapes
// TODO(syoyo): Blendshape
struct BlendShape {
@@ -1815,6 +1872,7 @@ struct UVTexture {
// https://graphics.pixar.com/usd/docs/UsdPreviewSurface-Proposal.html#UsdPreviewSurfaceProposal-TextureCoordinateOrientationinUSD
};
// UsdPreviewSurface
// USD's default? PBR shader
// https://graphics.pixar.com/usd/docs/UsdPreviewSurface-Proposal.html
// $USD/pxr/usdImaging/plugin/usdShaders/shaders/shaderDefs.usda
@@ -1861,6 +1919,15 @@ struct PreviewSurface {
int64_t displacement_id{-1}; // index to `Scene::shaders`
};
struct Shader {
std::string name;
std::string info_id; // Shader type.
// Currently we only support PreviewSurface, UVTexture and PrimvarReader_float2
nonstd::variant<PreviewSurface, UVTexture, PrimvarReader_float2> value;
};
// USDZ Schemas for AR
// https://developer.apple.com/documentation/arkit/usdz_schemas_for_ar/schema_definitions_for_third-party_digital_content_creation_dcc

View File

@@ -418,6 +418,11 @@ std::ostream &operator<<(std::ostream &os, const quatd &v) {
return os;
}
std::ostream &operator<<(std::ostream &os, const texcoord2f &v) {
os << "(" << v.s << ", " << v.t << ")";
return os;
}
bool Reconstructor::reconstruct(AttribMap &amap) {
err_.clear();

View File

@@ -48,6 +48,11 @@ namespace primvar {
// string literal. represent it as string_view
using token = nonstd::string_view;
struct asset
{
std::string asset_path;
};
// TODO(syoyo): 3D and 4D?
constexpr uint32_t TYPE_ID_1D_ARRAY_BIT = 1 << 10;
constexpr uint32_t TYPE_ID_2D_ARRAY_BIT = 1 << 11;
@@ -137,6 +142,8 @@ enum TypeId {
TYPE_ID_DICT,
TYPE_ID_ASSET,
TYPE_ID_ALL // terminator
};
@@ -499,6 +506,8 @@ DEFINE_TYPE_TRAIT(token, "token", TYPE_ID_TOKEN, 1);
DEFINE_TYPE_TRAIT(std::string, "string", TYPE_ID_STRING, 1);
DEFINE_TYPE_TRAIT(dict, "dictionary", TYPE_ID_DICT, 1);
DEFINE_TYPE_TRAIT(asset, "asset", TYPE_ID_ASSET, 1);
#undef DEFINE_TYPE_TRAIT
// 1D Array

View File

@@ -5110,6 +5110,7 @@ bool Parser::ReconstructScene(Scene *scene) {
return false;
}
return true;
}
bool Parser::ReadSpecs() {

View File

@@ -544,6 +544,8 @@ struct Scene {
// TODO(syoyo): Use std::variant(C++17) like static polymorphism?
//
std::vector<Xform> xforms;
std::vector<GeomCamera> geom_cameras;
std::vector<LuxSphereLight> lux_sphere_lights;
std::vector<GeomMesh> geom_meshes;
std::vector<GeomBasisCurves> geom_basis_curves;
std::vector<GeomPoints> geom_points;

View File

@@ -2105,8 +2105,8 @@ class USDAParser::Impl {
return false;
}
std::cout << "Path identifier = " << value << "\n";
PUSH_ERROR("TODO:" + primattr_name);
return false;
attr.var.set_scalar(value); // TODO: set as `Path` type
} else {
nonstd::optional<T> value;
if (!ReadBasicType(&value)) {
@@ -2410,7 +2410,24 @@ class USDAParser::Impl {
}
is_rel = true;
} else if (type_name == "texCoord2f") {
if (!ParseBasicPrimAttr<primvar::texcoord2f>(array_qual, primattr_name, &attr)) {
PUSH_ERROR("Failed to parse texCoord2f data.");
return false;
}
} else if (type_name == "asset") {
AssetReference assert_ref;
bool triple_deliminated{false};
if (!ParseAssetReference(&assert_ref, &triple_deliminated)) {
PUSH_ERROR("Failed to parse `asset` data.");
}
primvar::asset asset;
asset.asset_path = assert_ref.asset_reference;
attr.var.set_scalar(asset);
} else {
PUSH_ERROR("TODO: type = " + type_name);
return false;
@@ -3160,6 +3177,7 @@ class USDAParser::Impl {
bool ReadBasicType(nonstd::optional<primvar::point3d> *value);
bool ReadBasicType(nonstd::optional<primvar::normal3f> *value);
bool ReadBasicType(nonstd::optional<primvar::normal3d> *value);
bool ReadBasicType(nonstd::optional<primvar::texcoord2f> *value);
bool ReadBasicType(std::string *value);
bool ReadBasicType(int *value);
@@ -3181,6 +3199,7 @@ class USDAParser::Impl {
bool ReadBasicType(primvar::point3d *value);
bool ReadBasicType(primvar::normal3f *value);
bool ReadBasicType(primvar::normal3d *value);
bool ReadBasicType(primvar::texcoord2f *value);
// TimeSample data
bool ReadTimeSampleData(nonstd::optional<Vec2f> *value);
@@ -6072,6 +6091,28 @@ class USDAParser::Impl {
curves.name = node_name;
std::cout << to_string(curves, nestlevel) << "\n";
} else if (prim_type == "Camera") {
GeomCamera camera;
std::cout << "Reconstruct Camera\n";
if (!ReconstructGeomCamera(props, references, &camera)) {
PushError("Failed to reconstruct Camera.");
return false;
}
camera.name = node_name;
std::cout << to_string(camera, nestlevel) << "\n";
} else if (prim_type == "Shader") {
Shader shader;
std::cout << "Reconstruct Shader\n";
if (!ReconstructShader(props, references, &shader)) {
PushError("Failed to reconstruct Shader.");
return false;
}
shader.name = node_name;
std::cout << to_string(shader, nestlevel) << "\n";
} else {
PUSH_ERROR(" TODO: " + prim_type);
return false;
@@ -6146,6 +6187,30 @@ class USDAParser::Impl {
std::vector<std::pair<ListEditQual, AssetReference>> &references,
GeomBasisCurves *curves);
bool ReconstructGeomCamera(const std::map<std::string, Property> &properties,
std::vector<std::pair<ListEditQual, AssetReference>> &references,
GeomCamera *curves);
bool ReconstructShader(const std::map<std::string, Property> &properties,
std::vector<std::pair<ListEditQual, AssetReference>> &references,
Shader *shader);
bool ReconstructPreviewSurface(const std::map<std::string, Property> &properties,
std::vector<std::pair<ListEditQual, AssetReference>> &references,
PreviewSurface *surface);
bool ReconstructUVTexture(const std::map<std::string, Property> &properties,
std::vector<std::pair<ListEditQual, AssetReference>> &references,
UVTexture *texture);
bool ReconstructPrimvarReader_float2(const std::map<std::string, Property> &properties,
std::vector<std::pair<ListEditQual, AssetReference>> &references,
PrimvarReader_float2 *reader_float2);
bool ReconstructLuxSphereLight(const std::map<std::string, Property> &properties,
std::vector<std::pair<ListEditQual, AssetReference>> &references,
LuxSphereLight *light);
bool CheckHeader() { return ParseMagicHeader(); }
void ImportScene(tinyusdz::Scene &scene) { _scene = scene; }
@@ -7475,6 +7540,62 @@ bool USDAParser::Impl::ReconstructBasisCurves(const std::map<std::string, Proper
return true;
}
bool USDAParser::Impl::ReconstructGeomCamera(const std::map<std::string, Property> &properties,
std::vector<std::pair<ListEditQual, AssetReference>> &references,
GeomCamera *camera) {
for (const auto &prop : properties) {
if (prop.first == "focalLength") {
// TODO
} else {
std::cout << "TODO: " << prop.first << "\n";
}
}
return true;
}
bool USDAParser::Impl::ReconstructShader(const std::map<std::string, Property> &properties,
std::vector<std::pair<ListEditQual, AssetReference>> &references,
Shader *shader) {
for (const auto &prop : properties) {
if (prop.first == "info:id") {
const PrimAttrib &attr = prop.second.attrib;
auto p = attr.var.get_value<std::string>();
if (p) {
if (p->compare("UsdPreviewSurface") == 0) {
PreviewSurface surface;
if (!ReconstructPreviewSurface(properties, references, &surface)) {
PUSH_ERROR("Failed to reconstruct PreviewSurface.");
return false;
}
shader->value = surface;
} else if (p->compare("UsdUVTexture") == 0) {
PUSH_ERROR("TODO: UsdUVTexture");
} else if (p->compare("UsdPrimvarReader_float2") == 0) {
PUSH_ERROR("TODO: UsdPrimvarReader_float2");
} else {
PUSH_ERROR("TODO: Shader id: " + (*p));
return false;
}
}
} else {
std::cout << "TODO: " << prop.first << "\n";
}
}
return true;
}
bool USDAParser::Impl::ReconstructPreviewSurface(const std::map<std::string, Property> &properties,
std::vector<std::pair<ListEditQual, AssetReference>> &references,
PreviewSurface *surface) {
// TODO:
return true;
}
// 'None'
bool USDAParser::Impl::MaybeNone() {
std::vector<char> buf;
@@ -7968,6 +8089,50 @@ bool USDAParser::Impl::ReadBasicType(primvar::normal3d *value) {
return true;
}
bool USDAParser::Impl::ReadBasicType(primvar::texcoord2f *value) {
if (!Expect('(')) {
return false;
}
std::vector<float> values;
if (!SepBy1BasicType<float>(',', &values)) {
return false;
}
if (!Expect(')')) {
return false;
}
if (values.size() != 2) {
std::string msg = "The number of tuple elements must be " +
std::to_string(2) + ", but got " +
std::to_string(values.size()) + "\n";
PUSH_ERROR(msg);
return false;
}
value->s = values[0];
value->t = values[1];
return true;
}
bool USDAParser::Impl::ReadBasicType(nonstd::optional<primvar::texcoord2f> *value) {
if (MaybeNone()) {
(*value) = nonstd::nullopt;
return true;
}
primvar::texcoord2f v;
if (ReadBasicType(&v)) {
(*value) = v;
return true;
}
return false;
}
bool USDAParser::Impl::ReadBasicType(nonstd::optional<Vec2f> *value) {
if (MaybeNone()) {
(*value) = nonstd::nullopt;