Hi all,
I was trying to use instanced buffergeometry to instance a pointcloud, but hasn’t really been successful, if you guys could give me some help.
Basically I have an existing point cloud created using buffergeometry and Point:
var geometry = new THREE.BufferGeometry();
geometry.setAttribute( 'position', new THREE.BufferAttribute( positions, 3 ) );
geometry.setAttribute( 'customColor', new THREE.BufferAttribute( colors, 3 ) );
geometry.setAttribute( 'size', new THREE.BufferAttribute( sizes, 1 ) );
geometry.setAttribute( 'alpha', new THREE.BufferAttribute( alphas, 1 ) );
var System = new THREE.Points( geometry, shaderMaterial );
scene.add( System );
and I want to create some exact copies of it at different point in space, so basically just with different location offset, and not rotation/color change.
I tried to use instanced buffer geometry but it didn’t work:
var geometry = new THREE.InstancedBufferGeometry();
geometry.setAttribute( 'position', new THREE.BufferAttribute( positions, 3 ) );
geometry.setAttribute( 'customColor', new THREE.BufferAttribute( colors, 3 ) );
geometry.setAttribute( 'size', new THREE.BufferAttribute( sizes, 1 ) );
geometry.setAttribute( 'alpha', new THREE.BufferAttribute( alphas, 1 ) );
const sumDisp = new Float32Array(sumDisplacement);
geometry.setAttribute('offset', new THREE.InstancedBufferAttribute(sumDisp, 3 ));
var System = new THREE.Mesh( geometry, shaderMaterial );
System.frustumCulled = false;
scene.add( System );
Please help me if you have any suggestions
the material/ shader I created is
export var uniforms = {
color: { value: new THREE.Color( 0xffffff ) },
texture: { value: new THREE.TextureLoader().load( "textures/sprites/disc.png" ) }
};
export var shaderMaterial = new THREE.RawShaderMaterial( {
uniforms: uniforms,
vertexShader: `
precision highp float;
uniform mat4 modelViewMatrix;
uniform mat4 projectionMatrix;
attribute vec3 position;
attribute float size;
attribute vec3 customColor;
attribute vec3 offset;
attribute float alpha;
varying float vAlpha;
varying vec3 vColor;
void main() {
vColor = customColor;
vAlpha = alpha;
vec3 newPosition = position + offset;
vec4 mvPosition = modelViewMatrix * vec4( newPosition, 1.0 );
gl_PointSize = size * ( 300.0 / -mvPosition.z );
gl_Position = projectionMatrix * mvPosition;
}`,
fragmentShader: `
precision highp float;
uniform vec3 color;
uniform sampler2D texture;
varying vec3 vColor;
varying float vAlpha;
void main() {
gl_FragColor = vec4( color * vColor, vAlpha );
gl_FragColor = gl_FragColor * texture2D( texture, gl_PointCoord );
}`,
blending: THREE.AdditiveBlending,
depthTest: false,
transparent: true
});