Du bist nicht angemeldet.

Stilllegung des Forums
Das Forum wurde am 05.06.2023 nach über 20 Jahren stillgelegt (weitere Informationen und ein kleiner Rückblick).
Registrierungen, Anmeldungen und Postings sind nicht mehr möglich. Öffentliche Inhalte sind weiterhin zugänglich.
Das Team von spieleprogrammierer.de bedankt sich bei der Community für die vielen schönen Jahre.
Wenn du eine deutschsprachige Spieleentwickler-Community suchst, schau doch mal im Discord und auf ZFX vorbei!

Werbeanzeige

Phili

unregistriert

1

02.08.2006, 12:19

Vorschlag: "Interessante Shader"

Was haltet ihr davon hier ne kleine Rubrik zu machen, wo jeder interessante Shader bringen kann? Jeder der einen interessanten Shader kennt, kann ihn Posten. Folgendes sollte Vorhanden sein:
-Name des Shaders
-Sinn des Shaders
-kurze Beschreibung
-benötigte Vertex-Daten
-benötigte Shader-Constanten
-Code
-vieleicht nen Screenshot...

****************Shadowmapping**********************
Shader1:
Konstanten:
-mLightWorldViewProjection: Matrix für den Raum der Lichtquelle

Quellcode

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
float4x4        mLightWorldViewProjection;
struct VS_OUTPUT_ShadowMap
{
    float4        vPosition    : POSITION;
    float        fDepth        : TEXCOORD0;
};
VS_OUTPUT_ShadowMap VS_ShadowMap(float4 vInPos : POSITION)
{
    VS_OUTPUT_ShadowMap Out = (VS_OUTPUT_ShadowMap)(0);
    Out.vPosition    = mul(vInPos, mLightWorldViewProjection);
    Out.fDepth        = Out.vPosition.z;
    return Out;
}
float4 PS_ShadowMap(float fDepth : TEXCOORD0) : COLOR
{
    return float4(fDepth, fDepth, fDepth, 1.0f);
}


Shader2:
Konstanten:
-mLightWorldViewProjection: Die selbe Matrix wie vorhin
-mWorldViewProjection: Transformationsmatrix des Modells
-mRotation: Rotationsmatrix (für die Normalvektoren)
-vNegLightDirection: Lichtrichtung
-ShadowMap: Zuvor erzeugte LightDepthMap
-fAmbientShadow, fShadowIntensity, fShadowOffset, fSoftShadowFactor: Verschiedene Faktoren, die das Resultat des Schattens beeinflussen... Am besten durch experimentieren ausprobieren!

Quellcode

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
float4x4        mLightWorldViewProjection;
float4x4        mWorldViewProjection;
float3x3        mRotation;
float4            vNegLightDirection;
texture            ShadowMap;
float            fAmbientShadow;
float            fShadowIntensity;
float             fShadowOffset;
float            fSoftShadowFactor;
struct VS_OUTPUT_SoftShadow
{
    float4        vPosition    : POSITION;
    float2        vTex0        : TEXCOORD0;
    float2        vShadowTex    : TEXCOORD1;
    float2        vLightDepth    : TEXCOORD2;
};
VS_OUTPUT_SoftShadow VS_SoftShadow(float4 vInPos : POSITION, float3 vInNormal : NORMAL, float2 vInTex0 : TEXCOORD0)
{
    VS_OUTPUT_SoftShadow Out = (VS_OUTPUT_SoftShadow)(0);
    float4 LightWorldPos    = mul(vInPos, mLightWorldViewProjection);
    Out.vShadowTex.x        = 0.5f * LightWorldPos.x + 0.5f;
    Out.vShadowTex.y        = 0.5f - 0.5f * LightWorldPos.y;
    Out.vPosition            = mul(vInPos, mWorldViewProjection);
    Out.vTex0                = vInTex0;
    float3 vNormal             = mul(vInNormal, mRotation);
    Out.vLightDepth.x        = saturate(dot(vNormal, (float3)vNegLightDirection));
    Out.vLightDepth.y        = LightWorldPos.z - fShadowOffset;
    return Out;
}
sampler SS_Tex1 = sampler_state
{
    MINFILTER = LINEAR;
    MAGFILTER = LINEAR;
    MIPFILTER = LINEAR;
};
sampler SS_ShadowMap = sampler_state
{
    texture        = <ShadowMap>;
    MINFILTER    = LINEAR;
    MAGFILTER    = LINEAR;
    MIPFILTER    = LINEAR;
    ADDRESSU    = CLAMP;
    ADDRESSV    = CLAMP;
};
float4 PS_SoftShadow(float2 vInTex0 : TEXCOORD0, float2 vInShadowCoords : TEXCOORD1, float2 vLightDepth : TEXCOORD2) : COLOR
{
    float fTxPlus    = vInShadowCoords.x + fSoftShadowFactor;
    float fTyPlus    = vInShadowCoords.y + fSoftShadowFactor;
    float fTxMinus    = vInShadowCoords.x - fSoftShadowFactor;
    float fTyMinus    = vInShadowCoords.y - fSoftShadowFactor;
    float fTemp = 0.0f;
    fTemp  = step(0.0f, tex2D(SS_ShadowMap, float2(fTxPlus, fTyPlus)).x - vLightDepth.y);
    fTemp += step(0.0f, tex2D(SS_ShadowMap, float2(fTxMinus, fTyMinus)).x - vLightDepth.y);
    fTemp += step(0.0f, tex2D(SS_ShadowMap, float2(fTxPlus, fTyMinus)).x - vLightDepth.y);
    fTemp += step(0.0f, tex2D(SS_ShadowMap, float2(fTxMinus, fTyPlus)).x - vLightDepth.y);
    fTemp *= vLightDepth.x * fShadowIntensity;       
    fTemp += fAmbientShadow;
    return fTemp * tex2D(SS_Tex1, vInTex0);
}

Vielen Dank an Black-Panther!

Osram

Alter Hase

Beiträge: 889

Wohnort: Weissenthurm

Beruf: SW Entwickler

  • Private Nachricht senden

2

02.08.2006, 13:46

Re: [Tutorial] Interessante Shader

Zitat von »"Phili"«


-Name des Shaders
-Sinn des Shaders
-kurze Beschreibung
-benötigte Vertex-Daten
-benötigte Shader-Constanten
-Code
-vieleicht nen Screenshot...


Sprache (HLSL, GLSlang, asm etc) auch noch, viele kennen ja nur eine.

Ich finds durchaus ne gute Idee. Wie Abrexxes sagte, erst mal sehen wie viele kommen, wenn etliche kommen ists leicht den Thread sticky zu machen und wenn 100+ kommen sollten (halte ich für SEHR unwahrscheinlich) könnne wir immer noch ein neues Sub-Forum aufmachen und die da rein schieben.
"Games are algorithmic entertainment."

Black-Panther

Alter Hase

Beiträge: 1 443

Wohnort: Innsbruck

  • Private Nachricht senden

3

02.08.2006, 14:16

Gut, dann mach ich den Anfang:

Shadowmapping als Shader
Wir benötigen zwei verschiedene Shader. Einer ist dazu da, die LightDepthMap zu erstellen, und der andere zum Rendern des Objekts mit Schatten.
Um diese einsetzen zu könenn, rendert man das Objekt zwei mal. Einmal mit dem 1. und das 2. mal mit dem zweiten Shader.

Variablebeschreibungen:
mLightWorldViewProjection ist eine Matrix für den Raum der Lichtquelle. Für ein Directionallight:
mLightWorldViewProjection = mModelTransform * mLightCameraMatrix * mOrthogonaleProjectionsmatrix;

C-/C++-Quelltext

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
//Erstellen einer LightDepthMap aus der Sicht der Lichtquelle


//****************************************//


//Variablen

float4x4        mLightWorldViewProjection;

//****************************************//

//--------------------------------------------------------------------------------//

//****************************************//


//Ausgangsstruktur

struct VS_OUTPUT_ShadowMap
{
    float4      vPosition   : POSITION;
    float       fDepth      : TEXCOORD0;
};

//VertexShader

VS_OUTPUT_ShadowMap VS_ShadowMap(float4 vInPos : POSITION)
{
    VS_OUTPUT_ShadowMap Out = (VS_OUTPUT_ShadowMap)(0);
    
    //Vertexposition transformieren... in den Raum der Lichtquelle

    Out.vPosition   = mul(vInPos, mLightWorldViewProjection);
    Out.fDepth      = Out.vPosition.z;
    
    return Out;
}

//****************************************//


//****************************************//

//--------------------------------------------------------------------------------//

//****************************************//


//PixelShader

float4 PS_ShadowMap(float fDepth : TEXCOORD0) : COLOR
{
    return float4(fDepth, fDepth, fDepth, 1.0f);
}

//****************************************//


//****************************************//

//--------------------------------------------------------------------------------//

//****************************************//


TECHNIQUE ShadowMapping
{
    PASS DirectionalLight
    {
        CullMode = CW;
        
        VertexShader = compile vs_1_1 VS_ShadowMap();
        PixelShader = compile ps_1_1 PS_ShadowMap();    
    }
}

//****************************************//


Der zweite Shader: (erzeugt weiche Schatten!)

Variablenbeschreibung:
mLightWorldViewProjection: Die selbe Matrix wie vorhin
mWorldViewProjection: Transformationsmatrix des Modells
mRotation: Rotationsmatrix (für die Normalvektoren)
vNegLightDirection: Lichtrichtung
ShadowMap: Zuvor erzeugte LightDepthMap
fAmbientShadow, fShadowIntensity, fShadowOffset, fSoftShadowFactor: Verschiedene Faktoren, die das Resultat des Schattens beeinflussen... Am besten durch experimentieren ausprobieren!

C-/C++-Quelltext

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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
//Vertex und Pixelshader zum Zeichnen von Schatten und Nicht-Schatten aus einer

//LightDephtMap

//Hierbei handelt es sich um weiche Schatten


//****************************************//


//Variablen

float4x4        mLightWorldViewProjection;
float4x4        mWorldViewProjection;
float3x3        mRotation;
float4          vNegLightDirection;
texture         ShadowMap;
float           fAmbientShadow;
float           fShadowIntensity;
float           fShadowOffset;
float           fSoftShadowFactor;

//****************************************//


//Ausgangsstruktur

struct VS_OUTPUT_SoftShadow
{
    float4      vPosition   : POSITION;
    float2      vTex0       : TEXCOORD0;
    float2      vShadowTex  : TEXCOORD1;
    float2      vLightDepth : TEXCOORD2;
};

//VertexShader

VS_OUTPUT_SoftShadow VS_SoftShadow(float4 vInPos : POSITION, float3 vInNormal : NORMAL, float2 vInTex0 : TEXCOORD0)
{
    VS_OUTPUT_SoftShadow Out = (VS_OUTPUT_SoftShadow)(0);
    
    //Vertexposition transformieren... in den Raum der Lichtquelle

    float4 LightWorldPos    = mul(vInPos, mLightWorldViewProjection);
    //texturecoords berechnen

    Out.vShadowTex.x        = 0.5f * LightWorldPos.x + 0.5f;
    Out.vShadowTex.y        = 0.5f - 0.5f * LightWorldPos.y;
    
    Out.vPosition           = mul(vInPos, mWorldViewProjection);
    Out.vTex0               = vInTex0;
    
    //Hier wird der Reflextionsvektor gespeichert

    float3 vNormal          = mul(vInNormal, mRotation);
    Out.vLightDepth.x       = saturate(dot(vNormal, (float3)vNegLightDirection));
    
    //Die Tiefe des Pixels, welchen die Lichtquelle "sieht"

    Out.vLightDepth.y       = LightWorldPos.z - fShadowOffset;
    
    return Out;
}

//****************************************//


//Sampler

sampler SS_Tex1 = sampler_state
{
    MINFILTER = LINEAR;
    MAGFILTER = LINEAR;
    MIPFILTER = LINEAR;
};
sampler SS_ShadowMap = sampler_state
{
    texture     = <ShadowMap>;
    MINFILTER   = LINEAR;
    MAGFILTER   = LINEAR;
    MIPFILTER   = LINEAR;
    ADDRESSU    = CLAMP;
    ADDRESSV    = CLAMP;
};

//PixelShader

float4 PS_SoftShadow(float2 vInTex0 : TEXCOORD0, float2 vInShadowCoords : TEXCOORD1, float2 vLightDepth : TEXCOORD2) : COLOR
{
    //fTemp = 1 --> Licht || fTemp = 0 --> Schatten

    
    //fTemp für alle vier benachbarten Texel anwenden

    float fTxPlus   = vInShadowCoords.x + fSoftShadowFactor;
    float fTyPlus   = vInShadowCoords.y + fSoftShadowFactor;
    float fTxMinus  = vInShadowCoords.x - fSoftShadowFactor;
    float fTyMinus  = vInShadowCoords.y - fSoftShadowFactor;
    
    //TexCoords für die 4 Texel berechnen und Schattenwerte addieren

    float fTemp = 0.0f;
    fTemp  = step(0.0f, tex2D(SS_ShadowMap, float2(fTxPlus, fTyPlus)).x - vLightDepth.y);
    fTemp += step(0.0f, tex2D(SS_ShadowMap, float2(fTxMinus, fTyMinus)).x - vLightDepth.y);
    fTemp += step(0.0f, tex2D(SS_ShadowMap, float2(fTxPlus, fTyMinus)).x - vLightDepth.y);
    fTemp += step(0.0f, tex2D(SS_ShadowMap, float2(fTxMinus, fTyPlus)).x - vLightDepth.y);
    
    fTemp *= vLightDepth.x * fShadowIntensity;      //Wenn Schatten, ist dieser Therm gleich Null

    fTemp += fAmbientShadow;

    return fTemp * tex2D(SS_Tex1, vInTex0);
}

//****************************************//


TECHNIQUE SoftShadow
{
    PASS P1
    {
        CullMode = CW;
        
        VertexShader = compile vs_1_1 VS_SoftShadow();
        PixelShader = compile ps_2_0 PS_SoftShadow();   
    }
}

//****************************************//
stillalive studios
Drone Swarm (32.000 Dronen gleichzeitig steuern!)
facebook, twitter

TrommlBomml

Community-Fossil

Beiträge: 2 117

Wohnort: Berlin

Beruf: Software-Entwickler

  • Private Nachricht senden

4

20.03.2008, 15:33

Da will ich auch ma was beisteuern:

Name: Normalmapping + Diffuse + Specular-Lighting mit einem Punktlicht
Beschreibung: Mit Normalmapping kann man bei einem LowPoly-Mesh Details hinzufügen, indem man eine so genannte Normalmap nutzt. Diese Map wird gewöhnlicherweise aus einer Highpolyversion des betreffenden Meshs erzeugt. Der Vorteil des Normalmapping liegt darin, dass man mit weniger Polygonen viel Detail hinzufügen kann, z. B. Furchen, Risse, die durch das Pixelweise beleuchten durch die Normalmap ein Tiefeneffekt vorgetäuscht wird.
Sprache: HLSL

Benötigte Vertexdaten:
- Position
- Normale
- Tangente
- Texturkoordinate

benötigte Shaderkonstanten:
- WorldViewProjection-Matrix (Vertex Transformieren und Projezieren)
- World-Matrix (Tagent-Space transformieren)
- LichtPosition (Wo das Licht steht)
- Kameraposition (Für Specular)
- GrundTextur (Sichtbare)
- Normalmap (Speichert die Normalen)

Code:

C-/C++-Quelltext

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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
//------------------------------------------------------------------------------


float4x4 matWorldViewProj : WorldViewProjection;
float4x4         matWorld : World;
float3          vLightPos : Position;
float4 vEye               : Position;
texture             tBase : Diffuse;
texture           tNormal : Diffuse;

//------------------------------------------------------------------------------


struct SVSInput
{
    float4 vPosition : POSITION0;
    float3   vNormal : NORMAL0;
    float3  vTangent : TANGENT0;
    float2 vTexCoord : TEXCOORD0;
};

struct SVSOutput
{
    float4 vPosition : POSITION0;
    float2 vTexCoord : TEXCOORD0;
    float3 vLightDir : TEXCOORD1;
    float3 vView     : TEXCOORD2;
};

//------------------------------------------------------------------------------


SVSOutput VS_Main(SVSInput stInput)
{
    //Output-structure

    SVSOutput stOutput;

    //Position Transformieren

    stOutput.vPosition = mul(stInput.vPosition,matWorldViewProj);

    //Texturkoordinate übergeben

    stOutput.vTexCoord = stInput.vTexCoord;

    //Tangent Space erzeugen, gleich in World Transformieren

    float3x3 matTangentSpace;
    matTangentSpace[0] = mul(stInput.vTangent,matWorld);
    matTangentSpace[1] = mul(cross(stInput.vTangent,stInput.vNormal),matWorld);
    matTangentSpace[2] = mul(stInput.vNormal,matWorld);

    //Vertexpos in Welt Transformieren

    float3 vWorldPos = mul(stInput.vPosition,matWorld);
    
    //LichtRichtung berechnen und gleich in Tagentspace Transformieren

    stOutput.vLightDir = mul(matTangentSpace,vLightPos - vWorldPos);
    
    //Blickrichtung in Tagent Space

    stOutput.vView = mul(matTangentSpace,vEye - vWorldPos);

    //an PS übergeben

    return stOutput;
}

//------------------------------------------------------------------------------


//sampler erzeugen für Basemap und Normalmap

sampler samp0 = sampler_state
{
    texture = <tBase>;
    MINFILTER = LINEAR;
    MAGFILTER = LINEAR;
    MIPFILTER = LINEAR;
};
sampler samp1 = sampler_state
{
    texture = <tNormal>;
    MINFILTER = LINEAR;
    MAGFILTER = LINEAR;
    MIPFILTER = LINEAR;
};

//------------------------------------------------------------------------------


float4 PS_Main(SVSOutput stInput) : COLOR0
{
    //Normale aus Normalmap holen

    float3 vNormal = tex2D(samp1,stInput.vTexCoord)*2-1;
    
    //Diffuse-anteil berechnen: N . L

    float fDiffuse = saturate(dot(vNormal,normalize(stInput.vLightDir)));
    
    //Specularanteil berechnen: H . L

    float3 vHalf = normalize(stInput.vLightDir-stInput.vView);
    float fSpecular = pow(saturate(dot(vNormal,vHalf)),32);
    
    //Farbe zurückgeben

    return tex2D(samp0,stInput.vTexCoord) * fDiffuse + fSpecular;
}

//------------------------------------------------------------------------------


technique Normalmapping
{
    pass p0
    {
        VertexShader = compile vs_2_0 VS_Main();
        PixelShader  = compile ps_2_0 PS_Main();
    }
}


Anmerkungen:

Ich habe hier zum allgemeinen Verständnis ein paar lokale Variablen mehr genutzt, die man hätte zusammenfassen können, doch der Übersicht halber hab ichs erstmal so gelassen.

Screenshots:

Das ganze habe ich der Einfachhalt halber nur mit fxComposer getestet. Ich habe das Punktlicht bei 0,0,-100.


(Link)

5

20.03.2008, 15:46

Da hast du ein Semikolon vergessen:

C-/C++-Quelltext

1
float4 vEye               : Position <--

TrommlBomml

Community-Fossil

Beiträge: 2 117

Wohnort: Berlin

Beruf: Software-Entwickler

  • Private Nachricht senden

6

20.03.2008, 15:58

oh, dankeschön :). das is passiert weil ich da nen paar fx-composer-spezifische semantics weggenommen hab.

BlackSnake

Community-Fossil

Beiträge: 1 549

Beruf: Student

  • Private Nachricht senden

7

20.03.2008, 17:54

und für alle die keine ahnung haben, wie man tangenten und bitangenten im code selbst berechnet, es gibt da ne möglichkeit die dinger im pixelshader zu berechnen. dazu wird mindestens shader version 2.0 benötig.
es muss deshalb im pixelshader sein, weil die beiden hlsl funktionen (ddx, ddy) nur dort verfügbar sind. das ganze benötigt 14 pixelshader instructions.

die herleitung zu diesem ergebnis wird in dem buch shader x5 ausgibig diskutiert.

C-/C++-Quelltext

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
float3x3 computeTangentFrame(float3 _normal, float3 _position, float2 _texCoords)
{
    // Get edges

    float3 edgeFaceA = ddx(_position);
    float3 edgeFaceB = ddy(_position);
    float2 uvEdgeA = ddx(_texCoords);
    float2 uvEdgeB = ddy(_texCoords);

    // Calculate tangent and bitangent

    float2x3 m = float2x3(edgeFaceA, edgeFaceB);
    float3 tangent = mul(float2(uvEdgeA.x, uvEdgeB.x), m);
    float3 bitangent = mul(float2(uvEdgeA.y, uvEdgeB.y), m);

    //Build tangent frame

    float maxL = max(length(tangent), length(bitangent));
    return float3x3(tangent / maxL, bitangent / maxL, _normal);
}

dot

Supermoderator

Beiträge: 9 757

Wohnort: Graz

  • Private Nachricht senden

8

20.03.2008, 21:46

Zitat von »"BlackSnake"«

und für alle die keine ahnung haben, wie man tangenten und bitangenten im code selbst berechnet, es gibt da ne möglichkeit die dinger im pixelshader zu berechnen.


Das is aber imo eine gewaltige Verschwendung in jedem Frame 14 Pixelshaderinstruktionen zu verbrauchen nur weil man sich zu schade ist ca. 6 Zeilen code zu schreiben :p

Für 18 instruktionen bekommst du komplettes Parallax-Normalmapping inkl. Specular Highlights...

Beiträge: 774

Beruf: Student

  • Private Nachricht senden

9

28.03.2010, 22:56

Ein HLSL Shader für einfaches Postprocessing. Bloom per #define aktivierbar oder deaktivierbar.
Ansonsten kann man das Bild einfärben und die Sättigung verändern. Damit lässt sich die Stimmung in einem Spiel unglaublich beeinflussen.
Siehe Xrodon :D

Quellcode

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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#define BLOOM

// Parameter
float  fSaturation;
float3 Colorize;

const float3 grayScaleWeights = float3(0.3f, 0.59f, 0.11f);

// Bloom-Parameter
float3 cBloomLuminance;
float  fBloomBlurScale;
float2 vBloomPixelSize; // 1/Auflösung

// Szenenkopietextur
texture Scene;
sampler sampScene = sampler_state
{
   Texture = (Scene);
    AddressU = CLAMP;
    AddressV = CLAMP;
    MinFilter = POINT;
    MipFilter = POINT;
    MagFilter = POINT;
};

// Bloomhilfstexturen
texture Bloom1;
sampler sampBloom1 = sampler_state
{
   Texture = (Bloom1);
   MinFilter = LINEAR;
   MagFilter = LINEAR;
   MipFilter = LINEAR;
};
texture Bloom2;
sampler sampBloom2 = sampler_state
{
   Texture = (Bloom2);
   MinFilter = LINEAR;
   MagFilter = LINEAR;
   MipFilter = LINEAR;
};

// BlurrKernel 13fach
const float fBlurWeights[13] = 
{
    0.002216,  0.008764,  0.026995,  0.064759,  0.120985,  0.176033,
    0.199471,  0.176033,  0.120985,  0.064759,  0.026995,  0.008764,
    0.002216
};
const float PixelOffset[13] =
{ -6,-5,-4,-3,-2,-1,0,1,2,3,4,5,6 };

//----------------------------------------------------------------------------------------------------------------------------
// Allgemeiner Vertexshader
//----------------------------------------------------------------------------------------------------------------------------
struct VS_OUTPUT 
{
   float4 Pos  : POSITION;
   float2 Tex  : TEXCOORD0;
};
VS_OUTPUT VS(VS_OUTPUT In)
{return In;};

//----------------------------------------------------------------------------------------------------------------------------
// Shrink & Luminance
//----------------------------------------------------------------------------------------------------------------------------
float4 ShrinkAndLuminancePS(float2 Tex : TEXCOORD0) : COLOR
{
   float3 Color = tex2D(sampScene, Tex).rgb;
   return float4(pow(Color, cBloomLuminance), 1.0f);
};

///----------------------------------------------------------------------------------------------------------------------------
// BlurH
//----------------------------------------------------------------------------------------------------------------------------
float4 BlurHPS(float2 Tex : TEXCOORD0) : COLOR
{
   float3 Color = float3(0.0f, 0.0f, 0.0f);
   for (int i = 0; i < 13; i++)
        Color += tex2D(sampBloom1, Tex + float2(fBloomBlurScale * vBloomPixelSize.x * PixelOffset[i], 0.0f)) * fBlurWeights[i];
   return float4(Color, 1.0f);
};

//----------------------------------------------------------------------------------------------------------------------------
// BlurV
//----------------------------------------------------------------------------------------------------------------------------
float4 BlurVPS(float2 Tex : TEXCOORD0) : COLOR
{
   float3 Color = float3(0.0f, 0.0f, 0.0f);
   for (int i = 0; i < 13; i++)
        Color += tex2D(sampBloom2, Tex + float2(0.0f, fBloomBlurScale * vBloomPixelSize.y * PixelOffset[i])) * fBlurWeights[i];
   return float4(Color, 1.0f);;
};

//----------------------------------------------------------------------------------------------------------------------------
// Simple PostPro and Combine Bloomeffect with scene (optional per Preprocessor)
//----------------------------------------------------------------------------------------------------------------------------
float4 CombineBloom_SimplePostPro_PS(float2 Tex : TEXCOORD0) : COLOR
{
    float3 Color = tex2D(sampScene, Tex);
#ifdef BLOOM
    Color += tex2D(sampBloom1, Tex)*2;
#endif

    // Sättigung
    float3 scaledColor = Color * grayScaleWeights;
    float luminance = scaledColor.r + scaledColor.g + scaledColor.b;
    Color = lerp(float3(luminance, luminance, luminance), Color, fSaturation);

    // Einfärben
    Color *= Colorize;

    return float4(Color, 1.0f);
};

//----------------------------------------------------------------------------------------------------------------------------
// Technique Section for Bloom
//----------------------------------------------------------------------------------------------------------------------------
technique Bloom
{
   pass ShrinkAndLuminance
   {
    VertexShader = compile vs_2_0 VS();
    PixelShader  = compile ps_2_0 ShrinkAndLuminancePS();
   }
   pass BlurH
   {
    VertexShader = compile vs_2_0 VS();
    PixelShader  = compile ps_2_0 BlurHPS();
   }
   pass BlurV
   {
    VertexShader = compile vs_2_0 VS();
    PixelShader  = compile ps_2_0 BlurVPS();
   }
   pass Combine
   {
        VertexShader = compile vs_2_0 VS();
        PixelShader  = compile ps_2_0 CombineBloom_SimplePostPro_PS();
   }
}

TrommlBomml

Community-Fossil

Beiträge: 2 117

Wohnort: Berlin

Beruf: Software-Entwickler

  • Private Nachricht senden

10

29.03.2010, 21:09

Mach doch vielleicht noch ein vorher/nachher Screenshot dass man ein bisschen eindruck davon bekommt wie sich das ganz auswirkt :)

Werbeanzeige