Skip to content

Commit

Permalink
added uv coordinates to generated mesh
Browse files Browse the repository at this point in the history
  • Loading branch information
Miziziziz committed Jul 13, 2019
1 parent cf9460a commit da825b6
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 22 deletions.
29 changes: 17 additions & 12 deletions Spatial.tscn
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
[gd_scene load_steps=9 format=2]
[gd_scene load_steps=10 format=2]

[ext_resource path="res://projectdecal.gd" type="Script" id=1]
[ext_resource path="res://icon.png" type="Texture" id=1]
[ext_resource path="res://projectdecal.gd" type="Script" id=2]

[sub_resource type="SpatialMaterial" id=7]
albedo_color = Color( 1, 0, 0, 1 )
albedo_texture = ExtResource( 1 )

[sub_resource type="BoxShape" id=1]

Expand All @@ -22,10 +23,9 @@ points = PoolVector3Array( -0.5, -0.5, 0, -0.5, 0.5, 0, 0.5, 0.5, 0, -0.5, -0.5,
[node name="Spatial" type="Spatial"]

[node name="ImmediateGeometry" type="ImmediateGeometry" parent="."]
editor/display_folded = true
transform = Transform( 0.87322, -0.1516, -0.463146, 0, 0.950382, -0.311086, 0.487326, 0.271646, 0.829892, 6.20207, 4.00511, 0 )
material_override = SubResource( 7 )
script = ExtResource( 1 )
script = ExtResource( 2 )

[node name="Area" type="Area" parent="ImmediateGeometry"]
editor/display_folded = true
Expand All @@ -43,30 +43,35 @@ current = true

[node name="Position3D" type="Position3D" parent="ImmediateGeometry"]
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, -1.01, 5.282, 0.653 )
visible = false

[node name="MeshInstance" type="MeshInstance" parent="ImmediateGeometry"]
transform = Transform( 0.109253, 6.51926e-09, 2.38419e-06, 1.86265e-09, 0.109253, -4.05312e-06, -7.45058e-09, 1.86265e-08, 13.1517, 2.5, -2.5, 0 )
visible = false
mesh = SubResource( 2 )
material/0 = null

[node name="MeshInstance4" type="MeshInstance" parent="ImmediateGeometry"]
transform = Transform( 0.109253, 7.45058e-09, 2.86102e-06, 1.86265e-09, 0.109253, -5.00679e-06, -7.45058e-09, 2.6077e-08, 13.1517, -2.5, -2.5, 9.53674e-07 )
visible = false
mesh = SubResource( 2 )
material/0 = null

[node name="MeshInstance5" type="MeshInstance" parent="ImmediateGeometry"]
transform = Transform( 0.109253, 9.31323e-09, 2.86102e-06, 1.86265e-09, 0.109253, -5.72205e-06, -7.45058e-09, 3.1665e-08, 13.1517, -2.5, 2.5, 2.86102e-06 )
visible = false
mesh = SubResource( 2 )
material/0 = null

[node name="MeshInstance2" type="MeshInstance" parent="ImmediateGeometry"]
transform = Transform( 0.109253, 7.45058e-09, 2.86102e-06, 1.86265e-09, 0.109253, -5.00679e-06, -7.45058e-09, 2.6077e-08, 13.1517, 2.5, 2.5, 1.90735e-06 )
visible = false
mesh = SubResource( 2 )
material/0 = null

[node name="MeshInstance" type="MeshInstance" parent="."]
editor/display_folded = true
transform = Transform( 0.799742, 0, -0.600344, 0, 1, 0, 0.600344, 0, 0.799742, 3.20033, -2.50434, 7.0749 )
transform = Transform( 0.799742, 0, -0.600344, 0, 1, 0, 0.600344, 0, 0.799742, 4.11722, -1.83672, 6.23122 )
mesh = SubResource( 3 )
material/0 = null

Expand All @@ -77,7 +82,7 @@ shape = SubResource( 4 )

[node name="MeshInstance2" type="MeshInstance" parent="."]
editor/display_folded = true
transform = Transform( 0.571827, -0.661364, 0.485399, 0.504203, 0.750065, 0.427996, -0.647142, 0, 0.762369, -1.2803, 0, 4.55968 )
transform = Transform( 0.571827, -0.661364, 0.485399, 0.504203, 0.750065, 0.427996, -0.647142, 0, 0.762369, -0.363415, 0.667622, 3.716 )
mesh = SubResource( 3 )
material/0 = null

Expand All @@ -88,7 +93,7 @@ shape = SubResource( 4 )

[node name="MeshInstance4" type="MeshInstance" parent="."]
editor/display_folded = true
transform = Transform( 0.755716, 0.566421, 0.328723, -0.352748, 0.774968, -0.524398, -0.551781, 0.28034, 0.78546, 6.20663, -0.415373, 1.71833 )
transform = Transform( 0.755716, 0.566421, 0.328723, -0.352748, 0.774968, -0.524398, -0.551781, 0.28034, 0.78546, 7.12352, 0.252249, 0.874645 )
mesh = SubResource( 3 )
material/0 = null

Expand All @@ -99,7 +104,7 @@ shape = SubResource( 4 )

[node name="MeshInstance5" type="MeshInstance" parent="."]
editor/display_folded = true
transform = Transform( 0.856818, 0.0422742, -0.513882, -0.352748, 0.774968, -0.524398, 0.376074, 0.630584, 0.678919, 3.10583, -0.415373, 3.8834 )
transform = Transform( 0.856818, 0.0422742, -0.513882, -0.352748, 0.774968, -0.524398, 0.376074, 0.630584, 0.678919, 4.02272, 0.252249, 3.03971 )
mesh = SubResource( 3 )
material/0 = null

Expand All @@ -110,7 +115,7 @@ shape = SubResource( 4 )

[node name="MeshInstance8" type="MeshInstance" parent="."]
editor/display_folded = true
transform = Transform( -0.291527, 0.44151, 0.848575, -0.352748, 0.774968, -0.524398, -0.889146, -0.452209, -0.0701824, -0.225716, 3.62159, 4.55758 )
transform = Transform( -0.291527, 0.44151, 0.848575, -0.352748, 0.774968, -0.524398, -0.889146, -0.452209, -0.0701824, 0.69117, 4.28921, 3.71389 )
mesh = SubResource( 3 )
material/0 = null

Expand All @@ -121,7 +126,7 @@ shape = SubResource( 4 )

[node name="MeshInstance3" type="MeshInstance" parent="."]
editor/display_folded = true
transform = Transform( 0.670723, 0, 17.5374, 0, 17.6435, 0, -6.08769, 0, 1.93221, 0.42905, 0, 8.06414 )
transform = Transform( 0.670723, 0, 17.5374, 0, 17.6435, 0, -6.08769, 0, 1.93221, 1.34594, 0.667622, 7.22046 )
mesh = SubResource( 5 )
material/0 = null

Expand All @@ -132,7 +137,7 @@ shape = SubResource( 6 )

[node name="MeshInstance6" type="MeshInstance" parent="."]
editor/display_folded = true
transform = Transform( -12.4234, 0, 18.6734, 0, 22.5715, 0, -18.296, 0, -12.6797, -0.0594268, -1.67792, 8.39257 )
transform = Transform( -12.4234, 0, 18.6734, 0, 22.5715, 0, -18.296, 0, -12.6797, 0.857459, -1.0103, 7.54889 )
mesh = SubResource( 5 )
material/0 = null

Expand Down
18 changes: 10 additions & 8 deletions icon.png.import
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,35 @@

importer="texture"
type="StreamTexture"
path="res://.import/icon.png-487276ed1e3a0c39cad0279d744ee560.stex"
path.s3tc="res://.import/icon.png-487276ed1e3a0c39cad0279d744ee560.s3tc.stex"
path.etc2="res://.import/icon.png-487276ed1e3a0c39cad0279d744ee560.etc2.stex"
metadata={
"vram_texture": false
"imported_formats": [ "s3tc", "etc2" ],
"vram_texture": true
}

[deps]

source_file="res://icon.png"
dest_files=[ "res://.import/icon.png-487276ed1e3a0c39cad0279d744ee560.stex" ]
dest_files=[ "res://.import/icon.png-487276ed1e3a0c39cad0279d744ee560.s3tc.stex", "res://.import/icon.png-487276ed1e3a0c39cad0279d744ee560.etc2.stex" ]

[params]

compress/mode=0
compress/mode=2
compress/lossy_quality=0.7
compress/hdr_mode=0
compress/bptc_ldr=0
compress/normal_map=0
flags/repeat=0
flags/repeat=true
flags/filter=true
flags/mipmaps=false
flags/mipmaps=true
flags/anisotropic=false
flags/srgb=2
flags/srgb=1
process/fix_alpha_border=true
process/premult_alpha=false
process/HDR_as_SRGB=false
process/invert_color=false
stream=false
size_limit=0
detect_3d=true
detect_3d=false
svg/scale=1.0
17 changes: 15 additions & 2 deletions projectdecal.gd
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ extends ImmediateGeometry
onready var area = $Area

const Z_BUFFER_DIS = 0.01

const MIN_ANGLE = 0.0 #scale of 1.0 to 0.0
var frame_count = 0
func _physics_process(delta):
frame_count += 1
Expand Down Expand Up @@ -43,7 +43,7 @@ func add_surfaces(base, vertices, normals, indices):
#var normal = base.to_global(normals[indices[i]]) - base.global_transform.origin
var normal = normal_to_new_base(base, self, normals[indices[i]])
# if pointing towards us, duplicate it
if normal.dot(Vector3.FORWARD) > 0:
if normal.dot(Vector3.FORWARD) > MIN_ANGLE:
all_verts.append(point_to_new_base(base, self, vertices[indices[i]]))
all_verts.append(point_to_new_base(base, self, vertices[indices[i+1]]))
all_verts.append(point_to_new_base(base, self, vertices[indices[i+2]]))
Expand Down Expand Up @@ -91,19 +91,32 @@ func render_surfaces():
var vc = verts_in_area(vert0, vert1, vert2)
if vc == 3:
set_normal(all_norms[i])
set_uv(get_uv_from_vert(all_verts[i]))
add_vertex(all_verts[i])
set_normal(all_norms[i+1])
set_uv(get_uv_from_vert(all_verts[i+1]))
add_vertex(all_verts[i+1])
set_normal(all_norms[i+2])
set_uv(get_uv_from_vert(all_verts[i+2]))
add_vertex(all_verts[i+2])
elif vc != 0 or area_overlaps_tri(vert0, vert1, vert2):
var clipped_verts = clip_tri_to_area(PoolVector3Array([all_verts[i], all_verts[i+1], all_verts[i+2]]))
clipped_verts = double_check_clipped_tris(clipped_verts)
for v in clipped_verts:
set_normal(all_norms[i])
set_uv(get_uv_from_vert(v))
add_vertex(v)
end()

func get_uv_from_vert(vert):
var uv = Vector2()
uv.x = vert.x / to_local(area.to_global(Vector3.RIGHT)).x
uv.y = vert.y / to_local(area.to_global(Vector3.DOWN)).y
uv = (uv / 2.0) + Vector2(0.5, 0.5)
uv.x = clamp(uv.x, 0.0, 1.0)
uv.y = clamp(uv.y, 0.0, 1.0)
return uv

func verts_in_area(vert0, vert1, vert2):
var num_of_verts_in_area = 0
if area_contains_vert(vert0):
Expand Down

0 comments on commit da825b6

Please sign in to comment.