How to create a masking effect in unity - c#

how would I create an effect like the one in the link in unity with c#? i would like to control the reveal of a 3d object with a slider. is it possible? some kind of mask i assume?
via GIPHY

If you want to do this specifically with c#, you will need to use the Mesh api to generate a sphere mesh, then modify the verts to remove the ones you don't want to display. A much easier solution would be to use a fragment shader to not draw the pixels that were outside the boundaries you define. This would be much more versatile as well, it would apply to any mesh you provided to it.
Discarding pixels based on world position shows how to get a similar effect.

Related

Restrict the movement of an Object with ManipulationHandler-script attached

I want to move little Spheres with the ManipulationHandler-script attached on a big-Sphere in my Scene.
The movement of the little spheres needs to be restricted to the big sphere's "shell".
I have accomplished the behaviour (link provides a gif) without using the Manipulation-handler, updating the X and Y of the little Sphere in the Update-function.
Is there a way to achieve the same behaviour with the ManipulationHandler without rewriting it?
According to your description, the Solver in MRTK be able to implement this idea without even writing any code. If you are not limited to only using ManipulationHandler for other reasons, I highly recommend that you to use RadialView. You can refer to the following steps to implement this feature with Solver:
Add SolverHandler and RadialView components to the small sphere.
In the RadialView component, select Custom Overrideset property in the Tacked Target Type field.
Set the Transfom Override field to the large sphere.
In the Radial View component, set MaxViewDegrees to 360, set Min Distance and Max Distance to the radius of the large sphere,
Disable Smoothing .
Now, the small sphere can rotate around the large sphere and keep a fixed distance from it.

Monogame - draw automatically generated content from curves

I am writing a little Monogame and I am wondering how I can draw a curve that will be generated by some formula, then I want to fill everything bellow that curve with some color and I want to generate things on the top and drop them down until they collide with the curve. Check the image for a better understanding of my issue.
What type of object I should use for this kind of objects?
Alternative: If the curve and filled area cant be generated automatically or it is too hard to implement, I can make an image where the "unfilled" area is simply invisible, but I still get to the same issue, how to set the bounds such that they match the visible parts of the image and ignore the invisible?
Or even simpler how to get the top Y bound value for a given X value?
I found the solution here Per Pixel Collision - Could do with some general tips basicly it does a color detection collision.

How to render specific edges of a cube different from filling in XNA? (Monogame)

I am working with C#, Monogame and XNA 4.0. In my scene I have a lot of cubes. Some are connected, some are not. I would like to render the edges of the cube with another shader than the filling. Besides that, I would like to render the outer edges of connected cubes in another color (or thicker) than the edges within the cube-object. Here is a small painting to make clear what I want to do (sorry for my bad painting skills, but I think you will get it).
I know how to render a cube with a specific shader and I am also able to render the wireframe but I was not able to connect both methods. Besids that, the outer lines can not be rendered differently with this approach.
I tried it with post-effects like the edgefinding of comic shaders but in this approach I am not able render only specific edges. Besides that if two cubes are next to each other the shader does not recognize the edges.
I am not searching for a ready-to-use solution from you but I would be glad to get some tips/approaches/tutorials/similar projects/etc on how to achieve my goal. Are there some shader experts out there? I am at my wit's end.
(If you however would like to post a ready to use solution I would not be miffy :D)
It is a shame you're not using deferred shading, this would be pretty straight forward to implement if you were.
If you can access the normal and material for each pixel on screen through a texture lookup you can easily post-process this. You could use a 3x3 filter kernel and search for sufficiently large normal discontinuities (this would catch silhouette edges) and also search for pixels that lie on the transition between material IDs (this would catch the edges between blue and orange cubes). If your filter neighborhood satisfied either of these two conditions, then draw a black pixel to form the outline.
You should be able to do this if you use MRT rendering when you draw your cubes, and encode the normal + material ID into an RGBA texture (x,y,z,material).
The basic theory is described in this paper (pp. 13). In this case instead of using the depth as the secondary characteristic for outlining, you would use the material (or object ID, if you want EVERY cube to have an outline).

Draw points along the surface of a cube

So for an assignment I have to morph a cube into a sphere. All that's required is to have a bunch of points along the surface of a cube (don't need to be connected or actually be on a cube, but has to form a cube, though connections would make it easier to look at) and have them smoothly translate into a sphere shape.
The main problem is I never learned how to make points in XNA 4.0 and from what I've seen it's very different to what we did in OpenGL (we learned the old one in a previous class).
Would anyone be able to help me figure out making the cube shape I need? Each side would have 10x10 points with the points on the edge shared by the surfaces of that edge. The structure would need to be easy to copy or modify since I would need to have the start state, end state, and the intermediate state to translate the points between the two states.
If I left out anything that could be important let me know.
First of all, you should familiarise yourself with the Primitives3D sample. It illustrates all of the rendering APIs that you need.
Here is how I would approach this problem (you can look up these classes and methods on MSDN and it will hopefully help you flesh out the details):
Create an array of Vector3[] that represents an appropriately tessellated unit cube around (0,0)
Create a second array of Vector3[] and and use Vector3.Normalize to copy in the vertices from your first array. This will create a unit sphere with vertices that match up with the original cube.
Create an array of VertexPositionColor[]. Fill in the colour data however you like.
Use Vector3.Lerp to loop through the first two arrays, interpolating each element to set positions in the third array. This gives you a parameter you can animate - you will have to do this each frame (in Update is probably best).
Create an array of indices (short[]) that describes a triangle list of the tessellated cube (and, in turn, the sphere and animation between the two).
Set up a BasicEffect for rendering. This involves setting its World, View and Projection matrices and maybe turning on VertexColorEnabled. If you want lighting, see the sample for details (you'll need to use a vertex type with normals, and animate those normals correctly).
The way to render with an effect is: foreach(EffectPass effectPass in effect.CurrentTechnique.Passes) { effectPass.Apply(); /* your stuff here */ }
You could create a DynamicVertexBuffer and IndexBuffer and draw with those. But for something simple like this, DrawUserIndexedPrimitives is much easier (here is a recent answer with some details, and here's another one with a complete example of BasicEffect).
Note that you should only create these objects at startup (LoadContent is a good place). During rendering you're simply using them.
If you have trouble with your 3D rendering, and you're drawing text or sprites, see this article for how to fix it.

Restrict movement on reaching map border or any objects

I'm totally new in game-dev and would like to know the best practice about above question.
Let me explain more.
I want to create 2D game with top-down view and with free movement (without snapping to the grid) just like any Zelda game on GameBoy.
How should I store map bounds? Is there a way to do this automatically? For example I have a texture with background and texture with foreground where black color should appear transparent and should allow to move in space of it.
Thanks in advance.
For easy 2D collision detection, you'll probably implement bounding boxes.
Basically you will create a rectangle that represents every Game Object. The coordinates and size of the rectangle will be the same as the Texture2D (it is common to make this a property on the given class). Every time you update the position of your Texture, you update the position of your bounding box.
Now to check for collision, just loop through your game objects and see if any of the bounding boxes intersect.
Once you get the idea, you'll see that its very easy to implement. XNA also provides some math helpers to abstract the math (though its simple addition and subtraction).
Try this link for a more in depth explanation with code examples: http://www.dreamincode.net/forums/topic/180069-xna-2d-bounding-box-collision-detection/

Categories