How to mask a UI.RawImage in Unity? - unity3d

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

Related

Unity TilemapRenderer draw mode sliced

Unity SpriteRender has a useful drawMode parameter to define how the Sprite scales when its dimensions change, with the option to slice sprite if correctly set in Sprite Editor.
Unlikely TilemapRenderer does not have this drawMode option, therefore it is impossibile to correctly slice a Sprite of a Tile.
The image below is an example of what I would achieve, the first image is the sprite itself, the second one is the tile (NOT sliced) the third one is a UI Image, where the sprite is correctly sliced (this is what I want to achieve).
Is there any way to have that result with TilemapRenderer?

How to assign a texture around the edges of an object (as in the actual edges, not an outline)

I want to create a shader that assigns a texture to an object, then a different texture around the edges of that object (similar to an inside glow, or a "force field" but for all the edges in the object instead of the outline edges). Is this possible with shader graph? Or do I have to write a custom shader ?
ps: I want to do this for a single object, not the whole scene.
description :-
enter image description here
these picture were taken in blender just to demonstrate what I mean

Removing specular reflection from image

I'm trying to remove specular reflection from a ring light from my image. I want to remove specular reflection from both the test image and the flat image before performing flat-field correction. Here are the two images:
Test Image
Flat / Background Image
The light and the camera are placed directly above the flat object (changing geometry is not feasible). My understanding is that the signal received by the camera is the sum of diffused color and the specular reflection. So to estimate the reflection component, I placed a black surface (same material as the original object) and captured the reflected component. The black surface was captured as follows:
Black image
However, when I try to subtract it from my image, the ring shaped area in both the images become darker, which implies that the black surface had a stronger specular reflection component than both the test image and the flat image. After subtraction of specular component, the image looks like this:
Specular free image
Can someone tell me why is that?
Reflectance is a function of wavelength. So is the sensitivity of your camera.
Any calibration done with a homogeneous reflectance target will fail on anything inhomogeneous. No matter if it's due to surface roughness or colour.
Also your image is terribly underexposed.

Unity, GameObject Sprite(2D) how to remove transparent part?

I had join few pieces image to be a Map, and i make it able to click also.
but the problem is the image itself had transparent part, so when i click "Section A", maybe will trigger "Section B". Because "Section B" had transparent part is overlap on the Section A area.
So my question is, is that possible had any properties can adjust like it will auto remove transparent part?
or is must manual to adjust the Collider area? because my images had a lot, if manual adjust one by one, then is really take a lot of time.
And i using Box Collider for additional information.
Option 1. Pick some layered sprites. Access the texture of each sprite and read pixel from it, providing coordinates sophisticatedly extracted from mouse position, sprite position on screen and texture bounds provided by sprite. Supposing that opaque parts of sprites are not intersected, any sprite that have opaque pixel at given coordinates will be the result of picking.
Option 2. Replace box colliders with procedurally generated mesh colliders. The procedure will receive the same texture of sprite as an input and generate outline(s) using, say, marching squares algorithm. To convert outline vertices into mesh the procedure may use any trianulation algorithm that works well with concave polygons.

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.