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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
|
#include "visual_server_viewport.h"
#include "visual_server_global.h"
#include "visual_server_canvas.h"
void VisualServerViewport::_draw_viewport(Viewport *p_viewport) {
/* Camera should always be BEFORE any other 3D */
#if 0
bool scenario_draw_canvas_bg=false;
int scenario_canvas_max_layer=0;
if (!p_viewport->hide_canvas && !p_viewport->disable_environment && scenario_owner.owns(p_viewport->scenario)) {
Scenario *scenario=scenario_owner.get(p_viewport->scenario);
if (scenario->environment.is_valid()) {
if (rasterizer->is_environment(scenario->environment)) {
scenario_draw_canvas_bg=rasterizer->environment_get_background(scenario->environment)==VS::ENV_BG_CANVAS;
scenario_canvas_max_layer=rasterizer->environment_get_background_param(scenario->environment,VS::ENV_BG_PARAM_CANVAS_MAX_LAYER);
}
}
}
bool can_draw_3d=!p_viewport->hide_scenario && camera_owner.owns(p_viewport->camera) && scenario_owner.owns(p_viewport->scenario);
if (scenario_draw_canvas_bg) {
rasterizer->begin_canvas_bg();
}
if (!scenario_draw_canvas_bg && can_draw_3d) {
_draw_viewport_camera(p_viewport,false);
} else if (true /*|| !p_viewport->canvas_list.empty()*/){
//clear the viewport black because of no camera? i seriously should..
if (p_viewport->render_target_clear_on_new_frame || p_viewport->render_target_clear) {
if (p_viewport->transparent_bg) {
rasterizer->clear_viewport(Color(0,0,0,0));
}
else {
Color cc=clear_color;
if (scenario_draw_canvas_bg)
cc.a=0;
rasterizer->clear_viewport(cc);
}
p_viewport->render_target_clear=false;
}
}
#endif
VSG::rasterizer->clear_render_target(Color(0.5,0.5,0.5,1.0));
if (!p_viewport->hide_canvas) {
int i=0;
Map<Viewport::CanvasKey,Viewport::CanvasData*> canvas_map;
Rect2 clip_rect(0,0,p_viewport->size.x,p_viewport->size.y);
RasterizerCanvas::Light *lights=NULL;
RasterizerCanvas::Light *lights_with_shadow=NULL;
RasterizerCanvas::Light *lights_with_mask=NULL;
Rect2 shadow_rect;
int light_count=0;
for (Map<RID,Viewport::CanvasData>::Element *E=p_viewport->canvas_map.front();E;E=E->next()) {
Matrix32 xf = p_viewport->global_transform * E->get().transform;
VisualServerCanvas::Canvas *canvas = static_cast<VisualServerCanvas::Canvas*>(E->get().canvas);
//find lights in canvas
for(Set<RasterizerCanvas::Light*>::Element *F=canvas->lights.front();F;F=F->next()) {
RasterizerCanvas::Light* cl=F->get();
if (cl->enabled && cl->texture.is_valid()) {
//not super efficient..
Size2 tsize(VSG::storage->texture_get_width(cl->texture),VSG::storage->texture_get_height(cl->texture));
tsize*=cl->scale;
Vector2 offset=tsize/2.0;
cl->rect_cache=Rect2(-offset+cl->texture_offset,tsize);
cl->xform_cache=xf * cl->xform;
if (clip_rect.intersects_transformed(cl->xform_cache,cl->rect_cache)) {
cl->filter_next_ptr=lights;
lights=cl;
cl->texture_cache=NULL;
Matrix32 scale;
scale.scale(cl->rect_cache.size);
scale.elements[2]=cl->rect_cache.pos;
cl->light_shader_xform = (cl->xform_cache * scale).affine_inverse();
cl->light_shader_pos=cl->xform_cache[2];
if (cl->shadow_buffer.is_valid()) {
cl->shadows_next_ptr=lights_with_shadow;
if (lights_with_shadow==NULL) {
shadow_rect = cl->xform_cache.xform(cl->rect_cache);
} else {
shadow_rect=shadow_rect.merge( cl->xform_cache.xform(cl->rect_cache) );
}
lights_with_shadow=cl;
cl->radius_cache=cl->rect_cache.size.length();
}
if (cl->mode==VS::CANVAS_LIGHT_MODE_MASK) {
cl->mask_next_ptr=lights_with_mask;
lights_with_mask=cl;
}
light_count++;
}
VSG::canvas_render->light_internal_update(cl->light_internal,cl);
}
}
//print_line("lights: "+itos(light_count));
canvas_map[ Viewport::CanvasKey( E->key(), E->get().layer) ]=&E->get();
}
if (lights_with_shadow) {
//update shadows if any
RasterizerCanvas::LightOccluderInstance * occluders=NULL;
//make list of occluders
for (Map<RID,Viewport::CanvasData>::Element *E=p_viewport->canvas_map.front();E;E=E->next()) {
VisualServerCanvas::Canvas *canvas = static_cast<VisualServerCanvas::Canvas*>(E->get().canvas);
Matrix32 xf = p_viewport->global_transform * E->get().transform;
for(Set<RasterizerCanvas::LightOccluderInstance*>::Element *F=canvas->occluders.front();F;F=F->next()) {
if (!F->get()->enabled)
continue;
F->get()->xform_cache = xf * F->get()->xform;
if (shadow_rect.intersects_transformed(F->get()->xform_cache,F->get()->aabb_cache)) {
F->get()->next=occluders;
occluders=F->get();
}
}
}
//update the light shadowmaps with them
RasterizerCanvas::Light *light=lights_with_shadow;
while(light) {
VSG::canvas_render->canvas_light_shadow_buffer_update(light->shadow_buffer,light->xform_cache.affine_inverse(),light->item_mask,light->radius_cache/1000.0,light->radius_cache*1.1,occluders,&light->shadow_matrix_cache);
light=light->shadows_next_ptr;
}
VSG::rasterizer->restore_render_target();
// VSG::canvas_render->reset_canvas();
}
#if 0
if (scenario_draw_canvas_bg && canvas_map.front() && canvas_map.front()->key().layer>scenario_canvas_max_layer) {
_draw_viewport_camera(p_viewport,!can_draw_3d);
scenario_draw_canvas_bg=false;
}
#endif
for (Map<Viewport::CanvasKey,Viewport::CanvasData*>::Element *E=canvas_map.front();E;E=E->next()) {
VisualServerCanvas::Canvas *canvas = static_cast<VisualServerCanvas::Canvas*>(E->get()->canvas);
// print_line("canvas "+itos(i)+" size: "+itos(I->get()->canvas->child_items.size()));
//print_line("GT "+p_viewport->global_transform+". CT: "+E->get()->transform);
Matrix32 xform = p_viewport->global_transform * E->get()->transform;
RasterizerCanvas::Light *canvas_lights=NULL;
RasterizerCanvas::Light *ptr=lights;
while(ptr) {
if (E->get()->layer>=ptr->layer_min && E->get()->layer<=ptr->layer_max) {
ptr->next_ptr=canvas_lights;
canvas_lights=ptr;
}
ptr=ptr->filter_next_ptr;
}
VSG::canvas->render_canvas( canvas,xform,canvas_lights,lights_with_mask,clip_rect );
i++;
#if 0
if (scenario_draw_canvas_bg && E->key().layer>=scenario_canvas_max_layer) {
_draw_viewport_camera(p_viewport,!can_draw_3d);
scenario_draw_canvas_bg=false;
}
#endif
}
#if 0
if (scenario_draw_canvas_bg) {
_draw_viewport_camera(p_viewport,!can_draw_3d);
scenario_draw_canvas_bg=false;
}
#endif
//VSG::canvas_render->canvas_debug_viewport_shadows(lights_with_shadow);
}
}
void VisualServerViewport::draw_viewports() {
//sort viewports
//draw viewports
for(int i=0;i<active_viewports.size();i++) {
Viewport *vp = active_viewports[i];
if (vp->update_mode==VS::VIEWPORT_UPDATE_DISABLED)
continue;
ERR_CONTINUE( !vp->render_target.is_valid() );
VSG::rasterizer->set_current_render_target(vp->render_target);
_draw_viewport(vp);
if (vp->viewport_to_screen_rect!=Rect2()) {
//copy to screen if set as such
VSG::rasterizer->set_current_render_target(RID());
VSG::rasterizer->blit_render_target_to_screen(vp->render_target,vp->viewport_to_screen_rect,vp->viewport_to_screen);
}
if (vp->update_mode==VS::VIEWPORT_UPDATE_ONCE) {
vp->update_mode=VS::VIEWPORT_UPDATE_DISABLED;
}
}
}
RID VisualServerViewport::viewport_create() {
Viewport * viewport = memnew( Viewport );
RID rid = viewport_owner.make_rid(viewport);
viewport->self=rid;
viewport->hide_scenario=false;
viewport->hide_canvas=false;
viewport->render_target=VSG::storage->render_target_create();
return rid;
}
void VisualServerViewport::viewport_set_size(RID p_viewport,int p_width,int p_height){
ERR_FAIL_COND(p_width<0 && p_height<0);
Viewport * viewport = viewport_owner.getornull(p_viewport);
ERR_FAIL_COND(!viewport);
viewport->size=Size2(p_width,p_height);
VSG::storage->render_target_set_size(viewport->render_target,p_width,p_height);
}
void VisualServerViewport::viewport_set_active(RID p_viewport,bool p_active) {
Viewport * viewport = viewport_owner.getornull(p_viewport);
ERR_FAIL_COND(!viewport);
if (p_active) {
ERR_FAIL_COND(active_viewports.find(viewport)!=-1);//already active
active_viewports.push_back(viewport);
} else {
active_viewports.erase(viewport);
}
}
void VisualServerViewport::viewport_set_clear_mode(RID p_viewport,VS::ViewportClearMode p_clear_mode) {
Viewport * viewport = viewport_owner.getornull(p_viewport);
ERR_FAIL_COND(!viewport);
viewport->clear_mode=p_clear_mode;
}
void VisualServerViewport::viewport_attach_to_screen(RID p_viewport,const Rect2& p_rect,int p_screen){
Viewport * viewport = viewport_owner.getornull(p_viewport);
ERR_FAIL_COND(!viewport);
viewport->viewport_to_screen_rect=p_rect;
viewport->viewport_to_screen=p_screen;
}
void VisualServerViewport::viewport_detach(RID p_viewport){
Viewport * viewport = viewport_owner.getornull(p_viewport);
ERR_FAIL_COND(!viewport);
viewport->viewport_to_screen_rect=Rect2();
viewport->viewport_to_screen=0;
}
void VisualServerViewport::viewport_set_update_mode(RID p_viewport,VS::ViewportUpdateMode p_mode){
Viewport * viewport = viewport_owner.getornull(p_viewport);
ERR_FAIL_COND(!viewport);
viewport->update_mode=p_mode;
}
void VisualServerViewport::viewport_set_vflip(RID p_viewport,bool p_enable){
Viewport * viewport = viewport_owner.getornull(p_viewport);
ERR_FAIL_COND(!viewport);
VSG::storage->render_target_set_flag(viewport->render_target,RasterizerStorage::RENDER_TARGET_VFLIP,p_enable);
}
RID VisualServerViewport::viewport_get_texture(RID p_viewport) const{
const Viewport * viewport = viewport_owner.getornull(p_viewport);
ERR_FAIL_COND_V(!viewport,RID());
return VSG::storage->render_target_get_texture(viewport->render_target);
}
Image VisualServerViewport::viewport_capture(RID p_viewport) const{
const Viewport * viewport = viewport_owner.getornull(p_viewport);
ERR_FAIL_COND_V(!viewport,Image());
return VSG::storage->render_target_get_image(viewport->render_target);
}
void VisualServerViewport::viewport_set_hide_scenario(RID p_viewport,bool p_hide){
Viewport * viewport = viewport_owner.getornull(p_viewport);
ERR_FAIL_COND(!viewport);
viewport->hide_scenario=p_hide;
}
void VisualServerViewport::viewport_set_hide_canvas(RID p_viewport,bool p_hide){
Viewport * viewport = viewport_owner.getornull(p_viewport);
ERR_FAIL_COND(!viewport);
viewport->hide_canvas=p_hide;
}
void VisualServerViewport::viewport_set_disable_environment(RID p_viewport,bool p_disable){
Viewport * viewport = viewport_owner.getornull(p_viewport);
ERR_FAIL_COND(!viewport);
viewport->disable_environment=p_disable;
}
void VisualServerViewport::viewport_attach_camera(RID p_viewport,RID p_camera){
Viewport * viewport = viewport_owner.getornull(p_viewport);
ERR_FAIL_COND(!viewport);
viewport->camera=p_camera;
}
void VisualServerViewport::viewport_set_scenario(RID p_viewport,RID p_scenario){
Viewport * viewport = viewport_owner.getornull(p_viewport);
ERR_FAIL_COND(!viewport);
viewport->scenario=p_scenario;
}
void VisualServerViewport::viewport_attach_canvas(RID p_viewport,RID p_canvas){
Viewport * viewport = viewport_owner.getornull(p_viewport);
ERR_FAIL_COND(!viewport);
ERR_FAIL_COND(viewport->canvas_map.has(p_canvas));
VisualServerCanvas::Canvas *canvas = VSG::canvas->canvas_owner.getornull(p_canvas);
ERR_FAIL_COND(!canvas);
canvas->viewports.insert(p_viewport);
viewport->canvas_map[p_canvas]=Viewport::CanvasData();
viewport->canvas_map[p_canvas].layer=0;
viewport->canvas_map[p_canvas].canvas=canvas;
}
void VisualServerViewport::viewport_remove_canvas(RID p_viewport,RID p_canvas){
Viewport * viewport = viewport_owner.getornull(p_viewport);
ERR_FAIL_COND(!viewport);
VisualServerCanvas::Canvas *canvas = VSG::canvas->canvas_owner.getornull(p_canvas);
ERR_FAIL_COND(!canvas);
viewport->canvas_map.erase(p_canvas);
canvas->viewports.erase(p_viewport);
}
void VisualServerViewport::viewport_set_canvas_transform(RID p_viewport,RID p_canvas,const Matrix32& p_offset){
Viewport * viewport = viewport_owner.getornull(p_viewport);
ERR_FAIL_COND(!viewport);
ERR_FAIL_COND(!viewport->canvas_map.has(p_canvas));
viewport->canvas_map[p_canvas].transform=p_offset;
}
void VisualServerViewport::viewport_set_transparent_background(RID p_viewport,bool p_enabled){
Viewport * viewport = viewport_owner.getornull(p_viewport);
ERR_FAIL_COND(!viewport);
VSG::storage->render_target_set_flag(viewport->render_target,RasterizerStorage::RENDER_TARGET_TRANSPARENT,p_enabled);
}
void VisualServerViewport::viewport_set_global_canvas_transform(RID p_viewport,const Matrix32& p_transform){
Viewport * viewport = viewport_owner.getornull(p_viewport);
ERR_FAIL_COND(!viewport);
viewport->global_transform=p_transform;
}
void VisualServerViewport::viewport_set_canvas_layer(RID p_viewport,RID p_canvas,int p_layer){
Viewport * viewport = viewport_owner.getornull(p_viewport);
ERR_FAIL_COND(!viewport);
ERR_FAIL_COND(!viewport->canvas_map.has(p_canvas));
viewport->canvas_map[p_canvas].layer=p_layer;
}
bool VisualServerViewport::free(RID p_rid) {
Viewport * viewport = viewport_owner.getornull(p_rid);
if (!viewport)
return false;
VSG::storage->free( viewport->render_target );
while(viewport->canvas_map.front()) {
viewport_remove_canvas(p_rid,viewport->canvas_map.front()->key());
}
viewport_set_scenario(p_rid,RID());
active_viewports.erase(viewport);
viewport_owner.free(p_rid);
memdelete(viewport);
return true;
}
VisualServerViewport::VisualServerViewport()
{
}
|