Skip to content

Commit

Permalink
add occluder shrink amount
Browse files Browse the repository at this point in the history
Based on @Calinou 's advice
  • Loading branch information
gongpha committed Feb 17, 2023
1 parent 4e5be27 commit 9d031ad
Show file tree
Hide file tree
Showing 12 changed files with 152 additions and 69 deletions.
46 changes: 30 additions & 16 deletions addons/qmapbsp/importer/bsp_parser.gd
Original file line number Diff line number Diff line change
Expand Up @@ -566,10 +566,17 @@ func _BuildingData() -> float :
return 1.0
return float(load_index) / entity_geo_keys.size()

const EPS := 0.0001
const UUU := 0.0 # unused
var regions : Dictionary
var region_keys : Array
var target_ent : int = -1

var occ : ArrayOccluder3D
var occ_verts : PackedVector3Array
var occ_nors : PackedVector3Array
var occ_indices : PackedInt32Array

func _build_geo() -> bool :
if loc_load_index == 0 :
# CALL ONCE
Expand All @@ -580,11 +587,24 @@ func _build_geo() -> bool :
regions = entity_geo[entity_geo_keys[load_index]]
region_keys = regions.keys()

occ = null
occ_verts = PackedVector3Array()
occ_nors = PackedVector3Array()
occ_indices = PackedInt32Array()

if global_surface_mat :
global_surface_mat.set_shader_parameter(&'texs', global_textures)
global_surface_mat.set_shader_parameter(&'lmp', lmtex)

if wim._entity_prefers_occluder(target_ent) :
occ = ArrayOccluder3D.new()

if loc_load_index == region_keys.size() :
if occ :
for i in occ_verts.size() :
occ_verts[i] -= occ_nors[i] * wim._entity_occluder_shrink_amount(target_ent)
occ.set_arrays(occ_verts, occ_indices)
wim._entity_your_occluder(target_ent, occ)
region_keys.clear()
return true

Expand All @@ -596,13 +616,6 @@ func _build_geo() -> bool :
var extents : Vector3 = rarray[3]
var mesh : ArrayMesh

var occ : ArrayOccluder3D
var occ_verts : PackedVector3Array
var occ_indices : PackedInt32Array

if wim._entity_prefers_occluder(target_ent) :
occ = ArrayOccluder3D.new()

var global_surface_tool : SurfaceTool
var surface_tool : SurfaceTool

Expand Down Expand Up @@ -653,27 +666,31 @@ func _build_geo() -> bool :
C_UV.call(uvs[i])
if !uvrs.is_empty() :
C_UV2.call(uvrs[i])
C_NM.call(nors[i])
var nor := nors[i]
C_NM.call(nor)
if is_global :
C_CT.call(0, Color(s, lstyle, lsize.x / lightmap_size.x, UUU))
C_CT.call(1, lights)
var V := verts[i] - center
C_VT.call(V)
var V := verts[i]
C_VT.call(V - center)

var occ_mat := true
var include_in_occ := true
if (
mat is StandardMaterial3D and
mat.get_transparency() != BaseMaterial3D.TRANSPARENCY_DISABLED
) :
occ_mat = false
include_in_occ = false

if occ and occ_mat :
if occ and include_in_occ :
var idx := occ_verts.find(V)
if idx == -1 :
occ_indices.append(occ_verts.size())
occ_verts.append(V)
occ_nors.append(nor)
else :
occ_indices.append(idx)
var N := occ_nors[idx] + nor
occ_nors[idx] = N.normalized()

var pos : Vector2
var offset : Vector2
Expand Down Expand Up @@ -712,9 +729,6 @@ func _build_geo() -> bool :
global_surface_tool.generate_tangents()
mesh = global_surface_tool.commit(mesh)
wim._entity_your_mesh(target_ent, loc_load_index, mesh, center, r)
if occ :
occ.set_arrays(occ_verts, occ_indices)
wim._entity_your_occluder(target_ent, loc_load_index, occ, center, r)
loc_load_index += 1
return false

Expand Down
11 changes: 2 additions & 9 deletions addons/qmapbsp/importer/impext/scene.gd
Original file line number Diff line number Diff line change
Expand Up @@ -65,21 +65,14 @@ func _entity_your_mesh(
var last_added_occin : OccluderInstance3D
func _entity_your_occluder(
ent_id : int,
brush_id : int,
occluder : ArrayOccluder3D, origin : Vector3,
region
occluder : ArrayOccluder3D,
) -> void :
var node := _get_entity_node(ent_id)
if !node : return

last_added_occin = OccluderInstance3D.new()
last_added_occin.occluder = occluder
last_added_occin.name = 'occin%d' % brush_id
last_added_occin.set_meta(&'_qmapbsp_region', region)
if ent_id == 0 :
last_added_occin.position = origin
else :
_recenter(node, origin)
last_added_occin.name = 'occin'
node.add_child(last_added_occin)
if owner : last_added_occin.owner = owner

Expand Down
5 changes: 5 additions & 0 deletions addons/qmapbsp/importer/impext/trenchbroom.gd
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,8 @@ func _get_entity_node(id : int) -> Node :

func _entity_prefers_occluder(model_id : int) -> bool :
return super(model_id) or (model_id == 0 and map_config.bake_occluders)

func _entity_occluder_shrink_amount(
ent_id : int
) -> float :
return map_config.occluder_shrink_amount
16 changes: 13 additions & 3 deletions addons/qmapbsp/importer/world_importer.gd
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,22 @@ func _entity_your_shape(

func _entity_your_occluder(
ent_id : int,
brush_id : int,
occluder : ArrayOccluder3D, origin : Vector3,
region
occluder : ArrayOccluder3D,
) -> void :
pass

func _entity_occluder_includes_region(
ent_id : int,
occluder : ArrayOccluder3D,
region
) -> bool :
return true

func _entity_occluder_shrink_amount(
ent_id : int
) -> float :
return 1.0

func _entity_prefers_bsp_geometry(model_id : int) -> bool :
return true

Expand Down
7 changes: 7 additions & 0 deletions addons/qmapbsp/trenchbroom/map_config.gd
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,17 @@ class_name QmapbspTrenchbroomMapConfig
## If [code]false[/code], the importer will omit internal lightmap loading.
## and unwraps UV2.
@export var use_bsp_lightmap : bool = false
## The texel size used for baking UV2
@export var lightmap_texel : float = 1.0
## The ratio of Quake 1 unit per Godot unit
@export var inverse_scale_factor : float = 32.0
## The threshold for splitting meshes apart.
## When using occlusion culling, A lower value is much better
@export var mesh_splitting_size : float = 32.0 # godot unit
## The material used for faces that can't load any textures into
@export var default_material : Material
## Bakes occluders from the worldspawn entity
@export var bake_occluders : bool = false
## The amount for shrinking occluders.
## This method will reduce an over-occlusion that could make an inaccurate rasterizing.
@export var occluder_shrink_amount : float = 1.0
1 change: 0 additions & 1 deletion project.godot
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,3 @@ common/physics_ticks_per_second=120

global_illumination/sdfgi/probe_ray_count=0
global_illumination/sdfgi/frames_to_converge=3
occlusion_culling/use_occlusion_culling=true
2 changes: 1 addition & 1 deletion quake1_example/class/worldspawn.gd
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ extends StaticBody3D
class_name QmapbspQuakeWorldspawn

@onready var music := $music
@onready var wenv := $wenv
@onready var wenv : WorldEnvironment = $wenv

var props : Dictionary
func _get_properties(dict : Dictionary) : props = dict
Expand Down
6 changes: 1 addition & 5 deletions quake1_example/menu.gd
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,7 @@ var MENU := {
viewer.toggle_noclip()
],
["Toggle wireframe mode", func() :
get_viewport().debug_draw = (
Viewport.DEBUG_DRAW_DISABLED
if get_viewport().debug_draw == Viewport.DEBUG_DRAW_WIREFRAME else
Viewport.DEBUG_DRAW_WIREFRAME
)
viewer.toggle_wireframe_mode()
],
["Switch rendering mode", func() :
viewer.switch_render_mode()
Expand Down
19 changes: 19 additions & 0 deletions quake1_example/viewer.gd
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ var rendering : int = 0
var skytex : ImageTexture

func _ready() :
get_viewport().use_occlusion_culling = occlusion_culling
hud.setup(self)

console.hub = hub
Expand Down Expand Up @@ -62,6 +63,8 @@ func play_by_node() :
loading.hide()
add_child(map)

_update_wireframe_mode()


for n in get_tree().get_nodes_in_group(&'entities') :
if !n.has_method(&'_map_ready') : continue
Expand Down Expand Up @@ -220,3 +223,19 @@ var region_highlighting : bool = false
func toggle_region_highlighting() -> void :
region_highlighting = !region_highlighting
world_surface.set_shader_parameter(&'regionhl', region_highlighting)
var wireframe_enabled : bool = false
func toggle_wireframe_mode() -> void :
wireframe_enabled = !wireframe_enabled
_update_wireframe_mode()

func _update_wireframe_mode() -> void :
get_viewport().debug_draw = (
Viewport.DEBUG_DRAW_WIREFRAME
if wireframe_enabled else
Viewport.DEBUG_DRAW_DISABLED
)
worldspawn.wenv.environment.background_color = (
Color.WHITE
if wireframe_enabled else
Color.BLACK
)
40 changes: 6 additions & 34 deletions quake1_example/wim.gd
Original file line number Diff line number Diff line change
Expand Up @@ -69,40 +69,13 @@ func _texture_get_material_for_integrated(
return fluid
return super(name, tex)

func _model_get_region(
model_id : int,
face_index : int, facemat : Material
) :
if model_id == 0 :
if facemat.get_meta(&'fluid', false) :
return 1 # water (or whatever)
if facemat.get_meta(&'sky', false) :
return 2
return super(model_id, face_index, facemat)

func _entity_your_mesh(
ent_id : int,
brush_id : int,
mesh : ArrayMesh, origin : Vector3,
region
) -> void :
super(ent_id, brush_id, mesh, origin, region)
if ent_id == 0 :
if region is int :
match region :
1 :
# water (or whatever)
_new_fluid_area()
# MOVE
last_added_meshin.get_parent().remove_child(last_added_meshin)
fluid_area.add_child(last_added_meshin)
last_added_meshin.position = origin
# 2 :
# # sky
# if viewer.rendering != 0 :
# last_added_meshin.queue_free()
# last_added_meshin = null
# return
if region is Vector3i :
last_added_meshin.set_instance_shader_parameter(
&'region', region
Expand Down Expand Up @@ -133,18 +106,17 @@ func _entity_your_shape(

#if ent_id == 0 and last_added_col :
# added_brush[brush_id] = last_added_col
func _entity_your_occluder(

func _entity_occluder_includes_region(
ent_id : int,
brush_id : int,
occluder : ArrayOccluder3D, origin : Vector3,
occluder : ArrayOccluder3D,
region
) -> void :
) -> bool :
if ent_id == 0 :
if region is int :
# do not add an occluder to water/sky brushes
return
super(ent_id, brush_id, occluder, origin, region)
return false
return super(ent_id, occluder, region)

func _new_entity_node(classname : String) -> Node :
var node : Node = super(classname)
Expand Down
68 changes: 68 additions & 0 deletions trenchbroom_example/maps/5_interior.map
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,69 @@
( 192 576 208 ) ( 193 576 208 ) ( 192 576 209 ) prototype/prototype1 -128 192 0 1 1
( 448 64 208 ) ( 448 64 209 ) ( 448 65 208 ) prototype/prototype1 0 192 0 1 1
}
// brush 13
{
( 0 -640 128 ) ( 0 -639 128 ) ( 0 -640 129 ) prototype/prototype1 0 0 0 1 1
( 0 -640 128 ) ( 0 -640 129 ) ( 1 -640 128 ) prototype/prototype1 0 0 0 1 1
( 0 -640 320 ) ( 1 -640 320 ) ( 0 -639 320 ) prototype/prototype1 0 0 0 1 1
( 64 -576 384 ) ( 64 -575 384 ) ( 65 -576 384 ) prototype/prototype1 0 0 0 1 1
( 64 -576 192 ) ( 65 -576 192 ) ( 64 -576 193 ) prototype/prototype1 0 0 0 1 1
( 64 -576 192 ) ( 64 -576 193 ) ( 64 -575 192 ) prototype/prototype1 0 0 0 1 1
}
// brush 14
{
( -256 -832 256 ) ( -256 -831 256 ) ( -256 -832 257 ) prototype/prototype1 0 -64 0 1 1
( -256 -832 256 ) ( -256 -832 257 ) ( -255 -832 256 ) prototype/prototype1 0 -64 0 1 1
( -256 -832 192 ) ( -255 -832 192 ) ( -256 -831 192 ) prototype/prototype1 0 0 0 1 1
( 320 -384 256 ) ( 320 -383 256 ) ( 321 -384 256 ) prototype/prototype1 0 0 0 1 1
( 320 -384 320 ) ( 321 -384 320 ) ( 320 -384 321 ) prototype/prototype1 0 -64 0 1 1
( 320 -384 320 ) ( 320 -384 321 ) ( 320 -383 320 ) prototype/prototype1 0 -64 0 1 1
}
// brush 15
{
( -256 -832 256 ) ( -256 -831 256 ) ( -256 -832 257 ) prototype/prototype1 0 -64 0 1 1
( -256 -832 256 ) ( -256 -832 257 ) ( -255 -832 256 ) prototype/prototype1 0 -64 0 1 1
( 320 -384 448 ) ( 321 -384 448 ) ( 320 -383 448 ) prototype/prototype1 0 0 0 1 1
( 320 -384 512 ) ( 320 -383 512 ) ( 321 -384 512 ) prototype/prototype1 0 0 0 1 1
( 320 -384 320 ) ( 321 -384 320 ) ( 320 -384 321 ) prototype/prototype1 0 -64 0 1 1
( 320 -384 320 ) ( 320 -384 321 ) ( 320 -383 320 ) prototype/prototype1 0 -64 0 1 1
}
// brush 16
{
( 320 -384 320 ) ( 320 -383 320 ) ( 320 -384 321 ) prototype/prototype1 0 -64 0 1 1
( -256 -832 256 ) ( -256 -832 257 ) ( -255 -832 256 ) prototype/prototype1 0 -64 0 1 1
( 320 -384 256 ) ( 321 -384 256 ) ( 320 -383 256 ) prototype/prototype1 0 0 0 1 1
( 320 -384 448 ) ( 320 -383 448 ) ( 321 -384 448 ) prototype/prototype1 0 0 0 1 1
( 320 -384 320 ) ( 321 -384 320 ) ( 320 -384 321 ) prototype/prototype1 0 -64 0 1 1
( 384 -384 320 ) ( 384 -384 321 ) ( 384 -383 320 ) prototype/prototype1 0 -64 0 1 1
}
// brush 17
{
( -320 -384 320 ) ( -320 -383 320 ) ( -320 -384 321 ) prototype/prototype1 0 -64 0 1 1
( -256 -832 256 ) ( -256 -832 257 ) ( -255 -832 256 ) prototype/prototype1 0 -64 0 1 1
( 320 -384 256 ) ( 321 -384 256 ) ( 320 -383 256 ) prototype/prototype1 0 0 0 1 1
( 320 -384 448 ) ( 320 -383 448 ) ( 321 -384 448 ) prototype/prototype1 0 0 0 1 1
( 320 -384 320 ) ( 321 -384 320 ) ( 320 -384 321 ) prototype/prototype1 0 -64 0 1 1
( -256 -384 320 ) ( -256 -384 321 ) ( -256 -383 320 ) prototype/prototype1 0 -64 0 1 1
}
// brush 18
{
( -256 -384 320 ) ( -256 -383 320 ) ( -256 -384 321 ) prototype/prototype1 0 -64 0 1 1
( 320 -384 320 ) ( 320 -384 321 ) ( 321 -384 320 ) prototype/prototype1 0 -64 0 1 1
( 320 -384 256 ) ( 321 -384 256 ) ( 320 -383 256 ) prototype/prototype1 0 0 0 1 1
( 320 -384 448 ) ( 320 -383 448 ) ( 321 -384 448 ) prototype/prototype1 0 0 0 1 1
( 320 -320 320 ) ( 321 -320 320 ) ( 320 -320 321 ) prototype/prototype1 0 -64 0 1 1
( 320 -384 320 ) ( 320 -384 321 ) ( 320 -383 320 ) prototype/prototype1 0 -64 0 1 1
}
// brush 19
{
( -256 -384 320 ) ( -256 -383 320 ) ( -256 -384 321 ) prototype/prototype1 0 -64 0 1 1
( 320 -896 320 ) ( 320 -896 321 ) ( 321 -896 320 ) prototype/prototype1 0 -64 0 1 1
( 320 -384 256 ) ( 321 -384 256 ) ( 320 -383 256 ) prototype/prototype1 0 0 0 1 1
( 320 -384 448 ) ( 320 -383 448 ) ( 321 -384 448 ) prototype/prototype1 0 0 0 1 1
( 320 -832 320 ) ( 321 -832 320 ) ( 320 -832 321 ) prototype/prototype1 0 -64 0 1 1
( 320 -384 320 ) ( 320 -384 321 ) ( 320 -383 320 ) prototype/prototype1 0 -64 0 1 1
}
}
// entity 1
{
Expand Down Expand Up @@ -152,3 +215,8 @@
"classname" "light"
"origin" "-252 -132 68"
}
// entity 7
{
"classname" "light"
"origin" "-132 -636 324"
}
Binary file added trenchbroom_example/playable/interior_test.occ
Binary file not shown.

0 comments on commit 9d031ad

Please sign in to comment.