【DX11地形篇】17-扰动云

参考教程

Tutorial 12: Perturbed Clouds

学习记录

  这篇文章种我们介绍另一种渲染云层得方法,使用扰动纹理(Perturb Texture)实现,本篇代码基于上一篇,无新增类。

  上一篇中我们使用了两张纹理来渲染云层,这篇中我们依然使用两张,但是一张为云层纹理,一张为扰动纹理,我们此时需要更改 SkyPlane.ps 来使用 scale , translation , brightless 来进行计算纹理坐标 。此时的 SkyPlane.vs 如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
////////////////////////////////////////////////////////////////////////////////
// Filename: skyplane.ps
////////////////////////////////////////////////////////////////////////////////

/////////////
// GLOBALS //
/////////////
Texture2D cloudTexture : register(t0);
Texture2D perturbTexture : register(t1);
SamplerState SampleType;
cbuffer SkyBuffer
{
float translation;
float scale;
float brightness;
float padding;
};

//////////////
// TYPEDEFS //
//////////////
struct PixelInputType
{
float4 position : SV_POSITION;
float2 tex : TEXCOORD0;
};

////////////////////////////////////////////////////////////////////////////////
// Pixel Shader
////////////////////////////////////////////////////////////////////////////////
float4 SkyPlanePixelShader(PixelInputType input) : SV_TARGET
{
float4 perturbValue;
float4 cloudColor;

// Translate the texture coordinate sampling location by the translation value.
input.tex.x = input.tex.x + translation;

// Sample the texture value from the perturb texture using the translated texture coordinates.
perturbValue = perturbTexture.Sample(SampleType, input.tex);

// Multiply the perturb value by the perturb scale.
perturbValue = perturbValue * scale;

// Add the texture coordinates as well as the translation value to get the perturbed texture coordinate sampling location.
perturbValue.xy = perturbValue.xy + input.tex.xy + translation;

// Now sample the color from the cloud texture using the perturbed sampling coordinates.
cloudColor = cloudTexture.Sample(SampleType, perturbValue.xy);

// Reduce the color cloud by the brightness value.
cloudColor = cloudColor * brightness;

return cloudColor;
}

  主要的内容就是在这里,SkyPlaneShaderClass 的更改仅仅是支持新的 Shader ,其声明如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
////////////////////////////////////////////////////////////////////////////////
// Filename: skyplaneshaderclass.h
////////////////////////////////////////////////////////////////////////////////
#ifndef _SKYPLANESHADERCLASS_H_
#define _SKYPLANESHADERCLASS_H_


//////////////
// INCLUDES //
//////////////
#include <d3d11.h>
#include <DirectXMath.h>
#include <d3dx11async.h>
#include <fstream>
using namespace std;
using namespace DirectX;


////////////////////////////////////////////////////////////////////////////////
// Class name: SkyPlaneShaderClass
////////////////////////////////////////////////////////////////////////////////
class SkyPlaneShaderClass
{
private:
struct MatrixBufferType
{
XMMATRIX world;
XMMATRIX view;
XMMATRIX projection;
};
struct SkyBufferType
{
float translation;
float scale;
float brightness;
float padding;
};

public:
SkyPlaneShaderClass();
SkyPlaneShaderClass(const SkyPlaneShaderClass&);
~SkyPlaneShaderClass();

bool Initialize(ID3D11Device*, HWND);
void Shutdown();
bool Render(ID3D11DeviceContext*, int, XMMATRIX, XMMATRIX, XMMATRIX, ID3D11ShaderResourceView*, ID3D11ShaderResourceView*, float, float, float);

private:
bool InitializeShader(ID3D11Device*, HWND, CHAR*, CHAR*);
void ShutdownShader();
void OutputShaderErrorMessage(ID3D10Blob*, HWND, CHAR*);

bool SetShaderParameters(ID3D11DeviceContext*, XMMATRIX, XMMATRIX, XMMATRIX, ID3D11ShaderResourceView*, ID3D11ShaderResourceView*, float, float, float);
void RenderShader(ID3D11DeviceContext*, int);

private:
ID3D11VertexShader* m_vertexShader;
ID3D11PixelShader* m_pixelShader;
ID3D11InputLayout* m_layout;
ID3D11SamplerState* m_sampleState;
ID3D11Buffer* m_matrixBuffer;
ID3D11Buffer* m_skyBuffer;
};

#endif

  在我们的 SkyPlaneClass 中,我们更改之前的多个 Translation 变量为 TranslationBrightlessScale 以及更改其对应的 Get 方法,其声明如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
////////////////////////////////////////////////////////////////////////////////
// Filename: skyplaneclass.h
////////////////////////////////////////////////////////////////////////////////
#ifndef _SKYPLANECLASS_H_
#define _SKYPLANECLASS_H_


//////////////
// INCLUDES //
//////////////
#include <d3d11.h>
#include <d3dx10math.h>


///////////////////////
// MY CLASS INCLUDES //
///////////////////////
#include "textureclass.h"
#include <DirectXMath.h>


////////////////////////////////////////////////////////////////////////////////
// Class name: SkyPlaneClass
////////////////////////////////////////////////////////////////////////////////
class SkyPlaneClass
{
private:
struct SkyPlaneType
{
float x, y, z;
float tu, tv;
};
struct VertexType
{
DirectX::XMFLOAT3 position;
DirectX::XMFLOAT2 texture;
};

public:
SkyPlaneClass();
SkyPlaneClass(const SkyPlaneClass&);
~SkyPlaneClass();

bool Initialize(ID3D11Device* , ID3D11DeviceContext*, CHAR*, CHAR*);
void Shutdown();
void Render(ID3D11DeviceContext*);
void Frame();

int GetIndexCount();
ID3D11ShaderResourceView* GetCloudTexture();
ID3D11ShaderResourceView* GetPerturbTexture();

float GetScale();
float GetBrightness();
float GetTranslation();

private:
bool InitializeSkyPlane(int, float, float, float, int);
void ShutdownSkyPlane();

bool InitializeBuffers(ID3D11Device*, int);
void ShutdownBuffers();
void RenderBuffers(ID3D11DeviceContext*);

bool LoadTextures(ID3D11Device*,ID3D11DeviceContext*, CHAR*, CHAR*);
void ReleaseTextures();

private:
SkyPlaneType* m_skyPlane;
int m_vertexCount, m_indexCount;
ID3D11Buffer *m_vertexBuffer, *m_indexBuffer;
TextureClass *m_cloudTexture, *m_perturbTexture;
float m_scale, m_brightness, m_translation;
};

#endif

  最后我们修改 ShaderManagerClassZoneClass 对应部分,最终效果如下:

1

  源代码:DX11TerrainTutorial-PerturbedClouds

0%