1. 程式人生 > >建立View and Data – 3D模型

建立View and Data – 3D模型

View and Data – 3D模型

(轉自:blog.csdn.net/zzzh2088/article/details/64921162)

1、建立展示3D模型的容器,引入必要的css和js檔案

<!DOCTYPE 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"> </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.js"></script> <script src="https://developer.api.autodesk.com/viewingservice/v1/viewers/viewer3D.js"></script> </body> </html>

2、編寫擴充套件js,來展示自己的3D模型

var viewerApp;
// view物件
var viewer;
// 擴充套件物件
var extension; var _mainViewerSubToolbar = null; var controlGroup; // 配置初始化選項 // 這裡的accessToken和urn都是上傳模型檔案到官方伺服器返回的 var options = { env: 'AutodeskProduction', getAccessToken: function (onGetAccessToken) { var accessToken = '0E5yQLZxN4rddupcbDydhcRa6auQ'; var expireTimeSeconds = 60 * 30; onGetAccessToken(accessToken, expireTimeSeconds); } }; var documentId = 'urn:dXJuOmFkc2sub2JqZWN0czpvcy5vYmplY3Q6dGVzdGFuZG9zdGVjYXp6b2RpYXBpLWFydGh1cjQ1Ni9EaXNuZXltZXAucnZ0'; // 3D模型的初始化 Autodesk.Viewing.Initializer(options, function onInitialized() { var config = { extensions: ["Autodesk.Viewing.MarkupsCore","Autodesk.Viewing.MarkupRectangle"] }; viewerApp = new Autodesk.Viewing.ViewingApplication('MyViewerDiv'); viewerApp.registerViewer(viewerApp.k3D, Autodesk.Viewing.Private.GuiViewer3D, config); viewerApp.loadDocument(documentId, onDocumentLoadSuccess, onDocumentLoadFailure); }); // 文件載入成功的回撥 function onDocumentLoadSuccess(doc) { var viewables = viewerApp.bubble.search({'type': 'geometry'}); if (viewables.length === 0) { return; } viewerApp.selectItem(viewables[0].data, onItemLoadSuccess, onItemLoadFail); } // 文件載入失敗的回撥 function onDocumentLoadFailure(viewerErrorCode) { // ... } // 模型載入成功的回撥 function onItemLoadSuccess(viewer, item) { // ... } // 模型載入失敗的回撥 function onItemLoadFail(errorCode) { // ... }