Godot Shader :在一个面片上让鼠标滑过的地方凸起

今天一个朋友问的Unity问题,我试着用 Godot的 Shader实现一下。用Unity实现的话,原理是相同的

效果展示

如果要凸起更圆润的话,增加面片的细分就可以,代码部分应该可以优化,坐标运算搞得自己头晕了

Godot Shader :在一个面片上让鼠标滑过的地方凸起_第1张图片

GDScript部分

extends Camera

const ray_length : int = 1000

func _input(event):
	if event is InputEventMouse:
		var from:Vector3 = project_ray_origin(event.position)
		var to:Vector3 = from + project_ray_normal(event.position) * ray_length
		var state:PhysicsDirectSpaceState = get_world().direct_space_state
		var result:Dictionary  = state.intersect_ray(from,to)
		if result:
			get_tree().call_group("M_LISTENER","on_Mouse_move",result.position)
extends MeshInstance

onready var size : float = get("mesh").size.x
onready var mat = get("material/0")

func _ready():
	add_to_group("M_LISTENER")

func on_Mouse_move(pos : Vector3):
	var p2 = Vector2(0,1) + Vector2(pos.y,pos.x) / size
	mat.set_shader_param("position",p2)

Shader部分

shader_type spatial;
render_mode cull_disabled;

uniform sampler2D  stamp : hint_albedo;
uniform vec2 position;
uniform float bob_scale = 1.0;
uniform vec4 color : hint_color;

varying vec2 vertex_position;

void vertex(){
     
	vertex_position = UV - position;
	VERTEX.y += texture(stamp,vertex_position).r*bob_scale;
}

void fragment(){
     
	ALBEDO = color.rgb;
	NORMALMAP = texture(stamp,vertex_position).rgb;
}

stamp贴图

Godot Shader :在一个面片上让鼠标滑过的地方凸起_第2张图片

你可能感兴趣的:(Godot笔记,#,Godot,实践,Godot,Shader,游戏开发,着色器,vertex)