pardo

DynamicTexture with not power of 2 size not covering width/height

Recommended Posts

Hi Everyone,

I'm looking into this

mesh = BABYLON.MeshBuilder.CreatePlane("mesh", {
  width: 10,
  height: 10,
  sideOrientation: BABYLON.Mesh.DOUBLESIDE
}, scene);

mesh.material = new BABYLON.StandardMaterial(null, scene);
texture = new BABYLON.DynamicTexture("texture", {
  width: 10,
  height: 10
}, scene, false, BABYLON.Texture.NEAREST_SAMPLINGMODE);

mesh.material.diffuseTexture = texture;

context = texture.getContext();
context.fillStyle = '#F00';
context.fillRect(0, 0, 100, 100);

context.fillStyle = '#00F';
context.fillRect(0, 0, 15, 5);
texture.update();

// Trying to fix it like ( WIP )

mesh.material.diffuseTexture.uScale = 10 / mesh.material.diffuseTexture._canvas.width;
mesh.material.diffuseTexture.vScale = 10 / mesh.material.diffuseTexture._canvas.height;

On the internal the framework is forcing the texture to be power of 2 which is ok, but my problem is that the new canvas size is 16x16 instead of 10x10 but the way UV are mapped makes that the 10x10 size will only cover a portion of the plane instead of the full width/height. 

Some references on the code.

DynamicTexture constructor

https://github.com/BabylonJS/Babylon.js/blob/8220f1520a9faf4779ba7c0280514fb8f724b524/src/Materials/Textures/babylon.dynamicTexture.ts#L19

Engine.createDynamicTexture

https://github.com/BabylonJS/Babylon.js/blob/eefc1faaf1261143658c3c07e7f86b8f22f65b6e/src/babylon.engine.ts#L1815

 

Example: 

http://www.babylonjs-playground.com/#1MCWZJ#0

 

Share this post


Link to post
Share on other sites

Hi All , I got a fix

//FIX
texture.uScale = 10 / texture._canvas.width;
texture.vScale = 10 / texture._canvas.height;
// when drawing with the canvas context the top left corner should be (0,0)        
texture.vOffset = 1 - texture.vScale;

texture.uScale -= 0.001; // fix some texture bleeding
texture.vScale -= 0.001; // fix some texture bleeding

http://www.babylonjs-playground.com/#1MCWZJ#1

Share this post


Link to post
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...

  • Recently Browsing   0 members

    No registered users viewing this page.