C#, display and navigate structural 3D FEM model (helix-toolkit, SharpDX?) - c#

Hello! I am trying to implement a simple way to display the deformed shape of a beam. I found HelixToolkit that offers perfect tools, but I can't find the way to display different tiles of the same mesh with a different colour, or gradient. I found this: https://github.com/helix-toolkit/helix-toolkit/issues/885 that is the adding of the VertColorMaterial property, but it looks like it is for SharpDX library, but I started with HelixToolkit wpf (don't understand if in HelixToolkit is also available).
I can't even find a way to do it with SharpDX: it looks that there is almost no doc in internet.
Additionaly, SharpDX stopped its developement.
So:
do you know any example?
do you suggest me another library, which is fast/offers the ability of navigate the model, and it is compatible/use the wpf framework?
I also would like the ability to refine and subdivide a mesh.
Any kind of advice would be useful, I am new to the world of computer 3d graphic.
Thanks
EDIT 1:
I followed JonasH hint applying a texture, but it apply the texture for each tile. (See image).
I can only dinstict by out materian and in materia (set in the picture as Hue and the arrow Texture).
I need to apply one color for each polygon to give to the mesh a "FEM" style. Do you know how is it possibile with HelixToolkit?

You might consider using Kitware VTK instead of HelixToolkit. It’s extremely powerful library for scientific data visualization, well documented, perfect for finite element pre and post processing. You can take a look on my app, unfortunately it has not been documented yet, but just as an example:
https://github.com/galuszkm/STAN

I assume you have a color per vertex you want to use. I would recommend using wpf or helixToolkit wpf since they are quite easy to use. But as far as I'm aware they do not support vertex coloring.
A workaround would be to use a texture. I would assume you want to visualize some scalar property as a color. You would first need to create your MeshGeometry and assign the TextureCoordinates, simply assign the value you want to visualize to one of the texture coordinates in the 0-1 range. You would also need to create a gradient texture, either a gradientBrush or create an image. You would then assign the brush like:
var brush = new ImageBrush()
{
ImageSource = new BitmapImage(new Uri("gradient.png", UriKind.Relative))
};
var material = new DiffuseMaterial(brush);
GeometryModel3D model = new GeometryModel3D(mesh, material);

Related

How can I programmatically load a texture into an Image the same way the Unity Editor does?

In the image above, the first image is loaded via C# script. The second is assigned via the inspector in Unity editor. Note the dark gray border around the first image. How can I load the image via C# and have it not have the border?
The source image is a white-on-transparent PNG 512x512 pixels. It's being displayed in an UnityEngine.UI.Image sized at 30x30 with a red color assigned. The source image is identical (same location on disk) for both examples above.
The code I am using for the first image is as follows;
var texture = new Texture2D(512, 512);
texture.LoadImage(File.ReadAllBytes(Path.Combine(TexturePath, name)));
image.sprite = Sprite.Create(texture, new Rect(0,0, texture.width, texture.height), new Vector2(.5f,.5f), 100);
where image is the appropriate UnityEngine.UI.Image.
Note
The advantage of using the code above is that the images do not need to be embedded in the game that unity ends up building. It means these images can be distributed separately from the game. Using Resources.Load does not cater for this, and I suspect, is the same as assigning the image via the inspector, meaning that unity has already done something to the texture prior to assignment (likely something by the UnityEditor.TextureImporter)
Update
I investigated the Texture2D constructor some more and determined that the following code results in the image above, where the edges of the sprite no longer have the grey border, but now appear jagged. (Setting the last parameter to true retains the grey border).
var texture = new Texture2D(512, 512, TextureFormat.Alpha8, false);
Some googling has me thinking that the issue is mipmap related, and that the Unity Editor may be resolving this on import due to whatever occurs with UnityEditor.TextureImporter.borderMipMap as seen here. However, the UnityEditor namespace is not available when building the project.
The issue is that the PNG format uses a non-premultiplied Alpha and Unity uses straight alpha blending designed to work best with pre-multiplied alpha colors.
Better in-depth descriptions of Pre vs non-Pre can be found from:
NVidia
Microsoft
A Unity-specific discussion can be found Here (however, note this problem has nothing to do with mip-mapping, but can be exacerbated by filtering and resizing techniques)
You can also look to Unity's documentation on alpha importing to see a visual example of a common solution to this problem if you have access to pipeline-side creation of these PNGs (I have typically solved this by applying a post-process to modify PNGs after/during their creation).
Using a cutout shader is also a solution though it can result in jaggy/visual artifacts.
You would think rendering a sprite using the features of Unity's primary supported runtime load image file format would be a simple affair but, alas, it is quite a bit more complicated than that. I don't know the inner workings of UnityEditor.TextureImporter.borderMipMap but I suspect its inception is built around a similar problem (weight between the edge pixel alpha and any neighbors used in the filtering)
Use Resources.Load, thus you can configure the texture settings in the editor.

Using an Image as for Font in XNA (SpriteFont? Or something else?)

So I've gotten to the portion of a 2D XNA-based top-down hack-and-slash project I'm working on where I need to draw text onto the screen. What I want to use is a SpriteSheet-esque image I found with the characters and symbols I'm going to need for this project.
Now, I've done a little bit of reading on this before asking, and the only two ways of putting a "Font" or something onto the screen is by using Fonts already installed on the computer, or, according to this, I have to create a Custom Content Processor?
My question is, if I have an image (say, in PNG format), that has all the letters/characters I want on it, how do I use those letters in my 2D game? And it doesn't necessarily have to be an XNA-based solution.
Thanks in advance.
You draw it exactly how you would draw any other sprite from a sprite sheet.
You might already know that the SpriteBatch.Draw method has some overloads that can take a Rectangle that represents the source from your sprite-sheet.
you can keep track of your relation from chars to sprite rectangle with a dictionary
Dictionary<char, Rectangle> fontDict = new Dictionary<char,Rectangle>();
fontDict.Add('A', new Rectangle(/*params representing source of A*/);
and you can your word with a for or foreach loop
for(int i = 0; i<str.Count; i++)
{
Rectangle spriterect = fontDict[str[i]];
SpriteBach.Draw(/*params*/);
}
Keep in mind that you also have to manage the spacing of the letters out on your own, but it's possible to do on your own.
It does however Look like the ContentManager supports creating SpriteFonts based off of image formats such as jpg and png, so I'd say you might just be better off exploring that
further googling yields the FontTextureProcessor Class, which might be helpful.

PixelShading in C#

I hope I do not make my first mistake with my first post.
I am writing a library for several graphical effects and filters (for example Sobel or Gauß mask).
Because of the low Speed, doing this on the CPU, I wrote some shaders with the Shazzam tool.
My concret Problem is, that I am not able to use this shader in C#.
In the Internet I found only advice how to apply a pixelshader as a effect in XAML directly to a element, which is not usable for my application, because this makes it impossible to apply several shaders on one Image, which is needed, for example the Canny Edge Detector.
To illustarte this issue a Little pseudo-code, which should Show, what I expect from the method.
PixelShader somePixelShader = new PixelShader(pixelshader.ps);
somePixelShader.Input = Bitmap;
somePixelShader.Height = 200;
somePixelShader.Width = 800;
somePixelShader.Execute();
Bitmap = somePixelShader.Result;
As you see, everything should be done in C#.
Perhaps you can help me with my issue.
You can make a copy of the current effect output as a bitmap with RenderTargetBitmap, then submit this outputted image as the new input for the next effect, rinse, repeat.
Update : after a small (and inconclusive) test, this will not work : Can't render pixel shader to RenderTargetBitmap! Please help!
Check out these white papers for step-by-step instructions + examples on how to compile and use a pixel shader in WPF or SL.
You may also want to check out the WPF Pixel Shader Effects Library here.

Default 3D chart transparency with ASP.NET Chart Control?

like two guys before me there and second one there I have difficulty with 3D chart.
How to force them to be transparent like this picture:
taken from 3D Area chart example shipped with ASP.NET Chart controls. This chart has ChartColorPalette.BrightPastel pallete, but is transparent.
I have also 3D chart with ChartColorPalette.BrightPastel palette, but is not transparent and I still cannot found way how to make it transparent like example chart. (After examining example markup and codebehind):
The only way I've found is to set the color of series with alpha channel for transparency, or use color palette with transparent colors (for example ChartColorPalette.SemiTransparent) but there must be some other default way which I'm missing.
Reason I really need this to know is that I'm creating graphs without any code behind just using markup, so I'm finding it a little bit redundant to create code snippets only because of this.
Thank you very much for any answers.
Edit: I'm using .NET 3.5 version of ASP.NET charts.
I'd same problem here. Solved using this:
// After all series manipulation, add this
chart.ApplyPaletteColors();
foreach (var serie in chart.Series)
serie.Color = Color.FromArgb(220, serie.Color);
I see you're on this thread too. Unfortunately, you do need to use colors with an alpha channel in order to set transparency AFAIK
Here's a good overview of the palettes available and some basics on manipulating them
My guess is that you could create a new palette by iterating over the colors in the bright pastel palette and adding an alpha channel - if you do this in a global static, perhaps as part of your webapp init logic, you should be able to reference it pretty easily - if you don't want to have any codebehind, you ?may? be able to databind your custom palette to the CustomPalette property, but I can't say with certainty.
If you do use palette, and not one color per series, you have to set color for every point:
myChart.ApplyPaletteColors();
foreach (var series in myChart.Series)
{
foreach (var point in series.Points)
{
point.Color = Color.FromArgb(220, point.Color);
}
}

Render multiple layers in XNA

I'm using XNA, and I've run into a little problem. I need to support multiple layers, each with a distinct z order (I call these "viewports"). A picture is worth a thousand words, so here's what it should look like:
http://www.charlesstrahan.com/so_files/viewports.png
There are several things to notice here. Sprites do not render outside of their viewport, as you can see with Sprite B. Also, notice how the viewports are rendered - it's very similar to "layers" in Photoshop. Although Sprite C is has a z order of -1000, C still renders above Sprite A because its viewport's z-order is a greater than A's viewport's z-order.
There's one last detail that I couldn't display very well in the above picture. Each viewport needs to optionally render a color over its region of the screen - you could think of it as a "tinting" affect.
I'm completely at a loss when it comes to doing this the best way in XNA, so I could really use a short snippet of C#/VB.NET code that demonstrates this in action. Any help would be greatly appreciated.
You can easily do this with RenderTargets :-) There are lots of resources on how to use them on the web (example).
If you're just starting yoru project though, consider installing the XNA 4.0 (in CTP right now via the windows phone SDK). It's gotten a lot easier in the new version ... from an article that Shawn Hargreaves put out recently, RenderTarget changes in XNA Game Studio 4.0.
List<Texture2D> textures = new List<Texture2D>();
for (int i = 0; i < 100; i++)
{
RenderTarget2D rt = new RenderTarget2D(...);
GraphicsDevice.SetRenderTarget(rt);
DrawCharacterAnimationFrame(i);
GraphicsDevice.SetRenderTarget(null);
textures.Add(rt);
}
And the "tinting" feature you were asking about is braindead easy with this because you can just call the Clear method when you go to render each render target with whatever color you want.
You can just sort your objects into groups then render each group in order with clipping
device.ClipPlanes[0].Plane = plane;
device.ClipPlanes[0].IsEnabled = true;

Categories