Jump to content

Specular lighting issue when updating camera's ._viewMatrix


Densaugeo
 Share

Recommended Posts

So I'm trying out Babylon by porting a demo I made in Three. I'm replicating the custom camera controls from old demo by writing to the camera._viewMatrix, which works fine for moving the camera around, but specular highlights are frozen.

Since everything else seems to work, maybe the specular lighting uses a uniform that is not getting updated?

Note: I would like to be able to control the camera with matrix transforms.

Link to comment
Share on other sites

After some investigation, I have found that the cameras are updating _viewMatrix based on position, rotation, etc..

Since _viewMatrix is the transform of the scene relative to the camera, I found it useful to define a new matrix (called 'transform'), which is the inverse of _viewMatrix. It is similar to _worldMatrix, but I avoided using the same name due to possible conflicts.

Position.x, y, and z can be rebound to the appropriate fields on transform to keep them in sync with matrix updates. After that, _getViewMatrix can be altered to find _viewMatrix from the transform matrix instead of position, etc.. upVector and _cache.upVector can be hijacked to test transform for changes to know when to update _viewMatrix.

this._viewMatrix = new BABYLON.Matrix();
this.transform = initialTransform;
this.transform.invertToRef(this._viewMatrix);

// ._viewMatrix now comes from .transform
this._getViewMatrix = function() {
  this.transform.invertToRef(this._viewMatrix);
 
  return this._viewMatrix;
}

// Redirect .upVector + its cache to .transform, so that changes to .transform trigger updates
this._cache.upVector = BABYLON.Matrix.Identity();
this.upVector = this.transform;
 
// Bind .position to .transform
Object.defineProperties(this.position, {
  x: {
    enumerable: true,
    get: () => this.transform.m[12],
    set: v => this.transform.m[12] = v
  },
  y: ...
});

Works as far as I can tell. Overall, not as bad as it could be.

I'm still looking for a WebGL library that allows using matrices without such hacks though...

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...