three.js icon indicating copy to clipboard operation
three.js copied to clipboard

Surface sample texture with Color from texture

Open IanSweeneyAC opened this issue 3 years ago • 6 comments

@LeviPesin Try again, new branch

Description

use MeshSurfaceSampler to return colours from mesh texture map

webgl_instancing_scatter

This contribution is funded by AirCards

IanSweeneyAC avatar Sep 12 '22 14:09 IanSweeneyAC

Thanks @IanSweeneyAC!

Hm, I think the underlying problem might be that MeshSurfaceSampler's .sample( ... ) method does not provide any access to the sampled vertex indices (e.g. as barycentric coordinates). A user might want to access any of the vertex attributes, or sample other textures with the UVs.

Maybe we should add an additional method or two? For example, sampleBarycentric could return the sample as vertex indices and weights, and getAttribute could read a specified attribute for that sample:

// pointer to sample
const face = new THREE.Face();
const weights = new THREE.Vector3();

// values from sample
const position = new THREE.Vector3();
const uv = new THREE.Vector2();

sampler.sampleBarycentric( face, weights );
sampler.getAttribute( face, weights, 'position', position );
sampler.getAttribute( face, weights, 'uv', uv );

// ...

This could be more flexible for future use? I might prefer to leave the example demo as it is, though.

donmccurdy avatar Sep 12 '22 19:09 donmccurdy

Another possible benefit of the new API above — I think that would provide enough flexibility to sample an animated SkinnedMesh, apply the bone transform to the three vertices, and then blend the result using the weights. Related discussion:

https://discourse.threejs.org/t/reuse-model-animation-with-samples-of-its-geometry/28780

I suppose we could do that 'by default' too, but the flexibility here is nice.

donmccurdy avatar Sep 12 '22 20:09 donmccurdy

@donmccurdy you are ahead of me. I require sampling of animated skinnedMesh. Investigating.

IanSweeneyAC avatar Sep 13 '22 10:09 IanSweeneyAC

sampler.getAttribute( face, weights, 'position', position );

It might be a harder change to convince the project of but I could see this being added to core BufferGeometry to make reading barycentric interpolated values from geometry more convenient. Or perhaps a general utility in BufferGeometryUtils.

gkjohnson avatar Sep 13 '22 13:09 gkjohnson

I am also looking into the sampling of animated skinnedMesh. Has anyone made any progress on this so far? I'd be happy to jump and work together on this.

synergyseeker avatar Sep 18 '22 20:09 synergyseeker

@synergyseeker I'm investigating sampling a base skinned mesh so that when its animating, attached instances move accordingly. I was looking to attach flowers to sampled points of the bones demo and then enabling the animation loop. It looks like each sampled position will need to be its own geometry, so can no longer use InstancedMesh, which is the problem I got to before being diverted.

WIP is a bit messy, but I have something for looking up the weighted sample point bone weights an indexes.

IanSweeneyAC avatar Sep 20 '22 14:09 IanSweeneyAC

Let's continue in the PR below:

  • https://github.com/mrdoob/three.js/pull/26207

donmccurdy avatar Jun 07 '23 13:06 donmccurdy