Sign in to follow this  
ekba89

DX11 nvidia rain demo

Recommended Posts

ekba89    788
I'm trying to render rain with dx11 and hlsl and there aren't a lot of examples to create realistic rain. The best I've found so far is nvidia's rain demo from their directx 10 sdk. I've managed to convert it to directx 11 and it is working but I'm having hard time to understand the main part of their code which i added below.
My first problem is how they indexed their streak images. There are 370 rain streak images and if I'm not mistaken the maximum index value can be 368.
And my second problem is "if"s in the code. I didn't understand their purpose. And why they are using is_EpLp_angle_ccw to invert only one of the horizontal texture coordinates.
Also if there are any other rain tutorial that you can suggest I would like to check it out.

[code]void rainResponse(PSSceneIn input, float3 lightVector, float lightIntensity, float3 lightColor, float3 eyeVector, bool fallOffFactor, inout float4 rainResponseVal)
{

float opacity = 0.0;
float fallOff;
if(fallOffFactor)
{
float distToLight = length(lightVector);
fallOff = 1.0/( distToLight * distToLight);
fallOff = saturate(fallOff);
}
else
{ fallOff = 1;
}
if(fallOff > 0.01 && lightIntensity > 0.01 )
{
float3 dropDir = g_TotalVel;
#define MAX_VIDX 4
#define MAX_HIDX 8
// Inputs: lightVector, eyeVector, dropDir
float3 L = normalize(lightVector);
float3 E = normalize(eyeVector);
float3 N = normalize(dropDir);

bool is_EpLp_angle_ccw = true;
float hangle = 0;
float vangle = abs( (acos(dot(L,N)) * 180/PI) - 90 ); // 0 to 90

{
float3 Lp = normalize( L - dot(L,N)*N );
float3 Ep = normalize( E - dot(E,N)*N );
hangle = acos( dot(Ep,Lp) ) * 180/PI; // 0 to 180
hangle = (hangle-10)/20.0; // -0.5 to 8.5
is_EpLp_angle_ccw = dot( N, cross(Ep,Lp)) > 0;
}

if(vangle>=88.0)
{
hangle = 0;
is_EpLp_angle_ccw = true;
}

vangle = (vangle-10.0)/20.0; // -0.5 to 4.5

// Outputs:
// verticalLightIndex[1|2] - two indices in the vertical direction
// t - fraction at which the vangle is between these two indices (for lerp)
int verticalLightIndex1 = floor(vangle); // 0 to 5
int verticalLightIndex2 = min(MAX_VIDX, (verticalLightIndex1 + 1) );
verticalLightIndex1 = max(0, verticalLightIndex1);
float t = frac(vangle);
// textureCoordsH[1|2] used in case we need to flip the texture horizontally
float textureCoordsH1 = input.tex.x;
float textureCoordsH2 = input.tex.x;

// horizontalLightIndex[1|2] - two indices in the horizontal direction
// s - fraction at which the hangle is between these two indices (for lerp)
int horizontalLightIndex1 = 0;
int horizontalLightIndex2 = 0;
float s = 0;

s = frac(hangle);
horizontalLightIndex1 = floor(hangle); // 0 to 8
horizontalLightIndex2 = horizontalLightIndex1+1;
if( horizontalLightIndex1 < 0 )
{
horizontalLightIndex1 = 0;
horizontalLightIndex2 = 0;
}

if( is_EpLp_angle_ccw )
{
if( horizontalLightIndex2 > MAX_HIDX )
{
horizontalLightIndex2 = MAX_HIDX;
textureCoordsH2 = 1.0 - textureCoordsH2;
}
}
else
{
textureCoordsH1 = 1.0 - textureCoordsH1;
if( horizontalLightIndex2 > MAX_HIDX )
{
horizontalLightIndex2 = MAX_HIDX;
} else
{
textureCoordsH2 = 1.0 - textureCoordsH2;
}
}

if( verticalLightIndex1 >= MAX_VIDX )
{
textureCoordsH2 = input.tex.x;
horizontalLightIndex1 = 0;
horizontalLightIndex2 = 0;
s = 0;
}

// Generate the final texture coordinates for each sample
uint type = input.type;
uint2 texIndicesV1 = uint2(verticalLightIndex1*90 + horizontalLightIndex1*10 + type,
verticalLightIndex1*90 + horizontalLightIndex2*10 + type);
float3 tex1 = float3(textureCoordsH1, input.tex.y, texIndicesV1.x);
float3 tex2 = float3(textureCoordsH2, input.tex.y, texIndicesV1.y);
if( (verticalLightIndex1<4) && (verticalLightIndex2>=4) )
{
s = 0;
horizontalLightIndex1 = 0;
horizontalLightIndex2 = 0;
textureCoordsH1 = input.tex.x;
textureCoordsH2 = input.tex.x;
}

uint2 texIndicesV2 = uint2(verticalLightIndex2*90 + horizontalLightIndex1*10 + type,
verticalLightIndex2*90 + horizontalLightIndex2*10 + type);
float3 tex3 = float3(textureCoordsH1, input.tex.y, texIndicesV2.x);
float3 tex4 = float3(textureCoordsH2, input.tex.y, texIndicesV2.y);
// Sample opacity from the textures
float col1 = rainTextureArray.Sample( samAniso, tex1) * g_rainfactors[texIndicesV1.x];
float col2 = rainTextureArray.Sample( samAniso, tex2) * g_rainfactors[texIndicesV1.y];
float col3 = rainTextureArray.Sample( samAniso, tex3) * g_rainfactors[texIndicesV2.x];
float col4 = rainTextureArray.Sample( samAniso, tex4) * g_rainfactors[texIndicesV2.y];
// Compute interpolated opacity using the s and t factors
float hOpacity1 = lerp(col1,col2,s);
float hOpacity2 = lerp(col3,col4,s);
opacity = lerp(hOpacity1,hOpacity2,t);
opacity = pow(opacity,0.7); // inverse gamma correction (expand dynamic range)
opacity = 4*lightIntensity * opacity * fallOff;
}

rainResponseVal = float4(lightColor,opacity);
}[/code]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this  

  • Partner Spotlight

  • Similar Content

    • By RubenRS
      How do i open an image to use it as Texture2D information without D3DX11CreateShaderResourceViewFromFile? And how it works for different formats like (JPG, PNG, BMP, DDS,  etc.)?
      I have an (512 x 512) image with font letters, also i have the position and texcoord of every letter. The main idea is that i want to obtain the image pixel info, use the position and texcoords to create a new texture with one letter and render it. Or am I wrong in something?
    • By thmfrnk
      Hey,
      I found a very interesting blog post here: https://bartwronski.com/2017/04/13/cull-that-cone/
      However, I didn't really got how to use his "TestConeVsSphere" test in 3D (last piece of code on his post). I have the frustumCorners of a 2D Tile cell in ViewSpace and my 3D Cone Origin and Direction, so where to place the "testSphere"? I thought about to also move the Cone into viewspace and put the sphere to the Center of the Cell with the radius of half-cellsize, however what about depth? A sphere does not have inf depth?
      I am missing anything? Any Ideas?
      Thx, Thomas
    • By Modymek
      hi all
      I want to enable and disable shader in MPCH Media player Classic
      the MPCH have shader option using HLSL shaders
      I want the shader to read each file extension before it plays the file
      so if the video file name is video.GR.Mp4 it will play it in Grayscale shader 
      if it is not and standard file name Video.Mp4 without GR. unique extension so it plays standard without shader or end the shader
      here is the shader I have for grayscale
      // $MinimumShaderProfile: ps_2_0
      sampler s0 : register(s0);
      float4 main(float2 tex : TEXCOORD0) : COLOR {
          float c0 = dot(tex2D(s0, tex), float4(0.299, 0.587, 0.114, 0));
          return c0;
      }
       
      I want to add if or block stantement or bloean to detect file name before it call the shader in order to go to the procedure or disable it or goto end direct without it
       
      any thoughts or help
    • By noodleBowl
      I've gotten to part in my DirectX 11 project where I need to pass the MVP matrices to my vertex shader. And I'm a little lost when it comes to the use of the constant buffer with the vertex shader
      I understand I need to set up the constant buffer just like any other buffer:
      1. Create a buffer description with the D3D11_BIND_CONSTANT_BUFFER flag 2. Map my matrix data into the constant buffer 3. Use VSSetConstantBuffers to actually use the buffer But I get lost at the VertexShader part, how does my vertex shader know to use this constant buffer when we get to the shader side of things
      In the example I'm following I see they have this as their vertex shader, but I don't understand how the shader knows to use the MatrixBuffer cbuffer. They just use the members directly. What if there was multiple cbuffer declarations like the Microsoft documentation says you could have?
      //Inside vertex shader cbuffer MatrixBuffer { matrix worldMatrix; matrix viewMatrix; matrix projectionMatrix; }; struct VertexInputType { float4 position : POSITION; float4 color : COLOR; }; struct PixelInputType { float4 position : SV_POSITION; float4 color : COLOR; }; PixelInputType ColorVertexShader(VertexInputType input) { PixelInputType output; // Change the position vector to be 4 units for proper matrix calculations. input.position.w = 1.0f; // Calculate the position of the vertex against the world, view, and projection matrices. output.position = mul(input.position, worldMatrix); output.position = mul(output.position, viewMatrix); output.position = mul(output.position, projectionMatrix); // Store the input color for the pixel shader to use. output.color = input.color; return output; }  
    • By gomidas
      I am trying to add normal map to my project I have an example of a cube: 
      I have normal in my shader I think. Then I set shader resource view for texture (NOT BUMP)
                  device.ImmediateContext.PixelShader.SetShaderResource(0, textureView);             device.ImmediateContext.Draw(VerticesCount,0); What should I do to set my normal map or how it is done in dx11 generally example c++?
  • Popular Now