Skip to content

Commit 71be55d

Browse files
committed
Fix implementation notes formula
1 parent 6bc221c commit 71be55d

File tree

2 files changed

+33
-10
lines changed

2 files changed

+33
-10
lines changed

extensions/2.0/Vendor/EXT_textureInfo_constant_lod/README.md

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ The `repetitions` property specifies the number of times the texture is repeated
3636

3737
### Offset
3838

39-
The `offset` property is used to shift the texture, specified as a pair of numbers which are UV coordinates in the format [U, V].
39+
The `offset` property is used to shift the texture, specified as a pair of numbers in meters in the format [X, Y].
4040

4141
### Minimum Clamp Distance
4242

@@ -48,15 +48,38 @@ The `maxClampDistance` property specifies the maximum distance in meters from th
4848

4949
## Implementation Notes
5050

51-
This is a general formula that can be used to calculate the two different texture coordinates and how to blend them:
51+
This is a general formula that can be used to calculate the two different texture coordinates and how to blend them.
5252

53-
```
54-
logDepth = log2(v_uvCustom.z); // TODO translate to pseudocode - what is uvCustom.z again? Significance of logDepth?
55-
textureCoordinate1 = vUvCustom / clamp(pow(2.0, floor(logDepth)), minClampDistance, maxClampDistance) * repetitions
56-
// TODO note about + 1.0 here, what is vUvCustom
57-
textureCoordinate2 = vUvCustom / clamp(pow(2.0, floor(logDepth) + 1.0), minClampDistance, maxClampDistance) * repetitions
58-
result = mix(textureCoordinate1, textureCoordinate2, fract(logDepth))
59-
```
53+
In the vertex shader, where $worldPosition$ is the vertex position in world coordinates and $eyeSpace$ is the vertex position in camera coordinates:
54+
55+
$$customUvCoords = worldPosition + offset$$
56+
$$\text{if }isPerspectiveProjection \text{:}$$
57+
$$customUvCoords.z = -eyeSpace.z$$
58+
$$\text{if }isOrthographicProjection \text{:}$$
59+
$$customUvCoords.z = frustumWidth$$
60+
61+
The resulting $customUvCoords.xy$ should contain the vertex's X and Y position in world coordinates. $customUvCoords.z$ is the negative eye-space depth (z-coordinate) from the camera to the fragment when using perspective projection. Since all points are equidistant to the camera when using orthographic projection, this value should be set to the frustum width as a proxy for zoom level.
62+
63+
In the fragment shader:
64+
65+
$$logDepth = \log_{2}{customUvCoords.z}$$
66+
$$textureCoordinates_1 = customUvCoords.xy \div clamp(2^{\lfloor logDepth \rfloor}, minClampDistance, maxClampDistance) \times repetitions$$
67+
$$textureCoordinates_2 = customUvCoords.xy \div clamp(2^{\lfloor logDepth \rfloor + 1}, minClampDistance, maxClampDistance) \times repetitions$$
68+
$$result = mix(textureCoordinates_1, textureCoordinates_2, fract(logDepth))$$
69+
70+
>Note: the addition of $\lfloor logDepth \rfloor + 1$ when calculating $textureCoordinates_2$ allows the result of the $mix$ function to blend between two adjacent mipmap levels.
71+
72+
Where $clamp(x, minVal, maxVal)$ constrains the value of $x$ to the range of $minVal$ to $maxVal$, defined by the [GLSL definition](https://registry.khronos.org/OpenGL-Refpages/gl4/html/clamp.xhtml) of:
73+
74+
$$\min(\max(x, minVal), maxVal)$$
75+
76+
$fract(x)$ returns the fractional part of $x$, defined by the [GLSL definition](https://registry.khronos.org/OpenGL-Refpages/gl4/html/fract.xhtml) of:
77+
78+
$$x - \lfloor x \rfloor$$
79+
80+
and $mix(x, y, a)$ performs a linear interpolation between $x$ and $y$ using $a$ to weight between them, using the [GLSL definition](https://registry.khronos.org/OpenGL-Refpages/gl4/html/mix.xhtml) of:
81+
82+
$$x \times (1 − a) + y \times a$$
6083

6184
## JSON Schema
6285

extensions/2.0/Vendor/EXT_textureInfo_constant_lod/schema/textureInfo.EXT_textureInfo_constant_lod.schema.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
},
1616
"offset": {
1717
"type": "array",
18-
"description": "The offset of the UV coordinate origin as a factor of the texture dimensions.",
18+
"description": "The offset in meters used to shift the texture.",
1919
"items": {
2020
"type": "number"
2121
},

0 commit comments

Comments
 (0)