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
HLSL-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 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 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 |
cbuffer cbPerFrame { float3 gEyePosW; float3 gEmitPosW; float3 gEmitDirW; float gGameTime; float gTimeStep; float gAge; float4x4 gViewProj; }; cbuffer cbFixed { float2 gQuadTexC[4] = { float2(0.0f, 1.0f), float2(1.0f, 1.0f), float2(0.0f, 0.0f), float2(1.0f, 0.0f) }; }; Texture2D gTexture; Texture1D gRandomTex; SamplerState samLinear { Filter = MIN_MAG_MIP_LINEAR; AddressU = WRAP; AddressV = WRAP; }; DepthStencilState DisableDepth { DepthEnable = FALSE; DepthWriteMask = ZERO; }; DepthStencilState NoDepthWrites { DepthEnable = TRUE; DepthWriteMask = ZERO; }; BlendState AdditiveBlending { AlphaToCoverageEnable = FALSE; BlendEnable[0] = TRUE; SrcBlend = SRC_ALPHA; DestBlend = ONE; BlendOp = ADD; SrcBlendAlpha = ZERO; DestBlendAlpha = ZERO; BlendOpAlpha = ADD; RenderTargetWriteMask[0] = 0x0F; }; float3 RandUnitVec3(float offset) { float u = (gGameTime + offset); float3 v = gRandomTex.SampleLevel(samLinear, u, 0).xyz; return normalize(v); } #define PT_EMITTER 0 #define PT_FLARE 1 struct Particle { float3 InitialPosW : POSITION; float3 InitialVelW : VELOCITY; float2 SizeW : SIZE; float Age : AGE; uint Type : TYPE; }; Particle StreamOutVS(Particle vin) { return vin; } [maxvertexcount(2)] void StreamOutGS(point Particle gin[1], inout PointStream<Particle> ptStream) { gin[0].Age += gTimeStep; if (gin[0].Type == PT_EMITTER) { if (gin[0].Age > 0.005f) { float3 vRandom = RandUnitVec3(0.0f); if (gEmitDirW.x != 0.0f) vRandom.x *= gEmitDirW.x; else vRandom.x *= 0.5f; if (gEmitDirW.y != 0.0f) vRandom.y *= gEmitDirW.y; if (gEmitDirW.z != 0.0f) vRandom.z *= gEmitDirW.z; else vRandom.z *= 0.5f; Particle p; p.InitialPosW = gEmitPosW.xyz; p.InitialVelW = 1.2f * vRandom; p.SizeW = float2(1.75f, 1.75f); p.Age = 0.0f; p.Type = PT_FLARE; ptStream.Append(p); gin[0].Age = 0.0f; } ptStream.Append(gin[0]); } else { if (gin[0].Age <= 1.0f) ptStream.Append(gin[0]); } } GeometryShader gsStreamOut = ConstructGSWithSO(CompileShader(gs_5_0, StreamOutGS()), "POSITION.xyz; VELOCITY.xyz; SIZE.xy; AGE.x; TYPE.x"); technique11 StreamOutTech { pass P0 { SetVertexShader(CompileShader(vs_5_0, StreamOutVS())); SetGeometryShader(gsStreamOut); SetPixelShader(NULL); SetDepthStencilState(DisableDepth, 0); } } struct VertexOut { float3 PosW : POSITION; float2 SizeW : SIZE; float4 Color : COLOR; uint Type : TYPE; }; VertexOut DrawVS(Particle vin) { VertexOut vout; float t = vin.Age; float3 gAccelW = { gEmitDirW.x * 4.0f, gEmitDirW.y * 4.0f, gEmitDirW.z * 4.0f}; vout.PosW = 0.5f * t * t * gAccelW + t * vin.InitialVelW + vin.InitialPosW; float opacity = 1.0f - smoothstep(0.0f, 1.0f, t / 1.0f); vout.Color = float4(1.0f, 0.5f, 0.0f, opacity); vout.SizeW = vin.SizeW; vout.Type = vin.Type; return vout; } struct GeoOut { float4 PosH : SV_Position; float4 Color : COLOR; float2 Tex : TEXCOORD; }; [maxvertexcount(4)] void DrawGS(point VertexOut gin[1], inout TriangleStream<GeoOut> triStream) { if (gin[0].Type != PT_EMITTER) { float3 look = normalize(gEyePosW.xyz - gin[0].PosW); float3 right = normalize(cross(float3(0, 1, 0), look)); float3 up = cross(look, right); float halfWidth = 0.5f*gin[0].SizeW.x; float halfHeight = 0.5f*gin[0].SizeW.y; float4 v[4]; v[0] = float4(gin[0].PosW + halfWidth*right - halfHeight*up, 1.0f); v[1] = float4(gin[0].PosW + halfWidth*right + halfHeight*up, 1.0f); v[2] = float4(gin[0].PosW - halfWidth*right - halfHeight*up, 1.0f); v[3] = float4(gin[0].PosW - halfWidth*right + halfHeight*up, 1.0f); GeoOut gout; [unroll] for (int i = 0; i < 4; ++i) { gout.PosH = mul(v[i], gViewProj); gout.Tex = gQuadTexC[i]; gout.Color = gin[0].Color; triStream.Append(gout); } } } float4 DrawPS(GeoOut pin) : SV_TARGET { return gTexture.Sample(samLinear, float3(pin.Tex, 0))*pin.Color; } technique11 DrawTech { pass P0 { SetVertexShader(CompileShader(vs_5_0, DrawVS())); SetGeometryShader(CompileShader(gs_5_0, DrawGS())); SetPixelShader(CompileShader(ps_5_0, DrawPS())); SetBlendState(AdditiveBlending, float4(0.0f, 0.0f, 0.0f, 0.0f), 0xffffffff); SetDepthStencilState(NoDepthWrites, 0); } } |
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 |
void ParticleSystem::draw(const Camera& cam, ID3D11DeviceContext* dc) { D3DMATRIX VP = cam.getViewProj(); mFX->SetViewProj(VP); mFX->SetGameTime(static_cast<float>(mRandomTimer.getElapsedTime() / 1000.0f)); mFX->SetEyePosW(cam.getPosition()); mFX->SetTexArray(mTextureSRV); mFX->SetRandomTex(mRandomTexSRV); dc->IASetInputLayout(InputLayouts::Particle); dc->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_POINTLIST); UINT stride = sizeof(Vertex::Particle); UINT offset = 0; D3DX11_TECHNIQUE_DESC techDesc; for (unsigned int i = 0; i < mParticles.size(); i++) { mFX->SetEmitPosW(mParticles[i].mEmitPos); mFX->SetAge(mParticles[i].mAge); mFX->SetEmitDirW(mParticles[i].mEmitDir); if (mParticles[i].mFirstRun) dc->IASetVertexBuffers(0, 1, &mInitVB, &stride, &offset); else dc->IASetVertexBuffers(0, 1, &mDrawVB, &stride, &offset); dc->SOSetTargets(1, &mStreamOutVB, &offset); mFX->StreamOutTech->GetDesc(&techDesc); for (UINT p = 0; p < techDesc.Passes; p++) { mFX->StreamOutTech->GetPassByIndex(p)->Apply(0, dc); if (mParticles[i].mFirstRun) { dc->Draw(1, 0); mParticles[i].mFirstRun = false; } else dc->DrawAuto(); } ID3D11Buffer* bufferArray[1] = { 0 }; dc->SOSetTargets(1, bufferArray, &offset); std::swap(mDrawVB, mStreamOutVB); dc->IASetVertexBuffers(0, 1, &mDrawVB, &stride, &offset); mFX->DrawTech->GetDesc(&techDesc); for (UINT p = 0; p < techDesc.Passes; ++p) { mFX->DrawTech->GetPassByIndex(p)->Apply(0, dc); dc->DrawAuto(); } } } |
Dieser Beitrag wurde bereits 2 mal editiert, zuletzt von »FERNman« (29.03.2015, 12:33)
Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von »Sceiwen« (13.06.2015, 12:52)
Werbeanzeige