forked from c-frame/aframe-extras
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcube-env-map.js
131 lines (108 loc) · 3.8 KB
/
cube-env-map.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/**
* @param {Array<THREE.Material>|THREE.Material} material
* @return {Array<THREE.Material>}
*/
function ensureMaterialArray (material) {
if (!material) {
return [];
} else if (Array.isArray(material)) {
return material;
} else if (material.materials) {
return material.materials;
} else {
return [material];
}
}
/**
* @param {THREE.Object3D} mesh
* @param {Array<string>} materialNames
* @param {THREE.Texture} envMap
* @param {number} reflectivity [description]
*/
function applyEnvMap (mesh, materialNames, envMap, reflectivity) {
if (!mesh) return;
materialNames = materialNames || [];
mesh.traverse((node) => {
if (!node.isMesh) return;
const meshMaterials = ensureMaterialArray(node.material);
meshMaterials.forEach((material) => {
if (material && !('envMap' in material)) return;
if (materialNames.length && materialNames.indexOf(material.name) === -1) return;
material.envMap = envMap;
material.reflectivity = reflectivity;
material.needsUpdate = true;
});
});
}
/**
* Specifies an envMap on an entity, without replacing any existing material
* properties.
*/
module.exports = AFRAME.registerComponent('cube-env-map', {
multiple: true,
schema: {
path: {default: ''},
extension: {default: 'jpg', oneOf: ['jpg', 'png']},
format: {default: 'RGBFormat', oneOf: ['RGBFormat', 'RGBAFormat']},
enableBackground: {default: false},
reflectivity: {default: 1, min: 0, max: 1},
materials: {default: []}
},
init: function () {
const data = this.data;
this.texture = new THREE.CubeTextureLoader().load([
data.path + 'posx.' + data.extension, data.path + 'negx.' + data.extension,
data.path + 'posy.' + data.extension, data.path + 'negy.' + data.extension,
data.path + 'posz.' + data.extension, data.path + 'negz.' + data.extension
]);
this.texture.format = THREE[data.format];
this.object3dsetHandler = () => {
const mesh = this.el.getObject3D('mesh');
const data = this.data;
applyEnvMap(mesh, data.materials, this.texture, data.reflectivity);
};
this.el.addEventListener('object3dset', this.object3dsetHandler);
},
update: function (oldData) {
const data = this.data;
const mesh = this.el.getObject3D('mesh');
let addedMaterialNames = [];
let removedMaterialNames = [];
if (data.materials.length) {
if (oldData.materials) {
addedMaterialNames = data.materials.filter((name) => !oldData.materials.includes(name));
removedMaterialNames = oldData.materials.filter((name) => !data.materials.includes(name));
} else {
addedMaterialNames = data.materials;
}
}
if (addedMaterialNames.length) {
applyEnvMap(mesh, addedMaterialNames, this.texture, data.reflectivity);
}
if (removedMaterialNames.length) {
applyEnvMap(mesh, removedMaterialNames, null, 1);
}
if (oldData.materials && data.reflectivity !== oldData.reflectivity) {
const maintainedMaterialNames = data.materials
.filter((name) => oldData.materials.includes(name));
if (maintainedMaterialNames.length) {
applyEnvMap(mesh, maintainedMaterialNames, this.texture, data.reflectivity);
}
}
if (this.data.enableBackground && !oldData.enableBackground) {
this.setBackground(this.texture);
} else if (!this.data.enableBackground && oldData.enableBackground) {
this.setBackground(null);
}
},
remove: function () {
this.el.removeEventListener('object3dset', this.object3dsetHandler);
const mesh = this.el.getObject3D('mesh');
const data = this.data;
applyEnvMap(mesh, data.materials, null, 1);
if (data.enableBackground) this.setBackground(null);
},
setBackground: function (texture) {
this.el.sceneEl.object3D.background = texture;
}
});