Giter Club home page Giter Club logo

a-frame-component-gltf-manipulator's People

Contributors

akbartus avatar vincentfretin avatar

Stargazers

 avatar  avatar

Watchers

 avatar

Forkers

vincentfretin

a-frame-component-gltf-manipulator's Issues

Alternative API with multiple components

Thanks for sharing this example.
I did something similar in a project to override some material properties only, but using multiple components. Like this:

AFRAME.registerComponent("material-values", {
  multiple: true,
  schema: {
    materialName: { type: "string", default: "" },
    color: { type: "color", default: "" },
    map: { type: "string", default: "" },
    metalness: { type: "number", default: -1, min: 0, max: 1 },
    roughness: { type: "number", default: -1, min: 0, max: 1 },
    opacity: { type: "number", default: 1, min: 0, max: 1 }
  },

  events: {
    "model-loaded": function () {
      this.update();
    },
  },

  init() {
    this.rendererSystem = this.el.sceneEl.systems.renderer;
  },

  update: function () {
    const mesh = this.el.getObject3D("mesh");
    if (!mesh) return;

    const materialName = this.data.materialName;
    const color = this.data.color;
    const map = this.data.map;
    const metalness = this.data.metalness;
    const roughness = this.data.roughness;
    const opacity = this.data.opacity;
    const materialSeen = new Set();
    mesh.traverse((node) => {
      if (node.material && node.material.name === materialName) {
        if (materialSeen.has(node.material.name)) return;

        if (color !== "") {
          node.material.color.set(color);
          // The following line calls node.material.color.convertSRGBToLinear() when <a-scene renderer="colorManagement:true">
          this.rendererSystem.applyColorCorrection(node.material.color);
        } else {
          this.data.color = "#" + node.material.color.getHexString();
        }

        if (metalness !== -1) {
          node.material.metalness = metalness;
        } else {
          this.data.metalness = node.material.metalness;
        }

        if (roughness !== -1) {
          node.material.roughness = roughness;
        } else {
          this.data.roughness = node.material.roughness;
        }

        if (map) {
          const imageSrc = map;
          const loader = new THREE.TextureLoader();
          loader.load(
            imageSrc,
            function (texture) {
              if (node.material.map) {
                texture.encoding = node.material.map.encoding;
                texture.flipY = node.material.map.flipY;
                texture.offset.copy(node.material.map.offset);
                texture.repeat.copy(node.material.map.repeat);
                texture.wrapS = node.material.map.wrapS;
                texture.wrapT = node.material.map.wrapT;
                node.material.map.dispose();
              }
              node.material.map = texture;
              texture.needsUpdate = true;
              node.material.needsUpdate = true;
            },
            undefined,
            function () {
              console.error(`Error loading ${imageSrc}`);
            }
          );
        }
        node.material.opacity = opacity;
        // if opacity is 1, keep the current transparent value, that may be true
        if (opacity < 1) {
          node.material.transparent = opacity < 1;
        }
        node.material.needsUpdate = true;
        node.visible = opacity !== 0;
        materialSeen.add(node.material.name);
      }
    });
    materialSeen.clear();
  },
});

and using it like this:

<a-entity
  material-values__glass="materialName:glass;metalness:0.882;roughness:0.055;opacity:0.9"
  material-values__body="materialName:body_color;color:#ad050f;metalness:0.85;roughness:0.25"
  gltf-model="car.glb"
></a-entity>

If I don't specify a property, for example roughness, it defaults to -1, and I don't set it.
Also this way you can use the inspector and have a color picker to change the color.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.