Exemplo n.º 1
0
    public static Mesh CreateHiddenAreaMesh(HiddenAreaMesh_t src, Compositor_TextureBounds bounds)
    {
        if (src.unTriangleCount == 0)
            return null;

        var data = new float[src.unTriangleCount * 3 * 2]; //HmdVector2_t
        Marshal.Copy(src.pVertexData, data, 0, data.Length);

        var vertices = new Vector3[src.unTriangleCount * 3 + 12];
        var indices = new int[src.unTriangleCount * 3 + 24];

        var x0 = 2.0f * bounds.uMin - 1.0f;
        var x1 = 2.0f * bounds.uMax - 1.0f;
        var y0 = 2.0f * bounds.vMax - 1.0f;
        var y1 = 2.0f * bounds.vMin - 1.0f;

        for (int i = 0, j = 0; i < src.unTriangleCount * 3; i++)
        {
            var x = Lerp(x0, x1, data[j++]);
            var y = Lerp(y0, y1, data[j++]);
            vertices[i] = new Vector3(x, y, 0.0f);
            indices[i] = i;
        }

        // Add border
        var offset = (int)src.unTriangleCount * 3;
        var iVert = offset;
        vertices[iVert++] = new Vector3(-1, -1, 0);
        vertices[iVert++] = new Vector3(x0, -1, 0);
        vertices[iVert++] = new Vector3(-1,  1, 0);
        vertices[iVert++] = new Vector3(x0,  1, 0);
        vertices[iVert++] = new Vector3(x1, -1, 0);
        vertices[iVert++] = new Vector3( 1, -1, 0);
        vertices[iVert++] = new Vector3(x1,  1, 0);
        vertices[iVert++] = new Vector3( 1,  1, 0);
        vertices[iVert++] = new Vector3(x0, y0, 0);
        vertices[iVert++] = new Vector3(x1, y0, 0);
        vertices[iVert++] = new Vector3(x0, y1, 0);
        vertices[iVert++] = new Vector3(x1, y1, 0);

        var iTri = offset;
        indices[iTri++] = offset + 0;
        indices[iTri++] = offset + 1;
        indices[iTri++] = offset + 2;
        indices[iTri++] = offset + 2;
        indices[iTri++] = offset + 1;
        indices[iTri++] = offset + 3;
        indices[iTri++] = offset + 4;
        indices[iTri++] = offset + 5;
        indices[iTri++] = offset + 6;
        indices[iTri++] = offset + 6;
        indices[iTri++] = offset + 5;
        indices[iTri++] = offset + 7;
        indices[iTri++] = offset + 1;
        indices[iTri++] = offset + 4;
        indices[iTri++] = offset + 8;
        indices[iTri++] = offset + 8;
        indices[iTri++] = offset + 4;
        indices[iTri++] = offset + 9;
        indices[iTri++] = offset + 10;
        indices[iTri++] = offset + 11;
        indices[iTri++] = offset + 3;
        indices[iTri++] = offset + 3;
        indices[iTri++] = offset + 11;
        indices[iTri++] = offset + 6;

        var mesh = new Mesh();
        mesh.vertices = vertices;
        mesh.triangles = indices;
        return mesh;
    }
Exemplo n.º 2
0
internal static extern void SteamAPI_vr_IVRCompositor_Submit(IntPtr instancePtr, Hmd_Eye eEye, IntPtr pTexture, ref Compositor_TextureBounds pBounds);
Exemplo n.º 3
0
    private SteamVR(System.IntPtr pHmd, System.IntPtr pCompositor)
    {
        hmd = new CVRSystem(pHmd);
        Debug.Log("Connected to " + hmd_TrackingSystemName + ":" + hmd_SerialNumber);

        compositor = new CVRCompositor(pCompositor);

        var device = new UnityGraphicsDevice();
        GetUnityGraphicsDevice(ref device);
        switch (device.type)
        {
            case GfxDeviceRenderer.kGfxRendererD3D11:
                compositor.SetGraphicsDevice(Compositor_DeviceType.D3D11, device.ptr);
                break;
            case GfxDeviceRenderer.kGfxRendererOpenGL:
                compositor.SetGraphicsDevice(Compositor_DeviceType.OpenGL, device.ptr);
                break;
            default:
                throw new System.Exception("Unsupported device type.");
        }

        var capacity = compositor.GetLastError(null, 0);
        if (capacity > 1)
        {
            var result = new System.Text.StringBuilder((int)capacity);
            compositor.GetLastError(result, capacity);
            Debug.Log("Compositor - " + result);
        }

        // Register for a callback if our graphics device goes away, so we can properly clean up.
        var resetDelegate = new UnityResetDelegate(SteamVR.SafeDispose);
        callbackHandle = GCHandle.Alloc(resetDelegate);
        SetUnityResetCallback(Marshal.GetFunctionPointerForDelegate(resetDelegate));

        // Hook up the render thread present event just in case we wind up needing to use this.
        var error = HmdError.None;
        SetUnityRenderCallback(OpenVR.GetGenericInterface(IVRHmdDistortPresent_Version, ref error));

        // Setup render values
        uint w = 0, h = 0;
        hmd.GetRecommendedRenderTargetSize(ref w, ref h);
        sceneWidth = (float)w;
        sceneHeight = (float)h;

        float l_left = 0.0f, l_right = 0.0f, l_top = 0.0f, l_bottom = 0.0f;
        hmd.GetProjectionRaw(Hmd_Eye.Eye_Left, ref l_left, ref l_right, ref l_top, ref l_bottom);

        float r_left = 0.0f, r_right = 0.0f, r_top = 0.0f, r_bottom = 0.0f;
        hmd.GetProjectionRaw(Hmd_Eye.Eye_Right, ref r_left, ref r_right, ref r_top, ref r_bottom);

        tanHalfFov = new Vector2(
            Mathf.Max(-l_left, l_right, -r_left, r_right),
            Mathf.Max(-l_top, l_bottom, -r_top, r_bottom));

        textureBounds = new Compositor_TextureBounds[2];

        textureBounds[0].uMin = 0.5f + 0.5f * l_left / tanHalfFov.x;
        textureBounds[0].uMax = 0.5f + 0.5f * l_right / tanHalfFov.x;
        textureBounds[0].vMin = 0.5f + 0.5f * l_bottom / tanHalfFov.y;
        textureBounds[0].vMax = 0.5f + 0.5f * l_top / tanHalfFov.y;

        textureBounds[1].uMin = 0.5f + 0.5f * r_left / tanHalfFov.x;
        textureBounds[1].uMax = 0.5f + 0.5f * r_right / tanHalfFov.x;
        textureBounds[1].vMin = 0.5f + 0.5f * r_bottom / tanHalfFov.y;
        textureBounds[1].vMax = 0.5f + 0.5f * r_top / tanHalfFov.y;

        aspect = tanHalfFov.x / tanHalfFov.y;
        fieldOfView = 2.0f * Mathf.Atan(tanHalfFov.y) * Mathf.Rad2Deg;

        eyes = new SteamVR_Utils.RigidTransform[] {
            new SteamVR_Utils.RigidTransform(hmd.GetEyeToHeadTransform(Hmd_Eye.Eye_Left)),
            new SteamVR_Utils.RigidTransform(hmd.GetEyeToHeadTransform(Hmd_Eye.Eye_Right)) };

        SteamVR_Utils.Event.Listen("initializing", OnInitializing);
        SteamVR_Utils.Event.Listen("calibrating", OnCalibrating);
        SteamVR_Utils.Event.Listen("out_of_range", OnOutOfRange);
        SteamVR_Utils.Event.Listen("device_connected", OnDeviceConnected);
        SteamVR_Utils.Event.Listen("new_poses", OnNewPoses);
    }