Esempio n. 1
0
    private OVRMaterialData ProcessMaterial(int matId)
    {
        OVRMaterialData matData = new OVRMaterialData();

        var jsonMaterial   = m_jsonData["materials"][matId];
        var jsonPbrDetails = jsonMaterial["pbrMetallicRoughness"];

        var jsonBaseColor = jsonPbrDetails["baseColorTexture"];

        if (jsonBaseColor != null)
        {
            int textureId = jsonBaseColor["index"].AsInt;
            matData.textureId = textureId;
        }
        else
        {
            var jsonTextrure = jsonMaterial["emissiveTexture"];
            if (jsonTextrure != null)
            {
                int textureId = jsonTextrure["index"].AsInt;
                matData.textureId = textureId;
            }
        }

        matData.shader = m_Shader;
        return(matData);
    }
Esempio n. 2
0
    private Material CreateUnityMaterial(OVRMaterialData matData, bool loadMips)
    {
        Material mat = new Material(matData.shader);

        if (matData.texture.format == OVRTextureFormat.KTX2)
        {
            Texture2D texture;
            texture = new Texture2D(matData.texture.width, matData.texture.height, matData.texture.transcodedFormat, loadMips);
            texture.LoadRawTextureData(matData.texture.data);
            texture.Apply(false, true);
            mat.mainTexture = texture;
        }
        return(mat);
    }
Esempio n. 3
0
    private OVRMeshData ProcessMesh(JSONNode meshNode, bool loadMips)
    {
        OVRMeshData meshData = new OVRMeshData();

        int totalVertexCount = 0;
        var primitives       = meshNode["primitives"];

        int[] primitiveVertexCounts = new int[primitives.Count];
        for (int i = 0; i < primitives.Count; i++)
        {
            var jsonPrimitive = primitives[i];
            var jsonAttrbite  = jsonPrimitive["attributes"]["POSITION"];
            var jsonAccessor  = m_jsonData["accessors"][jsonAttrbite.AsInt];

            primitiveVertexCounts[i] = jsonAccessor["count"];
            totalVertexCount        += primitiveVertexCounts[i];
        }

        int[][]   indicies = new int[primitives.Count][];
        Vector3[] vertices = new Vector3[totalVertexCount];

        Vector3[] normals = null;
        if (primitives[0]["attributes"]["NORMAL"] != null)
        {
            normals = new Vector3[totalVertexCount];
        }

        Vector4[] tangents = null;
        if (primitives[0]["attributes"]["TANGENT"] != null)
        {
            tangents = new Vector4[totalVertexCount];
        }

        Vector2[] texcoords = null;
        if (primitives[0]["attributes"]["TEXCOORD_0"] != null)
        {
            texcoords = new Vector2[totalVertexCount];
        }

        Color[] colors = null;
        if (primitives[0]["attributes"]["COLOR_0"] != null)
        {
            colors = new Color[totalVertexCount];
        }

        BoneWeight[] boneWeights = null;
        if (primitives[0]["attributes"]["WEIGHTS_0"] != null)
        {
            boneWeights = new BoneWeight[totalVertexCount];
        }

        // Begin async processing of material and its texture
        OVRMaterialData matData       = default(OVRMaterialData);
        Task            transcodeTask = null;
        var             jsonMaterial  = primitives[0]["material"];

        if (jsonMaterial != null)
        {
            matData         = ProcessMaterial(jsonMaterial.AsInt);
            matData.texture = ProcessTexture(matData.textureId);
            transcodeTask   = Task.Run(() => { TranscodeTexture(ref matData.texture); });
        }

        int vertexOffset = 0;

        for (int i = 0; i < primitives.Count; i++)
        {
            var jsonPrimitive = primitives[i];

            int             indicesAccessorId = jsonPrimitive["indices"].AsInt;
            var             jsonAccessor      = m_jsonData["accessors"][indicesAccessorId];
            OVRGLTFAccessor indicesReader     = new OVRGLTFAccessor(jsonAccessor, m_jsonData);

            indicies[i] = new int[indicesReader.GetDataCount()];
            indicesReader.ReadAsInt(m_binaryChunk, ref indicies[i], 0);
            FlipTraingleIndices(ref indicies[i]);

            var jsonAttribute = jsonPrimitive["attributes"]["POSITION"];
            if (jsonAttribute != null)
            {
                jsonAccessor = m_jsonData["accessors"][jsonAttribute.AsInt];
                OVRGLTFAccessor dataReader = new OVRGLTFAccessor(jsonAccessor, m_jsonData);
                dataReader.ReadAsVector3(m_binaryChunk, ref vertices, vertexOffset, GLTFToUnitySpace);
            }

            jsonAttribute = jsonPrimitive["attributes"]["NORMAL"];
            if (jsonAttribute != null)
            {
                jsonAccessor = m_jsonData["accessors"][jsonAttribute.AsInt];
                OVRGLTFAccessor dataReader = new OVRGLTFAccessor(jsonAccessor, m_jsonData);
                dataReader.ReadAsVector3(m_binaryChunk, ref normals, vertexOffset, GLTFToUnitySpace);
            }

            jsonAttribute = jsonPrimitive["attributes"]["TANGENT"];
            if (jsonAttribute != null)
            {
                jsonAccessor = m_jsonData["accessors"][jsonAttribute.AsInt];
                OVRGLTFAccessor dataReader = new OVRGLTFAccessor(jsonAccessor, m_jsonData);
                dataReader.ReadAsVector4(m_binaryChunk, ref tangents, vertexOffset, GLTFToUnityTangent);
            }

            jsonAttribute = jsonPrimitive["attributes"]["TEXCOORD_0"];
            if (jsonAttribute != null)
            {
                jsonAccessor = m_jsonData["accessors"][jsonAttribute.AsInt];
                OVRGLTFAccessor dataReader = new OVRGLTFAccessor(jsonAccessor, m_jsonData);
                dataReader.ReadAsVector2(m_binaryChunk, ref texcoords, vertexOffset);
            }

            jsonAttribute = jsonPrimitive["attributes"]["COLOR_0"];
            if (jsonAttribute != null)
            {
                jsonAccessor = m_jsonData["accessors"][jsonAttribute.AsInt];
                OVRGLTFAccessor dataReader = new OVRGLTFAccessor(jsonAccessor, m_jsonData);
                dataReader.ReadAsColor(m_binaryChunk, ref colors, vertexOffset);
            }

            jsonAttribute = jsonPrimitive["attributes"]["WEIGHTS_0"];
            if (jsonAttribute != null)
            {
                jsonAccessor = m_jsonData["accessors"][jsonAttribute.AsInt];
                OVRGLTFAccessor weightReader = new OVRGLTFAccessor(jsonAccessor, m_jsonData);

                var             jointAttribute = jsonPrimitive["attributes"]["JOINTS_0"];
                var             jointAccessor  = m_jsonData["accessors"][jointAttribute.AsInt];
                OVRGLTFAccessor jointReader    = new OVRGLTFAccessor(jointAccessor, m_jsonData);

                Vector4[] weights = new Vector4[weightReader.GetDataCount()];
                Vector4[] joints  = new Vector4[jointReader.GetDataCount()];

                weightReader.ReadAsBoneWeights(m_binaryChunk, ref weights, 0);
                jointReader.ReadAsVector4(m_binaryChunk, ref joints, 0, Vector4.one);

                for (int w = 0; w < weights.Length; w++)
                {
                    boneWeights[vertexOffset + w].boneIndex0 = (int)joints[w].x;
                    boneWeights[vertexOffset + w].boneIndex1 = (int)joints[w].y;
                    boneWeights[vertexOffset + w].boneIndex2 = (int)joints[w].z;
                    boneWeights[vertexOffset + w].boneIndex3 = (int)joints[w].w;

                    boneWeights[vertexOffset + w].weight0 = weights[w].x;
                    boneWeights[vertexOffset + w].weight1 = weights[w].y;
                    boneWeights[vertexOffset + w].weight2 = weights[w].z;
                    boneWeights[vertexOffset + w].weight3 = weights[w].w;
                }
            }

            vertexOffset += primitiveVertexCounts[i];
        }

        Mesh mesh = new Mesh();

        mesh.vertices     = vertices;
        mesh.normals      = normals;
        mesh.tangents     = tangents;
        mesh.colors       = colors;
        mesh.uv           = texcoords;
        mesh.boneWeights  = boneWeights;
        mesh.subMeshCount = primitives.Count;

        int baseVertex = 0;

        for (int i = 0; i < primitives.Count; i++)
        {
            mesh.SetIndices(indicies[i], MeshTopology.Triangles, i, false, baseVertex);
            baseVertex += primitiveVertexCounts[i];
        }

        mesh.RecalculateBounds();
        meshData.mesh = mesh;

        if (transcodeTask != null)
        {
            transcodeTask.Wait();
            meshData.material = CreateUnityMaterial(matData, loadMips);
        }
        return(meshData);
    }