Using a sprite to mask 3D objects in Unity - unity3d

I have a png image that I want to work as a transparent mask, where it will be completely transparent in the scene but 3D objects behind it would not be shown (similar to a sprite mask, but needs to work with 3D objects).
So I added the image as a sprite (point - no filter) in my 3D scene and I have a shader already applied to the sprite material, but it is not working properly. Looks like the pixels are not being "cut correctly" in the borders.
I'm using this approach since the unity built-in "2D sprite mask" only affects objects using Sprite Renderer (as explained here https://docs.unity3d.com/Manual/class-SpriteMask.html), but I want it to affect 3D objects.
So this is the shader:
Shader "Masked/Mask" {
SubShader {
Tags {"Queue" = "Geometry+10" }
ColorMask 0
ZWrite On
Pass {}
}
}
This is the sprite to work as the mask (png image):
sprite mask
And these are the result that I want vs. what Im getting:
results compared
Im not good with shaders, so I would like to know if there is a change in the code that can be made in order to improve the result. I've already tried different formats for the sprite image (png, tif, compressions), but the results are the same.

I've done something similar using 'transparent' 3D occluders and render priorities, I imagine it would be similar with sprites...
Render non-occludable geometry (including Sprite) {"Queue" = "Geometry"}
Render occludable geometry {"Queue" = "Geometry+1"}
Strictly speaking, your occluder (the sprite) only needs to render to the depth (Z) buffer if it's always transparent.

Related

Shader: Mesh Renderer and Sprite Renderer are not respecting sorting order

I have a fbx model that uses Unity's mobile unlit shader and another sprite that uses a custom shader. I have checked that they are both in the same renderer queue, however, they don't draw in respect of the sorting order but of the depth only.
When I have sorting order of the sprite renderer set to 100 and sorting order of the mesh renderer set to 0, I'm expecting to see the sprite drawn in front of the model, regardless of the depth. But that's not what I am getting.
Currently, the 3D model is using Unity's built-in mobile unlit shader (https://github.com/TwoTailsGames/Unity-Built-in-Shaders/blob/master/DefaultResourcesExtra/Mobile/Mobile-Lightmap-Unlit.shader),
and my custom sprite shader has these flags set in a SubShader block:
Lighting Off
Cull Off
ZWrite Off
BlendOp [_OpColor]
Blend [_SrcFactor] [_DstFactor]
Please let me know if there are ways to achieve this?

shader graph messes up the texture (Unity 2D)

i am trying to make a glow shader but. when i add that _MainTex property this happens:-
shaded
shaded wireframe
I tried changing the mesh type to full rect but when i do that this happenfull rect mesh types :-
I don't know whats the problem plzz help , i am new to shader graph and i am using "sprite shader graph unlit" i also tried with but same problem . here's my shader graph :-shader Graph
The Sample Texture 2D node returns a vec4, but your Fragment output only accepts a vec3 and thus ignores the alpha information.
If you want the shader to also use the alpha values of your texture, you should plug the A output of the Sample Texture 2D node into the Alpha input of the Fragment.
You are currently returning the color values of the texture for all pixels and not filtering by alpha.
If this does not work, check if you have to enable translucency for the shader.

How to mask a UI.RawImage in Unity?

In Unity UI, I have an ordinary RawImage
(It's just sitting on a Panel)
I have a png, mask.png which is just a white shape on transparent.
How do you mask the RawImage ?
I have tried Mask, SpriteMask in many ways and it just won't work.
Even RectMask2D would be fine (to mask to a square shape) but it just doesn't seem to work?
Should I use Mask or SpriteMask
If so, do you perhaps have to / have to not set a Material, on the mask? On the RawImage?
I assume the Mask game object should be the parent of the RawImage, but??
What is the secret ?
The RawImagecomponent should work with masks just like your normal Image component does. Granted that the checkmark Maskable is ticked.
Note that the Mask or rect Mask 2D should be the parent of the (raw)images you are trying to mask. The hierarchy should be something like this:
Canvas<br>
| MaskObject (Contains (Raw)Image and Mask or Rect Mask 2d components)
| Object to mask (Contains the (raw)image to mask)
Notice how the white square (Image) gets cut off by the red square (Mask).
The component types between the masking image and the masked image do not need to match either. A RawImage can mask an Image and vice versa.
The Masking objects are again shown in red, where the white are the masked objects. The GameObject's names show the used (raw)image component for that gameobject.
The only exception is the SpriteMask which exclusively works with the Sprite Renderer component
There is not much explanation from Unity on masks... This being the closest to an explanation there is
Some more info about masks:
Masks work by comparing the ref(erence) values of the stencil buffers of the two (or more) objects (in this case images) and only drawing the pixels where the stencil buffer for both equals to 1 using the Stencil's Comp(arison) function. Meaning it is possible to create your own implementation of masks by creating a shader and implementing the Stencil buffer, this comes in handy when for example you want something like an inversed mask, where pixels are drawn everywhere except where the mask is (creating holes in an image) :)
just put the raw image as a child of a UI image and call the parent image "Mask" then put in any shape of sprite in the "Mask" image. Go to 'add component' then 'UI' then add 'mask'. Please look at this link https://learn.unity.com/tutorial/ui-masking#6032b6fdedbc2a06c234cd3e it work well for me

iOS Sprite Kit - SKSpriteNode - blend two sprites

Actually, I'm migrating a game from another platform, and I need to generate a sprite with two images.
The first image will be something like the form, a pattern or stamp, and the second is only a rectangle that sets color to the first. If the color was plane, it will be easy, I could use sprite.color and sprite.colorBlendFactor to play with it, but there are levels where the second image is a rectangle with two colors (red and green, for example).
Is there any way to implement these with Sprite Kit?
I mean, something like using Core image filter, and CIBlendWithAlphaMask, but only with Image and Mask image. (https://developer.apple.com/library/ios/documentation/graphicsimaging/Reference/CoreImageFilterReference/Reference/reference.html#//apple_ref/doc/uid/TP40004346) -> CIBlendWithAlphaMask.
Thanks.
Look into the SKCropNode class (documentation here) - it allows you to set a mask for an image underneath it.
In short, you would create two SKSpriteNodes - one with your stamp, the other with your coloured rectangle. Then:
SKCropNode *myCropNode = [SKCropNode node];
[myCropNode addChild:colouredRectangle]; // the colour to be rendered by the form/pattern
myCropNode.maskNode = stampNode; // the pattern sprite node
[self addChild:myCropNode];
Note that the results will probably be more similar to CIBlendWithMask rather than CIBlendWithAlphaMask, since the crop node will mask out any pixels below 5% transparency and render all pixels above this level, so the edges will be jagged rather than smoothly faded. Just don't use any semi-transparent areas in your mask and you'll be fine.

draw texture on 4 vertices plane

i have a standard plane created with unity and replaced its mesh filter (that had 121 tri, 202 vertices) with a mesh filter made in blender that has 2 tri/4 vertices.
if i set the material up with a texture, i get only a very small portion of the texture drawn on the plane. How can i draw the full texture on the new plane?
You need to adjust your UV mapping so that the 4 vertices cover the whole image. Take a look at the this demo file especially at the UV scene layout.
If a texture shows that way it means either the UVs of the imported model are wrong or the texture tilling or offset in the material are wrong.
Instead of importing a mesh for such simple shape you can create one procedurally in code like this: https://github.com/doukasd/Unity-Components/blob/master/ProceduralPlane/Assets/Scripts/Procedural/ProceduralPlane.cs