Overview

A recent addition to WorldCraft is the Fit button, which applies the necessary scales to make a texture fit exactly once over a face. This is often handy, but also it frequently falls short of the ideal. Say you have a 16 unit texture of a concrete tile, and a 40 unit wall to fit it along (we shall concern ourselves with only one dimension for now). Clicking Fit at this stage will give you one huge tile, badly distorted to fit the wall. However, if it is not fitted, you will be left with about half of a tile hanging off the end of the wall. This will look rather bizarre with a concrete slab. Ideally, you want to fit the texture a whole number of times, but with the minimum amount of distortion. Traditionally, this has been accomplished by trial and improvement with the offset and scale variables. But you can do it mathematically, much more accurately. Here's how.

Calculating the Scale Factor

We will now be working with a texture of dimension T tiling over a face of dimension F. First of all, we work out exactly how many times the texture will fit this face: this is F/T. Now clearly the minimum distortion will be achieved if we fit the texture the number of times equal to the nearest integer. Look at it like this:

<--T-->
 ______________
|     |     |  |
|     |     |  |
|_____|_____|__|

<-------F------>

Here, the texture fits just about 2.4 times. Therefore, for minimum distortion, we fit it 2 times. In general, the ideal number of times is round(F/T, 0). In other words, the closest integer to F/T. We will call this ideal fit E.

Now we calculate what dimension of texture would fit the face E times. This is simple: it's F/E, and we call this I. Now we know the base dimension of the texture being applied, and the dimension that we need it to be. So the scale factor required is the ratio of the ideal size to the true size: ie, I/T. For the purposes of WorldCraft, we need to round that to 2DP.

You're thinking "What? I'll never actually work that out!" So was I, when I devised this. But in fact, as the numerous references to pseudocode may have suggested, it works very well as a computer program. I wrote it first for my graphic calculator, but it could easily be ported to other languages if the mood takes you.

The only provision you will need is to check that neither of the entered dimension are 0, because then you will get a Division By Zero error. Anyway, here's the original listing for my Sharp EL-9400, in it's own interpretation of BASIC; the code is barely optimized, so you should have no trouble porting it if you desire. I've commented it too, although that's not actually supported by the calculator.

Print "Tex Dim:"
Label ENTERT
Input T
If T=0 Goto ENTERT //If they've entered an invalid value, make them enter again
Print "Face Dim:"
Label ENTERF
Input F
If F=0 Goto ENTERF//If they've entered an invalid value, make them enter again
T*round(F/T,0) » E
round(F/E,2) » S
Print "Scale Factor:"
Print S

For the original example of T=16 and F=40, the correct scale is 0.83. This might help you check that your adaptation of the program is working correctly, or it may just satisfy your curiosity. Enjoy.

Log in or register to write something here or to contact authors.