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
and much thanks for this link, had not seen this before, it is a geniuos tool. It will take me some time to fully understand this, so i have one quick question, how easy is it to still set a texture on my custom NodeMaterial. Basically what i want to accomplish is to have a texture with black/white and set the white as alpha so it only renders the black part. I don’t need a guide on how to accomplish that exactly, but can you tell me if it’s possible without spending serveral days designing the nodeMaterial. Sorry for my maybe uneducated question.