-
Notifications
You must be signed in to change notification settings - Fork 20
/
model-utils.js
111 lines (104 loc) · 2.67 KB
/
model-utils.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
100
101
102
103
104
105
106
107
108
109
110
111
/* global AFRAME, THREE */
AFRAME.registerComponent('lightmap', {
schema: {
src: {
type: "map"
},
intensity: {
default: 1
},
filter: {
default: ''
},
basis: {
default: false
},
channel: {
type: 'int',
default: 1
}
},
init() {
const src = typeof this.data.src === 'string' ? this.data.src : this.data.src.src;
const texture = new THREE.TextureLoader().load(src);
texture.flipY = false;
texture.channel = this.data.channel;
this.texture = texture;
this.el.addEventListener('object3dset', this.update.bind(this));
this.materials = new Map();
},
update() {
const filters = this.data.filter.trim().split(',');
this.el.object3D.traverse(function (o) {
if (o.material) {
if (filters.some(filter => o.material.name.includes(filter))) {
const sceneEl = this.el.sceneEl;
const m = o.material;
o.material = this.materials.has(m) ? this.materials.get(m) : new THREE.MeshPhongMaterial({
name: 'phong_' + m.name,
lightMap: this.texture,
lightMapIntensity: this.data.intensity,
color: m.color,
map: m.map,
transparent: m.transparent,
side: m.side,
depthWrite: m.depthWrite,
reflectivity: m.metalness,
toneMapped: m.toneMapped,
get envMap() {return sceneEl.object3D.environment}
});
this.materials.set(m, o.material);
}
}
}.bind(this));
}
});
AFRAME.registerComponent('depthwrite', {
schema: {
default: true
},
init() {
this.el.addEventListener('object3dset', this.update.bind(this));
},
update() {
this.el.object3D.traverse(function (o) {
if (o.material) {
o.material.depthWrite = this.data;
}
}.bind(this));
}
});
AFRAME.registerComponent('hideparts', {
schema: {
default: ""
},
init() {
this.el.addEventListener('object3dset', this.update.bind(this));
},
update() {
const filter = this.data.split(',');
this.el.object3D.traverse(function (o) {
if (o.type === 'Mesh' && filter.includes(o.name)) {
o.visible = false;
}
}.bind(this));
}
});
AFRAME.registerComponent('no-tonemapping', {
schema: {
default: ''
},
init() {
this.el.addEventListener('object3dset', this.update.bind(this));
},
update() {
const filters = this.data.trim().split(',');
this.el.object3D.traverse(function (o) {
if (o.material) {
if (filters.some(filter => o.material.name.includes(filter))) {
o.material.toneMapped = false;
}
}
}.bind(this));
}
});