aboutsummaryrefslogtreecommitdiff
path: root/game/addons/zylann.hterrain/util/direct_mesh_instance.gd
blob: 17c89c92b3680123df55ef917c68906db5a0b41b (plain) (blame)
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
@tool

# Implementation of MeshInstance which doesn't use the scene tree

var _mesh_instance := RID()
# Need to keep a reference so that the mesh RID doesn't get freed
var _mesh : Mesh


func _init():
   var rs = RenderingServer
   _mesh_instance = rs.instance_create()
   rs.instance_set_visible(_mesh_instance, true)


func _notification(p_what: int):
   if p_what == NOTIFICATION_PREDELETE:
      if _mesh_instance != RID():
         RenderingServer.free_rid(_mesh_instance)
         _mesh_instance = RID()


func enter_world(world: World3D):
   assert(_mesh_instance != RID())
   RenderingServer.instance_set_scenario(_mesh_instance, world.get_scenario())


func exit_world():
   assert(_mesh_instance != RID())
   RenderingServer.instance_set_scenario(_mesh_instance, RID())


func set_world(world: World3D):
   if world != null:
      enter_world(world)
   else:
      exit_world()


func set_transform(world_transform: Transform3D):
   assert(_mesh_instance != RID())
   RenderingServer.instance_set_transform(_mesh_instance, world_transform)


func set_mesh(mesh: Mesh):
   assert(_mesh_instance != RID())
   RenderingServer.instance_set_base(_mesh_instance, mesh.get_rid() if mesh != null else RID())
   _mesh = mesh


func set_material(material: Material):
   assert(_mesh_instance != RID())
   RenderingServer.instance_geometry_set_material_override( \
      _mesh_instance, material.get_rid() if material != null else RID())


func set_visible(visible: bool):
   assert(_mesh_instance != RID())
   RenderingServer.instance_set_visible(_mesh_instance, visible)


func set_aabb(aabb: AABB):
   assert(_mesh_instance != RID())
   RenderingServer.instance_set_custom_aabb(_mesh_instance, aabb)