-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathMaterialExt.js
104 lines (75 loc) · 2.91 KB
/
MaterialExt.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
///////////////////////////////////////////////////////////////////////////////
// Material Extension, illustrating custom material assigned to an existing
// component
// by Denis Grigor, September 2018
//
///////////////////////////////////////////////////////////////////////////////
class MaterialExtension extends Autodesk.Viewing.Extension {
constructor(viewer, options) {
super(viewer, options);
this.viewer = viewer;
this.tree = null;
this.colorPresets = [
'#73CEFF',
'#92CF00',
'#FFF365',
'#FFA923',
'#FF1600'
];
this.customize = this.customize.bind(this);
this.createMaterial = this.createMaterial.bind(this);
}
load() {
console.log('MaterialExtension is loaded!');
// this.viewer.addEventListener(Autodesk.Viewing.OBJECT_TREE_CREATED_EVENT,
this.viewer.addEventListener(Autodesk.Viewing.GEOMETRY_LOADED_EVENT,
this.customize);
return true;
}
unload() {
console.log('MaterialExtension is now unloaded!');
return true;
}
customize() {
// this.viewer.removeEventListener(Autodesk.Viewing.OBJECT_TREE_CREATED_EVENT,
this.viewer.removeEventListener(Autodesk.Viewing.GEOMETRY_LOADED_EVENT,
this.customize);
//Start custom code here ...
this.tree = this.viewer.model.getData().instanceTree;
let viewer = this.viewer;
const roomID = 412;
// const matman = this.viewer.impl.matman();
let myMaterial = this.createMaterial(this.colorPresets[0]);
// used to rescale and remove the z-fighting
let scaleRatio = 0.995; // this was determined as optimal through visual inspection
this.tree.enumNodeFragments(roomID, (fragId) => {
viewer.model.getFragmentList().setMaterial(fragId, myMaterial);
let fragProxy = viewer.impl.getFragmentProxy(viewer.model, fragId);
/* important technique if you want to remove z-fighting */
fragProxy.scale = new THREE.Vector3(scaleRatio,scaleRatio,scaleRatio);
fragProxy.updateAnimTransform();
// fragProxy.setMaterial(myMaterial);
});
// viewer.impl.invalidate(true);
viewer.impl.sceneUpdated(true);
}
createMaterial(color) {
const material = new THREE.MeshPhongMaterial({
side: THREE.DoubleSide,
reflectivity: 0.0,
flatShading: true,
transparent: true,
opacity: 0.5,
color
});
const materials = this.viewer.impl.matman();
const materialName = "MyCustomMaterial" + color.toString();
materials.addMaterial(materialName
,
material,
true);
return material;
}
}
Autodesk.Viewing.theExtensionManager.registerExtension('MaterialExtension',
MaterialExtension);