Hlsl outline shader

By using our site, you acknowledge that you have read and understand our Cookie PolicyPrivacy Policyand our Terms of Service.

hlsl outline shader

The dark mode beta is finally here. Change your preferences any time. Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information. I'm still working on an image processing project that makes use of HLSL shaders to add Photoshop-esque filters like drop shadow, bevel and so on. The outcome of this code looks ok when using simple shapes like an ellipse, but doesn't work when applying the shader on text:. It's obvious that there's an issue with the scaling.

I don't have any clue how to scale the original texture to use it as an outline. Is that even possible? Any other ideas how to implement an outer glow or outline filter in HLSL? Your scaling strategy cannot be applied in this situation.

Throw the scaling step away, use only blur steps and the composing step. It will work. If you want to control a glow size, use a kernel size of blur steps for that, instead of scaling. I used gaussian blurs to create below images. Learn more. Asked 7 years, 6 months ago. Active 7 years, 6 months ago.

hlsl outline shader

Viewed 5k times. Thank you in advance. Active Oldest Votes. Let me show you how the blur shader creates a glow effect. A : There is a original image. B : Change the color of the image, and apply the blur shader. C : Combine the blurred image with the original image.

Land rover discovery head unit wiring diagram 1

Dish Dish 4 4 silver badges 14 14 bronze badges.Data enters the graphics pipeline as a stream of primitives and is processed by the shader stages. The actual shader stages depend on the version of Direct3D, but certainly include the vertex, pixel and geometry stages.

Other stages include the hull and domain shaders for tessellation, and the compute shader.

Sprite Outline Shader

HLSL shaders can be compiled at author-time or at runtime, and set at runtime into the appropriate pipeline stage. Direct3D 9 shaders can be designed using shader model 1shader model 2 and shader model 3 ; Direct3D 10 shaders can only be designed on shader model 4.

Taylor guitars acoustic

Direct3D 11 shaders can be designed on shader model 5. Direct3D Skip to main content. Exit focus mode. In this section Topic Description Using shader linking We show how to create precompiled HLSL functions, package them into libraries, and link them into full shaders at run-time. Compiling Shaders Let's now look at various ways to compile your shader code and conventions for file extensions for shader code.

All DirectX 12 hardware supports Shader Model 5. Yes No. Any additional feedback? Skip Submit. Is this page helpful?

Nilesat satellite

We show how to create precompiled HLSL functions, package them into libraries, and link them into full shaders at run-time. Using Shaders in Direct3D 9. Using Shaders in Direct3D Debugging Shaders in Visual Studio.

Univision tlnovelas en vivo

Let's now look at various ways to compile your shader code and conventions for file extensions for shader code. Specifying Compiler Targets. Using HLSL minimum precision.

Subscribe to RSS

Starting with Windows 8, graphics drivers can implement minimum precision HLSL scalar data types by using any precision greater than or equal to their specified bit precision. This section describes the features of Shader Model 5.The material editor is a great tool for artists to create shaders thanks to its node-based system.

However, it does have its limitations. For example, you cannot create things such as loops and switch statements. Luckily, you can get around these limitations by writing your own code. To demonstrate all of this, you will use HLSL to desaturate the scene image, output different scene textures and create a Gaussian blur.

If you know a syntactically similar language such as Java, you should still be able to follow along. Start by downloading the materials for this tutorial you can find a link at the top or bottom of this tutorial. You will see the following scene:. First, you will use HLSL to desaturate the scene image. To do this, you need to create and use a Custom node in a post process material. This is the material you will edit to create the desaturation effect.

First, create a Custom node. Just like other nodes, it can have multiple inputs but is limited to one output. Next, make sure you have the Custom node selected and then go to the Details panel. You will see the following:. You might be wondering where the desaturation code came from. When you use a material node, it gets converted into HLSL. If you look through the generated code, you can find the appropriate section and copy-paste it. This will be useful later on when you create a Gaussian blur.

First, navigate to the Maps folder and open GaussianBlur. Unreal will generate HLSL for any nodes that contribute to the final output. This will open a separate window with the generated code. Now, you need to find where the SceneTexture code is. The easiest way to do this is to find the definition for CalcPixelMaterialInputs.

This function is where the engine calculates all the material outputs. If you look at the bottom of the function, you will see the final values for each output:.

Since this is a post process material, you only need to worry about EmissiveColor. As you can see, its value is the value of Local1. The LocalX variables are local variables the function uses to store intermediate values. If you look right above the outputs, you will see how the engine calculates each local variable. The final local variable Local1 in this case is usually a "dummy" calculation so you can ignore it. To test, you will output the World Normal. Go to the material editor and create a Custom node named Gaussian Blur.

Afterwards, put the following in the Code field:. This will output the World Normal for the current pixel. Next, disconnect the SceneTexture node. This is telling you that SceneTextureLookup does not exist in your material. So why does it work when using a SceneTexture node but not in a Custom node?You will learn to write a screen space shader to draw outlines around objects. This shader will be integrated with Unity's post-processing stack.

Outlineor edge detection effects are most commonly associated and paired with toon style shading. However, outline shaders have a wide variety of uses, from highlighting important objects on screen to increasing visual clarity in CAD rendering.

This tutorial will describe step-by-step how to write an outline shader in Unity. The shader will be written as a custom effect for Unity's post-processing stackbut the code can also be used in a regular image effect.

Concepts introduced here can be found in the Recolor effect from the Kino repository by keijiroa Unity project that contains a variety of custom effects for the post-processing stack. To complete this tutorial, you will need a working knowledge of Unity engine, and an intermediate knowledge of shaders.

These tutorials are made possible, and kept free and open sourceby your support. If you enjoy them, please consider becoming my patron through Patreon. Download the starter project provided above, open it in the Unity editor and open the Main scene.

In this scene are several objects, each with a different shape and silhouette. This gives us a variety of surface types to test our outline shader on. These components allow us to make use of the post-processing stack. Assigned to the Profile field of the volume is a post-process profile, OutlinePostProfile. This will contain data that we will use to configure our outline effect. Note that by default Anti-aliasing in the layer is set to No Anti-aliasing. It can useful to keep anti-aliasing disabled when developing screen space shaders, as it allows you to see the end product of the shader without any further modification applied.

For the "finished" screenshots in this tutorial, and for best results, anti-aliasing is set to Subpixel Morphological Anti-aliasing SMAA at High quality.

Open the Outline shader in your preferred code editor. Shaders written for Unity's post-processing stack have a few differences compared to standard image effects. As well, some functionality, such as texture sampling, is now handled by macros. Currently, the Outline file contains a simple fragment shader named Frag that samples the image on-screen and returns it without modification.

There is also a function named alphaBlend defined; we will use it later for blending our outlines with the on-screen image. To generate outlines, we will sample adjacent pixels and compare their values.

If the values are very different, we will draw an edge. Some edge detection algorithms work with grayscale images; because we are operating on computer rendered images and not photographs, we have better alternatives in the depth and normals buffers.

Crifiu

We will start by using the depth buffer. We will sample pixels from the depth buffer in a X shape, roughly centred around the current pixel being rendered. Add the following code to the top of the fragment shader. We first calculate two values, halfScaleFloor and halfScaleCeil. By scaling our UVs this way, we are able to increment our edge width exactly one pixel at a time—achieving a maximum possible granularity—while still keeping the coordinates centred around i.

Properties are created a bit differently with the post-processing stack. We will first define it as float is our shader program, as usual. Next, open the PostProcessOutline. This file contains classes that manage rendering our custom effect and exposing any configurable values to the editor. If you select the OutlinePostProfile asset now, you will see that Scale has been exposed to the inspector. We'll leave it at 1 for now. As previously stated, effects integrated with the post-processing stack use a variety of macros to ensure multi-platform compatibility.Woohoo, our first ever development blog post!

If you have any questions or critiques, feel free to comment. This is the end result, applied to an adorable Boston Terrier model made by our lead artist Kytana Le :. The shader has two passes: one that applies the texture and lighting, and one that applies the outline. We do two drawings of the mesh by using two passes : one for the regular lighting pass, and one for the outline pass.

To calculate lighting, it takes the dot product of the surface normal and light direction to result in a normalized scalerwhich it then uses to sample a position on the ramp shader on the x-axis. The ramp shader is a 2d texture that only has two colors on it: dark blue on the left, and white on the right.

The position on the x-axis of the ramp shader then corresponds to either of those colors. Areas on the model which are less exposed to light in shadow thus get the dark blue color, and areas on the model which are more exposed to the light get the white color.

And since lighting is one of two colors dark blue or whiteyou get a cel-shaded effect with sharp edges on the shadows and no blending. Basically, the z-buffer is used to place objects visually in front of or behind each other in a simulated 3D space, and the stencil buffer is used to do special effects to override the z-buffer. Achieving the scaled version of the mesh is simple: scale each vertex along its normal direction by outlineSize.

How to get Good Graphics in Unity

Scaling along the normals assures that the outline mesh will scale evenly around the entire object mesh, so even concave parts appear covered by an even thickness outline. The larger outlineSize is, the thicker the outline will be. The first pass the lighting and texturing pass writes to the stencil buffer.

The second pass the outline pass reads the stencil buffer, and, where it sees the same reference already written, keeps the original pixel in place.

In our case, the pixel written by the regular lighting pass. Finally, each pixel of the outline mesh is filled in with the same solid color. A beautiful outline. Mistake 1: Blended cel shading. This is clearly a rookie mistake from a first-time shader writer. My first try at the cel shader had some blended edges on the shadows, which is not what we want for a hard cel look. The mistake I made was doing the lighting calculation in the vertex shader step. Because the vertex shader is per-vertex, every pixel in between is linearly interpolated, which means you end up with a blended effect for some pixels if you do lighting in the vertex shader.

Moving the lighting calculation to the fragment shaderwhich is calculated per-pixel, fixed this immediately. Funnily enough, not ALL of the faces on the model were blended, which turned out to be because the model was non-manifold, which has to do with mistake 3. Mitake 2: Uneven outline. Scaling along the normals made a nice, even outline.Discussion in ' Shaders ' started by ProjectCrykenNov 14, Search Unity. Log in Create a Unity ID.

Unity Forum. Forums Quick Links. Asset Store Spring Sale has begun! Unite Now has started! Come level up your Unity skills and knowledge. Come post your questions! Joined: Jul 25, Posts: My entire game consists only of materials that are white, so in order for people to tell when one object ends and another begins I need a black outline around the object. The unity toon shader is inconsistent when it comes to showing lines and generally looks poor. I have zero knowledge of shaders, so any help appreciated.

Not unlike this antichamber :. ProjectCrykenNov 14, Joined: Aug 20, Posts: I think what you need is not outline, but edge detection. Unity has an edge-detection scripts in ImageEffects package, which I think would help you out.

Will that work in Unity Free? ProjectCrykenNov 15, Joined: Dec 9, Posts: 1, Unfortunately, no. Image Effects are Pro-only.

MauriNov 15, I guess I need an alternative then. I know shaders can do what I need, I just don't where to find them. Actually, Unity has four Toon Shading shaders two are with Outline and they're also working with Free.

Edit: I saw that you're already aware of the toon shaders. Didn't read your posting properly. My fault.

hlsl outline shader

Last edited: Nov 15, Joined: Jul 12, Posts: By using our site, you acknowledge that you have read and understand our Cookie PolicyPrivacy Policyand our Terms of Service. Game Development Stack Exchange is a question and answer site for professional and independent game developers.

It only takes a minute to sign up. How can I draw outlines around 3D models? I'm referring to something like the effects in a recent Pokemon game, which appear to have a single-pixel outline around them:.

Unreal Engine 4 Custom Shaders Tutorial

I can't know exactly how it's done, but I figured out a way which seems like pretty much what they do in the game. Looking at Raichu's mesh in Blender, you can see the ear highlighted in orange above is just a separate, disconnected object which intersects the head, creating an abrupt change in the surface normals.

Based on that, I tried generating the outline based on the normals, which requires rendering in two passes:.

Kakaotalk picture folder

First pass : Render the model textured and cel-shaded without the outlines, and render the camera-space normals to a second render target. Second pass : Do a full-screen edge detection filter over the normals from the first pass.

The first two images below show the outputs of the first pass. The third is the outline by itself, and the last is the final combined result. Here's the OpenGL fragment shader I used for edge detection in the second pass. It's the best I was able to come up with, but there might be a better way. It's probably not very well optimized either. And before rendering the first pass I clear the normals' render target to a vector facing away from the camera:.

I read somewhere I'll put a link in the comments that the Nintendo 3DS uses a fixed-function pipeline instead of shaders, so I guess this can't be the way it's done in the game exactly, but for now I'm convinced my method is close enough. This effect is particularly common in games that make use of cel shading effects, but is actually something that can be applied independently of the cel shading style.

What you are describing is called "feature edge rendering," and is in general process of highlighting the various contours and outlines of a model. There are many techniques available and many papers on the subject.

A simple technique is to render only the silhouette edge, the outmost outline. This can be done as simply as rendering the original model with a stencil write, and then rendering it again in thick wireframe mode, only where there was no stencil value. See here for an example implementation. That will not highlight the interior contour and crease edges, though as shown in your pictures.

Generally, to do that effectively, you need to extract information about the mesh's edges based on discontinuities in the face normals on either side of the edge, and build up a data structure representing each edge.

You can then write shaders to extrude or otherwise render those edges as regular geometry overtop your base model or in conjunction with it. The position of an edge, and normals of the adjacent faces relative to the view vector, are used to determine if a specific edge can be drawn.

You can find further discussion, details and papers with various examples on the internet. For example:. This results in outlines around your original model, if it's a simple model such as a cube. For more complex models with concave forms such as that in the image belowit is necessary to manually tweak the duplicate model to be somewhat "fatter" than its original counterpart, like a Minkowski Sum in 3D.

So if you're doing high poly work, best opt for that approach. Given modern console and desktop capacity for processing geometry, I'd not worry about a factor of 2 at all. You can test the effect for yourself in e. Blender without touching any code.

hlsl outline shader

Outlines should look like the image below, note how some are internal, e.


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *