I have a simple model consisting of multiple boxes. I am trying to use buffer geometry merger to reduce the number of draw calls. However, I am not able to change the transparency/opacity of the geometries after merging.
I already tried the following things:
I am setting the opacity of the element using element.setAtribute('material', { opacity: 0.6 });
However, I dont see the correct change happening.
I tried to set the opaacity creating a geometry.addAttribute('opacity',THREE.BufferAttribute(new Float32Array(geometry.attributes.position.array.length), 1)
and setting all elements to 0.6
.
However, this did not work either.
I’ve created a minimal example which you can find here: https://glitch.com/~ambitious-chill
The function to set transparency:
function setTransparency(object, value) {
var mesh;
var geometry;
var opacity;
mesh = object.object3DMap.mesh;
if (!mesh || !mesh.geometry) {
el.addEventListener('object3dset', function reUpdate(evt) {
if (evt.detail.type !== 'mesh') {
return;
}
el.removeEventListener('object3dset', reUpdate);
self.update(oldData);
});
return;
}
geometry = mesh.geometry;
// Empty geometry.
if (!geometry.attributes.position) {
console.warn('Geometry has no vertices', el);
return;
}
if (!geometry.attributes.opacity) {
geometry.addAttribute('opacity',
new THREE.BufferAttribute(
new Float32Array(geometry.attributes.position.array.length), 1
)
);
}
opacity = geometry.attributes.opacity.array;
for (i = 0; i < opacity.length; i += 1) {
opacity[i] = 1 - value;
}
geometry.attributes.opacity.needsUpdate = true;
}
<!DOCTYPE html>
<html>
<head>
<title>My A-Frame Scene</title>
<script src="https://aframe.io/releases/0.9.1/aframe.min.js"></script>
<script src="https://unpkg.com/aframe-geometry-merger-component/dist/aframe-geometry-merger-component.min.js"></script>
</head>
<body>
<a-scene stats>
<a-entity buffer-geometry-merger material="vertexColors: vertex">
<a-entity geometry="primitive:box; skipCache:true; buffer:true; width:32.0; height:1.0; depth:40.0"
position="19.0 1.5 23.0"
shader="flat"
flat-shading="true">
</a-entity>
<a-entity geometry="primitive:box; skipCache:true; buffer:true; width:7.0; height:11.0; depth:7.0"
position="28.5 7.5 9.5"
shader="flat"
flat-shading="true">
</a-entity>
</a-entity>
</a-scene>
</body>
</html>
Can anyone please tell me, what am I doing wrong? How can I manipulate the opacity after merging?
Thank you in advance!
If you want to set the opacity of the merged mesh, You can set the opacity on the parent entity using the material component:
<a-entity buffer-geometry-merger material="opacity: 0.5; transparent: true">
<a-box></a-box>
<a-cylinder></a-cylinder>
</a-entity>
fiddle here.