-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Initial play animation branch commit. #2
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -70,39 +70,72 @@ export class BabylonDecorator extends ADecorator { | |
this.behaveEngine.registerJsonPointer(jsonPtr, getterCallback, setterCallback, typeName); | ||
}; | ||
|
||
registerEngineCallback = (path: string, callback: (value: any, onComplete: ()=> void) => void) => { | ||
this.behaveEngine.registerEngineCallback(path, callback); | ||
} | ||
|
||
registerKnownPointers = () => { | ||
this.registerJsonPointer("nodes/99/scale", (path) => { | ||
const parts: string[] = path.split("/"); | ||
return [(this.world.glTFNodes[Number(parts[1])] as AbstractMesh).scaling.x, | ||
(this.world.glTFNodes[Number(parts[1])] as AbstractMesh).scaling.y, | ||
(this.world.glTFNodes[Number(parts[1])] as AbstractMesh).scaling.z]; | ||
}, (path, value) => { | ||
const parts: string[] = path.split("/"); | ||
(this.world.glTFNodes[Number(parts[1])] as AbstractMesh).scaling = new Vector3(value[0], value[1], value[2]); | ||
}, "float3") | ||
|
||
this.registerJsonPointer("nodes/99/translation", (path) => { | ||
const parts: string[] = path.split("/"); | ||
return [(this.world.glTFNodes[Number(parts[1])] as AbstractMesh).position.x, | ||
(this.world.glTFNodes[Number(parts[1])] as AbstractMesh).position.y, | ||
(this.world.glTFNodes[Number(parts[1])] as AbstractMesh).position.z]; | ||
}, (path, value) => { | ||
const parts: string[] = path.split("/"); | ||
console.log(value); | ||
(this.world.glTFNodes[Number(parts[1])] as AbstractMesh).position= new Vector3(value[0], value[1], value[2]); | ||
}, "float3") | ||
|
||
this.registerJsonPointer("nodes/99/rotation", (path) => { | ||
const parts: string[] = path.split("/"); | ||
return [ | ||
(this.world.glTFNodes[Number(parts[1])] as AbstractMesh).rotationQuaternion?.w, | ||
(this.world.glTFNodes[Number(parts[1])] as AbstractMesh).rotationQuaternion?.x, | ||
(this.world.glTFNodes[Number(parts[1])] as AbstractMesh).rotationQuaternion?.y, | ||
(this.world.glTFNodes[Number(parts[1])] as AbstractMesh).rotationQuaternion?.z]; | ||
}, (path, value) => { | ||
const parts: string[] = path.split("/"); | ||
(this.world.glTFNodes[Number(parts[1])] as AbstractMesh).rotationQuaternion = new Quaternion(value[1], value[2], value[3], value[0]); | ||
}, "float4") | ||
const gltfNodesSize:number = this.world.glTFNodes.length; | ||
|
||
this.registerEngineCallback(`playAnimation`, (value, onComplete) => { | ||
// Note : targetTime not yet implemented to do something yet | ||
const {animation, speed, loopCount, targetTime} = value; | ||
const animationGroup = this.scene.animationGroups[animation]; | ||
|
||
animationGroup.speedRatio = speed; | ||
|
||
// run infinitely | ||
if (loopCount <= 0) { | ||
animationGroup.start(true, speed); | ||
} | ||
else { | ||
let count = loopCount; | ||
animationGroup.start(false, speed); | ||
animationGroup.onAnimationGroupEndObservable.add(function() { | ||
-- count; | ||
if (count > 0) { | ||
animationGroup.start(false, speed); | ||
} else { | ||
onComplete(); | ||
} | ||
}); | ||
} | ||
}); | ||
|
||
for (let i = 0; i < gltfNodesSize; i++) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you only have to register it once for the highest number, so we could just do nodes/${glTFNodesSize}/scale etc.. |
||
this.registerJsonPointer(`nodes/${i}/scale`, (path) => { | ||
const parts: string[] = path.split("/"); | ||
return [(this.world.glTFNodes[Number(parts[1])] as AbstractMesh).scaling.x, | ||
(this.world.glTFNodes[Number(parts[1])] as AbstractMesh).scaling.y, | ||
(this.world.glTFNodes[Number(parts[1])] as AbstractMesh).scaling.z]; | ||
}, (path, value) => { | ||
const parts: string[] = path.split("/"); | ||
(this.world.glTFNodes[Number(parts[1])] as AbstractMesh).scaling = new Vector3(value[0], value[1], value[2]); | ||
}, "float3") | ||
|
||
this.registerJsonPointer(`nodes/${i}/translation`, (path) => { | ||
const parts: string[] = path.split("/"); | ||
return [(this.world.glTFNodes[Number(parts[1])] as AbstractMesh).position.x, | ||
(this.world.glTFNodes[Number(parts[1])] as AbstractMesh).position.y, | ||
(this.world.glTFNodes[Number(parts[1])] as AbstractMesh).position.z]; | ||
}, (path, value) => { | ||
const parts: string[] = path.split("/"); | ||
(this.world.glTFNodes[Number(parts[1])] as AbstractMesh).position= new Vector3(value[0], value[1], value[2]); | ||
}, "float3") | ||
|
||
this.registerJsonPointer(`nodes/${i}/rotation`, (path) => { | ||
const parts: string[] = path.split("/"); | ||
return [ | ||
(this.world.glTFNodes[Number(parts[1])] as AbstractMesh).rotationQuaternion?.w, | ||
(this.world.glTFNodes[Number(parts[1])] as AbstractMesh).rotationQuaternion?.x, | ||
(this.world.glTFNodes[Number(parts[1])] as AbstractMesh).rotationQuaternion?.y, | ||
(this.world.glTFNodes[Number(parts[1])] as AbstractMesh).rotationQuaternion?.z]; | ||
}, (path, value) => { | ||
const parts: string[] = path.split("/"); | ||
(this.world.glTFNodes[Number(parts[1])] as AbstractMesh).rotationQuaternion = new Quaternion(value[1], value[2], value[3], value[0]); | ||
}, "float4") | ||
|
||
} | ||
} | ||
|
||
public extractBehaveGraphFromScene = (): any => { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import {BehaveEngineNode, IBehaviourNodeProps} from "../../BehaveEngineNode"; | ||
|
||
export class PlayAnimation extends BehaveEngineNode { | ||
REQUIRED_VALUES = [{id: "animation"}, {id: "speed"}, {id: "loopCount"}, {id: "targetTime"}] | ||
|
||
_animation: number; | ||
_speed: number; | ||
_loopCount: number; | ||
_targetTime: number; | ||
|
||
constructor(props: IBehaviourNodeProps) { | ||
super(props); | ||
this.name = "PlayAnimation"; | ||
this.validateValues(this.values); | ||
this.validateFlows(this.flows); | ||
this.validateConfigurations(this.configuration); | ||
|
||
const {animation, speed, loopCount, targetTime} = this.evaluateAllValues(this.REQUIRED_VALUES.map(val => val.id)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. values should not be evaluated at construction time, general practice is configuration = "class" private variables and the values are temporary variables only evaluated and used during the processNode function There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Gotcha |
||
|
||
this._animation = animation; | ||
this._speed = speed; | ||
this._loopCount = loopCount; | ||
this._targetTime = targetTime; | ||
} | ||
|
||
override processNode(flowSocket?: string) { | ||
this.graphEngine.clearValueEvaluationCache(); | ||
|
||
const {animation, speed, loopCount, targetTime} = this.evaluateAllValues(this.REQUIRED_VALUES.map(val => val.id)); | ||
|
||
this._animation = animation; | ||
this._speed = speed; | ||
this._loopCount = loopCount; | ||
this._targetTime = targetTime; | ||
|
||
this.graphEngine.processNodeStarted(this); | ||
|
||
console.log("trying to run play animation animation path value"); | ||
//const storeThisProcessNode = super.processNode; | ||
const that = this; | ||
|
||
this.graphEngine.runEngineCallback("playAnimation", {animation, speed, loopCount, targetTime}, function() { | ||
if (that.flows.done) { | ||
that.addEventToWorkQueue(that.flows.done) | ||
} | ||
}); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,28 +38,32 @@ export const BabylonEngineComponent = (props: {behaveGraphRef: any, setBehaveGra | |
const [fileUploaded, setFileUploaded] = useState(false); | ||
const [clickedHotSpot, setClickedHotSpot] = useState<string | null>(null); | ||
|
||
function createScene() { | ||
// Create a scene | ||
sceneRef.current = new Scene(engineRef.current!); | ||
|
||
// Create a camera | ||
const camera = new ArcRotateCamera('camera', Math.PI / 3, Math.PI / 3, 10, Vector3.Zero(), sceneRef.current); | ||
camera.attachControl(canvasRef.current, true); | ||
camera.minZ = 0.001; | ||
canvasRef.current!.addEventListener("wheel", (e: any) => { | ||
e.preventDefault(); | ||
e.stopPropagation(); | ||
|
||
return false; | ||
}) | ||
|
||
// Create lights | ||
new HemisphericLight('light1', new Vector3(0, 1, 0), sceneRef.current); | ||
new DirectionalLight('light2', new Vector3(1, -1, 0), sceneRef.current); | ||
|
||
|
||
} | ||
|
||
useEffect(() => { | ||
// Create the Babylon.js engines | ||
engineRef.current = new Engine(canvasRef.current, true); | ||
|
||
// Create a scene | ||
sceneRef.current = new Scene(engineRef.current!); | ||
|
||
// Create a camera | ||
const camera = new ArcRotateCamera('camera', Math.PI / 3, Math.PI / 3, 10, Vector3.Zero(), sceneRef.current); | ||
camera.attachControl(canvasRef.current, true); | ||
camera.minZ = 0.001; | ||
canvasRef.current!.addEventListener("wheel", (e: any) => { | ||
e.preventDefault(); | ||
e.stopPropagation(); | ||
|
||
return false; | ||
}) | ||
|
||
// Create lights | ||
new HemisphericLight('light1', new Vector3(0, 1, 0), sceneRef.current); | ||
new DirectionalLight('light2', new Vector3(1, -1, 0), sceneRef.current); | ||
|
||
createScene(); | ||
// Run the render loop | ||
engineRef.current?.runRenderLoop(() => { | ||
sceneRef.current?.render(); | ||
|
@@ -74,13 +78,21 @@ export const BabylonEngineComponent = (props: {behaveGraphRef: any, setBehaveGra | |
}, []); | ||
|
||
const resetScene = async () => { | ||
sceneRef.current?.meshes.forEach(mesh => mesh.dispose()) | ||
sceneRef.current?.dispose(); | ||
createScene(); | ||
|
||
const file = fileInputRef.current!.files![0] | ||
|
||
const url = URL.createObjectURL(file); | ||
|
||
const container = await SceneLoader.LoadAssetContainerAsync("", url, sceneRef.current, undefined, ".glb"); | ||
container.addAllToScene(); | ||
|
||
// Stop all animations by default (only apply based off of play animation node) | ||
for (let i = 0; i < (sceneRef.current?.animationGroups?.length ?? 0); i ++) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Babylon has a loader plugin to do this (see https://forum.babylonjs.com/t/how-to-prevent-animations-from-auto-starting-by-sceneloader/27945) |
||
sceneRef.current?.animationGroups[i].stop(); | ||
} | ||
|
||
return buildGlTFNodeLayout(container.rootNodes[0]); | ||
}; | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is not a path, I do like the idea of abstracting the callbacks outside of babylon, but maybe we can do this someplace other than the json get/set trie