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

Custom attribute for Instance #937

Open
stopyransky opened this issue Apr 15, 2024 · 1 comment
Open

Custom attribute for Instance #937

stopyransky opened this issue Apr 15, 2024 · 1 comment

Comments

@stopyransky
Copy link

How to include a custom per-instance attribute to an Instance?
I have InstancedMesh, InstancedGeometry and ShaderMaterial where I need to use per-instance attributes.
In the docs we have no example for this. There are position, rotation scale and color available like so:

<T.Group position={positionOffset}>
   <Instance {scale} {color} >
</T.Group>

What in case of custom attribute to be used inside shader code? I have the following:
Float32BufferAttributes for primitive geometry setup and InstancedBufferAttribute to be included

<InstancedMesh bind:ref={instancedMesh} count={count}>
  <T.InstancedBufferGeometry attach="geometry" instanceCount={count}>
    <T.Float32BufferAttribute 
      attachObject={['attributes', 'position']} 
      attach={(parent, self) => {  parent.setAttribute('position', self) }}
      args={[vertices, 3]} 
    />
    <T.Float32BufferAttribute
        attachObject={['attributes', 'uv']} 
        args={[uvs, 2]}
        attach={(parent, self) => { parent.setAttribute('uv', self) }}
    />
    <T.InstancedBufferAttribute 
      attachObject={['attributes', 'myPerInstanceAttr']} 
      args={[new Float32Array(myPerInstanceAttrArray), 4, false, count]} 
      attach={(parent, self) => {  parent.setAttribute('myPerInstanceAttr', self) }}
    />
 
  </T.InstancedBufferGeometry>
  <T.ShaderMaterial 
    attach="material"
    bind:ref={tileMaterial}
    uniforms={uniforms}
    transparent={true}
    vertexShader={`
          attribute vec4 myPerInstanceAttr;
         //...
    `}
    {fragmentShader}
  />

Thanks for help!

@edsunman
Copy link
Contributor

I think your code should work, I have tried attaching InstancedBufferGeometry and InstancedBufferAttribute to InstancedMesh in the same way and it is working ok. If you console.log and inspect the geometry does it look like everything is attached properly?
eg: <T.InstancedBufferGeometry attach="geometry" on:create={(ibg) => console.log(ibg.ref)}>.

Also in your code above if you are using count as the 4th args parameter in InstancedBufferAttribute I think it will repeat the same attribute for every instance... unless I have misunderstood what you are trying to do!

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