-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathgame_engine_object_camera_vertex_cull.py
287 lines (227 loc) · 7.8 KB
/
game_engine_object_camera_vertex_cull.py
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
# SPDX-FileCopyrightText: Mitko Nikov
#
# SPDX-License-Identifier: GPL-3.0-or-later
import bpy
from bpy.types import AddonPreferences, PropertyGroup, Panel
from bpy.props import (StringProperty, EnumProperty, IntProperty,
FloatProperty, BoolProperty, PointerProperty)
from bpy.app.handlers import persistent
from bpy_extras.object_utils import world_to_camera_view
from mathutils import *
from math import *
bl_info = {
"name": "Camera Vertex Cull",
"author": "Mitko Nikov",
"version": (1, 0, 1),
"blender": (2, 83, 0),
"location": "Object > Camera Vertex Cull",
"description": "Hide vertices, edges and polys based on Camera Frustum.",
"doc_url": "",
"category": "Camera"
}
debug = False
hasEverEnabled = False
inWork = False # used for debouncing
# This creates a new array of vertices
# And applies the transform matrix in it
def applyTransform(obj):
global debug
mat = obj.matrix_world
vertices = obj.data.vertices
if debug:
print('------')
print(mat)
print('------')
verts = []
for vert in vertices:
verts.append(mat @ vert.co)
return verts
# Gets the camera according to the context
# Also, does some cool checks
def getCamera(context):
camera = context.scene.camera
if camera is None:
print("No scene camera")
elif camera.type == 'CAMERA':
if debug:
print("Regular scene camera")
else:
print("%s object as camera" % camera.type)
return False
if debug:
print(camera.data.view_frame())
return camera
@persistent
def update_handler(self, dummy):
context = bpy.context
camera = getCamera(context)
if camera is False:
return False
for obj in context.scene.objects:
if obj.type == 'MESH':
update_object(obj, context.scene, camera)
# This function updates the object based on context
def update_calc(self, context):
object = context.object
scene = context.scene
camera = getCamera(context)
if camera is False:
return False
if (object.type == 'MESH'):
update_object(object, scene, camera)
# This is the main update object function
def update_object(object, scene, camera):
global debug
global inWork
if inWork:
return
enabled = object.camera_cull_props.camera_cull_enabled
dist_enabled = object.camera_cull_props.distance_cull_enabled
margin = object.camera_cull_props.margin
distance = object.camera_cull_props.distance
if debug:
print("Enabled: ", enabled)
data = object.data
vgs = object.vertex_groups
if (object.type == 'CAMERA'):
print("Object is camera, cannot make a Camera Vertex Cull")
return False
vg = None
done = False
# Search for our Hide Group (Vertex Group)
for vgi in vgs:
if (vgi.name == "Hide_Group"):
vg = vgi
done = True
# Create it if it doesn't exist
if done is False:
if enabled is False:
return
vg = object.vertex_groups.new(name="Hide_Group")
# set every vertex weight to -1
for v in data.vertices:
vg.add([v.index], 1, "SUBTRACT")
if enabled:
# Apply the location, scale and rotation matrix
objAfterTransform = applyTransform(object)
# Convert to Camera View
coords_2d = [world_to_camera_view(scene, camera, coord) for coord in objAfterTransform]
# Iterate through the vertices
# The count is used to find the vertex index
count = -1
for x, y, distance_to_lens in coords_2d:
count = count + 1
if (x >= -margin and x <= 1 + margin and y >= -margin and y <= 1 + margin):
if (dist_enabled):
if (distance_to_lens <= distance):
continue
else:
continue
if debug:
print("Pixel Coords:", (x, y, distance_to_lens))
vg.add([data.vertices[count].index], 1, "ADD")
inWork = True
# Search for a Mask Modifier
alreadyHaveMask = False
for mod in object.modifiers:
if mod.type == 'MASK':
alreadyHaveMask = True
break
# Add if there's none
if not alreadyHaveMask:
bpy.ops.object.modifier_add(type='MASK')
# Set up the modifier with the vertex group
object.modifiers["Mask"].vertex_group = "Hide_Group"
object.modifiers["Mask"].invert_vertex_group = True
# Search for the handlers
toAddHandler = True
for han in bpy.app.handlers.frame_change_post:
if han.__name__ is 'update_handler':
toAddHandler = False
break
# Add the handlers if they don't exist
# There's no need of the handlers if the addon is never used
if toAddHandler:
bpy.app.handlers.frame_change_post.append(update_handler)
bpy.app.handlers.depsgraph_update_post.append(update_handler)
if debug:
print("[Camera Vertex Cull] Handlers added.")
# This is used to know to remove the handlers if it's used
global hasEverEnabled
hasEverEnabled = True
inWork = False
class CameraCullProperties(PropertyGroup):
camera_cull_enabled: BoolProperty(
name="Enable",
description="Hide vertexes based on the Camera frustum",
default=False,
update=update_calc
)
distance_cull_enabled: BoolProperty(
name="Enable Distance Cull",
description="Hide vertexes based on the Camera Distance",
default=False,
update=update_calc
)
margin: FloatProperty(
name="Margin",
description="Threshold outside the Camera frustum",
update=update_calc,
default=0.3,
precision=3,
min=0,
max=100
)
distance: FloatProperty(
name="Distance",
description="Culling Distance",
update=update_calc,
default=30,
precision=1,
min=0,
max=1000
)
class PLS_PT_CameraCullPropertiesPanel(Panel):
"""Camera Vertex Cull"""
bl_idname = "PLS_PT_camera_vertex_cull"
bl_label = "Camera Vertex Cull"
bl_space_type = 'PROPERTIES'
bl_region_type = 'WINDOW'
bl_context = 'object'
context = None
def draw(self, context):
layout = self.layout
if context.object.type == 'MESH':
settings = context.object.camera_cull_props
row = layout.row()
row.prop(settings, "camera_cull_enabled")
row.prop(settings, "distance_cull_enabled")
layout.prop(settings, "margin")
layout.prop(settings, "distance")
else:
layout.label(text="Select a mesh object")
class CameraVertexCullPreferences(AddonPreferences):
bl_idname = __name__
def draw(self, context):
layout = self.layout
layout.label(text="Vertex and Distance based culling")
classes = (
CameraCullProperties,
PLS_PT_CameraCullPropertiesPanel,
CameraVertexCullPreferences
)
def register():
for cls in classes:
bpy.utils.register_class(cls)
bpy.types.Object.camera_cull_props = PointerProperty(
type=CameraCullProperties)
def unregister():
global hasEverEnabled
for cls in classes:
bpy.utils.unregister_class(cls)
del bpy.types.Object.camera_cull_props
if hasEverEnabled:
bpy.app.handlers.frame_change_post.remove(update_handler)
bpy.app.handlers.depsgraph_update_post.remove(update_handler)
if __name__ == "__main__":
register()