Ignore:
Timestamp:
Sep 3, 2012, 1:28:06 PM (8 years ago)
Author:
sam
Message:

tutorial: sync HLSL version of the water/fire effect with the GLSL one.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tutorial/03_noise.lolfx

    r1855 r1863  
    8989{
    9090    /* Dither the transition between water and fire */
    91     float test = pass_Position.z * pass_Position.w + sin(2.0 * u_Time);
     91    float test = pass_Position.z * pass_Position.w + 1.5 * sin(u_Time);
    9292    vec2 d = vec2(16.0, 9.0) * pass_Position.xy;
    9393    test += 0.5 * (length(fract(d) - 0.5) - length(fract(d + 0.5) - 0.5));
     
    137137-- HLSL.Vert --
    138138
     139float mod289(float x)
     140{
     141    return x - floor(x * (1.0 / 289.0)) * 289.0;
     142}
     143
    139144void main(float2 in_Position : POSITION,
    140           uniform float2 u_WinSize,
     145          uniform float u_Time,
    141146          out float4 out_Position : POSITION,
    142           out float2 pass_Position : TEXCOORD0)
    143 {
    144     //pass_Position = in_Position * u_WinSize;
    145     pass_Position = in_Position * float2(1280.0, 720.0);
     147          out float4 pass_Position : TEXCOORD0,
     148          out float3 water[4] : TEXCOORD1,
     149          out float3 fire[4] : TEXCOORD5)
     150{
     151    float3x3 r = float3x3(0.36, 0.48, -0.8, -0.8, 0.60, 0.0, 0.48, 0.64, 0.60);
     152    float3 p_pos = mul(r, float3(in_Position * float2(16.0, 9.0), 0.0));
     153    float3 p_time = mul(r, float3(0.0, 0.0, u_Time * 2.0));
     154
     155    /* Noise sampling points for water */
     156    water[0] = p_pos / 2.0 + p_time;
     157    water[1] = p_pos / 4.0 + p_time;
     158    water[2] = p_pos / 8.0 + p_time;
     159    water[3] = p_pos / 16.0 + p_time;
     160
     161    /* Noise sampling points for fire */
     162    p_pos = 16.0 * p_pos - mul(r, float3(0.0, mod289(u_Time) * 128.0, 0.0));
     163    fire[0] = p_pos / 2.0 + p_time * 2.0;
     164    fire[1] = p_pos / 4.0 + p_time * 1.5;
     165    fire[2] = p_pos / 8.0 + p_time;
     166    fire[3] = p_pos / 16.0 + p_time;
     167
     168    /* Pass rotated screen coordinates */
     169    pass_Position.xy = in_Position;
     170    float2x2 rot = float2x2(cos(u_Time), sin(u_Time), -sin(u_Time), cos(u_Time));
     171    pass_Position.zw = mul(rot, in_Position);
     172
    146173    out_Position = float4(in_Position, 0.0, 1.0);
    147174}
     
    161188float noise3d(float3 p)
    162189{
    163     float3x3 r = float3x3(0.36, 0.48, -0.8, -0.8, 0.60, 0.0, 0.48, 0.64, 0.60);
    164     p = mul(r, p);
    165 
    166190    float3 a = floor(p);
    167191    float3 d = p - a;
     
    185209}
    186210
    187 void main(in float2 pass_Position : TEXCOORD0,
     211void main(in float4 pass_Position : TEXCOORD0,
     212          in float3 water[4] : TEXCOORD1,
     213          in float3 fire[4] : TEXCOORD5,
    188214          uniform float u_Time,
    189215          out float4 out_FragColor : COLOR)
    190216{
    191     float2 xy = pass_Position;
    192     float z = u_Time * 2.0;
    193 
    194     float tx = pass_Position.x * cos(u_Time) - pass_Position.y * sin(u_Time);
    195     float ty = pass_Position.x * sin(u_Time) + pass_Position.y * cos(u_Time);
    196 
    197     if (tx * ty > 0.0)
    198         xy.y -= z * 200.0;
    199 
    200     float p1 = noise3d(float3(xy / 80.0, z));
    201     float p2 = noise3d(float3(xy / 40.0, z));
    202     float p3 = noise3d(float3(xy / 20.0, z));
    203     float p4 = noise3d(float3(xy / 10.0, z));
     217    /* Dither the transition between water and fire */
     218    float test = pass_Position.z * pass_Position.w + 1.5 * sin(u_Time);
     219    float2 d = float2(16.0, 9.0) * pass_Position.xy;
     220    test += 0.5 * (length(frac(d) - 0.5) - length(frac(d + 0.5) - 0.5));
     221
     222    float3 points[4];
     223    points[0] = (test > 0.0) ? fire[0] : water[0];
     224    points[1] = (test > 0.0) ? fire[1] : water[1];
     225    points[2] = (test > 0.0) ? fire[2] : water[2];
     226    points[3] = (test > 0.0) ? fire[3] : water[3];
     227
     228    /* Compute 4 octaves of noise */
     229    float4 n = float4(noise3d(points[0]),
     230                      noise3d(points[1]),
     231                      noise3d(points[2]),
     232                      noise3d(points[3]));
    204233
    205234    float4 color;
    206235
    207     if (tx * ty > 0.0)
    208     {
    209         float p = 0.5 * p1 + 0.25 * p2 + 0.125 * (p3 + p4);
    210 
    211         /* Scroll by adding [-.5,.5] */
    212         p -= pass_Position.y / 1440.0;
     236    if (test > 0.0)
     237    {
     238        /* Use noise results for fire */
     239        float p = dot(n, float4(0.125, 0.125, 0.25, 0.5));
     240
     241        /* Fade to black on top of screen */
     242        p -= pass_Position.y * 0.8 + 0.25;
    213243        p = max(p, 0.0);
    214244        p = min(p, 1.0);
     
    223253    else
    224254    {
    225         float p = 0.5 * p1 + 0.25 * p2 + 0.125 * (p3 + p4);
    226 
    227         float q = p * p * (3.0 - 2.0 * p);
    228         float r = q * q * (3.0 - 2.0 * q);
     255        /* Use noise results for water */
     256        float p = dot(abs(2.0 * n - 1.0),
     257                      float4(0.5, 0.25, 0.125, 0.125));
     258        float q = sqrt(p);
     259
    229260        color = float4(1.0 - q,
    230                        1.0 - 0.5 * r,
     261                       1.0 - 0.5 * q,
    231262                       1.0,
    232263                       1.0);
Note: See TracChangeset for help on using the changeset viewer.