Knowing a rectangle (x, y, width, height) on a certain texture, how to set this region of a texture on a quad?
After seeing unshaded and spritesheet materials I’m achieving this via custom material which has four parameters - one for width ratio, the other for horizontal offset ratio and likewise other two for vertical coordinates and I’m computing tex coordinates in that custom material as follows:
void main() {
texCoord.x = inTexCoord.x * m_dw + m_dx;
texCoord.y = inTexCoord.y * m_dh + m_dy;
gl_Position = g_WorldViewProjectionMatrix * vec4(inPosition, 1.0);
}
I’m pre-computing m_dw
parameter as a ratio of crop rectangle width and texture width etc.
It works fine although I feel I’m missing something very basic and the same could be achieved much simpler. Is there another, simpler or maybe more efficient way of achieving the same?
If not, I can live with what I currently have and the only thing I’d love to improve is having an access to texture pixel width and height in the vert file. That way I could pass rectangle parameters as is to the material without computing dw, dh, dx, dy ratios. I was thinking that it is attribute vec2 inTexCoord;
but apparently it is not the right one. I did look at some definitions in jmonkeyengine/jme3-core/src/main/resources/Common/MatDefs/Misc at master · jMonkeyEngine/jmonkeyengine · GitHub but didn’t find an obvious clue how it could be done.