摘要:可能有許多原因你想在里加入自訂義的線型,例如顯示線框幾何視覺(jué)化包圍箱或者其他你想帶給使用者的視覺(jué)回饋。下面是我傳寫的一個(gè)例子,他可以在選重構(gòu)件后在場(chǎng)景里用自定義線型描繪它的包圍箱,在線示例可以參考這里
這篇文章的原著是 Autodesk AND 的 Philippe Leefsma,以下以我簡(jiǎn)稱。
可能有許多原因你想在 Viewer 里加入自訂義的線型,例如顯示線框(Wireframe)幾何、視覺(jué)化包圍箱(Bounding Box)或者其他你想帶給使用者的視覺(jué)回饋?;旧线@有三種方式來(lái)做到這件事,讓我們來(lái)看看到底要怎么做,首先我們要先產(chǎn)生一個(gè)線型幾何,其代碼入下所示:
const geometry = new THREE.Geometry () geometry.vertices.push (new THREE.Vector3 ( 0, 0, 0)) geometry.vertices.push (new THREE.Vector3 (10, 10, 10)) var linesMaterial = new THREE.LineBasicMaterial ({ color: new THREE.Color (0xFF0000), transparent: true, depthWrite: false, depthTest: true, linewidth: 10, opacity: 1.0 }) var lines = new THREE.Line (geometry, linesMaterial, THREE.LinePieces)
下一步我們來(lái)看看要怎么將這個(gè)線型加入到 Viewer 的場(chǎng)景里(scene):
一、將線型加到 viewer.impl.scene:
//1 - 第一種方法 viewer.impl.scene.add (lines) viewer.impl.invalidate (true)
但這個(gè)方法并不是一個(gè)靠譜的方法,因?yàn)樗荒茉?FireFox 里正常運(yùn)作,在我的 Chrome 上是沒(méi)有作用的。。。所以讓我們看看下一個(gè)方法。
二、將線型加到 viewer.impl.scene:
//2 - 第二種方法 viewer.impl.sceneAfter.add (lines) viewer.impl.invalidate (true)
這個(gè)方法比前一個(gè)好多了,他可以在多個(gè)瀏覽器上正常執(zhí)行,但當(dāng)你透過(guò)修改了構(gòu)件的可視性后(執(zhí)行了孤立顯示或隱藏等動(dòng)作),所有的線型都會(huì)跟著構(gòu)件一起消失,我想這是應(yīng)該是 Viewer 內(nèi)部的著色器(Shader)和渲染(Rendering)設(shè)置造成的。
三、產(chǎn)生 Viewer Overlay 場(chǎng)景,并將線型加入 Overlay 場(chǎng)景:
//3 - 第三種方法 viewer.impl.createOverlayScene ( "myOverlay", linesMaterial) viewer.impl.addOverlay ( "myOverlay", lines) viewer.impl.invalidate (true)
經(jīng)測(cè)試這個(gè)方法非常的靠譜,他可以在多個(gè)瀏覽器上正確執(zhí)行,并且不受構(gòu)件可視性的影響。但你不一定要使用第三個(gè)方法,你可以根據(jù)你的需求選擇適合你應(yīng)用場(chǎng)景的方法。
下面是我傳寫的一個(gè)例子 Viewing.Extension.BoundingBox,他可以在選重構(gòu)件后在 Viewer 場(chǎng)景里用自定義線型描繪它的包圍箱,在線示例可以參考這里:
///////////////////////////////////////////////////////////////// // BoundingBox Viewer Extension // By Philippe Leefsma, Autodesk Inc, August 2017 // ///////////////////////////////////////////////////////////////// import MultiModelExtensionBase from "Viewer.MultiModelExtensionBase" import Toolkit from "Viewer.Toolkit" class BoundingBoxExtension extends MultiModelExtensionBase { ///////////////////////////////////////////////////////// // Class constructor // ///////////////////////////////////////////////////////// constructor (viewer, options) { super (viewer, options) this.onContextMenu = this.onContextMenu.bind(this) this.linesMaterial = this.createMaterial(0x0000FF) this.lineGroups = [] } ///////////////////////////////////////////////////////// // // ///////////////////////////////////////////////////////// createMaterial (color = 0x000000, opacity = 1.0) { return new THREE.LineBasicMaterial({ color: new THREE.Color(color), transparent: true, depthWrite: false, depthTest: true, linewidth: 10, opacity }) } ///////////////////////////////////////////////////////// // Load callback // ///////////////////////////////////////////////////////// load () { this.viewer.loadDynamicExtension( "Viewing.Extension.ContextMenu").then( (ctxMenuExtension) => { ctxMenuExtension.addHandler( this.onContextMenu) }) console.log("Viewing.Extension.BoundingBox loaded") return true } ///////////////////////////////////////////////////////// // // ///////////////////////////////////////////////////////// get className() { return "bounding-box" } ///////////////////////////////////////////////////////// // Extension Id // ///////////////////////////////////////////////////////// static get ExtensionId () { return "Viewing.Extension.BoundingBox" } ///////////////////////////////////////////////////////// // Unload callback // ///////////////////////////////////////////////////////// unload () { console.log("Viewing.Extension.BoundingBox unloaded") super.unload () return true } ///////////////////////////////////////////////////////// // // ///////////////////////////////////////////////////////// onModelRootLoaded () { this.options.loader.show (false) this.viewer.impl.createOverlayScene ( "boundingBox", this.linesMaterial) } ///////////////////////////////////////////////////////// // // ///////////////////////////////////////////////////////// async onSelection (event) { if (event.selections.length) { const selection = event.selections[0] const model = this.viewer.activeModel || this.viewer.model this.selectedDbId = selection.dbIdArray[0] const bbox = await Toolkit.getWorldBoundingBox( model, this.selectedDbId) this.drawBox(bbox) } } ///////////////////////////////////////////////////////// // // ///////////////////////////////////////////////////////// drawBox (bbox) { const geometry = new THREE.Geometry() const { min, max } = bbox geometry.vertices.push(new THREE.Vector3(min.x, min.y, min.z)) geometry.vertices.push(new THREE.Vector3(max.x, min.y, min.z)) geometry.vertices.push(new THREE.Vector3(max.x, min.y, min.z)) geometry.vertices.push(new THREE.Vector3(max.x, min.y, max.z)) geometry.vertices.push(new THREE.Vector3(max.x, min.y, max.z)) geometry.vertices.push(new THREE.Vector3(min.x, min.y, max.z)) geometry.vertices.push(new THREE.Vector3(min.x, min.y, max.z)) geometry.vertices.push(new THREE.Vector3(min.x, min.y, min.z)) geometry.vertices.push(new THREE.Vector3(min.x, max.y, max.z)) geometry.vertices.push(new THREE.Vector3(max.x, max.y, max.z)) geometry.vertices.push(new THREE.Vector3(max.x, max.y, max.z)) geometry.vertices.push(new THREE.Vector3(max.x, max.y, min.z)) geometry.vertices.push(new THREE.Vector3(max.x, max.y, min.z)) geometry.vertices.push(new THREE.Vector3(min.x, max.y, min.z)) geometry.vertices.push(new THREE.Vector3(min.x, max.y, min.z)) geometry.vertices.push(new THREE.Vector3(min.x, max.y, max.z)) geometry.vertices.push(new THREE.Vector3(min.x, min.y, min.z)) geometry.vertices.push(new THREE.Vector3(min.x, max.y, min.z)) geometry.vertices.push(new THREE.Vector3(max.x, min.y, min.z)) geometry.vertices.push(new THREE.Vector3(max.x, max.y, min.z)) geometry.vertices.push(new THREE.Vector3(max.x, min.y, max.z)) geometry.vertices.push(new THREE.Vector3(max.x, max.y, max.z)) geometry.vertices.push(new THREE.Vector3(min.x, min.y, max.z)) geometry.vertices.push(new THREE.Vector3(min.x, max.y, max.z)) const lines = new THREE.Line(geometry, this.linesMaterial, THREE.LinePieces) this.lineGroups.push(lines) this.viewer.impl.addOverlay("boundingBox", lines) this.viewer.impl.invalidate( true, true, true) } ///////////////////////////////////////////////////////// // // ///////////////////////////////////////////////////////// onContextMenu (event) { const model = this.viewer.activeModel || this.viewer.model event.menu.forEach((entry) => { const title = entry.title.toLowerCase() switch (title) { case "isolate": entry.target = () => { Toolkit.isolateFull( this.viewer, this.selectedDbId, model) } break case "hide selected": entry.target = () => { Toolkit.hide( this.viewer, this.selectedDbId, model) } break case "show all objects": entry.target = () => { Toolkit.isolateFull( this.viewer, [], model) this.viewer.fitToView() } break default: break } }) const instanceTree = model.getData().instanceTree const dbId = event.dbId || (instanceTree ? instanceTree.getRootId() : -1) if (dbId > -1) { event.menu.push({ title: "Clear All BoundingBoxes", target: () => { this.lineGroups.forEach((lines) => { this.viewer.impl.removeOverlay("boundingBox", lines) }) this.viewer.impl.invalidate( true, true, true) this.lineGroups = [] } }) } } } Autodesk.Viewing.theExtensionManager.registerExtension ( BoundingBoxExtension.ExtensionId, BoundingBoxExtension) export default "Viewing.Extension.BoundingBox"
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/91850.html
摘要:最近有的小伙伴們都在詢問(wèn)要怎么在里顯示自訂義屬性,要做到這個(gè)是挺容易的。在來(lái)我們透過(guò)繼承來(lái)創(chuàng)建自個(gè)的屬性面板使用的語(yǔ)法,部份代碼來(lái)自的無(wú)法從服務(wù)器獲取屬性透過(guò)撰寫括展讓自定義屬性窗取代自帶的以上希望對(duì)各為小伙伴有幫助參考 最近有 Autodesk Forge 的小伙伴們都在詢問(wèn)要怎么在 Viewer 里顯示自訂義屬性,要做到這個(gè)是挺容易的。目前有兩種方式可以做到這個(gè)效果,一種是直接添加...
摘要:在官方釋出版的同時(shí)發(fā)布了新版本的,這個(gè)面版已被整個(gè)重新改寫,這次更新也加入一些新的交互行為,下面我們將會(huì)稍作解釋。 這禮拜的小技巧是關(guān)于如何以不加入太多的 JavaScript 的方式自訂義 ModelStructurePanel 的交互行為,這個(gè)小技巧受到這篇問(wèn)與答的啟發(fā):Prevent zoom in Forge viewer when clicking in Model Brow...
摘要:前陣子有些圈的朋友們都在詢問(wèn)同一個(gè)問(wèn)題要怎么在的自帶右鍵菜單上添加自定義項(xiàng)目或是只顯示自訂義項(xiàng)目以下將針對(duì)在自帶右鍵菜單上添加自定義項(xiàng)目和只顯示自訂義項(xiàng)目的右鍵菜單進(jìn)行說(shuō)明。 前陣子有些 Autodesk Forge 圈的朋友們都在詢問(wèn)同一個(gè)問(wèn)題『要怎么在 Viewer 的自帶右鍵菜單上添加自定義項(xiàng)目或是只顯示自訂義項(xiàng)目』~ 以下將針對(duì)『在自帶右鍵菜單上添加自定義項(xiàng)目』和『只顯示自訂義...
摘要:對(duì)于大多數(shù)的模型文檔都可以透過(guò)服務(wù)提取轉(zhuǎn)換在里渲染構(gòu)件外觀時(shí)所需的材質(zhì)及貼圖。所以我們可以透過(guò)它遍歷所有材質(zhì),找出我們想隱藏貼圖的那些材質(zhì),將它的顏色設(shè)置為灰色,同時(shí)也可以透過(guò)它將隱藏貼圖的材質(zhì)回復(fù)。 這篇文章來(lái)自 Autodesk ADN 的梁曉冬,以下以我簡(jiǎn)稱。 對(duì)于大多數(shù)的模型文檔都可以透過(guò) Autodesk Forge Model Derivative 服務(wù)提取、轉(zhuǎn)換在 Vie...
摘要:但很不幸的,新功能要加入里頭這件事對(duì)于開發(fā)團(tuán)隊(duì)而言絕非一件易事,是需要一些時(shí)間來(lái)完成的這篇文章將帶領(lǐng)大家用最少的工作量將上的新功能拿來(lái)上使用。在這個(gè)案例里頭,我們可以只將和其他相依文檔引入例如。 showImg(https://segmentfault.com/img/bV25af?w=1311&h=696); 對(duì)于 Forge Viewer 比較熟悉的朋友可能知道 Forge Vie...
閱讀 1658·2023-04-25 16:29
閱讀 963·2021-11-15 11:38
閱讀 2302·2021-09-23 11:45
閱讀 1429·2021-09-22 16:03
閱讀 2549·2019-08-30 15:54
閱讀 1211·2019-08-30 10:53
閱讀 2609·2019-08-29 15:24
閱讀 1108·2019-08-26 12:25