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); }
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); }
public static void WriteSparseOverrides(Scene scene, PrimMap primMap, BasisTransformation changeHandedness, float tolerance = 0.0001f) { var oldMode = scene.WriteMode; scene.WriteMode = Scene.WriteModes.Over; try { foreach (var path in scene.Find <XformableSample>()) { GameObject go; if (!primMap.TryGetValue(path, out go)) { continue; } var tx = go.transform; var xfNew = XformSample.FromTransform(tx); var xfOld = new XformSample(); scene.Read(path, xfOld); bool areClose = true; for (int i = 0; i < 16; i++) { if (Mathf.Abs(xfNew.transform[i] - xfOld.transform[i]) > tolerance) { areClose = false; break; } } if (areClose) { continue; } if (changeHandedness == BasisTransformation.SlowAndSafe) { xfNew.ConvertTransform(); } scene.Write(path, xfNew); } } finally { scene.WriteMode = oldMode; } }
/// Authors a USD Xform prim at the given path with the given matrix transform. static void CreateXform(USD.NET.Scene scene, string path, Matrix4x4?xform = null) { var sample = new XformSample(); if (xform.HasValue) { sample.transform = xform.Value; } else { sample.transform = Matrix4x4.identity; } scene.Write(path, sample); }
public void UsdAsset_Reload_FileDidNotChange_ValuesDoNotChange() { var xform = new XformSample(); m_usdAsset.GetScene().Read("/TestPrim", xform); var translate = xform.transform.GetColumn(3); Assert.AreEqual(new Vector4(1, 1, 1, 1), translate); // Refresh the asset. m_usdAsset.Reload(false); // Ensure that nothing changed. m_usdAsset.GetScene().Read("/TestPrim", xform); translate = xform.transform.GetColumn(3); Assert.AreEqual(new Vector4(1, 1, 1, 1), translate); }
public IEnumerator UsdRecorder_Records() { var sampleT = 3f; director.Play(); while (Time.time < director.duration) { yield return(null); } var scene = USD.NET.Scene.Open(usdSettings.OutputFile + ".usd"); var sample = new XformSample(); scene.Time = sampleT * scene.FrameRate; // Frames not seconds scene.Read("/Cube", sample); Assert.That(sampleT, Is.EqualTo(sample.transform.m03).Within(1e-5)); }
bool InitExportableParents(GameObject go) { if (m_primMap.ContainsKey(go)) { // Stop processing parents, this keeps the performance of the traversal linear. return(false); } // Any object we add will only be exported as an Xform. string path = UnityTypeConverter.GetPath(go.transform); SampleBase sample = new XformSample(); m_primMap.Add(go, new ExportPlan { path = path, sample = sample, exportFunc = ExportXform }); Debug.Log(path + " " + sample.GetType().Name); // Continue processing parents. return(true); }
public IEnumerator UsdAsset_Reload_FileHasChangedAndForceRebuild_NewValuesAreRetrieved() { var xform = new XformSample(); m_usdAsset.GetScene().Read("/TestPrim", xform); var translate = xform.transform.GetColumn(3); Assert.AreEqual(new Vector4(1, 1, 1, 1), translate); yield return(new WaitForSecondsRealtime(1f)); // Simulate the fact that the usd file was changed on disk. UpdateTestFile(); // Reload the asset. m_usdAsset.Reload(true); // Ensure the new attribute's values can be retrieved. m_usdAsset.GetScene().Read("/TestPrim", xform); translate = xform.transform.GetColumn(3); Assert.AreEqual(new Vector4(2, 2, 2, 1), translate); yield return(new WaitForSecondsRealtime(1f)); }
public static void ExportXform(ObjectContext objContext, ExportContext exportContext) { UnityEngine.Profiling.Profiler.BeginSample("USD: Xform Conversion"); XformSample sample = (XformSample)objContext.sample; var localRot = objContext.gameObject.transform.localRotation; var localScale = objContext.gameObject.transform.localScale; var path = new pxr.SdfPath(objContext.path); // If exporting for Z-Up, rotate the world. bool correctZUp = exportContext.scene.UpAxis == Scene.UpAxes.Z; sample.transform = GetLocalTransformMatrix( objContext.gameObject.transform, correctZUp, path.IsRootPrimPath(), exportContext.basisTransform); UnityEngine.Profiling.Profiler.EndSample(); UnityEngine.Profiling.Profiler.BeginSample("USD: Xform Write"); exportContext.scene.Write(objContext.path, sample); UnityEngine.Profiling.Profiler.EndSample(); }