Ejemplo n.º 1
0
        void ExportCamera(Scene scene, GameObject go, ExportPlan exportPlan)
        {
            CameraSample sample = (CameraSample)exportPlan.sample;
            Camera       camera = go.GetComponent <Camera>();

            sample.CopyFromCamera(camera);
            scene.Write(exportPlan.path, sample);
        }
Ejemplo n.º 2
0
        void ExportMesh(Scene scene, GameObject go, ExportPlan exportPlan)
        {
            MeshFilter mf        = go.GetComponent <MeshFilter>();
            Mesh       mesh      = mf.mesh;
            bool       unvarying = scene.Time == null;

            if (unvarying)
            {
                // Only export the mesh topology on the first frame.
                var sample = (MeshSample)exportPlan.sample;
                sample.transform = GetLocalTransformMatrix(go.transform);

                // Unity uses a forward vector that matches DirectX, but USD matches OpenGL, so a change of
                // basis is required. There are shortcuts, but this is fully general.
                sample.ConvertTransform();

                sample.normals  = mesh.normals;
                sample.points   = mesh.vertices;
                sample.tangents = mesh.tangents;
                sample.extent   = mesh.bounds;
                sample.colors   = mesh.colors;
                if (sample.colors != null && sample.colors.Length != sample.points.Length)
                {
                    sample.colors = null;
                }

                // Set face vertex counts and indices.
                sample.SetTriangles(mesh.triangles);

                scene.Write(exportPlan.path, sample);

                var    mr = go.GetComponent <MeshRenderer>();
                string usdMaterialPath;
                if (!m_materialMap.TryGetValue(mr.material, out usdMaterialPath))
                {
                    Debug.LogError("Invalid material bound for: " + exportPlan.path);
                }
                else
                {
                    MaterialSample.Bind(scene, exportPlan.path, usdMaterialPath);
                }
            }
            else
            {
                var sample = new XformSample();
                sample.transform = GetLocalTransformMatrix(go.transform);

                // Unity uses a forward vector that matches DirectX, but USD matches OpenGL, so a change of
                // basis is required. There are shortcuts, but this is fully general.
                sample.ConvertTransform();

                scene.Write(exportPlan.path, sample);
            }

            // On all other frames, we just export the mesh transform data.
            // TODO(jcowles): cant currently do this because the USD prim typeName is overwritten.
            //ExportXform(scene, go, exportPlan, unvarying: false);
        }
Ejemplo n.º 3
0
        void ExportXform(Scene scene, GameObject go, ExportPlan exportPlan)
        {
            XformSample sample = (XformSample)exportPlan.sample;

            sample.transform = GetLocalTransformMatrix(go.transform);

            // Unity uses a forward vector that matches DirectX, but USD matches OpenGL, so a change of
            // basis is required. There are shortcuts, but this is fully general.
            sample.ConvertTransform();

            scene.Write(exportPlan.path, sample);
        }
Ejemplo n.º 4
0
        void Update()
        {
            if (!IsRecording)
            {
                return;
            }

            // On the first frame, export all the unvarying data (e.g. mesh topology).
            // On subsequent frames, skip unvarying data to avoid writing redundant data.
            if (Time.frameCount == m_startFrame)
            {
                // First write materials.
                foreach (var kvp in m_materialMap)
                {
                    ExportMaterial(m_usdScene, kvp.Key, kvp.Value);
                }
                // Next, write geometry, which may also bind to materials written above.
                foreach (var kvp in m_primMap)
                {
                    ExportPlan exportPlan = kvp.Value;
                    GameObject go         = kvp.Key;
                    exportPlan.exportFunc(m_usdScene, go, exportPlan);
                }
            }

            // Set the time at which to read samples from USD.
            // If the FramesPerSecond is set to 1 above, this should be Time.time instead of frame count.
            m_usdScene.Time = Time.frameCount - m_startFrame;
            m_curFrame      = (int)m_usdScene.Time.Value;

            // Exit once we've recorded all frames.
            if (m_usdScene.Time > m_frameCount)
            {
                StopRecording();
                return;
            }

            // Record the time varying data that changes from frame to frame.
            foreach (var kvp in m_primMap)
            {
                ExportPlan exportPlan = kvp.Value;
                GameObject go         = kvp.Key;
                exportPlan.exportFunc(m_usdScene, go, exportPlan);
            }
        }
Ejemplo n.º 5
0
        private static void ExportImpl(GameObject root,
                                       ExportContext context)
        {
            var  scene        = context.scene;
            bool skipInactive = context.activePolicy == ActiveExportPolicy.DoNotExport;

            if (context.exportMaterials)
            {
                // TODO: should account for skipped objects and also skip their materials.
                UnityEngine.Profiling.Profiler.BeginSample("USD: Export Materials");
                foreach (var kvp in context.matMap)
                {
                    Material mat     = kvp.Key;
                    string   usdPath = kvp.Value;
                    if (!mat || usdPath == null)
                    {
                        continue;
                    }

                    try
                    {
                        Unity.Formats.USD.MaterialExporter.ExportMaterial(scene, kvp.Key, kvp.Value);
                    }
                    catch (Exception ex)
                    {
                        Debug.LogException(new Exception("Error exporting material: " + kvp.Value, ex));
                    }
                }

                UnityEngine.Profiling.Profiler.EndSample();
            }

            UnityEngine.Profiling.Profiler.BeginSample("USD: Process Export Plans");
            foreach (var kvp in context.plans)
            {
                GameObject go         = kvp.Key;
                ExportPlan exportPlan = kvp.Value;

                if (!go || exportPlan == null)
                {
                    continue;
                }

                if (go != root && !go.transform.IsChildOf(root.transform))
                {
                    continue;
                }

                if (skipInactive && go.activeInHierarchy == false)
                {
                    continue;
                }

                foreach (Exporter exporter in exportPlan.exporters)
                {
                    string     path   = exporter.path;
                    SampleBase sample = exporter.sample;
                    var        objCtx = new ObjectContext
                    {
                        gameObject     = go,
                        path           = path,
                        sample         = sample,
                        additionalData = exporter.data
                    };

                    try
                    {
                        exporter.exportFunc(objCtx, context);
                    }
                    catch (Exception ex)
                    {
                        Debug.LogException(new Exception("Error exporting: " + path, ex));
                        continue;
                    }

                    UnityEngine.Profiling.Profiler.BeginSample("USD: Process Visibility");
                    try
                    {
                        if (!go.gameObject.activeSelf)
                        {
                            switch (context.activePolicy)
                            {
                            case ActiveExportPolicy.Ignore:
                                // Nothing to see here.
                                break;

                            case ActiveExportPolicy.ExportAsVisibility:
                                // Make the prim invisible.
                                var im = new pxr.UsdGeomImageable(scene.GetPrimAtPath(path));
                                if (im)
                                {
                                    im.CreateVisibilityAttr().Set(pxr.UsdGeomTokens.invisible);
                                }

                                break;

                            case ActiveExportPolicy.ExportAsActive:
                                // TODO: this may actually cause errors because exported prims will not exist in
                                // the USD scene graph. Right now, that's too much responsibility on the caller,
                                // because the error messages will be mysterious.

                                // Make the prim inactive.
                                scene.GetPrimAtPath(path).SetActive(false);
                                break;
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        Debug.LogException(new Exception("Error setting visibility: " + path, ex));
                        continue;
                    }

                    UnityEngine.Profiling.Profiler.EndSample();
                } // foreach exporter
            }     // foreach plan

            UnityEngine.Profiling.Profiler.EndSample();
        }