-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRenderingSystem.php
More file actions
144 lines (120 loc) · 4.77 KB
/
Copy pathRenderingSystem.php
File metadata and controls
144 lines (120 loc) · 4.77 KB
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
<?php
namespace App\System;
use App\Component\SunComponent;
use App\Renderer\DistanceFogRenderer;
use App\Renderer\SkyboxRenderer;
use App\Voxel\ChunkRenderer;
use GL\Math\{GLM, Quat, Vec2, Vec3};
use VISU\ECS\EntitiesInterface;
use VISU\ECS\SystemInterface;
use VISU\Geo\Transform;
use VISU\Graphics\GLState;
use VISU\Graphics\Rendering\Pass\BackbufferData;
use VISU\Graphics\Rendering\Pass\CameraData;
use VISU\Graphics\Rendering\Pass\ClearPass;
use VISU\Graphics\Rendering\RenderContext;
use VISU\Graphics\Rendering\Renderer\FullscreenTextureRenderer;
use VISU\Graphics\ShaderCollection;
use VISU\Graphics\TextureOptions;
class RenderingSystem implements SystemInterface
{
/**
* Fullscreen Texture Debug Renderer
*/
private FullscreenTextureRenderer $fullscreenRenderer;
/**
* Voxel Rendere
*/
private ChunkRenderer $voxelRenderer;
/**
* Skybox Renderer
*/
private SkyboxRenderer $skyboxRenderer;
/**
* Fog Renderer
*/
private DistanceFogRenderer $fogRenderer;
/**
* Constructor
*/
public function __construct(
private GLState $gl,
private ShaderCollection $shaders
)
{
$this->fullscreenRenderer = new FullscreenTextureRenderer($this->gl);
$this->voxelRenderer = new ChunkRenderer($this->gl, $this->shaders);
$this->skyboxRenderer = new SkyboxRenderer($this->gl, $this->shaders);
$this->fogRenderer = new DistanceFogRenderer($this->gl, $this->shaders);
}
/**
* Registers the system, this is where you should register all required components.
*
* @return void
*/
public function register(EntitiesInterface $entities) : void
{
$entities->registerComponent(Transform::class);
$entities->registerComponent(SunComponent::class);
// create a global sun entity
$entities->setSingleton(new SunComponent);
}
/**
* Unregisters the system, this is where you can handle any cleanup.
*
* @return void
*/
public function unregister(EntitiesInterface $entities) : void
{
}
/**
* Updates handler, this is where the game state should be updated.
*
* @return void
*/
public function update(EntitiesInterface $entities) : void
{
}
/**
* Handles rendering of the scene, here you can attach additional render passes,
* modify the render pipeline or customize rendering related data.
*
* @param RenderContext $context
*/
public function render(EntitiesInterface $entities, RenderContext $context) : void
{
// retrieve the backbuffer and clear it
$backbuffer = $context->data->get(BackbufferData::class)->target;
$context->pipeline->addPass(new ClearPass($backbuffer));
// fetch the camera data
$cameraData = $context->data->get(CameraData::class);
// attach scene related data
$context->data->set($entities->getSingleton(SunComponent::class));
// create an intermediate
$sceneRenderTarget = $context->pipeline->createRenderTarget('scene', $cameraData->resolutionX, $cameraData->resolutionY);
// depth
$sceneDepth = $context->pipeline->createDepthAttachment($sceneRenderTarget);
// scene color
$sceneColorOptions = new TextureOptions;
$sceneColorOptions->internalFormat = GL_RGB;
$sceneColor = $context->pipeline->createColorAttachment($sceneRenderTarget, 'sceneColor', $sceneColorOptions);
// scene position
$spaceTextureOptions = new TextureOptions;
$spaceTextureOptions->internalFormat = GL_RGB32F;
$spaceTextureOptions->generateMipmaps = false;
$scenePosition = $context->pipeline->createColorAttachment($sceneRenderTarget, 'scenePosition', $spaceTextureOptions);
// clear the scene render target
$context->pipeline->addPass(new ClearPass($sceneRenderTarget));
// render the chunks
$this->voxelRenderer->attachPass($context->pipeline, $sceneRenderTarget, $entities);
// render the skybox
$this->skyboxRenderer->attachPass($context->pipeline, $sceneRenderTarget);
// create an intermediate for post processing
$postProcessRenderTarget = $context->pipeline->createRenderTarget('postProcess', $cameraData->resolutionX, $cameraData->resolutionY);
$postProcessColor = $context->pipeline->createColorAttachment($postProcessRenderTarget, 'postProcessColor', $sceneColorOptions);
// render the fog
$this->fogRenderer->attachPass($context->pipeline, $postProcessRenderTarget, $sceneColor, $scenePosition);
// add a pass that renders the scene render target to the backbuffer
$this->fullscreenRenderer->attachPass($context->pipeline, $backbuffer, $postProcessColor);
}
}