Per-Pixel Lighting in WPF - wpf

Is it possible to implement per-pixel-lighting in WPF 3d application (C#) by using their shader effects?
I have a basic 3d application running in WPF but it only shows Gouraud Shading, by interpolating shaded colour values between vertices to the inner of the polygons. I tried to implement a per-pixel lighting approach, like Phong, but I realize that I do not seem to have access to interpolated normals in the WPF pixel shader effects.
Is this the limitation of WPF, where one should better go with C++ and OpenGL/DirectX directly?

as far as I know, yes it is a WPF limitation...contrary to the OpenGL where you can do per-pixel shading, the WPF only allows you to work on a meshGeometry.
I think a way to go with it, would be to create textures that contains your shader effect, and then apply it per triangle.
This would take a longer amount of computation time, but should work.
Regards

Related

How to replace DrawClosedCurve / FillClosedCurve when moving from WinForms (GDI+) to WPF?

I've got an array of points (X,Y) which constitute a convex hull (a simple, irregularly shaped contour). Rather than rendering a polygon with straight edges, I want to render them with an approximate "smoothly curved" contour that passes through all of these points.
In WinForms/GDI+, this could be accomplished with the Graphics.DrawClosedCurve or Graphics.FillClosedCurve methods. There does not appear to be an equivalent in WPF.
I've looked into drawing using things like Path and BezierSegment, but I'm not sure if (and how) these can be used to generate a continuous closed curve, given a set of points. It appears that to do this, I'd perhaps have to generate a set of control points based on my contour as an intermediate step?
I have tried using the GDI+ methods to render onto a System.Drawing.Bitmap and then displaying that in the WPF application. This works, but the performance (particularly the conversion from System.Drawing.Bitmap to BitmapSource) is poor and not sufficient for the application. This is why I'm looking for a pure WPF solution.
Has anyone been able to draw a closed curve based off a set of points in WPF?
Unfortunately, there is no single-method equivalent to DrawClosedCurve in WPF, even though it's been requested. So you are left with at least two options:
Host a native window within your WPF window and perform all your drawing on it.
Implement your own cardinal spline drawing. You are on the right track with Bezier segments. However, there are existing implementations of it you can look at out there.

Is it technically possible to render a wpf Xaml element to a direct3d texture?

Lets say I have a simple WPF canvas where I draw a few buttons and shapes using xaml. I would like to render the canvas to a direct3d texture so I can have access to the pixels from within the GPU.
RenderTargetBitmap allows me to do software rendering. but this will be limiting in terms of performance as I will have to manually copy the pixels to where I want.
I also looked into using a custom shader effect on the canvas. but as far as I know it is impossible to write to a separate texture using direct3d 9.
So is it at all possible? if so how?

Applying a pixel shader to a ViewPort3D

I'm new to pixel shaders, and I'm trying to apply an underwater-effect to my 3d scene. I can apply it to an image and animate it easily enough, but not to my ViewPort3D. The computer just hangs when calling BeginAnimation on the effect being applied to the Viewport3D. Is this something that cannot be done in WPF?
After a little digging I learned that pixel shaders are only applied to 2 dimensional types, like images. So what I would need is called a vertex shader, which for WPF, there are none.

2D CAD application in WPF

I'm trying to write an CAD-like application in WPF(.NET 4.0) that needs to be able to display a lot of 2D points/lines. It will be used to display CAD-plans of entire cities with zoom, pan, rotate and point snapping on mouseover.
Right now I purely use WPF. I read the objects from the CAD file draw them into a StreamGeometry, use it as stroke of a new Path and add it to a Canvas, with several transforms.
My problem is that this solution doesn't scale well enough. It works fine with small CAD-files, but when I want to display like half a city(with houses and land boundaries) it is very very delayed.
I also tried to convert my CAD-file to an image, but
- a resolution a 32000x32000 is sometimes not enough
- when zooming out the lines are too thin.
In the end I need to be able to place this on a Canvas(2D/3D) as background.
What are my best options here?
Thanks,
Niklas
wpf is not good for a large 3d models. im afraid it is too slow. Your best bet is direct 3d or openGL
However, even with the speed of direct3d,openGL you will still need to work out how to cull as many polygons/vertices as possible before the rendering of the scene if you are trying to show an entire city.
there is a large amount of information on this (generally under game development)
there are a few techniques including frustrum culling, near and far plane culling.
also, since you probably have a static scene you may be able to use binary spacial partitioning.
As I understand the subject is 2D CAD system within WPF.
Great! I use it...
OpenGL and DirectX are in infinite loop OnDraw always. The CPU works all the time.
WPF/Silverlight 2D is smart model.
Yes, total amount of elements (for example, primitives inherited from Shape) must be not so much. But how many?
I tested own app (Silverlight). WPF will be a bit faster I hope...
Here my 2D CAD results. Performance is still great. Each beam consists of multiple primitives.
Use a VirtualCanvas like this one from Chris Lovett.

Can pixel shaders be used when rendering to an offscreen surface?

I'm considering integrating some D3D code I have with WPF via the new D3DImage as described
here:
My question is this: Do pixel shader's work on offscreen surfaces?
Rendering to an offscreen surface is generally less constrained than rendering directly to a back buffer. The only constraints that come with using an offscreen surface with D3DImage is that it must be in a 32-bit RGB/ARGB format (depending on your platform). Other than that, all that the hardware has to offer is at your disposal.
In fact, tons of shader effects take advantage of offscreen surfaces for multipass, or full screen post-processing.
I don't know if there's anything special about it with WPF, but in general yes, pixel shaders work on offscreen surfaces.
For some effects rendering to a different surface is required - glass refraction in front of a shader-rendered scene for example. Pixel shaders cannot access the current screen contents and so the view has to be first rendered to a buffer and then used as a texture in the refraction shader pass so that it can take the background colour from a pixel other than the one being calculated.

Resources