使用基本应用程序示例

时间:2017-05-23 00:33:58

标签: autodesk-forge autodesk-viewer

我正在尝试将WebVR扩展与基本应用程序一起使用。以下html代替VR中的3D模型,呈现2D模型。代码主要是样板文件,除了我调用WebVR扩展名的地方。提前谢谢!

<!DOCTYPE html>
<!-- vr.html -->
    <head>
    <meta name="viewport" content="width=device-width, minimum-scale=1.0, initial-scale=1, user-scalable=no" />
    <meta charset="utf-8">

    <!-- The Viewer CSS -->
    <link rel="stylesheet" href="https://developer.api.autodesk.com/viewingservice/v1/viewers/style.min.css" type="text/css">

    <!-- Developer CSS -->
    <style>
        body {
            margin: 0;
        }
        #MyViewerDiv {
            width: 100%;
            height: 100%;
            margin: 0;
            background-color: #F0F8FF;
        }
    </style>
</head>
<body>

    <!-- The Viewer will be instantiated here -->
    <div id="MyViewerDiv"></div>

    <!-- The Viewer JS -->
    <script src="https://developer.api.autodesk.com/viewingservice/v1/viewers/three.min.js"></script>
    <script src="https://developer.api.autodesk.com/viewingservice/v1/viewers/viewer3D.min.js"></script>

    <!-- Developer JS -->
    <script>
        var viewerApp;
        var options = {
            env: 'AutodeskProduction',
            // Here is the WebVR extension
            extensions: ['Autodesk.Viewing.WebVR'],
            getAccessToken: function(onGetAccessToken) {
                //
                // TODO: Replace static access token string below with call to fetch new token from your backend
                // Both values are provided by Forge's Authentication (OAuth) API.
                //
                // Example Forge's Authentication (OAuth) API return value:
                // {
                //    "access_token": "<YOUR_APPLICATION_TOKEN>",
                //    "token_type": "Bearer",
                //    "expires_in": 86400
                // }
                //
                var accessToken = {{ accessToken }};
                var expireTimeSeconds = 60 * 30;
                onGetAccessToken(accessToken, expireTimeSeconds);
            }

        };
        var documentId = {{ documentID }};
        // var config = {
            // extensions: ['Autodesk.Viewing.WebVR'],
            // experimental: ['webVR_orbitModel']
        // };
        Autodesk.Viewing.Initializer(options, function(){
            viewerApp = new Autodesk.Viewing.ViewingApplication('MyViewerDiv');
            viewerApp.registerViewer(viewerApp.k3D, Autodesk.Viewing.Private.GuiViewer3D);
            viewerApp.loadDocument(documentId, onDocumentLoadSuccess, onDocumentLoadFailure);
        });

        function onDocumentLoadSuccess(doc) {

            // We could still make use of Document.getSubItemsWithProperties()
            // However, when using a ViewingApplication, we have access to the **bubble** attribute,
            // which references the root node of a graph that wraps each object from the Manifest JSON.
            var viewables = viewerApp.bubble.search({'type':'geometry'});
            if (viewables.length === 0) {
                console.error('Document contains no viewables.');
                return;
            }

            // Choose any of the avialble viewables
            viewerApp.selectItem(viewables[0].data, onItemLoadSuccess, onItemLoadFail);
        }

        function onDocumentLoadFailure(viewerErrorCode) {
            console.error('onDocumentLoadFailure() - errorCode:' + viewerErrorCode);
        }

        function onItemLoadSuccess(viewer, item) {
            console.log('onItemLoadSuccess()!');
            console.log(viewer);
            console.log(item);

            // Congratulations! The viewer is now ready to be used.
            console.log('Viewers are equal: ' + (viewer === viewerApp.getCurrentViewer()));
        }

        function onItemLoadFail(errorCode) {
            console.error('onItemLoadFail() - errorCode:' + errorCode);
        }

    </script>
  </body>
</html>

{{documentID}}是我的骨灰盒,{{accessToken}}是我的代币。

1 个答案:

答案 0 :(得分:1)

首先,我不认为WebVR扩展适用于2D模型,我只是尝试使用f2d文件,并且它无法正常工作,您对VR中的2D有什么期望?

我还建议你使用Viewer版本至少2.13,如果你没有指定任何版本的查看器,默认情况下我会尝试使用2.12。如果您在https://developer.api.autodesk.com/viewingservice/v1/viewers/viewer3D.js?v=2.12检查WebVR扩展的查看器2.12代码,当您的浏览器本身不支持WebVR时,您将看到它不使用webvr-polyfill。但是,从查看器2.13开始,添加了以下代码以支持webvr-polyfill,这使得您的浏览器支持WebVR,即使它本身也不支持WebVR。

// check if browser supports webVR1.1 natively, if not, load polyfill
avp.loadDependency('VRFrameData', 'webvr-polyfill.min.js', function() {})

最后,如果您想检查结果,我在https://viewervr.herokuapp.com有一个简单的WebVR运行示例,仅此而已,只需加载&#34;&#39; Autodesk.Viewing.WebVR&#34;扩展程序,如果您在移动设备上运行它,您将看到预期的结果。

希望它有所帮助。