Shader problem + extracting world position and time ?

For questions about using Classic.

Post » Thu Nov 25, 2010 2:05 pm

Hi everyone,

I'm trying to make some very simple pixel shaders mostly for learning purpose (and creating color shifts based on world position or wind deformation for plants the same way). When i try to use it, construct gives me an error :


Here's the code i'm using :

[code:10w66x7m]// World recolor
// Valerien
// PS 2.0
// Overlaps an object's color depending on its position in the screen

//#PARAM float redAdd 0.1 : RedShift : Adds red to the object.
//#PARAM float greenAdd 0.1 : GreenShift : Adds green to the object.
//#PARAM float blueAdd 0.1 : BlueShift : Adds blue to the object.

//special values extracted from construct
texture ForegroundTexture;
// texture BackgroundTexture;
int boxLeft;

//sampler
sampler2D foreground = sampler_state
{
Texture = (ForegroundTexture);
MinFilter = Point;
MagFilter = Point;
MipFilter = Point;
}

//ColorShift function
float4 EffectProcess( float2 Tex : TEXCOORD0 ) : COLOR0
{
float4 front = tex2D(foreground, Tex.xy);
float3 colorShifter;

colorShifter.r = redAdd;
colorShifter.g = greenAdd;
colorShifter.b = blueAdd;

boxLeft.x *= 0.1;
//sin and cos functions are used for
frontTex.rgb *= ((sin(boxLeft.x) - 2*cos(boxLeft.x)) * colorShifter.rgb)/colorShifter.rgb;

return front;
}

//technique
technique ConstructEffect
{
pass p0
{
VertexShader = null;
PixelShader = compile ps_2_0 EffectProcess();
}
}[/code:10w66x7m]

Also, is there a way to get the position of the object passed through the shader ? And to get a time value from construct please ?

Thanks in advance !
B
72
S
15
G
4
Posts: 311
Reputation: 11,609

Post » Thu Nov 25, 2010 5:59 pm

The error message was thrown out, because you forgot the semicolon after sampler. But there were two more issues. You defined parameters for Construct, but didn't define them for the effect. You defined a float4 front but tried to access it as frontTex.

Here is the corrected code:
[code:3ft8mo3u]// World recolor
// Valerien
// PS 2.0
// Overlaps an object's color depending on its position in the screen

//#PARAM float redAdd 0.1 : RedShift : Adds red to the object.
float redAdd;

//#PARAM float greenAdd 0.1 : GreenShift : Adds green to the object.
float greenAdd;

//#PARAM float blueAdd 0.1 : BlueShift : Adds blue to the object.
float blueAdd;

//special values extracted from construct
texture ForegroundTexture;
// texture BackgroundTexture;
int boxLeft;

//sampler
sampler2D foreground = sampler_state
{
Texture = (ForegroundTexture);
MinFilter = Point;
MagFilter = Point;
MipFilter = Point;
};

//ColorShift function
float4 EffectProcess( float2 Tex : TEXCOORD0 ) : COLOR0
{
float4 front = tex2D(foreground, Tex.xy);
float3 colorShifter;

colorShifter.r = redAdd;
colorShifter.g = greenAdd;
colorShifter.b = blueAdd;

boxLeft.x *= 0.1;
//sin and cos functions are used for
front.rgb *= ((sin(boxLeft.x) - 2*cos(boxLeft.x)) * colorShifter.rgb)/colorShifter.rgb;

return front;
}

//technique
technique ConstructEffect
{
pass p0
{
VertexShader = null;
PixelShader = compile ps_2_0 EffectProcess();
}
}[/code:3ft8mo3u]

EDIT
[quote="Valerien":3ft8mo3u]Also, is there a way to get the position of the object passed through the shader ? And to get a time value from construct please ?[/quote:3ft8mo3u]
I guess you already had a look at this page?
The only "time value" you get is frameCounter, I'm afraid. Yes, it is not much...
For positions you have
boxLeft
boxTop
hotspotX
hotspotY
pixelWidth
pixelHeight
all expressed as a number from 0 to 1, relative to the display.
Image
B
23
S
8
G
10
Posts: 1,820
Reputation: 8,242

Post » Thu Nov 25, 2010 7:39 pm

Thanks a lot ! I thought that construct would define by itself the parameters ^^'. Also i've changed the param frontTex to front and forgot it in the main function.

About the variables from construct : the problem is that i cannot get values relative to the origin of the world. This would be cool to create effects such as colorShift for vegetation, to alterate wind effects, motion or so... and it only adds 1 instruction per shader. Well, maybe this will be added later.
B
72
S
15
G
4
Posts: 311
Reputation: 11,609

Post » Thu Nov 25, 2010 8:44 pm

[quote="Valerien":3tfnt6f6]About the variables from construct : the problem is that i cannot get values relative to the origin of the world. This would be cool to create effects such as colorShift for vegetation, to alterate wind effects, motion or so... and it only adds 1 instruction per shader. Well, maybe this will be added later.[/quote:3tfnt6f6]
I see what you mean. Yes, that would be great (and having a timer value also).

In the meantime, given your effect isn't too instruction heavy, you can get by with the following:
Add parameters for the current position of the left/top-edge of the display relative to the world. If you prefer to store them as pixel values, that's fine. You can use an always event to update the parameters, or any other method that comes to mind.
Now the calculation part. There are the variables pixelWidth and pixelHeight.
1/pixelWidth gives you the current width of the display in pixel, 1/pixelHeight gives you the height of the display in pixel.
You now have everything you need. For example: dLeft = 320, dTop= 112, dWidth = 1024, dHeight = 768
You may also add parameters for the width and height of the world.
And if you have pixel values but need relatives, just multiply horizontals by pixelWidth and verticals by pixelHeight. But make sure, the values are relative to the display, first.
Image
B
23
S
8
G
10
Posts: 1,820
Reputation: 8,242

Post » Thu Nov 25, 2010 10:53 pm

Ok so now, with some improvements, the shader works. Right now it passes only a 1dim value in the sin (hotspot) so it lacks a bit of variation... but that's fine for a side scroller.

[quote:3ayx6fvm]You may also add parameters for the width and height of the world.[/quote:3ayx6fvm]

How do you get such a parameter from construct please ? I've searched in the event sheet, but i cannot interact with the effects parameters with it. Is there a way to calculate something in the events or so and send it to the shader ? Or interact with its declared parameters in the event sheet ?

Thanks in advance.

I'll release my work to the community as soon as i've made some more stuff (desaturation, controllable blur, maybe motion blur, fake ssao and dof...).
B
72
S
15
G
4
Posts: 311
Reputation: 11,609

Post » Thu Nov 25, 2010 11:39 pm

[quote="Valerien":2xe85af2]How do you get such a parameter from construct please ? I've searched in the event sheet, but i cannot interact with the effects parameters with it. Is there a way to calculate something in the events or so and send it to the shader ? Or interact with its declared parameters in the event sheet ?[/quote:2xe85af2]
When I was talking about the world size, I didn't think of something interactive. You know the size of your world/layout, so you could just enter it in the panel. At least that's what I was thinking of.

During runtime, you would do it this way. Let's say your effect is called "myshader", and one of its parameters is called "World Width" (the display name is the one we need, not the variable name). You apply "myshader" to a sprite with the name "mysprite".
To set the "World Width" (in pseudo code):
[code:2xe85af2]+ Some Condition (e.g. Always)
-> mysprite: myshader: Set World Width to LayoutWidth[/code:2xe85af2]
Explanation: You access the shader's parameter with the wizard. After a doubleclick on the object with the shader, you will see a seperate tab for every effect applied to that object. (LayoutWidth is a system expression, in the wizard you find it under 'system', it is called "Get layout width")

You can also access the parameters of an effect that is applied to a layer. You find the appropriate action of the system object under 'Layer', it is called "Set layer effect param".

There are no getters for effect parameters, you can only set them.

Here is an example of an interaction with the parameters of an effect during runtime.
[EFFECT] Disk
Image
B
23
S
8
G
10
Posts: 1,820
Reputation: 8,242

Post » Fri Nov 26, 2010 8:54 am

Yeah, that's great ! I didn't see the separate tab as it was situated under the "sprite" tab. Thanks a lot !
B
72
S
15
G
4
Posts: 311
Reputation: 11,609

Post » Fri Nov 26, 2010 1:21 pm

Blurs are nice, but they sure can take a bite out of fps.
[url:3cydt521]http://dl.dropbox.com/u/666516/field.cap[/url:3cydt521]
What would be really nice is a gradient object that works like Tulamides Gradients shader.
IE a radial gradient.
Image Image
B
161
S
48
G
89
Posts: 7,347
Reputation: 66,249

Post » Fri Nov 26, 2010 2:39 pm

[quote="newt":1s9kosmh]What would be really nice is a gradient object that works like Tulamides Gradients shader.[/quote:1s9kosmh]
I totally agree, and it wouldn't be much of a work to enhance the current gradient object to be switchable between 'linear' and 'radial' mode... Ashley, ROJO? :wink:
Image
B
23
S
8
G
10
Posts: 1,820
Reputation: 8,242

Post » Fri Nov 26, 2010 7:44 pm

That and a variable blur on blur mask.
Image Image
B
161
S
48
G
89
Posts: 7,347
Reputation: 66,249

Next

Return to Help & Support using Construct Classic

Who is online

Users browsing this forum: No registered users and 6 guests