Jump to content

.getHeightAtCoordinates() not returning height correctly when paired with .convertToFlatShadedMesh()


saltemishel
 Share

Recommended Posts

Hi All,

It appears that .getHeightAtCoordinates() doesn't correctly when I'm using .convertToFlatShadedMesh()

Without converting to flat shaded mesh

https://www.dropbox.com/s/fydn2uq78ek43qm/Screenshot 2017-07-12 10.50.08.png?dl=0

 

After converting to flat shaded mesh, the boxes starts to float everywhere

https://www.dropbox.com/s/982wiz0rnjfshvm/Screenshot 2017-07-12 10.52.03.png?dl=0

Can this be fixed?

Or is there a way for my to make the ground flat shaded without using .convertToFlatShadedMesh()?

 

Thanks.

Link to comment
Share on other sites

When you convert the ground to a flat shaded one, this adds a lot of new vertices what modify the ground geometry (actually it's no more a real ground mesh... but something else).

As the method getHeightAtCoordinates() relies on the knowledge of the geometry to be the fastest possible, it doesn't work anylonger with a newly computed geometry.

I can't check this before weeks ... and it's not that simple to modify to work a flat shaded mesh, but I'll have a look when I am back.

For now, please just consider that the method getHeightAtCoordinates() only works with un-modified grounds (still works with rotated or translated grounds though)

Link to comment
Share on other sites

[Wingnut hugs @Pryme8 for his warm-hearted humility]  :)

Ok... let's see... what needs my annoying touch, here.  Umm...

When a heightMap ground gets converted to flat shaded, DO the added verts/indices/normals/etc... get appended-onto the original vertexData arrays?

OR... do the new verts/indices/normals/etc... get inter-mixed with the original v-data?  Or maybe... shifted into the original v-data?

I suppose I could go look at code.  https://github.com/BabylonJS/Babylon.js/blob/master/src/Mesh/babylon.mesh.ts#L1600

Okay, that just made my brain hurt.  :)

My question is... WHY is getHeightAtCoordinates() giving bad values when mesh is flat shaded?

Is the problem ACTUALLY happening... due-to the way we are converting to flat shaded?  hmm.

And... IF getHeightAtCoordinates() has a minor problem, I bet we can adjust it before @jerome gets back from vacation, and give him a happy surprise.  :)

Link to comment
Share on other sites

it returns bad values after flat shading because the method knows the initial geometry (that's why it's fast, no need for ray intersection). When flat shading, a new array is built, containing new vertices being in between the initial ones ... so another different geomety the method doesn't know anythting about.

Link to comment
Share on other sites

Why don't we have a flat shade option in the standard shader?
Manipulating the mesh in order to make it flat shaded seems counter intuitive to me when this could simply be handled with its fragment shader.

https://www.gamedev.net/articles/programming/graphics/the-basics-of-glsl-40-shaders-r2861

There is a section on a flat shader on this page.

Link to comment
Share on other sites

9 hours ago, Pryme8 said:

Why don't we have a flat shade option in the standard shader?
Manipulating the mesh in order to make it flat shaded seems counter intuitive to me when this could simply be handled with its fragment shader.

Seems like it would be fairly simple to do this.

Assuming that std derivatives are supported you can implement it like this.

Example playground with shader version on the left and convertToFlatShadedMesh on right: https://www.babylonjs-playground.com/#E0PJHQ

Link to comment
Share on other sites

@sable sorry about the noob question, I'm not sure if I'm missing anything, though you've already show me the playground.

It looks like this after I implement this material, maybe I'm doing it wrong?

https://www.dropbox.com/s/w4bx67e9gd36h6k/Screenshot 2017-07-13 11.34.12.png?dl=0

this is what I did:

this.mesh = BABYLON.Mesh.CreateGroundFromHeightMap("ground", "media/graphics/sprites/heightmap.jpg", 256, 256, 50, 0, 50, wgl.game.currentScene, false, this.successCallback.bind(this));
var mat = new BABYLON.CustomMaterial("cm", wgl.game.currentScene);
mat.Fragment_MainBegin(
  "vec3 U = dFdx(vPositionW);" +
  "vec3 V = dFdy(vPositionW); " +
  "vNormalW = -normalize(cross(U,V));"
);
this.mesh.material = mat;

Ow it says 
Uncaught TypeError: BABYLON.CustomMaterial is not a constructor

Edit: I implemented this library, the error is now gone but it's still looking like in the screenshot
https://preview.babylonjs.com/materialsLibrary/babylon.customMaterial.min.js

Link to comment
Share on other sites

11 hours ago, sable said:

Seems like it would be fairly simple to do this.

Assuming that std derivatives are supported you can implement it like this.

Example playground with shader version on the left and convertToFlatShadedMesh on right: https://www.babylonjs-playground.com/#E0PJHQ

We should just add this to the standard shader now.  That will fix this whole topic then correct? As far as I can see it looks to have the same effect and then the vertex data will not have changed.

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