-
-
Notifications
You must be signed in to change notification settings - Fork 35.4k
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
Examples: added instancing_modified example #18256
Conversation
var colorChunk = [ | ||
'vec4 diffuseColor = vec4( diffuse * vInstanceColor, opacity );' | ||
].join( '\n' ); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How much trouble would it be to make this modification offset the UVs rather than changing the color — aside from the effort of finding a model and texture that look good with some offsets? 🤔
There's an easier way to set per-instance color, which I included in instancing / scatter
:
three.js/examples/webgl_instancing_scatter.html
Lines 82 to 98 in 4541808
// Assign random colors to the blossoms. | |
var _color = new THREE.Color(); | |
var color = new Float32Array( count * 3 ); | |
var blossomPalette = [ 0xF20587, 0xF2D479, 0xF2C879, 0xF2B077, 0xF24405 ]; | |
for ( var i = 0; i < count; i ++ ) { | |
_color.setHex( blossomPalette[ Math.floor( Math.random() * blossomPalette.length ) ] ); | |
_color.toArray( color, i * 3 ); | |
} | |
blossomGeometry.setAttribute( 'color', new THREE.InstancedBufferAttribute( color, 3 ) ); | |
blossomMaterial.vertexColors = THREE.VertexColors; | |
stemMesh = new THREE.InstancedMesh( stemGeometry, stemMaterial, count ); | |
blossomMesh = new THREE.InstancedMesh( blossomGeometry, blossomMaterial, count ); |
^But if for some reason that's not a recommended way to do this, I'm happy to address it there.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm imagining a translation-only UV offset... so e.g. four sub-textures could be packed adjacent in one image...
. 1 – 2 – 3 – 4
◾️◾️◾️◾️
... and the offset would select a different sub-texture for different instances. That would seem like a good example for something like minecraft-style voxels, where there are a finite number of voxel types.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
blossomGeometry.setAttribute( 'color', new THREE.InstancedBufferAttribute( color, 3 ) );
blossomMesh = new THREE.InstancedMesh( blossomGeometry, blossomMaterial, count );
Yikes. I can't imagine adding an InstancedBufferAttribute
to BufferGeometry
ever working. I would not recommend we encourage that coding pattern -- even if it does work in this case.
^But if for some reason that's not a recommended way to do this, I'm happy to address it there.
Yes, I'd recommend you think about changing that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's InstancedMesh
+ InstancedBufferGeometry
+ InstancedBufferAttribute
there, which doesn't seem obviously wrong, but ¯\(ツ)/¯
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems to me that InstancedBufferAttribute
should only be added to InstancedBufferGeometry
. Adding it to regular BufferGeometry
is inappropriate -- IMO.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is no BufferGeometry
in any of these examples — I'm passing InstantedBufferGeometry
to the InstancedMesh
constructor.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm passing InstantedBufferGeometry to the InstancedMesh constructor.
blossomGeometry.setAttribute( 'color', new THREE.InstancedBufferAttribute( color, 3 ) );
Is not blossomGeometry
is an instance of BufferGeometry
?
I'm passing
InstancedBufferGeometry
to theInstancedMesh
constructor.
The InstancedMesh
constructor expects a BufferGeometry
, no?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ahh you're right. In my head that was definitely InstancedBufferGeometry. 🤔
I would expect the InstancedMesh
constructor to accept InstancedBufferGeometry
, to allow users to keep its API even when using other instanced attributes, but apparently I haven't actually tested that yet!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
^But I was not expecting BufferGeometry+InstancedBufferAttribute to work. I'll update that example...
Thanks! |
@mrdoob @donmccurdy Should I change the name to |
I don't have a preference between the current name and this suggestion, but I do like that neither is specifically focusing on color being the customized attribute. Also... had you realized that this example is also adding InstancedBufferAttribute to BufferGeometry? I didn't earlier, but: |
@donmccurdy Oh, gosh. I'm doing the same thing that you are doing. :/ OK, I think I will have to give it up and live with it. Sorry for the unnecessary noise... And thank you for your feedback. :-) |
I do agree that adding (a) if there are real reasons this is fragile or might fail in the future, we should perhaps log a warning now. (b) if |
I agree with @donmccurdy that since Or just remove the example. As it is, it is just confusing. |
How about a visibility attribute? |
I think for now, I'd recommend removing the example until the time at which someone suggests a use case for which customized instancing is required. |
I just did 😅 |
Is there a practical use case for which customized instancing is required? If so, we could show an example of that. |
Credit for the inspiration for this PR goes to @takahirox.
https://twitter.com/superhoge/status/1176740112345911297
This example is simpler, and just shows how to use code injection to add a custom instanced attribute -- in this case color -- to
InstancedMesh
.Suggestions welcome.