Пример #1
0
        public static void CreatePointInstancerManualTest()
        {
            var scene        = USD.NET.Scene.Create();
            var stageWeakRef = new pxr.UsdStageWeakPtr(scene.Stage);
            var pi           = pxr.UsdGeomPointInstancer.Define(stageWeakRef, new pxr.SdfPath("/Instancer"));
            var cube         = pxr.UsdGeomCube.Define(stageWeakRef, new pxr.SdfPath("/Instancer/Cube"));

            // The cube is the only prototype.
            pi.CreatePrototypesRel().AddTarget(cube.GetPath());

            // Create three instances of the cube (proto index=0).
            var intArray = new pxr.VtIntArray(3, 0);

            pi.CreateProtoIndicesAttr().Set(intArray);

            var mesh        = new pxr.UsdGeomMesh();
            var meshSamples = new USD.NET.Unity.MeshSample[0];

            // Create three positions.
            var vec3fArray = new pxr.VtVec3fArray(3);

            vec3fArray[0] = new pxr.GfVec3f(-2.5f, 0, 0);
            vec3fArray[1] = new pxr.GfVec3f(0, 0, 0);
            vec3fArray[2] = new pxr.GfVec3f(2.5f, 0, 0);
            pi.CreatePositionsAttr().Set(vec3fArray);

            scene.Stage.Export("D:\\instancer.usda");

            //
            // Compute the root transform for each instance.
            //
            var xforms = new pxr.VtMatrix4dArray(3);

            pi.ComputeInstanceTransformsAtTime(xforms, time: 1.0, baseTime: 0.0);

            for (int i = 0; i < xforms.size(); i++)
            {
                Console.WriteLine(xforms[i]);
            }
        }
Пример #2
0
        // ------------------------------------------------------------------------------------------ //
        // General Export Helpers.
        // ------------------------------------------------------------------------------------------ //

        // Copy mesh data to Unity and assign mesh with material.
        void BuildMesh(USD.NET.Unity.MeshSample usdMesh, GameObject go)
        {
            var      mf        = go.AddComponent <MeshFilter>();
            var      mr        = go.AddComponent <MeshRenderer>();
            var      unityMesh = new UnityEngine.Mesh();
            Material mat       = null;// = Material.Instantiate(m_material);

            unityMesh.vertices = usdMesh.points;

            // Triangulate n-gons.
            // For best performance, triangulate off-line and skip conversion.
            var indices = USD.NET.Unity.UnityTypeConverter.ToVtArray(usdMesh.faceVertexIndices);
            var counts  = USD.NET.Unity.UnityTypeConverter.ToVtArray(usdMesh.faceVertexCounts);

            pxr.UsdGeomMesh.Triangulate(indices, counts);
            USD.NET.Unity.UnityTypeConverter.FromVtArray(indices, ref usdMesh.faceVertexIndices);

            if (usdMesh.orientation == Orientation.LeftHanded)
            {
                // USD is right-handed, so the mesh needs to be flipped.
                // Unity is left-handed, but that doesn't matter here.
                for (int i = 0; i < usdMesh.faceVertexIndices.Length; i += 3)
                {
                    int tmp = usdMesh.faceVertexIndices[i];
                    usdMesh.faceVertexIndices[i]     = usdMesh.faceVertexIndices[i + 1];
                    usdMesh.faceVertexIndices[i + 1] = tmp;
                }
            }

            unityMesh.triangles = usdMesh.faceVertexIndices;

            if (usdMesh.extent.size.x > 0 || usdMesh.extent.size.y > 0 || usdMesh.extent.size.z > 0)
            {
                unityMesh.bounds = usdMesh.extent;
            }
            else
            {
                unityMesh.RecalculateBounds();
            }

            if (usdMesh.normals != null)
            {
                unityMesh.normals = usdMesh.normals;
            }
            else
            {
                unityMesh.RecalculateNormals();
            }

            if (usdMesh.tangents != null)
            {
                unityMesh.tangents = usdMesh.tangents;
            }
            else
            {
                unityMesh.RecalculateTangents();
            }

            if (usdMesh.colors != null)
            {
                // NOTE: The following color conversion assumes PlayerSettings.ColorSpace == Linear.
                // For best performance, convert color space to linear off-line and skip conversion.

                if (usdMesh.colors.Length == 1)
                {
                    // Constant color can just be set on the material.
                    mat.color = usdMesh.colors[0].gamma;
                }
                else if (usdMesh.colors.Length == usdMesh.points.Length)
                {
                    // Vertex colors map on to verts.
                    // TODO: move the conversion to C++ and use the color management API.
                    for (int i = 0; i < usdMesh.colors.Length; i++)
                    {
                        usdMesh.colors[i] = usdMesh.colors[i].gamma;
                    }

                    unityMesh.colors = usdMesh.colors;
                }
                else
                {
                    // FaceVarying and uniform both require breaking up the mesh and are not yet handled in
                    // this example.
                    Debug.LogWarning("Uniform (color per face) and FaceVarying (color per vert per face) "
                                     + "display color not supported in this example");
                }
            }

            // As in Unity, UVs are a dynamic type which can be vec2, vec3, or vec4.
            if (usdMesh.uv != null)
            {
                Type uvType = usdMesh.uv.GetType();
                if (uvType == typeof(Vector2[]))
                {
                    unityMesh.SetUVs(0, ((Vector2[])usdMesh.uv).ToList());
                }
                else if (uvType == typeof(Vector3[]))
                {
                    unityMesh.SetUVs(0, ((Vector3[])usdMesh.uv).ToList());
                }
                else if (uvType == typeof(Vector4[]))
                {
                    unityMesh.SetUVs(0, ((Vector4[])usdMesh.uv).ToList());
                }
                else
                {
                    throw new Exception("Unexpected UV type: " + usdMesh.uv.GetType());
                }
            }

            mr.material = mat;
            mf.mesh     = unityMesh;
        }