shaders – Generating the distance of an intersection through a box from the camera in Unity C#

I am not completely sure I understand what you’re asking, because it seems like you linked to the wrong timestamp in the video. It sounds like you’re asking how to change the color based on distance from the camera. If so:

Unity has a built-in shader variable _WorldSpaceCameraPos which represents the world-space position of the camera.

You can track the world-space position of a pixel in a vertex/fragment shader like this:

struct appdata {
    float4 vertex : POSITION;
};

struct v2f {
    float4 vertex : SV_POSITION;
    float3 worldPos : TEXCOORD0;
};

v2f vert (appdata v) {
    v2f result;
    result.vertex = UnityObjectToClipPos(v.vertex);
    //Calculate the world position of this pixel:
    result.worldPos = mul(unity_ObjectToWorld, v.vertex);
    return o;
}

Then in your frag function you can check the distance and perform math based on that:

fixed4 frag(v2f i) : SV_Target {
    //calculate distance from camera to pixel
    float dist = distance(_WorldSpaceCameraPos, i.worldPos);
    
    //do some math using the distance to determine your final color.
    //this is a very simple example:
    float blue = (1000 - dist) / 1000; //black at 1000m, pure blue at 0m
    blue = clamp(blue, 0, 1);
    fixed4 color = fixed4(0, 0, blue, 1);
}

Edit: Okay, now having seen the correct section of the video, it seems like you’re wondering how to shade the ocean based on the depth of the ocean. I don’t think my above code is directly applicable.

I am a little confused about the approach he’s using in the video. He says “this shader will be run for every pixel on the screen”, which sounds like he’s using a screen-space shader. I may be missing something, as I have not watched the entire video, but that does not seem like an optimal solution to me. If I understand correctly, he has to do a bunch of extra math to determine which pixels, out of all of the pixels on the screen, are actually part of the ocean, then shade them according to the depth buffer.

The way I would do this would be to use a material shader, not a screen-space shader. If you were making a spherical world like in the video, you would do this:

  1. Add a sphere to the scene at the same position as the planet, and give it a radius that matches your desired ocean depth.
  2. Author a material shader that determines color based on the depth buffer, similar to way he does in the video.
  3. Create a material that uses the material shader from step 2, and assign it to the sphere you created in step 1. Now you have your ocean and don’t need to perform screen-space calculations to determine which pixels onscreen should be considered “ocean” pixels.

If you’re doing a flat world, just make the ocean a plane instead of a sphere, and position it to achieve your desired ocean depth.

machine learning – Distance of two points in Grassmannian using Pl”{u}cker coordinate

Let $G(q,D)$ be the Grassmannian of $q$-dimensional vector spaces in $mathbb{R}^D$, where $q le D$ are positive integers. In the paper, a distance between two points of $G(q,D)$ are defined as follows. The authors represented a point in $G(q,D)$ by a matrix $X$ of size $D times q$ such that $X^T X$ is a $q times q$ identity matrix. For two points $X_1, X_2 in G(q,D)$, they define $d(X_1,X_2) = 1/sqrt{2} || X_1X_1^T – X_2X_2^T ||_F$, where $||cdot ||_F$ is the matrix Frobenius norm. Could $d(X_1,X_2)$ be defined using Pl”{u}cker coordinates in $mathbb{C}(G(q,D))$? Thank you very much.

formal languages – Largest set of 10-digit numbers where none have Hamming Distance = 1 with any other

I’m working on a system that will require manual data entry of 10-digit numbers (Σ = 0123456789). To help prevent data errors, I want to avoid generating any two strings that have a hamming distance of 1.

For example, if I generate the string 0123456789 then I never want to generate any of these strings: {1123456789, 2123456789, 3123456789, …}

What is the largest set of unique strings in the universe of possible strings that satisfy the constraint where no two strings have a hamming distance of 1? If this set can be identified, is there any reasonable way to enumerate it?

Set a minimum distance with navmeshagent

I am totally newb. How to set a minimum distance between AI chasing a player if AI has navmeshagent

        if (Vector3.Distance(transform.position, target.position) > minDistance)
        {
            enemy.destination = player.position;
        }

calculus – Max distance a motorcycle can travel?

This motorcycle goes on with two tires and have one extra with it. If the max distance a tire can go is 5 kilometers then what is the max distance the motorcycle can go with 3 tires and unlimited shuffle of tires.

Looking for highly efficient exponential or integration solution. A regular solution would give us 7.5 KM which is highly inefficient.

C++ STL: How does the distance() method work for a set/ multiset (stored internally as a self balancing tree)?

The complexity of the distance function depends on the type of the iterators supplied: in general it only required to take linear time in the distance but, in the special case in which the input iterators are random access iterators, the worst-case running time is linear. (I believe this is accounting for the time spent in the function in itself, and assumes that the time needed to advance iterators is constant).

The C++ specification do not mandate any particular implementation as long as it conforms to the required complexity so your question cannot be answered without inspecting the particular implementation that you are using.
However, just to convey the intuition, here are two possible implementations that would conform to the requirements:

  • Given random access iterators $x$ and $y$, distance($x$, $y$) returns $y-x$.
  • For general iterators increment $x$ until it equals $y$. Return the number of increments performed.

The type std::set does not return a random access iterator, therefore std::distance can take linear time and the second implementation above can be used. Now your question reduces to “how can the standard library iterate over the elements of a std::set in sorted order?”

The answer to this question depends once again on the implementation as there is no particular data structure mandated by the standard to implement std::set.
Since you mention red-black trees, which are a special kind of BSTs, this can easily be done by noticing that the order of iteration coincides with the order in which the vertices of a BST are visited by an in-order visit.

Notice that the concept of distance completely abstracts from the data structure used to store the elements of the set. Instead it only refer to the positions in which two elements appear when using an iterator to access the collection’s contents (in the case of std::set, the elements appear in sorted order).

calculations – What is the relationship between size of object with distance?

Inversely linear is a good approximation.
Imagine a 1,7m tall girl at 1 m distance b. Her head is at point B.
triangle that name corners and sides. Source: Wikipedia Article "Arctangent". This file is licensed under the Creative Commons Attribution-Share Alike 3.0 Unported license.

How does the size/length of an object vary with distance?

Let the girl walk away from you. Her size a stays the same. She appears smaller, because she is appearing under a smaller angle. Her angular size changes. Try to imagine it with the picture attached.
Using arctangent to calculate her angular size is the correct way. For small angles you can simplify:

Angular size is inversely proportional to its object distance, without using optical devices.

An object on full-field with focal length of 12 mm would be measured incorrectly. An error 2-5% in length measurement may be made. For fish-eye lenses this may be even worse. Hands-on rule: Use the inverse relationship if angular size is smaller than 10°.

nt.number theory – P-adic distance between solutions to S-unit equation

Let $p$ be a fixed prime number and $S$ is a finite set of prime numbers which does not contain $p$. A theorem of Siegel asserts that the number of solutions to the $S$-unit equations are finite; that is, there are only finitely many $S$-unit $u$ such that $1-u$ is also an $S$-unit. Therefor for each such $S$ there exist a lower bound on $|u_1-u_2|_p$ where $u_1$ and $u_2$ are solutions to $S$-unit equations.

My question is: does there exist such a lower bound uniformly? More precisely, does there exist a lower bound for the $p$-adic distance between solutions to the $S$-unit equations that only depends on the size of $S$(and perhaps on $p$)? Here we are assuming $S$ does not contain $p$.

dynamic programming – What is the difference between these two Edit Distance Algorithm

Edit Distance is very well known problem in computer science. Came up with following algorithm after reading through CLRS but it doesn’t work. Check the working algorithm below, I couldn’t find why first algorithm doesn’t work while second one does.

  public int find (String word1, String word2, int i, int j, int count) {
    if (i >= word1.length()) return  count + word2.length() - j;
    if (j >= word2.length()) return  count + word1.length() - i;

    if (dp(i)(j) != -1) return dp(i)(j);

    if (word1.charAt(i) == word2.charAt(j)) {
        dp(i)(j) = find(word1, word2, i+1, j+1, count);
    } else {
        int replace = find(word1, word2, i+1, j+1, count + 1);
        int delete = find(word1, word2, i+1, j, count + 1);
        int insert = find(word1, word2, i, j+1, count + 1);
        
        dp(i)(j) = Math.min(replace, Math.min(delete, insert));
    }

    return dp(i)(j);
}

Notice, how I’m passing the cost of edit in method argument. Now, the algorithm which works. Here I’m not passing the edit distance in the method parameter instead of I’m adding 1 to recursive method.

  public int find (String word1, String word2, int i, int j) {
    if (i >= word1.length()) return  word2.length() - j;
    if (j >= word2.length()) return  word1.length() - i;

    if (dp(i)(j) != -1) return dp(i)(j);

    if (word1.charAt(i) == word2.charAt(j)) {
        dp(i)(j)  = find(word1, word2, i+1, j+1, count);
    } else {
        int replace = find(word1, word2, i+1, j+1, count + 1);
        int delete = find(word1, word2, i+1, j, count + 1);
        int insert = find(word1, word2, i, j+1, count + 1);
        
        dp(i)(j) = 1 + Math.min(replace, Math.min(delete, insert));
    }

    return dp(i)(j);
}

I’m not able to think why first algorithm fails. Appreciate, if you can point my error in understanding.

fa.functional analysis – The least distance of $finell_infty(K,X)$ to $C_b(K,X)$

Let $X$ be a paracompact space and consider a bounded function $f:Ktomathbb R$ not necessarily continuous, that is, $finell_infty(K,mathbb R)$. It’s a well-known fact that the least distance of $f$ to some continuous $g:Kto mathbb R$ is half of the maximum oscilation of $f$. More precisely, we define the oscilation of $f$ at $k$ by
$$
osc_f(k) = inf_{Uinmathcal V_k} sup_{u,vin U} |f(u)-f(v)|,
$$

where $mathcal V_k$ is the set of open neighborhoods of $k$, and we have the following result, which is the Proposition 1.18.(ii) of the book Geometric Nonlinear Functional Analysis, by Y. Benyaminni and J. Lindenstrauss:

Let $f:Ktomathbb R$ and put $delta =|osc_f|_infty$, where $|cdot|_infty$ is the supremum norm. Then, there exists a continuous function $g:Ktomathbb R$ such that
$$
|f-g|_infty=frac{delta}{2},
$$

and this is the least distance of a continuous function from $f$.

This proposition is an application of Michael’s Selection Theorem, which is valid for any selection with domain in a paracompact space and counter-domain in the set of non-empty closed convex subsets of a Banach space $E$. Recently, I’ve proved a generalization of the proposition above for functions with counterdomain in $mathbb C$ (which I posted here). Of course the measure of the oscilation changed, but we still get the least distance of $f$ to a continuous function.

I can see, by my proof, that it can be extended to Hilbert spaces in general, but, I wonder if this result can be extended to a much more general class, I would say the uniformly convex Banach space, or something close to that.

My question is if there exists a result analogous to the proposition for a more general class of Banach spaces, or if someone could guide me on an argument that could be valid to prove such a result.