Created
September 21, 2022 18:07
-
-
Save benursu/029e139aef4c9f51bc1f23261b3090f4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// @input Component.ObjectTracking3D leftHand | |
// @input Component.ObjectTracking3D rightHand | |
// @input SceneObject heat | |
// @input SceneObject instructionsBack | |
// @input SceneObject instructionsFront | |
// @input SceneObject leftHandPalm | |
// @input SceneObject rightHandPalm | |
// @input SceneObject cameraProjection | |
// @input SceneObject cameraTracking | |
// @input SceneObject fireball | |
// @input SceneObject fireballBack0 | |
// @input SceneObject fireballBack1 | |
// @input SceneObject explosionBack0 | |
// @input SceneObject explosionBack1 | |
// @input SceneObject fireballCollision | |
// @input SceneObject fireballTarget | |
// @input SceneObject handInstructions | |
// @input SceneObject pointInstructions | |
// @input Asset.Texture fireball0Texture | |
// @input Asset.Texture fireball1Texture | |
// @input Asset.Texture explosion0Texture | |
// @input Asset.Texture explosion1Texture | |
// @input Component.AudioComponent fireballStart | |
// @input Component.AudioComponent fireballShot | |
// @input Component.AudioComponent fireballHit | |
///////////////////////////////////////////////////////////////////////////////////// | |
///////////////////////////////////////////////////////////////////////////////////// | |
///////////////////////////////////////////////////////////////////////////////////// | |
//camera | |
var cameraDirection = 'selfie'; | |
function onSwitchToFrontCamera() { | |
cameraDirection = 'selfie'; | |
script.instructionsFront.enabled = true; | |
script.instructionsBack.enabled = false; | |
script.fireballBack0.enabled = false; | |
// script.fireballBack1.enabled = false; | |
script.explosionBack0.enabled = false; | |
script.explosionBack1.enabled = false; | |
fireballHeatStay = false; | |
global.tweenManager.stopTween(script.heat, 'world fireball stay'); | |
global.tweenManager.stopTween(script.heat, 'world fireball shot'); | |
global.tweenManager.stopTween(script.heat, 'world fireball hit'); | |
global.tweenManager.startTween(script.heat, 'off'); | |
} | |
function onSwitchToBackCamera() { | |
cameraDirection = 'world'; | |
script.instructionsFront.enabled = false; | |
script.instructionsBack.enabled = true; | |
script.fireballBack0.enabled = true; | |
// script.fireballBack1.enabled = true; | |
script.explosionBack0.enabled = true; | |
script.explosionBack1.enabled = true; | |
fireballMode = 'waiting'; | |
script.handInstructions.enabled = false; | |
script.pointInstructions.enabled = false; | |
handInstructionsCancel(); | |
pointerInstructionsCancel(); | |
handInstructionsStarted = false; | |
handInstructionsShowDelayedEvent = null; | |
pointerInstructionsStarted = false; | |
pointerInstructionsShowDelayedEvent = null; | |
script.fireball0Texture.control.stop(); | |
script.fireball1Texture.control.stop(); | |
} | |
script.createEvent('CameraFrontEvent').bind(onSwitchToFrontCamera); | |
script.createEvent('CameraBackEvent').bind(onSwitchToBackCamera); | |
///////////////////////////////////////////////////////////////////////////////////// | |
///////////////////////////////////////////////////////////////////////////////////// | |
///////////////////////////////////////////////////////////////////////////////////// | |
//hand tracking | |
var isLeftTracking = false; | |
var isRightTracking = false; | |
var leftJoints = {}; | |
var rightJoints = {}; | |
const JOINT_NAMES = ["wrist","thumb-0","thumb-1","thumb-2","thumb-3","index-0","index-1","index-2","index-3","mid-0","mid-1","mid-2","mid-3","ring-0","ring-1","ring-2","ring-3","pinky-0","pinky-1","pinky-2","pinky-3","wrist_to_thumb","wrist_to_index","wrist_to_mid","wrist_to_ring","wrist_to_pinky"]; | |
function onInitialize() { | |
for (var i=0; i<JOINT_NAMES.length; i++) { | |
leftJoints[JOINT_NAMES[i]] = script.leftHand.createAttachmentPoint(JOINT_NAMES[i]); | |
rightJoints[JOINT_NAMES[i]] = script.rightHand.createAttachmentPoint(JOINT_NAMES[i]); | |
} | |
} | |
onInitialize(); | |
script.leftHand.onTrackingStarted = function() { | |
isLeftTracking = true; | |
}; | |
script.leftHand.onTrackingLost = function() { | |
isLeftTracking = false; | |
}; | |
script.rightHand.onTrackingStarted = function() { | |
isRightTracking = true; | |
}; | |
script.rightHand.onTrackingLost = function() { | |
isRightTracking = false; | |
}; | |
function getJointsPosition(handType, jointNames) { | |
if (getActiveHand == null) { | |
return; | |
} | |
if (!Array.isArray(jointNames)) { | |
if ((handType != "R") && getActiveHand() == "L") { | |
return leftJoints[jointNames].getTransform().getWorldPosition(); | |
} else if (handType != "L" && getActiveHand() == "R") { | |
return rightJoints[jointNames].getTransform().getWorldPosition(); | |
} | |
} else { | |
var vec3arr = []; | |
for (var i=0; i < jointNames.length; i ++) { | |
if ((handType != "R") && getActiveHand() == "L") { | |
vec3arr.push(leftJoints[jointNames[i]].getTransform().getWorldPosition()); | |
} else if (handType != "L" && getActiveHand() == "R") { | |
vec3arr.push(rightJoints[jointNames[i]].getTransform().getWorldPosition()); | |
} | |
} | |
return getAverageVec3(vec3arr); | |
} | |
} | |
function getAverageVec3(vecs) { | |
var result = new vec3(0,0,0); | |
for (var i=0; i < vecs.length; i ++) { | |
result = result.add(vecs[i]); | |
} | |
result = result.uniformScale(1 / vecs.length); | |
return result; | |
} | |
function getActiveHand() { | |
if (isLeftTracking) { | |
return "L"; | |
} else if (isRightTracking) { | |
return "R"; | |
} else { | |
return null; | |
} | |
} | |
// | |
global.isLeftHandTracking = function() { | |
return isLeftTracking; | |
}; | |
global.isRightHandTracking = function() { | |
return isRightTracking; | |
}; | |
global.getActiveHand = getActiveHand; | |
global.getJointsPosition = getJointsPosition; | |
///////////////////////////////////////////////////////////////////////////////////// | |
///////////////////////////////////////////////////////////////////////////////////// | |
///////////////////////////////////////////////////////////////////////////////////// | |
//fireball | |
var fireballMode = 'waiting'; //waiting,tracking,shooting,done | |
var fireballTrackingLerp = 0.25; | |
var fireballHeatStay = false; | |
var fireballTrackerOffset = 3.5; | |
var fireballActiveScale = new vec3(1, 1, 1); | |
var fireballDeactiveScale = new vec3(0, 0, 0); | |
var fireballScaleLerp = 0.5; | |
var fireballShootingStart = new vec3(0, 0, 0); | |
var fireballShootingEnd = new vec3(0, 0, 0); | |
var fireballShootingLerp = 0.1; | |
var fireballShootingCollisionDistance = 150; | |
var fireballShootingCollisionWarmupDistance = 160; | |
var fireballShootingCollisionWarmupStarted = false; | |
updateEvent = script.createEvent('UpdateEvent'); | |
updateEvent.bind(function(eventData) | |
{ | |
var trackingObject = null; | |
var target = null; | |
if(global.isRightHandTracking()){ | |
trackingObject = script.rightHandPalm; | |
} | |
if(global.isLeftHandTracking()){ | |
trackingObject = script.leftHandPalm; | |
} | |
if(cameraDirection == 'world'){ | |
//world | |
if(trackingObject != null){ | |
//back | |
target = trackingObject.getTransform().getWorldPosition(); | |
target.y += fireballTrackerOffset; | |
} | |
if(fireballMode == 'waiting'){ | |
//waiting for hand | |
handInstructionsShow(); | |
pointerInstructionsCancel(); | |
if(trackingObject != null){ | |
fireballMode = 'tracking'; | |
script.fireball.enabled = true; | |
script.fireballStart.play(1); | |
fireballShootingCollisionWarmupStarted = false; | |
script.fireball0Texture.control.play(-1,0); | |
script.fireball1Texture.control.play(-1,0); | |
script.fireball.getTransform().setWorldPosition(target); | |
} | |
}else if(fireballMode == 'tracking'){ | |
//tracking to hand | |
if(!fireballHeatStay){ | |
fireballHeatStay = true; | |
global.tweenManager.stopTween(script.heat, 'world fireball stay'); | |
global.tweenManager.stopTween(script.heat, 'world fireball shot'); | |
global.tweenManager.stopTween(script.heat, 'world fireball hit'); | |
global.tweenManager.stopTween(script.heat, 'off'); | |
global.tweenManager.startTween(script.heat, 'world fireball stay'); | |
} | |
handInstructionsCancel(); | |
pointerInstructionsShow(); | |
if(trackingObject == null){ | |
target = script.cameraTracking.getTransform().getWorldPosition(); | |
} | |
script.fireball.getTransform().setWorldPosition(vec3.lerp(script.fireball.getTransform().getWorldPosition(), target, fireballTrackingLerp)); | |
}else if(fireballMode == 'shooting'){ | |
//shooting | |
handInstructionsCancel(); | |
pointerInstructionsCancel(); | |
script.fireball.getTransform().setWorldPosition(vec3.lerp(script.fireball.getTransform().getWorldPosition(), fireballShootingEnd, fireballShootingLerp)); | |
var distance = script.fireball.getTransform().getWorldPosition().distance(fireballShootingEnd); | |
if(!fireballShootingCollisionWarmupStarted && distance < fireballShootingCollisionWarmupDistance){ | |
fireballShootingCollisionWarmupStarted = true; | |
script.explosion0Texture.control.play(1,0); | |
script.explosion1Texture.control.play(1,0); | |
script.fireballHit.play(1); | |
} | |
if(distance < fireballShootingCollisionDistance){ | |
//collision | |
script.fireballCollision.enabled = true; | |
script.fireballCollision.getTransform().setWorldPosition(script.fireball.getTransform().getWorldPosition()); | |
global.tweenManager.stopTween(script.heat, 'world fireball shot'); | |
global.tweenManager.startTween(script.heat, 'world fireball hit'); | |
var delayedEvent = script.createEvent('DelayedCallbackEvent'); | |
delayedEvent.bind(function(eventData) | |
{ | |
script.fireball.enabled = false; | |
fireballMode = 'waiting'; | |
}); | |
delayedEvent.reset(1); | |
} | |
} | |
} | |
}); | |
function onIndexFinger() { | |
if(cameraDirection == 'world' && fireballMode == 'tracking'){ | |
fireballShootingStart = script.fireball.getTransform().getWorldPosition(); | |
script.fireballTarget.getTransform().setWorldPosition(script.cameraProjection.getTransform().getWorldPosition()); | |
fireballShootingEnd = script.fireballTarget.getTransform().getWorldPosition(); | |
script.fireballShot.play(1); | |
script.pointInstructions.enabled = false; | |
fireballHeatStay = false; | |
global.tweenManager.stopTween(script.heat, 'world fireball stay'); | |
global.tweenManager.startTween(script.heat, 'world fireball shot'); | |
global.tweenManager.startTween(script.heat, 'world fireball hit'); | |
fireballMode = 'shooting'; | |
} | |
} | |
global.behaviorSystem.addCustomTriggerResponse('index_finger_gesture_trigger', onIndexFinger); | |
///////////////////////////////////////////////////////////////////////////////////// | |
///////////////////////////////////////////////////////////////////////////////////// | |
///////////////////////////////////////////////////////////////////////////////////// | |
//instructions | |
var instructionCounter = 0; | |
var instructionCounterMax = 2; | |
var handInstructionsStarted = false; | |
var handInstructionsShowDelayedEvent = null; | |
function handInstructionsShow(){ | |
if(!handInstructionsStarted && instructionCounter < instructionCounterMax){ | |
handInstructionsStarted = true; | |
handInstructionsShowDelayedEvent = script.createEvent('DelayedCallbackEvent'); | |
handInstructionsShowDelayedEvent.bind(function(eventData) | |
{ | |
script.handInstructions.enabled = true; | |
script.pointInstructions.enabled = false; | |
}); | |
handInstructionsShowDelayedEvent.reset(0.5); | |
} | |
} | |
function handInstructionsCancel(){ | |
handInstructionsStarted = false; | |
script.handInstructions.enabled = false; | |
script.removeEvent(handInstructionsShowDelayedEvent); | |
} | |
var pointerInstructionsStarted = false; | |
var pointerInstructionsShowDelayedEvent = null; | |
function pointerInstructionsShow(){ | |
if(!pointerInstructionsStarted && instructionCounter < instructionCounterMax){ | |
pointerInstructionsStarted = true; | |
instructionCounter++; | |
pointerInstructionsShowDelayedEvent = script.createEvent('DelayedCallbackEvent'); | |
pointerInstructionsShowDelayedEvent.bind(function(eventData) | |
{ | |
script.handInstructions.enabled = false; | |
script.pointInstructions.enabled = true; | |
}); | |
pointerInstructionsShowDelayedEvent.reset(0.5); | |
} | |
} | |
function pointerInstructionsCancel(){ | |
pointerInstructionsStarted = false; | |
script.pointInstructions.enabled = false; | |
script.removeEvent(pointerInstructionsShowDelayedEvent); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment