Exemplo n.º 1
0
    static void InitExportableObjects(GameObject go,
                                      ExportContext context) {
      var smr = go.GetComponent<SkinnedMeshRenderer>();
      var mr = go.GetComponent<MeshRenderer>();
      var mf = go.GetComponent<MeshFilter>();
      var cam = go.GetComponent<Camera>();
      Transform expRoot = context.exportRoot;

      var tmpPath = new pxr.SdfPath(UnityTypeConverter.GetPath(go.transform, expRoot));
      while (!tmpPath.IsRootPrimPath()) {
        tmpPath = tmpPath.GetParentPath();
      }

      // TODO: What if this path is in use?
      string materialBasePath = tmpPath.ToString() + "/Materials/";

      // Ensure the "Materials" prim is defined with a valid prim type.
      context.scene.Write(materialBasePath.TrimEnd('/'), new ScopeSample());

      if (smr != null) {
        foreach (var mat in smr.sharedMaterials) {
          if (!context.matMap.ContainsKey(mat)) {
            string usdPath = materialBasePath + pxr.UsdCs.TfMakeValidIdentifier(mat.name + "_" + mat.GetInstanceID().ToString());
            context.matMap.Add(mat, usdPath);
          }
        }
        CreateExportPlan(go, CreateSample<MeshSample>(context), MeshExporter.ExportSkinnedMesh, context);
        CreateExportPlan(go, CreateSample<MeshSample>(context), NativeExporter.ExportObject, context, insertFirst: false);
        if (smr.rootBone == null) {
          Debug.LogWarning("No root bone at: " + UnityTypeConverter.GetPath(go.transform, expRoot));
        } else if (smr.bones == null || smr.bones.Length == 0) {
          Debug.LogWarning("No bones at: " + UnityTypeConverter.GetPath(go.transform, expRoot));
        } else {
          // Each mesh in a model may have a different root bone, which now must be merged into a
          // single skeleton for export to USD.
          try {
            MergeBonesSimple(smr.transform, smr.rootBone, smr.bones, smr.sharedMesh.bindposes, context);
          } catch (Exception ex) {
            Debug.LogException(new Exception("Failed to merge bones for " + UnityTypeConverter.GetPath(smr.transform), ex));
          }
        }
      } else if (mf != null && mr != null) {
        foreach (var mat in mr.sharedMaterials) {
          if (mat == null) {
            continue;
          }
          if (!context.matMap.ContainsKey(mat)) {
            string usdPath = materialBasePath + pxr.UsdCs.TfMakeValidIdentifier(mat.name + "_" + mat.GetInstanceID().ToString());
            context.matMap.Add(mat, usdPath);
          }
        }
        CreateExportPlan(go, CreateSample<MeshSample>(context), MeshExporter.ExportMesh, context);
        CreateExportPlan(go, CreateSample<MeshSample>(context), NativeExporter.ExportObject, context, insertFirst: false);
      } else if (cam) {
        CreateExportPlan(go, CreateSample<CameraSample>(context), CameraExporter.ExportCamera, context);
        CreateExportPlan(go, CreateSample<CameraSample>(context), NativeExporter.ExportObject, context, insertFirst: false);
      }
    }
Exemplo n.º 2
0
        SkeletonSample ReadUsdSkeleton(Scene scene, out string skelRootPath)
        {
            skelRootPath = null;
            if (scene == null)
            {
                return(null);
            }

            try
            {
                var path        = new pxr.SdfPath(m_usdMeshPath);
                var absRoot     = pxr.SdfPath.AbsoluteRootPath();
                var skelRelName = new pxr.TfToken("skel:skeleton");

                while (path != absRoot)
                {
                    var prim = scene.GetPrimAtPath(path);
                    path = path.GetParentPath();

                    if (prim.HasRelationship(skelRelName))
                    {
                        var targets = prim.GetRelationship(skelRelName).GetForwardedTargets();
                        if (targets.Count == 0)
                        {
                            Debug.LogWarning("skel:skeleton has no targets at path: " + prim.GetPath());
                            continue;
                        }

                        var skelTarget = scene.GetPrimAtPath(targets[0]);
                        if (skelTarget == null)
                        {
                            Debug.LogWarning("prim <" + prim.GetPath() +
                                             "> has skel:skeleton with missing target path: " + targets[0]);
                            continue;
                        }

                        skelRootPath = skelTarget.GetPath().ToString();
                        var sample = new SkeletonSample();
                        scene.Read(skelTarget.GetPath(), sample);
                        return(sample);
                    }
                }

                Debug.LogWarning("Skeleton not found for path: " + m_usdMeshPath);
                return(null);
            }
            catch (System.Exception ex)
            {
                Debug.LogException(ex);
                return(null);
            }
        }
Exemplo n.º 3
0
        public static void SdfPathEqualityTest()
        {
            var A = new pxr.SdfPath("/Foo");
            var B = new pxr.SdfPath("/Foo");
            var C = new pxr.SdfPath("/Foo/Bar");

            AssertEqual(A, A);
            AssertEqual(B, B);
            AssertEqual(C, C);
            AssertEqual(A, B);
            AssertEqual(A, C.GetParentPath());
            AssertEqual(C.GetParentPath(), A);

            AssertEqual(A.GetHashCode(), A.GetHashCode());
            AssertEqual(B.GetHashCode(), B.GetHashCode());
            AssertEqual(C.GetHashCode(), C.GetHashCode());
            AssertEqual(A.GetHashCode(), B.GetHashCode());
            AssertEqual(A.GetHashCode(), C.GetParentPath().GetHashCode());
            AssertEqual(C.GetParentPath().GetHashCode(), A.GetHashCode());

            AssertEqual(A.GetHash(), A.GetHash());
            AssertEqual(B.GetHash(), B.GetHash());
            AssertEqual(C.GetHash(), C.GetHash());
            AssertEqual(A.GetHash(), B.GetHash());
            AssertEqual(A.GetHash(), C.GetParentPath().GetHash());
            AssertEqual(C.GetParentPath().GetHash(), A.GetHash());

            AssertEqual(A.ToString(), A.ToString());
            AssertEqual(B.ToString(), B.ToString());
            AssertEqual(C.ToString(), C.ToString());
            AssertEqual(A.ToString(), B.ToString());
            AssertEqual(A.ToString(), C.GetParentPath().ToString());
            AssertEqual(C.GetParentPath().ToString(), A.ToString());

            var hashSet = new HashSet <pxr.SdfPath>();

            hashSet.Add(A);
            AssertTrue(hashSet.Contains(A));
            AssertTrue(hashSet.Contains(B));
            AssertTrue(hashSet.Contains(C.GetParentPath()));
            AssertTrue(hashSet.Remove(B));

            hashSet.Add(B);
            AssertTrue(hashSet.Contains(A));
            AssertTrue(hashSet.Contains(B));
            AssertTrue(hashSet.Contains(C.GetParentPath()));
            AssertTrue(hashSet.Remove(C.GetParentPath()));

            var dict = new Dictionary <pxr.SdfPath, string>();

            dict.Add(A, A.GetString());
            AssertTrue(dict.ContainsKey(A));
            AssertTrue(dict.ContainsKey(B));
            AssertTrue(dict.ContainsKey(C.GetParentPath()));
            AssertTrue(dict.Remove(B));

            dict.Add(B, B.GetString());
            AssertTrue(dict.ContainsKey(A));
            AssertTrue(dict.ContainsKey(B));
            AssertTrue(dict.ContainsKey(C.GetParentPath()));
            AssertTrue(dict.Remove(C.GetParentPath()));
        }
Exemplo n.º 4
0
 // Recreate hierarchy.
 void AssignParent(pxr.SdfPath path, GameObject go)
 {
     m_primMap.Add(path, go);
     go.transform.SetParent(m_primMap[path.GetParentPath()].transform, worldPositionStays: false);
 }