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
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); } |
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); } |
Zitat von »"Phili"«
-Name des Shaders
-Sinn des Shaders
-kurze Beschreibung
-benötigte Vertex-Daten
-benötigte Shader-Constanten
-Code
-vieleicht nen Screenshot...
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(); } } //****************************************// |
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(); } } //****************************************// |
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(); } } |
C-/C++-Quelltext |
|
1 |
float4 vEye : Position <-- |
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); } |
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.
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(); } } |
Werbeanzeige