Tim Bernikovich
Tim Bernikovich

Reputation: 5945

How to determine if point lies inside shape?

I need to determine if point lies inside shape. In case our shape is circle it's easy:

highp vec2 textureCoordinateToUse = vec2(textureCoordinate.x, (textureCoordinate.y * aspectRatio + 0.5 - 0.5 * aspectRatio));
highp float dist = distance(center, textureCoordinateToUse);
textureCoordinateToUse = textureCoordinate;
if (dist < radius) {
    ...
}

But what if my shape is star, hexagon, spirale or etc? Does somebody know any fast way to do it? May I use images with alpha channels as shapes? How to do it?

UPDATE: I have just understand that the best option now is to pass another texture to shader. How can I do it? Now shader has 2 properties: varying highp vec2 textureCoordinate; and uniform sampler2D inputImageTexture;. And I want to pass another texture to check alpha channel of it inside shader code.

UPDATE 2: I have tried to load shape to shader (I think so). I'm using GPUImage framework, so I have set sampler2D with my shape to uniform and tried to check alpha channel there. Is it okay? On my iPhone 5s it's looks very well, but what about performance?

Upvotes: 0

Views: 1303

Answers (3)

game development germ
game development germ

Reputation: 1334

A shader won't give you anything because the result of shader's routine is an image.

In image-based approach the problem need to be reformulated. Lets say you have a grayscale image with rendered shapes where white and gray pixels define shapes and black pixels define nothing. You must know the center of each shape and the bounding circle of each shape. Note that bounding circles of shapes must not intersect each other.

Then you can probe a point agains shapes first by bounding circles (this probe is necessary to distinguish shapes because by peeking a pixel from image you can only know if you point intersect some shape), and second by peeking a certain pixel. If both probes are positive then your point is inside of a shape.

Upvotes: 1

game development germ
game development germ

Reputation: 1334

For arbitrary polygonal shape: 1. Triangulate you shape (for example using Delaunay triangulation). 2. Check you point against every triangle. It is trivial. 3. Improve performance by using bounding shapes around original polygonal shapes and space partitioning for triangles.

Upvotes: 1

Matic Oblak
Matic Oblak

Reputation: 16774

If you can have an analytic shape representation such as circle all you need to find is an equation that describes that shape.

If you have a pre-drawn shape and you can pack it into a texture you can do that as well. All you need is to treat the object as a rectangle (a whole texture image) and do a rectangle check such as for the circle plus get the colour of that texture and do a colour check. What to check in colour really depends on you, it can be black-white, use the alpha channel.. anything really.

If you have a complex drawn object such as 3D model you need to get a model projection (silhouette) which can be drawn to a frame buffer object and again used as a texture or better yet try to draw it directly to the scene using some additional buffer such as stencil which you can then again use in fragment shader to check a specific value.

Upvotes: 1

Related Questions