Skip to content
Advertisement

AFrame updating material.offset for each object

I made an AFrame component for creating hotspot, and I want to change material.offset.y of a hotspot when I hover it with cursor. So I first tried using AFrame animaiton attribute, but visibly, we can’t access to this property with animation.

So I added a eventListener to the hotspot JS object and changing the getObject3D(‘mesh’).material.map.offset.y, but it update all hotspots texture when I hover one, and don’t know why… I checked if this was pointing on a specific hotspot, and it is ! So I don’t understand why all textures are updating…

Here is the code:

JavaScript
JavaScript

So if anyone know how to prevent this behaviour, don’t hesitate to comment this post please.

Thanks, Navalex

Advertisement

Answer

The reason why all images are given the same offset is because a-frame re-uses textures for performance reasons.

With a setup like this:

JavaScript

If you log both element.getObject3D("mesh").material.map.uuid – they will be the same (fiddle). Updating one affects the other.


If you give them an initial offset in the material (but a different one for each object):

JavaScript

a-frame will create another instance of THREE.Texture() them (fiddle).


Otherwise, you could create a component which replaces the texture with a copy:

JavaScript

Which brings us to something like this (including animation, wrapping etc)

Advertisement