24 :
RenderPass(
device), _shadowRenderer(shadowRenderer), _light(light), _camera(camera),
25 _shadowCamera(shadowCamera), _graphicsDevice(
device), _face(face), _allCascadesRendering(allCascadesRendering)
28 _name =
"RenderPassShadowDirectional";
29 if (_light && _light->node()) {
30 _name +=
"-" + _light->node()->name();
32 _name +=
"-face" + std::to_string(_face);
37 if (!_graphicsDevice || !_light) {
42 if (!programLibrary) {
46 auto shadowShader = programLibrary->getShadowShader(
false);
47 auto shadowShaderDynBatch = programLibrary->getShadowShader(
true);
52 _graphicsDevice->setShader(shadowShader);
56 static auto shadowBlendState = std::make_shared<BlendState>();
57 static auto shadowDepthState = std::make_shared<DepthState>();
58 _graphicsDevice->setBlendState(shadowBlendState);
59 _graphicsDevice->setDepthState(shadowDepthState);
67 const float bias = _light->shadowBias() * -1000.0f;
68 _graphicsDevice->setDepthBias(bias, bias, 0.0f);
72 const int texSize = _light->shadowResolution();
76 const int faceCount = _light->numShadowFaces();
77 for (
int face = 0; face < faceCount; ++face) {
87 const float vpX = vpRect.
getX() *
static_cast<float>(texSize);
88 const float vpY = vpRect.
getY() *
static_cast<float>(texSize);
89 const float vpW = vpRect.
getZ() *
static_cast<float>(texSize);
90 const float vpH = vpRect.
getW() *
static_cast<float>(texSize);
92 _graphicsDevice->setViewport(vpX, vpY, vpW, vpH);
93 _graphicsDevice->setScissor(
94 static_cast<int>(vpX),
static_cast<int>(vpY),
95 static_cast<int>(vpW),
static_cast<int>(vpH));
99 * shadowCam->
node()->worldTransform().inverse();
106 for (
auto* meshInstance : renderComponent->meshInstances()) {
107 if (!meshInstance->visible()) {
114 auto vertexBuffer = meshInstance->mesh()->getVertexBuffer();
115 meshInstance->setVisibleThisFrame(
true);
116 _graphicsDevice->setVertexBuffer(vertexBuffer, 0);
118 if (meshInstance->isDynamicBatch()) {
120 if (shadowShaderDynBatch) {
121 _graphicsDevice->setShader(shadowShaderDynBatch);
123 auto* sbi = meshInstance->skinBatchInstance();
125 _graphicsDevice->setDynamicBatchPalette(sbi->paletteData(), sbi->paletteSizeBytes());
128 _graphicsDevice->draw(meshInstance->mesh()->getPrimitive(), meshInstance->mesh()->getIndexBuffer(), 1, -1,
true,
true);
130 _graphicsDevice->setShader(shadowShader);
132 const auto modelMatrix = (meshInstance->node() ? meshInstance->node()->worldTransform() :
Matrix4::identity());
133 _graphicsDevice->setTransformUniforms(viewProjection, modelMatrix);
134 _graphicsDevice->draw(meshInstance->mesh()->getPrimitive(), meshInstance->mesh()->getIndexBuffer(), 1, -1,
true,
true);