Jump to content

Updating Uniforms Doesn't Update Mesh


AlexB
 Share

Recommended Posts

I'm having a really hard time figuring this out. I'm creating this material.

 

this.material = new BABYLON.ShaderMaterial(
        part.url,
        this.viewService.scene,
        {
          vertex: '/shaders/v',
          fragment: '/shaders/f',
        },
        {
          needAlphaBlending: true,
          needAlphaTesting: true,
          defines: [
            '#extension GL_OES_standard_derivatives : enable',
            '#define IS_CLOWN_PASS 0',
            '#define IS_TRANSLUCENT false',
            '#define IS_METAL false',
          ],
          attributes: ['position', 'uv', 'normal'],
          uniforms: [
            'world',
            'worldView',
            'worldViewProjection',
            'view',
            'viewProjection',
            'u_combinedTextures',
            'texArr',
          ],
        }
      );

Then a few seconds later, in a totally different function, I'm attempting to update a uniform.
 

this.material.setTextureArray('texArr', [
        // red
        BABYLON.RawTexture.CreateRGBATexture(new Uint8Array([255, 0, 0, 255]), 1, 1, this.viewService.scene),
        // blue
        BABYLON.RawTexture.CreateRGBATexture(new Uint8Array([0, 0, 255, 255]), 1, 1, this.viewService.scene),
      ]);
this.material.isReady(mesh);
mesh.material = this.material;

This works if I paste this code right below where I create the material. Otherwise it seems to not set the values (I use SpectorJS to inspect the uniforms). Some questions:
 

  1. Is there another command I need to call to get the material to update?
  2. Does every uniform need to be in the initial uniforms array?
  3. How does setting the uniforms directly differ from scene.registerBeforeRender and doing it there?
Link to comment
Share on other sites

  • Is there another command I need to call to get the material to update?

The material should update on the next frame when you set the info.

  •  Does every uniform need to be in the initial uniforms array?

Yes they should be listed in the initial one in order to get their location

  • How does setting the uniforms directly differ from scene.registerBeforeRender and doing it there?

The first time the effect is created it keeps a map of uniform names to glUniformLocation based on the name you provide. Calling setTextureArray before the first render will automatically append the uniform name before the first render hence resulting in the uniform name being cached and usable. If you do it after I guess the cache does not contain the location hence why it does not work.

Hope this could help.

Link to comment
Share on other sites

This seems to be working, but only intermittently. Sometimes I will see my texture, but then I change some arbitrary code someone else and it'll stop working. It seems as though there is a race condition somewhere. When I inspect the scene using Spector, I don't always see the uniforms. I know for a fact that the material creator method is happening before the uniform setting method. I can also re-trigger the setter function so I don't think it's a race condition in my code. 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...