Exemplo n.º 1
0
    private IEnumerator FreeRenderModel(IntPtr pRenderModel)
    {
        yield return(new WaitForSeconds(1f));

        using (SteamVR_RenderModel.RenderModelInterfaceHolder renderModelInterfaceHolder = new SteamVR_RenderModel.RenderModelInterfaceHolder())
        {
            CVRRenderModels instance = renderModelInterfaceHolder.instance;
            instance.FreeRenderModel(pRenderModel);
        }
        yield break;
    }
Exemplo n.º 2
0
    static RenderModel LoadRenderModel(string renderModelName)
    {
        var error = HmdError.None;

        if (!SteamVR.active)
        {
            OpenVR.Init(ref error);
            if (error != HmdError.None)
            {
                return(null);
            }
        }

        var pRenderModels = OpenVR.GetGenericInterface(OpenVR.IVRRenderModels_Version, ref error);

        if (pRenderModels == System.IntPtr.Zero || error != HmdError.None)
        {
            if (!SteamVR.active)
            {
                OpenVR.Shutdown();
            }
            return(null);
        }

        var renderModels = new CVRRenderModels(pRenderModels);

        var renderModel = new RenderModel_t();

        if (!renderModels.LoadRenderModel(renderModelName, ref renderModel))
        {
            Debug.LogError("Failed to load render model " + renderModelName);

            if (!SteamVR.active)
            {
                OpenVR.Shutdown();
            }
            return(null);
        }

        var vertices = new Vector3[renderModel.unVertexCount];
        var normals  = new Vector3[renderModel.unVertexCount];
        var uv       = new Vector2[renderModel.unVertexCount];

        var type = typeof(RenderModel_Vertex_t);

        for (int iVert = 0; iVert < renderModel.unVertexCount; iVert++)
        {
            var ptr  = new System.IntPtr(renderModel.rVertexData.ToInt64() + iVert * Marshal.SizeOf(type));
            var vert = (RenderModel_Vertex_t)Marshal.PtrToStructure(ptr, type);

            vertices[iVert] = new Vector3(vert.vPosition.v[0], vert.vPosition.v[1], -vert.vPosition.v[2]);
            normals[iVert]  = new Vector3(vert.vNormal.v[0], vert.vNormal.v[1], -vert.vNormal.v[2]);
            uv[iVert]       = new Vector2(vert.rfTextureCoord[0], vert.rfTextureCoord[1]);
        }

        int indexCount = (int)renderModel.unTriangleCount * 3;
        var indices    = new short[indexCount];

        Marshal.Copy(renderModel.rIndexData, indices, 0, indices.Length);

        var triangles = new int[indexCount];

        for (int iTri = 0; iTri < renderModel.unTriangleCount; iTri++)
        {
            triangles[iTri * 3 + 0] = (int)indices[iTri * 3 + 2];
            triangles[iTri * 3 + 1] = (int)indices[iTri * 3 + 1];
            triangles[iTri * 3 + 2] = (int)indices[iTri * 3 + 0];
        }

        var mesh = new Mesh();

        mesh.vertices  = vertices;
        mesh.normals   = normals;
        mesh.uv        = uv;
        mesh.triangles = triangles;

        mesh.Optimize();
        //mesh.hideFlags = HideFlags.DontUnloadUnusedAsset;

        var textureMapData = new byte[renderModel.diffuseTexture.unWidth * renderModel.diffuseTexture.unHeight * 4];         // RGBA

        Marshal.Copy(renderModel.diffuseTexture.rubTextureMapData, textureMapData, 0, textureMapData.Length);

        var colors = new Color32[renderModel.diffuseTexture.unWidth * renderModel.diffuseTexture.unHeight];
        int iColor = 0;

        for (int iHeight = 0; iHeight < renderModel.diffuseTexture.unHeight; iHeight++)
        {
            for (int iWidth = 0; iWidth < renderModel.diffuseTexture.unWidth; iWidth++)
            {
                var r = textureMapData[iColor++];
                var g = textureMapData[iColor++];
                var b = textureMapData[iColor++];
                var a = textureMapData[iColor++];
                colors[iHeight * renderModel.diffuseTexture.unWidth + iWidth] = new Color32(r, g, b, a);
            }
        }

        var texture = new Texture2D(renderModel.diffuseTexture.unWidth, renderModel.diffuseTexture.unHeight, TextureFormat.ARGB32, true);

        texture.SetPixels32(colors);
        texture.Apply();

        //texture.hideFlags = HideFlags.DontUnloadUnusedAsset;

        renderModels.FreeRenderModel(ref renderModel);

        if (!SteamVR.active)
        {
            OpenVR.Shutdown();
        }

        return(new RenderModel(mesh, texture));
    }
Exemplo n.º 3
0
    RenderModel LoadRenderModel(CVRRenderModels renderModels, string renderModelName, string baseName)
    {
        var pRenderModel = System.IntPtr.Zero;

        EVRRenderModelError error;

        while (true)
        {
            error = renderModels.LoadRenderModel_Async(renderModelName, ref pRenderModel);
            if (error != EVRRenderModelError.Loading)
            {
                break;
            }

            System.Threading.Thread.Sleep(1);
        }

        if (error != EVRRenderModelError.None)
        {
            Debug.LogError(string.Format("Failed to load render model {0} - {1}", renderModelName, error.ToString()));
            return(null);
        }

        var renderModel = (RenderModel_t)Marshal.PtrToStructure(pRenderModel, typeof(RenderModel_t));

        var vertices = new Vector3[renderModel.unVertexCount];
        var normals  = new Vector3[renderModel.unVertexCount];
        var uv       = new Vector2[renderModel.unVertexCount];

        var type = typeof(RenderModel_Vertex_t);

        for (int iVert = 0; iVert < renderModel.unVertexCount; iVert++)
        {
            var ptr  = new System.IntPtr(renderModel.rVertexData.ToInt64() + iVert * Marshal.SizeOf(type));
            var vert = (RenderModel_Vertex_t)Marshal.PtrToStructure(ptr, type);

            vertices[iVert] = new Vector3(vert.vPosition.v0, vert.vPosition.v1, -vert.vPosition.v2);
            normals[iVert]  = new Vector3(vert.vNormal.v0, vert.vNormal.v1, -vert.vNormal.v2);
            uv[iVert]       = new Vector2(vert.rfTextureCoord0, vert.rfTextureCoord1);
        }

        int indexCount = (int)renderModel.unTriangleCount * 3;
        var indices    = new short[indexCount];

        Marshal.Copy(renderModel.rIndexData, indices, 0, indices.Length);

        var triangles = new int[indexCount];

        for (int iTri = 0; iTri < renderModel.unTriangleCount; iTri++)
        {
            triangles[iTri * 3 + 0] = (int)indices[iTri * 3 + 2];
            triangles[iTri * 3 + 1] = (int)indices[iTri * 3 + 1];
            triangles[iTri * 3 + 2] = (int)indices[iTri * 3 + 0];
        }

        var mesh = new Mesh();

        mesh.vertices  = vertices;
        mesh.normals   = normals;
        mesh.uv        = uv;
        mesh.triangles = triangles;

#if (UNITY_5_4 || UNITY_5_3 || UNITY_5_2 || UNITY_5_1 || UNITY_5_0)
        mesh.Optimize();
#endif
        //mesh.hideFlags = HideFlags.DontUnloadUnusedAsset;

        // Check cache before loading texture.
        var material = materials[renderModel.diffuseTextureId] as Material;
        if (material == null || material.mainTexture == null)
        {
            var pDiffuseTexture = System.IntPtr.Zero;

            while (true)
            {
                error = renderModels.LoadTexture_Async(renderModel.diffuseTextureId, ref pDiffuseTexture);
                if (error != EVRRenderModelError.Loading)
                {
                    break;
                }

                System.Threading.Thread.Sleep(1);
            }

            if (error == EVRRenderModelError.None)
            {
                var diffuseTexture = (RenderModel_TextureMap_t)Marshal.PtrToStructure(pDiffuseTexture, typeof(RenderModel_TextureMap_t));
                var texture        = new Texture2D(diffuseTexture.unWidth, diffuseTexture.unHeight, TextureFormat.ARGB32, false);
                if (SystemInfo.graphicsDeviceVersion.StartsWith("Direct3D 11"))
                {
                    texture.Apply();

                    while (true)
                    {
                        error = renderModels.LoadIntoTextureD3D11_Async(renderModel.diffuseTextureId, texture.GetNativeTexturePtr());
                        if (error != EVRRenderModelError.Loading)
                        {
                            break;
                        }

                        System.Threading.Thread.Sleep(1);
                    }
                }
                else
                {
                    var textureMapData = new byte[diffuseTexture.unWidth * diffuseTexture.unHeight * 4];                     // RGBA
                    Marshal.Copy(diffuseTexture.rubTextureMapData, textureMapData, 0, textureMapData.Length);

                    var colors = new Color32[diffuseTexture.unWidth * diffuseTexture.unHeight];
                    int iColor = 0;
                    for (int iHeight = 0; iHeight < diffuseTexture.unHeight; iHeight++)
                    {
                        for (int iWidth = 0; iWidth < diffuseTexture.unWidth; iWidth++)
                        {
                            var r = textureMapData[iColor++];
                            var g = textureMapData[iColor++];
                            var b = textureMapData[iColor++];
                            var a = textureMapData[iColor++];
                            colors[iHeight * diffuseTexture.unWidth + iWidth] = new Color32(r, g, b, a);
                        }
                    }

                    texture.SetPixels32(colors);
                    texture.Apply();
                }

                material             = new Material(shader != null ? shader : Shader.Find("Standard"));
                material.mainTexture = texture;
                //material.hideFlags = HideFlags.DontUnloadUnusedAsset;

                materials[renderModel.diffuseTextureId] = material;

                renderModels.FreeTexture(pDiffuseTexture);
            }
            else
            {
                Debug.Log("Failed to load render model texture for render model " + renderModelName);
            }
        }

        // Delay freeing when we can since we'll often get multiple requests for the same model right
        // after another (e.g. two controllers or two basestations).
#if UNITY_EDITOR
        if (!Application.isPlaying)
        {
            renderModels.FreeRenderModel(pRenderModel);
        }
        else
#endif
        StartCoroutine(FreeRenderModel(pRenderModel));

        return(new RenderModel(mesh, material));
    }
    static RenderModel LoadRenderModel(CVRRenderModels renderModels, string renderModelName, string baseName)
    {
        var pRenderModel = System.IntPtr.Zero;

        if (!renderModels.LoadRenderModel(renderModelName, ref pRenderModel))
        {
            Debug.LogError("Failed to load render model " + renderModelName);
            return(null);
        }

        var renderModel = (RenderModel_t)Marshal.PtrToStructure(pRenderModel, typeof(RenderModel_t));

        var vertices = new Vector3[renderModel.unVertexCount];
        var normals  = new Vector3[renderModel.unVertexCount];
        var uv       = new Vector2[renderModel.unVertexCount];

        var type = typeof(RenderModel_Vertex_t);

        for (int iVert = 0; iVert < renderModel.unVertexCount; iVert++)
        {
            var ptr  = new System.IntPtr(renderModel.rVertexData.ToInt64() + iVert * Marshal.SizeOf(type));
            var vert = (RenderModel_Vertex_t)Marshal.PtrToStructure(ptr, type);

            vertices[iVert] = new Vector3(vert.vPosition.v[0], vert.vPosition.v[1], -vert.vPosition.v[2]);
            normals[iVert]  = new Vector3(vert.vNormal.v[0], vert.vNormal.v[1], -vert.vNormal.v[2]);
            uv[iVert]       = new Vector2(vert.rfTextureCoord[0], vert.rfTextureCoord[1]);
        }

        int indexCount = (int)renderModel.unTriangleCount * 3;
        var indices    = new short[indexCount];

        Marshal.Copy(renderModel.rIndexData, indices, 0, indices.Length);

        var triangles = new int[indexCount];

        for (int iTri = 0; iTri < renderModel.unTriangleCount; iTri++)
        {
            triangles[iTri * 3 + 0] = (int)indices[iTri * 3 + 2];
            triangles[iTri * 3 + 1] = (int)indices[iTri * 3 + 1];
            triangles[iTri * 3 + 2] = (int)indices[iTri * 3 + 0];
        }

        var mesh = new Mesh();

        mesh.vertices  = vertices;
        mesh.normals   = normals;
        mesh.uv        = uv;
        mesh.triangles = triangles;

        mesh.Optimize();
        //mesh.hideFlags = HideFlags.DontUnloadUnusedAsset;

        // Check cache before loading texture.
        var material = materials[baseName + renderModel.diffuseTextureId] as Material;

        if (material == null || material.mainTexture == null)
        {
            var pDiffuseTexture = System.IntPtr.Zero;
            if (renderModels.LoadTexture(renderModel.diffuseTextureId, ref pDiffuseTexture))
            {
                var diffuseTexture = (RenderModel_TextureMap_t)Marshal.PtrToStructure(pDiffuseTexture, typeof(RenderModel_TextureMap_t));

                var textureMapData = new byte[diffuseTexture.unWidth * diffuseTexture.unHeight * 4];                 // RGBA
                Marshal.Copy(diffuseTexture.rubTextureMapData, textureMapData, 0, textureMapData.Length);

                var colors = new Color32[diffuseTexture.unWidth * diffuseTexture.unHeight];
                int iColor = 0;
                for (int iHeight = 0; iHeight < diffuseTexture.unHeight; iHeight++)
                {
                    for (int iWidth = 0; iWidth < diffuseTexture.unWidth; iWidth++)
                    {
                        var r = textureMapData[iColor++];
                        var g = textureMapData[iColor++];
                        var b = textureMapData[iColor++];
                        var a = textureMapData[iColor++];
                        colors[iHeight * diffuseTexture.unWidth + iWidth] = new Color32(r, g, b, a);
                    }
                }

                var texture = new Texture2D(diffuseTexture.unWidth, diffuseTexture.unHeight, TextureFormat.ARGB32, true);
                texture.SetPixels32(colors);
                texture.Apply();

                material             = new Material(Shader.Find("Standard"));
                material.mainTexture = texture;
                //material.hideFlags = HideFlags.DontUnloadUnusedAsset;

                materials[baseName + renderModel.diffuseTextureId] = material;
            }
            else
            {
                Debug.Log("Failed to load render model texture for render model " + renderModelName);
            }
        }

        renderModels.FreeRenderModel(ref renderModel);

        return(new RenderModel(mesh, material));
    }