I am working to generate terrain for our project, something that will be contained in the Model class that I can draw, but I new class would be alright since I may need to look inside for specific data often, and then I would just need the basic function to work with the Game class.
Anyway, I have a fair amount of knowledge of the XNA framework, but because of how convoluted it handles anything. So my problem is I can't just make a Model, I can't instantiate that class or anything. I have what I believe the proper data to form a model's geometry, which is all I need right now, and later possibly have it textured.
I don't know where to go from here.
XNA you usually use Content.Load, to have their content pipeline read in a file and parse it specifically, but I want to avoid that because I want my terrain generated. I can compute an array of Vertex data and indices for the triangles I want to make-up a mesh, but so far my efforts have tried to instantiate any object like Model or those it contains, have failed.
If there is some factory class I can use to build it, I have no idea what that is, so if someone else can point me in the right direction there and give me a rough outline on how to build a model, that would help.
If that's not the answer, maybe I need to do something completely different, either centered on using Content.Load or not, but basically I don't want my terrain sitting in a file, consistent between executions, I want to control the mesh data on load and randomize it, etc.
So how can I get a model generated completely programmatically, to show up on the screen, and still have its data exposed?
Model and its associated classes (eg: ModelMesh), are convenience classes. They are not the only way to draw models. It is expected that sometimes, particularly when doing something "special", you will have to re-implement them entirely, using the same low-level methods that Model uses.
Here's the quick version of what you should do:
First of all, at load time, create a VertexBuffer and an IndexBuffer and use SetData on each to fill each with the appropriate data.
Then, at draw time, do this:
GraphicsDevice.SetVertexBuffer(myVertexBuffer);
GraphicsDevice.Indices = myIndexBuffer;
// Set up your effect. Use a BasicEffect here, if you don't have something else.
myEffect.CurrentTechnique.Passes[0].Apply();
GraphicsDevice.Textures[0] = myTexture; // From Content.Load<Texture2D>("...")
GraphicsDevice.DrawIndexedPrimitives(...);
Related
I am using factories to generate Objects, however I am having trouble figuring out how to add these created object instances to the unity scene.
If I add a component of the type, it's a new instance and doesn't have the created data.
If I try and instantiate, It won't work because it's a custom class and has no gameObject.
Is there a common way to do this? or do I have to redesign how this is working?
So far the only thing I can think of is creating an Initialize method in the objects that sets all the properties, and then after the object is created and generated, I create a new Component, and call the initialize method on the GetComponent that I just added, setting the values to the values from the generated object.
Seems like a headache and bad way to do it though.
Basically I'm generating Items, using some Randomization functions to get their rarity/itemType etc, and then creating said Item using factories as I won't know what type they are until the game is already running.
I'm, in theory, trying to generate everything without any need for a database, except a sprite > object connection. But, the more and more I get into this the more I think i'm going to have to have some sort of database.
Long story short, I need the instances in the Unity scene so that when a player clicks on them he can "equip them, disenchant them, etc"
I have a FBX model with animation clips as subassets (embeded in it), I want to enable looping for all clips in it. I found that AnimationUtility's GetClipSettings method is obsolete now and also tried using it but it didn't work. This is probably because Unity has changed their API for looping and it seems that the only way to access loopTime property is by manually accessing the subassets of fbx model and checking the loopTime property. However, I have to automate this process since I'm generating dynamic animation controllers for my models before building assetbundles. The pipeline includes post/pre-processing and then building assetbundles through editor scripts. So far, I have come across this post using which I was able to enable loopTime just by setting ModelImporterClipAnimation.loopTime = true for every clip and it works. However, now the model seems to look completely pink as it loses reference to its materials for whatever reason, this happens only when I include the AssetPostprocessing script.
How should this be dealt with?
Why is the model losing materials if the OnPreprocessAnimation of AssetPostprocessor is included? Am I missing something?
Should I also handle Material references by some other method, if I mess with the AssetPostprocessor?
Can we access loopTime by some other way? It is not specified in the docs, though.
My code is exactly the same as the referenced post above other than the slight change to enable loopTime. Or is there a better way to do all this?
P.S. I'm posting this question after a day full of searching and experimenting, apologies in advance if the question is not clear enough. I can add more info if needed.
I think that you have to handle the material yourself. You can set ModelImporter.importMaterials to false to create a default diffuse material when importing the model.
If material already exist, you can also search for existing material and load it when importing the model:
ModelImporter modelImporter = (ModelImporter)assetImporter;
modelImporter.materialName = ModelImporterMaterialName.BasedOnMaterialName;
modelImporter.materialSearch = ModelImporterMaterialSearch.Local;
See ModelImporterMaterialName and ModelImporterMaterialSearch docs for other options you have.
I have a bunch of Shape-classes (classic) like Rectangle and Circle in my first module.
In my second module, I have a GUI, made with WPF. I want to show a ListBox of all Shape-classes. The ListBox shall contain the localizable name of the shape, which is saved as a resource string, and an icon, saved as a resource image.
I want my whole code to be as modular as possible, e.g. if I add a new Shape-class, I want to change as few classes as possible.
My first approach would be to make a helper class in my GUI-module, which for each shape holds the Shape's Type, its name as string, and its icon as a Bitmap (or similar). I would then initialize the list at one place, e.g.
var shapeList = new List<ShapeHelperClass>
{
new ShapeHelperClass(typeof(Rectangle), Resources.StringRectangle, Resources.IconRectangle),
new ShapeHelperClass(typeof(Circle), Resources.StringCircle, Resources.IconCircle),
};
and bind this list to the ListBox. Now, if I rename my classes or my resources, nothing will break, and localization should work properly. But, of course, if adding a new Shape-class in the first module, I also need to update this list.
Another approach would be to use reflection to find all my Shape classes, and build the list out of that. However, I would still need some Dictionaries or something similar to map the classes to the Resources. I could find the resources if they follow a pattern, like "Icon" + "Classname". However, if no icon is found, this is only noticed at runtime.
So, my questions are:
Is my first approach a good one, our could it be improved?
How can I make sure that a programmer who adds a new Shape also adds the new Resources and extends the mapping-list? Maybe by Unit-testing?
1.Is my first approach a good one, our could it be improved?
You could create a method in your first module that returns all shapes and call this one in your client application, e.g.:
var shapes = GetShapes();
List<ShapeHelperClass> helpers = new List<ShapeHelperClass>();
foreach(var shape in shapes)
helpers.Add(...);
Then you should never have to modify the client application as a shape is added or removed in the first module.
2.How can I make sure that a programmer who adds a new Shape also adds the new Resources and extends the mapping-list? Maybe by Unit-testing?
Maybe you could write a unit test that uses reflection to find all shape types and asserts that they are included in the list of shapes that is returned from the first module. And likewise for the resources. I can't think of any better automatic way of ensuring this really.
I've got some material parameters stored in the FBX file (DiffuseFactor, ShininessExponent, SpecularFactor and others), but I can't get to them using Effect.Parameters nor BasicEffect (they've got only the basic stuff - like EmmisiveColor or alpha). I know, that I can try to write a Effect-derived class, but is there any other way? A built-in feature or maybe some half-raw parameters stored somewhere?
If you create a custom model processor (for the content pipeline) and override the ConvertMaterial method you can access this type of data in the input MaterialContent.OpaqueData collection.
I know if you output an EffectMaterialContent from this method like they do in the Skinned Model sample, you can attach this data to the EffectMaterialContent's OpaqueData collection and it will be visible in the shader using the names you supply. This was as of XNA 3.1 anyway, I'm not sure if there's a better/alternate way to do this now.
EDIT: Wow, didn't realize this question was almost a year old.
Small design question here. I'm trying to develop a calculation app in C#. I have a class, let's call it InputRecord, which holds 100s of fields (multi dimensional arrays) This InputRecordclass will be used in a number of CalculationEngines. Each CalculcationEngine can make changes to a number of fields in the InputRecord. These changes are steps needed for it's calculation.
Now I don't want the local changes made to the InputRecord to be used in other CalculcationEngine's classes.
The first solution that comes to mind is using a struct: these are value types. However I'd like to use inheritance: each CalculationEngine needs a few fields only relevant to that engine: it's has it's own InputRecord, based on BaseInputRecord.
Can anyone point me to a design that will help me accomplish this?
If you really have a lot of data, using structs or common cloning techniques may not be very space-efficient (e.g. it would use much memory).
Sounds like a design where you need to have a "master store" and a "diff store", just analogous to a RDBMS you have data files and transactions.
Basically, you need to keep a list of the changes performed per calculation engine, and use the master values for items which aren't affected by any changes.
The elegant solution would be to not change the inputrecord. That would allow sharing (and parallel processing).
If that is not an option you will have to Clone the data. Give each derived class a constructor that takes the base Input as a parameter.
You can declare a Clone() method on your BaseInputRecord, then pass a copy to each CalculationEngine.