Shader problem + extracting world position and time ?

This forum is currently in read-only mode.
From the Asset Store
An educational game for Times Table. An easy to use template for developers to build larger games
  • 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 :

    <img src="http://s2.noelshack.com/uploads/images/1742912921277_sans_titre2.jpg">

    Here's the code i'm using :

    // 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 !
  • 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:

    // 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]
    
    [b]EDIT[/b]
    
    

    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 ?

    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.

  • 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.

  • 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.

    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.

  • 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.

    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...).

  • 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 ?

    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):

    + 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.
    [url=http://www.scirra.com/forum/viewtopic.php?f=29&t=5703][EFFECT] Disk[/url]
  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Yeah, that's great ! I didn't see the separate tab as it was situated under the "sprite" tab. Thanks a lot !

  • Blurs are nice, but they sure can take a bite out of fps.

    http://dl.dropbox.com/u/666516/field.cap

    What would be really nice is a gradient object that works like Tulamides Gradients shader.

    IE a radial gradient.

  • What would be really nice is a gradient object that works like Tulamides Gradients shader.

    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?

  • That and a variable blur on blur mask.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)