Exemplo n.º 1
0
        static void LoadPrimvars(Scene scene,
                                 Mesh unityMesh,
                                 string usdMeshPath,
                                 List <string> primvars,
                                 int[] faceVertexCounts,
                                 int[] faceVertexIndices)
        {
            if (primvars == null || primvars.Count == 0)
            {
                return;
            }
            var prim = scene.GetPrimAtPath(usdMeshPath);

            for (int i = 0; i < primvars.Count; i++)
            {
                var attr = prim.GetAttribute(new TfToken("primvars:" + primvars[i]));
                if (!attr)
                {
                    continue;
                }

                // Read the raw values.
                VtValue val = attr.Get(0.0);
                if (val.IsEmpty())
                {
                    continue;
                }
                VtVec2fArray vec2fArray = UsdCs.VtValueToVtVec2fArray(val);
                Vector2[]    values     = UnityTypeConverter.FromVtArray(vec2fArray);

                // Unroll indexed primvars.
                var        pv        = new UsdGeomPrimvar(attr);
                VtIntArray vtIndices = new VtIntArray();
                if (pv.GetIndices(vtIndices, 0.0))
                {
                    int[] indices = UnityTypeConverter.FromVtArray(vtIndices);
                    values = indices.Select(idx => values[idx]).ToArray();
                }

                // Handle primvar interpolation modes.
                TfToken interp = pv.GetInterpolation();
                if (interp == UsdGeomTokens.constant)
                {
                    Debug.Assert(values.Length == 1);
                    var newValues = new Vector2[unityMesh.vertexCount];
                    for (int idx = 0; idx < values.Length; idx++)
                    {
                        newValues[idx] = values[0];
                    }
                    values = newValues;
                }
                else if (interp == UsdGeomTokens.uniform)
                {
                    Debug.Assert(values.Length == faceVertexCounts.Length);
                    for (int faceIndex = 0; faceIndex < values.Length; faceIndex++)
                    {
                        var faceColor = values[faceIndex];
                        int idx       = 0;
                        var newValues = new Vector2[unityMesh.vertexCount];
                        for (int f = 0; f < faceVertexCounts[faceIndex]; f++)
                        {
                            int vertexInFaceIdx = faceVertexIndices[idx++];
                            newValues[vertexInFaceIdx] = faceColor;
                        }
                        values = newValues;
                    }
                }
                else if (interp == UsdGeomTokens.faceVarying)
                {
                    values = UnrollFaceVarying(unityMesh.vertexCount, values, faceVertexCounts, faceVertexIndices);
                }

                // Send them to Unity.
                unityMesh.SetUVs(i, values.ToList());
            }
        }