Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[uv-scroll] create a separate repo #15

Open
vincentfretin opened this issue Jul 18, 2023 · 3 comments
Open

[uv-scroll] create a separate repo #15

vincentfretin opened this issue Jul 18, 2023 · 3 comments
Assignees
Labels
enhancement New feature or request

Comments

@vincentfretin
Copy link
Member

vincentfretin commented Jul 18, 2023

This is a modified version of the uv-scroll.js hubs code that can be used with the aframe material component. Changes required were to use an aframe system and waiting for material to be loaded via the materialtextureloaded event.
Licensed under MPL 2.0

Usage:

@vincentfretin
Copy link
Member Author

vincentfretin commented Jul 19, 2023

I need to create a new repo in the c-frame organization with some examples how to use it with aframe, and publish to npm.

Here is the full code of the modified version:

/* global AFRAME, THREE */
// This is a modified copy of
// https://github.com/mozilla/hubs/blob/6c49ce303ad7e030c80b301dbc3ce100cac7d9c1/src/components/uv-scroll.js
// to work with an aframe system and waiting for material to be loaded via the materialtextureloaded event.
// @license MPL 2.0 https://github.com/mozilla/hubs/blob/master/LICENSE

const textureToData = new Map();
const registeredTextures = [];

AFRAME.registerSystem("uv-scroll", {
  tick(t, dt) {
    for (let i = 0; i < registeredTextures.length; i++) {
      const map = registeredTextures[i];
      const { offset, instances } = textureToData.get(map);
      const { component } = instances[0];

      offset.addScaledVector(component.data.speed, dt / 1000);

      offset.x = offset.x % 1.0;
      offset.y = offset.y % 1.0;

      const increment = component.data.increment;
      map.offset.x = increment.x ? offset.x - (offset.x % increment.x) : offset.x;
      map.offset.y = increment.y ? offset.y - (offset.y % increment.y) : offset.y;
    }
  },
});

/**
 * Animate the UV offset of a mesh's material
 * @component uv-scroll
 */
AFRAME.registerComponent("uv-scroll", {
  schema: {
    speed: { type: "vec2", default: { x: 0, y: 0 } },
    increment: { type: "vec2", default: { x: 0, y: 0 } },
  },
  play() {
    const mesh = this.el.getObject3D("mesh") || this.el.getObject3D("skinnedmesh");
    const material = mesh && mesh.material;
    if (material) {
      // We store mesh here instead of the material directly because we end up swapping out the material in injectCustomShaderChunks.
      // We need material in the first place because of MobileStandardMaterial
      const instance = { component: this, mesh };

      this.instance = instance;
      this.map = material.map || material.emissiveMap;

      if (this.map && !textureToData.has(this.map)) {
        textureToData.set(this.map, {
          offset: new THREE.Vector2(),
          instances: [instance],
        });
        registeredTextures.push(this.map);
      } else if (!this.map) {
        if (this.el.components.material) {
          // when using material and uv-scroll components
          this.el.addEventListener("materialtextureloaded", () => {
            this.map = material.map || material.emissiveMap;
            if (!textureToData.has(this.map)) {
              textureToData.set(this.map, {
                offset: new THREE.Vector2(),
                instances: [instance],
              });
              registeredTextures.push(this.map);
            }
          });
        } else {
          console.warn(
            "Ignoring uv-scroll added to mesh with no scrollable texture."
          );
        }
      } else {
        console.warn(
          "Multiple uv-scroll instances added to objects sharing a texture, only the speed/increment from the first one will have any effect"
        );
        textureToData.get(this.map).instances.push(instance);
      }
    }
  },

  pause() {
    if (this.map) {
      const instances = textureToData.get(this.map).instances;
      instances.splice(instances.indexOf(this.instance), 1);
      // If this was the last uv-scroll component for a given texture
      if (!instances.length) {
        textureToData.delete(this.map);
        registeredTextures.splice(registeredTextures.indexOf(this.map), 1);
      }
    }
  },
});

@vincentfretin
Copy link
Member Author

Demo: https://aframe-uvscroll.glitch.me
Glitch: https://glitch.com/edit/#!/aframe-uvscroll

<a-entity
  position="0 1.6 -2"
  geometry="primitive:plane;width:0.5;height:0.598"
  material="src:howtousetrigger.jpg;shader:flat;repeat: 0.5 1"
  uv-scroll="increment: 0.5 0; speed: 0.5 0"
></a-entity>

@vincentfretin
Copy link
Member Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant