Skip to content

Commit

Permalink
Merge pull request #226 from TwistedTwigleg/3D_IK_Demo
Browse files Browse the repository at this point in the history
3D IK demo (fixed)
  • Loading branch information
NathanLovato authored Mar 31, 2018
2 parents d5d7382 + 14b6f35 commit fc08eda
Show file tree
Hide file tree
Showing 37 changed files with 6,814 additions and 0 deletions.
Binary file added 3d/ik/GunMaterial.material
Binary file not shown.
597 changes: 597 additions & 0 deletions 3d/ik/addons/sade/IK_FABRIK.gd

Large diffs are not rendered by default.

Binary file added 3d/ik/addons/sade/editor_gizmo_texture.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
33 changes: 33 additions & 0 deletions 3d/ik/addons/sade/editor_gizmo_texture.png.import
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
[remap]

importer="texture"
type="StreamTexture"
path.s3tc="res://.import/editor_gizmo_texture.png-be14d96c2d7829c8511766ceb15d5a7f.s3tc.stex"
path.etc2="res://.import/editor_gizmo_texture.png-be14d96c2d7829c8511766ceb15d5a7f.etc2.stex"

[deps]

source_file="res://addons/sade/editor_gizmo_texture.png"
source_md5="14289d2a3712e442d3d3adf307a54241"

dest_files=[ "res://.import/editor_gizmo_texture.png-be14d96c2d7829c8511766ceb15d5a7f.s3tc.stex", "res://.import/editor_gizmo_texture.png-be14d96c2d7829c8511766ceb15d5a7f.etc2.stex" ]
dest_md5="3279a3a982c66d6f404c81517b218531"

[params]

compress/mode=2
compress/lossy_quality=0.7
compress/hdr_mode=0
compress/normal_map=0
flags/repeat=true
flags/filter=false
flags/mipmaps=true
flags/anisotropic=false
flags/srgb=1
process/fix_alpha_border=true
process/premult_alpha=false
process/HDR_as_SRGB=false
stream=false
size_limit=0
detect_3d=false
svg/scale=1.0
Binary file added 3d/ik/addons/sade/ik_fabrik.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
32 changes: 32 additions & 0 deletions 3d/ik/addons/sade/ik_fabrik.png.import
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
[remap]

importer="texture"
type="StreamTexture"
path="res://.import/ik_fabrik.png-c3d637ec075c87710a4a8abbfbd526e1.stex"

[deps]

source_file="res://addons/SADE/ik_fabrik.png"
source_md5="2909090602f64d38ce0bb7314ec7b39d"

dest_files=[ "res://.import/ik_fabrik.png-c3d637ec075c87710a4a8abbfbd526e1.stex" ]
dest_md5="80bbb72f55f6ea1f119b08dc61b9526e"

[params]

compress/mode=0
compress/lossy_quality=0.7
compress/hdr_mode=0
compress/normal_map=0
flags/repeat=0
flags/filter=true
flags/mipmaps=false
flags/anisotropic=false
flags/srgb=2
process/fix_alpha_border=true
process/premult_alpha=false
process/HDR_as_SRGB=false
stream=false
size_limit=0
detect_3d=true
svg/scale=1.0
205 changes: 205 additions & 0 deletions 3d/ik/addons/sade/ik_look_at.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,205 @@
tool
extends Spatial

export (NodePath) var skeleton_path setget _set_skeleton_path
export (String) var bone_name = ""
export (int, "_process", "_physics_process", "_notification", "none") var update_mode = 0 setget _set_update
export (int, "X-up", "Y-up", "Z-up") var look_at_axis = 1
export (bool) var use_our_rot_x = false
export (bool) var use_our_rot_y = false
export (bool) var use_our_rot_z = false
export (bool) var use_negative_our_rot = false
export (Vector3) var additional_rotation = Vector3()
export (bool) var debug_messages = false

var skeleton_to_use
var first_call = true
const empty_vector = Vector3()

func _ready():

set_process(false)
set_physics_process(false)
set_notify_transform(false)

if update_mode == 0:
set_process(true)
elif update_mode == 1:
set_physics_process(true)
elif update_mode == 2:
set_notify_transform(true)
else:
if debug_messages == true:
print (name, " - IK_LookAt: Unknown update mode. NOT updating skeleton")

if Engine.editor_hint == true:
_setup_for_editor()


func _setup_for_editor():
# So we can see the target in the editor, let's create a mesh instance,
# Add it as our child, and name it
var indicator = MeshInstance.new()
add_child(indicator)
indicator.name = "(EditorOnly) Visual indicator"

# We need to make a mesh for the mesh instance.
# The code below makes a small sphere mesh
var indicator_mesh = SphereMesh.new()
indicator_mesh.radius = 0.1
indicator_mesh.height = 0.2
indicator_mesh.radial_segments = 8
indicator_mesh.rings = 4

# The mesh needs a material (unless we want to use the defualt one).
# Let's create a material and use the EditorGizmoTexture to texture it.
var indicator_material = SpatialMaterial.new()
indicator_material.flags_unshaded = true
indicator_material.albedo_texture = preload("editor_gizmo_texture.png")
indicator_material.albedo_color = Color(1, 0.5, 0, 1)
indicator_mesh.material = indicator_material
indicator.mesh = indicator_mesh


func _set_update(new_value):
update_mode = new_value

# Set all of our processes to false
set_process(false)
set_physics_process(false)
set_notify_transform(false)

# Based on the value of upate, change how we handle updating the skeleton
if update_mode == 0:
set_process(true)
if debug_messages == true:
print (name, " - IK_LookAt: updating skeleton using _process...")
elif update_mode == 1:
set_physics_process(true)
if debug_messages == true:
print (name, " - IK_LookAt: updating skeleton using _physics_process...")
elif update_mode == 2:
set_notify_transform(true)
if debug_messages == true:
print (name, " - IK_LookAt: updating skeleton using _notification...")
else:
if debug_messages == true:
print (name, " - IK_LookAt: NOT updating skeleton due to unknown update method...")


func _set_skeleton_path(new_value):

# Because get_node doesn't work in the first call, we just want to assign instead
# This is to get around a issue with NodePaths exposed to the editor
if first_call == true:
skeleton_path = new_value
return

# Assign skeleton_path to whatever value is passed
skeleton_path = new_value

if skeleton_path == null:
if debug_messages == true:
print (name, " - IK_LookAt: No Nodepath selected for skeleton_path!")
return

# Get the node at that location, if there is one
var temp = get_node(skeleton_path)
if temp != null:
# If the node has the method "find_bone" then we can assume it is (likely) a skeleton
if temp.has_method("find_bone") == true:
skeleton_to_use = temp
if debug_messages == true:
print (name, " - IK_LookAt: attached to (new) skeleton")
# If not, then it's (likely) not a skeleton
else:
skeleton_to_use = null
if debug_messages == true:
print (name, " - IK_LookAt: skeleton_path does not point to a skeleton!")
else:
if debug_messages == true:
print (name, " - IK_LookAt: No Nodepath selected for skeleton_path!")


func update_skeleton():

# NOTE: Because get_node doesn't work in _ready, we need to skip
# a call before doing anything.
if first_call == true:
first_call = false
if skeleton_to_use == null:
_set_skeleton_path(skeleton_path)


# If we do not have a skeleton and/or we're not supposed to update, then return.
if skeleton_to_use == null:
return
if update_mode >= 3:
return

# Get the bone
var bone = skeleton_to_use.find_bone(bone_name)

# If no bone is found (-1), then return (and optionally print an error)
if bone == -1:
if debug_messages == true:
print (name, " - IK_LookAt: No bone in skeleton found with name [", bone_name, "]!")
return

# get the bone's rest position, and our position
var rest = skeleton_to_use.get_bone_global_pose(bone)
var our_position = global_transform.origin

# Convert our position relative to the skeleton's transform
var target_pos = skeleton_to_use.global_transform.xform_inv(global_transform.origin)

# Call helper's look_at function with the chosen up axis.
if look_at_axis == 0:
rest = rest.looking_at(target_pos, Vector3(1, 0, 0))
elif look_at_axis == 1:
rest = rest.looking_at(target_pos, Vector3(0, 1, 0))
elif look_at_axis == 2:
rest = rest.looking_at(target_pos, Vector3(0, 0, 1))
else:
rest = rest.looking_at(target_pos, Vector3(0, 1, 0))
if debug_messages == true:
print (name, " - IK_LookAt: Unknown look_at_axis value!")

# Get our rotation euler, and the bone's rotation euler
var rest_euler = rest.basis.get_euler()
var self_euler = global_transform.basis.orthonormalized().get_euler()

# If we using negative rotation, we flip our rotation euler
if use_negative_our_rot == true:
self_euler = -self_euler

# Apply our rotation euler, if wanted/required
if use_our_rot_x == true:
rest_euler.x = self_euler.x
if use_our_rot_y == true:
rest_euler.y = self_euler.y
if use_our_rot_z == true:
rest_euler.z = self_euler.z

# Rotate the bone by the (potentially) changed euler angle(s)
rest.basis = Basis(rest_euler)

# If we have additional rotation, then rotate it by the local rotation vectors
if additional_rotation != empty_vector:
rest.basis = rest.basis.rotated(rest.basis.x, deg2rad(additional_rotation.x))
rest.basis = rest.basis.rotated(rest.basis.y, deg2rad(additional_rotation.y))
rest.basis = rest.basis.rotated(rest.basis.z, deg2rad(additional_rotation.z))

# Finally, apply the bone rotation to the skeleton
skeleton_to_use.set_bone_global_pose(bone, rest)


# Various upate methods
# ---------------------
func _process(delta):
update_skeleton()
func _physics_process(delta):
update_skeleton()
func _notification(what):
if what == NOTIFICATION_TRANSFORM_CHANGED:
update_skeleton()
Binary file added 3d/ik/addons/sade/ik_look_at.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
32 changes: 32 additions & 0 deletions 3d/ik/addons/sade/ik_look_at.png.import
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
[remap]

importer="texture"
type="StreamTexture"
path="res://.import/ik_look_at.png-ec8164b5f09a539e05ec8153e50e1d99.stex"

[deps]

source_file="res://addons/SADE/ik_look_at.png"
source_md5="49fed7fb3ba1856215d1f334ed8bc583"

dest_files=[ "res://.import/ik_look_at.png-ec8164b5f09a539e05ec8153e50e1d99.stex" ]
dest_md5="690d9e1323d33b31eefd4014776d78d4"

[params]

compress/mode=0
compress/lossy_quality=0.7
compress/hdr_mode=0
compress/normal_map=0
flags/repeat=0
flags/filter=true
flags/mipmaps=false
flags/anisotropic=false
flags/srgb=2
process/fix_alpha_border=true
process/premult_alpha=false
process/HDR_as_SRGB=false
stream=false
size_limit=0
detect_3d=true
svg/scale=1.0
7 changes: 7 additions & 0 deletions 3d/ik/addons/sade/plugin.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[plugin]

name="S.A.D.E (Skeleton additions and extensions)"
description="S.A.D.E is A bunch of helpful nodes designed to make using skeletons in Godot powerful and easy."
author="TwistedTwigleg"
version="0.1.0"
script="plugin_main.gd"
21 changes: 21 additions & 0 deletions 3d/ik/addons/sade/plugin_main.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
tool
extends EditorPlugin

func _enter_tree():
# Plugin Initialization here!

# ------ IK STUFF ------
add_custom_type("IK_LookAt", "Spatial", preload("ik_look_at.gd"), preload("ik_look_at.png"))
add_custom_type("IK_FABRIK", "Spatial", preload("ik_fabrik.gd"), preload("ik_fabrik.png"))
# ------ ---------- ------



func _exit_tree():
# Plugin Clean-up here!

# ------ IK STUFF ------
remove_custom_type("IK_LookAt")
remove_custom_type("IK_FABRIK")
# ------ ---------- ------

Binary file added 3d/ik/battle_bot_colors.material
Binary file not shown.
Binary file added 3d/ik/battle_bot_emission.material
Binary file not shown.
10 changes: 10 additions & 0 deletions 3d/ik/button_change_scene.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
extends Button

export (String, FILE) var scene_to_change_to = null

func _ready():
connect("pressed", self, "change_scene")

func change_scene():
if scene_to_change_to != null:
get_tree().change_scene(scene_to_change_to)
Loading

0 comments on commit fc08eda

Please sign in to comment.