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

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.

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?

In Unity, how can I get a 2d Polygon collider that is the area of a big collider subtract some smaller collider inside the big one?

Like the image attached, I want to get a polygon collider of the area of the biggest collider subtract the areas of these two smaller colliders inside the big one?
I just want to have a collider that covers only the gray area in the image below.
At runtime please, it's ok to get a composite / polygon or what other types of collider.
Thanks very much.
Fastest way would indeed be to have 3 separate Colliders, and when collision happens with the big one, you also check that it does NOT happen with the two smaller colliders.
Check the Clipper library for polygon operations (also worth checking the eppz! Geometry library, which itself uses Clipper).
You can then use the resulting polygon "paths" (as it's called in the Clipper library) to create several EdgeCollider2Ds (you can [set its points][4] to create the shape of each polygon.
There is a problem with this approach though, which is that in the end you will not have a "solid" collider with an inside and an outside, and instead you will just have lines to collide with. Hopefully this will not be an issue for most of the cases.
I am not sure but you could use separate box colliders to get the same affect. Just need multiple references in the script.

Find angle face under mouse pointer in Unity 3d

I have a projector component and I need to find the angle that projected texture falls at to exclude the projecting on vertical faces.
My projector is under the mouse pointer and works ok when it is over an horizontal face:
I would like the projector to switch off on vertical faces to avoid this bad effect:
If possible, I would like to do it in the shader code to avoid the vertical projected image even if the cursor is located on the corners of an horizontal face and a part "goes out" on vertical face.
I found this solution in C#:
if (Physics.Raycast(MouseRay,out hitInfo)){
if(hitInfo.normal.y>0) {
// draw
} else {
// not draw
}
}
But only it works on curved surfaces and not, for example, on the face cubes.
How can I do this properly?
Normally they would use an image on a quad using TGA transparency, which rotates itself to the face that the middle of the object is aligned to, using ray to find the vertex and making it's absolute normal.
Other ways of doing it would be quite tricky, perhaps using decals... If you did it using a shader, it would take so much time... it's a case of problem solving not being ordered in order of importance for fast development. Technically you can project a volumetric texture on top of whatever object you are using... that way you can add your barred circle projected from a point in space towards the object, as a mathematical formula, it takes a while to do, check out volumetric textures, i have written some and in your case it needs the mouse pos sent to texture and maths to add transparent zone and red zone to texture. takes all day.
It's fine to have a flat circle that flips around when you change the pointer onto a different face, it will just look like a physical card and it's much easier to code, 10 minutes instead of many hours.

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.

Sprite Kit Physics Body Complex Shape - Spritekit

I have this situation: http://mokainteractive.com/example.png
I'd like to move the white ball inside the red track and detect wherever the balls touch the limit of the red track.
Which is the best solution? I have to create multiple transparent shape along the borders? Do you have other ideas?
thanks so much
In iOS8 you can create a single physics body for that kind of shape.
Make a texture of your shape with p.e. Adobe Illustrator and use this method:
init(texture texture: SKTexture!,alphaThreshold alphaThreshold: CFloat,size size: CGSize) -> SKPhysicsBody
The SKTexture is your shaped image. The body is defined by the colored pixels.
The alphaThresHold: The minimum alpha value for texels that should be part of the new physics body.
The Size is clear I think.
The texture is analyzed and all invisible pixels around the egg are ignored and only the color pixels are interpreted as the body of the SKPhysicsNode. You should use too many of these because they are very expensive to calculate for the Physics Engine.
Variations of this method are found in the SpriteKit Class Reference.
To your problem. Make an inverse texture of your area which should be transparent and pass it as texture to the physics body. It will be analyzed and a body around the free zone is created.
You cannot create a single physics body for that kind of shape.
Using bodyWithPolygonFromPath: will only allow you to create a convex polygonal path which obviously does not work for your shape.
I think you have 3 options here:
Use a number of bodyWithPolygonFromPath: bodies (probably the hardest to do and time consuming).
Use a number of various size bodyWithRectangleOfSize: bodies (not so hard but time consuming).
Use only straight lines in your image and use bodyWithRectangleOfSize: (the easiest and fastest). If you choose this option remember you are still free to rotate your straight lines to various angles.