Cloned objects with shader material added not rendering in game

Hi. I am using babylon 5.0 and realised that you have to use the options parameter to make sure the meshes are cloned and not instantiated.To fix animations in my game,I clone the animated objects instead of instantiating. I referred to this answer

Now animations seem to be working but the object does not render with the shader material. Instanced meshes render just fine. It’s just the cloned meshes causing problems. Any idea what the problem might be?

That seems strange, do you have a repro in the Playground that would show the problem?

I do not have a playground repo. But it is almost identical as the one above. The only difference being the fact that I am using a shader material.

export const track_batched_fragment = `
precision highp float;

varying vec2 vUV0;
varying vec4 vColor;
uniform sampler2D albedo;
void main(void) {
  float intensity = 2.0;
  vec4 color = texture2D(albedo,vUV0);
  color.rgb *= vColor.rgb * intensity;
  gl_FragColor = color;
}
`;
export const track_batched_vertex = `
precision highp float;

attribute vec3 position;
attribute vec3 normal;
attribute vec2 uv;
attribute vec4 color;
#include<instancesDeclaration>

uniform mat4 viewProjection;

varying vec2 vUV0;
varying vec4 vColor;

void main() {
  #include<instancesVertex>
  gl_Position = viewProjection * finalWorld * vec4(position,1.0);
  vUV0 = uv;
  vColor = color;
}
`;

The shader material is setup as follows

 private _createTrackMaterial(albedoTexture: string, index: any): ShaderMaterial {
    const mat = new ShaderMaterial('track_material_' + index, this._scene, trackShader, {
      attributes: ['position', 'normal', 'uv', 'color', 'world0', 'world1', 'world2', 'world3'],
      uniforms: ['viewProjection', 'worldViewProjection', 'albedo'],
      needAlphaBlending: true,
      needAlphaTesting: false,
    });
    const albedo = new Texture(albedoTexture, this._scene);
    mat.setTexture('albedo', albedo);
    mat.backFaceCulling = true;
    mat.forceDepthWrite = true;
    return mat;
  }

Another note. It renders fine with a PBR material. It’s the shader material causing the problem it seems

You need to add world to the list of uniforms:

https://playground.babylonjs.com/#AJA5J6#93

1 Like

Thanks. They rendered now, but stopped animating and moved out of place. It looks like an issue with the model I guess. When I am using a PBR material they are animating but are not in the correct place. Thanks for pointing it out

Look at my PG, I think they are at the right place and animated as you want?

In your own PG, you may want to add #include<bonesDeclaration> and #include<bonesVertex> at the correct location in the vertex shader.

1 Like

yeah I meant to say my project. I added both bone directives in the shader. Issue still persists

Updated vertex shader code

export const track_batched_vertex = `
precision highp float;

attribute vec3 position;
attribute vec3 normal;
attribute vec2 uv;
attribute vec4 color;
#include<bonesDeclaration>
#include<instancesDeclaration>

uniform mat4 viewProjection;

varying vec2 vUV0;
varying vec4 vColor;

void main() {
  #include<instancesVertex>
  #include<bonesVertex>
  gl_Position = viewProjection * finalWorld * vec4(position,1.0);
  vUV0 = uv;
  vColor = color;
}
`;

I think it will be hard to help more if you don’t provide a PG that does not work…

1 Like

OK thanks. I’ll try to create a PG. Thank you so much for your help so far

Ok I found the problem it seems. The animated model looks fine in other glb viewers but different in sandbox. I am using plattar exporter to export prefabs to gltf and then applying draco compression to glb. The glb looks fine on other online gltf viewers. Am I doing something wrong when I export it?

That should also work in the sandbox. We can have a look if you can provide the file.

2 Likes

yep i am trying to get permission to share the files. Will do if needed. Thanks for you help

1 Like