Skip to content

Commit

Permalink
Rastertek Tutorial 33: Fire
Browse files Browse the repository at this point in the history
  • Loading branch information
jackinf committed Apr 12, 2014
1 parent 9186a8a commit 0b5b444
Show file tree
Hide file tree
Showing 14 changed files with 839 additions and 173 deletions.
Binary file added Engine/data/fire01.dds
Binary file not shown.
Binary file added Engine/data/noise01.dds
Binary file not shown.
91 changes: 91 additions & 0 deletions Engine/fireps.hlsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
////////////////////////////////////////////////////////////////////////////////
// Filename: fire.ps
////////////////////////////////////////////////////////////////////////////////


/////////////
// GLOBALS //
/////////////
Texture2D fireTexture : register(t0);
Texture2D noiseTexture : register(t1);
Texture2D alphaTexture : register(t2);
SamplerState SampleType;
SamplerState SampleType2;

cbuffer DistortionBuffer
{
float2 distortion1;
float2 distortion2;
float2 distortion3;
float distortionScale;
float distortionBias;
};


//////////////
// TYPEDEFS //
//////////////
struct PixelInputType
{
float4 position : SV_POSITION;
float2 tex : TEXCOORD0;
float2 texCoords1 : TEXCOORD1;
float2 texCoords2 : TEXCOORD2;
float2 texCoords3 : TEXCOORD3;
};


////////////////////////////////////////////////////////////////////////////////
// Pixel Shader
////////////////////////////////////////////////////////////////////////////////
float4 FirePixelShader(PixelInputType input) : SV_TARGET
{
float4 noise1;
float4 noise2;
float4 noise3;
float4 finalNoise;
float perturb;
float2 noiseCoords;
float4 fireColor;
float4 alphaColor;


// Sample the same noise texture using the three different texture coordinates to get three different noise scales.
noise1 = noiseTexture.Sample(SampleType, input.texCoords1);
noise2 = noiseTexture.Sample(SampleType, input.texCoords2);
noise3 = noiseTexture.Sample(SampleType, input.texCoords3);

// Move the noise from the (0, 1) range to the (-1, +1) range.
noise1 = (noise1 - 0.5f) * 2.0f;
noise2 = (noise2 - 0.5f) * 2.0f;
noise3 = (noise3 - 0.5f) * 2.0f;

// Distort the three noise x and y coordinates by the three different distortion x and y values.
noise1.xy = noise1.xy * distortion1.xy;
noise2.xy = noise2.xy * distortion2.xy;
noise3.xy = noise3.xy * distortion3.xy;

// Combine all three distorted noise results into a single noise result.
finalNoise = noise1 + noise2 + noise3;

// Perturb the input texture Y coordinates by the distortion scale and bias values.
// The perturbation gets stronger as you move up the texture which creates the flame flickering at the top effect.
perturb = ((1.0f - input.tex.y) * distortionScale) + distortionBias;

// Now create the perturbed and distorted texture sampling coordinates that will be used to sample the fire color texture.
noiseCoords.xy = (finalNoise.xy * perturb) + input.tex.xy;

// Sample the color from the fire texture using the perturbed and distorted texture sampling coordinates.
// Use the clamping sample state instead of the wrap sample state to prevent flames wrapping around.
fireColor = fireTexture.Sample(SampleType2, noiseCoords.xy);

// Sample the alpha value from the alpha texture using the perturbed and distorted texture sampling coordinates.
// This will be used for transparency of the fire.
// Use the clamping sample state instead of the wrap sample state to prevent flames wrapping around.
alphaColor = alphaTexture.Sample(SampleType2, noiseCoords.xy);

// Set the alpha blending of the fire to the perturbed and distored alpha texture value.
fireColor.a = alphaColor;

return fireColor;
}
76 changes: 76 additions & 0 deletions Engine/firevs.hlsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
////////////////////////////////////////////////////////////////////////////////
// Filename: fire.vs
////////////////////////////////////////////////////////////////////////////////


/////////////
// GLOBALS //
/////////////
cbuffer MatrixBuffer
{
matrix worldMatrix;
matrix viewMatrix;
matrix projectionMatrix;
};

cbuffer NoiseBuffer
{
float frameTime;
float3 scrollSpeeds;
float3 scales;
float padding;
};


//////////////
// TYPEDEFS //
//////////////
struct VertexInputType
{
float4 position : POSITION;
float2 tex : TEXCOORD0;
};

struct PixelInputType
{
float4 position : SV_POSITION;
float2 tex : TEXCOORD0;
float2 texCoords1 : TEXCOORD1;
float2 texCoords2 : TEXCOORD2;
float2 texCoords3 : TEXCOORD3;
};


////////////////////////////////////////////////////////////////////////////////
// Vertex Shader
////////////////////////////////////////////////////////////////////////////////
PixelInputType FireVertexShader(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 texture coordinates for the pixel shader.
output.tex = input.tex;

// Compute texture coordinates for first noise texture using the first scale and upward scrolling speed values.
output.texCoords1 = (input.tex * scales.x);
output.texCoords1.y = output.texCoords1.y + (frameTime * scrollSpeeds.x);

// Compute texture coordinates for second noise texture using the second scale and upward scrolling speed values.
output.texCoords2 = (input.tex * scales.y);
output.texCoords2.y = output.texCoords2.y + (frameTime * scrollSpeeds.y);

// Compute texture coordinates for third noise texture using the third scale and upward scrolling speed values.
output.texCoords3 = (input.tex * scales.z);
output.texCoords3.y = output.texCoords3.y + (frameTime * scrollSpeeds.z);

return output;
}
2 changes: 2 additions & 0 deletions rastertek.sln
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "data", "data", "{2927A905-F
Engine\data\bump03.dds = Engine\data\bump03.dds
Engine\data\cube.txt = Engine\data\cube.txt
Engine\data\dirt01.dds = Engine\data\dirt01.dds
Engine\data\fire01.dds = Engine\data\fire01.dds
Engine\data\floor.txt = Engine\data\floor.txt
Engine\data\font.dds = Engine\data\font.dds
Engine\data\fontdata.txt = Engine\data\fontdata.txt
Expand All @@ -68,6 +69,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "data", "data", "{2927A905-F
Engine\data\light01.dds = Engine\data\light01.dds
Engine\data\marble01.dds = Engine\data\marble01.dds
Engine\data\mousepointer.dds = Engine\data\mousepointer.dds
Engine\data\noise01.dds = Engine\data\noise01.dds
Engine\data\plane01.txt = Engine\data\plane01.txt
Engine\data\seafloor.dds = Engine\data\seafloor.dds
Engine\data\sound01.wav = Engine\data\sound01.wav
Expand Down
Loading

0 comments on commit 0b5b444

Please sign in to comment.