Giter Club home page Giter Club logo

Comments (3)

mwmwmw avatar mwmwmw commented on July 20, 2024

NVM answered my own question.

I was just setting the prop on the CustomMaterial, but I needed to update the uniform value. Passing it in via a prop doesn't work.

Bad:

<CustomShaderMaterial
    myTexture={myTexture} 
    {...MaterialProps} 
 />

Good (maybe a nicer way to do this?, But it works)

useEffect(()=>{
 ref.current.uniforms.myTexture.value = myTexture;
},[])

<CustomShaderMaterial
    ref={ref}
    {...MaterialProps} 
/>

from three-customshadermaterial.

FarazzShaikh avatar FarazzShaikh commented on July 20, 2024

This is intentional as an API goal for CSM is to emulate standard ShaderMaterial syntax and that’s just how it’s done with shaders.

A nicer way to do this is to extend CSM/vanilla and make your own material class. Then you can define setters/getters to map JSX props to uniforms.

Untested pseudocode:

import CSM from "three-custom-shader-material/vanilla"
import { extend } from "@react-three/fiber"

class MyCustomMaterial extends CSM {
   constructor() {
      super({
         baseMaterial: ...,
         uniforms: {
            myTexture: { value: null }
         }
      })
   }
   
   set myTexture(value) {
      this.uniforms.myTexture.value = value;
   }
   
   get myTexture() {
      return this.uniforms.myTexture.value
   }
}

extend({ MyCustomMaterial: MyCustomMaterial })

<MyCustomMaterial 
   myTexture={texture}
/>

Unfortunately, having CSM handle this is infeasible as it requires dynamically generating getters/setters which may override setters/getters on the base material causing unintended effects. Its better to leave this in user-land

from three-customshadermaterial.

mwmwmw avatar mwmwmw commented on July 20, 2024

Yeah it works well. I was porting a shader over from Drei's shaderMaterial() and it did things just differently enough to throw a wrench in my brain.

Extending the base class is probably overkill for what I'm doing, but I like the example.

from three-customshadermaterial.

Related Issues (20)

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.