Welcome to mirror list, hosted at ThFree Co, Russian Federation.

overlay_mode_transfer.c « overlay « engines « draw « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 253f606b086f2ad477f2efa5ae8a2ddedcfda67e (plain)
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
/*
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * Copyright 2021, Blender Foundation.
 */

/** \file
 * \ingroup draw_engine
 */

#include "BKE_paint.h"
#include "DRW_render.h"

#include "ED_view3d.h"

#include "PIL_time.h"
#include "UI_resources.h"

#include "overlay_private.h"

void OVERLAY_mode_transfer_cache_init(OVERLAY_Data *vedata)
{
  OVERLAY_PassList *psl = vedata->psl;
  OVERLAY_PrivateData *pd = vedata->stl->pd;

  pd->mode_transfer.time = PIL_check_seconds_timer();

  for (int i = 0; i < 2; i++) {
    /* Non Meshes Pass (Camera, empties, lights ...) */
    DRWState state = DRW_STATE_WRITE_COLOR | DRW_STATE_DEPTH_EQUAL | DRW_STATE_BLEND_ALPHA;
    DRW_PASS_CREATE(psl->mode_transfer_ps[i], state | pd->clipping_state);
  }
}

#define MODE_TRANSFER_FLASH_LENGTH 0.55f
/* TODO(pablodp606): Remove this option for 3.0 if fade in/out is not used. */
#define MODE_TRANSFER_FLASH_FADE 0.0f
#define MODE_TRANSFER_FLASH_MAX_ALPHA 0.25f

static bool mode_transfer_is_animation_running(const float anim_time)
{
  return anim_time >= 0.0f && anim_time <= MODE_TRANSFER_FLASH_LENGTH;
}

static float mode_transfer_alpha_for_animation_time_get(const float anim_time)
{
  if (anim_time > MODE_TRANSFER_FLASH_LENGTH) {
    return 0.0f;
  }

  if (anim_time < 0.0f) {
    return 0.0f;
  }

  if (MODE_TRANSFER_FLASH_FADE <= 0.0f) {
    return (1.0f - (anim_time / MODE_TRANSFER_FLASH_LENGTH)) * MODE_TRANSFER_FLASH_MAX_ALPHA;
  }

  const float flash_fade_in_time = MODE_TRANSFER_FLASH_LENGTH * MODE_TRANSFER_FLASH_FADE;
  const float flash_fade_out_time = MODE_TRANSFER_FLASH_LENGTH - flash_fade_in_time;

  float alpha = 0.0f;
  if (anim_time < flash_fade_in_time) {
    alpha = anim_time / flash_fade_in_time;
  }
  else {
    const float fade_out_anim_time = anim_time - flash_fade_in_time;
    alpha = 1.0f - (fade_out_anim_time / flash_fade_out_time);
  }

  return alpha * MODE_TRANSFER_FLASH_MAX_ALPHA;
}

void OVERLAY_mode_transfer_cache_populate(OVERLAY_Data *vedata, Object *ob)
{
  OVERLAY_PrivateData *pd = vedata->stl->pd;
  OVERLAY_PassList *psl = vedata->psl;

  if (pd->xray_enabled) {
    return;
  }

  const float animation_time = pd->mode_transfer.time -
                               ob->runtime.overlay_mode_transfer_start_time;

  if (!mode_transfer_is_animation_running(animation_time)) {
    return;
  }

  const DRWContextState *draw_ctx = DRW_context_state_get();
  const bool use_sculpt_pbvh = BKE_sculptsession_use_pbvh_draw(ob, draw_ctx->v3d) &&
                               !DRW_state_is_image_render();
  const bool is_xray = (ob->dtx & OB_DRAW_IN_FRONT) != 0;

  DRWShadingGroup *mode_transfer_grp[2];

  for (int i = 0; i < 2; i++) {
    GPUShader *sh = OVERLAY_shader_uniform_color();
    mode_transfer_grp[i] = DRW_shgroup_create(sh, psl->mode_transfer_ps[i]);
    DRW_shgroup_uniform_block(mode_transfer_grp[i], "globalsBlock", G_draw.block_ubo);

    float color[4];
    UI_GetThemeColor3fv(TH_VERTEX_SELECT, color);
    color[3] = mode_transfer_alpha_for_animation_time_get(animation_time);
    srgb_to_linearrgb_v4(color, color);
    DRW_shgroup_uniform_vec4_copy(mode_transfer_grp[i], "color", color);
  }

  if (!pd->use_in_front) {
    mode_transfer_grp[IN_FRONT] = mode_transfer_grp[NOT_IN_FRONT];
  }

  pd->mode_transfer.any_animated = true;

  if (use_sculpt_pbvh) {
    DRW_shgroup_call_sculpt(mode_transfer_grp[is_xray], ob, false, false);
  }
  else {
    struct GPUBatch *geom = DRW_cache_object_surface_get(ob);
    if (geom) {
      DRW_shgroup_call(mode_transfer_grp[is_xray], geom, ob);
    }
  }
}

void OVERLAY_mode_transfer_draw(OVERLAY_Data *vedata)
{
  OVERLAY_PassList *psl = vedata->psl;

  DRW_draw_pass(psl->mode_transfer_ps[NOT_IN_FRONT]);
}

void OVERLAY_mode_transfer_infront_draw(OVERLAY_Data *vedata)
{
  OVERLAY_PassList *psl = vedata->psl;

  DRW_draw_pass(psl->mode_transfer_ps[IN_FRONT]);
}

void OVERLAY_mode_transfer_cache_finish(OVERLAY_Data *vedata)
{
  OVERLAY_PrivateData *pd = vedata->stl->pd;
  if (pd->mode_transfer.any_animated) {
    DRW_viewport_request_redraw();
  }
  pd->mode_transfer.any_animated = false;
}