67 lines
2.5 KiB
GDScript
67 lines
2.5 KiB
GDScript
extends Node3D
|
|
|
|
@onready var player1: CharacterBody3D = $Player1
|
|
@onready var camera: Camera3D = $Camera3D
|
|
@onready var level: Node3D = $Level
|
|
@onready var world_coloring_viewport: SubViewport = $WorldColoringViewport
|
|
@onready var world_coloring_player1_texture_rect: TextureRect = $WorldColoringViewport/Player1TextureRect
|
|
|
|
var camera_offset: Vector3;
|
|
var world_coloring_material: ShaderMaterial
|
|
var world_coloring_texture: ImageTexture
|
|
var world_coloring_image: Image
|
|
|
|
const player_mask_texture: = preload("res://assets/textures/player_draw_mask.png")
|
|
var player_mask_image: ImageTexture;
|
|
|
|
|
|
# Called when the node enters the scene tree for the first time.
|
|
func _ready():
|
|
camera_offset = camera.global_position - player1.global_position
|
|
|
|
world_coloring_material = load("res://materials/WorldColoringMaterialPass.tres")
|
|
world_coloring_material.set_shader_parameter("world_color_texture", world_coloring_texture)
|
|
|
|
world_coloring_image = Image.create(1000, 1000, 0, Image.FORMAT_RGBA8);
|
|
world_coloring_image.fill(Color(1, 0, 1, 1))
|
|
world_coloring_texture = ImageTexture.create_from_image(world_coloring_image)
|
|
|
|
apply_world_coloring_recursive(level)
|
|
|
|
# Called every frame. 'delta' is the elapsed time since the previous frame.
|
|
func _process(_delta):
|
|
camera.global_position = player1.global_position + camera_offset
|
|
pass
|
|
|
|
func _physics_process(_delta):
|
|
#var player_world_color_coordinate : Vector2 = player1.global_position
|
|
var world_coloring_player1_rect : Rect2
|
|
world_coloring_player1_rect.position = player_position_to_world_coloring_position(player1.global_position)
|
|
world_coloring_player1_rect.size = Vector2(10, 10)
|
|
|
|
world_coloring_image.fill_rect(world_coloring_player1_rect, player1.color)
|
|
world_coloring_texture = ImageTexture.create_from_image(world_coloring_image)
|
|
|
|
apply_world_coloring_recursive(level)
|
|
|
|
func player_position_to_world_coloring_position(position: Vector3):
|
|
var result = Vector2 (position.x * 10 + 495, position.z * 10 + 495)
|
|
return result;
|
|
|
|
func apply_world_coloring_recursive (node):
|
|
for child in node.get_children():
|
|
var mesh_instance := child as MeshInstance3D
|
|
if mesh_instance:
|
|
assign_world_coloring_pass(mesh_instance)
|
|
|
|
apply_world_coloring_recursive(child)
|
|
|
|
func assign_world_coloring_pass(mesh_instance: MeshInstance3D) -> void:
|
|
var material = mesh_instance.get_active_material(0)
|
|
if not material:
|
|
return
|
|
|
|
var world_coloring_pass :ShaderMaterial = material.next_pass as ShaderMaterial
|
|
world_coloring_pass.set_shader_parameter("world_color_texture", world_coloring_texture)
|
|
|