About Store Forum Documentation Contact
Donations:
158$/mo



Post Reply 
Custom Shaders
Author Message
Brainache Offline
Member

Post: #1
Custom Shaders
After your post regarding my material coloring questions...

I've looked at the custom shader tutorials - and I am eager to get my feet wet in shaders ( im a shader newb... so be warned!)

I am playing around with the existing tutorials to learn, and came across wanting to add a random vec to the color for testing...

I dont see any function for random numbers - can the Randomizer be used someone in the shader code?

Is there any documentation that I should take a look at to guide me during my shader exploration?

Thanks!
06-01-2009 05:07 PM
Find all posts by this user Quote this message in a reply
Esenthel Offline
Administrator

Post: #2
Re: Custom Shaders
you can use Randomizer but not exactly inside shaders,

this should be handled by for example:

creating a "Vec color;" value inside the shader
and then setting its value from the C++ game codes like this, shader->setInt("color",...); here you can set the value based on the Random value

creating shaders is handled just as in normal DirectX, so I can advise to take a look at the DirectX documentation
06-01-2009 06:00 PM
Find all posts by this user Quote this message in a reply
Brainache Offline
Member

Post: #3
Re: Custom Shaders
Allrighty will do...

Question on material access:

I see the contstants defined for most of the maps.. but I dont see one for LightMap ( I thought Lum was it.. but it appears as tho that gets overwritten by the shadowmapper - maybe its supposed to..)

My point: I'm looking to be able to assign a texture to a material to use as a color map... Im currently using the 'detail' texture slot for it.. but thought LightMap would be better since it isnt used anywhere...

Your thoughts? ( I dont believe I can access just *any* texture from the shader...)
06-01-2009 06:23 PM
Find all posts by this user Quote this message in a reply
Esenthel Offline
Administrator

Post: #4
Re: Custom Shaders
Hi,

Lightmap is set only while rendering in RM_LIGHT mode

for your case I can suggest using "detail" or "reflection" slots,

you can also define your custom texture in the shader, and then set it from the C++ codes
06-01-2009 06:46 PM
Find all posts by this user Quote this message in a reply
Brainache Offline
Member

Post: #5
Re: Custom Shaders
Hey there... need a nudge in the right direction if you can please..

I am trying to modify the color in a pixel shader based on the angle between the point and "UP" (Vec(0,1,0))

I've played around using comparisons between inPpos and inNrm but they change based on where the camera is facing... how can I convert the pos/nrm into world coords ( i'm guessing thats what I need)

Thanks
06-02-2009 06:56 PM
Find all posts by this user Quote this message in a reply
Esenthel Offline
Administrator

Post: #6
Re: Custom Shaders
Hi,

in the pixel shader the inPos and inNrm are in view space, so to convert it to world space you'd need to transform the vector by camera matrix (MatrixC) which is available in the shader constants

something like this

Vec world_space_normal=mul(inNrm,(Matrix3)MatrixC);

or

Vec world_space_normal=mul((Matrix3)MatrixC,inNrm);

I don't remember which should be first in the 'mul', because the order makes a difference, so please try both

and the (Matrix3) conversion is used to get only Orientation of MatrixC (by skipping its Position)
06-02-2009 10:52 PM
Find all posts by this user Quote this message in a reply
Brainache Offline
Member

Post: #7
Re: Custom Shaders
I've moved my shader code from a test app to the watch, but it appears as tho the 'getShader' function never gets called...

I've done "D.get_shader=GetShader;" at the end of InitPre

The compiling of the shader is working..
I think I have the shader assigned to the material correctly...

Even so.. I have an output at the start of 'getShader' (before the material/shader enum) check.. and it never gets called..

I am not using the shader enum from the tutorial "#include "../../../../../data/enum/material_user_shader.enum.h""...

I have an enum built in a shader class (that i'll be using to manage my shaders...) - could that be the cause?

Thanks
06-03-2009 11:07 PM
Find all posts by this user Quote this message in a reply
Esenthel Offline
Administrator

Post: #8
Re: Custom Shaders
Quote:but it appears as tho the 'getShader' function never gets called...
please use debugging to see if its called
"run to cursor"
06-04-2009 12:11 PM
Find all posts by this user Quote this message in a reply
Brainache Offline
Member

Post: #9
Re: Custom Shaders
Ok.. looks like it is getting called during initialization ( before my output could show it)...

So... that led me to how I am assignint the shader.. and here is where I'm at now:

I cant get the shader assigned to the materials for a CHR object correctly...
I've tried the following after the chr is created:

Material *skin = Materials("mtrl/leaf/0/0.mtrl");
skin->user_shader = SHD_COLORMAP;

for (int p=0;p<spawn->mesh->parts();p++)
{
for (int mi=0;mi<4;mi++)
{
if (spawn->mesh->part(p).material[mi]) spawn->mesh->part(p).material[mi]->user_shader = SHD_COLORMAP;
}
}

- shader does not get assigned...

However:
for (int p=0;p<spawn->mesh->parts();p++)
{
spawn->mesh->part(p).setMaterial(skin);
}

- Shader gets assigned and renders correctly... however... the model no longer animates!

What I want... is to get a pointer to the material being used by the chr.. and enable the shader for that material...
(loading the leaf material into skin variable is just a test)


Also - for effectiency.. if I could just say.. render all 'OBJ_CHR' with SHD_COLORMAPPER ... that would be peachy...

Where am I gonig wrong?

Thanks!
06-04-2009 07:17 PM
Find all posts by this user Quote this message in a reply
Esenthel Offline
Administrator

Post: #10
Re: Custom Shaders
changing 'user_shader' doesn't change the shader, but only the helper parameter which may be used in the custom 'GetShader' function to select the shader that you want.
if changing 'user_shader' in the codes, then you should call 'setShader' method for the Mesh, to set the new shader according to 'user_shader'
when calling 'setShader', GetShader function will be used inside

Quote:spawn->mesh->part(p).setMaterial(skin);
}

- Shader gets assigned and renders correctly...
setMaterial automatically calls 'setShader' inside

Quote:the model no longer animates!
that means that the animation was in the old shader, and that you dont use animation in the new shader
06-04-2009 07:26 PM
Find all posts by this user Quote this message in a reply
Brainache Offline
Member

Post: #11
Re: Custom Shaders
Aha... so 'GetShader' doesnt get called each render phase? is that correct?
06-04-2009 08:31 PM
Find all posts by this user Quote this message in a reply
Esenthel Offline
Administrator

Post: #12
Re: Custom Shaders
yes, that would be inefficient
06-04-2009 08:34 PM
Find all posts by this user Quote this message in a reply
Brainache Offline
Member

Post: #13
Re: Custom Shaders
Quote:the model no longer animates!
that means that the animation was in the old shader, and that you dont use animation in the new shader


Ok... now that has just completly confused me smile

How is the animation in the shader? How do I get the chr to be animated but render with a custom shader?

Here is how I am trying atm:

Game::ObjParams obj; // create object parameters variable
obj.type(true,"OBJ_CHR");
obj.sortParams();
obj.mesh(true,Meshs(S+spawnFile+".mesh"));
for (int p=0;p<obj.mesh()->parts();p++)
{
for (int mi=0;mi<4;mi++)
{
if (obj.mesh()->part(p).material[mi]) obj.mesh()->part(p).material[mi]->user_shader = SHD_COLORMAP;
}
}
obj.mesh()->setShader();

Phys *phys = new Phys(1);
phys->part(0).create(Capsule(0.2,1));
obj.phys(true,phys);

Game::Obj *spawnObj = Game::World.objCreateNear(obj,Matrix(obj.scale(),spawnPos));

This is setting the shader correctly.. but w/o animation...
06-04-2009 10:17 PM
Find all posts by this user Quote this message in a reply
Esenthel Offline
Administrator

Post: #14
Re: Custom Shaders
Hi,

for animation skinned custom shaders I'd need to make a tutorial for that, this will be ready when I'll finish updating the World Editor (at least few days).
06-05-2009 04:26 PM
Find all posts by this user Quote this message in a reply
Brainache Offline
Member

Post: #15
Re: Custom Shaders
Allrighty.. Looking forward to the shader and the world editor!
06-06-2009 03:31 AM
Find all posts by this user Quote this message in a reply
Post Reply