You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Description: Drawing sprites that sort pixels by depth
3
+
TOC: How to draw Depth Sprites
4
+
5
+
## Question:
6
+
7
+
How can I draw sprites that sort each pixel by depth?
8
+
9
+
## Answer:
10
+
11
+
To use depth sprites you need to enable the [`EXT_frag_depth`](https://www.khronos.org/registry/webgl/extensions/EXT_frag_depth/) extension if it exists. Then you can write to `gl_fragDepthEXT` in your fragment shader. Making depth sprites sounds like more work to me than making 3D models.
12
+
13
+
In that case you just load 2 textures per sprite, one for color, one for depth and then do something like
You'd set `depthOffset` and `depthScale` to something like
39
+
40
+
var yTemp = yPosOfSpriteInPixelsFromTopOfScreen + tallestSpriteHeight;
41
+
var depthOffset = 1. - yTemp / 65536;
42
+
var depthScale = 1 / 256;
43
+
44
+
That assumes each value in the depth texture is less per depth change.
45
+
46
+
As for how to draw in 2D in WebGL [see this article](webgl-2d-drawimage.html).
47
+
48
+
Here's an example that seems to work. I generated the image because I'm too lazy to draw it in photoshop. Manually drawing depth values is pretty tedious. It assumes the furthest pixel in the image of depth values of 1, the next closest pixels have a depth value of 2, etc.
49
+
50
+
In other words if you had a small 3x3 isometric cube the depth values would be something like
The top left is what the image looks like. The top middle is 2 images drawn side by side. The top right is 2 images drawn one further down in y (x, y is the iso-plane). The bottom left is two images one drawn below the other (below the plane). The bottom middle is the same thing just separated more. The bottom right is the same thing except drawn in the opposite order (just to check it works)
75
+
76
+
To save memory you could put the depth value in the alpha channel of the color texture. If it's 0 discard.
0 commit comments