I am creating simple 3D editor. I now can draw simple primitives like cube but, I don't know how to save this object to som variable and then copy it to other coordinates. I also don't know how rotate/scale/move this cube and save this new shape to variable. I have seen many tutorials on this topic, but in every one them, guy is moving only camera, not drawn object(cube). So basically I just need tutorial how to save some object to variable, than load this object from variable and draw it many times on different coordinates over scene and apply some transformation to these new objects(move,rotate,scale). I am creating this app in C# and OpenTK
Use the GL.Translate(x,y,z), GL.Rotate(θ,x,y,z) and GL.Scale(sx,sy,sz) functions to move the coordinate system origin, orientation and scaling.
So drawing a cube after the command
GL.Translate(10,10,100);
will draw the cube at the above location. Here is an (clunky) example of this process below:
Related
I'm trying to make an application that has sprites going down the screen with numbers in them. The Text(Script) component only works for 3D objects. I've tried using Text Mesh and making that a child of the sprites but I need them to be instantiated as a Prefab. So when I load the sprite in as a prefab it loses it's relationship to the Text Mesh. Does anyone know of a solution that worked for them?
There are many ways to do it depending upon your requirement. I will show you two of the most basic.
1- Use 2D Sprite and TextMesh.
2- Use Canvas Image and Text Objects.
P.S First one, that is 2D Sprite can be Instantiated directly in the hierarchy but for 2nd(Canvas Image) you need to instantiate it inside the canvas obj means as a child of canvas object.
I want to make RPG game using Unity2d with the tile feature to draw the game map.
I created a new class inherited from UnityEngine.Tilemaps.Tile and overrided void GetTileData.
In void GetTileData I determine the sprite to show for each tile according to the tile's neighbors.
See the image below. The source image is input from the inspector. The input is only one image like this below. I don't want to make massive input images caus that totally mess up.
But then I have a problem. When in the game I have to extract certain blocks from the source image and combine them into a new sprite to show onto the map as a tile sprite.
Just want to know, if I have the 4 rectangles known, and want to combine them into a sprite like the image above, how can I do that?
There is no easy way to do what you want in code. Simply use a tilemap with smaller base tiles.
I have an issue when importing Tiled maps with Object layers into a MonoGame project. Everything is pretty much working great; I can import the Tiled map into the project and render the map with the use of MonoGame.Extended.
My issue starts when I try to use an Object from an Object Layer in the Tiled map. I can access the object, and I can access its properties (position, etc) just fine. In fact, when accessing the object's properties, they output exactly as they should. Below are images to clarify:
This is the image of the object within the Tiled map editor
And below is the code where I import the map, collect the object's information, and assign that information (position, in this case) to another object (the player)
gameMaps.Add(MapBuilder.LoadTiledMap("TestMap"));
gameMapRenderer = new TiledMapRenderer(graphicsDevice);
var viewportAdapter = new BoxingViewportAdapter(GameInstance.Instance.Window, graphicsDevice, 1440 / 4, 810 / 4);
mainCamera = new Camera2D(viewportAdapter);
var player = new Player();
var playerStartPos = new Vector2();
foreach(var obj in gameMaps[0].ObjectLayers[0].Objects)
{
if (obj.Name.Equals("PlayerStart"))
{
playerStartPos = obj.Position;
break;
}
}
player.position = playerStartPos;
entities.Add(player);
mainCamera.LookAt(player.position);
But below is the image that demonstrates the issue:
Issue as an image
The sprite/character is the player object that is assigned the position, in case that wasn't clear.
To explain: I am able to get the object's position and assign it to my 'Player' object, and my player is then at the correct position per what the object's position is in the Tiled map editor. However, the Tiled map itself is actually rendering at an incorrect position- even though I never make any adjustments to the position of the map as a whole.
Some further details that might help:
In the Tiled map editor, the position x0,y0 is the utmost top tile of the map. However, in MonoGame, if I go the same position- the Tiled map is actually rendered/placed some dimensions below. This means that the positioning of the map is incorrect with what it should be.
Below is an image showing what that position is in MonoGame, and how it is not properly correlating with what that position is in Tiled.
MonoGame screenshot
After speaking a bit to the creator of Monogame.Extended, we narrowed down the issue. It was actually two things:
In the Tiled map editor, the grid system for isometric maps is different from the grid system Monogame uses to render everything. Tiled's grid system for isometric maps is a normal grid but rotated to match the isometric view, whereas Monogames is just a normal grid. This causes the position (x,y) of Objects in Tiled to be imported into Monogame with a position value that doesnt match with where it should actually be in Monogame- which explains the position being wrong in the image above. Monogame.Extended's creator took note on the bug but said a fix isnt certain because it would require a formula for converting an isometric Tiled position to a Monogame position.
Because my sprites had extra padding at the top, it was causing the overall map to render x many pixels lower than at what should be the proper 0 position. It had something to do with the map being generated in Monogame via the sprites dimensions, instead of the maps. Not a giant problem, honestly, since a workaround could be presumably made in a custom implementation of the TiledMapRenderer.
I can post the link to the Monogame.Extended discussion later.
What I want
To create an effect like an slash in my 2D project. For that, I've an image in my assets already, and I want to draw it from top to the bottom, allowing it to be drawn in maybe half a second, progressively.
The idea is to draw it to create the effect of the slash itself, and the progressively mode is to "fit" the slash image itself, imagine the first one of the following link, that would be drawn from left to right.
To achieve that kind of drawing, the only thing that I've found is fillAmount for an Image, combined with the fillMethod.
The problem
The problem is that these methods are for the Image class, that it's not recommended for game drawing but only for UI.
Anyway, I've tried to do it this way, and I've not been able to do it, because the Unity environment didn't let me drag&drop my slash asset into the Image variable that I had created into my script. I thought that it may be because of the type of import, but also trying to import it with the other options resulted in nothing better.
The questions
Just answering one of these questions would solve my situation (or I think so):
How can I make Unity accept my image/asset into my Image object in the script?
Another way to create such effect using Sprite or GameObjects directly?
Method 1: You can use sprite sheet to create a 2D animation
1-1. Prepare a splash effect sprite sheet (an image map contains continuous action image), like
1-2. Change the sprite mode to multiple in inspector window
1-3. Slice the sprites in sprite editor
1-4. Multi-select the sprites and drag into hierarchy window, Unity will ask you to save this animation
1-5. You will get an animation GameObject
Method 2: If you can make it like a circular progress bar, check Radial/ Circular Progress Bar in unity3d
I am just getting into XNA programming and have been unable to figure out how can I access the texture from a ".x" model. I am using a custom shader to display my model (just a cube with a texture mapped on it) with the filters set to point. To do this I needed to pass the effect my texture file which needed to be imported separately from my model or else it would complain since it is included in my model as well. This works perfectly how I want it, however this isn't really an agreeable method when I have many different models with their own textures.
My question is:
How am I able to access the texture included in my model directly from it and send that to my shader? Or am I able to access it directly with HLSL?
What I have tried:
I have found posts saying that it can assigned to a texture variable with:
Texture2d texture = ((BasicEffect)model.Meshes[0].Effects[0]).Texture
When I tried this the game runs but the cubes are just black. I can see that the texture variable is holding info and has the right dimensions but I can't tell if it is correctly holding the actual image. When I used just the BasicEffect they rendered just fine with their texture.
Update:
I have managed to get this to work after a little bit of fiddling. My game loads in a few hundred of the same cube and upon creation of each it would try save the texture of the model using the code above and then go through the mesh parts and change the effects to my custom effect. I discovered that the first cube created would save the texture okay but any subsequent cubes created would complain that they can't be cast as a BasicEffect. This resulted in one textured cube and then a lot of black ones. I am guessing that when it reuses the same model over and over like that it will just use the one that was modified to use my custom effect which was done on the first instance of the cube. Is this normal? I have got them all to render as textured by changing the texture variable to static.
Please observe that you are assigning the texture of your model to a temporary Texture2D variable, and not setting the Texture present in the Effect currently tied to your mesh.
If you do the following:
Texture2D textureToSet = Content.Load<Texture2D>("MyTex");
//Keep in mind that this method requires a basic effect type and that only one
//effect is present on each mesh to work properly.
foreach(Mesh mesh in model.Meshes)
{
((BasicEffect)(mesh.Effects[0])).Texture = textureToSet;
}
The quirky stuff going on inside the foreach is simply that you are grabbing the effect, then casting it to a BasicEffect and using its Texture property to give it a new texture to draw when used. Please see the documentation and Shawn's blog for a more detailed introduction.
If anyone else is wondering about this as I was then saving the texture using:
Texture2d texture = ((BasicEffect)model.Meshes[0].Effects[0]).Texture
This does work but there is one thing to watch for which is what was causing me problems. If you change the effect of the model from the default BasicEffect for one instance it will be changed for every instance of the model created thereafter. So you will only be able to use the above code before you change the effect for the first time on a particular model.
I later found this book which describes exactly how to extract the texture and other information from a model: 3D Graphics with XNA Game Studio 4.0 by Sean James - Specifically chapter 2