Can’t Load .fbx from blender to Monogame correctly - c#

I am using pipeline.exe tool, to convert an .fbx file I made using Blender (using the default Blender 2.73a export settings) to .xnb, and then manually copy and paste it in my content directory (Is this one of the correct ways to convert it properly ?).
After watching this video https://www.youtube.com/watch?v=u7tLYMPC828 I saw that you have to convert the .fbx file that you create using blender, to a new .fbx file, using a tool called fbx converter 2013, and then use that file in the pipeline.exe tool, to produce the final .xnb file. Without doing that, I can't even see my model when drawn on the screen.
With the steps I described above, I managed to see the .xnb file on my screen but it was not exactly the original model I had made. For example:
As you can see the real model I got (after the convertion with the fbx converter 2013) is much longer, whereas the one on the left (the one I draw in my game), is like packed in a small square.
If you are using blender, please let me know how do you import the .fbx to your Monogame project. (Or you can suggest to me another software you are using to make models and tell me your method of importing models to Monogame)
This is how I am drawing my model:
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.Black);
Vector3 modelPosition = new Vector3(-25, 0, 0);
float modelRotation = 0;
float aspectRatio = graphics.GraphicsDevice.Viewport.AspectRatio;
// Draw the model. A model can have multiple meshes, so loop.
foreach (ModelMesh mesh in Brick.Meshes)
{
Vector3 cameraPosition = new Vector3(0.0f, 0.0f, 50.0f);
foreach (BasicEffect effect in mesh.Effects)
{
effect.EnableDefaultLighting();
effect.World = Matrix.CreateRotationZ(modelRotation)*
Matrix.CreateTranslation(modelPosition);
effect.View = Matrix.CreateLookAt(cameraPosition,
Vector3.Zero, Vector3.Up);
effect.Projection = Matrix.CreatePerspectiveFieldOfView(
MathHelper.ToRadians(45.0f), aspectRatio, 1.0f, 10000.0f);
}
// Draw the mesh, using the effects set above.
mesh.Draw();
}
base.Draw(gameTime);
}
Edit: I also tried exporting to .X from blender and when i converted to .xnb, loaded the model and drew it, nothing shows up.

Ok, I finally realized what the problem was... my drawing code was incorrect. I tried https://msdn.microsoft.com/en-us/library/bb197293(v=xnagamestudio.31).aspx code from msdn website and it all worked out. I am stupid. Also, downloading https://msxna.codeplex.com/releases/view/117230 and installing all the files in the correct order (following the instructions .txt) fixed the monogame content project error I was having where I couldn't create the project (error missing project subtype 6D335F3A-9D43-41b4-9D22-F6F17C4BE596), obviously because I hadn't installed things properly.

Related

How do I set the target sorting layer in Unity Light2D using Universal Render Pipeline (URP)

I'm making a top-down shooter game and I'm trying to add a muzzle flash by using URP light 2D.
I'm trying to set the target sorting layers for the URP Light2D. I've looked in the documentation and everywhere, but I can't seem to find the code for the target sorting layer. It seems that the target sorting layer can't be changed programmatically. This is a big problem especially when you created the Light2D component in code.
Here is my code:
GameObject MuzzleFlash = new GameObject("MuzzleFlash");
Light2D lightComp = MuzzleFlash.AddComponent<Light2D>();
lightComp.lightType = Light2D.LightType.Point;
// Add the target sorting layers here
MuzzleFlash.transform.position = fromPosition; // From position is a Vector3
I have looked through the Unity forums and found this:
[SerializeField] public int[] m_ApplyToSortingLayers = new int[1];
lightComp.m_ApplyToSortingLayers = new int[] {
SortingLayer.NameToID("someLayer1"),
SortingLayer.NameToID("someLayer2"),
SortingLayer.NameToID("Default"),
};
Source: Can Target Sorting Layer be set in code for Light2D?
But when I plugged that into my code, the console said that
"'Light2D' does not contain a definition for 'm_ApplyToSortingLayers' and no accessible extension method 'm_ApplyToSortingLayers' accepting a first argument of type 'Light2D' could be found".
I also got this message from the console:
"Some scripts have compilation errors which may prevent obsolete API usages to get updated. Obsolete API updating will continue automatically after these errors get fixed."
When will there be a proper API for setting target layers for Light2D? I am new to Unity, so if anyone could help me that would be great.

Unity ProBuilder API changes to mesh won't render even though they are beeing applied

Hi I was trying to setup a system to procedurally generate 3d rooms on Unity.
I used the ProBuilder API to generate a normal cube and that worked fine. But as soon as I want to do any more than that through the API it doesn't work.
For example if I want to flip the normals of the whole mesh I use this code:
for (int i = 0; i < cube.faces.Count; i++)
{
Debug.Log("Face: " + cube.faces[i]);
cube.faces[i].Reverse();
}
If I run the game now the cube still looks as before, but if I try to select single faces it behaves like the code worked. Also if I try to do it manually through the manual ProBuilder tool I have to use the function 2 times to make it work.
To me it seems like the code works but somehow it doesn't get rendered in game afterwards.
Has anybody had the same problem and might be able to help me out?
Cube Generation Function:
void generateBlock(Vector3 size, Vector3 position)
{
cube = ShapeGenerator.GenerateCube(PivotLocation.Center, size);
cube.GetComponent<MeshRenderer>().material = material;
cube.transform.position = position;
}

How do I make audio play with GetComponent? (Unity, C#)

I'm making a version of minesweeper in Unity with C# for a project where different audio plays one shot depending on which number is inside the box you click (adjacent to each bomb, e.g. '1' would play a major chord, '2' a minor chord, etc.)
Part of my code that relates to this:
// Different Textures
public Sprite[] emptyTextures;
public Sprite mineTexture;
public AudioSource myFx;
public AudioClip[] audioFiles;
// Load another texture
public void loadTexture(int adjacentCount)
{
if (mine)
GetComponent<SpriteRenderer>().sprite = mineTexture;
else
GetComponent<SpriteRenderer>().sprite = emptyTextures[adjacentCount];
}
I can't seem to get the audio to work. I assume it would be something like this?
GetComponent<SpriteRenderer>().sprite = emptyTextures[adjacentCount];
GetComponent<AudioSource>().clip = audioFiles[adjacentCount];
But I just keep getting errors. I'm very new to C# so any help would be appreciated. Thanks! :)
p.s. I've been using this tutorial to create it: https://noobtuts.com/unity/2d-minesweeper-game
Edit:
Errors:
MissingComponentException: There is no 'AudioSource' attached to the "default" game object, but a script is trying to access it.
There is an audio source (myFx), but when I then remove that and add an audio source directly to the gameobject, I get no errors but no audio plays.
You are on the right track - you specify the clip you want to play with
GetComponent<AudioSource>().clip = audioFiles[adjacentCount];
The only thing left to do is actually play the clip by calling
GetComponent<AudioSource>().Play();

GameObject.Find() works in editor, but returns null in build

I'm currently using GameObject.Find() to retrieve an object generated by the Mapbox Unity SDK. The code works fine in the editor, but when I build the project it returns null. The offending code is the following:
GameObject go = GameObject.Find ("16/32699/21126");
if(go != null) {
//do the thing
} else {
Debug.Log("the tile doesn't exist");
}
I've tried building in both WebGL and Native OSX, both with the same result.
in the editor, the exact name of the object I'm looking for in the scene hierarchy is 16/32699/21126. I appreciate that using the forward slash is how you search for sub-children in unity, but it seemed to work fine in the editor.
below is a screenshot of the scene hierarchy.
The behaviour script is attached to the Map object.
Do you know what could be causing this, or if there is another way of searching for the object I'm after?
You can directly access the tile through the map visualises..
example code.
UnwrappedTileId unwrappedTileId = Conversions.LatitudeLongitudeToTileId(latLon.x, latLon.y, (int)_mapManager.Zoom); //either use lat,lon or other way to get unwraptileid
var tile = _mapManager.MapVisualizer.ActiveTiles[unwrappedTileId];//_mapManager is your abstract map
//active tiles is the dictionary created by mapbox sdk to track which tiles are active
if(null!= tile)
{
myModel.transform.SetParent(tile.transform, true);
}
else
{
Debug.Log("Map tile is null");
}

C# Managed DirectX sprite2D locating bug?

I want to draw 2D sprite.
And here is strange thing: if i Sprite.Draw2D with first overload:
using (Sprite Spr = new Sprite(Gfx))
{
Spr.Begin(SpriteFlags.AlphaBlend);
int SprY = Gfx.PresentationParameters.BackBufferHeight/2;
int SprX = Gfx.PresentationParameters.BackBufferWidth/2;
Spr.Draw2D(Tex, new Point(0,0), 0.0f, new Point(SprX,SprY), Color.White);
Spr.End();
while (!Spr.Disposed)
Spr.Dispose();
}
It locating right in 128,128.
But I want to scale-down my image.
So i use 3rd overload of Sprite.Draw2D:
Spr.Draw2D(Tex, new Rectangle(0, 0, 1024, 1024), new Rectangle(0,0,256,256), new Point(SprX,SprY) , Color.White);
Then, it draws to weired position(but correct-sized).
I found-out that width of screen in case of 3rd overload = 1280.
And no matter of PresentationParameters or ViewPort sizes. Even on other machine it is 1280, and center will be 640.
I've tryed to debug Gfx(Device) object and see if any appereance of '1280' number, and it is appear in Gfx.DisplayMode. Unfortunatly, i cant set this parameter because "Device.SetDisplayMode is deprecated" as MSDN says. And DisplayMode.Width is differs between two computers i've test my 'game' on.
Probably i've missed something realy HUGE and stack on it.
By the way, im using DirectX libs from "C:\Windows\Microsoft.NET\DirectX for Managed Code\1.0.2902.0" if this matters.

Categories

Resources