diff --git a/extensions/pl_ecs_ext.c b/extensions/pl_ecs_ext.c index 5455aad..f1e40fe 100644 --- a/extensions/pl_ecs_ext.c +++ b/extensions/pl_ecs_ext.c @@ -562,7 +562,12 @@ pl_ecs_add_component(plComponentLibrary* ptLibrary, plComponentType tType, plEnt if(bAddSlot) pl_sb_add(sbComponents); ptManager->pComponents = sbComponents; - sbComponents[uComponentIndex] = (plTransformComponent){.tWorld = pl_identity_mat4(), .tScale = {1.0f, 1.0f, 1.0f}, .tRotation = {0.0f, 0.0f, 0.0f, 1.0f}}; + sbComponents[uComponentIndex] = (plTransformComponent){ + .tWorld = pl_identity_mat4(), + .tScale = {1.0f, 1.0f, 1.0f}, + .tRotation = {0.0f, 0.0f, 0.0f, 1.0f}, + .tFlags = PL_TRANSFORM_FLAGS_DIRTY + }; return &sbComponents[uComponentIndex]; } @@ -917,9 +922,6 @@ pl_ecs_create_transform(plComponentLibrary* ptLibrary, const char* pcName, plTra plEntity tNewEntity = pl_ecs_create_tag(ptLibrary, pcName); plTransformComponent* ptTransform = pl_ecs_add_component(ptLibrary, PL_COMPONENT_TYPE_TRANSFORM, tNewEntity); - ptTransform->tScale = (plVec3){1.0f, 1.0f, 1.0f}; - ptTransform->tRotation = (plVec4){0.0f, 0.0f, 0.0f, 1.0f}; - ptTransform->tWorld = pl_identity_mat4(); if(pptCompOut) *pptCompOut = ptTransform; @@ -1180,7 +1182,14 @@ pl_run_transform_update_system(plComponentLibrary* ptLibrary) for(uint32_t i = 0; i < uComponentCount; i++) { plTransformComponent* ptTransform = &sbtComponents[i]; - ptTransform->tWorld = pl_rotation_translation_scale(ptTransform->tRotation, ptTransform->tTranslation, ptTransform->tScale); + if(ptTransform->tFlags & PL_TRANSFORM_FLAGS_DIRTY) + { + ptTransform->tWorld = pl_rotation_translation_scale(ptTransform->tRotation, ptTransform->tTranslation, ptTransform->tScale); + + plHierarchyComponent* ptHierarchyComponent = pl_ecs_get_component(ptLibrary, PL_COMPONENT_TYPE_HIERARCHY, ptLibrary->tTransformComponentManager.sbtEntities[i]); + if(ptHierarchyComponent == NULL) // let object update system have a chance to handle flag + ptTransform->tFlags &= ~PL_TRANSFORM_FLAGS_DIRTY; + } } pl_end_cpu_sample(gptProfile, 0); @@ -1198,8 +1207,11 @@ pl_run_hierarchy_update_system(plComponentLibrary* ptLibrary) plHierarchyComponent* ptHierarchyComponent = pl_ecs_get_component(ptLibrary, PL_COMPONENT_TYPE_HIERARCHY, tChildEntity); plTransformComponent* ptParentTransform = pl_ecs_get_component(ptLibrary, PL_COMPONENT_TYPE_TRANSFORM, ptHierarchyComponent->tParent); plTransformComponent* ptChildTransform = pl_ecs_get_component(ptLibrary, PL_COMPONENT_TYPE_TRANSFORM, tChildEntity); - if(ptParentTransform && ptChildTransform) + if(ptParentTransform && ptChildTransform->tFlags & PL_TRANSFORM_FLAGS_DIRTY) + { ptChildTransform->tWorld = pl_mul_mat4(&ptParentTransform->tWorld, &ptChildTransform->tWorld); + ptChildTransform->tFlags &= ~PL_TRANSFORM_FLAGS_DIRTY; + } } pl_end_cpu_sample(gptProfile, 0); @@ -1332,6 +1344,7 @@ pl_run_animation_update_system(plComponentLibrary* ptLibrary, float fDeltaTime) const plAnimationSampler* ptSampler = &ptAnimationComponent->sbtSamplers[ptChannel->uSamplerIndex]; const plAnimationDataComponent* ptData = pl_ecs_get_component(ptLibrary, PL_COMPONENT_TYPE_ANIMATION_DATA, ptSampler->tData); plTransformComponent* ptTransform = pl_ecs_get_component(ptLibrary, PL_COMPONENT_TYPE_TRANSFORM, ptChannel->tTarget); + ptTransform->tFlags |= PL_TRANSFORM_FLAGS_DIRTY; // wrap t around, so the animation loops. // make sure that t is never earlier than the first keyframe and never later then the last keyframe. diff --git a/extensions/pl_ecs_ext.h b/extensions/pl_ecs_ext.h index fd28e20..19cdd92 100644 --- a/extensions/pl_ecs_ext.h +++ b/extensions/pl_ecs_ext.h @@ -87,6 +87,7 @@ typedef int plLightFlags; typedef int plLightType; typedef int plHumanoidBone; typedef int plEnvironmentProbeFlags; +typedef int plTransformFlags; typedef union _plEntity { @@ -392,6 +393,12 @@ enum _plEnvironmentProbeFlags PL_ENVIRONMENT_PROBE_FLAGS_PARALLAX_CORRECTION_BOX = 1 << 3, }; +enum _plTransformFlags +{ + PL_TRANSFORM_FLAGS_NONE = 0, + PL_TRANSFORM_FLAGS_DIRTY = 1 << 0, +}; + //----------------------------------------------------------------------------- // [SECTION] structs //----------------------------------------------------------------------------- @@ -532,10 +539,11 @@ typedef struct _plLayerComponent typedef struct _plTransformComponent { - plVec3 tScale; - plVec4 tRotation; - plVec3 tTranslation; - plMat4 tWorld; + plVec3 tScale; + plVec4 tRotation; + plVec3 tTranslation; + plMat4 tWorld; + plTransformFlags tFlags; } plTransformComponent; typedef struct _plMaterialComponent diff --git a/sandbox/app.c b/sandbox/app.c index a1348a8..6055405 100644 --- a/sandbox/app.c +++ b/sandbox/app.c @@ -199,10 +199,10 @@ pl_app_load(plApiRegistryI* ptApiRegistry, plEditorData* ptEditorData) ptLight->uCascadeCount = 4; ptLight->fIntensity = 1.0f; ptLight->uShadowResolution = 1024; - ptLight->afCascadeSplits[0] = 5.0f; - ptLight->afCascadeSplits[1] = 10.0f; - ptLight->afCascadeSplits[2] = 20.0f; - ptLight->afCascadeSplits[3] = 48.0f; + ptLight->afCascadeSplits[0] = 0.10f; + ptLight->afCascadeSplits[1] = 0.25f; + ptLight->afCascadeSplits[2] = 0.50f; + ptLight->afCascadeSplits[3] = 1.00f; ptLight->tFlags |= PL_LIGHT_FLAG_CAST_SHADOW | PL_LIGHT_FLAG_VISUALIZER; plEntity tPointLight = gptEcs->create_point_light(ptMainComponentLibrary, "point light", (plVec3){0.0f, 2.0f, 2.0f}, &ptLight); diff --git a/sandbox/pl_gizmo.c b/sandbox/pl_gizmo.c index 31ee253..3c5bbd3 100644 --- a/sandbox/pl_gizmo.c +++ b/sandbox/pl_gizmo.c @@ -116,12 +116,15 @@ pl_gizmo(plGizmoData* ptGizmoData, plDrawList3D* ptGizmoDrawlist, plComponentLib { case PL_SELECTION_MODE_TRANSLATION: pl__gizmo_translation(ptGizmoData, ptGizmoDrawlist, ptCamera, ptSelectedTransform, ptParentTransform); + ptSelectedTransform->tFlags |= PL_TRANSFORM_FLAGS_DIRTY; break; case PL_SELECTION_MODE_ROTATION: pl__gizmo_rotation(ptGizmoData, ptGizmoDrawlist, ptCamera, ptSelectedTransform, ptParentTransform); + ptSelectedTransform->tFlags |= PL_TRANSFORM_FLAGS_DIRTY; break; case PL_SELECTION_MODE_SCALE: pl__gizmo_scale(ptGizmoData, ptGizmoDrawlist, ptCamera, ptSelectedTransform, ptParentTransform); + ptSelectedTransform->tFlags |= PL_TRANSFORM_FLAGS_DIRTY; break; case PL_SELECTION_MODE_NONE: default: