XTK Flickering in Overlay mesh - overlay

So I have tried to overlay 2 meshes in xtk with alpha blending, setting different colors and opacities.
An example can be seen here
http://biostat.jhsph.edu/~jmuschel/XTK_Flicker_Example/
This doesn't happen when 2 meshes are both rendered in the same renderer but don't overlap.
I can't seem to understand why this would happen with the flickering.

That's the sorting algorithm screwing with you. When rendering transparent objects graphic engines like three.js or xtk like to sort the objects in the scene from back to front so the transparency is accumulated correctly (read more about it in 'Learning WebGL').
Due to your scene having one big transparent object inside another big transparent object with both having the same origin that sorting mechanism gets confused and starts flipping the objects between front and back. When the inner object is rendered first then the outer will blend its transparency with the inner, but when the outer is rendered first then the inner object will be ignored due to its surfaces lying behind the ones of the outer (depth testing).
To solve this you may try to force the inner object to be rendered first.

You can disable the renderer re-ordering (which is also the solution to this problem in three.js) by doing
r0 = new X.renderer3D();
r0.init();
r0.config.ORDERING_ENABLED = false
This way the order in which you add objects to the scene will determine the order in which they are rendered. It fixed my problem with flickering.

Related

How to make inner edge shader in unity shader graph?

I want to create a wall shader that looks like this:
Image
The inner part is already done (diagonal lines) and works fine, but I can't do the inner edge around the shape correctly. It has to lign-up properly and react to object scales and probably position
I only managed to make it work with rectangle objects any other shapes break the effect of continuity. The problem lies in the edge outline which doesn't work with such custom shapes.
I tried to do this with the Rectangle node but I could apply object scale tilling to it so I did the outline another way but as I said it's totally broken with complex shapes
Here are the results:
Image
The white on the right shape is this broken edge outline. So if someone can create an inner edge outline I would be glad to learn it!

Apart from Hierarchy, Layers and Sorts, Parent/Child, and z-axis, is there anything else controlling what appears in front on screen?

I think I understand how Hierarchy (lowest sibling rendered last, appears on top), Layers (can assign to layers - which have their own order), Sort (sort of a sub-layer), Child (rendered in front of parent), and z-axis (higher numbers go in front) control how each object appears in front or behind other objects. But -consistently - I have a TextMeshPro object that ends up behind its parent sprite (a square). Adjusting all of the 4 parameters mentioned here has no effect on the render - the text remains behind the sprite. Moreover, the sprite seems to NOT follow sorting rules (placing it high in the hierarchy makes it appear in front, whereas lower in the hierarchy makes it behind even the parent). What am I missing?
I've tried manipulating all the parameters but the text simply will not go in front of the sprite.

Render order according to hierarchy in Unity

I am trying to understand how Unity decides what to draw first in a 2D game. I could just give everything an order in layer, but I have so many objects that it would be so much easier if it was just drawing in the order of the hierarchy. I could write a script that gives every object its index, but I also want to see it in editor.
So the question is, is there an option that I can check so that it uses the order in the hierarchy window as the default sorting order?
From your last screenshot I could see you are using SpriteRenderer.
The short answer to the question "is there an option that I can check so that it uses the order in the hierarchy window as the default sorting order?" would be no, there isn't by default*.
Sprite renderers calculates which object is in front of others in one of two ways:
By using the distance to the camera, this will draw the objects closest to the camera on top of the rest of the objects in that same order in layer, as per the docs:
Sprite Sort Point
This property is only available when the Sprite Renderer’s Draw Mode is set to Simple.
In a 2D project, the Main Camera is set to Orthographic Projection mode by default. In this mode, Unity renders Sprites in the order of their their distance to the camera, along the direction of the Camera’s view.
If you want to keep everything on the same sorting layer/order in layer you can change the order in which the objects appear by moving one of the two objects further away from the camera (this is probably further down the z axis). For example if your Cashew is on z = 0, and you place the walnut on z = 1 then the cashew will be drawn on top of the walnut. If Cashew is on z=0 and the walnut is on z = -1 then the walnut will be draw on top (Since negative is closer to the camera). If both of the objects are on z - 0 they are both equally as far away from the camera, so it becomes a coin toss for which object gets drawn in front, as it does not take into account the hierarchy.
The second way the order can be changed is by creating different sorting layers, and adjusting the order in layer in the sprite renderer component. But you already figured that out.
*However, that doesn't mean it cannot be done, technically...
If you feel adventurous there is nothing stopping you from making an editor script that automates setting the order in layer for you based on the position in the hierarchy. This script would loop through all the objects in your hierarchy, grab the index of the object in the hierarchy, and assign the index to the Order in Layer.
I don't think Unity has such feature (https://docs.unity3d.com/Manual/2DSorting.html).
Usually you shall define some Sorting Layers:
far background
background
foreground
and assign Sprite Renderer of each sprite to one of Sorting Layers

Check if object is all painted

I have a brown sprite, which contains a hole with triangular shape.
I've added a trail renderer (and set its order in layer to appear behind the sprite), so the user can paint the sprite's hole without painting the sprite itself.
My question is: how can it detect when the hole is all painted?
I thought about using a shader to check if there is any black pixel in the screen, but I don't know if it's possible, because the shader won't know in what percentage of the image it is.
One way would be to take a screenshot with the ScreenCapture.CaptureScreenshotAsTexture method and then loop through an array of pixel colors from Texture2D.GetPixels32. You could then check if the array contains 'black' pixels.
I would do it in a coroutine for better performance as doing it every frame may slow down your application. Also what is important when it comes to CaptureScreenshotAsTexture according to unity docs:
To get a reliable output from this method you must make sure it is called once the frame rendering has ended, and not during the rendering process. A simple way of ensuring this is to call it from a coroutine that yields on WaitForEndOfFrame. If you call this method during the rendering process you will get unpredictable and undefined results.

Transparent shader allows the objects below to show on top

In my scene, the smileys(Quad with png image) are placed at Y:0 and the dots(Quad with tiling 3X3) are placed at Y: -0.25.
The shader I need to use for the smileys is Transparent-Diffuse as I am using a circle png image.
But the dots I use below are showing up above the smiley. Using any other shader like Diffuse solves the issue but the smiley becomes a square.
Screenshot:
If you need any more clarifications please dont hesitate to ask.
Edit:
I have attached the shader details of both the smiley and the dots from the inspector panel.
link: http://postimg.org/image/cvws1os7d/
Edit 2:
I have found that the issue should be with the MainCamera and especially with distance & "Field Of View".
I need to use "Perspective" as projection type and 140 as Field of View.
If I change the projection type to Orthographic the issue is completely fixed.
The below screenshots show how the distance and Field of View controls the appearance of the dots over the smiley.
Screenshot 1:
Y position: 8.48
Field of View: 30
link: http://postimg.org/image/s31tttrkp/
Screenshot 2:
Y position: 9.7
Field of View: 30
link: http://postimg.org/image/f71sq0y4b/
Screenshot 3:
Y position: 11.41
Field of View: 30
link: http://postimg.org/image/3uk4az3d3/
Screenshot 4:
Y position: 1
Field of View: 140
link: http://postimg.org/image/bul9zwg7z/
Can this be a clue?
Just a couple of info, on how transparency is typically implemented (not only by Unity).
Meanwhile opaque objects can be drawn in any order (even if sorting them in front-to-back order can eventually improve some GPU performances relying on an early z-cull). Which pixels are visible can be deduced using the depth value stored into the z-buffer.
You can't rely on z-buffer for transparency.
For rendering translucent objects a typical approach is to draw them after all opaque objects, and sorting them in back-to-front order (transparent objects more distant from the camera are drawn first).
Now the question is: how do you sort objects? with a perspective camera and meshes of a generic shape, the solution is not obvious.
For quad meshes oriented parallel to a ortographic camera view plane, the z order is implicitly correct (that's why it always works for you).
You can also notice that camera position influences the drawing order, because with perspective camera the order is calculated as distance between object position and camera.
So what can you do with Unity3d, in your specific use case scenario?
A couple of tricks:
Explicitly set the render queue of the material
Explicitly set the render order inside the shader (similar of the above, but equals to every object with the same shader)
Fake the depth using Offset into the shader (not that useful in your case but worth to be known)
hope this helps
EDIT
I didn't know that, the camera transparency sorting mode appears to be customizable. So this is another solution, maybe the best for your case if you want to use a perspective camera.
If you are using Sprite Renderer component to render the images, you have to change the rendering order with Sorting Layer and Order in Layer parameters instead of changing the Y position.
Sorting layers can be added by clicking the "default" and choosing "Add Sorting Layer..". The order of the layers is changed by dragging them into different order. With Order in Layer lower numbers are rendered first. This means that higher numbers will be drawn on top of lower ones.