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

Component for THREE.ShaderMaterial #71

Open
oveddan opened this issue Jan 8, 2016 · 2 comments
Open

Component for THREE.ShaderMaterial #71

oveddan opened this issue Jan 8, 2016 · 2 comments
Assignees

Comments

@oveddan
Copy link
Contributor

oveddan commented Jan 8, 2016

Per the example using shaders:
https://github.com/oveddan/react-three/blob/three-shader-example/examples/shader/shader.jsx#L22

It would be great if there was a react component for a THREE.ShaderMaterial

Currently this is done like:

cass Wavey extends React.Component {
  constructor(props) {
    super(props)

    this.uniforms = {
      time: { type: "f", value: props.time },
      resolution: { type: "v2", value: new THREE.Vector2(props.width, props.height) }
    }; 

    this.material = new THREE.ShaderMaterial({
      uniforms: this.uniforms,
      vertexShader: vertexShader,
      fragmentShader: fragmentShader
    });
  }
  componentWillReceiveProps(nextProps) {
    this.uniforms.time.value = nextProps.time

    if(nextProps.width !== this.props.width)
      this.uniforms.resolution.value.x = nextProps.width;

    if(nextProps.height !== this.props.height)
      this.uniforms.resolution.value.y = nextProps.height;
  }
  render() {
    return <Mesh geometry={geometry} material={this.material} />
  }
}

It would be great if there was some way to have a ShaderMaterial as a child element of the mesh,
and there was an easy way to pass new values to the uniforms.

@oveddan oveddan changed the title React-Three version of THREE.ShaderMaterial Component for THREE.ShaderMaterial Jan 8, 2016
@Izzimach
Copy link
Owner

Izzimach commented Feb 4, 2016

The problem here is that things like ShaderMaterial and Geometry are not instances of Object3D and the underlying scene graph is built using Object3D nodes. I mean it's certainly possible but would require a lot of work and/or hackery. Especially problematic is that the behavior is undefined when people do odd things like have multiple Geometry child or other strange things.

I usually recommend doing something like you've shown above - write a component to do all the nasty work and then use that component everywhere. #34 lists a few other components I wanted to write that would be similar.

@Izzimach Izzimach self-assigned this Feb 4, 2016
@Izzimach
Copy link
Owner

Also you might want to look at react-three-renderer which takes a different approach and betters supports embedding geometry/material as React components.

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

No branches or pull requests

2 participants