<p>In order to ensure that the pattern shapes are always either wholly present or absent, never cut off, it's necessary to ensure that the same <code>p</code> value is used for all texels within the shape. In your example of circles, all the texels in a given circle need to agree on <code>p</code>.</p>

<p>I assume that you have some way of evaluating <code>p</code> at a given point on the surface (whether it is looked up from a texture or calculated from some function). Then one way to ensure a group of texels all get the same <code>p</code> value is to ensure they all look it up from the same point.</p>

<p>The UVs of this evaluation point could be stored in extra channels of the pattern texture. For instance, you could have the red and green channels store the UV coordinates at which to evaluate <code>p</code>, the blue channel store the threshold at which to turn on that pattern element, and the alpha store the antialiased gray level of the pattern to display. The UV+threshold data could also be in a separate, secondary texture if desired.</p>

<p>To generate this UV+threshold texture, starting from an input pattern texture, you could programmatically find connected components (e.g. by searching for black pixels and flood-filling). Set the evaluation point for all texels in each component to the UV of the component's center, and generate a random threshold for it. Then, when rendering, use a pixel shader that first samples this texture, then looks up <code>p</code> at the given evaluation point and compares it to the given threshold.</p>

<p>That way, each pattern shape will see a uniform <code>p</code> value and threshold, and will either turn on or turn off fully. As <code>p</code> increases, more of the shapes will pass their threshold and appear, giving the impression of a continuously varying density.</p>

CC BY-SA 3.0
2016-03-12T02:49:48.353Z
I really like this idea, I'll have to try it out. It makes perfect sense. Thanks!
Ryan
CC BY-SA 3.0
2016-03-12T02:40:28.083Z
2016-03-12T02:40:28.083Z
Nathan Reed
<p>Another thought is along these lines.  If you have a uniform point set defined on the plane and a mapping function from the plane to the target surface then the density function on the surface is how areas map from the plane surface to the target.  So an area preserving map would result in a uniform points on the target.</p>

CC BY-SA 3.0
2016-03-14T10:32:47.963Z
2016-03-14T10:32:47.963Z
MB Reynolds
<p>Assume there is some value <code>p</code>, calculated per-frame, that varies continuously over the surface of an object. The value of <code>p</code> determines the density of some pattern on the surface. For example, in a case with only two possible densities if <code>p &lt; 0.3</code> it is high density, otherwise it is low.</p>

<p>I've thought of a simple high-level solution: create two textures, each of different densities, and based on the value of <code>p</code> sample from the appropriate one. However, there's a problem with the boundary between high- and low-densities.</p>

<p>Here is an example to illustrate the problem (note my problem is NOT exclusive to this example pattern of dots. I describe the patterns I'm working with later on):</p>

<p><a href="https://i.sstatic.net/krVHY.png" rel="noreferrer"><img src="https://i.sstatic.net/krVHY.png" alt="enter image description here"></a></p>

<p>And here is the threshold between low- and high- (displayed on the high density texture but that's not relevant.) If under the line, it implies the high-density texture should be sampled.</p>

<p><a href="https://i.sstatic.net/6WmhA.png" rel="noreferrer"><img src="https://i.sstatic.net/6WmhA.png" alt="enter image description here"></a></p>

<p>And finally here is the comparison between what is desired and what would actually happen using this method:</p>

<p><a href="https://i.sstatic.net/DFsgY.png" rel="noreferrer"><img src="https://i.sstatic.net/DFsgY.png" alt="enter image description here"></a></p>

<p>The problem is that when a high-density-only circle crosses the line, it will be ignored when <code>p</code> indicates the low-density texture to be sampled, resulting in a truncated circle. I don't know how to solve this problem because <code>p</code> varies every frame, so I can't just 'bake' a boundary between the two densities. It's easy to prevent the reverse problem by creating the high-density texture from the lower one (i.e. if a circle is on the low-density texture ensure it is on the high-density texture.) </p>

<p>I'm interested if anyone has a way to improve my solution or even has another method entirely. The constraint here is that <code>p</code> is calculated per-frame in real-time. Another constraint is related to the pattern texture: the pattern is black and white, where black is the pattern and white is the background (like the circles in the example). The pattern may not just be identical shapes repeated, but any arrangement of arbitrary black shapes over a white background. (Maybe pattern is the wrong choice of word.)   </p>

<p>I'm not familiar with the research in this field so I wasn't sure which keywords to search, so I'd even appreciate if anyone could point me in the right direction.</p>

CC BY-SA 3.0
2016-01-26T07:37:53.980Z
Is your actual texture also just black dots on white? If so, you might be able to generate the texture procedurally, e.g. with [Poisson disc sampling](http://devmag.org.za/2009/05/03/poisson-disk-sampling/) with varying radii. Otherwise, if your texture is actually something more continuous, blending might be an option (that wouldn't work well for crisp textures like your example though). So if you could clarify what your actual pattern/texture looks like that might help getting more helpful answers.
Martin Ender
CC BY-SA 3.0
2016-01-27T02:06:31.343Z
Another option is to look into Wang tiling.  [introduction](http://procworld.blogspot.com/2013/01/introduction-to-wang-tiles.html), [tile genetics](http://procworld.blogspot.com/2013/01/tile-genetics.html), [GPU gems](http://http.developer.nvidia.com/GPUGems2/gpugems2_chapter12.html)
maogenc
CC BY-SA 3.0
2016-01-27T04:13:21.460Z
@MartinBüttner good point, I've added a line about the type of texture
Ryan
CC BY-SA 3.0
2016-01-27T08:24:37.303Z
Have you looked at texture bombing? http://http.developer.nvidia.com/GPUGems/gpugems_ch20.html
Mikkel Gjoel
CC BY-SA 3.0
2016-01-29T12:19:35.967Z
Can you give an idea of the range of shapes/patterns you need to cover and how much flexibility there can be in the approach? @DanielMGessel's suggestion of changing the circle radii instead of their density might make things simpler and give smoother animation but it's not yet clear how much your approach is constrained.
trichoplax is on Codidact now
CC BY-SA 3.0
2016-02-06T06:25:19.577Z
How is define the pattern ? as an image of pixels, or as a noise function, a procedural field, etc, with some threshold to make in B&W ? in the letter case you could adapt the value of the threshold.

Anyway, we need more information about what is the texture to be able to answer.
Fabrice NEYRET
CC BY-SA 3.0
2016-03-15T18:53:54.283Z
I just came across this paper from SIGGRAPH 2006 that talks about a way to do this using wang tiles.
Recursive Wang Tiles for Real-Time Blue Noise
http://johanneskopf.de/publications/blue_noise/
Alan Wolfe
<texture><shader><real-time>
How to create a patterned object with variable pattern density in real-time?
Ryan
# How to create a patterned object with variable pattern density in real-time?

(Asked by: username_0 on 2016-01-25)

Assume there is some value `p`, calculated per-frame, that varies continuously over the surface of an object. The value of `p` determines the density of some pattern on the surface. For example, in a case with only two possible densities if `p < 0.3` it is high density, otherwise it is low.

I've thought of a simple high-level solution: create two textures, each of different densities, and based on the value of `p` sample from the appropriate one. However, there's a problem with the boundary between high- and low-densities.

Here is an example to illustrate the problem (note my problem is NOT exclusive to this example pattern of dots. I describe the patterns I'm working with later on):

[![enter image description here](https://i.sstatic.net/krVHY.png)](https://i.sstatic.net/krVHY.png)

And here is the threshold between low- and high- (displayed on the high density texture but that's not relevant.) If under the line, it implies the high-density texture should be sampled.

[![enter image description here](https://i.sstatic.net/6WmhA.png)](https://i.sstatic.net/6WmhA.png)

And finally here is the comparison between what is desired and what would actually happen using this method:

[![enter image description here](https://i.sstatic.net/DFsgY.png)](https://i.sstatic.net/DFsgY.png)

The problem is that when a high-density-only circle crosses the line, it will be ignored when `p` indicates the low-density texture to be sampled, resulting in a truncated circle. I don't know how to solve this problem because `p` varies every frame, so I can't just 'bake' a boundary between the two densities. It's easy to prevent the reverse problem by creating the high-density texture from the lower one (i.e. if a circle is on the low-density texture ensure it is on the high-density texture.)

I'm interested if anyone has a way to improve my solution or even has another method entirely. The constraint here is that `p` is calculated per-frame in real-time. Another constraint is related to the pattern texture: the pattern is black and white, where black is the pattern and white is the background (like the circles in the example). The pattern may not just be identical shapes repeated, but any arrangement of arbitrary black shapes over a white background. (Maybe pattern is the wrong choice of word.)

I'm not familiar with the research in this field so I wasn't sure which keywords to search, so I'd even appreciate if anyone could point me in the right direction.

**Tags:** texture, shader, real-time

> **Comments:**
> **username_1**: Is your actual texture also just black dots on white? If so, you might be able to generate the texture procedurally, e.g. with [Poisson disc sampling](http://devmag.org.za/2009/05/03/poisson-disk-sampling/) with varying radii. Otherwise, if your texture is actually something more continuous, blending might be an option (that wouldn't work well for crisp textures like your example though). So if you could clarify what your actual pattern/texture looks like that might help getting more helpful answers.  
> **username_2**: Another option is to look into Wang tiling.  [introduction](http://procworld.blogspot.com/2013/01/introduction-to-wang-tiles.html), [tile genetics](http://procworld.blogspot.com/2013/01/tile-genetics.html), [GPU gems](http://http.developer.nvidia.com/GPUGems2/gpugems2_chapter12.html)  
> **username_0**: @MartinBüttner good point, I've added a line about the type of texture  
> **username_3**: Have you looked at texture bombing? http://http.developer.nvidia.com/GPUGems/gpugems_ch20.html  
> **username_4**: Can you give an idea of the range of shapes/patterns you need to cover and how much flexibility there can be in the approach? @DanielMGessel's suggestion of changing the circle radii instead of their density might make things simpler and give smoother animation but it's not yet clear how much your approach is constrained.  
> **username_5**: How is define the pattern ? as an image of pixels, or as a noise function, a procedural field, etc, with some threshold to make in B&W ? in the letter case you could adapt the value of the threshold.  Anyway, we need more information about what is the texture to be able to answer.  
> **username_6**: I just came across this paper from SIGGRAPH 2006 that talks about a way to do this using wang tiles. Recursive Wang Tiles for Real-Time Blue Noise http://johanneskopf.de/publications/blue_noise/  

---

## 2 answers

### Answer by username_7 on 2016-03-12. Score: 6

In order to ensure that the pattern shapes are always either wholly present or absent, never cut off, it's necessary to ensure that the same `p` value is used for all texels within the shape. In your example of circles, all the texels in a given circle need to agree on `p`.

I assume that you have some way of evaluating `p` at a given point on the surface (whether it is looked up from a texture or calculated from some function). Then one way to ensure a group of texels all get the same `p` value is to ensure they all look it up from the same point.

The UVs of this evaluation point could be stored in extra channels of the pattern texture. For instance, you could have the red and green channels store the UV coordinates at which to evaluate `p`, the blue channel store the threshold at which to turn on that pattern element, and the alpha store the antialiased gray level of the pattern to display. The UV+threshold data could also be in a separate, secondary texture if desired.

To generate this UV+threshold texture, starting from an input pattern texture, you could programmatically find connected components (e.g. by searching for black pixels and flood-filling). Set the evaluation point for all texels in each component to the UV of the component's center, and generate a random threshold for it. Then, when rendering, use a pixel shader that first samples this texture, then looks up `p` at the given evaluation point and compares it to the given threshold.

That way, each pattern shape will see a uniform `p` value and threshold, and will either turn on or turn off fully. As `p` increases, more of the shapes will pass their threshold and appear, giving the impression of a continuously varying density.

> **Comments:**
> **username_0**: I really like this idea, I'll have to try it out. It makes perfect sense. Thanks!  

---

### Answer by username_8 on 2016-03-14. Score: 0

Another thought is along these lines. If you have a uniform point set defined on the plane and a mapping function from the plane to the target surface then the density function on the surface is how areas map from the plane surface to the target. So an area preserving map would result in a uniform points on the target.


---

