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

Enable mat2/3/4 attribute in shader #16141

Merged
merged 2 commits into from
Aug 19, 2021
Merged

Conversation

takahirox
Copy link
Collaborator

@takahirox takahirox commented Apr 3, 2019

#16140

https://stackoverflow.com/questions/38853096/webgl-how-to-bind-values-to-a-mat4-attribute

Suggested API.

// glsl
attribute mat4 matrix;

// js
geometry.addAttribute( 'matrix', new THREE.BufferAttribute( data, 16 ) );

Change

  • Keeping not only attribute location but also attribute type in WebGLProgram.
  • If attribute type is mat2/3/4 renderer calls enableAttribute() and vertexAttribPointer() multiple times.
var locationsNum = 1;
if ( programAttribute.type === _gl.FLOAT_MAT2 ) locationsNum = 2;
if ( programAttribute.type === _gl.FLOAT_MAT3 ) locationsNum = 3;
if ( programAttribute.type === _gl.FLOAT_MAT4 ) locationsNum = 4;

....

for ( var i = 0; i < locationsNum; i ++ ) {

	state.enableAttribute( programAttribute.location + i );

}

....

for ( var i = 0; i < locationsNum; i ++ ) {

	_gl.vertexAttribPointer(
		programAttribute.location + i,
		size / locationsNum,
		type,
		normalized,
		size * bytesPerElement,
		( size / locationsNum ) * i * bytesPerElement
	);

}

@Shimmen
Copy link

Shimmen commented Jun 17, 2019

What is the status on this? Any chance of a merge in the near future? Thanks!

@yhdjyyzk
Copy link

yhdjyyzk commented Apr 21, 2021

what's the status now ? thanks !

@mrdoob
Copy link
Owner

mrdoob commented Apr 21, 2021

I guess this will need to be rebased?

@mrdoob mrdoob added this to the rXXX milestone Apr 21, 2021
@Mugen87
Copy link
Collaborator

Mugen87 commented Apr 21, 2021

As far as I remember, this code was originally added for an instanced mesh implementation. However, @mrdoob implemented it differently:

} else if ( name === 'instanceMatrix' ) {
const attribute = attributes.get( object.instanceMatrix );
// TODO Attribute may not be available on context restore
if ( attribute === undefined ) continue;
const buffer = attribute.buffer;
const type = attribute.type;
enableAttributeAndDivisor( programAttribute + 0, 1 );
enableAttributeAndDivisor( programAttribute + 1, 1 );
enableAttributeAndDivisor( programAttribute + 2, 1 );
enableAttributeAndDivisor( programAttribute + 3, 1 );
gl.bindBuffer( gl.ARRAY_BUFFER, buffer );
gl.vertexAttribPointer( programAttribute + 0, 4, type, false, 64, 0 );
gl.vertexAttribPointer( programAttribute + 1, 4, type, false, 64, 16 );
gl.vertexAttribPointer( programAttribute + 2, 4, type, false, 64, 32 );
gl.vertexAttribPointer( programAttribute + 3, 4, type, false, 64, 48 );
} else if ( name === 'instanceColor' ) {

@takahirox
Copy link
Collaborator Author

From #16140 (comment)

I'm willing to rebase to resolve the conflicts if anyone is still interested in this addition although Three.js API supports instancing now.

@mrdoob
Copy link
Owner

mrdoob commented Aug 11, 2021

@takahirox Sounds good to me! I think now it's a good time for it 👍

@takahirox
Copy link
Collaborator Author

Updated.

@mrdoob mrdoob modified the milestones: r???, r132 Aug 19, 2021
@mrdoob mrdoob merged commit 19359e3 into mrdoob:dev Aug 19, 2021
@mrdoob
Copy link
Owner

mrdoob commented Aug 19, 2021

Thanks!

@takahirox takahirox deleted the Mat234Attribute branch August 19, 2021 15:54
@joshuaellis joshuaellis mentioned this pull request Aug 30, 2021
10 tasks
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

Successfully merging this pull request may close these issues.

5 participants