Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Professional Discussion / [STICKY] Learning to write Shaders

Author
Message
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 27th Aug 2010 02:23
Quote: "and I was first wondering what the dot product of the normal and the opposite of the light direction was doing in there. After a little research, I think I figured it out. Am I right in saying that this value is there to tell how strong the light is based on the direction that the vertex is facing? Since both vectors are normalized the dot product will turn out to be the cosine of the angle between them. Since the cosine of 0 is 1.0 and the cosine of 90 is 0.0, the object will no longer be lit at a point perpendicular to the light direction. Is that right?"


Yes.

Quote: "Nevermind, I think I understand now. The attenuation accounts for how close to the light the pixel is. For pixels near the edge of the light range the attenuation is low, and for pixels deep within it the attenuation is high. Otherwise the whole side of the object would have the same diffuse lighting regardless of the distance from the light. The diffuse doesn't account for the distance from the light anymore since the vectors were normalized. Is that correct?"


Yes. I've used a linear fall-off in light intensity as the distance from the light increases, but the physically more correct rate would be an inverse square law which would be more expensive to evaluate.

Quote: "Well I guess post isn't really a question anymore since I thought about the problem while I was writing, but I still have to be able to explain things to know I understand them"


Agreed.

Quote: "I'll probably ask a question about the specular lighting soon, so watch out"


I will.
Syncaidius
19
Years of Service
User Offline
Joined: 22nd Mar 2005
Location: United Kingdom
Posted: 27th Aug 2010 19:05 Edited at: 27th Aug 2010 19:07
Hi, I'm having a little trouble creating my own deferred lighting shader. I've got the basics down, such has how I would go about rendering light effects and so on, but I'm stuck as to how I would render a depth map to an image for the whole scene.

I need 3 basic things for the start of my deffered shader:
scene image - the scene as it was before any shaders applied, which I can already do/get
Depth map - white for distant pixels/parts of the scene, black for close
Normal map - Renders every pixel on the scene based on the direction the object/mesh/thingy is facing in the scene/camera.

I've done this in c# quite easily, one reason being because you can apply the effect to every object and send the output/final image of the shader to a seperate render target (instead of back onto whichever object)... which I'm not sure can be done in DBP. To my knowledge, when you apply an effect to an object in DBP, it only works "on" the object, rather than allowing you to send the end result elsewhere other than back onto the object.

This little problem is stopping from going any further, so any help at all on this would be appriciated, thanks.

Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 27th Aug 2010 20:19 Edited at: 27th Aug 2010 20:22
There are probably a few ways of doing this in DBPro - and I imagine the basic idea is much the same as in C++. If you're happy applying shaders to each object then just do separate renders of the whole scene using two more cameras and send the output to two images. For this you'll need before the main loop:



then in the main loop include:



I don't know how you are implementing this exactly but something along the above lines should work.

Edit You'll probably find it helpful to try this with a very simple scene first to check the overall logic - it'll also be much easier for others to help you iron out basic bugs. Then when it's working apply the same steps to your main project.
Sixty Squares
17
Years of Service
User Offline
Joined: 7th Jun 2006
Location: Somewhere in the world
Posted: 27th Aug 2010 20:35 Edited at: 27th Aug 2010 20:36
Green Gandalf posted a shadow mapping shader that could help. It's on Page 42 of this thread.

Also, http://www.evolved-software.com/ (EVOLVED's website) has a bunch of shaders you might want to check out.

Here's what I've learned about shadow shading over the past few days...

For the depth map, what I've found is that you can make a separate technique in the effect file for rendering the depth map. You would then use this technique (which would color the pixel based on depth) and render it to the object. The object in this form would then be rendered on a SEPARATE CAMERA besides the main scene camera. Then the object would be textured on another stage with the depth map and you would swap over to the normal technique which renders the object with the shadows.

But yeah I'd have a look at some examples. And remember, to see if one step is turning out OK you can always use PASTE IMAGE to paste the depth map onto the screen to see if it looks how it should.

Sorry if that was confusing, Green Gandalf can probably give some better advice

@Green Gandalf: So far I think I'm actually understanding that specular shader .

EDIT:
Oh, looks like Green Gandalf already responded. That's what happens when you leave an unfinished post up while you go do something else I guess...


Guns, cinematics, stealth, items and more!
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 27th Aug 2010 20:51
Quote: "Oh, looks like Green Gandalf already responded."


Yes - but you had the sense to point to some examples.


Quote: "@Green Gandalf: So far I think I'm actually understanding that specular shader"


Syncaidius
19
Years of Service
User Offline
Joined: 22nd Mar 2005
Location: United Kingdom
Posted: 27th Aug 2010 21:04 Edited at: 27th Aug 2010 21:11
Thanks Green Gandalf.

Am I right to assume that setting/switching the effect/textures on an instance source object will also apply the change to its instance copies too?

Its been a long time since I used shaders and instanced objects together, so if any instanced copies update, Your logic will fit perfectly.

EDIT:
If I do get it working, I'll post it as a little project here for everyone else to learn from or use.

Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 27th Aug 2010 22:59
Quote: "Am I right to assume that setting/switching the effect/textures on an instance source object will also apply the change to its instance copies too?"


I don't know. That's a good example where it's a good idea to test that idea on a simple scene first. One of my earlier shader demos uses cloned objects. It worked fine till I tested it on my new machine (OK it's 15 months old now). It crashes but doesn't crash if I remove the effect applied to the cloned objects. When I remember I'll investigate that issue with some simple demos. I doubt it's anything to do with DBP upgrades - the crash occurs with old exes as well as new but only on the one machine. Works fine on my old dinosaurs. Frustrating.

Quote: "If I do get it working, I'll post it as a little project here for everyone else to learn from or use."


That will be useful. Thanks.
Syncaidius
19
Years of Service
User Offline
Joined: 22nd Mar 2005
Location: United Kingdom
Posted: 28th Aug 2010 13:44 Edited at: 28th Aug 2010 13:49
It seems your idea worked well. There was a depth shader included with dark shader so I took that and used it as the depth technique for my own shader, applied your logic to it and it has turned out well. I have a depth map.



EDIT:

I'll try fixing up a quick demo later.

Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 28th Aug 2010 14:36
Thanks for the update.

Here's a simple shader demo for you all to play with. It merely adjusts the colour components of the object's texture. Press the left or right mouse buttons to change the colour intensity. The image is set to greyscale initially.

Attachments

Login to view attachments
Syncaidius
19
Years of Service
User Offline
Joined: 22nd Mar 2005
Location: United Kingdom
Posted: 28th Aug 2010 14:55
Nice little demo.


Another update on the defferred shader, managed to get the normal map calculated now:

It doesn't look like much in the screenshot, but it plays an important part when it comes to the lighting part of my deffered lighting system.

Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 28th Aug 2010 15:13
Quote: "but it plays an important part when it comes to the lighting part of my deffered lighting system."


Indeed. Looking forward to seeing what you achieve.

How are you coding the normals - in world space, object space or tangent space? I guess world space is probably easiest.
Math89
20
Years of Service
User Offline
Joined: 23rd Jan 2004
Location: UK
Posted: 29th Aug 2010 00:16
Trying to achieve a good deferred shading system in DBPro is a waste of time as you can't use Multiple Render Targets. Of course you can achieve a good quality, but it will be way too slow for any practical use. Furthermore, if I remember correctly, even when using fastsync, a lot of useless things are updated such as animations for example, which means that the normal pass will be slightly different from the albedo pass (and anyway, animations are slow in DBPro, so it will be 4 times slower if you need 4 passes).
Anyway, good luck, I know that writing a deferred shader can be quite frustrating.
Sixty Squares
17
Years of Service
User Offline
Joined: 7th Jun 2006
Location: Somewhere in the world
Posted: 30th Aug 2010 00:51 Edited at: 30th Aug 2010 01:00
Hello again, I have 2 real questions this time

1)

I was looking at Lee's Tip of the Month in April's Newsletter and noticed the new RENDERCOLOR semantic. Supposedly this would make creating effects such as bloom much easier, because the glow and blur passes could all be done with 1 camera.

Does anyone know how to use this semantic? I'd like to see what it can do

-------------------------------------------------------------

2)

What is the point of having multiple passes in a shader? Do textures carry from the first pass over to the second pass? For example, say an object was initially textured with a red texture. If the first pass applies a pixel shader effect to an object which turns it green, and the second pass brightens the color of the object, would the object appear bright green or bright red?


Thanks for any help!


Quote: "a lot of useless things are updated such as animations for example"


Actually, that might not be true anymore (at least the animation part). See my misguided bug report:

http://forum.thegamecreators.com/?m=forum_view&t=174863&b=15


Guns, cinematics, stealth, items and more!
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 30th Aug 2010 12:42
Quote: "Does anyone know how to use this semantic? I'd like to see what it can do "


You can use it to speed up things like blur in a fullscreen shader or a shadowmapping shader. In a blur shader, for example, you can separate the blurring into horizontal and vertical passes which can save an enormous amount of computation. There's a blur example in the full-screen shaders that come with DarkShader. If you haven't got it you probably should. One point of intermediate render targets is that you can access intermediate results for adjacent pixels as in the two pass blurring technique.

Actually I don't know what Lee meant by that Newsletter remark - I thought the RENDERTARGET semantic was already supported because the extra stuff that came with DarkShader was included in DBPro a long time ago.

Quote: "What is the point of having multiple passes in a shader? Do textures carry from the first pass over to the second pass? For example, say an object was initially textured with a red texture. If the first pass applies a pixel shader effect to an object which turns it green, and the second pass brightens the color of the object, would the object appear bright green or bright red?"


Various reasons. Two spring to mind immediately:

1. Instruction count limits - up to Shader Model 2 the number of instructions allowed in a pixel shader was quite small and is still restricted in Shader Model 3. This made it difficult to implement things like multiple per-pixel lights with bumpmapping. If you look at some of EVOLVED's bumpmapping shaders for several lights you'll see he's split the calculations into two or more passes. This works well with lighting because you can use additive alpha-blending in each pass to add the effect of the new light to the results of lights already rendered in an an earlier pass.

2. See my comments about blurring.
Sixty Squares
17
Years of Service
User Offline
Joined: 7th Jun 2006
Location: Somewhere in the world
Posted: 30th Aug 2010 20:13
Thanks for responding, I think I understand now . I never knew the blur shader used that semantic

Quote: "Instruction count limits - up to Shader Model 2 the number of instructions allowed in a pixel shader was quite small and is still restricted in Shader Model 3."


This is probably a really dumb question, but what is an instruction?


Guns, cinematics, stealth, items and more!
_Pauli_
AGK Developer
14
Years of Service
User Offline
Joined: 13th Aug 2009
Location: Germany
Posted: 30th Aug 2010 21:22 Edited at: 30th Aug 2010 21:50
Hey, I'm trying to implement a shader to do some reflections on a plane (like a mirror or water). But I'm stuck right now, maybe someone can help!
(I hope you don't mind some DarkGDK code but it's essentially the same in DBPro)

I tried to do it like this:

1) I have to cameras. The main camera 0, which is the actual screen output, and camera 1, which is at the same X and Z positions as camera 0, but it is inverted on the Y axis (since the mirror is a flat plane on the ground at Y position 0). The plane is textured with the image of camera 1.



2) Then I hide the plane, change the current camera to the reflection camera, sync mask it, get its view matrix and pass it to the shader for projective texturing. Then the plane is shown again and the default camera is set back to 0.



3) Finally here is the shader code:



So the problem is, that the reflection is actually shown on the plane, but not projected correctly! Maybe someone already did something similar and can give some advice...


EDIT:
Hmm, okay... somehow I fixed it by flipping the projected Y coordinate in the pixel shader! (just put a minus before input.reflectpos.y/input.reflectpos.w)

Now the plot thickens, the fps decreases, and the awesomeness goes through the roof.
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 30th Aug 2010 22:32 Edited at: 30th Aug 2010 22:33
I like that sort of question.

@Sixty Squares

An instruction is a fundamental command in the compiled asm code. Most simple shader statements such as

pos.x = In.pos.x+1;

compile to one asm instruction. Others can compile to a large number of instructions depending on the Shader Model you're using - older Shader Models generally need more instructions.

If you have DarkShader you can see the asm instructions by using Build/Save assembly which saves the asm code to a txt file.

It's the total number that matters. Here's a relevant extract from the MS DX9 SDK docs:

Attachments

Login to view attachments
prasoc
15
Years of Service
User Offline
Joined: 8th Oct 2008
Location:
Posted: 1st Sep 2010 02:13
Coming in a bit later here ^_^ but I was wondering if anyone knew how/where to get a shader that is the same as photoshops "find edges" tool (and works in dgdk) and then how could I apply things like "overlay" to the shader? I found this http://www.nathanm.com/photoshop-blending-math/ but I have no idea about shader programming

Sixty Squares
17
Years of Service
User Offline
Joined: 7th Jun 2006
Location: Somewhere in the world
Posted: 1st Sep 2010 03:17
@Green Gandalf: Ah, thanks for that. But looking at those numbers, it doesn't seem like a lot of instructions are allowed. Surely I've seen more than 8 arithmetic operations done in a ps_1_4 shader before... or am I misunderstanding something?


Guns, cinematics, stealth, items and more!
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 1st Sep 2010 13:15
Quote: "Surely I've seen more than 8 arithmetic operations done in a ps_1_4 shader before"


You probably have.

The words you've missed are "per phase" - there's more than one "phase" in a PS1.4 shader, two I think which probably means the limit is 16. Programmers who write in asm need to know about phase - HLSL programmers don't. It's a mysterious concept to me and I decided long ago that it wasn't worth worrying about.

Here's an example of a pixel shader written in HLSL for PS1.4 (one of the DarkShader shaders):



and here's the compiled asm code for that pixel shader:



where you'll see "phase" part way through the list of instructions.

Quote: "it doesn't seem like a lot of instructions are allowed"


Indeed. Which is one reason for writing shaders carefully. The main practical reason for writing efficient shader code is that the pixel shader instructions are repeated for each screen pixel and many times a second. So the sheer weight of processing soon mounts up. The instruction count limits are not so taxing for pixel shader models PS2a/b and above - but up to PS2 they are a nuisance.
jason p sage
16
Years of Service
User Offline
Joined: 10th Jun 2007
Location: Ellington, CT USA
Posted: 1st Sep 2010 23:31
I wanted to add (you folks probably already know this but) something that Green Gandalf's post reminded me of I thought I'd mention.

The Assembly bits mentioned above, and the way that these "mini programs" can execute MANY times per second (per pixel let's say) on a decent graphics card the numbers are quite astonishing...

...anyways.. this is exactly why folks often use SHADER CODE and Graphics processors (Nvidia, ATI whatever) for doing complex computer processing for things often not graphically related at all.

I know of one particular project where a fellow (granted graphics related - but not really a shader thing)... where using LIDAR (Laser scans of buildings... say factories with complex piping etc)... that turning this HUGE data set into a 3D model requires analyse of each 3d point - and there might be TERABYTES worth of them. So By making simple (shader GPU capable) calculations - the data sets could be passed through the Graphics card and computed faster than using the CPU not to mention most decent graphics card have multiple cores in them - and therefore LOTS and LOTS of data coould be processed in parallel.

Sorry for the tangent - but when I read your Post Green Gandalf, this came to mind - just how powerful these little GPU's have become.

--Jason

Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 2nd Sep 2010 00:21
Yes, GFX cards are very powerful parallel processors and are often underutilised. I believe nVidia's CUDA is a language which allows you to use the GFX card as a processor.
Sixty Squares
17
Years of Service
User Offline
Joined: 7th Jun 2006
Location: Somewhere in the world
Posted: 2nd Sep 2010 03:44
@Green Gandalf: Ah. Thanks for explaining that, I didn't know what a phase was . I guess you can squeeze more calculations in there than I thought! I haven't run into instruction limit problems so far, anyway.


Guns, cinematics, stealth, items and more!
Sixty Squares
17
Years of Service
User Offline
Joined: 7th Jun 2006
Location: Somewhere in the world
Posted: 6th Sep 2010 23:08 Edited at: 7th Sep 2010 01:30
Just thought I'd let everyone know about this:
http://forum.thegamecreators.com/?m=forum_view&t=175275&b=1

Post a shader here and it should get added to the archive the next time I update it.

Green Gandalf seems to have been posting the most around here


Guns, cinematics, stealth, items and more!
Pharoseer
17
Years of Service
User Offline
Joined: 21st Feb 2007
Location: Right behind you
Posted: 8th Sep 2010 09:54 Edited at: 8th Sep 2010 09:55
Hey guys,

Took a bit of time the other day to monkey around with shaders again. I wrote a simple example that creates a seamless animated Worley noise effect using both Euclidean and Manhattan distance formulas.

The only problem I've run into is an occasional graphical glitch near both the vertical and horizontal centers. I'm sure it's a floating point problem I'm not correcting for.

Anyway, feel free to use this if you can find a use for it and if anybody spots my mistake, let me know. Also, the code is heavily documented so it should be "fairly" easy to understand.

Cheers,
Frank

Attachments

Login to view attachments
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 8th Sep 2010 15:17 Edited at: 8th Sep 2010 15:19
Nice demo.

From your code:

Quote: "// I'm not sure why, but if the camera is perfectly centered vertically with the surface affected by the shader, then
// there are often visual artifacts. Perhaps Green Gandalf might know the reason? Set Y to 0 (zero) to see what I'm
// talking about."


It took me some time to spot any glitches (I had to slow your code down - sync rate 0 was way too fast on my machine). Is this what you mean?



If it is I'll delve into the code and see if there's a simple explanation. If not could you describe the glitch?

Attachments

Login to view attachments
Pharoseer
17
Years of Service
User Offline
Joined: 21st Feb 2007
Location: Right behind you
Posted: 9th Sep 2010 00:59
That's exactly the problem, GG. I've noticed it in at least one other shader I'm working on (which has other problems as well). I've been assuming it was a floating point error or the like since it occurs specifically at the centers (at least on my machine).

I honestly don't have a real clue as to what is causing it. The reason I've been thinking it must be a floating point error is because both shaders use "tiles" from the source texture to generate the final texture. If you'd like I can post the other shader I've been working on as well. It's fairly simple compared to this one.

Thanks,
Frank

Quote: "(I had to slow your code down - sync rate 0 was way too fast on my machine)"


P.S. I'm jealous. sync rate 0 gives me a maximum of about 150 FPS when I can barely see the cubes and as low as 5 when I'm as close as your screenshot. Ah well, this laptop is a few years old.
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 9th Sep 2010 01:21 Edited at: 9th Sep 2010 01:27
Quote: "That's exactly the problem, GG. I've noticed it in at least one other shader I'm working on (which has other problems as well). I've been assuming it was a floating point error or the like since it occurs specifically at the centers (at least on my machine)."


I've noticed it in other situations too. It might be a floating point problem but I have a feeling it is something to do with the way you've wrapped around the texture coordinates. For example it could be a byproduct of the way mipmapping is done internally. It's late now and I haven't got time to check tonight but what happens if you turn off mipmapping in the shader texture sampler? Does the problem disappear? (You might get other problems instead of course. )

[Edit I see you've already turned off mipmapping. I'll have another think tomorrow.]

Anyway, thanks for confirming that. I'll have a fresh look tomorrow.
Pharoseer
17
Years of Service
User Offline
Joined: 21st Feb 2007
Location: Right behind you
Posted: 9th Sep 2010 02:02
Thanks GG, there's no rush. I'm seldom in a hurry with these things. I work on many projects at once so that I don't get impatient when I can't fix a problem with one of them. I think I'm going to tinker with some other shaders while I wait. I may have more for you to look at soon enough.

Cheers,
Frank
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 9th Sep 2010 23:29 Edited at: 10th Sep 2010 00:12
Had a look at your shader but no luck with the artefacts yet.

[Edit!!! OUCH! No wonder I couldn't see any difference! I saved my mods with a new file name but didn't change the fx call in the dba file, so please ignore most of the following. I'll come back again when I've been a bit more careful.]

I did spot some possible optimizations - one major and a few minor ones (stylistic ones really).

[Edit2 The following comments about the 3x3 for loop are daft - I missed something obvious. ]

The main one was that I just can't see the point of the 3x3 "testing" loop in the pixel shader - it seems to use the final values that are examined in the loop. Perhaps I missed something?

Here's my modified version of the Euclidean pixel shader - I've changed only the Euclidean pixel shader and as far as I can tell it still works. It should run much faster too - about 18 instructions rather than 45.



Still a good looking shader though. I hope you produce more of these. [Edit Looks even better now I've realised I was talking nonsense earlier. ]
Pharoseer
17
Years of Service
User Offline
Joined: 21st Feb 2007
Location: Right behind you
Posted: 10th Sep 2010 02:20
Hey GG,

Don't feel bad. I do the same thing with your shaders. I've spent ages looking at one of them before not knowing why you did a particular thing until I decided to "fix" it, only to have it stop working altogether.

I have to admit, I was excited at the idea that you had optimized the shader down to 18 instructions. With that kind of magic you would have definitely earned the moniker "Gandalf."

Thanks again for taking a look at it.

Cheers,
Frank
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 10th Sep 2010 11:50
Quote: "I was excited at the idea that you had optimized the shader down to 18 instructions."


Yes, it was rather too good to be true - that alone should have been enough to warn me. The truth sunk in eventually of course.

Here's my slightly modified version of your original shader - I've changed only the Euclidean pixel shader but I guess the same or similar changes could be made to the other pixel shaders as well. Most of the changes are stylistic but a couple of other minor changes merely remove something that I couldn't see a need for. The net result is a reduction of about 4 instructions.

I've added brief comments where I think it's necessary.



I still think it looks very nice.
Pharoseer
17
Years of Service
User Offline
Joined: 21st Feb 2007
Location: Right behind you
Posted: 12th Sep 2010 00:47
Thanks again, GG. I went back and looked at the differences between your version and mine. I ended up making all the changes you suggested with one minor difference. I switched the order of the arguments in the lerp command. It's a slight visual difference, but nothing big. The weird commands I had in there to "trick" the compiler were removed as well. Something strange was going on that I was trying to correct, but obviously I was on the wrong track.

I've attached the improved and cleaned up copy of the shader with credits to you included in the comments. Anyone is free to use it as long as they keep those in there.

Also, as promised, I should have another shader or two ready to showcase in the next day or so (provided I can work on them). I'm really hoping for something to show today, but we'll see how that goes.

Attachments

Login to view attachments
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 12th Sep 2010 13:18
Thanks. I look forward to seeing more from you. That was a nicely coded and set out shader. We need more of those here.

Quote: "I switched the order of the arguments in the lerp command. It's a slight visual difference, but nothing big."


Should be a major difference unless the two colours you are "lerping" are similar or the lerp weight doesn't vary much. For example if you lerp between black and white then swapping the role of black and white will give you the negative image which will be quite noticeable unless the lerping constant is around 0.5.
Omen
17
Years of Service
User Offline
Joined: 7th Nov 2006
Location: Maple Grove, MN US
Posted: 26th Sep 2010 21:13
Has anyone been able to get Variance Shadow Mapping working in DBPro - I did a pretty exhaustive search and found only 1 reference to it, but of course links were broken to the example files.

Here's what I've got so far, but everything in the scene just comes up black (object are about 200 units in size). I wanted to modify the light direction - but I'm not even sure which vector to modify to do that... is it u_shadow_projection for the light direction?



Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 26th Sep 2010 21:30
Nice long shader.

Perhaps you could remind us what variance shadow mapping is?
Dr Tank
14
Years of Service
User Offline
Joined: 1st Apr 2009
Location: Southampton, UK
Posted: 27th Sep 2010 00:41 Edited at: 27th Sep 2010 00:44
When it comes out black I try commenting out stuff with /* ..... */ until it does something (that will often mean commenting out variables that are used for the output, so replace the output with something simple). Then uncomment stuff until it doesn't work and you can find what's bad. You could have missed out a semicolon, not declared a variable, misspelled a variable, hit the instruction limit. Loads of things.

If you replace

output.color = float4(lit_color, 1.0);

with

output.color = float4(light_color, 1.0);

and your shader is still black, the problem is probably with your shader rather than the way you're using it.
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 27th Sep 2010 02:11
Use an editor like Dark Shader - that'll tell you straightaway whether it's syntactically correct or not and where the first error is. When it compiles it's worth doing the other stuff.
Math89
20
Years of Service
User Offline
Joined: 23rd Jan 2004
Location: UK
Posted: 27th Sep 2010 21:10
Variance Shadow Mapping is a technique based upon mipmapping for having smooth shadows in constant time. The only problem is that DBPro doesn't have commands for mipmap generation (which would be required on the render target).
Dr Tank
14
Years of Service
User Offline
Joined: 1st Apr 2009
Location: Southampton, UK
Posted: 27th Sep 2010 23:10
Quote: "Variance Shadow Mapping is a technique based upon mipmapping for having smooth shadows in constant time. The only problem is that DBPro doesn't have commands for mipmap generation (which would be required on the render target). "

This is an interesting point. This may be making the shadow mapping in my game slow.
Wilf
Valued Member
17
Years of Service
User Offline
Joined: 1st Jun 2006
Location: Gone to Unity.
Posted: 29th Sep 2010 14:46
Hi guys,

Shader noob here, can anyone help? I am trying to:
Apply Evolved's Cartoon Shader to an object Lightmapped using Darklights

Apply an alpha mask using SET BLEND MAPPING ON to simulate lit windows

Have the thing illuminated from a single light source. It needs to be very dark on the side away from the light.

Here is the object exported from Dark Lights without shading enabled:


(It does almost everything needed except there are no groovy cartoon edges or shading)

This is the object exported from Dark Lights with shading enabled and Evolveds Cartoon Shader applied:



(It appears to be rotating/warping the diffuse map)

Any ideas why the texture is warping?
Also, if you zoom out far enough to see the light sphere object, the cartoon edges stop rendering.

I've attached the whole project to this post (gimme da codez )

See the standard version by commenting out the following line


Thanks, any help is appreciated!

Attachments

Login to view attachments
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 29th Sep 2010 18:46
EVOLVED's cartoon shader assumes stage 0 is the "toon" image and stage 1 is the "shade" image. It doesn't use the other stages.

I don't think you can mix blend mapping with a shader - use one or the other.

Your best bet is to amend the shader so it uses all five textures, e.g.

stage 0 - base texture
stage 1 - lightmap
stage 2 - alpha mask
stage 3 - toon image
stage 4 - shade image

The first three could probably be applied using straightrorward blend mapping - but the last two will need the shader as far as I know so to combine them you'll need a shader.

I had a look at the X files and could only find one set of UV coords. Is that the case or should there be two? Lightmapped objects often (usually?) use different UV coords for the base texture and lightmap but not necessarily.

Quote: "Any ideas why the texture is warping?"


Because your code moves the light and the shader uses that to look up the shade texture which you've replaced with the diffuse texture.
jason p sage
16
Years of Service
User Offline
Joined: 10th Jun 2007
Location: Ellington, CT USA
Posted: 29th Sep 2010 18:55
@Green Gandalf:

I'm not positive but I think the DirectX file format allows there to be multiple UV's... like for a given mesh you can have an array of 8 of them.. I think the file format changes depending on how the model was saved..

I might be wrong here... been awhile since I've been in the game trenches...

Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 29th Sep 2010 19:01 Edited at: 29th Sep 2010 19:54
It does - but I couldn't see it in the X file. It had the FVF template but no FVF data.

Edit Just checked the dbo version of the cruiser - that has only one set of UV coordinates as well. This isn't a problem if the object is designed that way.

Edit2 Ah! The non-shader version of the cruiser does have two sets of UV coordinates - but what I thought was the corresponding X file doesn't. I think we need Wilf to clarify this. If the lightmapping uses a different set of UV coords, i.e. different UV coord values, then the shader version will need those as well. In fact I don't see why two versions of the model are needed (although the multitexturing might be a problem).

Edit3 What purpose does the toon image serve? Could it just be replaced by the diffuse texture, cruiser.dds? That would simplify things. See image below:

Attachments

Login to view attachments
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 29th Sep 2010 21:34 Edited at: 29th Sep 2010 21:35
Is this more like what you want?



I've temporarily removed the lighting since you have a lightmap. Actually, what's the logic of having a lightmap in this instance - lightmaps are usually used for static objects?

Attachments

Login to view attachments
Wilf
Valued Member
17
Years of Service
User Offline
Joined: 1st Jun 2006
Location: Gone to Unity.
Posted: 29th Sep 2010 21:54
Yes, thats almost exactly what I want!

The lightmap is there to approximate a SSAO (Screen Space Ambient Occlusion) look - I like the hint of depth that it gives. I'm hoping this in combination with the cartoon edges will give a distinctive look that minimises texture detail. I like the look of expanses of metal covered with one paint colour, broken up with a few high-tech looking panels (e.g. around the windows.

To answer your previous question, yes the toon texture would be replaced with the diffuse one. I'm hoping to use the textures in the model rather than specify them because I'd like to apply this shader to my tactical levels as well (minus the alpha map):

Each wall and floor 'block' is textured separately.

Also yes it should have 2 sets of UVs- one for the LMs and one for diffuse, but you've figured that out already
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 29th Sep 2010 22:08
Quote: "Yes, thats almost exactly what I want! "


Thanks. Sounds like we're nearly there. What else do you want to add? Just the additional light that I removed or something else? Once we've got that sorted out I'll post a clean () version of my code.
Wilf
Valued Member
17
Years of Service
User Offline
Joined: 1st Jun 2006
Location: Gone to Unity.
Posted: 29th Sep 2010 22:16
I'm wracking my brain but I can't think of anything else...

Thanks so much for this!
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 29th Sep 2010 22:19
OK. I'll reinstate EVOLVED's lighting, clean my code and post it, probably later this evening.

Quote: "I'm wracking my brain but I can't think of anything else...
"


Thank goodness for that. It's been a busy day.
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 29th Sep 2010 22:43
Here you go:

Revised dba code:



revised shader code:



Any problems, just ask.

Login to post a reply

Server time is: 2024-03-29 00:47:34
Your offset time is: 2024-03-29 00:47:34