| 769 | } |
| 770 | |
| 771 | bool Cloth::OnPreUpdate() |
| 772 | { |
| 773 | #if WITH_CLOTH |
| 774 | if (!IsActiveInHierarchy()) |
| 775 | return true; |
| 776 | if (!_simulationSettings.UpdateWhenOffscreen && _simulationSettings.CullDistance > 0) |
| 777 | { |
| 778 | // Cull based on distance |
| 779 | bool cull = false; |
| 780 | if (_lastMinDstSqr >= Math::Square(_simulationSettings.CullDistance)) |
| 781 | cull = true; // Cull |
| 782 | else if (_lastMinDstSqr >= Math::Square(_simulationSettings.CullDistance * 0.8f)) |
| 783 | cull = _frameCounter % 4 == 0; // Update once every 4 frames |
| 784 | else if (_lastMinDstSqr >= Math::Square(_simulationSettings.CullDistance * 0.5f)) |
| 785 | cull = _frameCounter % 2 == 0; // Update once every 2 frames |
| 786 | _lastMinDstSqr = MAX_Real; |
| 787 | _frameCounter++; |
| 788 | if (cull) |
| 789 | return true; |
| 790 | } |
| 791 | |
| 792 | // Get current skinned mesh pose for the simulation of the non-kinematic vertices |
| 793 | if (auto* animatedModel = Cast<AnimatedModel>(GetParent())) |
| 794 | { |
| 795 | if (animatedModel->GraphInstance.NodesPose.IsEmpty() || _paint.IsEmpty()) |
| 796 | return false; |
| 797 | const ModelInstanceActor::MeshReference meshRef = GetMesh(); |
| 798 | if (meshRef.Actor == nullptr) |
| 799 | return false; |
| 800 | MeshAccessor accessor; |
| 801 | MeshBufferType bufferTypes[1] = { MeshBufferType::Vertex0 }; |
| 802 | if (accessor.LoadMesh(meshRef.Get(), false, ToSpan(bufferTypes, 1))) |
| 803 | return false; |
| 804 | auto positionStream = accessor.Position(); |
| 805 | auto blendIndicesStream = accessor.BlendIndices(); |
| 806 | auto blendWeightsStream = accessor.BlendWeights(); |
| 807 | if (!positionStream.IsValid() || !blendIndicesStream.IsValid() || !blendWeightsStream.IsValid()) |
| 808 | return false; |
| 809 | const int32 verticesCount = positionStream.GetCount(); |
| 810 | if (verticesCount != _paint.Count()) |
| 811 | { |
| 812 | LOG(Warning, "Incorrect cloth '{}' paint size {} for mesh '{}' that has {} vertices", GetNamePath(), _paint.Count(), meshRef.ToString(), verticesCount); |
| 813 | return false; |
| 814 | } |
| 815 | PROFILE_CPU_NAMED("Skinned Pose"); |
| 816 | PhysicsBackend::LockClothParticles(_cloth); |
| 817 | const Span<const Float4> particles = PhysicsBackend::GetClothParticles(_cloth); |
| 818 | // TODO: optimize memory allocs (eg. write directly to nvCloth mapped range or use shared allocator) |
| 819 | Array<Float4> particlesSkinned; |
| 820 | particlesSkinned.Set(particles.Get(), particles.Length()); |
| 821 | |
| 822 | Span<Matrix> pose; |
| 823 | animatedModel->GetCurrentPose(pose); |
| 824 | const SkeletonData& skeleton = animatedModel->SkinnedModel->Skeleton; |
| 825 | const SkeletonBone* bones = skeleton.Bones.Get(); |
| 826 | |
| 827 | // Animated model uses skinning thus requires to set vertex position inverse to skeleton bones |
| 828 | const float* paint = _paint.Get(); |
no test coverage detected