Water Shader

This forum is currently in read-only mode.
  • Hi,

    I have this water shader HLSL code:

    struct WaterVertexToPixel
    
    {
    
    float4 Position : POSITION;
    
    float4 ReflectionMapSamplingPos : TEXCOORD1;
    
    float2 BumpMapSamplingPos : TEXCOORD2;
    
    float4 RefractionMapSamplingPos : TEXCOORD3;
    
    float4 Position3D : TEXCOORD4;
    
    };
    
    struct WaterPixelToFrame
    
    {
    
    float4 Color : COLOR0;
    
    };
    
    WaterVertexToPixel WaterVS(float4 inPos : POSITION, float2 inTex: TEXCOORD)
    
    {
    
    WaterVertexToPixel Output = (WaterVertexToPixel)0;
    
    float4�4 preViewProjection = mul (xView, xProjection);
    
    float4�4 preWorldViewProjection = mul (xWorld, preViewProjection);
    
    float4�4 preReflectionViewProjection = mul (xReflectionView, xProjection);
    
    float4�4 preWorldReflectionViewProjection = mul (xWorld, preReflectionViewProjection);
    
    Output.Position = mul(inPos, preWorldViewProjection);
    
    Output.ReflectionMapSamplingPos = mul(inPos, preWorldReflectionViewProjection);
    
    Output.RefractionMapSamplingPos = mul(inPos, preWorldViewProjection);
    
    Output.Position3D = inPos;
    
    float4 absoluteTexCoords = float4(inTex, 0, 1);
    
    float4 rotatedTexCoords = mul(absoluteTexCoords, xWindDirection);
    
    float2 moveVector = float2(0, 1);
    
    // moving the water
    
    Output.BumpMapSamplingPos = rotatedTexCoords.xy/xWaveLength + xTime*xWindForce*moveVector.xy;
    
    return Output;
    
    }
    
    WaterPixelToFrame WaterPS(WaterVertexToPixel PSIn)
    
    {
    
    WaterPixelToFrame Output = (WaterPixelToFrame)0;
    
    float2 ProjectedTexCoords;
    
    ProjectedTexCoords.x = PSIn.ReflectionMapSamplingPos.x/PSIn.ReflectionMapSamplingPos.w/2.0f + 0.5f;
    
    ProjectedTexCoords.y = -PSIn.ReflectionMapSamplingPos.y/PSIn.ReflectionMapSamplingPos.w/2.0f + 0.5f;
    
    // sampling the bump map
    
    float4 bumpColor = tex2D(WaterBumpMapSampler, PSIn.BumpMapSamplingPos);
    
    // perturbation of the color
    
    float2 perturbation = xWaveHeight*(bumpColor.rg � 0.5f);
    
    // the final texture coordinates
    
    float2 perturbatedTexCoords = ProjectedTexCoords + perturbation;
    
    float4 reflectiveColor = tex2D(ReflectionSampler, perturbatedTexCoords);
    
    float2 ProjectedRefrTexCoords;
    
    ProjectedRefrTexCoords.x = PSIn.RefractionMapSamplingPos.x/PSIn.RefractionMapSamplingPos.w/2.0f + 0.5f;
    
    ProjectedRefrTexCoords.y = -PSIn.RefractionMapSamplingPos.y/PSIn.RefractionMapSamplingPos.w/2.0f + 0.5f;
    
    float2 perturbatedRefrTexCoords = ProjectedRefrTexCoords + perturbation;
    
    float4 refractiveColor = tex2D(RefractionSampler, perturbatedRefrTexCoords);
    
    float3 eyeVector = normalize(xCamPos � PSIn.Position3D);
    
    float3 normalVector = float3(0,0,1);
    
    /////////////////////////////////////////////////
    
    // FRESNEL TERM APPROXIMATION
    
    /////////////////////////////////////////////////
    
    float fresnelTerm = (float)0;
    
    if ( fresnelMode == 0 )
    
    {
    
    fresnelTerm = 1-dot(eyeVector, normalVector)*1.3f;
    
    } else
    
    if ( fresnelMode == 1 )
    
    {
    
    fresnelTerm = 0.02+0.97f*pow((1-dot(eyeVector, normalVector)),5);
    
    } else
    
    if ( fresnelMode == 2 )
    
    {
    
    float fangle = 1.0f+dot(eyeVector, normalVector);
    
    fangle = pow(fangle,5);
    
    // fresnelTerm = fangle*50;
    
    fresnelTerm = 1/fangle;
    
    }
    
    // fresnelTerm = (1/pow((fresnelTerm+1.0f),5))+0.2f; //
    
    //Hardness factor � user input
    
    fresnelTerm = fresnelTerm * xDrawMode;
    
    //just to be sure that the value is between 0 and 1;
    
    fresnelTerm = fresnelTerm < 0? 0 : fresnelTerm;
    
    fresnelTerm = fresnelTerm > 1? 1 : fresnelTerm;
    
    // creating the combined color
    
    float4 combinedColor = refractiveColor*(1-fresnelTerm) + reflectiveColor*(fresnelTerm);
    
    /////////////////////////////////////////////////
    
    // WATER COLORING
    
    /////////////////////////////////////////////////
    
    float4 dullColor = float4(0.1f, 0.1f, 0.2f, 1.0f);
    
    float dullBlendFactor = xdullBlendFactor;
    
    Output.Color = (dullBlendFactor*dullColor + (1-dullBlendFactor)*combinedColor);
    
    /////////////////////////////////////////////////
    
    // Specular Highlights
    
    /////////////////////////////////////////////////
    
    float4 speccolor;
    
    float3 lightSourceDir = normalize(float3(0.1f,0.6f,0.5f));
    
    float3 halfvec = normalize(eyeVector+lightSourceDir+float3(perturbation.x*specPerturb,perturbation.y*specPerturb,0));
    
    float3 temp = 0;
    
    temp.x = pow(dot(halfvec,normalVector),specPower);
    
    speccolor = float4(0.98,0.97,0.7,0.6);
    
    speccolor = speccolor*temp.x;
    
    speccolor = float4(speccolor.x*speccolor.w,speccolor.y*speccolor.w,speccolor.z*speccolor.w,0);
    
    Output.Color = Output.Color + speccolor;
    
    return Output;
    
    }
    
    technique Water
    
    {
    
    pass Pass0
    
    {
    
    VertexShader = compile vs_2_0 WaterVS();
    
    PixelShader = compile ps_2_0 WaterPS();
    
    }
    
    }[/code:2191t6d9]
    
    And I would like to use it in Construct, but can someone convert this to make it work on Construct?
    
    When I try to use it in Construct, it gives to me an error about something like a perspective projection..
    
    Help, please?
  • Construct does not support vertex shaders, so I don't know how this can be converted. Where did you find the FX file?

  • I understood.. Thanks.

    I found it on a tutorial about shaders if I remember correctly..

  • for water effects look at the reflections thread.

    http://www.scirra.com/phpBB3/viewtopic.php?f=2&t=3582&st=0&sk=t&sd=a

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Thanks.. Now I try making my own top-down water reflection effect.

  • top down shouldnt be too hard, you just need to have the sky and have a seperate character sprite of his underside on a layer below the ground, then use the erase effect over the ground layer for puddles or wtv, the result will look reflecty

    oh yah throwing in some magnify ripple effects when you step would probably look cool too.

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