Hi ya all, I wanted to use instances for every possible mesh in my scene and encountered some problems on the way. If you have a Mesh with a custom shader instancing isn’t working flawless, i already found some post and got that i have to adjust the custom shader, but i can’t figure out how. Here is the cutom vertexShader used in this case:
// Precision
+ 'precision highp float;\r\n'
// Attributes
+ 'attribute vec3 position;\r\n'
+ 'attribute vec2 uv;\r\n'
// Uniforms
+ 'uniform mat4 world;\r\n'
+ '#include<instancesDeclaration>\r\n'
+ 'uniform mat4 worldViewProjection;\r\n'
// Varying
+ 'varying vec2 vUV;\r\n'
// MAIN
+ 'void main(void) {\r\n'
+ '#include<instancesVertex>\r\n'
+ ' vec4 p = vec4(position, 1.0);\r\n'
+ ' gl_Position = worldViewProjection * finalWorld * p;\r\n'
+ ' vUV = uv;\r\n'
+ '}\r\n';
I basically just added the two includes and multiplied worldViewProjection by finalWorld, but i have no idea where this value comes from.
And the fragmentShader:
// Precision
+ 'precision highp float;\r\n'
// Varying
+ 'varying vec2 vUV;\r\n'
// Uniforms - Diffuse
+ 'uniform sampler2D diffuseTexture;\r\n'
// MAIN
+ 'void main(void) {\r\n'
+ ' gl_FragColor = texture2D(diffuseTexture, vUV);\r\n'
+ '}\r\n';
And here the shaderMaterial gets created:
const material = new ShaderMaterial(
mesh.name + '-material',
scene,
{
vertexElement: 'custom',
fragmentElement: 'custom'
},
{
attributes: ['position', 'uv', 'normal', 'world0', 'world1', 'world2', 'world3'],
uniforms: ['world', 'worldViewProjection'],
needAlphaBlending: true,
defines: ['#define INSTANCES']
}
);
material.alphaMode = Engine.ALPHA_MULTIPLY;
In the final creation i just extended the attributes and added the defines statement.
It seems better than before, cause at least the plane materials get rendered at diffrent positions, but their aren’t the correct ones. I have to say i’m really not that common with shader programming and have no good understanding, what is going on there