Jump to content

Bug with texture UVs or offsets


fenomas
 Share

Recommended Posts

Hi,

 

Latest builds have some kind of bug (or at least breaking change) related to texture offsets.

 

Reproduce:

 

1. Clone this repo (or other projects relying on texture uv offsets)

 

2. Run the local demo per instructions:

cd babylon-atlasnpm installnpm test

(Demo should work normally)

 

3. Replace /example/babylon.js with the latest version of dist/preview release/babylon.max.js

 

(Demo breaks)

 

 

The breaking commit is 6303307. The dist build from before that commit works fine.

 
Link to comment
Share on other sites

I don't know which version is correct, because I don't know what the intended behavior is.

 

That is, I got my sprite handling to work with the old version, but it was not very intuitive. Presumably I can rewrite my code to work with the new version, and it may or may not be more intuitive. But unless there's a unit test I can't tell you which is "right", that depends on your intent.

 

So to be clear this is intentionally a breaking change, right?

Link to comment
Share on other sites

It is a bug fixing :) so indirectly a breaking changes. You know that I HATE breaking changes but the behavior of uvScale/offset was wrong so it was a no brainer

 

I can help you fix your code if you want.

 

So here is a basic texture applied to a plane:

var ground = BABYLON.Mesh.CreateGround("ground1", 6, 6, 2, scene);var mat = new BABYLON.StandardMaterial("mat", scene);mat.diffuseTexture = new BABYLON.Texture("textures/misc.jpg", scene);mat.specularColor = BABYLON.Color3.Black();	ground.material = mat;

And the associated render:

post-4442-0-40432200-1443113298.jpg

 

Now let's add some offsets:

var mat = new BABYLON.StandardMaterial("mat", scene);mat.diffuseTexture = new BABYLON.Texture("textures/misc.jpg", scene);mat.diffuseTexture.uOffset = 0.6;

post-4442-0-03331700-1443113330.jpg

 

As you can see the texture "moved" to the right by a factor of 60%

 

And now le'ts add some scaling:

var mat = new BABYLON.StandardMaterial("mat", scene);mat.diffuseTexture = new BABYLON.Texture("textures/misc.jpg", scene);mat.diffuseTexture.uOffset = 0.6;mat.diffuseTexture.vScale = 10;

post-4442-0-08930300-1443113427.jpg

 

 

The issue with previous version was that scaling was wrongly computed resulting (sometimes) in wrong rendering.

 

Hope this helps :)

 

 

 

Link to comment
Share on other sites

I'm glad this makes sense to you! But to me as a user any scale/translate operation is equivalent to another, except for where the transformation's origin is, and which operation is done first. So without knowing what you're intending for those things I have no way of knowing what you think is correct. 

 

Put it this way - here is a pseudocode declaration of a texture atlas:

var texture_atlas_size = {  w: 444,  h: 333}var sprite = {  x: 123,  y: 234,  w: 34,  h: 45}

If you can tell me what you intend to be the correct code to display that sprite in a texture, then I can tell you if the current implementation is correct.  :)

Link to comment
Share on other sites

Ok so here is a small sample:

post-4442-0-67418500-1443202300_thumb.pn

 

I would like to get this one:

post-4442-0-79179000-1443202639_thumb.pn

 

Here are the coordinates (remember that with textures, the origin is at the left  bottom:

post-4442-0-20928600-1443202642_thumb.pn

 

So the code to get it is the following:

mat.diffuseTexture = new BABYLON.Texture("textures/player.png", scene, false);	mat.diffuseTexture.uOffset = 1233 / 1408;mat.diffuseTexture.vOffset = 60 / 192;mat.diffuseTexture.uScale = 51 / 1408; // Note this is the width and not the coordinate: 1284 - 1233mat.diffuseTexture.vScale = 66 / 192; // Note this is the height:126 - 60

with this result:

post-4442-0-12833500-1443202782_thumb.jp

 

and the playground:

http://www.babylonjs-playground.com/#4DVSK

Link to comment
Share on other sites

Here are the coordinates (remember that with textures, the origin is at the left  bottom:

 

That's why I said any transformation is as good as another unless you know the intent.

 

Texture atlases measure from the upper left. If the sprite you're using came from an atlas its declaration would be:

frame: {  x: 1233,  y: 66,  w: 51,  h: 66}

and the code to display it (in today's build) would be:

  uOffset = x / texturew;  vOffset = 1 - (y+h) / textureh;  uScale = w / texturew;  vScale = h / textureh;

http://www.babylonjs-playground.com/#4DVSK#1

 

If that's your intention then the current implementation is right. Like I said, without documentation or unit tests it's not really possible for anyone else to talk about correctness without knowing your intention.

Link to comment
Share on other sites

Code = intention here (I mean, there is no secret, code is open, my intention is coded inside)

 

Babylon.js is a community project. I cannot do everything by myself. Please update the doc with whatever you may find useful. This is why the project is open sourced. To work together to provide a better framework

Link to comment
Share on other sites

Code = intention here (I mean, there is no secret, code is open, my intention is coded inside)

 

Well, code only = intention if it's correct.   :)  In this case you were asking if the code was correct, so that's why I asked about intention.

 

 

Chatter aside, it would be really useful if the playground had a way to specify using BJS 2.2 or 2.1. I would like to have reported this issue with a PG, but I had no way to check that the code would have worked under 2.2 for comparison.

Link to comment
Share on other sites

Chatter aside, it would be really useful if the playground had a way to specify using BJS 2.2 or 2.1. I would like to have reported this issue with a PG, but I had no way to check that the code would have worked under 2.2 for comparison.

 

I was thinking something similar today.  A dropdown for babylonjs versions would be awesome.

Link to comment
Share on other sites

  • 3 weeks later...

Oh my fracking god!!! That particular change have broken every materials of every projects I'm working on! Fine tuned speculars, bumps, reflections, everything is affected :(

I'm not using offsets and scales to address sprites in spritesheets, I'm using them to produce materials as realistic as possible thanks to multiple UVs and textures layers usage.

 

I'm happy to see that a bug was fixed, but manually redoing hundreds of materials in a lot of materials libraries would be an endless and painfull job... :unsure: I'm sure there is a smart way to do that, based on how things have changed!

 

Please DK, would you mind providing us a quick explanation on what the bug was, what have changed and which parts of standardMaterial were affected by the fix (UVscales and offsets, every texture slots, something else ?).

I tried to find the incriminated commit but didn't managed to spot it.

 

Do you think it's possible to deduce from your changes a brief "converting rule to follow" to be able for us to properly fix materials without requiring to refine each one visually ? That would be very nice.

Link to comment
Share on other sites

Only UV scale and offset changed.

 

The simplest way for you could be to force this (use the old formula):

BABYLON.Texture.prototype._prepareRowForTextureGeneration = function(x, y, z, t) {  x -= this.uOffset + 0.5;  y -= this.vOffset + 0.5;  z -= 0.5;  BABYLON.Vector3.TransformCoordinatesFromFloatsToRef(x, y, z, this._rowGenerationMatrix, t);  t.x *= this.uScale;  t.y *= this.vScale;  t.x += 0.5;  t.y += 0.5;  t.z += 0.5;}

For the record here is the new version:

x *= this.uScale;y *= this.vScale;x -= 0.5 * this.uScale;y -= 0.5 * this.vScale;z -= 0.5;Vector3.TransformCoordinatesFromFloatsToRef(x, y, z, this._rowGenerationMatrix, t);t.x += 0.5 * this.uScale + this.uOffset;t.y += 0.5 * this.vScale + this.vOffset;t.z += 0.5;

So globally, now scaling is done BEFORE translation which was not the case before

 

Now: scaling x rotation x translation

Before: translation x rotation x scaling

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