Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Original Author Miziziziz
- # https://github.com/Miziziziz/2D-directional-shadows-godot
- #
- # modified by iRad (Yellatch Games)
- # draw a fake wall on one edge of the polygon
- extends Node2D
- export var wall_height = 10
- export var wall_bot_color: Color = Color(0.25,0.25,0.25)
- export var wall_top_color: Color = Color(0.5,0.5,0.5)
- onready var vertices = get_parent().polygon
- onready var num_of_vertices = vertices.size()
- var cam = null
- func _ready():
- if not cam:
- set_process(false)
- func _process(delta):
- if not cam:
- return
- update()
- func _draw():
- if not cam:
- return
- var normals = []
- for vertice_ind in range(num_of_vertices):
- var vertice = vertices[vertice_ind]
- var next_vertice = vertices[(vertice_ind + 1) % num_of_vertices]
- var normal = (next_vertice - vertice).normalized().rotated(PI / 2.0)
- normals.append(normal)
- var top_vertices = []
- for vertice_ind in range(num_of_vertices):
- var vertice = vertices[vertice_ind]
- var next_vertice = vertices[(vertice_ind + 1) % num_of_vertices]
- var normal = normals[vertice_ind]
- var vertice_proj = vertice - (cam.global_position - (global_position+vertice) )*wall_height/100
- if (cam.global_position - (global_position+vertice) ).dot(normal) > 0:
- var next_vertice_proj = next_vertice - (cam.global_position - (global_position+next_vertice) )*wall_height/100
- draw_polygon([vertice, vertice_proj, next_vertice_proj, next_vertice],
- [wall_bot_color, wall_top_color, wall_top_color, wall_bot_color],
- [Vector2(0,0),Vector2(0,1),Vector2(1,1),Vector2(1,0)],
- get_parent().texture,null,true)
- top_vertices.append(vertice_proj)
- draw_polygon(top_vertices,get_parent().vertex_colors,get_parent().uv,get_parent().texture,null,true)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement