You've already forked UnrealEngineUWP
mirror of
https://github.com/izzy2lost/UnrealEngineUWP.git
synced 2026-03-26 18:15:20 -07:00
#lockdown Nick.Penwarden #rb none ========================== MAJOR FEATURES + CHANGES ========================== Change 3267632 on 2017/01/23 by Jurre.deBaare Marker syncs not working correctly in Blend Spaces #fix Ensure that SampleIndexWithMarkers is serialized #JIRA UE-40975 Change 3266915 on 2017/01/20 by Arciel.Rekman Fix Persona crash on Linux (UE-38790). - Static template variable got instantiated into multiple DSOs; probably exacerbated by --as-needed since this does not happen without it. #jira UE-38790 Change 3266785 on 2017/01/20 by Ian.Fox #OnlineSubsystemLive - Make usage of CachedUsers thread safe. Duplicates CL 3245390 #jira UE-40649 Change 3266762 on 2017/01/20 by Rolando.Caloca UE4.15 - Fix for reallocating scene color #jira UE-40633 Change 3266642 on 2017/01/20 by Lina.Halper Downgraded Warning to Info #jira: UE-40643 Change 3266532 on 2017/01/20 by Jeff.Campeau Fix multiplatform Windows includes defeating the safety check in MinWindows.h #jira UE-40778 #rn Fixed a compile warning on Xbox One when XboxOneMinApi.h was included before MinWindows.h. Change 3266523 on 2017/01/20 by Marc.Audy Fix case where child actor could avoid getting begin play call #jira UE-40960 Change 3266474 on 2017/01/20 by Peter.Sauerbrei fix for using an API not yet available in iOS 8 #jira UE-40698 Change 3266339 on 2017/01/20 by Frank.Fella Sequencer - Fix UI issues with multi-track section rows. + Don't show an empty sub-track when there are no sections. + Expand parent tracks by default. #Jira UE-40487 Change 3266283 on 2017/01/20 by Jeff.Fisher UE-40683 GearVR projects rendering black -Fix from Remi Palandri #jira UE-40683 #review-3265824 @nick.whiting @ryan.vance Change 3266264 on 2017/01/20 by Lina.Halper Downgraded warning and changed log message #jira: UE-40643 Change 3266239 on 2017/01/20 by Peter.Sauerbrei fix for virtual joystick not showing up on some devices #jira UE-40472 Change 3266084 on 2017/01/20 by Mitchell.Wilson Resaving level to have correct starting camera position. Saved in wrong position after fixing a bug. #jira UE-40887 Change3266077on 2017/01/20 by Matt.Kuhlenschmidt Fixed "Wait for Movies to Complete" flag being reversed #jira UE-40943 Change 3266076 on 2017/01/20 by Mitchell.Wilson Updating occulsion bounds method on P_spark_burst_2 so it is not occluded when spawned inside of the coin mesh in BP_Overview example. Updating some post process examples due to changes made with Post Process settings. Film and Scene Color are temporary fixes and are intended to be fully updated in 4.16 #jira UE-40830 UE-40887 Change 3266034 on 2017/01/20 by Benn.Gallagher Fixed crash when reimporting APEX destructibles from apb/x files caused by not allowing the renderer to flush destroy resource commands before emptying an array. #jira UE-40911 Change 3266027 on 2017/01/20 by Ian.Fox #OnlineSubsystemLive - Fix CreateSession and FindSession each permanently failing after first failure. Duplicates CL 3262175 #jira UE-39110 Change 3265906 on 2017/01/20 by Marcus.Wassmer Fix GPU particle AFR flickering and optimize injection transfers. Duplicate CL's 3260302, 3261252, 3265662, 3265678 #jira UE-40915 Change 3265873 on 2017/01/20 by Mark.Satterthwaite Duplicate CL #3262535: Make sure to set rasterizer state when rendering with a material in FSlateRHIRenderingPolicy::DrawElements #jira UE-40842 Change 3265857 on 2017/01/20 by Jamie.Dale Fixed font pathing issue that could happen in an out-of-source packaged build #jira UE-40855 Change 3265675 on 2017/01/20 by Matt.Kuhlenschmidt Move Dirt Mask Intensity to the correct post process category #jira UE-40851 Change 3265674 on 2017/01/20 by Rolando.Caloca UE4.15 - Revert #jira UE-40633 Change 3265647 on 2017/01/20 by Mitchell.Wilson Updating spawn location of the player pawn after unpossessing character in example 1.10. #jira UE-40870 Change 3265612 on 2017/01/20 by Alexis.Matte Prevent name clash warning when doing automation test #jira UE-40788 Change 3265553 on 2017/01/20 by Matthew.Griffin Fixed Shadow variable warning Change 3265366 on 2017/01/20 by Dmitriy.Dyomin Fixed: Vulkan crashes on Adreno Galaxy S7 #jira UE-40840 Change 3265294 on 2017/01/19 by Dmitriy.Dyomin Fixed typo which was causing assert on mobile #jira UE-40633 Change 3265111 on 2017/01/19 by Rolando.Caloca UE4.15 - Fix for scene color crash #jira UE-40633 Change 3264789 on 2017/01/19 by Josh.Adams - Redoing a fix from Dev-Plat for UI_BUILD_SHIPPING_EDITOR #jira UE-40798 Change 3264780 on 2017/01/19 by Rolando.Caloca UE4.15 - Add Morph compute GPU stat #jira UE-40891 Change 3264486 on 2017/01/19 by Mark.Satterthwaite Fix the crash on startup on Intel GPUs - this is due to Intel Metal forcing SM4 to avoid some drivers bugs in SM5 but I got the condition for initialisation in FMinimalDummyForwardLightingResources wrong so it's attempting to create a RWBuffer for SM4 which won't work. #jira UE-40863 Change 3264427 on 2017/01/19 by Rolando.Caloca UE4.15 - Track down crash #jira UE-40633 Change 3264393 on 2017/01/19 by Aaron.McLeran #jira UE-40850 Re-fixing UE-39650 again in 4.15. I hope this bug doesn't regress yet again! Change 3264364 on 2017/01/19 by Daniel.Wright In forward shading SceneCaptureSource modes Normal and BaseColor are replaced with SceneColorHDR as the GBuffer is not available. This is a silent failure for now as there's no good content error reporting mechanism for scene captures. #jira UE-39658 Change 3264284 on 2017/01/19 by Mark.Satterthwaite Duplicate CL #3264251: Modify some asserts in MetalRHI - technically using a store-action of ENoAction on Stencil buffers should make it invalid to restart a render-pass but on Mac it will work because ENoAction won't invalidate anything written. In future we need to use deferred store-actions in Metal so that we can "restart" passes while enforcing correct Load/Store actions. #jira UE-40803 Change 3264282 on 2017/01/19 by Benn.Gallagher CIS fix, bad expression that failed to compile Mac #jira UE-40716 Change 3264257 on 2017/01/19 by Mike.Beach Revising fix in UBlueprint::BeginCacheForCookedPlatformData(), saving off nativization data if the -nativizeAssets param is present (not just if it was enabled in packaging settings). #jira UE-40620 Change 3264242 on 2017/01/19 by Daniel.Wright [Copy] Sharing IndirectLightingCacheTextureSampler samplers #jira UE-40727 Change 3264191 on 2017/01/19 by Ori.Cohen Fix heightfield not working with traces underneath. #JIRA UE-39819 Change 3264139 on 2017/01/19 by Benn.Gallagher Removed collision between clothing in external skeletal mesh components, as clothing simulations could already be in flight and editing collisions while the simulation is running is not supported by APEX #jira UE-40716 Change 3264110 on 2017/01/19 by Max.Preussner MfMedia: Disabled plug-in on Windows 10, because it is currently broken #jira UE-406344 Change 3264108 on 2017/01/19 by Max.Preussner MfMedia: Fixed compile errors on Windows 10 #jira UE-40644 Change 3264099 on 2017/01/19 by Jamie.Dale Adding deprecation warning for 4.14 style PO export #jira UE-40592 Change 3264089 on 2017/01/19 by Matthew.Griffin Reworked DDC commandlet to make sure it actually calls BeginCacheForCookedPlatformData on assets Skip doing this for Engine content if -ProjectOnly is set as that takes a long time and isn't necessary for the way we use it #jira UE-39968 Change 3264065 on 2017/01/19 by James.Golding Fix ModifyCurve node not calling init/update in SourcePose #jira UE-40852 Change 3263729 on 2017/01/19 by Alexis.Matte Fix a bad condition when filling the material sorting array #jira UE-40814 Change 3263704 on 2017/01/19 by Jack.Porter Fix compile error in AndroidESDeferredOpenGL.cpp when " ES Deferred Shading Renderer" is enabled. #jira UE-40659 Change 3263627 on 2017/01/19 by Jack.Porter Fixed black textures when Vulkan is packaged for ETC1 #jira UE-40658 Change 3263554 on 2017/01/19 by Jack.Porter Fixes to HISMC LOD to use new screen size calculation. Solves issue where HISMC was always rendered at lowest LOD. #jira UE-38930 Change 3263535 on 2017/01/19 by Matthew.Griffin Removed unnecessary directories to always cook Problem was actually down to string asset references not being resolved in file set generation Change 3263534 on 2017/01/19 by Matthew.Griffin Added -SkipPublish parameter to BuildLauncherSample command so that we don't chunk and post preflights Change 3263267 on 2017/01/18 by Dan.Oconnor Fix for editing of TMap/TSet variables in structure editor, async tasks, and when using UK2Node_CommutativeAssociativeBinaryOperator. #jira UE-40428 Change 3263219 on 2017/01/18 by Dan.Oconnor Fix copy paste error found by UDN user Craig.Wright that could result in fatal bytecode execution #jira UE-19425 Change 3262980 on 2017/01/18 by Maciej.Mroz #jira UE-40394, UE-40395, UE-40426, UE-40484, UE-40770 Integrated cl 3262851, 3261613, 3260908 from Dev-Blueprint Change 3262908 on 2017/01/18 by Ori.Cohen When refreshing physics assets, don't do so on components that have no bodies. #JIRA UE-40764 Change 3262709 on 2017/01/18 by Matt.Kuhlenschmidt Fix a crash if a background blur widget ends up being negative or zero sized #jira UE-40820 Change 3262606 on 2017/01/18 by Marc.Audy Don't bother the user with force feedback based on where the unpossessed pawn is standing in the world while in simulate mode #jira UE-40785 Change 3262416 on 2017/01/18 by Marc.Audy Reenable audio threading #jira UE-00000 Change 3262125 on 2017/01/18 by Chris.Wood Fixed unnecessary truncate in SMenuAnchor::Tick that caused menu placement to wobble [UE-40293] - Dropdown selection box jitters when mouse is moved over top of it on Mac #jira UE-40293 Change 3262103 on 2017/01/18 by Jamie.Dale Merging some cooker fixes CL# 3262089 - Fixing RedirectCollector issues with projects outside the UE4 directory CL# 3262091 - Guarding against potentially invalid call to FString::Mid CL# 3262094 - Cook on the fly builds now resolve string asset references #jira UE-40790 Change 3262082 on 2017/01/18 by Chris.Bunner Accumulate used particle materials from final mesh material module, not first. #jira UE-39953 Change 3261996 on 2017/01/18 by Matthew.Griffin Allow Samples to be built in pre-flights if you are specifying an engine version Change 3261995 on 2017/01/18 by Matthew.Griffin Resolve string asset references after loading packages to ensure that we find all required files Change 3261934 on 2017/01/18 by Allan.Bentham Bump shader version to force changes in 3260307 to occur. #jira UE-39701 Change 3261842 on 2017/01/18 by Graeme.Thornton Manual copy of CL 3253580 from Dev-Core Added some validation of the class index in exportmap entries #jira UE-37873 Change 3261017 on 2017/01/17 by Mitchell.Wilson Resaving all levels to resolve short form string asset reference warnings. #jira UE-40732 Change 3260918 on 2017/01/17 by Andrew.Rodham Sequencer: Request unloaded levels to be loaded when being made visible through sequencer #jira UE-40082 Change 3260909 on 2017/01/17 by Ben.Marsh Fix error running "Clean" in installed build. #jira UE-40751 Change 3260757 on 2017/01/17 by Jeff.Fisher UE-39654 Crash when launching Google VR project -Via SwitchGameWindowToUseGameViewport we get an early ResizeViewport which does an early Draw. This calls GetStereoProjectionMatrix before the game has ticked and fetched the device info we use to build that matrix. -In this change we make the call to setup that information in the GoogleVRHMD constructor, to ensure it is done before anything tries to use it. -I also added some asserts. #jira UE-39654 #review-3260644 Change 3260637 on 2017/01/17 by Alexis.Matte Fix crash when importing skeletal mesh containing a texture or a material using the same name. #jira UE-40538 Change 3260630 on 2017/01/17 by Marc.Audy When installing a feature pack maintain the include of the template so that any properties inside it are not lost by replacing it with the project's PCH include Update all C++ feature packs to include the original project .h in the files that are copied in to the new project #jira UE-40730 Change 3260600 on 2017/01/17 by matt.barnes Test content for sequencer event tracks #jira UE-29618 Change 3260593 on 2017/01/17 by Mieszko.Zielinski Made FSupportedAreaData export as part of engine API #UE4 #jira UE-40739 Change 3260538 on 2017/01/17 by Marc.Audy Always display axes in debug info, but show -- for value when we don't yet know the ranges #jira UE-40700 Change 3260422 on 2017/01/17 by Marc.Audy Expose level streaming incremental unregister component cvars in the engine streaming section of the project settings #jira UE-10109 Change 3260392 on 2017/01/17 by Ben.Woodhouse Duplicated from CL 3260107: Fix FMonitoredProcess to prevent infinite loop in -nothreading mode #jira UE-40717 Change 3260358 on 2017/01/17 by Chris.Bunner Only validate tonemapper LUT input if actually hooked up. #jira UE-40467 Change 3260327 on 2017/01/17 by Frank.Fella PlatformMediaSource - Fix Validate to check all specified media sources, and change GetURL to get the url for the current platform when running uncooked. #jira UE-40709 Change 3260307 on 2017/01/17 by Allan.Bentham Restore metal compiler's shader source serialization code when the shader is to be compiled at runtime. #jira UE-39701 Change 3260276 on 2017/01/17 by Alex.Delesky #jira UE-40276 - Fixing an issue where a Standalone game launched from the editor cannot toggle fullscreen mode. Change 3260274 on 2017/01/17 by Chris.Wood Added check for null World ptr in AActor::PostEditChangeProperty to fix crash when pasting temporary Actors [UE-40492] - Crash after ejecting from PIE session and selecting a component in the details panel #jira UE-40492 Change 3260230 on 2017/01/17 by Ben.Woodhouse Duplicated from dev-rendering@3232283 D3D12 - downgrade root signature size warning to a log following a discussion with Microsoft. There's not much we can actually do about it, and it's not relevant to all hardware #jira UE-36999 Change 3260096 on 2017/01/17 by Thomas.Sarkanen Fixed crash when rendering out a level sequence with layered animations When a level contained sequences with layered animations that *werent* taking part in the render (i.e. they were not part of the current master sequence) then their instances were initialized but not ticked. When their components then got a call to evaluate their bone transforms, the cached blends were in an uninitialized state. #jira UE-40654 - Render Movie using separate process crashes capture process Change 3259875 on 2017/01/17 by Dmitriy.Dyomin Fixed: SunTemple is washed out in one color on some Android devices #jira UE-40689 Change 3259011 on 2017/01/16 by Max.Chen Matinee to Level Sequence: Make RegisterTrackConverters pure virtual #jira UE-37328 Change 3258992 on 2017/01/16 by Rolando.Caloca UE4.15 - Integrate fix for outlines (3258807) #jira UE-40690 Change 3258949 on 2017/01/16 by mason.seay Disabled TranslatedMass test #jira UE-29618 Change 3258860 on 2017/01/16 by Max.Preussner Media: Prevent loading of media plug-ins in console apps, such as game servers (OR-34819) #jira OR-34819 Change 3258846 on 2017/01/16 by Max.Preussner MfMedia: Fixed incorrect tracks being played in multi-track media sources (UE-39703) #jira UE-39703 Change 3258813 on 2017/01/16 by Benn.Gallagher Added error on import for APEX clothing files that either have no submeshes or have no submeshes with simulated vertices. #jira UE-40614 Change 3258771 on 2017/01/16 by James.Golding Skip fatal warning in UBodySetup::Serialize if duplicating (e.g. spawning component via SCS with a BodySetup in its template) #jira UE-40418 Change 3258747 on 2017/01/16 by Max.Chen Sequencer: AddUnique SequencerActorTag to prevent multiple tags being added when spawning/despawning. #jira UE-40665 Change 3258630 on 2017/01/16 by Jurre.deBaare CIS IfDef issue fix #JIRA UE-1234 Change 3258541 on 2017/01/16 by Phillip.Kavan [UE-40131] Revised fix that will work for "inclusive" BP nativization with data-only BPs. change summary: - revised code in UBlueprint::BeginCacheForCookedPlatformData() to also support the "inclusive" nativization method #jira UE-40131 Change 3258532 on 2017/01/16 by Max.Chen Sequencer: Fix max row index off by one error . This was always incorrect, but it was masked by the fact that FixRowIndices() was called on the track when the UI gets built. That function was removed from the node layer in CL #3252753 and therefore exposed this bug. #jira UE-40642 Change 3258505 on 2017/01/16 by Marc.Audy Improve messaging when installing vehicle and vehicle adv C++ feature packs #jira UE-40647 Change 3258478 on 2017/01/16 by Matt.Kuhlenschmidt PR #3131: UE-40567: Added nullcheck to FSplinePointDetails (Contributed by projectgheist) #jira UE-40567 Change 3258457 on 2017/01/16 by Jurre.deBaare SpeedTree Billboards rendering with Incorrect Material #fix Ensure that we add a section info entry for the billboard models/lods during SpeedTree importing #jira UE-39677 Change 3258442 on 2017/01/16 by Alexis.Matte Skeletalmesh import, make sure we increment the lod index when animation is not imported #jira UE-40640 Change 3258431 on 2017/01/16 by Jurre.deBaare Back out changelist 3258392 #fix issue was already resolved #jira UE-1234 Change 3258392 on 2017/01/16 by Jurre.deBaare Fix for non-unity CIS #JIRA UE-1234 Change 3258358 on 2017/01/16 by Matthew.Griffin Prevent warning from being shown when XMPP module is not built #jira UE-40616 (I guess LoadModule could be changed to LoadModuleChecked now if they do exist) Change 3258144 on 2017/01/15 by Marc.Audy Fix non-unity CIS errors #jira UE-00000 Change 3258141 on 2017/01/15 by zachary.wilson Adding testing content for Distance Field Indirect Shadows #jira UE-29618 Change 3258049 on 2017/01/14 by Nick.Shin UFE sent incorrect header data on missing file also, it seems that UFE was written to expect clients to close the connection -- (this should be closed manually -- which will flush the data and then close out the socket -- but, since this is a developer tool... leaving this as-is) first, 404 was not sending the required double newline after headers second, since connection are not closed manually (server side) send a dummy payload with content-length data #jira UE-39992 Quicklaunch UFE HTML5 fails with "NS_ERROR_Failure" Change 3257984 on 2017/01/14 by Aaron.McLeran Attempting another fix for static analysis warning in CIS #jira UE-40645 Change 3257904 on 2017/01/14 by Aaron.McLeran Resolving static analysis warnings reported by CIS #jira UE-40645 Change 3257883 on 2017/01/14 by Aaron.McLeran Fixing build warning with CL 3257826 #jira UE-40645 Change 3257826 on 2017/01/13 by Aaron.McLeran Integrating fixes from Dev-Framework and Odin to Release-415 #jira UE-40645 Change 3257654 on 2017/01/13 by Marc.Audy Until plugins can drive their own dependencies vehicle and vehicle adv feature packs will not compile automatically and will pop up a message log informing the user of the actions they need to manually take. #jira UE-40466 Change 3257608 on 2017/01/13 by John.Pollard PC: Assertion Fail with UPackageMapClient::AddNetFieldExportGroup() viewing replays #jira OR-34522 Change 3257489 on 2017/01/13 by Mitchell.Wilson Removing preview mesh from multiple materials to resolve CIS warnings. #jira UE-40628 Change 3257485 on 2017/01/13 by Chris.Babcock Don't initialize FMinimalDummyForwardLightingResources for unneeded feature levels (below SM4) #jira UE-40602 #ue4 #android Change 3257444 on 2017/01/13 by Matt.Barnes Updating test assets for UEQATC-2967 #jira UE-29618 Change 3257324 on 2017/01/13 by Arciel.Rekman Linux: Update runtime CEF lib as well (UE-401413). - Followup to CL 3256081. #jira UE-40413 (Merging CL 3257241 from Dev-Platform to Release-4.15) Change 3257140 on 2017/01/13 by Lina.Halper Fix crash with deleting all poses #jira: UE-40537 Change 3257066 on 2017/01/13 by Jurre.deBaare CIS fix for game builds #jira UE-1234 Change 3257056 on 2017/01/13 by Ben.Zeigler #jira UE-40318 Fix crash in streamablemanager where callbacks would get called on a deleted manager. This is being rewritten in 4.16, so do a quick fix for 4.15 to avoid the crash Change 3256839 on 2017/01/13 by Jurre.deBaare Added conversion of HLOD transition screen size to new transition screen area values #fix During serialization patch up the values of transition screen size within the hierarchical lod setups #misc Updated the default value to a screen size to screen area equivalent #JIRA UE-40518 Change 3256761 on 2017/01/13 by Mieszko.Zielinski Fixed EQS debug rendering not clearing previously displayed labels if new request has no labels #UE4 #jira UE-40589 Change 3256177 on 2017/01/12 by Josh.Adams - Moved the MfMedia plugin outside of XboxOne directory, because it's a Windows plugin as well (that happens to also work on XboxOne - all public APIs) #jira UE-40391 Change 3256131 on 2017/01/12 by Jamie.Dale Fixing log spam when trying to load an empty font data #jira UE-40555 Change 3256081 on 2017/01/12 by Arciel.Rekman Fixed CEF compatibility problems on Ubuntu 14.04 (UE-40413). - Also deleted Debug version of it. - Change by yaakuro. #jira UE-40413 (Edigrating CL 3256065 from Dev-Platform to Release-4.15) Change 3256046 on 2017/01/12 by Jon.Nabozny Use PxConvexFlag::eSHIFT_VERTICES when cooking meshes to fix baked in transforms. #jira UE-39212 Change 3255939 on 2017/01/12 by mason.seay Rebuilt lighting #jira UE-29618 Change 3255912 on 2017/01/12 by Olaf.Piesche Replicating fix from 3246828 for #jira UE-39249 Change 3255909 on 2017/01/12 by Rolando.Caloca UE4.15 - Support for choosing discrete AMD GPU #jira UE-40546 Change 3255835 on 2017/01/12 by Martin.Wilson Fix newly added virtual bones not being on screen. #jira UE-40516 Change 3255774 on 2017/01/12 by Mark.Satterthwaite Merging 3251926 for Richard.Wallis: #jira UE-38828 Crash after Enabling Forward Shading on Mac and Creating/Editing Materials. Using TGlobalResource to avoid constant resource allocation. Prev fix (in CL 3239454) caused a crash in D3D11 with zero sized resource views. Change 3255771 on 2017/01/12 by Alexis.Matte Fix a crash when re-importing asset with no material #jira UE-40510 Change 3255746 on 2017/01/12 by Jon.Nabozny Change _DEBUG to PX_DEBUG in ConvexHullLib.cpp #jira UE-0000 Change 3255659 on 2017/01/12 by Jon.Nabozny Enable Shifting Vertices during Convex Hull cooking to prevent precision issues. (Copied CL-3249100 from Dev-Phyics-Upgrade to support new flag) #jira UE-39212 Change3255617on 2017/01/12 by Ori.Cohen Fix crash when computing mass for an async object. Using passed in rigid body instead of assuming SyncRigidActor #JIRA UE-40458 Change 3255536 on 2017/01/12 by Jamie.Dale Fixed crash when using an object picker against the 'Object' type This also optimizes some filter code to avoid filtering when it would be pointless (and just slows things down). #jira UE-40408 Change 3255451 on 2017/01/12 by Chris.Wood Fixed read only text color in SCommentBubble [UE-40384] - Reference Viewer comment text is difficult to read Also changed DetermineForegroundColor() method in EditableTextBox classes to fallback on ForegroundColorOverride if it is set and ReadOnlyForegroundColorOverride isn't set. #jira UE-40384 Change 3255448 on 2017/01/12 by Chris.Wood Removed blinking cursor/caret on read only editable text layouts. [UE-40502] - Flashing cursor/caret showing in read-only editable text layouts #jira UE-40502 Change 3255445 on 2017/01/12 by Marc.Audy Create the dynamic level streaming persistent object correctly outered to the World rather than the transient package to avoid GetWorld() crashing #jira UE-00000 Change 3255441 on 2017/01/12 by Jon.Nabozny Regenerate collision for the basic Cube mesh to fix resting issues and invalid verts. #jira UE-40478 Change 3255407 on 2017/01/12 by Yannick.Lange VREditor: - Fix: Assertion Failed crash after pressing F8 in PIE while Foliage Mode was selected - Fix: Assertion Failed crash after pressing F8 in PIE while Paint Mode was selected - Added extra checks for other possible future cases #jira UE-39786 UE-39789 Change 3255393 on 2017/01/12 by Chris.Bunner Duplicating CL 3255244: Removed test variable from MaterialExpressionVectorParameter. #jira UE-40517 Change 3255375 on 2017/01/12 by Steve.Robb CIS fix. #jira UE-39556 Change 3255334 on 2017/01/12 by samuel.proctor Corrected QA Container asset to remove pin warning. #jira UE-29618 Change 3255319 on 2017/01/12 by james.cobbett Fixing motion blur issue with test content for Pose Snapshots. #jira UE-29618 Change 3255247 on 2017/01/12 by Nick.Darnell Slate - Slate's Tab Manager is now a bit smarter about allowing Focus/BringToFront attention grabbing methods. In order to make the UI less jumpy it was restricted to only allowing alerts and bring to front to be triggered if you were on the window, or child window of the active application window. That can negatively impact cases where a user takes an action (clicks a link ro button saying open/goto this tab), that is on another window. To work around this limitation, the Tab Manager will also permit the action if Slate is currently processing user input, implying that the action being taken is in direct response to the user pressing a button and interacting with the UI. #jira UE-40313 Change 3255236 on 2017/01/12 by Phillip.Kavan [UE-40131] Non-native child BPs can now properly override a nativized parent BP's components in a cooked build with exclusive Blueprint class nativiation. - Mirrored from //UE4/Dev-Blueprints (CL# 3254024,3254391) #jira UE-40131 Change 3255216 on 2017/01/12 by Rolando.Caloca UE4.15 - Fix compile issue on Vulkan 1.0.37.0 or newer #jira UE-40506 Change 3255206 on 2017/01/12 by Steve.Robb Use outer walking IsA() implementation in editor to get around reinstancing and hot reload issues. #fyi mike.beach #jira UE-39556 Change 3255195 on 2017/01/12 by mason.seay Adjusted slope to fix platform discrepancy #jira UE-29618 Change 3255086 on 2017/01/12 by Jack.Porter Fix XboxOneShaderCompiler.cpp non-unity compilation #jira None Change 3255085 on 2017/01/12 by Jack.Porter Missing HTML5 changes from CL 3254907 #jira UE-39111 Change 3255031 on 2017/01/12 by Jack.Porter More iOS GoogleVR changes missing from CL 3254907 #jira UE-39111 Change 3254991 on 2017/01/12 by Jack.Porter Missing file from CL 3254907 #jira UE-39111 Change 3254907 on 2017/01/11 by Jack.Porter Android MSAA changes - use r.MobileMSAA cvar, support more than 2x, fix issues where targets other than scene color were created with MSAA #jira UE-39111 #jira UE-35849 #jira UEMOB-35 Change 3254810 on 2017/01/11 by Arciel.Rekman Linux: fix for crash on exit (UE-40488). #jira UE-40488 Change 3254617 on 2017/01/11 by Peter.Sauerbrei remake the fix for missing PhysXVehicle library in binary for IOS and TVOS #jira UE-39349 Change 3254489 on 2017/01/11 by mason.seay Other minor improvements to the map #jira UE-29618 Change 3254477 on 2017/01/11 by mason.seay Map tweaks to prevent the vehicle from getting stuck #jira UE-29618 Change 3254431 on 2017/01/11 by Mitchell.Wilson Rebuilt lighting on all StarterContent levels. #jira UE-40468 Change 3254333 on 2017/01/11 by mason.seay Adjusted lightmap on mesh to remove odd rendering splotches #jira UE-29618 Change 3254131 on 2017/01/11 by Rolando.Caloca UE4.15 - Missing dumped shaders #jira UE-40465 Change 3254126 on 2017/01/11 by Jeff.Fisher UE-40422 Vive Motion Controllers unable to Play Haptic Effect -Removed an unnecessary remapping of controllerindex to deviceid, they are the same now. #jira UE-40422 #review-3254084 Change 3254046 on 2017/01/11 by Mark.Satterthwaite Merging 3233811: Fix compiling QA-Material tessellation shaders that don't need to emit from Hull or sample in Domain the HSOut buffer which was confusing MetalBackend. #jira UE-39935 Change 3254021 on 2017/01/11 by james.cobbett Test content for Pose Snapshot testing #jira UE-29618 Change 3253993 on 2017/01/11 by Alexis.Matte Fix the morph target import #jira UE-40424 Change 3253948 on 2017/01/11 by mason.seay Fixed Level BP logic that was causing Access None error #jira UE-29618 Change 3253884 on 2017/01/11 by mason.seay Updated mesh colors on map. Disabled motion blur #jira UE-29618 Change 3253862 on 2017/01/11 by mason.seay Disabled Always Show Mobile Input (turned on by accident) #jira UE-29618 Change 3253859 on 2017/01/11 by Mark.Satterthwaite Merging 3252866: Fix Metal shader pipeline hash collisions caused by deferring MTLFunction construction until PrepareToDraw so that we may use Function-Constants to specialise the shader source without generating additional permutations. This is required to generate proper tessellation shaders which are specialised against the index-buffer usage & type (none, uint16, uint32). While we're here amend the hash functions to make better use of the existing hash functions to improve the distribution and hopefully reduce the possibility of collisions in future. #jira UE-40357 Change 3253854 on 2017/01/11 by Mark.Satterthwaite Merging 3252859: Fix the calculation of Metal tessellation struct alignment and size to use largest member size, so that we don't assert in debug or cause out-of-bounds access in development/shipping. #jira UE-40410 Change 3253853 on 2017/01/11 by Mark.Satterthwaite Merging 3237394: Add Metal-specific permutations of TBasePassHS - they affect the C++ definition on all platforms but are only cached or used on Metal - because the way we compile the combined VS+HS tessellation stage requires that the combined VS + HS HLSL code references the same resources, otherwise we get incorrect resouce bindings and subsequently fail to render properly. Long-term the Metal tessellation code will need to be refactored so that the vertex shader stage is emitted as a separate shader from the hull shader stage as this but will keep cropping back up and continue to complicate the engine. #jira UE-39799 Change 3253852 on 2017/01/11 by Mark.Satterthwaite Merging 3236850: Make changing the Metal Shader Version project setting prompt the user to restart for the changes to take effect. #jira UE-39801 Change 3253834 on 2017/01/11 by mason.seay Updated mobile input textures to be power of two #jira UE-29618 Change 3253807 on 2017/01/11 by Mark.Satterthwaite Merging 3232641 & 3236788 & 3233854 & 3249742 from Dev-Rendering: 3232641: - Eliminate redundant state changes in MetalRHI in the state cache. - Add a new debug level for setting buffers to nil prior to calls to set*Bytes so that the tool doesn't display incorrect data. - Make testing for validation & statistics features use the same EMetalFeatures API as everything else for consistency. - Cache the fallback depth-stencil texture in the state cache and ignore it for determining whether a pass can restart - if we are using this texture its contents are worthless anyway. 3236788: Fix 10.11.6 support (aka -nometalv2): the stencil view workaround necessitates a mid-render blit and the way things were setup resulted in the HasValidRenderTargets assert firing. Refactored the code to separate the concept or valid render-states in the cache from active render-states in the render-pass. Now it works as intended and will be needed for 4.15. 3233854: More information about texture type validation errors in Metal. 3249742: Fix missing GPU particles on Mac. Pointers getting reused is causing the blendstate equality operator to fail. Simple workaround until we have time for a proper fix. #jira UE-40200 Change 3253636 on 2017/01/11 by Chris.Wood Improved tracking of runtime and debugger attachment for analytics purposes. [UE-39780] - Change IsDebugger to WasDebuggerPresent in all crash/AS analytics [UE-39777] - Update MTBF IsDebugger state for every heartbeat [UE-39778] - UnrealWatchdog to send WasDebuggerPresent state for app if set [UE-39779] - UnrealWatchdog to send total run time of process Debugger state was previously read once at startup or once at the time of an event. Debugger is now checked during the heartbeat and doesn't reset flag when detached so we know if a session was ever debugged. Also reporting total run time in UnrealWatchdog. Watchdog still doesn't run when debugging but and will never show popups to a debugger user even when forced on with -forcewatchdog. #jira UE-39780, UE-39777, UE-39778, UE-39779 Change 3253281 on 2017/01/10 by Dan.Oconnor Typo fix caused parameter in local struct definition to shadow the local #jira UE-40027 Change 3253231 on 2017/01/10 by Dan.Oconnor Mirror of 3253220 These pins should infer together #jira UE-40427 Change 3253125 on 2017/01/10 by Uriel.Doyon Brought back CL 3242117 and 3238685, which got lost on the way: - Fix for possiblel check fail when changin mobility of actors. - Fix for possible check fail when processing streaming data. #jira UE-39996 Change 3252936 on 2017/01/10 by Marc.Audy CopyPropertiesForUnrelatedObjects needs to consider path not just name of subobjects when matching them up to copy properties and update references Ensure that a reinstanced child actor component ends up pointing at the correct child actor template #jira UE-40027 Change 3252886 on 2017/01/10 by Lina.Halper Fix for invalid AnimCurves when curve is added while running #jira: UE-39826 Change 3252753 on 2017/01/10 by Frank.Fella Sequencer - Change track rows to use separate track nodes in the display node tree, fixes key edit issues on animation and audio tracks. #jira UE-39836 Change 3252640 on 2017/01/10 by Lukasz.Furman fixed NavCollision losing user settings after any property change copy of 3252628 #jira UE-40388 Change 3252614 on 2017/01/10 by Daniel.Wright UStaticMeshComponent::InvalidateLightingCacheDetailed uses MarkRenderStateDirty. Massively speeds up duplication of HISMC with many instances (10+ minutes -> seconds), as InvalidateLightingCacheDetailed gets called for every instance. #jira UE-40406 Change 3252609 on 2017/01/10 by mason.seay Updated map with text actors for more visual clarity #jira UE-29618 Change 3252477 on 2017/01/10 by Daniel.Wright [Copy] Fixed race condition with FPrecomputedLightVolume::Data which was exposed when switching lighting scenarios #jira UE-39852 Change 3252451 on 2017/01/10 by Daniel.Wright Garbage collection calls UWorld>SendAllEndOfFrameUpdates() on all loaded worlds first so that deferred recreate render states happen before any UObjects are deleted * Fixes rendering thread crashes in the order of events of 1) SetMaterial 2) GC 3) Rendering command that dereferences the UMaterial #jira UE-30089 Change 3252418 on 2017/01/10 by Ben.Zeigler #jira UE-40390 Fix crash saving blueprint with an inherited DataTable/CurveTable reference. Delta serialization meant that the necessary name wasn't in the name table, so adding it manually now. Change 3252410 on 2017/01/10 by Max.Chen Sequencer : Filter sections on select in range Copy from Dev-Sequencer #jira UE-37854 Change3252385on 2017/01/10 by Max.Chen Sequencer: Update auto tangents when setting key time. This fixes a bug where dragging keys with auto tangents doesn't recompute tangents properly. #jira UE-39923 Change 3252360 on 2017/01/10 by Allan.Bentham Remove incorrect assert for iOS. #jira UE-40385 Change 3252297 on 2017/01/10 by mason.seay Test assets for suspending cloth simulation #jira UE-29618 Change 3252125 on 2017/01/10 by Mieszko.Zielinski Fallout fix after removal of BlackboardKeyUtils::CalculateComparisonResult declaration from the AIModule #UE4 #jira UE-40099 Change 3251987 on 2017/01/10 by Allan.Bentham Fix HQ DoF #jira UE-35548 Change 3251856 on 2017/01/10 by Jack.Porter Fixed Get Instances Overlapping Box blueprint function due to issue with FBox constructor. Added MakeBox and MakeBox2D kismet native functions Fixed box overlap test ignoring instance scale #jira UE-34409 Change3251519on 2017/01/09 by Daniel.Wright [Copy] Fixed GLandscapeLayerUsageMaterial getting GC'ed #jira UE-40055 Change 3251146 on 2017/01/09 by Lina.Halper Fix on stable track data carrying over to pose asset - decided to clean up track data in anim sequence since we don't really need that data anymore #jira: UE-40351 #code review: Martin.Wilson Change 3251056 on 2017/01/09 by Lina.Halper fixed crash when pose node contains stale data when updating source. #jira: UE-40258 #code review; Thomas.Sarkanen Change 3251035 on 2017/01/09 by Mitchell.Wilson Removed preview mesh in M_GodRay to resolve CIS warning. Relinked textures used in two materials to resolve CIS warnings. #jira UE-40350 Change 3250959 on 2017/01/09 by Mitchell.Wilson Updating master sequence playback end time so the final audio track can be heard. Updating multiple shots to resolve issues with audio not playing back properly. #jira UE-40321 UE-40335 Change 3250896 on 2017/01/09 by Andrew.Rodham Sequencer: Fixed level visibility not working in PIE #jira UE-40082 Change 3250895 on 2017/01/09 by Andrew.Rodham Sequencer: Fixed evaluation of overlapping audio and skeletal aninmation sections - Audio and skeletal animation sections now continue to support legacy evaluation order. Overlapping sections of the same priority on the same row will be filtered out such that only the section with the latest start time will be evaluated. #jira UE-40320 Change 3250830 on 2017/01/09 by Ben.Woodhouse Duplicated from //ue4/Release-4.14 CL 3238182 Disable timestamp queries on pre-Maxwell nvidia hardware. Local testing suggests that this is the major cause of instability in the UE4.14 release. It's possible that we could be more targeted by only excluding Fermi and older hardware, but identifying fermi hardware by device ID is difficult in practice, since the range overlaps with Kepler. #jira UE-38818 Change 3250790 on 2017/01/09 by Lauren.Ridge Fixing backspace on VR Editor numberpad menu. #jira UE-39770 Change 3250681 on 2017/01/09 by Ben.Woodhouse Duplicated from dev-rendering@3249296: XB1/Fast semantics: Add missing L1/L2 cache flush on transition to readable (or RW). The missing cache flush was causing indeterminism when reading from a texture shortly after writing to it as a render target. This fixes bloom and diffuse irradiance issues The bug has been there for a while, but CL 3227787 (drawclear early out) caused it to manifest #jira UE-39727 #jira UE-40238 Change 3250680 on 2017/01/09 by Ben.Woodhouse Duplicated from dev-rendering@3238664 Fix dbuffer decal rendering issues in fullscreen on PC. Also fixes crash in editor when viewing dbuffer materials. Pass clearcolor in RT params for system textures to workaround a bug with ClearColorTexture not working in fullscreen mode on DX11. Make sure dbuffer targets are bound if we're rendering mesh decals #jira UT-6891 #jira UE-39842 #jira UE-39949 Change 3250609 on 2017/01/09 by Steve.Robb Maximum number of stats-using threads increased to 512. #jira UE-38153 Change 3250604 on 2017/01/09 by Andrew.Rodham Sequencer: Fixed incorrect seed being used when generating new animation type IDs for object properties #jira UE-40327 Change 3250589 on 2017/01/09 by Matthew.Griffin Changed publish symbols node to use runtime dependencies instead of manually including the whole PhysX folder Avoids unused configs and VS2013 files #jira UE-39171 Change 3250578 on 2017/01/09 by Matthew.Griffin Removed art tools from released build now that they are available separately on the Marketplace Change 3250282 on 2017/01/07 by Mieszko.Zielinski Fixed UNavigationSystem::bNavigationAutoUpdateEnabled getting ignored by recent addition to related condition in UNavigationSystem #UE4 Reported by UT team. Replication of a fix from Dev-Framework that didn't make it to 4.15 stream #jira UE-40324 Change 3250276 on 2017/01/07 by Mieszko.Zielinski Fixed not being able to add elements to UAIPerceptionStimuliSourceComponent.RegisterAsSourceForSenses for instances manually placed on the map #UE4 #jira UE-31711 Change 3250219 on 2017/01/07 by Mieszko.Zielinski Extended comment to AISenseConfig_Sight::PeripheralVisionAngleDegrees to make it clear how it works #UE4 #jira UE-31731 Change 3250147 on 2017/01/07 by Andrew.Rodham Added missing includes #jira UE-40019 Change 3250096 on 2017/01/06 by Nick.Shin refetch on timed out GET/POST requests correction to: UE_MakeHTTPDataRequest #jira UE-39992 Quicklaunch UFE HTML5 fails with "NS_ERROR_Failure" Change 3249963 on 2017/01/06 by Mieszko.Zielinski removed unused and undefined BlackboardKeyUtils::CalculateComparisonResult #UE4 #jira UE-40099 Change 3249829 on 2017/01/06 by Alexis.Matte turn on the material name clash feature for the content browser importer. #jira UE-40298 Change 3249791 on 2017/01/06 by andrew.porter QAGame: Added level blueprint logic to QA-Sequencer that lets tester override sequence bindings #jira UE-29618 Change 3249755 on 2017/01/06 by Jamie.Dale Some fixes for object reference detection and notification when deleting assets #jira UE-40121 Change 3249727 on 2017/01/06 by James.Golding #jira UE-40242 Change 3249707 on 2017/01/06 by Mitchell.Wilson Removing preview mesh with incorrect path from materials to resolve warnings in CIS. #jira UE-40311 Change 3249543 on 2017/01/06 by Michael.Dupuis #jira UE-40299: validate if UISettings is valid Change 3249506 on 2017/01/06 by Alexis.Matte Make sure we use the correct LodIndex when importing a new LOD in case a previous LOD import fail. #jira UE-40240 Change 3249477 on 2017/01/06 by Ori.Cohen Fix incorrect warning when moving kinematic objects during simulation. #JIRA UE-40290 Change 3249472 on 2017/01/06 by Andrew.Rodham Sequencer: Undo now works as expected when editing the properties of a key #jira UE-40019 Change 3249390 on 2017/01/06 by Mitchell.Wilson Removing preview meshes with improper path from materials to resolve CIS warnings in landscape mountains sample. #jira UE-40300 Change 3249317 on 2017/01/06 by Alexis.Matte Fix a crash when loading skeletalmesh with no section #jira UE-40249 Change3249294on 2017/01/06 by Mitchell.Wilson Updated defaultengine.ini for Match 3 to resolve warnings in CIS. ServerDefaultMap and TransitionMap had invalid paths. #jira UE-40295 Change 3249213 on 2017/01/06 by Chris.Bunner Fixed up logic for windowed/fullscreen output display selection when working with HDR. Now selects the most appropriate display if HDR enabled, else current monitor window is on. FullscreenDisplay commandline functions regardless of HDR support. #jira OR-33525, OR-33536, OR-33540, OR-33520 Change 3249135 on 2017/01/06 by Martin.Wilson Fix root motion issues on additive animations. - Fix scale issue on resetting root bone - Fix loss of root motion when animation is additive. #jira UE-40232 Change 3248522 on 2017/01/05 by Alexis.Matte Fix a crash when reimporting morph target. Also fix a crash when initiating ColorVertexBuffer with NULL value #jira UE-40201 Change 3248271 on 2017/01/05 by Andrew.Rodham Sequencer: Only reset persistent evaluation data when the sequence has changed - This ensures that we don't destroy persistent data that is assumed to still exist (i.e. it was created in ::Setup) from the same sequence #jira UE-40234 Change 3248092 on 2017/01/05 by Ben.Marsh UBT: Remove the [Obsolete] attribute from methods in TargetRules; the [ObsoleteOverride] attribute gives a much better (and more concise) warning with specific instructions on how to resolve it. Change 3248091 on 2017/01/05 by Marcus.Wassmer Tick renderthreadtickables in -onethread to avoid leaks. #jira UE-40248 Change 3248063 on 2017/01/05 by Marc.Audy Route FAudioDevice::StopAllSounds to the audio thread if called on the game thread #jira UE-40243 Change 3247995 on 2017/01/05 by Maciej.Mroz NativizationSummary object is always present. manually merged cl#3247985 from Dev-Blueprints #jira UE-40035 Change 3247873 on 2017/01/05 by Chad.Garyet Adding "Generate QA Labels" buildgraph node and automation script. Port of createNewLabel and createMinimumLabel python scripts into UAT #jira UEB-725 Change 3247855 on 2017/01/05 by Nick.Shin refetch on timed out GET/POST requests #jira UE-39992 Quicklaunch UFE HTML5 fails with "NS_ERROR_Failure" Change 3247737 on 2017/01/05 by Marc.Audy static mesh component instance data now correclty inherits from pritive component instance data instead of skipping it and inheriting directly from scene component instance data #jira UE-40053 Change 3247723 on 2017/01/05 by mason.seay Asset for suspend cloth bug #jira UE-29618 Change 3247708 on 2017/01/05 by Mitchell.Wilson Updating project settings to disable dbuffer decals to resolve rendering issues in Showdown while using -game -vr #jira UE-40195 Change 3247652 on 2017/01/05 by Martin.Wilson Fixes for animation notifies window -Fix notify not being removed from skeleton -Fix crash where editor is not refreshed after notify removal #jira UE-40154 Change3247638on 2017/01/05 by mason.seay Test assets for cloth suspension #jira UE-29618 Change 3247630 on 2017/01/05 by Alexis.Matte Prevent crash when the import fail and we have no staticmesh created #jira UE-40024 Change 3247556 on 2017/01/05 by Ben.Marsh Fix non-unity compile error. Change 3247547 on 2017/01/05 by Jurre.deBaare Crash while using the Delete Button in the HLOD Outliner while a Generated Proxy Mesh is opened in the Static Mesh Editor #fix Unify path for both delete cluster options in the outliner UI #jira UE-40066 Change 3247539 on 2017/01/05 by Benn.Gallagher Fixed serialization crash for simplified skeletal meshes leading to corrupted assets that crash on load after skin weight buffer changes. #jira UE-40199 Change 3247515 on 2017/01/05 by Allan.Bentham Fix inverted planar reflections when mobileLDR Fixed incorrect gamma 2 planar reflection rendering when mobileLDR #jira UE-32868 Change 3247502 on 2017/01/05 by Dmitriy.Dyomin Fixed: Single digit frame rate when sculpting landscape foliage. #jira UE-39532 Change3247232on 2017/01/04 by Ben.Marsh Remove private include from public header. Prevents compiling samples from installed build of the engine without private headers. #jira UE-40135, UE-40137, UE-40139, UE-40140, UE-40141, UE-40142, UE-40143, UE-40144 Change 3247002 on 2017/01/04 by Chris.Babcock Changed Vulkan hitchy pipeline log message verbosity #jira UE-38354 #ue4 #android #dontbackcopy Change 3246927 on 2017/01/04 by matt.barnes Updating QAGame content to facilitate UEQATC-2969 #jira UE-29618 Change 3246894 on 2017/01/04 by Mike.Beach Mirroring CL 3245322 from Dev-BP Fixed a crash when implementing a native interface in a BP #jira UE-40155, UE-40203 Change 3246830 on 2017/01/04 by Chris.Bunner Allow AllocGBuffer call when in simple-forward so dummy uniform buffer creation can occur. #jira UE-39756 Change 3246816 on 2017/01/04 by Jon.Nabozny Fix Anim Notifies Tab not opening in Animation Editor. #JIRA UE-40134 Change 3246804 on 2017/01/04 by Ori.Cohen Touch engine file to trigger re-link. #JIRA UE-40156 Change 3246709 on 2017/01/04 by mason.seay Updated map #jira UE-29618 Change 3246606 on 2017/01/04 by Ori.Cohen Fix for sweeps taking too long time (OR-32839). - Exhaustive investigation uncovered apparent numerical problems in this code (when compiling with clang 3.9.x with -ffast-math). - Current solution can result in overshoot for certain trace extents, but they are not expected to be a practical problem in Unreal. - NVidia is aware and will investigate a better solution. #tests Compiled Linux server with the changed PhysX and continuously ran bot matches for about a day. #JIRA UE-40156 Change 3246571 on 2017/01/04 by Marc.Audy Look at the body instance's desired collision enabled value rather than the primitive component's current collision enabled value when determining whether physics state should be created #jira UE-39994 Change 3246527 on 2017/01/04 by tim.gautier QAGame: BP_MediaPlayer now displays the name of the MediaPlayer plugin currently in use during playback #jira UE-29618 Change 3246480 on 2017/01/04 by mason.seay Map update #jira UE-29618 Change 3246470 on 2017/01/04 by Ori.Cohen Guard against infinitely thin geometry which fixes some nans. This showed up as issues in various projects #JIRA UE-00000 Change 3246413 on 2017/01/04 by Jon.Nabozny Cube asset did not have Tri Meshes. Reimported to fix the issue. -- Copied from 3233164 -- #jira UE-39657 Change 3246388 on 2017/01/04 by Jon.Nabozny Set 'p.MoveIgnoreFirstBlockingOverlap' to be enabled by default (3158732). This causes collision behavior to remain unchanged unless people opt in to the new behavior. -- Copied from 3239735 (bot health fixed by a different CL) -- #jira UE-39387 Change 3246352 on 2017/01/04 by Jon.Nabozny Fix FPredictProjectilePathParams to use a valid default value for TraceChannel. This requires the use of a new bool bTraceWithChannel which is enabled by default. -- Copied from 3239765 -- #JIRA UE-39726 Change 3246341 on 2017/01/04 by Ori.Cohen Allow vehicles to inherit from PawnMovementComponent and only use the pawn/ai capabilities when a Pawn owner is used. #JIRA UE-39508 Change 3246178 on 2017/01/04 by Andrew.Rodham Sequencer: When playback stops naturally, the play position is set to the boundary that caused playback to stop (the end if playing forwards, the start if playing backwards) - This is to reconcile the movie scene sequence player with previous behaviour #jira UE-40076 Change 3246102 on 2017/01/04 by Benn.Gallagher Fixed single threaded physics dispatcher triggering checks from clothing when running with a CPU with two or fewer cores. #jira UE-39811 Change 3246100 on 2017/01/04 by Benn.Gallagher Fixed ensure triggered when using root motion with sub instances Fixed crash reinstancing an active anim class that had subinstances #jira UE-39582 #jira UE-39579 Change 3246092 on 2017/01/04 by Marc.Audy PR #3082: Improve comment for UInputComponent (Contributed by Soleone) #jira UE-40098 Change 3246084 on 2017/01/04 by Matthew.Griffin Remove bad files Change 3246076 on 2017/01/04 by Matt.Kuhlenschmidt Fixed all non-editable text properties having a double disabled effect. The text box is read only which prevents edting but still allows copying text from it. This feature had regressed and the disabled effect on top of the read only effect made it too difficult to see the text. #jira UE-39652 Change 3246043 on 2017/01/04 by Steve.Robb Use of CastChecked instead of Cast in implementations of IStructSerializerBackend::WriteProperty. This is both more efficient and will hopefully make it easier to diagnose the issue. #jira UE-39872 Change3246032on 2017/01/04 by Martin.Wilson Change FindBoneIndex to FindRawBoneIndex (final bone maps are not built until after all adding is done so they will not be found) #jira UE-40105 Change 3246016 on 2017/01/04 by Andrew.Rodham Editor: Insert/Duplicate/Delete menu on array properties now only closes itself on click, rather than all menus - This allows us to edit such properties on context menus #jira UE-39998 Change 3246005 on 2017/01/04 by Thomas.Sarkanen Fixed asset attachment issues in Skeleton Tree Assets were being attached uniquely, so only one asset could be attached to a bone/socket. However the calling code didnt know that the unique attachment function just gave up, so the item just got added to the bottom of the tree. The attachment filter was not set correctly to allow for bone attatchments, so only sockets could be attached to. The attach parent name was not initialized, so assets could not be deleted one at a time. #jira UE-40040 - With multiple Preview assets on one bone, only one appears in Skeleton Tree #jira UE-40041 - Preview assets appear at the bottom of the skeleton tree Change 3246002 on 2017/01/04 by Andrew.Rodham Sequencer: Fixed actor tick prerequisites not getting set up correctly for master sequences #jira UE-39975 Change 3245979 on 2017/01/04 by Andrew.Rodham Sequencer: Fixed scrubbing audio tracks not working propertly #jira UE-40048 Change 3245978 on 2017/01/04 by Andrew.Rodham Sequencer: Fixed dropping a level onto a level visibility section not marking the track as changed, and not correctly creating a transaction #jira UE-39998 Change 3245977 on 2017/01/04 by Andrew.Rodham Sequencer: Fixed crash caused by lingering persistent evaluation data #jira UE-40064 Change 3245971 on 2017/01/04 by Dmitriy.Dyomin Fixed: Using Set World Origin Location will cause the player pawn to stutter #jira UE-40022 Change 3245725 on 2017/01/03 by Matt.Barnes Further improvments on test assets for UEQATC-2963 #jira UE-29618 Change 3245658 on 2017/01/03 by Arciel.Rekman Linux: fix ARM32 build (UE-39913). #jira UE-39913 (Redoing CL 3240982 from Dev-Platform in Release-4.15) Change 3245577 on 2017/01/03 by Mason.Seay More vehicle updates #jira UE-29618 Change 3245556 on 2017/01/03 by Matt.Barnes Updating test content for UEQATC-2963 #jira UEQATC-2963 Change 3245461 on 2017/01/03 by mason.seay Updating Inertia Tensor Scale to improve Vehicle Handling #jira UE-40013 Change 3245442 on 2017/01/03 by Jeff.Fisher UEVR-495 Assert when switching to 2d mode. sceHmdReprojectionStart failing. -There was a race condition between switching output modes on the render thread and sceHmdReprojectionStart on the RHI thread. The flush fixes that. The reprojection would simply have failed that frame previously in shipping which would not matter much as we are switching output modes anyway. #jira UEVR-495 #review-3245374 Change 3245427 on 2017/01/03 by Jeff.Fisher UEVR-456 check if we are using camera before doing camera disconnected dialog on PSVR -If the tracker is active, but we are tracking nothing (ie we have the morpheus hmd tracking plugin, and started up with it, but switched to 2d mode) don't pop up the camera setup warning until we start trying to track something again. -This is useful for apps that have 2d and vr modes. #jira UEVR-456 #review-3245372 Change 3245329 on 2017/01/03 by mason.seay Level and vehicle tweaks #jira UE-29618 Change 3245275 on 2017/01/03 by Chris.Babcock Added EngineVersion to AndroidManfiest.xml metadata #jira UE-40123 #ue4 #android Change 3245235 on 2017/01/03 by Guillaume.Abadie Cherry picks CL 3234813 from Dev-Rendering: Fixes texture mask static lighting when using GBuffer selective outputs. #jira UE-39527 Change 3245183 on 2017/01/03 by Chris.Babcock Added missing #undef LOCTEXT_NAMESPACE to some files (contributed by projectgheist) #jira UE-40103 #PR #3085 #ue4 #android Change 3245120 on 2017/01/03 by mason.seay Missed some assets #jira UE-29618 Change 3245116 on 2017/01/03 by mason.seay Mass fucntional test #jira UE-29618 Change 3245049 on 2017/01/03 by Ben.Marsh PR #3086: Fixed ScriptGeneratorPlugin #includes (Contributed by projectgheist) Change 3244924 on 2017/01/03 by Ben.Zeigler #jira UE-40057 Fix regression in public access for SwapPlayerControllers, from GitHub #3072 Change 3244831 on 2017/01/03 by Mitchell.Wilson Fixed hole in collision around level. #jira UE-39576 Change 3244817 on 2017/01/03 by Matthew.Griffin Change check for files being under engine directory to avoid problems with relative paths #jira UE-40096 Change 3244801 on 2017/01/03 by Andrew.Rodham Editor: Fixed color picker not working when opened from a details panel on a context menu - When a color picker is opened from a details panel that's on a context menu, it now opens as a sub menu - Added the ability to find an open menu from a widget path to FSlateApplication #jira UE-39932 Change 3244776 on 2017/01/03 by Matt.Kuhlenschmidt Fix window handle and device context being accessed by scene viewports after the underlying window has been destroyed by the OS. This is an invalid state on linux and using some vr devices. #jira UE-7388 Change 3244672 on 2017/01/03 by Ben.Marsh Search all directories containing universal CRT installations from the registry, rather than assuming that the first one found will contain the universal CRT version we want to use. Attempt to fix issues described in PR #3059. Change3244668on 2017/01/03 by Thomas.Sarkanen Added "Reimport Animation" and "Export to FBX" to the animation editor toolbar Options were in the asset menu before. #jira UE-39643 - Missing "Reimport" option for animation assets Change 3244667 on 2017/01/03 by Thomas.Sarkanen Reduced default URO distances in-line with new LOD calculations New values should give (roughly) the same effect as the older values with the older system. #jira UE-39939 - URO LOD distance factors different with the new screen size metric Change 3244654 on 2017/01/03 by Matthew.Griffin Added functionality to specify Loading Phase for plugin templates Changed Blueprint Library Template so that it loads pre loading screen and can be linked correctly in blueprints that use it #jira UE-38826 Change 3244631 on 2017/01/03 by Dmitriy.Dyomin Fixed: TM_Landscape_LOD Folder does not Live Update contents after generating LODs with Create Per Package Asset #jira UE-37368 Change3244548on 2017/01/02 by Jack.Porter Fix for Post-process Materials rendering incorrectly in editor mobile preview after viewport is resized #jira UE-39905 Change 3244389 on 2016/12/30 by Phillip.Kavan [UE-39816] Fix broken pin links caused by renaming interface function input/output parameters prior to compiling the interface, but after renaming the function itself. Mirrored from //UE4/Dev-Blueprints (CL# 3244388). #jira UE-39816 Change 3244248 on 2016/12/29 by laz.matech Saved the new sublevel in the persistent level and set it to hidden by default #jira UE-29618 Change 3244213 on 2016/12/29 by laz.matech Added a sublevel to QA-Sequencer map #jira UE-29618 Change 3243857 on 2016/12/27 by samuel.proctor Altered Container asset to have proper console input #jira UE-29618 Change 3243852 on 2016/12/27 by Mason.Seay Forgot config file #jira UE-29618 Change 3243847 on 2016/12/27 by mason.seay Improved mobile input #jira UE-29618 Change 3243536 on 2016/12/24 by Phillip.Kavan [UE-39944] Extend the GetClassDefaults node to include output pin exceptions for TSet/TMap properties (i.e. mirror safeguards already in place for TArray). Mirrored from //UE4/Dev-Blueprints (CL# 3243210). #jira UE-39944 Change 3243535 on 2016/12/24 by Phillip.Kavan [UE-39816] Renaming interface input/output parameters will no longer cause broken pin links at interface function call sites in Blueprints that are currently loaded. Mirrored from //UE4/Dev-Blueprints (CL# 3243207). #jira UE-39816 Change 3243534 on 2016/12/24 by Phillip.Kavan [UE-39733] Fix incorrect graph pin value display names for user-defined enum types. Mirrored from //UE4/Dev-Blueprints (CL# 3239965). #jira UE-39733 Change 3243532 on 2016/12/24 by Phillip.Kavan [UE-39854] Fix nativized assets build error when there are no native code dependencies. Mirrored from //UE4/Dev-Blueprints (CL# 3239778). #jira UE-39854 Change 3243529 on 2016/12/24 by Phillip.Kavan [UE-38999] Dump component tree node hierarchy to the output log on error state during widget generation. Mirrored from //UE4/Dev-Blueprints (CL# 3239289). #jira UE-38999 Change 3243442 on 2016/12/23 by mason.seay QAGame cleanup - Replacing copy pose from mesh test assets #jira UE-29618 Change 3243215 on 2016/12/22 by Dmitriy.Dyomin Fixed: Switching to ES2 feature level preview renders black in editor #jira UE-40009 Change 3243185 on 2016/12/22 by Ryan.Vance #jira UEVR-478 Integrating 3235308 Mono changes from DevVR. Change 3243183 on 2016/12/22 by Ryan.Vance #jira UEVR-455 Integrating 3243173 post present call back implementation from 4.14.1 Change 3243182 on 2016/12/22 by Ryan.Vance #jira UE-39269 Working around a nullptr deref in the Oculus runtime. Change 3243153 on 2016/12/22 by mason.seay WIP map update #jira UE-29618 Change 3243128 on 2016/12/22 by andrew.porter QAGame: Adding Actor Sequence test content for a crash. #jira UE-29618 Change 3243117 on 2016/12/22 by Jeff.Fisher UE-34004 GitHub 2659 : Implement support for OpenVR controller roles. -Rather than assigning unreal hands to controllers in the order the controllers are connected assign unreal hands to match the ones the API is using. -We now defer setting up controllers that are disconnected. This lets connected controllers, that may have hand preference from steam, occupy their desired hands first. If a controller is connected later and does not have a role it is assigned to an unoccupied hand or to the right hand. -This can still end up ignoring role in the following circumstance (and I can get it to do this): get one controller to prefer'right' and the other to have no preference. Power off the 'right' prefering controller. Start the game with only the no-preference controller on. The game will put that controller in the right slot, because the api gives it no other hints. Then power on the controller that preferred 'right'. That controller will now be assigned left, because right is occupied. I don't see a way around that without the ability to switch which hand a controller is associated with at runtime. -This does not yet handle starting with 2 controllers, disconnecting one, then connecting a third controller well. That did not work before either. A new Jira was created for that. #2659 #jira UE-34004 #review-3231154 Change 3243093 on 2016/12/22 by mason.seay Some tweaks to vehicle levels #jira UE-29618 Change 3243084 on 2016/12/22 by andrew.porter QAGame: Cleaned up Sequencer_OverrideBindings #jira UE-29618 Change 3243009 on 2016/12/22 by andrew.porter QAGame: Renaming actor in Sequencer_OverrideBindings. #jira UE-29618 Change 3243003 on 2016/12/22 by andrew.porter QAGame: Removing override bindings from level sequence #jira UE-29618 Change 3242996 on 2016/12/22 by andrew.porter QAGame: Slight tweak to QA-Sequencer. #jira UE-29618 Change 3242982 on 2016/12/22 by Marc.Audy Properly reenable stats sounds in both game and level editor #jira UE-40015 Change 3242959 on 2016/12/22 by mason.seay Test map for vehicles and moving meshes #jira UE-29618 Change 3242934 on 2016/12/22 by andrew.porter QAGame: Adding test content to QA-Sequencer for Override Bindings #jira UE-29618 Change 3242870 on 2016/12/22 by Mason.Seay QAGame footprint reduction: Clearing out content (were in for old bug reports) #jira UE-29618 Change 3242799 on 2016/12/22 by tim.gautier QAGame - Adding the following assets for Sequencer Event Track testing: -TM-Sequencer_EventTrack + BuildData -QA_LightStruct -Sequencer_EventTrack #jira UE-29618 Change 3242792 on 2016/12/22 by samuel.proctor Correcting Container test asset for proper output #jira UE-29618 Change 3242727 on 2016/12/22 by Dmitriy.Dyomin Fixed: LoadLevelIntstance returns a reference that can't be used to send an interface message #jira UE-40005 Change 3242666 on 2016/12/22 by Dmitriy.Dyomin Fixed: Packaging Android app for Mali Graphics Debugger v4.3.0 fails #jira UE-39534 Change 3242373 on 2016/12/21 by Ori.Cohen Allow vehicles to override inertia tensor after any mass properties have changed. #JIRA UE-39566 Change 3242323 on 2016/12/21 by Josh.Adams - Somehow my last change just got completely lost in the edigrate shuffle. Or something. I have no idea! Rdoing it #jira UE-39966 Change 3242286 on 2016/12/21 by mason.seay Vehicle Assets and Maps #jira UE-29618 Change 3242284 on 2016/12/21 by Marc.Audy Fix "stat sounds" not working after PIE completes and a new one is begun #jira UE-32743 #jira UE-39511 Change 3242281 on 2016/12/21 by Ori.Cohen Fix multi select being very slow in phat #JIRA UE-39559 Change 3242229 on 2016/12/21 by Ben.Marsh Fixup workspace for building PhysX. Change 3242227 on 2016/12/21 by Marc.Audy Properly update listener position for stat sounds #jira UE-38850 Change 3242218 on 2016/12/21 by Ori.Cohen Fix physx html5 compilation APEX issue. #JIRA UE-39566 Change 3242174 on 2016/12/21 by Ori.Cohen Fix incorrect moment of inertia for convex elements with translation. #JIRA UE-39566 Change 3242145 on 2016/12/21 by Ori.Cohen Port 4.14 hotfix for vehicle stability #JIRA UE-38710 Change 3242139 on 2016/12/21 by Ori.Cohen Port 4.14 hotfix: Fix crash when setting collision trace in construction script. #JIRA UE-39341 Change 3242088 on 2016/12/21 by Alexis.Matte Fix the drag and drop material on level instance to drop on the correct material slot Fix the serialization of the staticmesh property FMeshSectionInfoMap #jira UE-39952 Change 3242081 on 2016/12/21 by Andrew.Rodham Sequencer: Make details view focused when resetting inner struct contents to ensure that focus path is valid. #jira UE-39851 Change 3242079 on 2016/12/21 by Andrew.Rodham Sequencer: Evaluation templates are now only fully rebuilt in PIE, and will not re-cycle track identifiers - This addresses issues with newly compiled tracks recycling the persistent data of old stale tracks. - This commit also ensures we don't fully rebuild templates in the editor when in Sequencer #jira UE-39882 Change 3242078 on 2016/12/21 by Andrew.Rodham Sequencer: Fixed crash when deactivating a section in sequencer #jira UE-39880 Change 3242026 on 2016/12/21 by Josh.Adams - Fixed compile errors in tools after NVNRHI move #jira UE-39966 Change 3241994 on 2016/12/21 by andrew.porter QAGame: Disabled auto play on Sequencer_AnimNotify. #jira UE-29618 Change 3241989 on 2016/12/21 by Mitchell.Wilson Resolving CIS warnings in Content examples. Fixed up redirectors. Moved a texture from developer folder into project and relinked in POM_Debug material. Fixed up BP Commentary Box which was failing to compile. Updated spawn rate on Pulse Ring so it works as intended. #jira UE-39984 Change 3241986 on 2016/12/21 by mason.seay Vehicle Landscape Test map (mainly for crash investigation) #jira UE-29618 Change 3241914 on 2016/12/21 by Josh.Adams - Removed invalid and confusing .ini settings #jira UE-39982 Change 3241902 on 2016/12/21 by Josh.Adams - Moved NVNRHI stuff out of RHI.Build.cs #jira UE-39966 Change 3241889 on 2016/12/21 by andrew.porter QAGame: Added new level sequence to QA-Sequencer level #jira UE-29618 Change 3241884 on 2016/12/21 by Alexis.Matte Make sure the color grading cursor follow the mouse by using the exponent value when painting the cursor. #jira UE-39834 Change 3241869 on 2016/12/21 by andrew.porter QAGame: Adding test content for Sequencer Animation Notifies #jira UE-29618 Change 3241809 on 2016/12/21 by Chris.Wood Fix non-unity build errors in UnrealWatchdog. [UE-39940] - GitHub 3054 : Added EngineBuildSettings.h to UnrealWatchdog.cpp PR #3054: Added EngineBuildSettings.h to UnrealWatchdog.cpp (Contributed by ryanjon2040) #jira UE-39940 Change 3241806 on 2016/12/21 by Marc.Audy Don't unload and then reload streaming levels that are marked to be hidden. #jira UE-39883 Change 3241802 on 2016/12/21 by Marc.Audy Add new object flag RF_NeedInitialization to indicate that ~FObjectInitalizer and PostInitProperties have not been executed for the object Do not allow Modify calls on Objects that have not been initialized #jira UE-39731 Change 3241790 on 2016/12/21 by Marc.Audy Don't rerun construction scripts when an actor has seamless traveled from another level #jira UE-39699 Change 3241789 on 2016/12/21 by Marc.Audy Check Owner has a valid world before trying to access Scene (4.14.2) #jira UE-39560 Change 3241786 on 2016/12/21 by Marc.Audy Fixed crash when seamless travelling in PIE from levels other than the current editor level with a streaming sublevel shared with the current editor level #jira UE-39407 Change 3241781 on 2016/12/21 by Mitchell.Wilson Fixed up redirectors for SkeletalMesh and Personal Walkthroughs. #jira UE-30953 Change 3241747 on 2016/12/21 by mason.seay Tag Query test map and assets #jira UE-29618 Change 3240938 on 2016/12/20 by Ben.Marsh Remaking QFE fixes from 4.14 branch. Change 3240740 on 2016/12/20 by Ben.Marsh Update branch name for analytics. [CL 3272229 by Matthew Griffin in Main branch]
8902 lines
305 KiB
C++
8902 lines
305 KiB
C++
// Copyright 1998-2017 Epic Games, Inc. All Rights Reserved.
|
|
|
|
#include "MeshUtilities.h"
|
|
#include "Misc/MessageDialog.h"
|
|
#include "Misc/ScopeLock.h"
|
|
#include "Containers/Ticker.h"
|
|
#include "Misc/FeedbackContext.h"
|
|
#include "Misc/ScopedSlowTask.h"
|
|
#include "Misc/ConfigCacheIni.h"
|
|
#include "Modules/ModuleManager.h"
|
|
#include "UObject/Package.h"
|
|
#include "Misc/PackageName.h"
|
|
#include "Textures/SlateIcon.h"
|
|
#include "Styling/SlateTypes.h"
|
|
#include "Framework/Commands/UIAction.h"
|
|
#include "Framework/Commands/UICommandList.h"
|
|
#include "Framework/MultiBox/MultiBoxExtender.h"
|
|
#include "Framework/MultiBox/MultiBoxBuilder.h"
|
|
#include "Components/MeshComponent.h"
|
|
#include "RawIndexBuffer.h"
|
|
#include "Components/StaticMeshComponent.h"
|
|
#include "Engine/StaticMesh.h"
|
|
#include "Materials/Material.h"
|
|
#include "RawMesh.h"
|
|
#include "StaticMeshResources.h"
|
|
#include "MeshBuild.h"
|
|
#include "NvTriStrip.h"
|
|
#include "forsythtriangleorderoptimizer.h"
|
|
#include "nvtess.h"
|
|
#include "SkeletalMeshTools.h"
|
|
#include "Engine/SkeletalMesh.h"
|
|
#include "Components/SkinnedMeshComponent.h"
|
|
#include "ImageUtils.h"
|
|
#include "LayoutUV.h"
|
|
#include "mikktspace.h"
|
|
#include "DistanceFieldAtlas.h"
|
|
#include "Misc/FbxErrors.h"
|
|
#include "Components/SplineMeshComponent.h"
|
|
#include "PhysicsEngine/ConvexElem.h"
|
|
#include "PhysicsEngine/AggregateGeom.h"
|
|
#include "PhysicsEngine/BodySetup.h"
|
|
#include "MaterialUtilities.h"
|
|
#include "IHierarchicalLODUtilities.h"
|
|
#include "HierarchicalLODUtilitiesModule.h"
|
|
#include "MeshBoneReduction.h"
|
|
#include "MeshMergeData.h"
|
|
#include "Editor/EditorPerProjectUserSettings.h"
|
|
#include "GPUSkinVertexFactory.h"
|
|
#include "Developer/AssetTools/Public/IAssetTools.h"
|
|
#include "Developer/AssetTools/Public/AssetToolsModule.h"
|
|
#include "Materials/MaterialInstanceDynamic.h"
|
|
#include "GameFramework/Character.h"
|
|
#include "Components/CapsuleComponent.h"
|
|
#include "Animation/DebugSkelMeshComponent.h"
|
|
#include "Widgets/Text/STextBlock.h"
|
|
#include "Widgets/Input/SComboButton.h"
|
|
|
|
#include "LandscapeProxy.h"
|
|
#include "Landscape.h"
|
|
#include "LandscapeHeightfieldCollisionComponent.h"
|
|
#include "Engine/MeshMergeCullingVolume.h"
|
|
#include "ProxyMaterialUtilities.h"
|
|
#include "Toolkits/AssetEditorManager.h"
|
|
#include "LevelEditor.h"
|
|
#include "IAnimationBlueprintEditor.h"
|
|
#include "IAnimationBlueprintEditorModule.h"
|
|
#include "IAnimationEditor.h"
|
|
#include "IAnimationEditorModule.h"
|
|
#include "ISkeletalMeshEditor.h"
|
|
#include "ISkeletalMeshEditorModule.h"
|
|
#include "ISkeletonEditor.h"
|
|
#include "ISkeletonEditorModule.h"
|
|
#include "IPersonaToolkit.h"
|
|
#include "Dialogs/DlgPickAssetPath.h"
|
|
#include "SkeletalRenderPublic.h"
|
|
#include "AssetRegistryModule.h"
|
|
#include "Framework/Notifications/NotificationManager.h"
|
|
#include "Widgets/Notifications/SNotificationList.h"
|
|
#include "Engine/MeshSimplificationSettings.h"
|
|
|
|
#include "IDetailCustomization.h"
|
|
#include "EditorStyleSet.h"
|
|
#include "PropertyEditorModule.h"
|
|
#include "DetailLayoutBuilder.h"
|
|
#include "DetailCategoryBuilder.h"
|
|
#include "IDetailPropertyRow.h"
|
|
#include "DetailWidgetRow.h"
|
|
|
|
//@todo - implement required vector intrinsics for other implementations
|
|
#if PLATFORM_ENABLE_VECTORINTRINSICS
|
|
#include "kDOP.h"
|
|
#endif
|
|
|
|
#if WITH_EDITOR
|
|
#include "Editor.h"
|
|
#include "UnrealEdMisc.h"
|
|
#endif
|
|
|
|
|
|
/*------------------------------------------------------------------------------
|
|
MeshUtilities module.
|
|
------------------------------------------------------------------------------*/
|
|
|
|
// The version string is a GUID. If you make a change to mesh utilities that
|
|
// causes meshes to be rebuilt you MUST generate a new GUID and replace this
|
|
// string with it.
|
|
|
|
#define MESH_UTILITIES_VER TEXT("228332BAE0224DD294E232B87D83948F")
|
|
|
|
DEFINE_LOG_CATEGORY_STATIC(LogMeshUtilities, Verbose, All);
|
|
|
|
#define LOCTEXT_NAMESPACE "MeshUtils"
|
|
|
|
// CVars
|
|
static TAutoConsoleVariable<int32> CVarTriangleOrderOptimization(
|
|
TEXT("r.TriangleOrderOptimization"),
|
|
1,
|
|
TEXT("Controls the algorithm to use when optimizing the triangle order for the post-transform cache.\n")
|
|
TEXT("0: Use NVTriStrip (slower)\n")
|
|
TEXT("1: Use Forsyth algorithm (fastest)(default)")
|
|
TEXT("2: No triangle order optimization. (least efficient, debugging purposes only)"),
|
|
ECVF_Default);
|
|
|
|
static FAutoConsoleVariable CVarMeshReductionModule(
|
|
TEXT("r.MeshReductionModule"),
|
|
TEXT("QuadricMeshReduction"),
|
|
TEXT("Name of what mesh reduction module to choose. If blank it chooses any that exist.\n"),
|
|
ECVF_ReadOnly);
|
|
|
|
class FMeshUtilities : public IMeshUtilities
|
|
{
|
|
public:
|
|
/** Default constructor. */
|
|
FMeshUtilities()
|
|
: StaticMeshReduction(NULL)
|
|
, SkeletalMeshReduction(NULL)
|
|
, MeshMerging(NULL)
|
|
, DistributedMeshMerging(NULL)
|
|
, Processor(NULL)
|
|
{
|
|
}
|
|
|
|
void UpdateMeshReductionModule()
|
|
{
|
|
TArray<FName> ModuleNames;
|
|
FModuleManager::Get().FindModules(TEXT("*MeshReduction"), ModuleNames);
|
|
|
|
for(int32 Index = 0; Index < ModuleNames.Num(); Index++)
|
|
{
|
|
FString String = CVarMeshReductionModule->GetString();
|
|
bool bIsChoosenModule = ModuleNames[Index].GetPlainNameString().Equals(String);
|
|
|
|
IMeshReductionModule& MeshReductionModule = FModuleManager::LoadModuleChecked<IMeshReductionModule>(ModuleNames[Index]);
|
|
|
|
// Look for MeshReduction interface
|
|
if(MeshReductionModule.GetStaticMeshReductionInterface())
|
|
{
|
|
if(bIsChoosenModule || StaticMeshReduction == NULL)
|
|
{
|
|
StaticMeshReduction = MeshReductionModule.GetStaticMeshReductionInterface();
|
|
UE_LOG(LogMeshUtilities, Log, TEXT("Using %s for automatic static mesh reduction"), *ModuleNames[Index].ToString());
|
|
}
|
|
}
|
|
|
|
// Look for MeshReduction interface
|
|
if(MeshReductionModule.GetSkeletalMeshReductionInterface())
|
|
{
|
|
if(bIsChoosenModule || SkeletalMeshReduction == NULL)
|
|
{
|
|
SkeletalMeshReduction = MeshReductionModule.GetSkeletalMeshReductionInterface();
|
|
UE_LOG(LogMeshUtilities, Log, TEXT("Using %s for automatic skeletal mesh reduction"), *ModuleNames[Index].ToString());
|
|
}
|
|
}
|
|
|
|
// Look for MeshMerging interface
|
|
if(MeshReductionModule.GetMeshMergingInterface())
|
|
{
|
|
if(bIsChoosenModule || MeshMerging == NULL)
|
|
{
|
|
MeshMerging = MeshReductionModule.GetMeshMergingInterface();
|
|
UE_LOG(LogMeshUtilities, Log, TEXT("Using %s for automatic mesh merging"), *ModuleNames[Index].ToString());
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
private:
|
|
/** Cached pointer to the mesh reduction interface. */
|
|
IMeshReduction* StaticMeshReduction;
|
|
/** Cached pointer to the mesh reduction interface. */
|
|
IMeshReduction* SkeletalMeshReduction;
|
|
/** Cached pointer to the mesh merging interface. */
|
|
IMeshMerging* MeshMerging;
|
|
/** Cached pointer to the distributed mesh merging interface. */
|
|
IMeshMerging* DistributedMeshMerging;
|
|
/** Cached version string. */
|
|
FString VersionString;
|
|
/** True if Simplygon is being used for mesh reduction. */
|
|
bool bUsingSimplygon;
|
|
/** True if NvTriStrip is being used for tri order optimization. */
|
|
bool bUsingNvTriStrip;
|
|
/** True if we disable triangle order optimization. For debugging purposes only */
|
|
bool bDisableTriangleOrderOptimization;
|
|
|
|
class FProxyGenerationProcessor* Processor;
|
|
|
|
// IMeshUtilities interface.
|
|
virtual const FString& GetVersionString() const override
|
|
{
|
|
return VersionString;
|
|
}
|
|
|
|
virtual bool BuildStaticMesh(
|
|
FStaticMeshRenderData& OutRenderData,
|
|
TArray<FStaticMeshSourceModel>& SourceModels,
|
|
const FStaticMeshLODGroup& LODGroup,
|
|
int32 ImportVersion = EImportStaticMeshVersion::LastVersion
|
|
) override;
|
|
|
|
virtual void BuildStaticMeshVertexAndIndexBuffers(
|
|
TArray<FStaticMeshBuildVertex>& OutVertices,
|
|
TArray<TArray<uint32> >& OutPerSectionIndices,
|
|
TArray<int32>& OutWedgeMap,
|
|
const FRawMesh& RawMesh,
|
|
const TMultiMap<int32, int32>& OverlappingCorners,
|
|
const TMap<uint32, uint32>& MaterialToSectionMapping,
|
|
float ComparisonThreshold,
|
|
FVector BuildScale,
|
|
int32 ImportVersion
|
|
) override;
|
|
|
|
virtual bool GenerateStaticMeshLODs(TArray<FStaticMeshSourceModel>& Models, const FStaticMeshLODGroup& LODGroup) override;
|
|
|
|
virtual void GenerateSignedDistanceFieldVolumeData(
|
|
const FStaticMeshLODResources& LODModel,
|
|
class FQueuedThreadPool& ThreadPool,
|
|
const TArray<EBlendMode>& MaterialBlendModes,
|
|
const FBoxSphereBounds& Bounds,
|
|
float DistanceFieldResolutionScale,
|
|
bool bGenerateAsIfTwoSided,
|
|
FDistanceFieldVolumeData& OutData) override;
|
|
|
|
virtual bool BuildSkeletalMesh(FStaticLODModel& LODModel, const FReferenceSkeleton& RefSkeleton, const TArray<FVertInfluence>& Influences, const TArray<FMeshWedge>& Wedges, const TArray<FMeshFace>& Faces, const TArray<FVector>& Points, const TArray<int32>& PointToOriginalMap, const MeshBuildOptions& BuildOptions = MeshBuildOptions(), TArray<FText> * OutWarningMessages = NULL, TArray<FName> * OutWarningNames = NULL) override;
|
|
bool BuildSkeletalMesh_Legacy(FStaticLODModel& LODModel, const FReferenceSkeleton& RefSkeleton, const TArray<FVertInfluence>& Influences, const TArray<FMeshWedge>& Wedges, const TArray<FMeshFace>& Faces, const TArray<FVector>& Points, const TArray<int32>& PointToOriginalMap, bool bKeepOverlappingVertices = false, bool bComputeNormals = true, bool bComputeTangents = true, TArray<FText> * OutWarningMessages = NULL, TArray<FName> * OutWarningNames = NULL);
|
|
|
|
virtual IMeshReduction* GetStaticMeshReductionInterface() override;
|
|
virtual IMeshReduction* GetSkeletalMeshReductionInterface() override;
|
|
virtual IMeshMerging* GetMeshMergingInterface() override;
|
|
virtual void CacheOptimizeIndexBuffer(TArray<uint16>& Indices) override;
|
|
virtual void CacheOptimizeIndexBuffer(TArray<uint32>& Indices) override;
|
|
void CacheOptimizeVertexAndIndexBuffer(TArray<FStaticMeshBuildVertex>& Vertices, TArray<TArray<uint32> >& PerSectionIndices, TArray<int32>& WedgeMap);
|
|
|
|
virtual void BuildSkeletalAdjacencyIndexBuffer(
|
|
const TArray<FSoftSkinVertex>& VertexBuffer,
|
|
const uint32 TexCoordCount,
|
|
const TArray<uint32>& Indices,
|
|
TArray<uint32>& OutPnAenIndices
|
|
) override;
|
|
|
|
virtual void RechunkSkeletalMeshModels(USkeletalMesh* SrcMesh, int32 MaxBonesPerChunk) override;
|
|
|
|
virtual void CalcBoneVertInfos(USkeletalMesh* SkeletalMesh, TArray<FBoneVertInfo>& Infos, bool bOnlyDominant) override;
|
|
|
|
/**
|
|
* Convert a set of mesh components in their current pose to a static mesh.
|
|
* @param InMeshComponents The mesh components we want to convert
|
|
* @param InRootTransform The transform of the root of the mesh we want to output
|
|
* @param InPackageName The package name to create the static mesh in. If this is empty then a dialog will be displayed to pick the mesh.
|
|
* @return a new static mesh (specified by the user)
|
|
*/
|
|
virtual UStaticMesh* ConvertMeshesToStaticMesh(const TArray<UMeshComponent*>& InMeshComponents, const FTransform& InRootTransform = FTransform::Identity, const FString& InPackageName = FString()) override;
|
|
|
|
/**
|
|
* Builds a renderable skeletal mesh LOD model. Note that the array of chunks
|
|
* will be destroyed during this process!
|
|
* @param LODModel Upon return contains a renderable skeletal mesh LOD model.
|
|
* @param RefSkeleton The reference skeleton associated with the model.
|
|
* @param Chunks Skinned mesh chunks from which to build the renderable model.
|
|
* @param PointToOriginalMap Maps a vertex's RawPointIdx to its index at import time.
|
|
*/
|
|
void BuildSkeletalModelFromChunks(FStaticLODModel& LODModel, const FReferenceSkeleton& RefSkeleton, TArray<FSkinnedMeshChunk*>& Chunks, const TArray<int32>& PointToOriginalMap);
|
|
|
|
// IModuleInterface interface.
|
|
virtual void StartupModule() override;
|
|
virtual void ShutdownModule() override;
|
|
|
|
DEPRECATED(4.12, "Please use MergeActor with new signature instead")
|
|
virtual void MergeActors(
|
|
const TArray<AActor*>& SourceActors,
|
|
const FMeshMergingSettings& InSettings,
|
|
UPackage* InOuter,
|
|
const FString& InBasePackageName,
|
|
int32 UseLOD, // does not build all LODs but only use this LOD to create base mesh
|
|
TArray<UObject*>& OutAssetsToSync,
|
|
FVector& OutMergedActorLocation,
|
|
bool bSilent = false) const override;
|
|
|
|
virtual void MergeActors(
|
|
const TArray<AActor*>& SourceActors,
|
|
const FMeshMergingSettings& InSettings,
|
|
UPackage* InOuter,
|
|
const FString& InBasePackageName,
|
|
TArray<UObject*>& OutAssetsToSync,
|
|
FVector& OutMergedActorLocation,
|
|
bool bSilent = false) const override;
|
|
|
|
|
|
DEPRECATED(4.12, "Please use MergeStaticMeshComponents with new signature instead")
|
|
virtual void MergeStaticMeshComponents(
|
|
const TArray<UStaticMeshComponent*>& ComponentsToMerge,
|
|
UWorld* World,
|
|
const FMeshMergingSettings& InSettings,
|
|
UPackage* InOuter,
|
|
const FString& InBasePackageName,
|
|
int32 UseLOD, // does not build all LODs but only use this LOD to create base mesh
|
|
TArray<UObject*>& OutAssetsToSync,
|
|
FVector& OutMergedActorLocation,
|
|
const float ScreenSize,
|
|
bool bSilent = false) const override;
|
|
|
|
virtual void MergeStaticMeshComponents(
|
|
const TArray<UStaticMeshComponent*>& ComponentsToMerge,
|
|
UWorld* World,
|
|
const FMeshMergingSettings& InSettings,
|
|
UPackage* InOuter,
|
|
const FString& InBasePackageName,
|
|
TArray<UObject*>& OutAssetsToSync,
|
|
FVector& OutMergedActorLocation,
|
|
const float ScreenSize,
|
|
bool bSilent = false) const override;
|
|
|
|
virtual void CreateProxyMesh(const TArray<AActor*>& InActors, const struct FMeshProxySettings& InMeshProxySettings, UPackage* InOuter, const FString& InProxyBasePackageName, const FGuid InGuid, FCreateProxyDelegate InProxyCreatedDelegate, const bool bAllowAsync,
|
|
const float ScreenAreaSize = 1.0f) override;
|
|
|
|
DEPRECATED(4.11, "Please use CreateProxyMesh with new signature")
|
|
virtual void CreateProxyMesh(
|
|
const TArray<AActor*>& Actors,
|
|
const struct FMeshProxySettings& InProxySettings,
|
|
UPackage* InOuter,
|
|
const FString& ProxyBasePackageName,
|
|
TArray<UObject*>& OutAssetsToSync,
|
|
FVector& OutProxyLocation
|
|
) override;
|
|
|
|
virtual void CreateProxyMesh(
|
|
const TArray<AActor*>& Actors,
|
|
const struct FMeshProxySettings& InProxySettings,
|
|
UPackage* InOuter,
|
|
const FString& ProxyBasePackageName,
|
|
TArray<UObject*>& OutAssetsToSync,
|
|
const float ScreenAreaSize = 1.0f) override;
|
|
|
|
virtual void FlattenMaterialsWithMeshData(TArray<UMaterialInterface*>& InMaterials, TArray<FRawMeshExt>& InSourceMeshes, TMap<FMeshIdAndLOD, TArray<int32>>& InMaterialIndexMap, TArray<bool>& InMeshShouldBakeVertexData, const FMaterialProxySettings &InMaterialProxySettings, TArray<FFlattenMaterial> &OutFlattenedMaterials) const override;
|
|
|
|
bool ConstructRawMesh(
|
|
const UStaticMeshComponent* InMeshComponent,
|
|
int32 InLODIndex,
|
|
const bool bPropagateVertexColours,
|
|
FRawMesh& OutRawMesh,
|
|
TArray<FSectionInfo>& OutUniqueSections,
|
|
TArray<int32>& OutGlobalMaterialIndices
|
|
) const;
|
|
|
|
virtual void ExtractMeshDataForGeometryCache(FRawMesh& RawMesh, const FMeshBuildSettings& BuildSettings, TArray<FStaticMeshBuildVertex>& OutVertices, TArray<TArray<uint32> >& OutPerSectionIndices, int32 ImportVersion);
|
|
|
|
virtual bool PropagatePaintedColorsToRawMesh(const UStaticMeshComponent* StaticMeshComponent, int32 LODIndex, FRawMesh& RawMesh) const override;
|
|
|
|
virtual void CalculateTextureCoordinateBoundsForRawMesh(const FRawMesh& InRawMesh, TArray<FBox2D>& OutBounds) const override;
|
|
|
|
virtual void CalculateTextureCoordinateBoundsForSkeletalMesh(const FStaticLODModel& LODModel, TArray<FBox2D>& OutBounds) const override;
|
|
|
|
virtual bool GenerateUniqueUVsForStaticMesh(const FRawMesh& RawMesh, int32 TextureResolution, TArray<FVector2D>& OutTexCoords) const override;
|
|
virtual bool GenerateUniqueUVsForSkeletalMesh(const FStaticLODModel& LODModel, int32 TextureResolution, TArray<FVector2D>& OutTexCoords) const override;
|
|
|
|
virtual bool RemoveBonesFromMesh(USkeletalMesh* SkeletalMesh, int32 LODIndex, const TArray<FName>* BoneNamesToRemove) const override;
|
|
|
|
virtual void CalculateTangents(const TArray<FVector>& InVertices, const TArray<uint32>& InIndices, const TArray<FVector2D>& InUVs, const TArray<uint32>& InSmoothingGroupIndices, const uint32 InTangentOptions, TArray<FVector>& OutTangentX, TArray<FVector>& OutTangentY, TArray<FVector>& OutNormals) const override;
|
|
|
|
// Need to call some members from this class, (which is internal to this module)
|
|
friend class FStaticMeshUtilityBuilder;
|
|
|
|
protected:
|
|
void AddAnimationBlueprintEditorToolbarExtender();
|
|
|
|
void RemoveAnimationBlueprintEditorToolbarExtender();
|
|
|
|
TSharedRef<FExtender> GetAnimationBlueprintEditorToolbarExtender(const TSharedRef<FUICommandList> CommandList, TSharedRef<IAnimationBlueprintEditor> InAnimationBlueprintEditor);
|
|
|
|
void AddAnimationEditorToolbarExtender();
|
|
|
|
void RemoveAnimationEditorToolbarExtender();
|
|
|
|
TSharedRef<FExtender> GetAnimationEditorToolbarExtender(const TSharedRef<FUICommandList> CommandList, TSharedRef<IAnimationEditor> InAnimationEditor);
|
|
|
|
void AddSkeletalMeshEditorToolbarExtender();
|
|
|
|
void RemoveSkeletalMeshEditorToolbarExtender();
|
|
|
|
TSharedRef<FExtender> GetSkeletalMeshEditorToolbarExtender(const TSharedRef<FUICommandList> CommandList, TSharedRef<ISkeletalMeshEditor> InSkeletalMeshEditor);
|
|
|
|
void AddSkeletonEditorToolbarExtender();
|
|
|
|
void RemoveSkeletonEditorToolbarExtender();
|
|
|
|
TSharedRef<FExtender> GetSkeletonEditorToolbarExtender(const TSharedRef<FUICommandList> CommandList, TSharedRef<ISkeletonEditor> InSkeletonEditor);
|
|
|
|
void HandleAddConvertComponentToStaticMeshToToolbar(FToolBarBuilder& ParentToolbarBuilder, UMeshComponent* MeshComponent);
|
|
|
|
void AddLevelViewportMenuExtender();
|
|
|
|
void RemoveLevelViewportMenuExtender();
|
|
|
|
TSharedRef<FExtender> GetLevelViewportContextMenuExtender(const TSharedRef<FUICommandList> CommandList, const TArray<AActor*> InActors);
|
|
|
|
void ConvertActorMeshesToStaticMesh(const TArray<AActor*> InActors);
|
|
|
|
FDelegateHandle ModuleLoadedDelegateHandle;
|
|
FDelegateHandle LevelViewportExtenderHandle;
|
|
FDelegateHandle AnimationBlueprintEditorExtenderHandle;
|
|
FDelegateHandle AnimationEditorExtenderHandle;
|
|
FDelegateHandle SkeletalMeshEditorExtenderHandle;
|
|
FDelegateHandle SkeletonEditorExtenderHandle;
|
|
};
|
|
|
|
IMPLEMENT_MODULE(FMeshUtilities, MeshUtilities);
|
|
|
|
class FProxyGenerationProcessor : FTickerObjectBase
|
|
{
|
|
public:
|
|
FProxyGenerationProcessor()
|
|
{
|
|
#if WITH_EDITOR
|
|
FEditorDelegates::MapChange.AddRaw(this, &FProxyGenerationProcessor::OnMapChange);
|
|
FEditorDelegates::NewCurrentLevel.AddRaw(this, &FProxyGenerationProcessor::OnNewCurrentLevel);
|
|
#endif // WITH_EDITOR
|
|
}
|
|
|
|
~FProxyGenerationProcessor()
|
|
{
|
|
#if WITH_EDITOR
|
|
FEditorDelegates::MapChange.RemoveAll(this);
|
|
FEditorDelegates::NewCurrentLevel.RemoveAll(this);
|
|
#endif // WITH_EDITOR
|
|
}
|
|
|
|
void AddProxyJob(FGuid InJobGuid, FMergeCompleteData* InCompleteData)
|
|
{
|
|
FScopeLock Lock(&StateLock);
|
|
ProxyMeshJobs.Add(InJobGuid, InCompleteData);
|
|
}
|
|
|
|
virtual bool Tick(float DeltaTime) override
|
|
{
|
|
FScopeLock Lock(&StateLock);
|
|
for (const auto& Entry : ToProcessJobDataMap)
|
|
{
|
|
FGuid JobGuid = Entry.Key;
|
|
FProxyGenerationData* Data = Entry.Value;
|
|
|
|
// Process the job
|
|
ProcessJob(JobGuid, Data);
|
|
|
|
// Data retrieved so can now remove the job from the map
|
|
ProxyMeshJobs.Remove(JobGuid);
|
|
delete Data->MergeData;
|
|
delete Data;
|
|
}
|
|
|
|
ToProcessJobDataMap.Reset();
|
|
|
|
return true;
|
|
}
|
|
|
|
void ProxyGenerationComplete(FRawMesh& OutProxyMesh, struct FFlattenMaterial& OutMaterial, const FGuid OutJobGUID)
|
|
{
|
|
FScopeLock Lock(&StateLock);
|
|
FMergeCompleteData** FindData = ProxyMeshJobs.Find(OutJobGUID);
|
|
if (FindData && *FindData)
|
|
{
|
|
FMergeCompleteData* Data = *FindData;
|
|
|
|
FProxyGenerationData* GenerationData = new FProxyGenerationData();
|
|
GenerationData->Material = OutMaterial;
|
|
GenerationData->RawMesh = OutProxyMesh;
|
|
GenerationData->MergeData = Data;
|
|
|
|
ToProcessJobDataMap.Add(OutJobGUID, GenerationData);
|
|
}
|
|
}
|
|
|
|
//@third party BEGIN SIMPLYGON
|
|
void ProxyGenerationFailed(const FGuid OutJobGUID, const FString& ErrorMessage)
|
|
{
|
|
FScopeLock Lock(&StateLock);
|
|
FMergeCompleteData** FindData = ProxyMeshJobs.Find(OutJobGUID);
|
|
if (FindData && *FindData)
|
|
{
|
|
UE_LOG(LogMeshUtilities, Log, TEXT("Failed to generate proxy mesh for cluster %s, %s"), *(*FindData)->ProxyBasePackageName, *ErrorMessage);
|
|
ProxyMeshJobs.Remove(OutJobGUID);
|
|
}
|
|
}
|
|
//@third party END SIMPLYGON
|
|
|
|
|
|
protected:
|
|
/** Called when the map has changed*/
|
|
void OnMapChange(uint32 MapFlags)
|
|
{
|
|
ClearProcessingData();
|
|
}
|
|
|
|
/** Called when the current level has changed */
|
|
void OnNewCurrentLevel()
|
|
{
|
|
ClearProcessingData();
|
|
}
|
|
|
|
/** Clears the processing data array/map */
|
|
void ClearProcessingData()
|
|
{
|
|
FScopeLock Lock(&StateLock);
|
|
ProxyMeshJobs.Empty();
|
|
ToProcessJobDataMap.Empty();
|
|
}
|
|
|
|
protected:
|
|
/** Structure storing the data required during processing */
|
|
struct FProxyGenerationData
|
|
{
|
|
FRawMesh RawMesh;
|
|
FFlattenMaterial Material;
|
|
FMergeCompleteData* MergeData;
|
|
};
|
|
|
|
void ProcessJob(const FGuid& JobGuid, FProxyGenerationData* Data)
|
|
{
|
|
TArray<UObject*> OutAssetsToSync;
|
|
const FString AssetBaseName = FPackageName::GetShortName(Data->MergeData->ProxyBasePackageName);
|
|
const FString AssetBasePath = Data->MergeData->InOuter ? TEXT("") : FPackageName::GetLongPackagePath(Data->MergeData->ProxyBasePackageName) + TEXT("/");
|
|
|
|
// Retrieve flattened material data
|
|
FFlattenMaterial& FlattenMaterial = Data->Material;
|
|
|
|
// Resize flattened material
|
|
FMaterialUtilities::ResizeFlattenMaterial(FlattenMaterial, Data->MergeData->InProxySettings);
|
|
|
|
// Optimize flattened material
|
|
FMaterialUtilities::OptimizeFlattenMaterial(FlattenMaterial);
|
|
|
|
// Create a new proxy material instance
|
|
UMaterialInstanceConstant* ProxyMaterial = ProxyMaterialUtilities::CreateProxyMaterialInstance(Data->MergeData->InOuter, Data->MergeData->InProxySettings.MaterialSettings, FlattenMaterial, AssetBasePath, AssetBaseName, OutAssetsToSync);
|
|
|
|
// Set material static lighting usage flag if project has static lighting enabled
|
|
static const auto AllowStaticLightingVar = IConsoleManager::Get().FindTConsoleVariableDataInt(TEXT("r.AllowStaticLighting"));
|
|
const bool bAllowStaticLighting = (!AllowStaticLightingVar || AllowStaticLightingVar->GetValueOnGameThread() != 0);
|
|
if (bAllowStaticLighting)
|
|
{
|
|
ProxyMaterial->CheckMaterialUsage(MATUSAGE_StaticLighting);
|
|
}
|
|
|
|
// Construct proxy static mesh
|
|
UPackage* MeshPackage = Data->MergeData->InOuter;
|
|
FString MeshAssetName = TEXT("SM_") + AssetBaseName;
|
|
if (MeshPackage == nullptr)
|
|
{
|
|
MeshPackage = CreatePackage(NULL, *(AssetBasePath + MeshAssetName));
|
|
MeshPackage->FullyLoad();
|
|
MeshPackage->Modify();
|
|
}
|
|
|
|
UStaticMesh* StaticMesh = NewObject<UStaticMesh>(MeshPackage, FName(*MeshAssetName), RF_Public | RF_Standalone);
|
|
StaticMesh->InitResources();
|
|
|
|
FString OutputPath = StaticMesh->GetPathName();
|
|
|
|
// make sure it has a new lighting guid
|
|
StaticMesh->LightingGuid = FGuid::NewGuid();
|
|
|
|
// Set it to use textured lightmaps. Note that Build Lighting will do the error-checking (texcoordindex exists for all LODs, etc).
|
|
StaticMesh->LightMapResolution = Data->MergeData->InProxySettings.LightMapResolution;
|
|
StaticMesh->LightMapCoordinateIndex = 1;
|
|
|
|
FStaticMeshSourceModel* SrcModel = new (StaticMesh->SourceModels) FStaticMeshSourceModel();
|
|
/*Don't allow the engine to recalculate normals*/
|
|
SrcModel->BuildSettings.bRecomputeNormals = false;
|
|
SrcModel->BuildSettings.bRecomputeTangents = false;
|
|
SrcModel->BuildSettings.bRemoveDegenerates = true;
|
|
SrcModel->BuildSettings.bUseHighPrecisionTangentBasis = false;
|
|
SrcModel->BuildSettings.bUseFullPrecisionUVs = false;
|
|
SrcModel->RawMeshBulkData->SaveRawMesh(Data->RawMesh);
|
|
|
|
//Assign the proxy material to the static mesh
|
|
StaticMesh->StaticMaterials.Add(FStaticMaterial(ProxyMaterial));
|
|
|
|
//Set the Imported version before calling the build
|
|
StaticMesh->ImportVersion = EImportStaticMeshVersion::LastVersion;
|
|
|
|
StaticMesh->Build();
|
|
StaticMesh->PostEditChange();
|
|
|
|
OutAssetsToSync.Add(StaticMesh);
|
|
|
|
// Execute the delegate received from the user
|
|
Data->MergeData->CallbackDelegate.ExecuteIfBound(JobGuid, OutAssetsToSync);
|
|
}
|
|
protected:
|
|
/** Holds Proxy mesh job data together with the job Guid */
|
|
TMap<FGuid, FMergeCompleteData*> ProxyMeshJobs;
|
|
/** Holds Proxy generation data together with the job Guid */
|
|
TMap<FGuid, FProxyGenerationData*> ToProcessJobDataMap;
|
|
/** Critical section to keep ProxyMeshJobs/ToProcessJobDataMap access thread-safe */
|
|
FCriticalSection StateLock;
|
|
};
|
|
|
|
//@todo - implement required vector intrinsics for other implementations
|
|
#if PLATFORM_ENABLE_VECTORINTRINSICS
|
|
|
|
class FMeshBuildDataProvider
|
|
{
|
|
public:
|
|
|
|
/** Initialization constructor. */
|
|
FMeshBuildDataProvider(
|
|
const TkDOPTree<const FMeshBuildDataProvider, uint32>& InkDopTree) :
|
|
kDopTree(InkDopTree)
|
|
{}
|
|
|
|
// kDOP data provider interface.
|
|
|
|
FORCEINLINE const TkDOPTree<const FMeshBuildDataProvider, uint32>& GetkDOPTree(void) const
|
|
{
|
|
return kDopTree;
|
|
}
|
|
|
|
FORCEINLINE const FMatrix& GetLocalToWorld(void) const
|
|
{
|
|
return FMatrix::Identity;
|
|
}
|
|
|
|
FORCEINLINE const FMatrix& GetWorldToLocal(void) const
|
|
{
|
|
return FMatrix::Identity;
|
|
}
|
|
|
|
FORCEINLINE FMatrix GetLocalToWorldTransposeAdjoint(void) const
|
|
{
|
|
return FMatrix::Identity;
|
|
}
|
|
|
|
FORCEINLINE float GetDeterminant(void) const
|
|
{
|
|
return 1.0f;
|
|
}
|
|
|
|
private:
|
|
|
|
const TkDOPTree<const FMeshBuildDataProvider, uint32>& kDopTree;
|
|
};
|
|
|
|
/** Generates unit length, stratified and uniformly distributed direction samples in a hemisphere. */
|
|
void GenerateStratifiedUniformHemisphereSamples(int32 NumThetaSteps, int32 NumPhiSteps, FRandomStream& RandomStream, TArray<FVector4>& Samples)
|
|
{
|
|
Samples.Empty(NumThetaSteps * NumPhiSteps);
|
|
for (int32 ThetaIndex = 0; ThetaIndex < NumThetaSteps; ThetaIndex++)
|
|
{
|
|
for (int32 PhiIndex = 0; PhiIndex < NumPhiSteps; PhiIndex++)
|
|
{
|
|
const float U1 = RandomStream.GetFraction();
|
|
const float U2 = RandomStream.GetFraction();
|
|
|
|
const float Fraction1 = (ThetaIndex + U1) / (float)NumThetaSteps;
|
|
const float Fraction2 = (PhiIndex + U2) / (float)NumPhiSteps;
|
|
|
|
const float R = FMath::Sqrt(1.0f - Fraction1 * Fraction1);
|
|
|
|
const float Phi = 2.0f * (float)PI * Fraction2;
|
|
// Convert to Cartesian
|
|
Samples.Add(FVector4(FMath::Cos(Phi) * R, FMath::Sin(Phi) * R, Fraction1));
|
|
}
|
|
}
|
|
}
|
|
|
|
class FMeshDistanceFieldAsyncTask : public FNonAbandonableTask
|
|
{
|
|
public:
|
|
FMeshDistanceFieldAsyncTask(TkDOPTree<const FMeshBuildDataProvider, uint32>* InkDopTree,
|
|
const TArray<FVector4>* InSampleDirections,
|
|
FBox InVolumeBounds,
|
|
FIntVector InVolumeDimensions,
|
|
float InVolumeMaxDistance,
|
|
int32 InZIndex,
|
|
TArray<FFloat16>* DistanceFieldVolume)
|
|
:
|
|
kDopTree(InkDopTree),
|
|
SampleDirections(InSampleDirections),
|
|
VolumeBounds(InVolumeBounds),
|
|
VolumeDimensions(InVolumeDimensions),
|
|
VolumeMaxDistance(InVolumeMaxDistance),
|
|
ZIndex(InZIndex),
|
|
OutDistanceFieldVolume(DistanceFieldVolume),
|
|
bNegativeAtBorder(false)
|
|
{}
|
|
|
|
void DoWork();
|
|
|
|
FORCEINLINE TStatId GetStatId() const
|
|
{
|
|
RETURN_QUICK_DECLARE_CYCLE_STAT(FMeshDistanceFieldAsyncTask, STATGROUP_ThreadPoolAsyncTasks);
|
|
}
|
|
|
|
bool WasNegativeAtBorder() const
|
|
{
|
|
return bNegativeAtBorder;
|
|
}
|
|
|
|
private:
|
|
|
|
// Readonly inputs
|
|
TkDOPTree<const FMeshBuildDataProvider, uint32>* kDopTree;
|
|
const TArray<FVector4>* SampleDirections;
|
|
FBox VolumeBounds;
|
|
FIntVector VolumeDimensions;
|
|
float VolumeMaxDistance;
|
|
int32 ZIndex;
|
|
|
|
// Output
|
|
TArray<FFloat16>* OutDistanceFieldVolume;
|
|
bool bNegativeAtBorder;
|
|
};
|
|
|
|
void FMeshDistanceFieldAsyncTask::DoWork()
|
|
{
|
|
FMeshBuildDataProvider kDOPDataProvider(*kDopTree);
|
|
const FVector DistanceFieldVoxelSize(VolumeBounds.GetSize() / FVector(VolumeDimensions.X, VolumeDimensions.Y, VolumeDimensions.Z));
|
|
const float VoxelDiameterSqr = DistanceFieldVoxelSize.SizeSquared();
|
|
|
|
for (int32 YIndex = 0; YIndex < VolumeDimensions.Y; YIndex++)
|
|
{
|
|
for (int32 XIndex = 0; XIndex < VolumeDimensions.X; XIndex++)
|
|
{
|
|
const FVector VoxelPosition = FVector(XIndex + .5f, YIndex + .5f, ZIndex + .5f) * DistanceFieldVoxelSize + VolumeBounds.Min;
|
|
const int32 Index = (ZIndex * VolumeDimensions.Y * VolumeDimensions.X + YIndex * VolumeDimensions.X + XIndex);
|
|
|
|
float MinDistance = VolumeMaxDistance;
|
|
int32 Hit = 0;
|
|
int32 HitBack = 0;
|
|
|
|
for (int32 SampleIndex = 0; SampleIndex < SampleDirections->Num(); SampleIndex++)
|
|
{
|
|
const FVector RayDirection = (*SampleDirections)[SampleIndex];
|
|
|
|
if (FMath::LineBoxIntersection(VolumeBounds, VoxelPosition, VoxelPosition + RayDirection * VolumeMaxDistance, RayDirection))
|
|
{
|
|
FkHitResult Result;
|
|
|
|
TkDOPLineCollisionCheck<const FMeshBuildDataProvider, uint32> kDOPCheck(
|
|
VoxelPosition,
|
|
VoxelPosition + RayDirection * VolumeMaxDistance,
|
|
true,
|
|
kDOPDataProvider,
|
|
&Result);
|
|
|
|
bool bHit = kDopTree->LineCheck(kDOPCheck);
|
|
|
|
if (bHit)
|
|
{
|
|
Hit++;
|
|
|
|
const FVector HitNormal = kDOPCheck.GetHitNormal();
|
|
|
|
if (FVector::DotProduct(RayDirection, HitNormal) > 0
|
|
// MaterialIndex on the build triangles was set to 1 if two-sided, or 0 if one-sided
|
|
&& kDOPCheck.Result->Item == 0)
|
|
{
|
|
HitBack++;
|
|
}
|
|
|
|
const float CurrentDistance = VolumeMaxDistance * Result.Time;
|
|
|
|
if (CurrentDistance < MinDistance)
|
|
{
|
|
MinDistance = CurrentDistance;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
const float UnsignedDistance = MinDistance;
|
|
|
|
// Consider this voxel 'inside' an object if more than 50% of the rays hit back faces
|
|
MinDistance *= (Hit == 0 || HitBack < SampleDirections->Num() * .5f) ? 1 : -1;
|
|
|
|
// If we are very close to a surface and nearly all of our rays hit backfaces, treat as inside
|
|
// This is important for one sided planes
|
|
if (FMath::Square(UnsignedDistance) < VoxelDiameterSqr && HitBack > .95f * Hit)
|
|
{
|
|
MinDistance = -UnsignedDistance;
|
|
}
|
|
|
|
MinDistance = FMath::Min(MinDistance, VolumeMaxDistance);
|
|
const float VolumeSpaceDistance = MinDistance / VolumeBounds.GetExtent().GetMax();
|
|
|
|
if (MinDistance < 0 &&
|
|
(XIndex == 0 || XIndex == VolumeDimensions.X - 1 ||
|
|
YIndex == 0 || YIndex == VolumeDimensions.Y - 1 ||
|
|
ZIndex == 0 || ZIndex == VolumeDimensions.Z - 1))
|
|
{
|
|
bNegativeAtBorder = true;
|
|
}
|
|
|
|
(*OutDistanceFieldVolume)[Index] = FFloat16(VolumeSpaceDistance);
|
|
}
|
|
}
|
|
}
|
|
|
|
void FMeshUtilities::GenerateSignedDistanceFieldVolumeData(
|
|
const FStaticMeshLODResources& LODModel,
|
|
class FQueuedThreadPool& ThreadPool,
|
|
const TArray<EBlendMode>& MaterialBlendModes,
|
|
const FBoxSphereBounds& Bounds,
|
|
float DistanceFieldResolutionScale,
|
|
bool bGenerateAsIfTwoSided,
|
|
FDistanceFieldVolumeData& OutData)
|
|
{
|
|
if (DistanceFieldResolutionScale > 0)
|
|
{
|
|
const double StartTime = FPlatformTime::Seconds();
|
|
const FPositionVertexBuffer& PositionVertexBuffer = LODModel.PositionVertexBuffer;
|
|
FIndexArrayView Indices = LODModel.IndexBuffer.GetArrayView();
|
|
TArray<FkDOPBuildCollisionTriangle<uint32> > BuildTriangles;
|
|
|
|
FVector BoundsSize = Bounds.GetBox().GetExtent() * 2;
|
|
float MaxDimension = FMath::Max(FMath::Max(BoundsSize.X, BoundsSize.Y), BoundsSize.Z);
|
|
|
|
// Consider the mesh a plane if it is very flat
|
|
const bool bMeshWasPlane = BoundsSize.Z * 100 < MaxDimension
|
|
// And it lies mostly on the origin
|
|
&& Bounds.Origin.Z - Bounds.BoxExtent.Z < KINDA_SMALL_NUMBER
|
|
&& Bounds.Origin.Z + Bounds.BoxExtent.Z > -KINDA_SMALL_NUMBER;
|
|
|
|
for (int32 i = 0; i < Indices.Num(); i += 3)
|
|
{
|
|
FVector V0 = PositionVertexBuffer.VertexPosition(Indices[i + 0]);
|
|
FVector V1 = PositionVertexBuffer.VertexPosition(Indices[i + 1]);
|
|
FVector V2 = PositionVertexBuffer.VertexPosition(Indices[i + 2]);
|
|
|
|
if (bMeshWasPlane)
|
|
{
|
|
// Flatten out the mesh into an actual plane, this will allow us to manipulate the component's Z scale at runtime without artifacts
|
|
V0.Z = 0;
|
|
V1.Z = 0;
|
|
V2.Z = 0;
|
|
}
|
|
|
|
const FVector LocalNormal = ((V1 - V2) ^ (V0 - V2)).GetSafeNormal();
|
|
|
|
// No degenerates
|
|
if (LocalNormal.IsUnit())
|
|
{
|
|
bool bTriangleIsOpaqueOrMasked = false;
|
|
|
|
for (int32 SectionIndex = 0; SectionIndex < LODModel.Sections.Num(); SectionIndex++)
|
|
{
|
|
const FStaticMeshSection& Section = LODModel.Sections[SectionIndex];
|
|
|
|
if ((uint32)i >= Section.FirstIndex && (uint32)i < Section.FirstIndex + Section.NumTriangles * 3)
|
|
{
|
|
if (MaterialBlendModes.IsValidIndex(Section.MaterialIndex))
|
|
{
|
|
bTriangleIsOpaqueOrMasked = !IsTranslucentBlendMode(MaterialBlendModes[Section.MaterialIndex]);
|
|
}
|
|
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (bTriangleIsOpaqueOrMasked)
|
|
{
|
|
BuildTriangles.Add(FkDOPBuildCollisionTriangle<uint32>(
|
|
bGenerateAsIfTwoSided,
|
|
V0,
|
|
V1,
|
|
V2));
|
|
}
|
|
}
|
|
}
|
|
|
|
TkDOPTree<const FMeshBuildDataProvider, uint32> kDopTree;
|
|
kDopTree.Build(BuildTriangles);
|
|
|
|
//@todo - project setting
|
|
const int32 NumVoxelDistanceSamples = 1200;
|
|
TArray<FVector4> SampleDirections;
|
|
const int32 NumThetaSteps = FMath::TruncToInt(FMath::Sqrt(NumVoxelDistanceSamples / (2.0f * (float)PI)));
|
|
const int32 NumPhiSteps = FMath::TruncToInt(NumThetaSteps * (float)PI);
|
|
FRandomStream RandomStream(0);
|
|
GenerateStratifiedUniformHemisphereSamples(NumThetaSteps, NumPhiSteps, RandomStream, SampleDirections);
|
|
TArray<FVector4> OtherHemisphereSamples;
|
|
GenerateStratifiedUniformHemisphereSamples(NumThetaSteps, NumPhiSteps, RandomStream, OtherHemisphereSamples);
|
|
|
|
for (int32 i = 0; i < OtherHemisphereSamples.Num(); i++)
|
|
{
|
|
FVector4 Sample = OtherHemisphereSamples[i];
|
|
Sample.Z *= -1;
|
|
SampleDirections.Add(Sample);
|
|
}
|
|
|
|
static const auto CVar = IConsoleManager::Get().FindTConsoleVariableDataInt(TEXT("r.DistanceFields.MaxPerMeshResolution"));
|
|
const int32 PerMeshMax = CVar->GetValueOnAnyThread();
|
|
|
|
// Meshes with explicit artist-specified scale can go higher
|
|
const int32 MaxNumVoxelsOneDim = DistanceFieldResolutionScale <= 1 ? PerMeshMax / 2 : PerMeshMax;
|
|
const int32 MinNumVoxelsOneDim = 8;
|
|
|
|
static const auto CVarDensity = IConsoleManager::Get().FindTConsoleVariableDataFloat(TEXT("r.DistanceFields.DefaultVoxelDensity"));
|
|
const float VoxelDensity = CVarDensity->GetValueOnAnyThread();
|
|
|
|
const float NumVoxelsPerLocalSpaceUnit = VoxelDensity * DistanceFieldResolutionScale;
|
|
FBox MeshBounds(Bounds.GetBox());
|
|
|
|
{
|
|
const float MaxOriginalExtent = MeshBounds.GetExtent().GetMax();
|
|
// Expand so that the edges of the volume are guaranteed to be outside of the mesh
|
|
// Any samples outside the bounds will be clamped to the border, so they must be outside
|
|
const FVector NewExtent(MeshBounds.GetExtent() + FVector(.2f * MaxOriginalExtent).ComponentMax(4 * MeshBounds.GetExtent() / MinNumVoxelsOneDim));
|
|
FBox DistanceFieldVolumeBounds = FBox(MeshBounds.GetCenter() - NewExtent, MeshBounds.GetCenter() + NewExtent);
|
|
const float DistanceFieldVolumeMaxDistance = DistanceFieldVolumeBounds.GetExtent().Size();
|
|
|
|
const FVector DesiredDimensions(DistanceFieldVolumeBounds.GetSize() * FVector(NumVoxelsPerLocalSpaceUnit));
|
|
|
|
const FIntVector VolumeDimensions(
|
|
FMath::Clamp(FMath::TruncToInt(DesiredDimensions.X), MinNumVoxelsOneDim, MaxNumVoxelsOneDim),
|
|
FMath::Clamp(FMath::TruncToInt(DesiredDimensions.Y), MinNumVoxelsOneDim, MaxNumVoxelsOneDim),
|
|
FMath::Clamp(FMath::TruncToInt(DesiredDimensions.Z), MinNumVoxelsOneDim, MaxNumVoxelsOneDim));
|
|
|
|
OutData.Size = VolumeDimensions;
|
|
OutData.LocalBoundingBox = DistanceFieldVolumeBounds;
|
|
OutData.DistanceFieldVolume.AddZeroed(VolumeDimensions.X * VolumeDimensions.Y * VolumeDimensions.Z);
|
|
|
|
TIndirectArray<FAsyncTask<FMeshDistanceFieldAsyncTask>> AsyncTasks;
|
|
|
|
for (int32 ZIndex = 0; ZIndex < VolumeDimensions.Z; ZIndex++)
|
|
{
|
|
FAsyncTask<FMeshDistanceFieldAsyncTask>* Task = new FAsyncTask<class FMeshDistanceFieldAsyncTask>(
|
|
&kDopTree,
|
|
&SampleDirections,
|
|
DistanceFieldVolumeBounds,
|
|
VolumeDimensions,
|
|
DistanceFieldVolumeMaxDistance,
|
|
ZIndex,
|
|
&OutData.DistanceFieldVolume);
|
|
|
|
Task->StartBackgroundTask(&ThreadPool);
|
|
|
|
AsyncTasks.Add(Task);
|
|
}
|
|
|
|
bool bNegativeAtBorder = false;
|
|
|
|
for (int32 TaskIndex = 0; TaskIndex < AsyncTasks.Num(); TaskIndex++)
|
|
{
|
|
FAsyncTask<FMeshDistanceFieldAsyncTask>& Task = AsyncTasks[TaskIndex];
|
|
Task.EnsureCompletion(false);
|
|
bNegativeAtBorder = bNegativeAtBorder || Task.GetTask().WasNegativeAtBorder();
|
|
}
|
|
|
|
OutData.bMeshWasClosed = !bNegativeAtBorder;
|
|
OutData.bBuiltAsIfTwoSided = bGenerateAsIfTwoSided;
|
|
OutData.bMeshWasPlane = bMeshWasPlane;
|
|
|
|
UE_LOG(LogMeshUtilities, Log, TEXT("Finished distance field build in %.1fs - %ux%ux%u distance field, %u triangles"),
|
|
(float)(FPlatformTime::Seconds() - StartTime),
|
|
VolumeDimensions.X,
|
|
VolumeDimensions.Y,
|
|
VolumeDimensions.Z,
|
|
Indices.Num() / 3);
|
|
|
|
// Toss distance field if mesh was not closed
|
|
if (bNegativeAtBorder)
|
|
{
|
|
OutData.Size = FIntVector(0, 0, 0);
|
|
OutData.DistanceFieldVolume.Empty();
|
|
|
|
UE_LOG(LogMeshUtilities, Log, TEXT("Discarded distance field as mesh was not closed! Assign a two-sided material to fix."));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
#else
|
|
|
|
void FMeshUtilities::GenerateSignedDistanceFieldVolumeData(
|
|
const FStaticMeshLODResources& LODModel,
|
|
class FQueuedThreadPool& ThreadPool,
|
|
const TArray<EBlendMode>& MaterialBlendModes,
|
|
const FBoxSphereBounds& Bounds,
|
|
float DistanceFieldResolutionScale,
|
|
bool bGenerateAsIfTwoSided,
|
|
FDistanceFieldVolumeData& OutData)
|
|
{
|
|
if (DistanceFieldResolutionScale > 0)
|
|
{
|
|
UE_LOG(LogMeshUtilities, Error, TEXT("Couldn't generate distance field for mesh, platform is missing required Vector intrinsics."));
|
|
}
|
|
}
|
|
|
|
#endif
|
|
|
|
/*------------------------------------------------------------------------------
|
|
NVTriStrip for cache optimizing index buffers.
|
|
------------------------------------------------------------------------------*/
|
|
|
|
namespace NvTriStrip
|
|
{
|
|
/**
|
|
* Converts 16 bit indices to 32 bit prior to passing them into the real GenerateStrips util method
|
|
*/
|
|
void GenerateStrips(
|
|
const uint8* Indices,
|
|
bool Is32Bit,
|
|
const uint32 NumIndices,
|
|
PrimitiveGroup** PrimGroups,
|
|
uint32* NumGroups
|
|
)
|
|
{
|
|
if (Is32Bit)
|
|
{
|
|
GenerateStrips((uint32*)Indices, NumIndices, PrimGroups, NumGroups);
|
|
}
|
|
else
|
|
{
|
|
// convert to 32 bit
|
|
uint32 Idx;
|
|
TArray<uint32> NewIndices;
|
|
NewIndices.AddUninitialized(NumIndices);
|
|
for (Idx = 0; Idx < NumIndices; ++Idx)
|
|
{
|
|
NewIndices[Idx] = ((uint16*)Indices)[Idx];
|
|
}
|
|
GenerateStrips(NewIndices.GetData(), NumIndices, PrimGroups, NumGroups);
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
* Orders a triangle list for better vertex cache coherency.
|
|
*
|
|
* *** WARNING: This is safe to call for multiple threads IF AND ONLY IF all
|
|
* threads call SetListsOnly(true) and SetCacheSize(CACHESIZE_GEFORCE3). If
|
|
* NvTriStrip is ever used with different settings the library will need
|
|
* some modifications to be thread-safe. ***
|
|
*/
|
|
template<typename IndexDataType, typename Allocator>
|
|
void CacheOptimizeIndexBuffer(TArray<IndexDataType, Allocator>& Indices)
|
|
{
|
|
static_assert(sizeof(IndexDataType) == 2 || sizeof(IndexDataType) == 4, "Indices must be short or int.");
|
|
|
|
PrimitiveGroup* PrimitiveGroups = NULL;
|
|
uint32 NumPrimitiveGroups = 0;
|
|
bool Is32Bit = sizeof(IndexDataType) == 4;
|
|
|
|
SetListsOnly(true);
|
|
SetCacheSize(CACHESIZE_GEFORCE3);
|
|
|
|
GenerateStrips((uint8*)Indices.GetData(), Is32Bit, Indices.Num(), &PrimitiveGroups, &NumPrimitiveGroups);
|
|
|
|
Indices.Empty();
|
|
Indices.AddUninitialized(PrimitiveGroups->numIndices);
|
|
|
|
if (Is32Bit)
|
|
{
|
|
FMemory::Memcpy(Indices.GetData(), PrimitiveGroups->indices, Indices.Num() * sizeof(IndexDataType));
|
|
}
|
|
else
|
|
{
|
|
for (uint32 I = 0; I < PrimitiveGroups->numIndices; ++I)
|
|
{
|
|
Indices[I] = (uint16)PrimitiveGroups->indices[I];
|
|
}
|
|
}
|
|
|
|
delete[] PrimitiveGroups;
|
|
}
|
|
}
|
|
|
|
/*------------------------------------------------------------------------------
|
|
Forsyth algorithm for cache optimizing index buffers.
|
|
------------------------------------------------------------------------------*/
|
|
|
|
namespace Forsyth
|
|
{
|
|
/**
|
|
* Converts 16 bit indices to 32 bit prior to passing them into the real OptimizeFaces util method
|
|
*/
|
|
void OptimizeFaces(
|
|
const uint8* Indices,
|
|
bool Is32Bit,
|
|
const uint32 NumIndices,
|
|
uint32 NumVertices,
|
|
uint32* OutIndices,
|
|
uint16 CacheSize
|
|
)
|
|
{
|
|
if (Is32Bit)
|
|
{
|
|
OptimizeFaces((uint32*)Indices, NumIndices, NumVertices, OutIndices, CacheSize);
|
|
}
|
|
else
|
|
{
|
|
// convert to 32 bit
|
|
uint32 Idx;
|
|
TArray<uint32> NewIndices;
|
|
NewIndices.AddUninitialized(NumIndices);
|
|
for (Idx = 0; Idx < NumIndices; ++Idx)
|
|
{
|
|
NewIndices[Idx] = ((uint16*)Indices)[Idx];
|
|
}
|
|
OptimizeFaces(NewIndices.GetData(), NumIndices, NumVertices, OutIndices, CacheSize);
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
* Orders a triangle list for better vertex cache coherency.
|
|
*/
|
|
template<typename IndexDataType, typename Allocator>
|
|
void CacheOptimizeIndexBuffer(TArray<IndexDataType, Allocator>& Indices)
|
|
{
|
|
static_assert(sizeof(IndexDataType) == 2 || sizeof(IndexDataType) == 4, "Indices must be short or int.");
|
|
bool Is32Bit = sizeof(IndexDataType) == 4;
|
|
|
|
// Count the number of vertices
|
|
uint32 NumVertices = 0;
|
|
for (int32 Index = 0; Index < Indices.Num(); ++Index)
|
|
{
|
|
if (Indices[Index] > NumVertices)
|
|
{
|
|
NumVertices = Indices[Index];
|
|
}
|
|
}
|
|
NumVertices += 1;
|
|
|
|
TArray<uint32> OptimizedIndices;
|
|
OptimizedIndices.AddUninitialized(Indices.Num());
|
|
uint16 CacheSize = 32;
|
|
OptimizeFaces((uint8*)Indices.GetData(), Is32Bit, Indices.Num(), NumVertices, OptimizedIndices.GetData(), CacheSize);
|
|
|
|
if (Is32Bit)
|
|
{
|
|
FMemory::Memcpy(Indices.GetData(), OptimizedIndices.GetData(), Indices.Num() * sizeof(IndexDataType));
|
|
}
|
|
else
|
|
{
|
|
for (int32 I = 0; I < OptimizedIndices.Num(); ++I)
|
|
{
|
|
Indices[I] = (uint16)OptimizedIndices[I];
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
void FMeshUtilities::CacheOptimizeIndexBuffer(TArray<uint16>& Indices)
|
|
{
|
|
if (bUsingNvTriStrip)
|
|
{
|
|
NvTriStrip::CacheOptimizeIndexBuffer(Indices);
|
|
}
|
|
else if (!bDisableTriangleOrderOptimization)
|
|
{
|
|
Forsyth::CacheOptimizeIndexBuffer(Indices);
|
|
}
|
|
}
|
|
|
|
void FMeshUtilities::CacheOptimizeIndexBuffer(TArray<uint32>& Indices)
|
|
{
|
|
if (bUsingNvTriStrip)
|
|
{
|
|
NvTriStrip::CacheOptimizeIndexBuffer(Indices);
|
|
}
|
|
else if (!bDisableTriangleOrderOptimization)
|
|
{
|
|
Forsyth::CacheOptimizeIndexBuffer(Indices);
|
|
}
|
|
}
|
|
|
|
/*------------------------------------------------------------------------------
|
|
NVTessLib for computing adjacency used for tessellation.
|
|
------------------------------------------------------------------------------*/
|
|
|
|
/**
|
|
* Provides static mesh render data to the NVIDIA tessellation library.
|
|
*/
|
|
class FStaticMeshNvRenderBuffer : public nv::RenderBuffer
|
|
{
|
|
public:
|
|
|
|
/** Construct from static mesh render buffers. */
|
|
FStaticMeshNvRenderBuffer(
|
|
const FPositionVertexBuffer& InPositionVertexBuffer,
|
|
const FStaticMeshVertexBuffer& InVertexBuffer,
|
|
const TArray<uint32>& Indices)
|
|
: PositionVertexBuffer(InPositionVertexBuffer)
|
|
, VertexBuffer(InVertexBuffer)
|
|
{
|
|
check(PositionVertexBuffer.GetNumVertices() == VertexBuffer.GetNumVertices());
|
|
mIb = new nv::IndexBuffer((void*)Indices.GetData(), nv::IBT_U32, Indices.Num(), false);
|
|
}
|
|
|
|
/** Retrieve the position and first texture coordinate of the specified index. */
|
|
virtual nv::Vertex getVertex(unsigned int Index) const
|
|
{
|
|
nv::Vertex Vertex;
|
|
|
|
check(Index < PositionVertexBuffer.GetNumVertices());
|
|
|
|
const FVector& Position = PositionVertexBuffer.VertexPosition(Index);
|
|
Vertex.pos.x = Position.X;
|
|
Vertex.pos.y = Position.Y;
|
|
Vertex.pos.z = Position.Z;
|
|
|
|
if (VertexBuffer.GetNumTexCoords())
|
|
{
|
|
const FVector2D UV = VertexBuffer.GetVertexUV(Index, 0);
|
|
Vertex.uv.x = UV.X;
|
|
Vertex.uv.y = UV.Y;
|
|
}
|
|
else
|
|
{
|
|
Vertex.uv.x = 0.0f;
|
|
Vertex.uv.y = 0.0f;
|
|
}
|
|
|
|
return Vertex;
|
|
}
|
|
|
|
private:
|
|
|
|
/** The position vertex buffer for the static mesh. */
|
|
const FPositionVertexBuffer& PositionVertexBuffer;
|
|
|
|
/** The vertex buffer for the static mesh. */
|
|
const FStaticMeshVertexBuffer& VertexBuffer;
|
|
|
|
/** Copying is forbidden. */
|
|
FStaticMeshNvRenderBuffer(const FStaticMeshNvRenderBuffer&);
|
|
FStaticMeshNvRenderBuffer& operator=(const FStaticMeshNvRenderBuffer&);
|
|
};
|
|
|
|
/**
|
|
* Provides skeletal mesh render data to the NVIDIA tessellation library.
|
|
*/
|
|
class FSkeletalMeshNvRenderBuffer : public nv::RenderBuffer
|
|
{
|
|
public:
|
|
|
|
/** Construct from static mesh render buffers. */
|
|
FSkeletalMeshNvRenderBuffer(
|
|
const TArray<FSoftSkinVertex>& InVertexBuffer,
|
|
const uint32 InTexCoordCount,
|
|
const TArray<uint32>& Indices)
|
|
: VertexBuffer(InVertexBuffer)
|
|
, TexCoordCount(InTexCoordCount)
|
|
{
|
|
mIb = new nv::IndexBuffer((void*)Indices.GetData(), nv::IBT_U32, Indices.Num(), false);
|
|
}
|
|
|
|
/** Retrieve the position and first texture coordinate of the specified index. */
|
|
virtual nv::Vertex getVertex(unsigned int Index) const
|
|
{
|
|
nv::Vertex Vertex;
|
|
|
|
check(Index < (unsigned int)VertexBuffer.Num());
|
|
|
|
const FSoftSkinVertex& SrcVertex = VertexBuffer[Index];
|
|
|
|
Vertex.pos.x = SrcVertex.Position.X;
|
|
Vertex.pos.y = SrcVertex.Position.Y;
|
|
Vertex.pos.z = SrcVertex.Position.Z;
|
|
|
|
if (TexCoordCount > 0)
|
|
{
|
|
Vertex.uv.x = SrcVertex.UVs[0].X;
|
|
Vertex.uv.y = SrcVertex.UVs[0].Y;
|
|
}
|
|
else
|
|
{
|
|
Vertex.uv.x = 0.0f;
|
|
Vertex.uv.y = 0.0f;
|
|
}
|
|
|
|
return Vertex;
|
|
}
|
|
|
|
private:
|
|
/** The vertex buffer for the skeletal mesh. */
|
|
const TArray<FSoftSkinVertex>& VertexBuffer;
|
|
const uint32 TexCoordCount;
|
|
|
|
/** Copying is forbidden. */
|
|
FSkeletalMeshNvRenderBuffer(const FSkeletalMeshNvRenderBuffer&);
|
|
FSkeletalMeshNvRenderBuffer& operator=(const FSkeletalMeshNvRenderBuffer&);
|
|
};
|
|
|
|
static void BuildStaticAdjacencyIndexBuffer(
|
|
const FPositionVertexBuffer& PositionVertexBuffer,
|
|
const FStaticMeshVertexBuffer& VertexBuffer,
|
|
const TArray<uint32>& Indices,
|
|
TArray<uint32>& OutPnAenIndices
|
|
)
|
|
{
|
|
if (Indices.Num())
|
|
{
|
|
FStaticMeshNvRenderBuffer StaticMeshRenderBuffer(PositionVertexBuffer, VertexBuffer, Indices);
|
|
nv::IndexBuffer* PnAENIndexBuffer = nv::tess::buildTessellationBuffer(&StaticMeshRenderBuffer, nv::DBM_PnAenDominantCorner, true);
|
|
check(PnAENIndexBuffer);
|
|
const int32 IndexCount = (int32)PnAENIndexBuffer->getLength();
|
|
OutPnAenIndices.Empty(IndexCount);
|
|
OutPnAenIndices.AddUninitialized(IndexCount);
|
|
for (int32 Index = 0; Index < IndexCount; ++Index)
|
|
{
|
|
OutPnAenIndices[Index] = (*PnAENIndexBuffer)[Index];
|
|
}
|
|
delete PnAENIndexBuffer;
|
|
}
|
|
else
|
|
{
|
|
OutPnAenIndices.Empty();
|
|
}
|
|
}
|
|
|
|
void FMeshUtilities::BuildSkeletalAdjacencyIndexBuffer(
|
|
const TArray<FSoftSkinVertex>& VertexBuffer,
|
|
const uint32 TexCoordCount,
|
|
const TArray<uint32>& Indices,
|
|
TArray<uint32>& OutPnAenIndices
|
|
)
|
|
{
|
|
if (Indices.Num())
|
|
{
|
|
FSkeletalMeshNvRenderBuffer SkeletalMeshRenderBuffer(VertexBuffer, TexCoordCount, Indices);
|
|
nv::IndexBuffer* PnAENIndexBuffer = nv::tess::buildTessellationBuffer(&SkeletalMeshRenderBuffer, nv::DBM_PnAenDominantCorner, true);
|
|
check(PnAENIndexBuffer);
|
|
const int32 IndexCount = (int32)PnAENIndexBuffer->getLength();
|
|
OutPnAenIndices.Empty(IndexCount);
|
|
OutPnAenIndices.AddUninitialized(IndexCount);
|
|
for (int32 Index = 0; Index < IndexCount; ++Index)
|
|
{
|
|
OutPnAenIndices[Index] = (*PnAENIndexBuffer)[Index];
|
|
}
|
|
delete PnAENIndexBuffer;
|
|
}
|
|
else
|
|
{
|
|
OutPnAenIndices.Empty();
|
|
}
|
|
}
|
|
|
|
void FMeshUtilities::RechunkSkeletalMeshModels(USkeletalMesh* SrcMesh, int32 MaxBonesPerChunk)
|
|
{
|
|
#if WITH_EDITORONLY_DATA
|
|
TIndirectArray<FStaticLODModel> DestModels;
|
|
TIndirectArray<FSkinnedModelData> ModelData;
|
|
FReferenceSkeleton RefSkeleton = SrcMesh->RefSkeleton;
|
|
uint32 VertexBufferBuildFlags = SrcMesh->GetVertexBufferFlags();
|
|
FSkeletalMeshResource* SrcMeshResource = SrcMesh->GetImportedResource();
|
|
FVector TriangleSortCenter;
|
|
bool bHaveTriangleSortCenter = SrcMesh->GetSortCenterPoint(TriangleSortCenter);
|
|
|
|
for (int32 ModelIndex = 0; ModelIndex < SrcMeshResource->LODModels.Num(); ++ModelIndex)
|
|
{
|
|
FSkinnedModelData& TmpModelData = *new(ModelData)FSkinnedModelData();
|
|
SkeletalMeshTools::CopySkinnedModelData(TmpModelData, SrcMeshResource->LODModels[ModelIndex]);
|
|
}
|
|
|
|
for (int32 ModelIndex = 0; ModelIndex < ModelData.Num(); ++ModelIndex)
|
|
{
|
|
TArray<FSkinnedMeshChunk*> Chunks;
|
|
TArray<int32> PointToOriginalMap;
|
|
TArray<ETriangleSortOption> SectionSortOptions;
|
|
|
|
const FSkinnedModelData& SrcModel = ModelData[ModelIndex];
|
|
FStaticLODModel& DestModel = *new(DestModels)FStaticLODModel();
|
|
|
|
SkeletalMeshTools::UnchunkSkeletalModel(Chunks, PointToOriginalMap, SrcModel);
|
|
SkeletalMeshTools::ChunkSkinnedVertices(Chunks, MaxBonesPerChunk);
|
|
|
|
for (int32 ChunkIndex = 0; ChunkIndex < Chunks.Num(); ++ChunkIndex)
|
|
{
|
|
int32 SectionIndex = Chunks[ChunkIndex]->OriginalSectionIndex;
|
|
SectionSortOptions.Add(SrcModel.Sections[SectionIndex].TriangleSorting);
|
|
}
|
|
check(SectionSortOptions.Num() == Chunks.Num());
|
|
|
|
BuildSkeletalModelFromChunks(DestModel, RefSkeleton, Chunks, PointToOriginalMap);
|
|
check(DestModel.Sections.Num() == SectionSortOptions.Num());
|
|
|
|
DestModel.NumTexCoords = SrcModel.NumTexCoords;
|
|
DestModel.BuildVertexBuffers(VertexBufferBuildFlags);
|
|
for (int32 SectionIndex = 0; SectionIndex < DestModel.Sections.Num(); ++SectionIndex)
|
|
{
|
|
DestModel.SortTriangles(TriangleSortCenter, bHaveTriangleSortCenter, SectionIndex, SectionSortOptions[SectionIndex]);
|
|
}
|
|
}
|
|
|
|
//@todo-rco: Swap() doesn't seem to work
|
|
Exchange(SrcMeshResource->LODModels, DestModels);
|
|
|
|
// TODO: Also need to patch bEnableShadowCasting in the LODInfo struct.
|
|
#endif // #if WITH_EDITORONLY_DATA
|
|
}
|
|
|
|
void FMeshUtilities::CalcBoneVertInfos(USkeletalMesh* SkeletalMesh, TArray<FBoneVertInfo>& Infos, bool bOnlyDominant)
|
|
{
|
|
SkeletalMeshTools::CalcBoneVertInfos(SkeletalMesh, Infos, bOnlyDominant);
|
|
}
|
|
|
|
// Helper function for ConvertMeshesToStaticMesh
|
|
static void AddOrDuplicateMaterial(UMaterialInterface* InMaterialInterface, const FString& InPackageName, TArray<UMaterialInterface*>& OutMaterials)
|
|
{
|
|
if (InMaterialInterface && !InMaterialInterface->GetOuter()->IsA<UPackage>())
|
|
{
|
|
// Convert runtime material instances to new concrete material instances
|
|
// Create new package
|
|
FString OriginalMaterialName = InMaterialInterface->GetName();
|
|
FString MaterialPath = FPackageName::GetLongPackagePath(InPackageName) / OriginalMaterialName;
|
|
FString MaterialName;
|
|
FAssetToolsModule& AssetToolsModule = FModuleManager::LoadModuleChecked<FAssetToolsModule>("AssetTools");
|
|
AssetToolsModule.Get().CreateUniqueAssetName(MaterialPath, TEXT(""), MaterialPath, MaterialName);
|
|
UPackage* MaterialPackage = CreatePackage(NULL, *MaterialPath);
|
|
|
|
// Duplicate the object into the new package
|
|
UMaterialInterface* NewMaterialInterface = DuplicateObject<UMaterialInterface>(InMaterialInterface, MaterialPackage, *MaterialName);
|
|
NewMaterialInterface->SetFlags(RF_Public | RF_Standalone);
|
|
|
|
if (UMaterialInstanceDynamic* MaterialInstanceDynamic = Cast<UMaterialInstanceDynamic>(NewMaterialInterface))
|
|
{
|
|
UMaterialInstanceDynamic* OldMaterialInstanceDynamic = CastChecked<UMaterialInstanceDynamic>(InMaterialInterface);
|
|
MaterialInstanceDynamic->K2_CopyMaterialInstanceParameters(OldMaterialInstanceDynamic);
|
|
}
|
|
|
|
NewMaterialInterface->MarkPackageDirty();
|
|
|
|
FAssetRegistryModule::AssetCreated(NewMaterialInterface);
|
|
|
|
InMaterialInterface = NewMaterialInterface;
|
|
}
|
|
|
|
OutMaterials.Add(InMaterialInterface);
|
|
}
|
|
|
|
// Helper function for ConvertMeshesToStaticMesh
|
|
template <typename ComponentType>
|
|
static void ProcessMaterials(ComponentType* InComponent, const FString& InPackageName, TArray<UMaterialInterface*>& OutMaterials)
|
|
{
|
|
const int32 NumMaterials = InComponent->GetNumMaterials();
|
|
for (int32 MaterialIndex = 0; MaterialIndex < NumMaterials; MaterialIndex++)
|
|
{
|
|
UMaterialInterface* MaterialInterface = InComponent->GetMaterial(MaterialIndex);
|
|
AddOrDuplicateMaterial(MaterialInterface, InPackageName, OutMaterials);
|
|
}
|
|
}
|
|
|
|
// Helper function for ConvertMeshesToStaticMesh
|
|
static bool IsValidSkinnedMeshComponent(USkinnedMeshComponent* InComponent)
|
|
{
|
|
return InComponent && InComponent->MeshObject && InComponent->IsVisible();
|
|
}
|
|
|
|
/** Helper struct for tracking validity of optional buffers */
|
|
struct FRawMeshTracker
|
|
{
|
|
FRawMeshTracker()
|
|
: bValidColors(false)
|
|
{
|
|
FMemory::Memset(bValidTexCoords, 0);
|
|
}
|
|
|
|
bool bValidTexCoords[MAX_MESH_TEXTURE_COORDS];
|
|
bool bValidColors;
|
|
};
|
|
|
|
// Helper function for ConvertMeshesToStaticMesh
|
|
static void SkinnedMeshToRawMeshes(USkinnedMeshComponent* InSkinnedMeshComponent, int32 InOverallMaxLODs, const FMatrix& InComponentToWorld, const FString& InPackageName, TArray<FRawMeshTracker>& OutRawMeshTrackers, TArray<FRawMesh>& OutRawMeshes, TArray<UMaterialInterface*>& OutMaterials)
|
|
{
|
|
const int32 BaseMaterialIndex = OutMaterials.Num();
|
|
|
|
// Export all LODs to raw meshes
|
|
const int32 NumLODs = InSkinnedMeshComponent->MeshObject->GetSkeletalMeshResource().LODModels.Num();
|
|
|
|
for (int32 OverallLODIndex = 0; OverallLODIndex < InOverallMaxLODs; OverallLODIndex++)
|
|
{
|
|
int32 LODIndexRead = FMath::Min(OverallLODIndex, NumLODs - 1);
|
|
|
|
FRawMesh& RawMesh = OutRawMeshes[OverallLODIndex];
|
|
FRawMeshTracker& RawMeshTracker = OutRawMeshTrackers[OverallLODIndex];
|
|
const int32 BaseVertexIndex = RawMesh.VertexPositions.Num();
|
|
|
|
FSkeletalMeshLODInfo& SrcLODInfo = InSkinnedMeshComponent->SkeletalMesh->LODInfo[LODIndexRead];
|
|
|
|
// Get the CPU skinned verts for this LOD
|
|
TArray<FFinalSkinVertex> FinalVertices;
|
|
InSkinnedMeshComponent->GetCPUSkinnedVertices(FinalVertices, LODIndexRead);
|
|
|
|
FSkeletalMeshResource& SkeletalMeshResource = InSkinnedMeshComponent->MeshObject->GetSkeletalMeshResource();
|
|
FStaticLODModel& StaticLODModel = SkeletalMeshResource.LODModels[LODIndexRead];
|
|
|
|
// Copy skinned vertex positions
|
|
for (int32 VertIndex = 0; VertIndex < FinalVertices.Num(); ++VertIndex)
|
|
{
|
|
RawMesh.VertexPositions.Add(InComponentToWorld.TransformPosition(FinalVertices[VertIndex].Position));
|
|
}
|
|
|
|
const uint32 NumTexCoords = FMath::Min(StaticLODModel.VertexBufferGPUSkin.GetNumTexCoords(), (uint32)MAX_MESH_TEXTURE_COORDS);
|
|
const int32 NumSections = StaticLODModel.Sections.Num();
|
|
FRawStaticIndexBuffer16or32Interface& IndexBuffer = *StaticLODModel.MultiSizeIndexContainer.GetIndexBuffer();
|
|
|
|
for (int32 SectionIndex = 0; SectionIndex < NumSections; SectionIndex++)
|
|
{
|
|
const FSkelMeshSection& SkelMeshSection = StaticLODModel.Sections[SectionIndex];
|
|
if (!SkelMeshSection.bDisabled)
|
|
{
|
|
// Build 'wedge' info
|
|
const int32 NumWedges = SkelMeshSection.NumTriangles * 3;
|
|
for(int32 WedgeIndex = 0; WedgeIndex < NumWedges; WedgeIndex++)
|
|
{
|
|
const int32 VertexIndexForWedge = IndexBuffer.Get(SkelMeshSection.BaseIndex + WedgeIndex);
|
|
|
|
RawMesh.WedgeIndices.Add(BaseVertexIndex + VertexIndexForWedge);
|
|
|
|
const FFinalSkinVertex& SkinnedVertex = FinalVertices[VertexIndexForWedge];
|
|
const FVector TangentX = InComponentToWorld.TransformVector(SkinnedVertex.TangentX);
|
|
const FVector TangentZ = InComponentToWorld.TransformVector(SkinnedVertex.TangentZ);
|
|
const FVector4 UnpackedTangentZ = SkinnedVertex.TangentZ;
|
|
const FVector TangentY = (TangentX ^ TangentZ).GetSafeNormal() * UnpackedTangentZ.W;
|
|
|
|
RawMesh.WedgeTangentX.Add(TangentX);
|
|
RawMesh.WedgeTangentY.Add(TangentY);
|
|
RawMesh.WedgeTangentZ.Add(TangentZ);
|
|
|
|
for (uint32 TexCoordIndex = 0; TexCoordIndex < MAX_MESH_TEXTURE_COORDS; TexCoordIndex++)
|
|
{
|
|
if (TexCoordIndex >= NumTexCoords)
|
|
{
|
|
RawMesh.WedgeTexCoords[TexCoordIndex].AddDefaulted();
|
|
}
|
|
else
|
|
{
|
|
RawMesh.WedgeTexCoords[TexCoordIndex].Add(StaticLODModel.VertexBufferGPUSkin.GetVertexUV(VertexIndexForWedge, TexCoordIndex));
|
|
RawMeshTracker.bValidTexCoords[TexCoordIndex] = true;
|
|
}
|
|
}
|
|
|
|
if (StaticLODModel.ColorVertexBuffer.IsInitialized())
|
|
{
|
|
RawMesh.WedgeColors.Add(StaticLODModel.ColorVertexBuffer.VertexColor(VertexIndexForWedge));
|
|
RawMeshTracker.bValidColors = true;
|
|
}
|
|
else
|
|
{
|
|
RawMesh.WedgeColors.Add(FColor::White);
|
|
}
|
|
}
|
|
|
|
int32 MaterialIndex = SkelMeshSection.MaterialIndex;
|
|
// use the remapping of material indices for all LODs besides the base LOD
|
|
if (LODIndexRead > 0 && SrcLODInfo.LODMaterialMap.IsValidIndex(SkelMeshSection.MaterialIndex))
|
|
{
|
|
MaterialIndex = FMath::Clamp<int32>(SrcLODInfo.LODMaterialMap[SkelMeshSection.MaterialIndex], 0, InSkinnedMeshComponent->SkeletalMesh->Materials.Num());
|
|
}
|
|
|
|
// copy face info
|
|
for (uint32 TriIndex = 0; TriIndex < SkelMeshSection.NumTriangles; TriIndex++)
|
|
{
|
|
RawMesh.FaceMaterialIndices.Add(BaseMaterialIndex + MaterialIndex);
|
|
RawMesh.FaceSmoothingMasks.Add(0); // Assume this is ignored as bRecomputeNormals is false
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
ProcessMaterials<USkinnedMeshComponent>(InSkinnedMeshComponent, InPackageName, OutMaterials);
|
|
}
|
|
|
|
// Helper function for ConvertMeshesToStaticMesh
|
|
static bool IsValidStaticMeshComponent(UStaticMeshComponent* InComponent)
|
|
{
|
|
return InComponent && InComponent->GetStaticMesh() && InComponent->GetStaticMesh()->RenderData && InComponent->IsVisible();
|
|
}
|
|
|
|
// Helper function for ConvertMeshesToStaticMesh
|
|
static void StaticMeshToRawMeshes(UStaticMeshComponent* InStaticMeshComponent, int32 InOverallMaxLODs, const FMatrix& InComponentToWorld, const FString& InPackageName, TArray<FRawMeshTracker>& OutRawMeshTrackers, TArray<FRawMesh>& OutRawMeshes, TArray<UMaterialInterface*>& OutMaterials)
|
|
{
|
|
const int32 BaseMaterialIndex = OutMaterials.Num();
|
|
|
|
const int32 NumLODs = InStaticMeshComponent->GetStaticMesh()->RenderData->LODResources.Num();
|
|
|
|
for (int32 OverallLODIndex = 0; OverallLODIndex < InOverallMaxLODs; OverallLODIndex++)
|
|
{
|
|
int32 LODIndexRead = FMath::Min(OverallLODIndex, NumLODs - 1);
|
|
|
|
FRawMesh& RawMesh = OutRawMeshes[OverallLODIndex];
|
|
FRawMeshTracker& RawMeshTracker = OutRawMeshTrackers[OverallLODIndex];
|
|
const FStaticMeshLODResources& LODResource = InStaticMeshComponent->GetStaticMesh()->RenderData->LODResources[LODIndexRead];
|
|
const int32 BaseVertexIndex = RawMesh.VertexPositions.Num();
|
|
|
|
for (int32 VertIndex = 0; VertIndex < LODResource.GetNumVertices(); ++VertIndex)
|
|
{
|
|
RawMesh.VertexPositions.Add(InComponentToWorld.TransformPosition(LODResource.PositionVertexBuffer.VertexPosition((uint32)VertIndex)));
|
|
}
|
|
|
|
const FIndexArrayView IndexArrayView = LODResource.IndexBuffer.GetArrayView();
|
|
const FStaticMeshVertexBuffer& StaticMeshVertexBuffer = LODResource.VertexBuffer;
|
|
const int32 NumTexCoords = FMath::Min(StaticMeshVertexBuffer.GetNumTexCoords(), (uint32)MAX_MESH_TEXTURE_COORDS);
|
|
const int32 NumSections = LODResource.Sections.Num();
|
|
|
|
for (int32 SectionIndex = 0; SectionIndex < NumSections; SectionIndex++)
|
|
{
|
|
const FStaticMeshSection& StaticMeshSection = LODResource.Sections[SectionIndex];
|
|
|
|
const int32 NumIndices = StaticMeshSection.NumTriangles * 3;
|
|
for (int32 IndexIndex = 0; IndexIndex < NumIndices; IndexIndex++)
|
|
{
|
|
int32 Index = IndexArrayView[StaticMeshSection.FirstIndex + IndexIndex];
|
|
RawMesh.WedgeIndices.Add(BaseVertexIndex + Index);
|
|
|
|
RawMesh.WedgeTangentX.Add(InComponentToWorld.TransformVector(StaticMeshVertexBuffer.VertexTangentX(Index)));
|
|
RawMesh.WedgeTangentY.Add(InComponentToWorld.TransformVector(StaticMeshVertexBuffer.VertexTangentY(Index)));
|
|
RawMesh.WedgeTangentZ.Add(InComponentToWorld.TransformVector(StaticMeshVertexBuffer.VertexTangentZ(Index)));
|
|
|
|
for (int32 TexCoordIndex = 0; TexCoordIndex < MAX_MESH_TEXTURE_COORDS; TexCoordIndex++)
|
|
{
|
|
if (TexCoordIndex >= NumTexCoords)
|
|
{
|
|
RawMesh.WedgeTexCoords[TexCoordIndex].AddDefaulted();
|
|
}
|
|
else
|
|
{
|
|
RawMesh.WedgeTexCoords[TexCoordIndex].Add(StaticMeshVertexBuffer.GetVertexUV(Index, TexCoordIndex));
|
|
RawMeshTracker.bValidTexCoords[TexCoordIndex] = true;
|
|
}
|
|
}
|
|
|
|
if (LODResource.ColorVertexBuffer.IsInitialized())
|
|
{
|
|
RawMesh.WedgeColors.Add(LODResource.ColorVertexBuffer.VertexColor(Index));
|
|
RawMeshTracker.bValidColors = true;
|
|
}
|
|
else
|
|
{
|
|
RawMesh.WedgeColors.Add(FColor::White);
|
|
}
|
|
}
|
|
|
|
// copy face info
|
|
for (uint32 TriIndex = 0; TriIndex < StaticMeshSection.NumTriangles; TriIndex++)
|
|
{
|
|
RawMesh.FaceMaterialIndices.Add(BaseMaterialIndex + StaticMeshSection.MaterialIndex);
|
|
RawMesh.FaceSmoothingMasks.Add(0); // Assume this is ignored as bRecomputeNormals is false
|
|
}
|
|
}
|
|
}
|
|
|
|
ProcessMaterials<UStaticMeshComponent>(InStaticMeshComponent, InPackageName, OutMaterials);
|
|
}
|
|
|
|
UStaticMesh* FMeshUtilities::ConvertMeshesToStaticMesh(const TArray<UMeshComponent*>& InMeshComponents, const FTransform& InRootTransform, const FString& InPackageName)
|
|
{
|
|
// Build a package name to use
|
|
FString MeshName;
|
|
FString PackageName;
|
|
if (InPackageName.IsEmpty())
|
|
{
|
|
FString NewNameSuggestion = FString(TEXT("StaticMesh"));
|
|
FString PackageNameSuggestion = FString(TEXT("/Game/Meshes/")) + NewNameSuggestion;
|
|
FString Name;
|
|
FAssetToolsModule& AssetToolsModule = FModuleManager::LoadModuleChecked<FAssetToolsModule>("AssetTools");
|
|
AssetToolsModule.Get().CreateUniqueAssetName(PackageNameSuggestion, TEXT(""), PackageNameSuggestion, Name);
|
|
|
|
TSharedPtr<SDlgPickAssetPath> PickAssetPathWidget =
|
|
SNew(SDlgPickAssetPath)
|
|
.Title(LOCTEXT("ConvertToStaticMeshPickName", "Choose New StaticMesh Location"))
|
|
.DefaultAssetPath(FText::FromString(PackageNameSuggestion));
|
|
|
|
if (PickAssetPathWidget->ShowModal() == EAppReturnType::Ok)
|
|
{
|
|
// Get the full name of where we want to create the mesh asset.
|
|
PackageName = PickAssetPathWidget->GetFullAssetPath().ToString();
|
|
MeshName = FPackageName::GetLongPackageAssetName(PackageName);
|
|
|
|
// Check if the user inputed a valid asset name, if they did not, give it the generated default name
|
|
if (MeshName.IsEmpty())
|
|
{
|
|
// Use the defaults that were already generated.
|
|
PackageName = PackageNameSuggestion;
|
|
MeshName = *Name;
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
PackageName = InPackageName;
|
|
MeshName = *FPackageName::GetLongPackageAssetName(PackageName);
|
|
}
|
|
|
|
if(!PackageName.IsEmpty() && !MeshName.IsEmpty())
|
|
{
|
|
TArray<FRawMesh> RawMeshes;
|
|
TArray<UMaterialInterface*> Materials;
|
|
|
|
TArray<FRawMeshTracker> RawMeshTrackers;
|
|
|
|
FMatrix WorldToRoot = InRootTransform.ToMatrixWithScale().Inverse();
|
|
|
|
// first do a pass to determine the max LOD level we will be combining meshes into
|
|
int32 OverallMaxLODs = 0;
|
|
for (UMeshComponent* MeshComponent : InMeshComponents)
|
|
{
|
|
USkinnedMeshComponent* SkinnedMeshComponent = Cast<USkinnedMeshComponent>(MeshComponent);
|
|
UStaticMeshComponent* StaticMeshComponent = Cast<UStaticMeshComponent>(MeshComponent);
|
|
|
|
if (IsValidSkinnedMeshComponent(SkinnedMeshComponent))
|
|
{
|
|
OverallMaxLODs = FMath::Max(SkinnedMeshComponent->MeshObject->GetSkeletalMeshResource().LODModels.Num(), OverallMaxLODs);
|
|
}
|
|
else if(IsValidStaticMeshComponent(StaticMeshComponent))
|
|
{
|
|
OverallMaxLODs = FMath::Max(StaticMeshComponent->GetStaticMesh()->RenderData->LODResources.Num(), OverallMaxLODs);
|
|
}
|
|
}
|
|
|
|
// Resize raw meshes to accommodate the number of LODs we will need
|
|
RawMeshes.SetNum(OverallMaxLODs);
|
|
RawMeshTrackers.SetNum(OverallMaxLODs);
|
|
|
|
// Export all visible components
|
|
for (UMeshComponent* MeshComponent : InMeshComponents)
|
|
{
|
|
FMatrix ComponentToWorld = MeshComponent->GetComponentTransform().ToMatrixWithScale() * WorldToRoot;
|
|
|
|
USkinnedMeshComponent* SkinnedMeshComponent = Cast<USkinnedMeshComponent>(MeshComponent);
|
|
UStaticMeshComponent* StaticMeshComponent = Cast<UStaticMeshComponent>(MeshComponent);
|
|
|
|
if (IsValidSkinnedMeshComponent(SkinnedMeshComponent))
|
|
{
|
|
SkinnedMeshToRawMeshes(SkinnedMeshComponent, OverallMaxLODs, ComponentToWorld, PackageName, RawMeshTrackers, RawMeshes, Materials);
|
|
}
|
|
else if (IsValidStaticMeshComponent(StaticMeshComponent))
|
|
{
|
|
StaticMeshToRawMeshes(StaticMeshComponent, OverallMaxLODs, ComponentToWorld, PackageName, RawMeshTrackers, RawMeshes, Materials);
|
|
}
|
|
}
|
|
|
|
uint32 MaxInUseTextureCoordinate = 0;
|
|
|
|
// scrub invalid vert color & tex coord data
|
|
check(RawMeshes.Num() == RawMeshTrackers.Num());
|
|
for (int32 RawMeshIndex = 0; RawMeshIndex < RawMeshes.Num(); RawMeshIndex++)
|
|
{
|
|
if (!RawMeshTrackers[RawMeshIndex].bValidColors)
|
|
{
|
|
RawMeshes[RawMeshIndex].WedgeColors.Empty();
|
|
}
|
|
|
|
for (uint32 TexCoordIndex = 0; TexCoordIndex < MAX_MESH_TEXTURE_COORDS; TexCoordIndex++)
|
|
{
|
|
if (!RawMeshTrackers[RawMeshIndex].bValidTexCoords[TexCoordIndex])
|
|
{
|
|
RawMeshes[RawMeshIndex].WedgeTexCoords[TexCoordIndex].Empty();
|
|
}
|
|
else
|
|
{
|
|
// Store first texture coordinate index not in use
|
|
MaxInUseTextureCoordinate = FMath::Max(MaxInUseTextureCoordinate, TexCoordIndex);
|
|
}
|
|
}
|
|
}
|
|
|
|
// Check if we got some valid data.
|
|
bool bValidData = false;
|
|
for (FRawMesh& RawMesh : RawMeshes)
|
|
{
|
|
if (RawMesh.IsValidOrFixable())
|
|
{
|
|
bValidData = true;
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (bValidData)
|
|
{
|
|
// Then find/create it.
|
|
UPackage* Package = CreatePackage(NULL, *PackageName);
|
|
check(Package);
|
|
|
|
// Create StaticMesh object
|
|
UStaticMesh* StaticMesh = NewObject<UStaticMesh>(Package, *MeshName, RF_Public | RF_Standalone);
|
|
StaticMesh->InitResources();
|
|
|
|
StaticMesh->LightingGuid = FGuid::NewGuid();
|
|
|
|
// Determine which texture coordinate map should be used for storing/generating the lightmap UVs
|
|
const uint32 LightMapIndex = FMath::Min(MaxInUseTextureCoordinate + 1, (uint32)MAX_MESH_TEXTURE_COORDS - 1);
|
|
|
|
// Add source to new StaticMesh
|
|
for (FRawMesh& RawMesh : RawMeshes)
|
|
{
|
|
if (RawMesh.IsValidOrFixable())
|
|
{
|
|
FStaticMeshSourceModel* SrcModel = new (StaticMesh->SourceModels) FStaticMeshSourceModel();
|
|
SrcModel->BuildSettings.bRecomputeNormals = false;
|
|
SrcModel->BuildSettings.bRecomputeTangents = false;
|
|
SrcModel->BuildSettings.bRemoveDegenerates = true;
|
|
SrcModel->BuildSettings.bUseHighPrecisionTangentBasis = false;
|
|
SrcModel->BuildSettings.bUseFullPrecisionUVs = false;
|
|
SrcModel->BuildSettings.bGenerateLightmapUVs = true;
|
|
SrcModel->BuildSettings.SrcLightmapIndex = 0;
|
|
SrcModel->BuildSettings.DstLightmapIndex = LightMapIndex;
|
|
SrcModel->RawMeshBulkData->SaveRawMesh(RawMesh);
|
|
}
|
|
}
|
|
|
|
// Copy materials to new mesh
|
|
for(UMaterialInterface* Material : Materials)
|
|
{
|
|
StaticMesh->StaticMaterials.Add(FStaticMaterial(Material));
|
|
}
|
|
|
|
//Set the Imported version before calling the build
|
|
StaticMesh->ImportVersion = EImportStaticMeshVersion::LastVersion;
|
|
|
|
// Set light map coordinate index to match DstLightmapIndex
|
|
StaticMesh->LightMapCoordinateIndex = LightMapIndex;
|
|
|
|
// setup section info map
|
|
for (int32 RawMeshLODIndex = 0; RawMeshLODIndex < RawMeshes.Num(); RawMeshLODIndex++)
|
|
{
|
|
const FRawMesh& RawMesh = RawMeshes[RawMeshLODIndex];
|
|
TArray<int32> UniqueMaterialIndices;
|
|
for (int32 MaterialIndex : RawMesh.FaceMaterialIndices)
|
|
{
|
|
UniqueMaterialIndices.AddUnique(MaterialIndex);
|
|
}
|
|
|
|
int32 SectionIndex = 0;
|
|
for (int32 UniqueMaterialIndex : UniqueMaterialIndices)
|
|
{
|
|
StaticMesh->SectionInfoMap.Set(RawMeshLODIndex, SectionIndex, FMeshSectionInfo(UniqueMaterialIndex));
|
|
SectionIndex++;
|
|
}
|
|
}
|
|
|
|
// Build mesh from source
|
|
StaticMesh->Build(false);
|
|
StaticMesh->PostEditChange();
|
|
|
|
StaticMesh->MarkPackageDirty();
|
|
|
|
// Notify asset registry of new asset
|
|
FAssetRegistryModule::AssetCreated(StaticMesh);
|
|
|
|
// Display notification so users can quickly access the mesh
|
|
if (GIsEditor)
|
|
{
|
|
FNotificationInfo Info(FText::Format(LOCTEXT("SkeletalMeshConverted", "Successfully Converted Mesh"), FText::FromString(StaticMesh->GetName())));
|
|
Info.ExpireDuration = 8.0f;
|
|
Info.bUseLargeFont = false;
|
|
Info.Hyperlink = FSimpleDelegate::CreateLambda([=]() { FAssetEditorManager::Get().OpenEditorForAssets(TArray<UObject*>({ StaticMesh })); });
|
|
Info.HyperlinkText = FText::Format(LOCTEXT("OpenNewAnimationHyperlink", "Open {0}"), FText::FromString(StaticMesh->GetName()));
|
|
TSharedPtr<SNotificationItem> Notification = FSlateNotificationManager::Get().AddNotification(Info);
|
|
if ( Notification.IsValid() )
|
|
{
|
|
Notification->SetCompletionState( SNotificationItem::CS_Success );
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return nullptr;
|
|
}
|
|
|
|
/**
|
|
* Builds a renderable skeletal mesh LOD model. Note that the array of chunks
|
|
* will be destroyed during this process!
|
|
* @param LODModel Upon return contains a renderable skeletal mesh LOD model.
|
|
* @param RefSkeleton The reference skeleton associated with the model.
|
|
* @param Chunks Skinned mesh chunks from which to build the renderable model.
|
|
* @param PointToOriginalMap Maps a vertex's RawPointIdx to its index at import time.
|
|
*/
|
|
void FMeshUtilities::BuildSkeletalModelFromChunks(FStaticLODModel& LODModel, const FReferenceSkeleton& RefSkeleton, TArray<FSkinnedMeshChunk*>& Chunks, const TArray<int32>& PointToOriginalMap)
|
|
{
|
|
#if WITH_EDITORONLY_DATA
|
|
// Clear out any data currently held in the LOD model.
|
|
LODModel.Sections.Empty();
|
|
LODModel.NumVertices = 0;
|
|
if (LODModel.MultiSizeIndexContainer.IsIndexBufferValid())
|
|
{
|
|
LODModel.MultiSizeIndexContainer.GetIndexBuffer()->Empty();
|
|
}
|
|
|
|
// Setup the section and chunk arrays on the model.
|
|
for (int32 ChunkIndex = 0; ChunkIndex < Chunks.Num(); ++ChunkIndex)
|
|
{
|
|
FSkinnedMeshChunk* SrcChunk = Chunks[ChunkIndex];
|
|
|
|
FSkelMeshSection& Section = *new(LODModel.Sections) FSkelMeshSection();
|
|
Section.MaterialIndex = SrcChunk->MaterialIndex;
|
|
Exchange(Section.BoneMap, SrcChunk->BoneMap);
|
|
|
|
// Update the active bone indices on the LOD model.
|
|
for (int32 BoneIndex = 0; BoneIndex < Section.BoneMap.Num(); ++BoneIndex)
|
|
{
|
|
LODModel.ActiveBoneIndices.AddUnique(Section.BoneMap[BoneIndex]);
|
|
}
|
|
}
|
|
|
|
LODModel.ActiveBoneIndices.Sort();
|
|
|
|
// Reset 'final vertex to import vertex' map info
|
|
LODModel.MeshToImportVertexMap.Empty();
|
|
LODModel.MaxImportVertex = 0;
|
|
|
|
// Keep track of index mapping to chunk vertex offsets
|
|
TArray< TArray<uint32> > VertexIndexRemap;
|
|
VertexIndexRemap.Empty(LODModel.Sections.Num());
|
|
// Pack the chunk vertices into a single vertex buffer.
|
|
TArray<uint32> RawPointIndices;
|
|
LODModel.NumVertices = 0;
|
|
|
|
int32 PrevMaterialIndex = -1;
|
|
int32 CurrentChunkBaseVertexIndex = -1; // base vertex index for all chunks of the same material
|
|
int32 CurrentChunkVertexCount = -1; // total vertex count for all chunks of the same material
|
|
int32 CurrentVertexIndex = 0; // current vertex index added to the index buffer for all chunks of the same material
|
|
|
|
// rearrange the vert order to minimize the data fetched by the GPU
|
|
for (int32 SectionIndex = 0; SectionIndex < LODModel.Sections.Num(); SectionIndex++)
|
|
{
|
|
if (IsInGameThread())
|
|
{
|
|
GWarn->StatusUpdate(SectionIndex, LODModel.Sections.Num(), NSLOCTEXT("UnrealEd", "ProcessingSections", "Processing Sections"));
|
|
}
|
|
|
|
FSkinnedMeshChunk* SrcChunk = Chunks[SectionIndex];
|
|
FSkelMeshSection& Section = LODModel.Sections[SectionIndex];
|
|
TArray<FSoftSkinBuildVertex>& ChunkVertices = SrcChunk->Vertices;
|
|
TArray<uint32>& ChunkIndices = SrcChunk->Indices;
|
|
|
|
// Reorder the section index buffer for better vertex cache efficiency.
|
|
CacheOptimizeIndexBuffer(ChunkIndices);
|
|
|
|
// Calculate the number of triangles in the section. Note that CacheOptimize may change the number of triangles in the index buffer!
|
|
Section.NumTriangles = ChunkIndices.Num() / 3;
|
|
TArray<FSoftSkinBuildVertex> OriginalVertices;
|
|
Exchange(ChunkVertices, OriginalVertices);
|
|
ChunkVertices.AddUninitialized(OriginalVertices.Num());
|
|
|
|
TArray<int32> IndexCache;
|
|
IndexCache.AddUninitialized(ChunkVertices.Num());
|
|
FMemory::Memset(IndexCache.GetData(), INDEX_NONE, IndexCache.Num() * IndexCache.GetTypeSize());
|
|
int32 NextAvailableIndex = 0;
|
|
// Go through the indices and assign them new values that are coherent where possible
|
|
for (int32 Index = 0; Index < ChunkIndices.Num(); Index++)
|
|
{
|
|
const int32 OriginalIndex = ChunkIndices[Index];
|
|
const int32 CachedIndex = IndexCache[OriginalIndex];
|
|
|
|
if (CachedIndex == INDEX_NONE)
|
|
{
|
|
// No new index has been allocated for this existing index, assign a new one
|
|
ChunkIndices[Index] = NextAvailableIndex;
|
|
// Mark what this index has been assigned to
|
|
IndexCache[OriginalIndex] = NextAvailableIndex;
|
|
NextAvailableIndex++;
|
|
}
|
|
else
|
|
{
|
|
// Reuse an existing index assignment
|
|
ChunkIndices[Index] = CachedIndex;
|
|
}
|
|
// Reorder the vertices based on the new index assignment
|
|
ChunkVertices[ChunkIndices[Index]] = OriginalVertices[OriginalIndex];
|
|
}
|
|
}
|
|
|
|
// Build the arrays of rigid and soft vertices on the model's chunks.
|
|
for (int32 SectionIndex = 0; SectionIndex < LODModel.Sections.Num(); SectionIndex++)
|
|
{
|
|
FSkelMeshSection& Section = LODModel.Sections[SectionIndex];
|
|
TArray<FSoftSkinBuildVertex>& ChunkVertices = Chunks[SectionIndex]->Vertices;
|
|
|
|
if (IsInGameThread())
|
|
{
|
|
// Only update status if in the game thread. When importing morph targets, this function can run in another thread
|
|
GWarn->StatusUpdate(SectionIndex, LODModel.Sections.Num(), NSLOCTEXT("UnrealEd", "ProcessingChunks", "Processing Chunks"));
|
|
}
|
|
|
|
CurrentVertexIndex = 0;
|
|
CurrentChunkVertexCount = 0;
|
|
PrevMaterialIndex = Section.MaterialIndex;
|
|
|
|
// Calculate the offset to this chunk's vertices in the vertex buffer.
|
|
Section.BaseVertexIndex = CurrentChunkBaseVertexIndex = LODModel.NumVertices;
|
|
|
|
// Update the size of the vertex buffer.
|
|
LODModel.NumVertices += ChunkVertices.Num();
|
|
|
|
// Separate the section's vertices into rigid and soft vertices.
|
|
TArray<uint32>& ChunkVertexIndexRemap = *new(VertexIndexRemap)TArray<uint32>();
|
|
ChunkVertexIndexRemap.AddUninitialized(ChunkVertices.Num());
|
|
|
|
for (int32 VertexIndex = 0; VertexIndex < ChunkVertices.Num(); VertexIndex++)
|
|
{
|
|
const FSoftSkinBuildVertex& SoftVertex = ChunkVertices[VertexIndex];
|
|
|
|
FSoftSkinVertex NewVertex;
|
|
NewVertex.Position = SoftVertex.Position;
|
|
NewVertex.TangentX = SoftVertex.TangentX;
|
|
NewVertex.TangentY = SoftVertex.TangentY;
|
|
NewVertex.TangentZ = SoftVertex.TangentZ;
|
|
FMemory::Memcpy(NewVertex.UVs, SoftVertex.UVs, sizeof(FVector2D)*MAX_TEXCOORDS);
|
|
NewVertex.Color = SoftVertex.Color;
|
|
for (int32 i = 0; i < MAX_TOTAL_INFLUENCES; ++i)
|
|
{
|
|
// it only adds to the bone map if it has weight on it
|
|
// BoneMap contains only the bones that has influence with weight of >0.f
|
|
// so here, just make sure it is included before setting the data
|
|
if (Section.BoneMap.IsValidIndex(SoftVertex.InfluenceBones[i]))
|
|
{
|
|
NewVertex.InfluenceBones[i] = SoftVertex.InfluenceBones[i];
|
|
NewVertex.InfluenceWeights[i] = SoftVertex.InfluenceWeights[i];
|
|
}
|
|
}
|
|
Section.SoftVertices.Add(NewVertex);
|
|
ChunkVertexIndexRemap[VertexIndex] = (uint32)(Section.BaseVertexIndex + CurrentVertexIndex);
|
|
CurrentVertexIndex++;
|
|
// add the index to the original wedge point source of this vertex
|
|
RawPointIndices.Add(SoftVertex.PointWedgeIdx);
|
|
// Also remember import index
|
|
const int32 RawVertIndex = PointToOriginalMap[SoftVertex.PointWedgeIdx];
|
|
LODModel.MeshToImportVertexMap.Add(RawVertIndex);
|
|
LODModel.MaxImportVertex = FMath::Max<float>(LODModel.MaxImportVertex, RawVertIndex);
|
|
}
|
|
|
|
// update NumVertices
|
|
Section.NumVertices = Section.SoftVertices.Num();
|
|
|
|
// update max bone influences
|
|
Section.CalcMaxBoneInfluences();
|
|
|
|
// Log info about the chunk.
|
|
UE_LOG(LogSkeletalMesh, Log, TEXT("Section %u: %u vertices, %u active bones"),
|
|
SectionIndex,
|
|
Section.GetNumVertices(),
|
|
Section.BoneMap.Num()
|
|
);
|
|
}
|
|
|
|
// Copy raw point indices to LOD model.
|
|
LODModel.RawPointIndices.RemoveBulkData();
|
|
if (RawPointIndices.Num())
|
|
{
|
|
LODModel.RawPointIndices.Lock(LOCK_READ_WRITE);
|
|
void* Dest = LODModel.RawPointIndices.Realloc(RawPointIndices.Num());
|
|
FMemory::Memcpy(Dest, RawPointIndices.GetData(), LODModel.RawPointIndices.GetBulkDataSize());
|
|
LODModel.RawPointIndices.Unlock();
|
|
}
|
|
|
|
#if DISALLOW_32BIT_INDICES
|
|
LODModel.MultiSizeIndexContainer.CreateIndexBuffer(sizeof(uint16));
|
|
#else
|
|
LODModel.MultiSizeIndexContainer.CreateIndexBuffer((LODModel.NumVertices < MAX_uint16) ? sizeof(uint16) : sizeof(uint32));
|
|
#endif
|
|
|
|
// Finish building the sections.
|
|
for (int32 SectionIndex = 0; SectionIndex < LODModel.Sections.Num(); SectionIndex++)
|
|
{
|
|
FSkelMeshSection& Section = LODModel.Sections[SectionIndex];
|
|
|
|
const TArray<uint32>& SectionIndices = Chunks[SectionIndex]->Indices;
|
|
FRawStaticIndexBuffer16or32Interface* IndexBuffer = LODModel.MultiSizeIndexContainer.GetIndexBuffer();
|
|
Section.BaseIndex = IndexBuffer->Num();
|
|
const int32 NumIndices = SectionIndices.Num();
|
|
const TArray<uint32>& SectionVertexIndexRemap = VertexIndexRemap[SectionIndex];
|
|
for (int32 Index = 0; Index < NumIndices; Index++)
|
|
{
|
|
uint32 VertexIndex = SectionVertexIndexRemap[SectionIndices[Index]];
|
|
IndexBuffer->AddItem(VertexIndex);
|
|
}
|
|
}
|
|
|
|
// Free the skinned mesh chunks which are no longer needed.
|
|
for (int32 i = 0; i < Chunks.Num(); ++i)
|
|
{
|
|
delete Chunks[i];
|
|
Chunks[i] = NULL;
|
|
}
|
|
Chunks.Empty();
|
|
|
|
// Build the adjacency index buffer used for tessellation.
|
|
{
|
|
TArray<FSoftSkinVertex> Vertices;
|
|
LODModel.GetVertices(Vertices);
|
|
|
|
FMultiSizeIndexContainerData IndexData;
|
|
LODModel.MultiSizeIndexContainer.GetIndexBufferData(IndexData);
|
|
|
|
FMultiSizeIndexContainerData AdjacencyIndexData;
|
|
AdjacencyIndexData.DataTypeSize = IndexData.DataTypeSize;
|
|
|
|
BuildSkeletalAdjacencyIndexBuffer(Vertices, LODModel.NumTexCoords, IndexData.Indices, AdjacencyIndexData.Indices);
|
|
LODModel.AdjacencyMultiSizeIndexContainer.RebuildIndexBuffer(AdjacencyIndexData);
|
|
}
|
|
|
|
// Compute the required bones for this model.
|
|
USkeletalMesh::CalculateRequiredBones(LODModel, RefSkeleton, NULL);
|
|
#endif // #if WITH_EDITORONLY_DATA
|
|
}
|
|
|
|
/*------------------------------------------------------------------------------
|
|
Common functionality.
|
|
------------------------------------------------------------------------------*/
|
|
|
|
/** Helper struct for building acceleration structures. */
|
|
struct FIndexAndZ
|
|
{
|
|
float Z;
|
|
int32 Index;
|
|
|
|
/** Default constructor. */
|
|
FIndexAndZ() {}
|
|
|
|
/** Initialization constructor. */
|
|
FIndexAndZ(int32 InIndex, FVector V)
|
|
{
|
|
Z = 0.30f * V.X + 0.33f * V.Y + 0.37f * V.Z;
|
|
Index = InIndex;
|
|
}
|
|
};
|
|
|
|
/** Sorting function for vertex Z/index pairs. */
|
|
struct FCompareIndexAndZ
|
|
{
|
|
FORCEINLINE bool operator()(FIndexAndZ const& A, FIndexAndZ const& B) const { return A.Z < B.Z; }
|
|
};
|
|
|
|
static int32 ComputeNumTexCoords(FRawMesh const& RawMesh, int32 MaxSupportedTexCoords)
|
|
{
|
|
int32 NumWedges = RawMesh.WedgeIndices.Num();
|
|
int32 NumTexCoords = 0;
|
|
for (int32 TexCoordIndex = 0; TexCoordIndex < MAX_MESH_TEXTURE_COORDS; ++TexCoordIndex)
|
|
{
|
|
if (RawMesh.WedgeTexCoords[TexCoordIndex].Num() != NumWedges)
|
|
{
|
|
break;
|
|
}
|
|
NumTexCoords++;
|
|
}
|
|
return FMath::Min(NumTexCoords, MaxSupportedTexCoords);
|
|
}
|
|
|
|
/**
|
|
* Returns true if the specified points are about equal
|
|
*/
|
|
inline bool PointsEqual(const FVector& V1, const FVector& V2, float ComparisonThreshold)
|
|
{
|
|
if (FMath::Abs(V1.X - V2.X) > ComparisonThreshold
|
|
|| FMath::Abs(V1.Y - V2.Y) > ComparisonThreshold
|
|
|| FMath::Abs(V1.Z - V2.Z) > ComparisonThreshold)
|
|
{
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
static inline FVector GetPositionForWedge(FRawMesh const& Mesh, int32 WedgeIndex)
|
|
{
|
|
int32 VertexIndex = Mesh.WedgeIndices[WedgeIndex];
|
|
return Mesh.VertexPositions[VertexIndex];
|
|
}
|
|
|
|
struct FMeshEdge
|
|
{
|
|
int32 Vertices[2];
|
|
int32 Faces[2];
|
|
};
|
|
|
|
/**
|
|
* This helper class builds the edge list for a mesh. It uses a hash of vertex
|
|
* positions to edges sharing that vertex to remove the n^2 searching of all
|
|
* previously added edges. This class is templatized so it can be used with
|
|
* either static mesh or skeletal mesh vertices
|
|
*/
|
|
template <class VertexClass> class TEdgeBuilder
|
|
{
|
|
protected:
|
|
/**
|
|
* The list of indices to build the edge data from
|
|
*/
|
|
const TArray<uint32>& Indices;
|
|
/**
|
|
* The array of verts for vertex position comparison
|
|
*/
|
|
const TArray<VertexClass>& Vertices;
|
|
/**
|
|
* The array of edges to create
|
|
*/
|
|
TArray<FMeshEdge>& Edges;
|
|
/**
|
|
* List of edges that start with a given vertex
|
|
*/
|
|
TMultiMap<FVector, FMeshEdge*> VertexToEdgeList;
|
|
|
|
/**
|
|
* This function determines whether a given edge matches or not. It must be
|
|
* provided by derived classes since they have the specific information that
|
|
* this class doesn't know about (vertex info, influences, etc)
|
|
*
|
|
* @param Index1 The first index of the edge being checked
|
|
* @param Index2 The second index of the edge
|
|
* @param OtherEdge The edge to compare. Was found via the map
|
|
*
|
|
* @return true if the edge is a match, false otherwise
|
|
*/
|
|
virtual bool DoesEdgeMatch(int32 Index1, int32 Index2, FMeshEdge* OtherEdge) = 0;
|
|
|
|
/**
|
|
* Searches the list of edges to see if this one matches an existing and
|
|
* returns a pointer to it if it does
|
|
*
|
|
* @param Index1 the first index to check for
|
|
* @param Index2 the second index to check for
|
|
*
|
|
* @return NULL if no edge was found, otherwise the edge that was found
|
|
*/
|
|
inline FMeshEdge* FindOppositeEdge(int32 Index1, int32 Index2)
|
|
{
|
|
FMeshEdge* Edge = NULL;
|
|
TArray<FMeshEdge*> EdgeList;
|
|
// Search the hash for a corresponding vertex
|
|
VertexToEdgeList.MultiFind(Vertices[Index2].Position, EdgeList);
|
|
// Now search through the array for a match or not
|
|
for (int32 EdgeIndex = 0; EdgeIndex < EdgeList.Num() && Edge == NULL;
|
|
EdgeIndex++)
|
|
{
|
|
FMeshEdge* OtherEdge = EdgeList[EdgeIndex];
|
|
// See if this edge matches the passed in edge
|
|
if (OtherEdge != NULL && DoesEdgeMatch(Index1, Index2, OtherEdge))
|
|
{
|
|
// We have a match
|
|
Edge = OtherEdge;
|
|
}
|
|
}
|
|
return Edge;
|
|
}
|
|
|
|
/**
|
|
* Updates an existing edge if found or adds the new edge to the list
|
|
*
|
|
* @param Index1 the first index in the edge
|
|
* @param Index2 the second index in the edge
|
|
* @param Triangle the triangle that this edge was found in
|
|
*/
|
|
inline void AddEdge(int32 Index1, int32 Index2, int32 Triangle)
|
|
{
|
|
// If this edge matches another then just fill the other triangle
|
|
// otherwise add it
|
|
FMeshEdge* OtherEdge = FindOppositeEdge(Index1, Index2);
|
|
if (OtherEdge == NULL)
|
|
{
|
|
// Add a new edge to the array
|
|
int32 EdgeIndex = Edges.AddZeroed();
|
|
Edges[EdgeIndex].Vertices[0] = Index1;
|
|
Edges[EdgeIndex].Vertices[1] = Index2;
|
|
Edges[EdgeIndex].Faces[0] = Triangle;
|
|
Edges[EdgeIndex].Faces[1] = -1;
|
|
// Also add this edge to the hash for faster searches
|
|
// NOTE: This relies on the array never being realloced!
|
|
VertexToEdgeList.Add(Vertices[Index1].Position, &Edges[EdgeIndex]);
|
|
}
|
|
else
|
|
{
|
|
OtherEdge->Faces[1] = Triangle;
|
|
}
|
|
}
|
|
|
|
public:
|
|
/**
|
|
* Initializes the values for the code that will build the mesh edge list
|
|
*/
|
|
TEdgeBuilder(const TArray<uint32>& InIndices,
|
|
const TArray<VertexClass>& InVertices,
|
|
TArray<FMeshEdge>& OutEdges) :
|
|
Indices(InIndices), Vertices(InVertices), Edges(OutEdges)
|
|
{
|
|
// Presize the array so that there are no extra copies being done
|
|
// when adding edges to it
|
|
Edges.Empty(Indices.Num());
|
|
}
|
|
|
|
/**
|
|
* Virtual dtor
|
|
*/
|
|
virtual ~TEdgeBuilder(){}
|
|
|
|
|
|
/**
|
|
* Uses a hash of indices to edge lists so that it can avoid the n^2 search
|
|
* through the full edge list
|
|
*/
|
|
void FindEdges(void)
|
|
{
|
|
// @todo Handle something other than trilists when building edges
|
|
int32 TriangleCount = Indices.Num() / 3;
|
|
int32 EdgeCount = 0;
|
|
// Work through all triangles building the edges
|
|
for (int32 Triangle = 0; Triangle < TriangleCount; Triangle++)
|
|
{
|
|
// Determine the starting index
|
|
int32 TriangleIndex = Triangle * 3;
|
|
// Get the indices for the triangle
|
|
int32 Index1 = Indices[TriangleIndex];
|
|
int32 Index2 = Indices[TriangleIndex + 1];
|
|
int32 Index3 = Indices[TriangleIndex + 2];
|
|
// Add the first to second edge
|
|
AddEdge(Index1, Index2, Triangle);
|
|
// Now add the second to third
|
|
AddEdge(Index2, Index3, Triangle);
|
|
// Add the third to first edge
|
|
AddEdge(Index3, Index1, Triangle);
|
|
}
|
|
}
|
|
};
|
|
|
|
/**
|
|
* This is the static mesh specific version for finding edges
|
|
*/
|
|
class FStaticMeshEdgeBuilder : public TEdgeBuilder<FStaticMeshBuildVertex>
|
|
{
|
|
public:
|
|
/**
|
|
* Constructor that passes all work to the parent class
|
|
*/
|
|
FStaticMeshEdgeBuilder(const TArray<uint32>& InIndices,
|
|
const TArray<FStaticMeshBuildVertex>& InVertices,
|
|
TArray<FMeshEdge>& OutEdges) :
|
|
TEdgeBuilder<FStaticMeshBuildVertex>(InIndices, InVertices, OutEdges)
|
|
{
|
|
}
|
|
|
|
/**
|
|
* This function determines whether a given edge matches or not for a static mesh
|
|
*
|
|
* @param Index1 The first index of the edge being checked
|
|
* @param Index2 The second index of the edge
|
|
* @param OtherEdge The edge to compare. Was found via the map
|
|
*
|
|
* @return true if the edge is a match, false otherwise
|
|
*/
|
|
bool DoesEdgeMatch(int32 Index1, int32 Index2, FMeshEdge* OtherEdge)
|
|
{
|
|
return Vertices[OtherEdge->Vertices[1]].Position == Vertices[Index1].Position &&
|
|
OtherEdge->Faces[1] == -1;
|
|
}
|
|
};
|
|
|
|
static void ComputeTriangleTangents(
|
|
const TArray<FVector>& InVertices,
|
|
const TArray<uint32>& InIndices,
|
|
const TArray<FVector2D>& InUVs,
|
|
TArray<FVector>& OutTangentX,
|
|
TArray<FVector>& OutTangentY,
|
|
TArray<FVector>& OutTangentZ,
|
|
float ComparisonThreshold
|
|
)
|
|
{
|
|
const int32 NumTriangles = InIndices.Num() / 3;
|
|
OutTangentX.Empty(NumTriangles);
|
|
OutTangentY.Empty(NumTriangles);
|
|
OutTangentZ.Empty(NumTriangles);
|
|
|
|
for (int32 TriangleIndex = 0; TriangleIndex < NumTriangles; TriangleIndex++)
|
|
{
|
|
int32 UVIndex = 0;
|
|
|
|
FVector P[3];
|
|
for (int32 i = 0; i < 3; ++i)
|
|
{
|
|
P[i] = InVertices[InIndices[TriangleIndex * 3 + i]];
|
|
}
|
|
|
|
const FVector Normal = ((P[1] - P[2]) ^ (P[0] - P[2])).GetSafeNormal(ComparisonThreshold);
|
|
FMatrix ParameterToLocal(
|
|
FPlane(P[1].X - P[0].X, P[1].Y - P[0].Y, P[1].Z - P[0].Z, 0),
|
|
FPlane(P[2].X - P[0].X, P[2].Y - P[0].Y, P[2].Z - P[0].Z, 0),
|
|
FPlane(P[0].X, P[0].Y, P[0].Z, 0),
|
|
FPlane(0, 0, 0, 1)
|
|
);
|
|
|
|
const FVector2D T1 = InUVs[TriangleIndex * 3 + 0];
|
|
const FVector2D T2 = InUVs[TriangleIndex * 3 + 1];
|
|
const FVector2D T3 = InUVs[TriangleIndex * 3 + 2];
|
|
|
|
FMatrix ParameterToTexture(
|
|
FPlane(T2.X - T1.X, T2.Y - T1.Y, 0, 0),
|
|
FPlane(T3.X - T1.X, T3.Y - T1.Y, 0, 0),
|
|
FPlane(T1.X, T1.Y, 1, 0),
|
|
FPlane(0, 0, 0, 1)
|
|
);
|
|
|
|
// Use InverseSlow to catch singular matrices. Inverse can miss this sometimes.
|
|
const FMatrix TextureToLocal = ParameterToTexture.Inverse() * ParameterToLocal;
|
|
|
|
OutTangentX.Add(TextureToLocal.TransformVector(FVector(1, 0, 0)).GetSafeNormal());
|
|
OutTangentY.Add(TextureToLocal.TransformVector(FVector(0, 1, 0)).GetSafeNormal());
|
|
OutTangentZ.Add(Normal);
|
|
|
|
FVector::CreateOrthonormalBasis(
|
|
OutTangentX[TriangleIndex],
|
|
OutTangentY[TriangleIndex],
|
|
OutTangentZ[TriangleIndex]
|
|
);
|
|
}
|
|
|
|
check(OutTangentX.Num() == NumTriangles);
|
|
check(OutTangentY.Num() == NumTriangles);
|
|
check(OutTangentZ.Num() == NumTriangles);
|
|
}
|
|
|
|
static void ComputeTriangleTangents(
|
|
TArray<FVector>& OutTangentX,
|
|
TArray<FVector>& OutTangentY,
|
|
TArray<FVector>& OutTangentZ,
|
|
FRawMesh const& RawMesh,
|
|
float ComparisonThreshold
|
|
)
|
|
{
|
|
ComputeTriangleTangents(RawMesh.VertexPositions, RawMesh.WedgeIndices, RawMesh.WedgeTexCoords[0], OutTangentX, OutTangentY, OutTangentZ, ComparisonThreshold);
|
|
|
|
/*int32 NumTriangles = RawMesh.WedgeIndices.Num() / 3;
|
|
TriangleTangentX.Empty(NumTriangles);
|
|
TriangleTangentY.Empty(NumTriangles);
|
|
TriangleTangentZ.Empty(NumTriangles);
|
|
|
|
for (int32 TriangleIndex = 0; TriangleIndex < NumTriangles; TriangleIndex++)
|
|
{
|
|
int32 UVIndex = 0;
|
|
|
|
FVector P[3];
|
|
for (int32 i = 0; i < 3; ++i)
|
|
{
|
|
P[i] = GetPositionForWedge(RawMesh, TriangleIndex * 3 + i);
|
|
}
|
|
|
|
const FVector Normal = ((P[1] - P[2]) ^ (P[0] - P[2])).GetSafeNormal(ComparisonThreshold);
|
|
FMatrix ParameterToLocal(
|
|
FPlane(P[1].X - P[0].X, P[1].Y - P[0].Y, P[1].Z - P[0].Z, 0),
|
|
FPlane(P[2].X - P[0].X, P[2].Y - P[0].Y, P[2].Z - P[0].Z, 0),
|
|
FPlane(P[0].X, P[0].Y, P[0].Z, 0),
|
|
FPlane(0, 0, 0, 1)
|
|
);
|
|
|
|
FVector2D T1 = RawMesh.WedgeTexCoords[UVIndex][TriangleIndex * 3 + 0];
|
|
FVector2D T2 = RawMesh.WedgeTexCoords[UVIndex][TriangleIndex * 3 + 1];
|
|
FVector2D T3 = RawMesh.WedgeTexCoords[UVIndex][TriangleIndex * 3 + 2];
|
|
FMatrix ParameterToTexture(
|
|
FPlane(T2.X - T1.X, T2.Y - T1.Y, 0, 0),
|
|
FPlane(T3.X - T1.X, T3.Y - T1.Y, 0, 0),
|
|
FPlane(T1.X, T1.Y, 1, 0),
|
|
FPlane(0, 0, 0, 1)
|
|
);
|
|
|
|
// Use InverseSlow to catch singular matrices. Inverse can miss this sometimes.
|
|
const FMatrix TextureToLocal = ParameterToTexture.Inverse() * ParameterToLocal;
|
|
|
|
TriangleTangentX.Add(TextureToLocal.TransformVector(FVector(1, 0, 0)).GetSafeNormal());
|
|
TriangleTangentY.Add(TextureToLocal.TransformVector(FVector(0, 1, 0)).GetSafeNormal());
|
|
TriangleTangentZ.Add(Normal);
|
|
|
|
FVector::CreateOrthonormalBasis(
|
|
TriangleTangentX[TriangleIndex],
|
|
TriangleTangentY[TriangleIndex],
|
|
TriangleTangentZ[TriangleIndex]
|
|
);
|
|
}
|
|
|
|
check(TriangleTangentX.Num() == NumTriangles);
|
|
check(TriangleTangentY.Num() == NumTriangles);
|
|
check(TriangleTangentZ.Num() == NumTriangles);*/
|
|
}
|
|
|
|
/**
|
|
* Create a table that maps the corner of each face to its overlapping corners.
|
|
* @param OutOverlappingCorners - Maps a corner index to the indices of all overlapping corners.
|
|
* @param RawMesh - The mesh for which to compute overlapping corners.
|
|
*/
|
|
static void FindOverlappingCorners(
|
|
TMultiMap<int32, int32>& OutOverlappingCorners,
|
|
const TArray<FVector>& InVertices,
|
|
const TArray<uint32>& InIndices,
|
|
float ComparisonThreshold
|
|
)
|
|
{
|
|
const int32 NumWedges = InIndices.Num();
|
|
|
|
// Create a list of vertex Z/index pairs
|
|
TArray<FIndexAndZ> VertIndexAndZ;
|
|
VertIndexAndZ.Reserve(NumWedges);
|
|
for (int32 WedgeIndex = 0; WedgeIndex < NumWedges; WedgeIndex++)
|
|
{
|
|
new(VertIndexAndZ)FIndexAndZ(WedgeIndex, InVertices[InIndices[WedgeIndex]]);
|
|
}
|
|
|
|
// Sort the vertices by z value
|
|
VertIndexAndZ.Sort(FCompareIndexAndZ());
|
|
|
|
// Search for duplicates, quickly!
|
|
for (int32 i = 0; i < VertIndexAndZ.Num(); i++)
|
|
{
|
|
// only need to search forward, since we add pairs both ways
|
|
for (int32 j = i + 1; j < VertIndexAndZ.Num(); j++)
|
|
{
|
|
if (FMath::Abs(VertIndexAndZ[j].Z - VertIndexAndZ[i].Z) > ComparisonThreshold)
|
|
break; // can't be any more dups
|
|
|
|
const FVector& PositionA = InVertices[InIndices[VertIndexAndZ[i].Index]];
|
|
const FVector& PositionB = InVertices[InIndices[VertIndexAndZ[j].Index]];
|
|
|
|
if (PointsEqual(PositionA, PositionB, ComparisonThreshold))
|
|
{
|
|
OutOverlappingCorners.Add(VertIndexAndZ[i].Index, VertIndexAndZ[j].Index);
|
|
OutOverlappingCorners.Add(VertIndexAndZ[j].Index, VertIndexAndZ[i].Index);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Create a table that maps the corner of each face to its overlapping corners.
|
|
* @param OutOverlappingCorners - Maps a corner index to the indices of all overlapping corners.
|
|
* @param RawMesh - The mesh for which to compute overlapping corners.
|
|
*/
|
|
static void FindOverlappingCorners(
|
|
TMultiMap<int32, int32>& OutOverlappingCorners,
|
|
FRawMesh const& RawMesh,
|
|
float ComparisonThreshold
|
|
)
|
|
{
|
|
FindOverlappingCorners(OutOverlappingCorners, RawMesh.VertexPositions, RawMesh.WedgeIndices, ComparisonThreshold);
|
|
}
|
|
|
|
/**
|
|
* Smoothing group interpretation helper structure.
|
|
*/
|
|
struct FFanFace
|
|
{
|
|
int32 FaceIndex;
|
|
int32 LinkedVertexIndex;
|
|
bool bFilled;
|
|
bool bBlendTangents;
|
|
bool bBlendNormals;
|
|
};
|
|
|
|
static void ComputeTangents(
|
|
const TArray<FVector>& InVertices,
|
|
const TArray<uint32>& InIndices,
|
|
const TArray<FVector2D>& InUVs,
|
|
const TArray<uint32>& SmoothingGroupIndices,
|
|
TMultiMap<int32, int32> const& OverlappingCorners,
|
|
TArray<FVector>& OutTangentX,
|
|
TArray<FVector>& OutTangentY,
|
|
TArray<FVector>& OutTangentZ,
|
|
const uint32 TangentOptions
|
|
)
|
|
{
|
|
bool bBlendOverlappingNormals = (TangentOptions & ETangentOptions::BlendOverlappingNormals) != 0;
|
|
bool bIgnoreDegenerateTriangles = (TangentOptions & ETangentOptions::IgnoreDegenerateTriangles) != 0;
|
|
float ComparisonThreshold = bIgnoreDegenerateTriangles ? THRESH_POINTS_ARE_SAME : 0.0f;
|
|
|
|
// Compute per-triangle tangents.
|
|
TArray<FVector> TriangleTangentX;
|
|
TArray<FVector> TriangleTangentY;
|
|
TArray<FVector> TriangleTangentZ;
|
|
|
|
ComputeTriangleTangents(
|
|
InVertices,
|
|
InIndices,
|
|
InUVs,
|
|
TriangleTangentX,
|
|
TriangleTangentY,
|
|
TriangleTangentZ,
|
|
bIgnoreDegenerateTriangles ? SMALL_NUMBER : 0.0f
|
|
);
|
|
|
|
// Declare these out here to avoid reallocations.
|
|
TArray<FFanFace> RelevantFacesForCorner[3];
|
|
TArray<int32> AdjacentFaces;
|
|
TArray<int32> DupVerts;
|
|
|
|
int32 NumWedges = InIndices.Num();
|
|
int32 NumFaces = NumWedges / 3;
|
|
|
|
// Allocate storage for tangents if none were provided.
|
|
if (OutTangentX.Num() != NumWedges)
|
|
{
|
|
OutTangentX.Empty(NumWedges);
|
|
OutTangentX.AddZeroed(NumWedges);
|
|
}
|
|
if (OutTangentY.Num() != NumWedges)
|
|
{
|
|
OutTangentY.Empty(NumWedges);
|
|
OutTangentY.AddZeroed(NumWedges);
|
|
}
|
|
if (OutTangentZ.Num() != NumWedges)
|
|
{
|
|
OutTangentZ.Empty(NumWedges);
|
|
OutTangentZ.AddZeroed(NumWedges);
|
|
}
|
|
|
|
for (int32 FaceIndex = 0; FaceIndex < NumFaces; FaceIndex++)
|
|
{
|
|
int32 WedgeOffset = FaceIndex * 3;
|
|
FVector CornerPositions[3];
|
|
FVector CornerTangentX[3];
|
|
FVector CornerTangentY[3];
|
|
FVector CornerTangentZ[3];
|
|
|
|
for (int32 CornerIndex = 0; CornerIndex < 3; CornerIndex++)
|
|
{
|
|
CornerTangentX[CornerIndex] = FVector::ZeroVector;
|
|
CornerTangentY[CornerIndex] = FVector::ZeroVector;
|
|
CornerTangentZ[CornerIndex] = FVector::ZeroVector;
|
|
CornerPositions[CornerIndex] = InVertices[InIndices[WedgeOffset + CornerIndex]];
|
|
RelevantFacesForCorner[CornerIndex].Reset();
|
|
}
|
|
|
|
// Don't process degenerate triangles.
|
|
if (PointsEqual(CornerPositions[0], CornerPositions[1], ComparisonThreshold)
|
|
|| PointsEqual(CornerPositions[0], CornerPositions[2], ComparisonThreshold)
|
|
|| PointsEqual(CornerPositions[1], CornerPositions[2], ComparisonThreshold))
|
|
{
|
|
continue;
|
|
}
|
|
|
|
// No need to process triangles if tangents already exist.
|
|
bool bCornerHasTangents[3] = { 0 };
|
|
for (int32 CornerIndex = 0; CornerIndex < 3; CornerIndex++)
|
|
{
|
|
bCornerHasTangents[CornerIndex] = !OutTangentX[WedgeOffset + CornerIndex].IsZero()
|
|
&& !OutTangentY[WedgeOffset + CornerIndex].IsZero()
|
|
&& !OutTangentZ[WedgeOffset + CornerIndex].IsZero();
|
|
}
|
|
if (bCornerHasTangents[0] && bCornerHasTangents[1] && bCornerHasTangents[2])
|
|
{
|
|
continue;
|
|
}
|
|
|
|
// Calculate smooth vertex normals.
|
|
float Determinant = FVector::Triple(
|
|
TriangleTangentX[FaceIndex],
|
|
TriangleTangentY[FaceIndex],
|
|
TriangleTangentZ[FaceIndex]
|
|
);
|
|
|
|
// Start building a list of faces adjacent to this face.
|
|
AdjacentFaces.Reset();
|
|
for (int32 CornerIndex = 0; CornerIndex < 3; CornerIndex++)
|
|
{
|
|
int32 ThisCornerIndex = WedgeOffset + CornerIndex;
|
|
DupVerts.Reset();
|
|
OverlappingCorners.MultiFind(ThisCornerIndex, DupVerts);
|
|
DupVerts.Add(ThisCornerIndex); // I am a "dup" of myself
|
|
for (int32 k = 0; k < DupVerts.Num(); k++)
|
|
{
|
|
AdjacentFaces.AddUnique(DupVerts[k] / 3);
|
|
}
|
|
}
|
|
|
|
// We need to sort these here because the criteria for point equality is
|
|
// exact, so we must ensure the exact same order for all dups.
|
|
AdjacentFaces.Sort();
|
|
|
|
// Process adjacent faces
|
|
for (int32 AdjacentFaceIndex = 0; AdjacentFaceIndex < AdjacentFaces.Num(); AdjacentFaceIndex++)
|
|
{
|
|
int32 OtherFaceIndex = AdjacentFaces[AdjacentFaceIndex];
|
|
for (int32 OurCornerIndex = 0; OurCornerIndex < 3; OurCornerIndex++)
|
|
{
|
|
if (bCornerHasTangents[OurCornerIndex])
|
|
continue;
|
|
|
|
FFanFace NewFanFace;
|
|
int32 CommonIndexCount = 0;
|
|
|
|
// Check for vertices in common.
|
|
if (FaceIndex == OtherFaceIndex)
|
|
{
|
|
CommonIndexCount = 3;
|
|
NewFanFace.LinkedVertexIndex = OurCornerIndex;
|
|
}
|
|
else
|
|
{
|
|
// Check matching vertices against main vertex .
|
|
for (int32 OtherCornerIndex = 0; OtherCornerIndex < 3; OtherCornerIndex++)
|
|
{
|
|
if (PointsEqual(
|
|
CornerPositions[OurCornerIndex],
|
|
InVertices[InIndices[OtherFaceIndex * 3 + OtherCornerIndex]],
|
|
ComparisonThreshold
|
|
))
|
|
{
|
|
CommonIndexCount++;
|
|
NewFanFace.LinkedVertexIndex = OtherCornerIndex;
|
|
}
|
|
}
|
|
}
|
|
|
|
// Add if connected by at least one point. Smoothing matches are considered later.
|
|
if (CommonIndexCount > 0)
|
|
{
|
|
NewFanFace.FaceIndex = OtherFaceIndex;
|
|
NewFanFace.bFilled = (OtherFaceIndex == FaceIndex); // Starter face for smoothing floodfill.
|
|
NewFanFace.bBlendTangents = NewFanFace.bFilled;
|
|
NewFanFace.bBlendNormals = NewFanFace.bFilled;
|
|
RelevantFacesForCorner[OurCornerIndex].Add(NewFanFace);
|
|
}
|
|
}
|
|
}
|
|
|
|
// Find true relevance of faces for a vertex normal by traversing
|
|
// smoothing-group-compatible connected triangle fans around common vertices.
|
|
for (int32 CornerIndex = 0; CornerIndex < 3; CornerIndex++)
|
|
{
|
|
if (bCornerHasTangents[CornerIndex])
|
|
continue;
|
|
|
|
int32 NewConnections;
|
|
do
|
|
{
|
|
NewConnections = 0;
|
|
for (int32 OtherFaceIdx = 0; OtherFaceIdx < RelevantFacesForCorner[CornerIndex].Num(); OtherFaceIdx++)
|
|
{
|
|
FFanFace& OtherFace = RelevantFacesForCorner[CornerIndex][OtherFaceIdx];
|
|
// The vertex' own face is initially the only face with bFilled == true.
|
|
if (OtherFace.bFilled)
|
|
{
|
|
for (int32 NextFaceIndex = 0; NextFaceIndex < RelevantFacesForCorner[CornerIndex].Num(); NextFaceIndex++)
|
|
{
|
|
FFanFace& NextFace = RelevantFacesForCorner[CornerIndex][NextFaceIndex];
|
|
if (!NextFace.bFilled) // && !NextFace.bBlendTangents)
|
|
{
|
|
if ((NextFaceIndex != OtherFaceIdx)
|
|
&& (SmoothingGroupIndices[NextFace.FaceIndex] & SmoothingGroupIndices[OtherFace.FaceIndex]))
|
|
{
|
|
int32 CommonVertices = 0;
|
|
int32 CommonTangentVertices = 0;
|
|
int32 CommonNormalVertices = 0;
|
|
for (int32 OtherCornerIndex = 0; OtherCornerIndex < 3; OtherCornerIndex++)
|
|
{
|
|
for (int32 NextCornerIndex = 0; NextCornerIndex < 3; NextCornerIndex++)
|
|
{
|
|
int32 NextVertexIndex = InIndices[NextFace.FaceIndex * 3 + NextCornerIndex];
|
|
int32 OtherVertexIndex = InIndices[OtherFace.FaceIndex * 3 + OtherCornerIndex];
|
|
if (PointsEqual(
|
|
InVertices[NextVertexIndex],
|
|
InVertices[OtherVertexIndex],
|
|
ComparisonThreshold))
|
|
{
|
|
CommonVertices++;
|
|
|
|
|
|
const FVector2D& UVOne = InUVs[NextFace.FaceIndex * 3 + NextCornerIndex];
|
|
const FVector2D& UVTwo = InUVs[OtherFace.FaceIndex * 3 + OtherCornerIndex];
|
|
|
|
if (UVsEqual(UVOne, UVTwo))
|
|
{
|
|
CommonTangentVertices++;
|
|
}
|
|
if (bBlendOverlappingNormals
|
|
|| NextVertexIndex == OtherVertexIndex)
|
|
{
|
|
CommonNormalVertices++;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
// Flood fill faces with more than one common vertices which must be touching edges.
|
|
if (CommonVertices > 1)
|
|
{
|
|
NextFace.bFilled = true;
|
|
NextFace.bBlendNormals = (CommonNormalVertices > 1);
|
|
NewConnections++;
|
|
|
|
// Only blend tangents if there is no UV seam along the edge with this face.
|
|
if (OtherFace.bBlendTangents && CommonTangentVertices > 1)
|
|
{
|
|
float OtherDeterminant = FVector::Triple(
|
|
TriangleTangentX[NextFace.FaceIndex],
|
|
TriangleTangentY[NextFace.FaceIndex],
|
|
TriangleTangentZ[NextFace.FaceIndex]
|
|
);
|
|
if ((Determinant * OtherDeterminant) > 0.0f)
|
|
{
|
|
NextFace.bBlendTangents = true;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
} while (NewConnections > 0);
|
|
}
|
|
|
|
// Vertex normal construction.
|
|
for (int32 CornerIndex = 0; CornerIndex < 3; CornerIndex++)
|
|
{
|
|
if (bCornerHasTangents[CornerIndex])
|
|
{
|
|
CornerTangentX[CornerIndex] = OutTangentX[WedgeOffset + CornerIndex];
|
|
CornerTangentY[CornerIndex] = OutTangentY[WedgeOffset + CornerIndex];
|
|
CornerTangentZ[CornerIndex] = OutTangentZ[WedgeOffset + CornerIndex];
|
|
}
|
|
else
|
|
{
|
|
for (int32 RelevantFaceIdx = 0; RelevantFaceIdx < RelevantFacesForCorner[CornerIndex].Num(); RelevantFaceIdx++)
|
|
{
|
|
FFanFace const& RelevantFace = RelevantFacesForCorner[CornerIndex][RelevantFaceIdx];
|
|
if (RelevantFace.bFilled)
|
|
{
|
|
int32 OtherFaceIndex = RelevantFace.FaceIndex;
|
|
if (RelevantFace.bBlendTangents)
|
|
{
|
|
CornerTangentX[CornerIndex] += TriangleTangentX[OtherFaceIndex];
|
|
CornerTangentY[CornerIndex] += TriangleTangentY[OtherFaceIndex];
|
|
}
|
|
if (RelevantFace.bBlendNormals)
|
|
{
|
|
CornerTangentZ[CornerIndex] += TriangleTangentZ[OtherFaceIndex];
|
|
}
|
|
}
|
|
}
|
|
if (!OutTangentX[WedgeOffset + CornerIndex].IsZero())
|
|
{
|
|
CornerTangentX[CornerIndex] = OutTangentX[WedgeOffset + CornerIndex];
|
|
}
|
|
if (!OutTangentY[WedgeOffset + CornerIndex].IsZero())
|
|
{
|
|
CornerTangentY[CornerIndex] = OutTangentY[WedgeOffset + CornerIndex];
|
|
}
|
|
if (!OutTangentZ[WedgeOffset + CornerIndex].IsZero())
|
|
{
|
|
CornerTangentZ[CornerIndex] = OutTangentZ[WedgeOffset + CornerIndex];
|
|
}
|
|
}
|
|
}
|
|
|
|
// Normalization.
|
|
for (int32 CornerIndex = 0; CornerIndex < 3; CornerIndex++)
|
|
{
|
|
CornerTangentX[CornerIndex].Normalize();
|
|
CornerTangentY[CornerIndex].Normalize();
|
|
CornerTangentZ[CornerIndex].Normalize();
|
|
|
|
// Gram-Schmidt orthogonalization
|
|
CornerTangentY[CornerIndex] -= CornerTangentX[CornerIndex] * (CornerTangentX[CornerIndex] | CornerTangentY[CornerIndex]);
|
|
CornerTangentY[CornerIndex].Normalize();
|
|
|
|
CornerTangentX[CornerIndex] -= CornerTangentZ[CornerIndex] * (CornerTangentZ[CornerIndex] | CornerTangentX[CornerIndex]);
|
|
CornerTangentX[CornerIndex].Normalize();
|
|
CornerTangentY[CornerIndex] -= CornerTangentZ[CornerIndex] * (CornerTangentZ[CornerIndex] | CornerTangentY[CornerIndex]);
|
|
CornerTangentY[CornerIndex].Normalize();
|
|
}
|
|
|
|
// Copy back to the mesh.
|
|
for (int32 CornerIndex = 0; CornerIndex < 3; CornerIndex++)
|
|
{
|
|
OutTangentX[WedgeOffset + CornerIndex] = CornerTangentX[CornerIndex];
|
|
OutTangentY[WedgeOffset + CornerIndex] = CornerTangentY[CornerIndex];
|
|
OutTangentZ[WedgeOffset + CornerIndex] = CornerTangentZ[CornerIndex];
|
|
}
|
|
}
|
|
|
|
check(OutTangentX.Num() == NumWedges);
|
|
check(OutTangentY.Num() == NumWedges);
|
|
check(OutTangentZ.Num() == NumWedges);
|
|
}
|
|
|
|
|
|
static void ComputeTangents(
|
|
FRawMesh& RawMesh,
|
|
TMultiMap<int32, int32> const& OverlappingCorners,
|
|
uint32 TangentOptions
|
|
)
|
|
{
|
|
const float ComparisonThreshold = (TangentOptions & ETangentOptions::IgnoreDegenerateTriangles) ? THRESH_POINTS_ARE_SAME : 0.0f;
|
|
ComputeTangents(RawMesh.VertexPositions, RawMesh.WedgeIndices, RawMesh.WedgeTexCoords[0], RawMesh.FaceSmoothingMasks, OverlappingCorners, RawMesh.WedgeTangentX, RawMesh.WedgeTangentY, RawMesh.WedgeTangentZ, TangentOptions);
|
|
}
|
|
|
|
/*------------------------------------------------------------------------------
|
|
MikkTSpace for computing tangents.
|
|
------------------------------------------------------------------------------*/
|
|
|
|
static int MikkGetNumFaces(const SMikkTSpaceContext* Context)
|
|
{
|
|
FRawMesh *UserData = (FRawMesh*)(Context->m_pUserData);
|
|
return UserData->WedgeIndices.Num() / 3;
|
|
}
|
|
|
|
static int MikkGetNumVertsOfFace(const SMikkTSpaceContext* Context, const int FaceIdx)
|
|
{
|
|
// All of our meshes are triangles.
|
|
return 3;
|
|
}
|
|
|
|
static void MikkGetPosition(const SMikkTSpaceContext* Context, float Position[3], const int FaceIdx, const int VertIdx)
|
|
{
|
|
FRawMesh *UserData = (FRawMesh*)(Context->m_pUserData);
|
|
FVector VertexPosition = UserData->GetWedgePosition(FaceIdx * 3 + VertIdx);
|
|
Position[0] = VertexPosition.X;
|
|
Position[1] = VertexPosition.Y;
|
|
Position[2] = VertexPosition.Z;
|
|
}
|
|
|
|
static void MikkGetNormal(const SMikkTSpaceContext* Context, float Normal[3], const int FaceIdx, const int VertIdx)
|
|
{
|
|
FRawMesh *UserData = (FRawMesh*)(Context->m_pUserData);
|
|
FVector &VertexNormal = UserData->WedgeTangentZ[FaceIdx * 3 + VertIdx];
|
|
for (int32 i = 0; i < 3; ++i)
|
|
{
|
|
Normal[i] = VertexNormal[i];
|
|
}
|
|
}
|
|
|
|
static void MikkSetTSpaceBasic(const SMikkTSpaceContext* Context, const float Tangent[3], const float BitangentSign, const int FaceIdx, const int VertIdx)
|
|
{
|
|
FRawMesh *UserData = (FRawMesh*)(Context->m_pUserData);
|
|
FVector &VertexTangent = UserData->WedgeTangentX[FaceIdx * 3 + VertIdx];
|
|
for (int32 i = 0; i < 3; ++i)
|
|
{
|
|
VertexTangent[i] = Tangent[i];
|
|
}
|
|
FVector Bitangent = BitangentSign * FVector::CrossProduct(UserData->WedgeTangentZ[FaceIdx * 3 + VertIdx], VertexTangent);
|
|
FVector &VertexBitangent = UserData->WedgeTangentY[FaceIdx * 3 + VertIdx];
|
|
for (int32 i = 0; i < 3; ++i)
|
|
{
|
|
VertexBitangent[i] = -Bitangent[i];
|
|
}
|
|
}
|
|
|
|
static void MikkGetTexCoord(const SMikkTSpaceContext* Context, float UV[2], const int FaceIdx, const int VertIdx)
|
|
{
|
|
FRawMesh *UserData = (FRawMesh*)(Context->m_pUserData);
|
|
FVector2D &TexCoord = UserData->WedgeTexCoords[0][FaceIdx * 3 + VertIdx];
|
|
UV[0] = TexCoord.X;
|
|
UV[1] = TexCoord.Y;
|
|
}
|
|
|
|
// MikkTSpace implementations for skeletal meshes, where tangents/bitangents are ultimately derived from lists of attributes.
|
|
|
|
// Holder for skeletal data to be passed to MikkTSpace.
|
|
// Holds references to the wedge, face and points vectors that BuildSkeletalMesh is given.
|
|
// Holds reference to the calculated normals array, which will be fleshed out if they've been calculated.
|
|
// Holds reference to the newly created tangent and bitangent arrays, which MikkTSpace will fleshed out if required.
|
|
class MikkTSpace_Skeletal_Mesh
|
|
{
|
|
public:
|
|
const TArray<FMeshWedge> &wedges; //Reference to wedge list.
|
|
const TArray<FMeshFace> &faces; //Reference to face list. Also contains normal/tangent/bitanget/UV coords for each vertex of the face.
|
|
const TArray<FVector> &points; //Reference to position list.
|
|
bool bComputeNormals; //Copy of bComputeNormals.
|
|
TArray<FVector> &TangentsX; //Reference to newly created tangents list.
|
|
TArray<FVector> &TangentsY; //Reference to newly created bitangents list.
|
|
TArray<FVector> &TangentsZ; //Reference to computed normals, will be empty otherwise.
|
|
|
|
MikkTSpace_Skeletal_Mesh(
|
|
const TArray<FMeshWedge> &Wedges,
|
|
const TArray<FMeshFace> &Faces,
|
|
const TArray<FVector> &Points,
|
|
bool bInComputeNormals,
|
|
TArray<FVector> &VertexTangentsX,
|
|
TArray<FVector> &VertexTangentsY,
|
|
TArray<FVector> &VertexTangentsZ
|
|
)
|
|
:
|
|
wedges(Wedges),
|
|
faces(Faces),
|
|
points(Points),
|
|
bComputeNormals(bInComputeNormals),
|
|
TangentsX(VertexTangentsX),
|
|
TangentsY(VertexTangentsY),
|
|
TangentsZ(VertexTangentsZ)
|
|
{
|
|
}
|
|
};
|
|
|
|
static int MikkGetNumFaces_Skeletal(const SMikkTSpaceContext* Context)
|
|
{
|
|
MikkTSpace_Skeletal_Mesh *UserData = (MikkTSpace_Skeletal_Mesh*)(Context->m_pUserData);
|
|
return UserData->faces.Num();
|
|
}
|
|
|
|
static int MikkGetNumVertsOfFace_Skeletal(const SMikkTSpaceContext* Context, const int FaceIdx)
|
|
{
|
|
// Confirmed?
|
|
return 3;
|
|
}
|
|
|
|
static void MikkGetPosition_Skeletal(const SMikkTSpaceContext* Context, float Position[3], const int FaceIdx, const int VertIdx)
|
|
{
|
|
MikkTSpace_Skeletal_Mesh *UserData = (MikkTSpace_Skeletal_Mesh*)(Context->m_pUserData);
|
|
const FVector &VertexPosition = UserData->points[UserData->wedges[UserData->faces[FaceIdx].iWedge[VertIdx]].iVertex];
|
|
Position[0] = VertexPosition.X;
|
|
Position[1] = VertexPosition.Y;
|
|
Position[2] = VertexPosition.Z;
|
|
}
|
|
|
|
static void MikkGetNormal_Skeletal(const SMikkTSpaceContext* Context, float Normal[3], const int FaceIdx, const int VertIdx)
|
|
{
|
|
MikkTSpace_Skeletal_Mesh *UserData = (MikkTSpace_Skeletal_Mesh*)(Context->m_pUserData);
|
|
// Get different normals depending on whether they've been calculated or not.
|
|
if (UserData->bComputeNormals) {
|
|
FVector &VertexNormal = UserData->TangentsZ[FaceIdx * 3 + VertIdx];
|
|
Normal[0] = VertexNormal.X;
|
|
Normal[1] = VertexNormal.Y;
|
|
Normal[2] = VertexNormal.Z;
|
|
}
|
|
else
|
|
{
|
|
const FVector &VertexNormal = UserData->faces[FaceIdx].TangentZ[VertIdx];
|
|
Normal[0] = VertexNormal.X;
|
|
Normal[1] = VertexNormal.Y;
|
|
Normal[2] = VertexNormal.Z;
|
|
}
|
|
}
|
|
|
|
static void MikkSetTSpaceBasic_Skeletal(const SMikkTSpaceContext* Context, const float Tangent[3], const float BitangentSign, const int FaceIdx, const int VertIdx)
|
|
{
|
|
MikkTSpace_Skeletal_Mesh *UserData = (MikkTSpace_Skeletal_Mesh*)(Context->m_pUserData);
|
|
FVector &VertexTangent = UserData->TangentsX[FaceIdx * 3 + VertIdx];
|
|
VertexTangent.X = Tangent[0];
|
|
VertexTangent.Y = Tangent[1];
|
|
VertexTangent.Z = Tangent[2];
|
|
|
|
FVector Bitangent;
|
|
// Get different normals depending on whether they've been calculated or not.
|
|
if (UserData->bComputeNormals) {
|
|
Bitangent = BitangentSign * FVector::CrossProduct(UserData->TangentsZ[FaceIdx * 3 + VertIdx], VertexTangent);
|
|
}
|
|
else
|
|
{
|
|
Bitangent = BitangentSign * FVector::CrossProduct(UserData->faces[FaceIdx].TangentZ[VertIdx], VertexTangent);
|
|
}
|
|
FVector &VertexBitangent = UserData->TangentsY[FaceIdx * 3 + VertIdx];
|
|
// Switch the tangent space swizzle to X+Y-Z+ for legacy reasons.
|
|
VertexBitangent.X = -Bitangent[0];
|
|
VertexBitangent.Y = -Bitangent[1];
|
|
VertexBitangent.Z = -Bitangent[2];
|
|
}
|
|
|
|
static void MikkGetTexCoord_Skeletal(const SMikkTSpaceContext* Context, float UV[2], const int FaceIdx, const int VertIdx)
|
|
{
|
|
MikkTSpace_Skeletal_Mesh *UserData = (MikkTSpace_Skeletal_Mesh*)(Context->m_pUserData);
|
|
const FVector2D &TexCoord = UserData->wedges[UserData->faces[FaceIdx].iWedge[VertIdx]].UVs[0];
|
|
UV[0] = TexCoord.X;
|
|
UV[1] = TexCoord.Y;
|
|
}
|
|
|
|
static void ComputeTangents_MikkTSpace(
|
|
FRawMesh& RawMesh,
|
|
TMultiMap<int32, int32> const& OverlappingCorners,
|
|
uint32 TangentOptions
|
|
)
|
|
{
|
|
bool bBlendOverlappingNormals = (TangentOptions & ETangentOptions::BlendOverlappingNormals) != 0;
|
|
bool bIgnoreDegenerateTriangles = (TangentOptions & ETangentOptions::IgnoreDegenerateTriangles) != 0;
|
|
float ComparisonThreshold = bIgnoreDegenerateTriangles ? THRESH_POINTS_ARE_SAME : 0.0f;
|
|
|
|
// Compute per-triangle tangents.
|
|
TArray<FVector> TriangleTangentX;
|
|
TArray<FVector> TriangleTangentY;
|
|
TArray<FVector> TriangleTangentZ;
|
|
|
|
ComputeTriangleTangents(
|
|
TriangleTangentX,
|
|
TriangleTangentY,
|
|
TriangleTangentZ,
|
|
RawMesh,
|
|
bIgnoreDegenerateTriangles ? SMALL_NUMBER : 0.0f
|
|
);
|
|
|
|
// Declare these out here to avoid reallocations.
|
|
TArray<FFanFace> RelevantFacesForCorner[3];
|
|
TArray<int32> AdjacentFaces;
|
|
TArray<int32> DupVerts;
|
|
|
|
int32 NumWedges = RawMesh.WedgeIndices.Num();
|
|
int32 NumFaces = NumWedges / 3;
|
|
|
|
bool bWedgeNormals = true;
|
|
bool bWedgeTSpace = false;
|
|
for (int32 WedgeIdx = 0; WedgeIdx < RawMesh.WedgeTangentZ.Num(); ++WedgeIdx)
|
|
{
|
|
bWedgeNormals = bWedgeNormals && (!RawMesh.WedgeTangentZ[WedgeIdx].IsNearlyZero());
|
|
}
|
|
|
|
if (RawMesh.WedgeTangentX.Num() > 0 && RawMesh.WedgeTangentY.Num() > 0)
|
|
{
|
|
bWedgeTSpace = true;
|
|
for (int32 WedgeIdx = 0; WedgeIdx < RawMesh.WedgeTangentX.Num()
|
|
&& WedgeIdx < RawMesh.WedgeTangentY.Num(); ++WedgeIdx)
|
|
{
|
|
bWedgeTSpace = bWedgeTSpace && (!RawMesh.WedgeTangentX[WedgeIdx].IsNearlyZero()) && (!RawMesh.WedgeTangentY[WedgeIdx].IsNearlyZero());
|
|
}
|
|
}
|
|
|
|
// Allocate storage for tangents if none were provided, and calculate normals for MikkTSpace.
|
|
if (RawMesh.WedgeTangentZ.Num() != NumWedges || !bWedgeNormals)
|
|
{
|
|
// normals are not included, so we should calculate them
|
|
RawMesh.WedgeTangentZ.Empty(NumWedges);
|
|
RawMesh.WedgeTangentZ.AddZeroed(NumWedges);
|
|
|
|
// we need to calculate normals for MikkTSpace
|
|
UE_LOG(LogMeshUtilities, Log, TEXT("Invalid vertex normals found for mesh. Forcing recomputation of vertex normals for MikkTSpace. Fix mesh or disable \"Use MikkTSpace Tangent Space\" to avoid forced recomputation of normals."));
|
|
|
|
for (int32 FaceIndex = 0; FaceIndex < NumFaces; FaceIndex++)
|
|
{
|
|
int32 WedgeOffset = FaceIndex * 3;
|
|
FVector CornerPositions[3];
|
|
FVector CornerNormal[3];
|
|
|
|
for (int32 CornerIndex = 0; CornerIndex < 3; CornerIndex++)
|
|
{
|
|
CornerNormal[CornerIndex] = FVector::ZeroVector;
|
|
CornerPositions[CornerIndex] = GetPositionForWedge(RawMesh, WedgeOffset + CornerIndex);
|
|
RelevantFacesForCorner[CornerIndex].Reset();
|
|
}
|
|
|
|
// Don't process degenerate triangles.
|
|
if (PointsEqual(CornerPositions[0], CornerPositions[1], ComparisonThreshold)
|
|
|| PointsEqual(CornerPositions[0], CornerPositions[2], ComparisonThreshold)
|
|
|| PointsEqual(CornerPositions[1], CornerPositions[2], ComparisonThreshold))
|
|
{
|
|
continue;
|
|
}
|
|
|
|
// No need to process triangles if tangents already exist.
|
|
bool bCornerHasNormal[3] = { 0 };
|
|
for (int32 CornerIndex = 0; CornerIndex < 3; CornerIndex++)
|
|
{
|
|
bCornerHasNormal[CornerIndex] = !RawMesh.WedgeTangentZ[WedgeOffset + CornerIndex].IsZero();
|
|
}
|
|
if (bCornerHasNormal[0] && bCornerHasNormal[1] && bCornerHasNormal[2])
|
|
{
|
|
continue;
|
|
}
|
|
|
|
// Start building a list of faces adjacent to this face.
|
|
AdjacentFaces.Reset();
|
|
for (int32 CornerIndex = 0; CornerIndex < 3; CornerIndex++)
|
|
{
|
|
int32 ThisCornerIndex = WedgeOffset + CornerIndex;
|
|
DupVerts.Reset();
|
|
OverlappingCorners.MultiFind(ThisCornerIndex, DupVerts);
|
|
DupVerts.Add(ThisCornerIndex); // I am a "dup" of myself
|
|
for (int32 k = 0; k < DupVerts.Num(); k++)
|
|
{
|
|
AdjacentFaces.AddUnique(DupVerts[k] / 3);
|
|
}
|
|
}
|
|
|
|
// We need to sort these here because the criteria for point equality is
|
|
// exact, so we must ensure the exact same order for all dups.
|
|
AdjacentFaces.Sort();
|
|
|
|
// Process adjacent faces
|
|
for (int32 AdjacentFaceIndex = 0; AdjacentFaceIndex < AdjacentFaces.Num(); AdjacentFaceIndex++)
|
|
{
|
|
int32 OtherFaceIndex = AdjacentFaces[AdjacentFaceIndex];
|
|
for (int32 OurCornerIndex = 0; OurCornerIndex < 3; OurCornerIndex++)
|
|
{
|
|
if (bCornerHasNormal[OurCornerIndex])
|
|
continue;
|
|
|
|
FFanFace NewFanFace;
|
|
int32 CommonIndexCount = 0;
|
|
|
|
// Check for vertices in common.
|
|
if (FaceIndex == OtherFaceIndex)
|
|
{
|
|
CommonIndexCount = 3;
|
|
NewFanFace.LinkedVertexIndex = OurCornerIndex;
|
|
}
|
|
else
|
|
{
|
|
// Check matching vertices against main vertex .
|
|
for (int32 OtherCornerIndex = 0; OtherCornerIndex < 3; OtherCornerIndex++)
|
|
{
|
|
if (PointsEqual(
|
|
CornerPositions[OurCornerIndex],
|
|
GetPositionForWedge(RawMesh, OtherFaceIndex * 3 + OtherCornerIndex),
|
|
ComparisonThreshold
|
|
))
|
|
{
|
|
CommonIndexCount++;
|
|
NewFanFace.LinkedVertexIndex = OtherCornerIndex;
|
|
}
|
|
}
|
|
}
|
|
|
|
// Add if connected by at least one point. Smoothing matches are considered later.
|
|
if (CommonIndexCount > 0)
|
|
{
|
|
NewFanFace.FaceIndex = OtherFaceIndex;
|
|
NewFanFace.bFilled = (OtherFaceIndex == FaceIndex); // Starter face for smoothing floodfill.
|
|
NewFanFace.bBlendTangents = NewFanFace.bFilled;
|
|
NewFanFace.bBlendNormals = NewFanFace.bFilled;
|
|
RelevantFacesForCorner[OurCornerIndex].Add(NewFanFace);
|
|
}
|
|
}
|
|
}
|
|
|
|
// Find true relevance of faces for a vertex normal by traversing
|
|
// smoothing-group-compatible connected triangle fans around common vertices.
|
|
for (int32 CornerIndex = 0; CornerIndex < 3; CornerIndex++)
|
|
{
|
|
if (bCornerHasNormal[CornerIndex])
|
|
continue;
|
|
|
|
int32 NewConnections;
|
|
do
|
|
{
|
|
NewConnections = 0;
|
|
for (int32 OtherFaceIdx = 0; OtherFaceIdx < RelevantFacesForCorner[CornerIndex].Num(); OtherFaceIdx++)
|
|
{
|
|
FFanFace& OtherFace = RelevantFacesForCorner[CornerIndex][OtherFaceIdx];
|
|
// The vertex' own face is initially the only face with bFilled == true.
|
|
if (OtherFace.bFilled)
|
|
{
|
|
for (int32 NextFaceIndex = 0; NextFaceIndex < RelevantFacesForCorner[CornerIndex].Num(); NextFaceIndex++)
|
|
{
|
|
FFanFace& NextFace = RelevantFacesForCorner[CornerIndex][NextFaceIndex];
|
|
if (!NextFace.bFilled) // && !NextFace.bBlendTangents)
|
|
{
|
|
if ((NextFaceIndex != OtherFaceIdx)
|
|
&& (RawMesh.FaceSmoothingMasks[NextFace.FaceIndex] & RawMesh.FaceSmoothingMasks[OtherFace.FaceIndex]))
|
|
{
|
|
int32 CommonVertices = 0;
|
|
int32 CommonNormalVertices = 0;
|
|
for (int32 OtherCornerIndex = 0; OtherCornerIndex < 3; OtherCornerIndex++)
|
|
{
|
|
for (int32 NextCornerIndex = 0; NextCornerIndex < 3; NextCornerIndex++)
|
|
{
|
|
int32 NextVertexIndex = RawMesh.WedgeIndices[NextFace.FaceIndex * 3 + NextCornerIndex];
|
|
int32 OtherVertexIndex = RawMesh.WedgeIndices[OtherFace.FaceIndex * 3 + OtherCornerIndex];
|
|
if (PointsEqual(
|
|
RawMesh.VertexPositions[NextVertexIndex],
|
|
RawMesh.VertexPositions[OtherVertexIndex],
|
|
ComparisonThreshold))
|
|
{
|
|
CommonVertices++;
|
|
if (bBlendOverlappingNormals
|
|
|| NextVertexIndex == OtherVertexIndex)
|
|
{
|
|
CommonNormalVertices++;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
// Flood fill faces with more than one common vertices which must be touching edges.
|
|
if (CommonVertices > 1)
|
|
{
|
|
NextFace.bFilled = true;
|
|
NextFace.bBlendNormals = (CommonNormalVertices > 1);
|
|
NewConnections++;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
while (NewConnections > 0);
|
|
}
|
|
|
|
|
|
// Vertex normal construction.
|
|
for (int32 CornerIndex = 0; CornerIndex < 3; CornerIndex++)
|
|
{
|
|
if (bCornerHasNormal[CornerIndex])
|
|
{
|
|
CornerNormal[CornerIndex] = RawMesh.WedgeTangentZ[WedgeOffset + CornerIndex];
|
|
}
|
|
else
|
|
{
|
|
for (int32 RelevantFaceIdx = 0; RelevantFaceIdx < RelevantFacesForCorner[CornerIndex].Num(); RelevantFaceIdx++)
|
|
{
|
|
FFanFace const& RelevantFace = RelevantFacesForCorner[CornerIndex][RelevantFaceIdx];
|
|
if (RelevantFace.bFilled)
|
|
{
|
|
int32 OtherFaceIndex = RelevantFace.FaceIndex;
|
|
if (RelevantFace.bBlendNormals)
|
|
{
|
|
CornerNormal[CornerIndex] += TriangleTangentZ[OtherFaceIndex];
|
|
}
|
|
}
|
|
}
|
|
if (!RawMesh.WedgeTangentZ[WedgeOffset + CornerIndex].IsZero())
|
|
{
|
|
CornerNormal[CornerIndex] = RawMesh.WedgeTangentZ[WedgeOffset + CornerIndex];
|
|
}
|
|
}
|
|
}
|
|
|
|
// Normalization.
|
|
for (int32 CornerIndex = 0; CornerIndex < 3; CornerIndex++)
|
|
{
|
|
CornerNormal[CornerIndex].Normalize();
|
|
}
|
|
|
|
// Copy back to the mesh.
|
|
for (int32 CornerIndex = 0; CornerIndex < 3; CornerIndex++)
|
|
{
|
|
RawMesh.WedgeTangentZ[WedgeOffset + CornerIndex] = CornerNormal[CornerIndex];
|
|
}
|
|
}
|
|
}
|
|
|
|
if (RawMesh.WedgeTangentX.Num() != NumWedges)
|
|
{
|
|
RawMesh.WedgeTangentX.Empty(NumWedges);
|
|
RawMesh.WedgeTangentX.AddZeroed(NumWedges);
|
|
}
|
|
if (RawMesh.WedgeTangentY.Num() != NumWedges)
|
|
{
|
|
RawMesh.WedgeTangentY.Empty(NumWedges);
|
|
RawMesh.WedgeTangentY.AddZeroed(NumWedges);
|
|
}
|
|
|
|
if (!bWedgeTSpace)
|
|
{
|
|
// we can use mikktspace to calculate the tangents
|
|
SMikkTSpaceInterface MikkTInterface;
|
|
MikkTInterface.m_getNormal = MikkGetNormal;
|
|
MikkTInterface.m_getNumFaces = MikkGetNumFaces;
|
|
MikkTInterface.m_getNumVerticesOfFace = MikkGetNumVertsOfFace;
|
|
MikkTInterface.m_getPosition = MikkGetPosition;
|
|
MikkTInterface.m_getTexCoord = MikkGetTexCoord;
|
|
MikkTInterface.m_setTSpaceBasic = MikkSetTSpaceBasic;
|
|
MikkTInterface.m_setTSpace = nullptr;
|
|
|
|
SMikkTSpaceContext MikkTContext;
|
|
MikkTContext.m_pInterface = &MikkTInterface;
|
|
MikkTContext.m_pUserData = (void*)(&RawMesh);
|
|
MikkTContext.m_bIgnoreDegenerates = bIgnoreDegenerateTriangles;
|
|
genTangSpaceDefault(&MikkTContext);
|
|
}
|
|
|
|
check(RawMesh.WedgeTangentX.Num() == NumWedges);
|
|
check(RawMesh.WedgeTangentY.Num() == NumWedges);
|
|
check(RawMesh.WedgeTangentZ.Num() == NumWedges);
|
|
}
|
|
|
|
static void BuildDepthOnlyIndexBuffer(
|
|
TArray<uint32>& OutDepthIndices,
|
|
const TArray<FStaticMeshBuildVertex>& InVertices,
|
|
const TArray<uint32>& InIndices,
|
|
const TArray<FStaticMeshSection>& InSections
|
|
)
|
|
{
|
|
int32 NumVertices = InVertices.Num();
|
|
if (InIndices.Num() <= 0 || NumVertices <= 0)
|
|
{
|
|
OutDepthIndices.Empty();
|
|
return;
|
|
}
|
|
|
|
// Create a mapping of index -> first overlapping index to accelerate the construction of the shadow index buffer.
|
|
TArray<FIndexAndZ> VertIndexAndZ;
|
|
VertIndexAndZ.Empty(NumVertices);
|
|
for (int32 VertIndex = 0; VertIndex < NumVertices; VertIndex++)
|
|
{
|
|
new(VertIndexAndZ)FIndexAndZ(VertIndex, InVertices[VertIndex].Position);
|
|
}
|
|
VertIndexAndZ.Sort(FCompareIndexAndZ());
|
|
|
|
// Setup the index map. 0xFFFFFFFF == not set.
|
|
TArray<uint32> IndexMap;
|
|
IndexMap.AddUninitialized(NumVertices);
|
|
FMemory::Memset(IndexMap.GetData(), 0xFF, NumVertices * sizeof(uint32));
|
|
|
|
// Search for duplicates, quickly!
|
|
for (int32 i = 0; i < VertIndexAndZ.Num(); i++)
|
|
{
|
|
uint32 SrcIndex = VertIndexAndZ[i].Index;
|
|
float Z = VertIndexAndZ[i].Z;
|
|
IndexMap[SrcIndex] = FMath::Min(IndexMap[SrcIndex], SrcIndex);
|
|
|
|
// Search forward since we add pairs both ways.
|
|
for (int32 j = i + 1; j < VertIndexAndZ.Num(); j++)
|
|
{
|
|
if (FMath::Abs(VertIndexAndZ[j].Z - Z) > THRESH_POINTS_ARE_SAME * 4.01f)
|
|
break; // can't be any more dups
|
|
|
|
uint32 OtherIndex = VertIndexAndZ[j].Index;
|
|
if (PointsEqual(InVertices[SrcIndex].Position, InVertices[OtherIndex].Position,/*bUseEpsilonCompare=*/ true))
|
|
{
|
|
IndexMap[SrcIndex] = FMath::Min(IndexMap[SrcIndex], OtherIndex);
|
|
IndexMap[OtherIndex] = FMath::Min(IndexMap[OtherIndex], SrcIndex);
|
|
}
|
|
}
|
|
}
|
|
|
|
// Build the depth-only index buffer by remapping all indices to the first overlapping
|
|
// vertex in the vertex buffer.
|
|
OutDepthIndices.Empty();
|
|
for (int32 SectionIndex = 0; SectionIndex < InSections.Num(); ++SectionIndex)
|
|
{
|
|
const FStaticMeshSection& Section = InSections[SectionIndex];
|
|
int32 FirstIndex = Section.FirstIndex;
|
|
int32 LastIndex = FirstIndex + Section.NumTriangles * 3;
|
|
for (int32 SrcIndex = FirstIndex; SrcIndex < LastIndex; ++SrcIndex)
|
|
{
|
|
uint32 VertIndex = InIndices[SrcIndex];
|
|
OutDepthIndices.Add(IndexMap[VertIndex]);
|
|
}
|
|
}
|
|
}
|
|
|
|
static float GetComparisonThreshold(FMeshBuildSettings const& BuildSettings)
|
|
{
|
|
return BuildSettings.bRemoveDegenerates ? THRESH_POINTS_ARE_SAME : 0.0f;
|
|
}
|
|
|
|
/*------------------------------------------------------------------------------
|
|
Static mesh building.
|
|
------------------------------------------------------------------------------*/
|
|
|
|
static FStaticMeshBuildVertex BuildStaticMeshVertex(FRawMesh const& RawMesh, int32 WedgeIndex, FVector BuildScale)
|
|
{
|
|
FStaticMeshBuildVertex Vertex;
|
|
Vertex.Position = GetPositionForWedge(RawMesh, WedgeIndex) * BuildScale;
|
|
|
|
const FMatrix ScaleMatrix = FScaleMatrix(BuildScale).Inverse().GetTransposed();
|
|
Vertex.TangentX = ScaleMatrix.TransformVector(RawMesh.WedgeTangentX[WedgeIndex]).GetSafeNormal();
|
|
Vertex.TangentY = ScaleMatrix.TransformVector(RawMesh.WedgeTangentY[WedgeIndex]).GetSafeNormal();
|
|
Vertex.TangentZ = ScaleMatrix.TransformVector(RawMesh.WedgeTangentZ[WedgeIndex]).GetSafeNormal();
|
|
|
|
if (RawMesh.WedgeColors.IsValidIndex(WedgeIndex))
|
|
{
|
|
Vertex.Color = RawMesh.WedgeColors[WedgeIndex];
|
|
}
|
|
else
|
|
{
|
|
Vertex.Color = FColor::White;
|
|
}
|
|
|
|
int32 NumTexCoords = FMath::Min<int32>(MAX_MESH_TEXTURE_COORDS, MAX_STATIC_TEXCOORDS);
|
|
for (int32 i = 0; i < NumTexCoords; ++i)
|
|
{
|
|
if (RawMesh.WedgeTexCoords[i].IsValidIndex(WedgeIndex))
|
|
{
|
|
Vertex.UVs[i] = RawMesh.WedgeTexCoords[i][WedgeIndex];
|
|
}
|
|
else
|
|
{
|
|
Vertex.UVs[i] = FVector2D(0.0f, 0.0f);
|
|
}
|
|
}
|
|
return Vertex;
|
|
}
|
|
|
|
static bool AreVerticesEqual(
|
|
FStaticMeshBuildVertex const& A,
|
|
FStaticMeshBuildVertex const& B,
|
|
float ComparisonThreshold
|
|
)
|
|
{
|
|
if (!PointsEqual(A.Position, B.Position, ComparisonThreshold)
|
|
|| !NormalsEqual(A.TangentX, B.TangentX)
|
|
|| !NormalsEqual(A.TangentY, B.TangentY)
|
|
|| !NormalsEqual(A.TangentZ, B.TangentZ)
|
|
|| A.Color != B.Color)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
// UVs
|
|
for (int32 UVIndex = 0; UVIndex < MAX_STATIC_TEXCOORDS; UVIndex++)
|
|
{
|
|
if (!UVsEqual(A.UVs[UVIndex], B.UVs[UVIndex]))
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
void FMeshUtilities::BuildStaticMeshVertexAndIndexBuffers(
|
|
TArray<FStaticMeshBuildVertex>& OutVertices,
|
|
TArray<TArray<uint32> >& OutPerSectionIndices,
|
|
TArray<int32>& OutWedgeMap,
|
|
const FRawMesh& RawMesh,
|
|
const TMultiMap<int32, int32>& OverlappingCorners,
|
|
const TMap<uint32, uint32>& MaterialToSectionMapping,
|
|
float ComparisonThreshold,
|
|
FVector BuildScale,
|
|
int32 ImportVersion
|
|
)
|
|
{
|
|
TMap<int32, int32> FinalVerts;
|
|
TArray<int32> DupVerts;
|
|
int32 NumFaces = RawMesh.WedgeIndices.Num() / 3;
|
|
|
|
// Process each face, build vertex buffer and per-section index buffers.
|
|
for (int32 FaceIndex = 0; FaceIndex < NumFaces; FaceIndex++)
|
|
{
|
|
int32 VertexIndices[3];
|
|
FVector CornerPositions[3];
|
|
|
|
for (int32 CornerIndex = 0; CornerIndex < 3; CornerIndex++)
|
|
{
|
|
CornerPositions[CornerIndex] = GetPositionForWedge(RawMesh, FaceIndex * 3 + CornerIndex);
|
|
}
|
|
|
|
// Don't process degenerate triangles.
|
|
if (PointsEqual(CornerPositions[0], CornerPositions[1], ComparisonThreshold)
|
|
|| PointsEqual(CornerPositions[0], CornerPositions[2], ComparisonThreshold)
|
|
|| PointsEqual(CornerPositions[1], CornerPositions[2], ComparisonThreshold))
|
|
{
|
|
for (int32 CornerIndex = 0; CornerIndex < 3; CornerIndex++)
|
|
{
|
|
OutWedgeMap.Add(INDEX_NONE);
|
|
}
|
|
continue;
|
|
}
|
|
|
|
for (int32 CornerIndex = 0; CornerIndex < 3; CornerIndex++)
|
|
{
|
|
int32 WedgeIndex = FaceIndex * 3 + CornerIndex;
|
|
FStaticMeshBuildVertex ThisVertex = BuildStaticMeshVertex(RawMesh, WedgeIndex, BuildScale);
|
|
|
|
DupVerts.Reset();
|
|
OverlappingCorners.MultiFind(WedgeIndex, DupVerts);
|
|
DupVerts.Sort();
|
|
|
|
int32 Index = INDEX_NONE;
|
|
for (int32 k = 0; k < DupVerts.Num(); k++)
|
|
{
|
|
if (DupVerts[k] >= WedgeIndex)
|
|
{
|
|
// the verts beyond me haven't been placed yet, so these duplicates are not relevant
|
|
break;
|
|
}
|
|
|
|
int32 *Location = FinalVerts.Find(DupVerts[k]);
|
|
if (Location != NULL
|
|
&& AreVerticesEqual(ThisVertex, OutVertices[*Location], ComparisonThreshold))
|
|
{
|
|
Index = *Location;
|
|
break;
|
|
}
|
|
}
|
|
if (Index == INDEX_NONE)
|
|
{
|
|
Index = OutVertices.Add(ThisVertex);
|
|
FinalVerts.Add(WedgeIndex, Index);
|
|
}
|
|
VertexIndices[CornerIndex] = Index;
|
|
}
|
|
|
|
// Reject degenerate triangles.
|
|
if (VertexIndices[0] == VertexIndices[1]
|
|
|| VertexIndices[1] == VertexIndices[2]
|
|
|| VertexIndices[0] == VertexIndices[2])
|
|
{
|
|
for (int32 CornerIndex = 0; CornerIndex < 3; CornerIndex++)
|
|
{
|
|
OutWedgeMap.Add(INDEX_NONE);
|
|
}
|
|
continue;
|
|
}
|
|
|
|
// Put the indices in the material index buffer.
|
|
uint32 SectionIndex = 0;
|
|
if (ImportVersion < RemoveStaticMeshSkinxxWorkflow)
|
|
{
|
|
SectionIndex = FMath::Clamp(RawMesh.FaceMaterialIndices[FaceIndex], 0, OutPerSectionIndices.Num() - 1);
|
|
}
|
|
else
|
|
{
|
|
SectionIndex = MaterialToSectionMapping.FindChecked(RawMesh.FaceMaterialIndices[FaceIndex]);
|
|
}
|
|
TArray<uint32>& SectionIndices = OutPerSectionIndices[SectionIndex];
|
|
for (int32 CornerIndex = 0; CornerIndex < 3; CornerIndex++)
|
|
{
|
|
SectionIndices.Add(VertexIndices[CornerIndex]);
|
|
OutWedgeMap.Add(VertexIndices[CornerIndex]);
|
|
}
|
|
}
|
|
}
|
|
|
|
void FMeshUtilities::CacheOptimizeVertexAndIndexBuffer(
|
|
TArray<FStaticMeshBuildVertex>& Vertices,
|
|
TArray<TArray<uint32> >& PerSectionIndices,
|
|
TArray<int32>& WedgeMap
|
|
)
|
|
{
|
|
// Copy the vertices since we will be reordering them
|
|
TArray<FStaticMeshBuildVertex> OriginalVertices = Vertices;
|
|
|
|
// Initialize a cache that stores which indices have been assigned
|
|
TArray<int32> IndexCache;
|
|
IndexCache.AddUninitialized(Vertices.Num());
|
|
FMemory::Memset(IndexCache.GetData(), INDEX_NONE, IndexCache.Num() * IndexCache.GetTypeSize());
|
|
int32 NextAvailableIndex = 0;
|
|
|
|
// Iterate through the section index buffers,
|
|
// Optimizing index order for the post transform cache (minimizes the number of vertices transformed),
|
|
// And vertex order for the pre transform cache (minimizes the amount of vertex data fetched by the GPU).
|
|
for (int32 SectionIndex = 0; SectionIndex < PerSectionIndices.Num(); SectionIndex++)
|
|
{
|
|
TArray<uint32>& Indices = PerSectionIndices[SectionIndex];
|
|
|
|
if (Indices.Num())
|
|
{
|
|
// Optimize the index buffer for the post transform cache with.
|
|
CacheOptimizeIndexBuffer(Indices);
|
|
|
|
// Copy the index buffer since we will be reordering it
|
|
TArray<uint32> OriginalIndices = Indices;
|
|
|
|
// Go through the indices and assign them new values that are coherent where possible
|
|
for (int32 Index = 0; Index < Indices.Num(); Index++)
|
|
{
|
|
const int32 CachedIndex = IndexCache[OriginalIndices[Index]];
|
|
|
|
if (CachedIndex == INDEX_NONE)
|
|
{
|
|
// No new index has been allocated for this existing index, assign a new one
|
|
Indices[Index] = NextAvailableIndex;
|
|
// Mark what this index has been assigned to
|
|
IndexCache[OriginalIndices[Index]] = NextAvailableIndex;
|
|
NextAvailableIndex++;
|
|
}
|
|
else
|
|
{
|
|
// Reuse an existing index assignment
|
|
Indices[Index] = CachedIndex;
|
|
}
|
|
// Reorder the vertices based on the new index assignment
|
|
Vertices[Indices[Index]] = OriginalVertices[OriginalIndices[Index]];
|
|
}
|
|
}
|
|
}
|
|
|
|
for (int32 i = 0; i < WedgeMap.Num(); i++)
|
|
{
|
|
int32 MappedIndex = WedgeMap[i];
|
|
if (MappedIndex != INDEX_NONE)
|
|
{
|
|
WedgeMap[i] = IndexCache[MappedIndex];
|
|
}
|
|
}
|
|
}
|
|
|
|
class FStaticMeshUtilityBuilder
|
|
{
|
|
public:
|
|
FStaticMeshUtilityBuilder() : Stage(EStage::Uninit), NumValidLODs(0) {}
|
|
|
|
bool GatherSourceMeshesPerLOD(TArray<FStaticMeshSourceModel>& SourceModels, IMeshReduction* MeshReduction)
|
|
{
|
|
check(Stage == EStage::Uninit);
|
|
|
|
// Gather source meshes for each LOD.
|
|
for (int32 LODIndex = 0; LODIndex < SourceModels.Num(); ++LODIndex)
|
|
{
|
|
FStaticMeshSourceModel& SrcModel = SourceModels[LODIndex];
|
|
FRawMesh& RawMesh = *new(LODMeshes)FRawMesh;
|
|
TMultiMap<int32, int32>& OverlappingCorners = *new(LODOverlappingCorners)TMultiMap<int32, int32>;
|
|
|
|
if (!SrcModel.RawMeshBulkData->IsEmpty())
|
|
{
|
|
SrcModel.RawMeshBulkData->LoadRawMesh(RawMesh);
|
|
// Make sure the raw mesh is not irreparably malformed.
|
|
if (!RawMesh.IsValidOrFixable())
|
|
{
|
|
UE_LOG(LogMeshUtilities, Error, TEXT("Raw mesh is corrupt for LOD%d."), LODIndex);
|
|
return false;
|
|
}
|
|
LODBuildSettings[LODIndex] = SrcModel.BuildSettings;
|
|
|
|
float ComparisonThreshold = GetComparisonThreshold(LODBuildSettings[LODIndex]);
|
|
int32 NumWedges = RawMesh.WedgeIndices.Num();
|
|
|
|
// Find overlapping corners to accelerate adjacency.
|
|
FindOverlappingCorners(OverlappingCorners, RawMesh, ComparisonThreshold);
|
|
|
|
// Figure out if we should recompute normals and tangents.
|
|
bool bRecomputeNormals = SrcModel.BuildSettings.bRecomputeNormals || RawMesh.WedgeTangentZ.Num() != NumWedges;
|
|
bool bRecomputeTangents = SrcModel.BuildSettings.bRecomputeTangents || RawMesh.WedgeTangentX.Num() != NumWedges || RawMesh.WedgeTangentY.Num() != NumWedges;
|
|
|
|
// Dump normals and tangents if we are recomputing them.
|
|
if (bRecomputeTangents)
|
|
{
|
|
RawMesh.WedgeTangentX.Empty(NumWedges);
|
|
RawMesh.WedgeTangentX.AddZeroed(NumWedges);
|
|
RawMesh.WedgeTangentY.Empty(NumWedges);
|
|
RawMesh.WedgeTangentY.AddZeroed(NumWedges);
|
|
}
|
|
if (bRecomputeNormals)
|
|
{
|
|
RawMesh.WedgeTangentZ.Empty(NumWedges);
|
|
RawMesh.WedgeTangentZ.AddZeroed(NumWedges);
|
|
}
|
|
|
|
// Compute any missing tangents.
|
|
{
|
|
// Static meshes always blend normals of overlapping corners.
|
|
uint32 TangentOptions = ETangentOptions::BlendOverlappingNormals;
|
|
if (SrcModel.BuildSettings.bRemoveDegenerates)
|
|
{
|
|
// If removing degenerate triangles, ignore them when computing tangents.
|
|
TangentOptions |= ETangentOptions::IgnoreDegenerateTriangles;
|
|
}
|
|
|
|
//MikkTSpace should be use only when the user want to recompute the normals or tangents otherwise should always fallback on builtin
|
|
if (SrcModel.BuildSettings.bUseMikkTSpace && (SrcModel.BuildSettings.bRecomputeNormals || SrcModel.BuildSettings.bRecomputeTangents))
|
|
{
|
|
ComputeTangents_MikkTSpace(RawMesh, OverlappingCorners, TangentOptions);
|
|
}
|
|
else
|
|
{
|
|
ComputeTangents(RawMesh, OverlappingCorners, TangentOptions);
|
|
}
|
|
}
|
|
|
|
// At this point the mesh will have valid tangents.
|
|
check(RawMesh.WedgeTangentX.Num() == NumWedges);
|
|
check(RawMesh.WedgeTangentY.Num() == NumWedges);
|
|
check(RawMesh.WedgeTangentZ.Num() == NumWedges);
|
|
|
|
// Generate lightmap UVs
|
|
if (SrcModel.BuildSettings.bGenerateLightmapUVs)
|
|
{
|
|
if (RawMesh.WedgeTexCoords[SrcModel.BuildSettings.SrcLightmapIndex].Num() == 0)
|
|
{
|
|
SrcModel.BuildSettings.SrcLightmapIndex = 0;
|
|
}
|
|
|
|
FLayoutUV Packer(&RawMesh, SrcModel.BuildSettings.SrcLightmapIndex, SrcModel.BuildSettings.DstLightmapIndex, SrcModel.BuildSettings.MinLightmapResolution);
|
|
|
|
Packer.FindCharts(OverlappingCorners);
|
|
bool bPackSuccess = Packer.FindBestPacking();
|
|
if (bPackSuccess)
|
|
{
|
|
Packer.CommitPackedUVs();
|
|
}
|
|
}
|
|
HasRawMesh[LODIndex] = true;
|
|
}
|
|
else if (LODIndex > 0 && MeshReduction)
|
|
{
|
|
// If a raw mesh is not explicitly provided, use the raw mesh of the
|
|
// next highest LOD.
|
|
RawMesh = LODMeshes[LODIndex - 1];
|
|
OverlappingCorners = LODOverlappingCorners[LODIndex - 1];
|
|
LODBuildSettings[LODIndex] = LODBuildSettings[LODIndex - 1];
|
|
HasRawMesh[LODIndex] = false;
|
|
}
|
|
}
|
|
check(LODMeshes.Num() == SourceModels.Num());
|
|
check(LODOverlappingCorners.Num() == SourceModels.Num());
|
|
|
|
// Bail if there is no raw mesh data from which to build a renderable mesh.
|
|
if (LODMeshes.Num() == 0 || LODMeshes[0].WedgeIndices.Num() == 0)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
Stage = EStage::Gathered;
|
|
return true;
|
|
}
|
|
|
|
bool ReduceLODs(TArray<FStaticMeshSourceModel>& SourceModels, const FStaticMeshLODGroup& LODGroup, IMeshReduction* MeshReduction, bool& bOutWasReduced)
|
|
{
|
|
check(Stage == EStage::Gathered);
|
|
|
|
// Reduce each LOD mesh according to its reduction settings.
|
|
for (int32 LODIndex = 0; LODIndex < SourceModels.Num(); ++LODIndex)
|
|
{
|
|
const FStaticMeshSourceModel& SrcModel = SourceModels[LODIndex];
|
|
FMeshReductionSettings ReductionSettings = LODGroup.GetSettings(SrcModel.ReductionSettings, LODIndex);
|
|
LODMaxDeviation[NumValidLODs] = 0.0f;
|
|
if (LODIndex != NumValidLODs)
|
|
{
|
|
LODBuildSettings[NumValidLODs] = LODBuildSettings[LODIndex];
|
|
LODOverlappingCorners[NumValidLODs] = LODOverlappingCorners[LODIndex];
|
|
}
|
|
|
|
if (MeshReduction && (ReductionSettings.PercentTriangles < 1.0f || ReductionSettings.MaxDeviation > 0.0f))
|
|
{
|
|
FRawMesh& InMesh = LODMeshes[ReductionSettings.BaseLODModel];
|
|
FRawMesh& DestMesh = LODMeshes[NumValidLODs];
|
|
TMultiMap<int32, int32>& InOverlappingCorners = LODOverlappingCorners[ReductionSettings.BaseLODModel];
|
|
TMultiMap<int32, int32>& DestOverlappingCorners = LODOverlappingCorners[NumValidLODs];
|
|
|
|
MeshReduction->Reduce(DestMesh, LODMaxDeviation[NumValidLODs], InMesh, InOverlappingCorners, ReductionSettings);
|
|
if (DestMesh.WedgeIndices.Num() > 0 && !DestMesh.IsValid())
|
|
{
|
|
UE_LOG(LogMeshUtilities, Error, TEXT("Mesh reduction produced a corrupt mesh for LOD%d"), LODIndex);
|
|
return false;
|
|
}
|
|
bOutWasReduced = true;
|
|
|
|
// Recompute adjacency information.
|
|
DestOverlappingCorners.Reset();
|
|
float ComparisonThreshold = GetComparisonThreshold(LODBuildSettings[NumValidLODs]);
|
|
FindOverlappingCorners(DestOverlappingCorners, DestMesh, ComparisonThreshold);
|
|
}
|
|
|
|
if (LODMeshes[NumValidLODs].WedgeIndices.Num() > 0)
|
|
{
|
|
NumValidLODs++;
|
|
}
|
|
}
|
|
|
|
if (NumValidLODs < 1)
|
|
{
|
|
return false;
|
|
}
|
|
Stage = EStage::Reduce;
|
|
return true;
|
|
}
|
|
|
|
bool GenerateRenderingMeshes(FMeshUtilities& MeshUtilities, FStaticMeshRenderData& OutRenderData, TArray<FStaticMeshSourceModel>& InOutModels, int32 ImportVersion)
|
|
{
|
|
check(Stage == EStage::Reduce);
|
|
// Generate per-LOD rendering data.
|
|
OutRenderData.AllocateLODResources(NumValidLODs);
|
|
for (int32 LODIndex = 0; LODIndex < NumValidLODs; ++LODIndex)
|
|
{
|
|
FStaticMeshLODResources& LODModel = OutRenderData.LODResources[LODIndex];
|
|
FRawMesh& RawMesh = LODMeshes[LODIndex];
|
|
LODModel.MaxDeviation = LODMaxDeviation[LODIndex];
|
|
|
|
TArray<FStaticMeshBuildVertex> Vertices;
|
|
TArray<TArray<uint32> > PerSectionIndices;
|
|
|
|
TMap<uint32, uint32> MaterialToSectionMapping;
|
|
|
|
// Find out how many sections are in the mesh.
|
|
TArray<int32> MaterialIndices;
|
|
for ( const int32 MaterialIndex : RawMesh.FaceMaterialIndices )
|
|
{
|
|
// Find all unique material indices
|
|
MaterialIndices.AddUnique(MaterialIndex);
|
|
}
|
|
|
|
// Need X number of sections for X number of material indices
|
|
//for (const int32 MaterialIndex : MaterialIndices)
|
|
for ( int32 Index = 0; Index < MaterialIndices.Num(); ++Index)
|
|
{
|
|
const int32 MaterialIndex = MaterialIndices[Index];
|
|
FStaticMeshSection* Section = new(LODModel.Sections) FStaticMeshSection();
|
|
Section->MaterialIndex = MaterialIndex;
|
|
if (ImportVersion < RemoveStaticMeshSkinxxWorkflow)
|
|
{
|
|
MaterialToSectionMapping.Add(MaterialIndex, MaterialIndex);
|
|
}
|
|
else
|
|
{
|
|
MaterialToSectionMapping.Add(MaterialIndex, Index);
|
|
}
|
|
new(PerSectionIndices)TArray<uint32>;
|
|
}
|
|
|
|
// Build and cache optimize vertex and index buffers.
|
|
{
|
|
// TODO_STATICMESH: The wedge map is only valid for LODIndex 0 if no reduction has been performed.
|
|
// We can compute an approximate one instead for other LODs.
|
|
TArray<int32> TempWedgeMap;
|
|
TArray<int32>& WedgeMap = (LODIndex == 0 && InOutModels[0].ReductionSettings.PercentTriangles >= 1.0f) ? OutRenderData.WedgeMap : TempWedgeMap;
|
|
float ComparisonThreshold = GetComparisonThreshold(LODBuildSettings[LODIndex]);
|
|
MeshUtilities.BuildStaticMeshVertexAndIndexBuffers(Vertices, PerSectionIndices, WedgeMap, RawMesh, LODOverlappingCorners[LODIndex], MaterialToSectionMapping, ComparisonThreshold, LODBuildSettings[LODIndex].BuildScale3D, ImportVersion);
|
|
check(WedgeMap.Num() == RawMesh.WedgeIndices.Num());
|
|
|
|
if (RawMesh.WedgeIndices.Num() < 100000 * 3)
|
|
{
|
|
MeshUtilities.CacheOptimizeVertexAndIndexBuffer(Vertices, PerSectionIndices, WedgeMap);
|
|
check(WedgeMap.Num() == RawMesh.WedgeIndices.Num());
|
|
}
|
|
}
|
|
|
|
verifyf(Vertices.Num() != 0, TEXT("No valid vertices found for the mesh."));
|
|
|
|
// Initialize the vertex buffer.
|
|
int32 NumTexCoords = ComputeNumTexCoords(RawMesh, MAX_STATIC_TEXCOORDS);
|
|
LODModel.VertexBuffer.SetUseHighPrecisionTangentBasis(LODBuildSettings[LODIndex].bUseHighPrecisionTangentBasis);
|
|
LODModel.VertexBuffer.SetUseFullPrecisionUVs(LODBuildSettings[LODIndex].bUseFullPrecisionUVs);
|
|
LODModel.VertexBuffer.Init(Vertices, NumTexCoords);
|
|
LODModel.PositionVertexBuffer.Init(Vertices);
|
|
LODModel.ColorVertexBuffer.Init(Vertices);
|
|
|
|
// Concatenate the per-section index buffers.
|
|
TArray<uint32> CombinedIndices;
|
|
bool bNeeds32BitIndices = false;
|
|
for (int32 SectionIndex = 0; SectionIndex < LODModel.Sections.Num(); SectionIndex++)
|
|
{
|
|
FStaticMeshSection& Section = LODModel.Sections[SectionIndex];
|
|
TArray<uint32> const& SectionIndices = PerSectionIndices[SectionIndex];
|
|
Section.FirstIndex = 0;
|
|
Section.NumTriangles = 0;
|
|
Section.MinVertexIndex = 0;
|
|
Section.MaxVertexIndex = 0;
|
|
|
|
if (SectionIndices.Num())
|
|
{
|
|
Section.FirstIndex = CombinedIndices.Num();
|
|
Section.NumTriangles = SectionIndices.Num() / 3;
|
|
|
|
CombinedIndices.AddUninitialized(SectionIndices.Num());
|
|
uint32* DestPtr = &CombinedIndices[Section.FirstIndex];
|
|
uint32 const* SrcPtr = SectionIndices.GetData();
|
|
|
|
Section.MinVertexIndex = *SrcPtr;
|
|
Section.MaxVertexIndex = *SrcPtr;
|
|
|
|
for (int32 Index = 0; Index < SectionIndices.Num(); Index++)
|
|
{
|
|
uint32 VertIndex = *SrcPtr++;
|
|
|
|
bNeeds32BitIndices |= (VertIndex > MAX_uint16);
|
|
Section.MinVertexIndex = FMath::Min<uint32>(VertIndex, Section.MinVertexIndex);
|
|
Section.MaxVertexIndex = FMath::Max<uint32>(VertIndex, Section.MaxVertexIndex);
|
|
*DestPtr++ = VertIndex;
|
|
}
|
|
}
|
|
}
|
|
LODModel.IndexBuffer.SetIndices(CombinedIndices, bNeeds32BitIndices ? EIndexBufferStride::Force32Bit : EIndexBufferStride::Force16Bit);
|
|
|
|
// Build the reversed index buffer.
|
|
if (InOutModels[0].BuildSettings.bBuildReversedIndexBuffer)
|
|
{
|
|
TArray<uint32> InversedIndices;
|
|
const int32 IndexCount = CombinedIndices.Num();
|
|
InversedIndices.AddUninitialized(IndexCount);
|
|
|
|
for (int32 SectionIndex = 0; SectionIndex < LODModel.Sections.Num(); ++SectionIndex)
|
|
{
|
|
const FStaticMeshSection& SectionInfo = LODModel.Sections[SectionIndex];
|
|
const int32 SectionIndexCount = SectionInfo.NumTriangles * 3;
|
|
|
|
for (int32 i = 0; i < SectionIndexCount; ++i)
|
|
{
|
|
InversedIndices[SectionInfo.FirstIndex + i] = CombinedIndices[SectionInfo.FirstIndex + SectionIndexCount - 1 - i];
|
|
}
|
|
}
|
|
LODModel.ReversedIndexBuffer.SetIndices(InversedIndices, bNeeds32BitIndices ? EIndexBufferStride::Force32Bit : EIndexBufferStride::Force16Bit);
|
|
}
|
|
|
|
// Build the depth-only index buffer.
|
|
TArray<uint32> DepthOnlyIndices;
|
|
{
|
|
BuildDepthOnlyIndexBuffer(
|
|
DepthOnlyIndices,
|
|
Vertices,
|
|
CombinedIndices,
|
|
LODModel.Sections
|
|
);
|
|
|
|
if (DepthOnlyIndices.Num() < 50000 * 3)
|
|
{
|
|
MeshUtilities.CacheOptimizeIndexBuffer(DepthOnlyIndices);
|
|
}
|
|
|
|
LODModel.DepthOnlyIndexBuffer.SetIndices(DepthOnlyIndices, bNeeds32BitIndices ? EIndexBufferStride::Force32Bit : EIndexBufferStride::Force16Bit);
|
|
}
|
|
|
|
// Build the inversed depth only index buffer.
|
|
if (InOutModels[0].BuildSettings.bBuildReversedIndexBuffer)
|
|
{
|
|
TArray<uint32> ReversedDepthOnlyIndices;
|
|
const int32 IndexCount = DepthOnlyIndices.Num();
|
|
ReversedDepthOnlyIndices.AddUninitialized(IndexCount);
|
|
for (int32 i = 0; i < IndexCount; ++i)
|
|
{
|
|
ReversedDepthOnlyIndices[i] = DepthOnlyIndices[IndexCount - 1 - i];
|
|
}
|
|
LODModel.ReversedDepthOnlyIndexBuffer.SetIndices(ReversedDepthOnlyIndices, bNeeds32BitIndices ? EIndexBufferStride::Force32Bit : EIndexBufferStride::Force16Bit);
|
|
}
|
|
|
|
// Build a list of wireframe edges in the static mesh.
|
|
{
|
|
TArray<FMeshEdge> Edges;
|
|
TArray<uint32> WireframeIndices;
|
|
|
|
FStaticMeshEdgeBuilder(CombinedIndices, Vertices, Edges).FindEdges();
|
|
WireframeIndices.Empty(2 * Edges.Num());
|
|
for (int32 EdgeIndex = 0; EdgeIndex < Edges.Num(); EdgeIndex++)
|
|
{
|
|
FMeshEdge& Edge = Edges[EdgeIndex];
|
|
WireframeIndices.Add(Edge.Vertices[0]);
|
|
WireframeIndices.Add(Edge.Vertices[1]);
|
|
}
|
|
LODModel.WireframeIndexBuffer.SetIndices(WireframeIndices, bNeeds32BitIndices ? EIndexBufferStride::Force32Bit : EIndexBufferStride::Force16Bit);
|
|
}
|
|
|
|
// Build the adjacency index buffer used for tessellation.
|
|
if (InOutModels[0].BuildSettings.bBuildAdjacencyBuffer)
|
|
{
|
|
TArray<uint32> AdjacencyIndices;
|
|
|
|
BuildStaticAdjacencyIndexBuffer(
|
|
LODModel.PositionVertexBuffer,
|
|
LODModel.VertexBuffer,
|
|
CombinedIndices,
|
|
AdjacencyIndices
|
|
);
|
|
LODModel.AdjacencyIndexBuffer.SetIndices(AdjacencyIndices, bNeeds32BitIndices ? EIndexBufferStride::Force32Bit : EIndexBufferStride::Force16Bit);
|
|
}
|
|
}
|
|
|
|
// Copy the original material indices to fixup meshes before compacting of materials was done.
|
|
if (NumValidLODs > 0)
|
|
{
|
|
OutRenderData.MaterialIndexToImportIndex = LODMeshes[0].MaterialIndexToImportIndex;
|
|
}
|
|
|
|
// Calculate the bounding box.
|
|
FBox BoundingBox(0);
|
|
FPositionVertexBuffer& BasePositionVertexBuffer = OutRenderData.LODResources[0].PositionVertexBuffer;
|
|
for (uint32 VertexIndex = 0; VertexIndex < BasePositionVertexBuffer.GetNumVertices(); VertexIndex++)
|
|
{
|
|
BoundingBox += BasePositionVertexBuffer.VertexPosition(VertexIndex);
|
|
}
|
|
BoundingBox.GetCenterAndExtents(OutRenderData.Bounds.Origin, OutRenderData.Bounds.BoxExtent);
|
|
|
|
// Calculate the bounding sphere, using the center of the bounding box as the origin.
|
|
OutRenderData.Bounds.SphereRadius = 0.0f;
|
|
for (uint32 VertexIndex = 0; VertexIndex < BasePositionVertexBuffer.GetNumVertices(); VertexIndex++)
|
|
{
|
|
OutRenderData.Bounds.SphereRadius = FMath::Max(
|
|
(BasePositionVertexBuffer.VertexPosition(VertexIndex) - OutRenderData.Bounds.Origin).Size(),
|
|
OutRenderData.Bounds.SphereRadius
|
|
);
|
|
}
|
|
|
|
Stage = EStage::GenerateRendering;
|
|
return true;
|
|
}
|
|
|
|
bool ReplaceRawMeshModels(TArray<FStaticMeshSourceModel>& SourceModels)
|
|
{
|
|
check(Stage == EStage::Reduce);
|
|
|
|
check(HasRawMesh[0]);
|
|
check(SourceModels.Num() >= NumValidLODs);
|
|
bool bDirty = false;
|
|
for (int32 Index = 1; Index < NumValidLODs; ++Index)
|
|
{
|
|
if (!HasRawMesh[Index])
|
|
{
|
|
SourceModels[Index].RawMeshBulkData->SaveRawMesh(LODMeshes[Index]);
|
|
bDirty = true;
|
|
}
|
|
}
|
|
|
|
Stage = EStage::ReplaceRaw;
|
|
return true;
|
|
}
|
|
|
|
private:
|
|
enum class EStage
|
|
{
|
|
Uninit,
|
|
Gathered,
|
|
Reduce,
|
|
GenerateRendering,
|
|
ReplaceRaw,
|
|
};
|
|
|
|
EStage Stage;
|
|
|
|
int32 NumValidLODs;
|
|
|
|
TIndirectArray<FRawMesh> LODMeshes;
|
|
TIndirectArray<TMultiMap<int32, int32> > LODOverlappingCorners;
|
|
float LODMaxDeviation[MAX_STATIC_MESH_LODS];
|
|
FMeshBuildSettings LODBuildSettings[MAX_STATIC_MESH_LODS];
|
|
bool HasRawMesh[MAX_STATIC_MESH_LODS];
|
|
};
|
|
|
|
bool FMeshUtilities::BuildStaticMesh(FStaticMeshRenderData& OutRenderData, TArray<FStaticMeshSourceModel>& SourceModels, const FStaticMeshLODGroup& LODGroup, int32 ImportVersion)
|
|
{
|
|
FStaticMeshUtilityBuilder Builder;
|
|
if (!Builder.GatherSourceMeshesPerLOD(SourceModels, StaticMeshReduction))
|
|
{
|
|
return false;
|
|
}
|
|
|
|
bool bWasReduced = false;
|
|
if (!Builder.ReduceLODs(SourceModels, LODGroup, StaticMeshReduction, bWasReduced))
|
|
{
|
|
return false;
|
|
}
|
|
|
|
return Builder.GenerateRenderingMeshes(*this, OutRenderData, SourceModels, ImportVersion);
|
|
}
|
|
|
|
bool FMeshUtilities::GenerateStaticMeshLODs(TArray<FStaticMeshSourceModel>& Models, const FStaticMeshLODGroup& LODGroup)
|
|
{
|
|
FStaticMeshUtilityBuilder Builder;
|
|
if (!Builder.GatherSourceMeshesPerLOD(Models, StaticMeshReduction))
|
|
{
|
|
return false;
|
|
}
|
|
|
|
bool bWasReduced = false;
|
|
if (!Builder.ReduceLODs(Models, LODGroup, StaticMeshReduction, bWasReduced))
|
|
{
|
|
return false;
|
|
}
|
|
|
|
if (bWasReduced)
|
|
{
|
|
return Builder.ReplaceRawMeshModels(Models);
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
class IMeshBuildData
|
|
{
|
|
public:
|
|
virtual uint32 GetWedgeIndex(uint32 FaceIndex, uint32 TriIndex) = 0;
|
|
virtual uint32 GetVertexIndex(uint32 WedgeIndex) = 0;
|
|
virtual uint32 GetVertexIndex(uint32 FaceIndex, uint32 TriIndex) = 0;
|
|
virtual FVector GetVertexPosition(uint32 WedgeIndex) = 0;
|
|
virtual FVector GetVertexPosition(uint32 FaceIndex, uint32 TriIndex) = 0;
|
|
virtual FVector2D GetVertexUV(uint32 FaceIndex, uint32 TriIndex, uint32 UVIndex) = 0;
|
|
virtual uint32 GetFaceSmoothingGroups(uint32 FaceIndex) = 0;
|
|
|
|
virtual uint32 GetNumFaces() = 0;
|
|
virtual uint32 GetNumWedges() = 0;
|
|
|
|
virtual TArray<FVector>& GetTangentArray(uint32 Axis) = 0;
|
|
virtual void ValidateTangentArraySize() = 0;
|
|
|
|
virtual SMikkTSpaceInterface* GetMikkTInterface() = 0;
|
|
virtual void* GetMikkTUserData() = 0;
|
|
|
|
const IMeshUtilities::MeshBuildOptions& BuildOptions;
|
|
TArray<FText>* OutWarningMessages;
|
|
TArray<FName>* OutWarningNames;
|
|
bool bTooManyVerts;
|
|
|
|
protected:
|
|
IMeshBuildData(
|
|
const IMeshUtilities::MeshBuildOptions& InBuildOptions,
|
|
TArray<FText>* InWarningMessages,
|
|
TArray<FName>* InWarningNames)
|
|
: BuildOptions(InBuildOptions)
|
|
, OutWarningMessages(InWarningMessages)
|
|
, OutWarningNames(InWarningNames)
|
|
, bTooManyVerts(false)
|
|
{
|
|
}
|
|
};
|
|
|
|
class SkeletalMeshBuildData : public IMeshBuildData
|
|
{
|
|
public:
|
|
SkeletalMeshBuildData(
|
|
FStaticLODModel& InLODModel,
|
|
const FReferenceSkeleton& InRefSkeleton,
|
|
const TArray<FVertInfluence>& InInfluences,
|
|
const TArray<FMeshWedge>& InWedges,
|
|
const TArray<FMeshFace>& InFaces,
|
|
const TArray<FVector>& InPoints,
|
|
const TArray<int32>& InPointToOriginalMap,
|
|
const IMeshUtilities::MeshBuildOptions& InBuildOptions,
|
|
TArray<FText>* InWarningMessages,
|
|
TArray<FName>* InWarningNames)
|
|
: IMeshBuildData(InBuildOptions, InWarningMessages, InWarningNames)
|
|
, MikkTUserData(InWedges, InFaces, InPoints, InBuildOptions.bComputeNormals, TangentX, TangentY, TangentZ)
|
|
, LODModel(InLODModel)
|
|
, RefSkeleton(InRefSkeleton)
|
|
, Influences(InInfluences)
|
|
, Wedges(InWedges)
|
|
, Faces(InFaces)
|
|
, Points(InPoints)
|
|
, PointToOriginalMap(InPointToOriginalMap)
|
|
{
|
|
MikkTInterface.m_getNormal = MikkGetNormal_Skeletal;
|
|
MikkTInterface.m_getNumFaces = MikkGetNumFaces_Skeletal;
|
|
MikkTInterface.m_getNumVerticesOfFace = MikkGetNumVertsOfFace_Skeletal;
|
|
MikkTInterface.m_getPosition = MikkGetPosition_Skeletal;
|
|
MikkTInterface.m_getTexCoord = MikkGetTexCoord_Skeletal;
|
|
MikkTInterface.m_setTSpaceBasic = MikkSetTSpaceBasic_Skeletal;
|
|
MikkTInterface.m_setTSpace = nullptr;
|
|
}
|
|
|
|
virtual uint32 GetWedgeIndex(uint32 FaceIndex, uint32 TriIndex) override
|
|
{
|
|
return Faces[FaceIndex].iWedge[TriIndex];
|
|
}
|
|
|
|
virtual uint32 GetVertexIndex(uint32 WedgeIndex) override
|
|
{
|
|
return Wedges[WedgeIndex].iVertex;
|
|
}
|
|
|
|
virtual uint32 GetVertexIndex(uint32 FaceIndex, uint32 TriIndex) override
|
|
{
|
|
return Wedges[Faces[FaceIndex].iWedge[TriIndex]].iVertex;
|
|
}
|
|
|
|
virtual FVector GetVertexPosition(uint32 WedgeIndex) override
|
|
{
|
|
return Points[Wedges[WedgeIndex].iVertex];
|
|
}
|
|
|
|
virtual FVector GetVertexPosition(uint32 FaceIndex, uint32 TriIndex) override
|
|
{
|
|
return Points[Wedges[Faces[FaceIndex].iWedge[TriIndex]].iVertex];
|
|
}
|
|
|
|
virtual FVector2D GetVertexUV(uint32 FaceIndex, uint32 TriIndex, uint32 UVIndex) override
|
|
{
|
|
return Wedges[Faces[FaceIndex].iWedge[TriIndex]].UVs[UVIndex];
|
|
}
|
|
|
|
virtual uint32 GetFaceSmoothingGroups(uint32 FaceIndex)
|
|
{
|
|
return Faces[FaceIndex].SmoothingGroups;
|
|
}
|
|
|
|
virtual uint32 GetNumFaces() override
|
|
{
|
|
return Faces.Num();
|
|
}
|
|
|
|
virtual uint32 GetNumWedges() override
|
|
{
|
|
return Wedges.Num();
|
|
}
|
|
|
|
virtual TArray<FVector>& GetTangentArray(uint32 Axis) override
|
|
{
|
|
if (Axis == 0)
|
|
{
|
|
return TangentX;
|
|
}
|
|
else if (Axis == 1)
|
|
{
|
|
return TangentY;
|
|
}
|
|
|
|
return TangentZ;
|
|
}
|
|
|
|
virtual void ValidateTangentArraySize() override
|
|
{
|
|
check(TangentX.Num() == Wedges.Num());
|
|
check(TangentY.Num() == Wedges.Num());
|
|
check(TangentZ.Num() == Wedges.Num());
|
|
}
|
|
|
|
virtual SMikkTSpaceInterface* GetMikkTInterface() override
|
|
{
|
|
return &MikkTInterface;
|
|
}
|
|
|
|
virtual void* GetMikkTUserData() override
|
|
{
|
|
return (void*)&MikkTUserData;
|
|
}
|
|
|
|
TArray<FVector> TangentX;
|
|
TArray<FVector> TangentY;
|
|
TArray<FVector> TangentZ;
|
|
TArray<FSkinnedMeshChunk*> Chunks;
|
|
|
|
SMikkTSpaceInterface MikkTInterface;
|
|
MikkTSpace_Skeletal_Mesh MikkTUserData;
|
|
|
|
FStaticLODModel& LODModel;
|
|
const FReferenceSkeleton& RefSkeleton;
|
|
const TArray<FVertInfluence>& Influences;
|
|
const TArray<FMeshWedge>& Wedges;
|
|
const TArray<FMeshFace>& Faces;
|
|
const TArray<FVector>& Points;
|
|
const TArray<int32>& PointToOriginalMap;
|
|
};
|
|
|
|
class FSkeletalMeshUtilityBuilder
|
|
{
|
|
public:
|
|
FSkeletalMeshUtilityBuilder()
|
|
: Stage(EStage::Uninit)
|
|
{
|
|
}
|
|
|
|
public:
|
|
void Skeletal_FindOverlappingCorners(
|
|
TMultiMap<int32, int32>& OutOverlappingCorners,
|
|
IMeshBuildData* BuildData,
|
|
float ComparisonThreshold
|
|
)
|
|
{
|
|
int32 NumFaces = BuildData->GetNumFaces();
|
|
int32 NumWedges = BuildData->GetNumWedges();
|
|
check(NumFaces * 3 <= NumWedges);
|
|
|
|
// Create a list of vertex Z/index pairs
|
|
TArray<FIndexAndZ> VertIndexAndZ;
|
|
VertIndexAndZ.Empty(NumWedges);
|
|
for (int32 FaceIndex = 0; FaceIndex < NumFaces; FaceIndex++)
|
|
{
|
|
for (int32 TriIndex = 0; TriIndex < 3; ++TriIndex)
|
|
{
|
|
uint32 Index = BuildData->GetWedgeIndex(FaceIndex, TriIndex);
|
|
new(VertIndexAndZ)FIndexAndZ(Index, BuildData->GetVertexPosition(Index));
|
|
}
|
|
}
|
|
|
|
// Sort the vertices by z value
|
|
VertIndexAndZ.Sort(FCompareIndexAndZ());
|
|
|
|
// Search for duplicates, quickly!
|
|
for (int32 i = 0; i < VertIndexAndZ.Num(); i++)
|
|
{
|
|
// only need to search forward, since we add pairs both ways
|
|
for (int32 j = i + 1; j < VertIndexAndZ.Num(); j++)
|
|
{
|
|
if (FMath::Abs(VertIndexAndZ[j].Z - VertIndexAndZ[i].Z) > ComparisonThreshold)
|
|
break; // can't be any more dups
|
|
|
|
FVector PositionA = BuildData->GetVertexPosition(VertIndexAndZ[i].Index);
|
|
FVector PositionB = BuildData->GetVertexPosition(VertIndexAndZ[j].Index);
|
|
|
|
if (PointsEqual(PositionA, PositionB, ComparisonThreshold))
|
|
{
|
|
OutOverlappingCorners.Add(VertIndexAndZ[i].Index, VertIndexAndZ[j].Index);
|
|
OutOverlappingCorners.Add(VertIndexAndZ[j].Index, VertIndexAndZ[i].Index);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
void Skeletal_ComputeTriangleTangents(
|
|
TArray<FVector>& TriangleTangentX,
|
|
TArray<FVector>& TriangleTangentY,
|
|
TArray<FVector>& TriangleTangentZ,
|
|
IMeshBuildData* BuildData,
|
|
float ComparisonThreshold
|
|
)
|
|
{
|
|
int32 NumTriangles = BuildData->GetNumFaces();
|
|
TriangleTangentX.Empty(NumTriangles);
|
|
TriangleTangentY.Empty(NumTriangles);
|
|
TriangleTangentZ.Empty(NumTriangles);
|
|
|
|
for (int32 TriangleIndex = 0; TriangleIndex < NumTriangles; TriangleIndex++)
|
|
{
|
|
const int32 UVIndex = 0;
|
|
FVector P[3];
|
|
|
|
for (int32 i = 0; i < 3; ++i)
|
|
{
|
|
P[i] = BuildData->GetVertexPosition(TriangleIndex, i);
|
|
}
|
|
|
|
const FVector Normal = ((P[1] - P[2]) ^ (P[0] - P[2])).GetSafeNormal(ComparisonThreshold);
|
|
FMatrix ParameterToLocal(
|
|
FPlane(P[1].X - P[0].X, P[1].Y - P[0].Y, P[1].Z - P[0].Z, 0),
|
|
FPlane(P[2].X - P[0].X, P[2].Y - P[0].Y, P[2].Z - P[0].Z, 0),
|
|
FPlane(P[0].X, P[0].Y, P[0].Z, 0),
|
|
FPlane(0, 0, 0, 1)
|
|
);
|
|
|
|
FVector2D T1 = BuildData->GetVertexUV(TriangleIndex, 0, UVIndex);
|
|
FVector2D T2 = BuildData->GetVertexUV(TriangleIndex, 1, UVIndex);
|
|
FVector2D T3 = BuildData->GetVertexUV(TriangleIndex, 2, UVIndex);
|
|
FMatrix ParameterToTexture(
|
|
FPlane(T2.X - T1.X, T2.Y - T1.Y, 0, 0),
|
|
FPlane(T3.X - T1.X, T3.Y - T1.Y, 0, 0),
|
|
FPlane(T1.X, T1.Y, 1, 0),
|
|
FPlane(0, 0, 0, 1)
|
|
);
|
|
|
|
// Use InverseSlow to catch singular matrices. Inverse can miss this sometimes.
|
|
const FMatrix TextureToLocal = ParameterToTexture.Inverse() * ParameterToLocal;
|
|
|
|
TriangleTangentX.Add(TextureToLocal.TransformVector(FVector(1, 0, 0)).GetSafeNormal());
|
|
TriangleTangentY.Add(TextureToLocal.TransformVector(FVector(0, 1, 0)).GetSafeNormal());
|
|
TriangleTangentZ.Add(Normal);
|
|
|
|
FVector::CreateOrthonormalBasis(
|
|
TriangleTangentX[TriangleIndex],
|
|
TriangleTangentY[TriangleIndex],
|
|
TriangleTangentZ[TriangleIndex]
|
|
);
|
|
}
|
|
}
|
|
|
|
void Skeletal_ComputeTangents(
|
|
IMeshBuildData* BuildData,
|
|
TMultiMap<int32, int32> const& OverlappingCorners
|
|
)
|
|
{
|
|
bool bBlendOverlappingNormals = true;
|
|
bool bIgnoreDegenerateTriangles = BuildData->BuildOptions.bRemoveDegenerateTriangles;
|
|
float ComparisonThreshold = bIgnoreDegenerateTriangles ? THRESH_POINTS_ARE_SAME : 0.0f;
|
|
|
|
// Compute per-triangle tangents.
|
|
TArray<FVector> TriangleTangentX;
|
|
TArray<FVector> TriangleTangentY;
|
|
TArray<FVector> TriangleTangentZ;
|
|
|
|
Skeletal_ComputeTriangleTangents(
|
|
TriangleTangentX,
|
|
TriangleTangentY,
|
|
TriangleTangentZ,
|
|
BuildData,
|
|
bIgnoreDegenerateTriangles ? SMALL_NUMBER : 0.0f
|
|
);
|
|
|
|
TArray<FVector>& WedgeTangentX = BuildData->GetTangentArray(0);
|
|
TArray<FVector>& WedgeTangentY = BuildData->GetTangentArray(1);
|
|
TArray<FVector>& WedgeTangentZ = BuildData->GetTangentArray(2);
|
|
|
|
// Declare these out here to avoid reallocations.
|
|
TArray<FFanFace> RelevantFacesForCorner[3];
|
|
TArray<int32> AdjacentFaces;
|
|
TArray<int32> DupVerts;
|
|
|
|
int32 NumFaces = BuildData->GetNumFaces();
|
|
int32 NumWedges = BuildData->GetNumWedges();
|
|
check(NumFaces * 3 <= NumWedges);
|
|
|
|
// Allocate storage for tangents if none were provided.
|
|
if (WedgeTangentX.Num() != NumWedges)
|
|
{
|
|
WedgeTangentX.Empty(NumWedges);
|
|
WedgeTangentX.AddZeroed(NumWedges);
|
|
}
|
|
if (WedgeTangentY.Num() != NumWedges)
|
|
{
|
|
WedgeTangentY.Empty(NumWedges);
|
|
WedgeTangentY.AddZeroed(NumWedges);
|
|
}
|
|
if (WedgeTangentZ.Num() != NumWedges)
|
|
{
|
|
WedgeTangentZ.Empty(NumWedges);
|
|
WedgeTangentZ.AddZeroed(NumWedges);
|
|
}
|
|
|
|
for (int32 FaceIndex = 0; FaceIndex < NumFaces; FaceIndex++)
|
|
{
|
|
int32 WedgeOffset = FaceIndex * 3;
|
|
FVector CornerPositions[3];
|
|
FVector CornerTangentX[3];
|
|
FVector CornerTangentY[3];
|
|
FVector CornerTangentZ[3];
|
|
|
|
for (int32 CornerIndex = 0; CornerIndex < 3; CornerIndex++)
|
|
{
|
|
CornerTangentX[CornerIndex] = FVector::ZeroVector;
|
|
CornerTangentY[CornerIndex] = FVector::ZeroVector;
|
|
CornerTangentZ[CornerIndex] = FVector::ZeroVector;
|
|
CornerPositions[CornerIndex] = BuildData->GetVertexPosition(FaceIndex, CornerIndex);
|
|
RelevantFacesForCorner[CornerIndex].Reset();
|
|
}
|
|
|
|
// Don't process degenerate triangles.
|
|
if (PointsEqual(CornerPositions[0], CornerPositions[1], ComparisonThreshold)
|
|
|| PointsEqual(CornerPositions[0], CornerPositions[2], ComparisonThreshold)
|
|
|| PointsEqual(CornerPositions[1], CornerPositions[2], ComparisonThreshold))
|
|
{
|
|
continue;
|
|
}
|
|
|
|
// No need to process triangles if tangents already exist.
|
|
bool bCornerHasTangents[3] = { 0 };
|
|
for (int32 CornerIndex = 0; CornerIndex < 3; CornerIndex++)
|
|
{
|
|
bCornerHasTangents[CornerIndex] = !WedgeTangentX[WedgeOffset + CornerIndex].IsZero()
|
|
&& !WedgeTangentY[WedgeOffset + CornerIndex].IsZero()
|
|
&& !WedgeTangentZ[WedgeOffset + CornerIndex].IsZero();
|
|
}
|
|
if (bCornerHasTangents[0] && bCornerHasTangents[1] && bCornerHasTangents[2])
|
|
{
|
|
continue;
|
|
}
|
|
|
|
// Calculate smooth vertex normals.
|
|
float Determinant = FVector::Triple(
|
|
TriangleTangentX[FaceIndex],
|
|
TriangleTangentY[FaceIndex],
|
|
TriangleTangentZ[FaceIndex]
|
|
);
|
|
|
|
// Start building a list of faces adjacent to this face.
|
|
AdjacentFaces.Reset();
|
|
for (int32 CornerIndex = 0; CornerIndex < 3; CornerIndex++)
|
|
{
|
|
int32 ThisCornerIndex = WedgeOffset + CornerIndex;
|
|
DupVerts.Reset();
|
|
OverlappingCorners.MultiFind(ThisCornerIndex, DupVerts);
|
|
DupVerts.Add(ThisCornerIndex); // I am a "dup" of myself
|
|
for (int32 k = 0; k < DupVerts.Num(); k++)
|
|
{
|
|
AdjacentFaces.AddUnique(DupVerts[k] / 3);
|
|
}
|
|
}
|
|
|
|
// We need to sort these here because the criteria for point equality is
|
|
// exact, so we must ensure the exact same order for all dups.
|
|
AdjacentFaces.Sort();
|
|
|
|
// Process adjacent faces
|
|
for (int32 AdjacentFaceIndex = 0; AdjacentFaceIndex < AdjacentFaces.Num(); AdjacentFaceIndex++)
|
|
{
|
|
int32 OtherFaceIndex = AdjacentFaces[AdjacentFaceIndex];
|
|
for (int32 OurCornerIndex = 0; OurCornerIndex < 3; OurCornerIndex++)
|
|
{
|
|
if (bCornerHasTangents[OurCornerIndex])
|
|
continue;
|
|
|
|
FFanFace NewFanFace;
|
|
int32 CommonIndexCount = 0;
|
|
|
|
// Check for vertices in common.
|
|
if (FaceIndex == OtherFaceIndex)
|
|
{
|
|
CommonIndexCount = 3;
|
|
NewFanFace.LinkedVertexIndex = OurCornerIndex;
|
|
}
|
|
else
|
|
{
|
|
// Check matching vertices against main vertex .
|
|
for (int32 OtherCornerIndex = 0; OtherCornerIndex < 3; OtherCornerIndex++)
|
|
{
|
|
if (PointsEqual(
|
|
CornerPositions[OurCornerIndex],
|
|
BuildData->GetVertexPosition(OtherFaceIndex, OtherCornerIndex),
|
|
ComparisonThreshold
|
|
))
|
|
{
|
|
CommonIndexCount++;
|
|
NewFanFace.LinkedVertexIndex = OtherCornerIndex;
|
|
}
|
|
}
|
|
}
|
|
|
|
// Add if connected by at least one point. Smoothing matches are considered later.
|
|
if (CommonIndexCount > 0)
|
|
{
|
|
NewFanFace.FaceIndex = OtherFaceIndex;
|
|
NewFanFace.bFilled = (OtherFaceIndex == FaceIndex); // Starter face for smoothing floodfill.
|
|
NewFanFace.bBlendTangents = NewFanFace.bFilled;
|
|
NewFanFace.bBlendNormals = NewFanFace.bFilled;
|
|
RelevantFacesForCorner[OurCornerIndex].Add(NewFanFace);
|
|
}
|
|
}
|
|
}
|
|
|
|
// Find true relevance of faces for a vertex normal by traversing
|
|
// smoothing-group-compatible connected triangle fans around common vertices.
|
|
for (int32 CornerIndex = 0; CornerIndex < 3; CornerIndex++)
|
|
{
|
|
if (bCornerHasTangents[CornerIndex])
|
|
continue;
|
|
|
|
int32 NewConnections;
|
|
do
|
|
{
|
|
NewConnections = 0;
|
|
for (int32 OtherFaceIdx = 0; OtherFaceIdx < RelevantFacesForCorner[CornerIndex].Num(); OtherFaceIdx++)
|
|
{
|
|
FFanFace& OtherFace = RelevantFacesForCorner[CornerIndex][OtherFaceIdx];
|
|
// The vertex' own face is initially the only face with bFilled == true.
|
|
if (OtherFace.bFilled)
|
|
{
|
|
for (int32 NextFaceIndex = 0; NextFaceIndex < RelevantFacesForCorner[CornerIndex].Num(); NextFaceIndex++)
|
|
{
|
|
FFanFace& NextFace = RelevantFacesForCorner[CornerIndex][NextFaceIndex];
|
|
if (!NextFace.bFilled) // && !NextFace.bBlendTangents)
|
|
{
|
|
if (NextFaceIndex != OtherFaceIdx)
|
|
//&& (RawMesh.FaceSmoothingMasks[NextFace.FaceIndex] & RawMesh.FaceSmoothingMasks[OtherFace.FaceIndex]))
|
|
{
|
|
int32 CommonVertices = 0;
|
|
int32 CommonTangentVertices = 0;
|
|
int32 CommonNormalVertices = 0;
|
|
for (int32 OtherCornerIndex = 0; OtherCornerIndex < 3; OtherCornerIndex++)
|
|
{
|
|
for (int32 NextCornerIndex = 0; NextCornerIndex < 3; NextCornerIndex++)
|
|
{
|
|
int32 NextVertexIndex = BuildData->GetVertexIndex(NextFace.FaceIndex, NextCornerIndex);
|
|
int32 OtherVertexIndex = BuildData->GetVertexIndex(OtherFace.FaceIndex, OtherCornerIndex);
|
|
if (PointsEqual(
|
|
BuildData->GetVertexPosition(NextFace.FaceIndex, NextCornerIndex),
|
|
BuildData->GetVertexPosition(OtherFace.FaceIndex, OtherCornerIndex),
|
|
ComparisonThreshold))
|
|
{
|
|
CommonVertices++;
|
|
|
|
|
|
if (UVsEqual(
|
|
BuildData->GetVertexUV(NextFace.FaceIndex, NextCornerIndex, 0),
|
|
BuildData->GetVertexUV(OtherFace.FaceIndex, OtherCornerIndex, 0)))
|
|
{
|
|
CommonTangentVertices++;
|
|
}
|
|
if (bBlendOverlappingNormals
|
|
|| NextVertexIndex == OtherVertexIndex)
|
|
{
|
|
CommonNormalVertices++;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
// Flood fill faces with more than one common vertices which must be touching edges.
|
|
if (CommonVertices > 1)
|
|
{
|
|
NextFace.bFilled = true;
|
|
NextFace.bBlendNormals = (CommonNormalVertices > 1);
|
|
NewConnections++;
|
|
|
|
// Only blend tangents if there is no UV seam along the edge with this face.
|
|
if (OtherFace.bBlendTangents && CommonTangentVertices > 1)
|
|
{
|
|
float OtherDeterminant = FVector::Triple(
|
|
TriangleTangentX[NextFace.FaceIndex],
|
|
TriangleTangentY[NextFace.FaceIndex],
|
|
TriangleTangentZ[NextFace.FaceIndex]
|
|
);
|
|
if ((Determinant * OtherDeterminant) > 0.0f)
|
|
{
|
|
NextFace.bBlendTangents = true;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
while (NewConnections > 0);
|
|
}
|
|
|
|
// Vertex normal construction.
|
|
for (int32 CornerIndex = 0; CornerIndex < 3; CornerIndex++)
|
|
{
|
|
if (bCornerHasTangents[CornerIndex])
|
|
{
|
|
CornerTangentX[CornerIndex] = WedgeTangentX[WedgeOffset + CornerIndex];
|
|
CornerTangentY[CornerIndex] = WedgeTangentY[WedgeOffset + CornerIndex];
|
|
CornerTangentZ[CornerIndex] = WedgeTangentZ[WedgeOffset + CornerIndex];
|
|
}
|
|
else
|
|
{
|
|
for (int32 RelevantFaceIdx = 0; RelevantFaceIdx < RelevantFacesForCorner[CornerIndex].Num(); RelevantFaceIdx++)
|
|
{
|
|
FFanFace const& RelevantFace = RelevantFacesForCorner[CornerIndex][RelevantFaceIdx];
|
|
if (RelevantFace.bFilled)
|
|
{
|
|
int32 OtherFaceIndex = RelevantFace.FaceIndex;
|
|
if (RelevantFace.bBlendTangents)
|
|
{
|
|
CornerTangentX[CornerIndex] += TriangleTangentX[OtherFaceIndex];
|
|
CornerTangentY[CornerIndex] += TriangleTangentY[OtherFaceIndex];
|
|
}
|
|
if (RelevantFace.bBlendNormals)
|
|
{
|
|
CornerTangentZ[CornerIndex] += TriangleTangentZ[OtherFaceIndex];
|
|
}
|
|
}
|
|
}
|
|
if (!WedgeTangentX[WedgeOffset + CornerIndex].IsZero())
|
|
{
|
|
CornerTangentX[CornerIndex] = WedgeTangentX[WedgeOffset + CornerIndex];
|
|
}
|
|
if (!WedgeTangentY[WedgeOffset + CornerIndex].IsZero())
|
|
{
|
|
CornerTangentY[CornerIndex] = WedgeTangentY[WedgeOffset + CornerIndex];
|
|
}
|
|
if (!WedgeTangentZ[WedgeOffset + CornerIndex].IsZero())
|
|
{
|
|
CornerTangentZ[CornerIndex] = WedgeTangentZ[WedgeOffset + CornerIndex];
|
|
}
|
|
}
|
|
}
|
|
|
|
// Normalization.
|
|
for (int32 CornerIndex = 0; CornerIndex < 3; CornerIndex++)
|
|
{
|
|
CornerTangentX[CornerIndex].Normalize();
|
|
CornerTangentY[CornerIndex].Normalize();
|
|
CornerTangentZ[CornerIndex].Normalize();
|
|
|
|
// Gram-Schmidt orthogonalization
|
|
CornerTangentY[CornerIndex] -= CornerTangentX[CornerIndex] * (CornerTangentX[CornerIndex] | CornerTangentY[CornerIndex]);
|
|
CornerTangentY[CornerIndex].Normalize();
|
|
|
|
CornerTangentX[CornerIndex] -= CornerTangentZ[CornerIndex] * (CornerTangentZ[CornerIndex] | CornerTangentX[CornerIndex]);
|
|
CornerTangentX[CornerIndex].Normalize();
|
|
CornerTangentY[CornerIndex] -= CornerTangentZ[CornerIndex] * (CornerTangentZ[CornerIndex] | CornerTangentY[CornerIndex]);
|
|
CornerTangentY[CornerIndex].Normalize();
|
|
}
|
|
|
|
// Copy back to the mesh.
|
|
for (int32 CornerIndex = 0; CornerIndex < 3; CornerIndex++)
|
|
{
|
|
WedgeTangentX[WedgeOffset + CornerIndex] = CornerTangentX[CornerIndex];
|
|
WedgeTangentY[WedgeOffset + CornerIndex] = CornerTangentY[CornerIndex];
|
|
WedgeTangentZ[WedgeOffset + CornerIndex] = CornerTangentZ[CornerIndex];
|
|
}
|
|
}
|
|
|
|
check(WedgeTangentX.Num() == NumWedges);
|
|
check(WedgeTangentY.Num() == NumWedges);
|
|
check(WedgeTangentZ.Num() == NumWedges);
|
|
}
|
|
|
|
void Skeletal_ComputeTangents_MikkTSpace(
|
|
IMeshBuildData* BuildData,
|
|
TMultiMap<int32, int32> const& OverlappingCorners
|
|
)
|
|
{
|
|
bool bBlendOverlappingNormals = true;
|
|
bool bIgnoreDegenerateTriangles = BuildData->BuildOptions.bRemoveDegenerateTriangles;
|
|
float ComparisonThreshold = bIgnoreDegenerateTriangles ? THRESH_POINTS_ARE_SAME : 0.0f;
|
|
|
|
// Compute per-triangle tangents.
|
|
TArray<FVector> TriangleTangentX;
|
|
TArray<FVector> TriangleTangentY;
|
|
TArray<FVector> TriangleTangentZ;
|
|
|
|
Skeletal_ComputeTriangleTangents(
|
|
TriangleTangentX,
|
|
TriangleTangentY,
|
|
TriangleTangentZ,
|
|
BuildData,
|
|
bIgnoreDegenerateTriangles ? SMALL_NUMBER : 0.0f
|
|
);
|
|
|
|
TArray<FVector>& WedgeTangentX = BuildData->GetTangentArray(0);
|
|
TArray<FVector>& WedgeTangentY = BuildData->GetTangentArray(1);
|
|
TArray<FVector>& WedgeTangentZ = BuildData->GetTangentArray(2);
|
|
|
|
// Declare these out here to avoid reallocations.
|
|
TArray<FFanFace> RelevantFacesForCorner[3];
|
|
TArray<int32> AdjacentFaces;
|
|
TArray<int32> DupVerts;
|
|
|
|
int32 NumFaces = BuildData->GetNumFaces();
|
|
int32 NumWedges = BuildData->GetNumWedges();
|
|
check(NumFaces * 3 == NumWedges);
|
|
|
|
bool bWedgeNormals = true;
|
|
bool bWedgeTSpace = false;
|
|
for (int32 WedgeIdx = 0; WedgeIdx < WedgeTangentZ.Num(); ++WedgeIdx)
|
|
{
|
|
for (int32 VertexIndex = 0; VertexIndex < 3; VertexIndex++)
|
|
bWedgeNormals = bWedgeNormals && (!WedgeTangentZ[WedgeIdx].IsNearlyZero());
|
|
}
|
|
|
|
if (WedgeTangentX.Num() > 0 && WedgeTangentY.Num() > 0)
|
|
{
|
|
bWedgeTSpace = true;
|
|
for (int32 WedgeIdx = 0; WedgeIdx < WedgeTangentX.Num()
|
|
&& WedgeIdx < WedgeTangentY.Num(); ++WedgeIdx)
|
|
{
|
|
bWedgeTSpace = bWedgeTSpace && (!WedgeTangentX[WedgeIdx].IsNearlyZero()) && (!WedgeTangentY[WedgeIdx].IsNearlyZero());
|
|
}
|
|
}
|
|
|
|
// Allocate storage for tangents if none were provided, and calculate normals for MikkTSpace.
|
|
if (WedgeTangentZ.Num() != NumWedges || !bWedgeNormals)
|
|
{
|
|
// normals are not included, so we should calculate them
|
|
WedgeTangentZ.Empty(NumWedges);
|
|
WedgeTangentZ.AddZeroed(NumWedges);
|
|
// we need to calculate normals for MikkTSpace
|
|
|
|
for (int32 FaceIndex = 0; FaceIndex < NumFaces; FaceIndex++)
|
|
{
|
|
int32 WedgeOffset = FaceIndex * 3;
|
|
FVector CornerPositions[3];
|
|
FVector CornerNormal[3];
|
|
|
|
for (int32 CornerIndex = 0; CornerIndex < 3; CornerIndex++)
|
|
{
|
|
CornerNormal[CornerIndex] = FVector::ZeroVector;
|
|
CornerPositions[CornerIndex] = BuildData->GetVertexPosition(FaceIndex, CornerIndex);
|
|
RelevantFacesForCorner[CornerIndex].Reset();
|
|
}
|
|
|
|
// Don't process degenerate triangles.
|
|
if (PointsEqual(CornerPositions[0], CornerPositions[1], ComparisonThreshold)
|
|
|| PointsEqual(CornerPositions[0], CornerPositions[2], ComparisonThreshold)
|
|
|| PointsEqual(CornerPositions[1], CornerPositions[2], ComparisonThreshold))
|
|
{
|
|
continue;
|
|
}
|
|
|
|
// No need to process triangles if tangents already exist.
|
|
bool bCornerHasNormal[3] = { 0 };
|
|
for (int32 CornerIndex = 0; CornerIndex < 3; CornerIndex++)
|
|
{
|
|
bCornerHasNormal[CornerIndex] = !WedgeTangentZ[WedgeOffset + CornerIndex].IsZero();
|
|
}
|
|
if (bCornerHasNormal[0] && bCornerHasNormal[1] && bCornerHasNormal[2])
|
|
{
|
|
continue;
|
|
}
|
|
|
|
// Start building a list of faces adjacent to this face.
|
|
AdjacentFaces.Reset();
|
|
for (int32 CornerIndex = 0; CornerIndex < 3; CornerIndex++)
|
|
{
|
|
int32 ThisCornerIndex = WedgeOffset + CornerIndex;
|
|
DupVerts.Reset();
|
|
OverlappingCorners.MultiFind(ThisCornerIndex, DupVerts);
|
|
DupVerts.Add(ThisCornerIndex); // I am a "dup" of myself
|
|
for (int32 k = 0; k < DupVerts.Num(); k++)
|
|
{
|
|
AdjacentFaces.AddUnique(DupVerts[k] / 3);
|
|
}
|
|
}
|
|
|
|
// We need to sort these here because the criteria for point equality is
|
|
// exact, so we must ensure the exact same order for all dups.
|
|
AdjacentFaces.Sort();
|
|
|
|
// Process adjacent faces
|
|
for (int32 AdjacentFaceIndex = 0; AdjacentFaceIndex < AdjacentFaces.Num(); AdjacentFaceIndex++)
|
|
{
|
|
int32 OtherFaceIndex = AdjacentFaces[AdjacentFaceIndex];
|
|
for (int32 OurCornerIndex = 0; OurCornerIndex < 3; OurCornerIndex++)
|
|
{
|
|
if (bCornerHasNormal[OurCornerIndex])
|
|
continue;
|
|
|
|
FFanFace NewFanFace;
|
|
int32 CommonIndexCount = 0;
|
|
|
|
// Check for vertices in common.
|
|
if (FaceIndex == OtherFaceIndex)
|
|
{
|
|
CommonIndexCount = 3;
|
|
NewFanFace.LinkedVertexIndex = OurCornerIndex;
|
|
}
|
|
else
|
|
{
|
|
// Check matching vertices against main vertex .
|
|
for (int32 OtherCornerIndex = 0; OtherCornerIndex < 3; OtherCornerIndex++)
|
|
{
|
|
if (PointsEqual(
|
|
CornerPositions[OurCornerIndex],
|
|
BuildData->GetVertexPosition(OtherFaceIndex, OtherCornerIndex),
|
|
ComparisonThreshold
|
|
))
|
|
{
|
|
CommonIndexCount++;
|
|
NewFanFace.LinkedVertexIndex = OtherCornerIndex;
|
|
}
|
|
}
|
|
}
|
|
|
|
// Add if connected by at least one point. Smoothing matches are considered later.
|
|
if (CommonIndexCount > 0)
|
|
{
|
|
NewFanFace.FaceIndex = OtherFaceIndex;
|
|
NewFanFace.bFilled = (OtherFaceIndex == FaceIndex); // Starter face for smoothing floodfill.
|
|
NewFanFace.bBlendTangents = NewFanFace.bFilled;
|
|
NewFanFace.bBlendNormals = NewFanFace.bFilled;
|
|
RelevantFacesForCorner[OurCornerIndex].Add(NewFanFace);
|
|
}
|
|
}
|
|
}
|
|
|
|
// Find true relevance of faces for a vertex normal by traversing
|
|
// smoothing-group-compatible connected triangle fans around common vertices.
|
|
for (int32 CornerIndex = 0; CornerIndex < 3; CornerIndex++)
|
|
{
|
|
if (bCornerHasNormal[CornerIndex])
|
|
continue;
|
|
|
|
int32 NewConnections;
|
|
do
|
|
{
|
|
NewConnections = 0;
|
|
for (int32 OtherFaceIdx = 0; OtherFaceIdx < RelevantFacesForCorner[CornerIndex].Num(); OtherFaceIdx++)
|
|
{
|
|
FFanFace& OtherFace = RelevantFacesForCorner[CornerIndex][OtherFaceIdx];
|
|
// The vertex' own face is initially the only face with bFilled == true.
|
|
if (OtherFace.bFilled)
|
|
{
|
|
for (int32 NextFaceIndex = 0; NextFaceIndex < RelevantFacesForCorner[CornerIndex].Num(); NextFaceIndex++)
|
|
{
|
|
FFanFace& NextFace = RelevantFacesForCorner[CornerIndex][NextFaceIndex];
|
|
if (!NextFace.bFilled) // && !NextFace.bBlendTangents)
|
|
{
|
|
if ((NextFaceIndex != OtherFaceIdx)
|
|
&& (BuildData->GetFaceSmoothingGroups(NextFace.FaceIndex) & BuildData->GetFaceSmoothingGroups(OtherFace.FaceIndex)))
|
|
{
|
|
int32 CommonVertices = 0;
|
|
int32 CommonNormalVertices = 0;
|
|
for (int32 OtherCornerIndex = 0; OtherCornerIndex < 3; OtherCornerIndex++)
|
|
{
|
|
for (int32 NextCornerIndex = 0; NextCornerIndex < 3; NextCornerIndex++)
|
|
{
|
|
int32 NextVertexIndex = BuildData->GetVertexIndex(NextFace.FaceIndex, NextCornerIndex);
|
|
int32 OtherVertexIndex = BuildData->GetVertexIndex(OtherFace.FaceIndex, OtherCornerIndex);
|
|
if (PointsEqual(
|
|
BuildData->GetVertexPosition(NextFace.FaceIndex, NextCornerIndex),
|
|
BuildData->GetVertexPosition(OtherFace.FaceIndex, OtherCornerIndex),
|
|
ComparisonThreshold))
|
|
{
|
|
CommonVertices++;
|
|
if (bBlendOverlappingNormals
|
|
|| NextVertexIndex == OtherVertexIndex)
|
|
{
|
|
CommonNormalVertices++;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
// Flood fill faces with more than one common vertices which must be touching edges.
|
|
if (CommonVertices > 1)
|
|
{
|
|
NextFace.bFilled = true;
|
|
NextFace.bBlendNormals = (CommonNormalVertices > 1);
|
|
NewConnections++;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
while (NewConnections > 0);
|
|
}
|
|
|
|
// Vertex normal construction.
|
|
for (int32 CornerIndex = 0; CornerIndex < 3; CornerIndex++)
|
|
{
|
|
if (bCornerHasNormal[CornerIndex])
|
|
{
|
|
CornerNormal[CornerIndex] = WedgeTangentZ[WedgeOffset + CornerIndex];
|
|
}
|
|
else
|
|
{
|
|
for (int32 RelevantFaceIdx = 0; RelevantFaceIdx < RelevantFacesForCorner[CornerIndex].Num(); RelevantFaceIdx++)
|
|
{
|
|
FFanFace const& RelevantFace = RelevantFacesForCorner[CornerIndex][RelevantFaceIdx];
|
|
if (RelevantFace.bFilled)
|
|
{
|
|
int32 OtherFaceIndex = RelevantFace.FaceIndex;
|
|
if (RelevantFace.bBlendNormals)
|
|
{
|
|
CornerNormal[CornerIndex] += TriangleTangentZ[OtherFaceIndex];
|
|
}
|
|
}
|
|
}
|
|
if (!WedgeTangentZ[WedgeOffset + CornerIndex].IsZero())
|
|
{
|
|
CornerNormal[CornerIndex] = WedgeTangentZ[WedgeOffset + CornerIndex];
|
|
}
|
|
}
|
|
}
|
|
|
|
// Normalization.
|
|
for (int32 CornerIndex = 0; CornerIndex < 3; CornerIndex++)
|
|
{
|
|
CornerNormal[CornerIndex].Normalize();
|
|
}
|
|
|
|
// Copy back to the mesh.
|
|
for (int32 CornerIndex = 0; CornerIndex < 3; CornerIndex++)
|
|
{
|
|
WedgeTangentZ[WedgeOffset + CornerIndex] = CornerNormal[CornerIndex];
|
|
}
|
|
}
|
|
}
|
|
|
|
if (WedgeTangentX.Num() != NumWedges)
|
|
{
|
|
WedgeTangentX.Empty(NumWedges);
|
|
WedgeTangentX.AddZeroed(NumWedges);
|
|
}
|
|
if (WedgeTangentY.Num() != NumWedges)
|
|
{
|
|
WedgeTangentY.Empty(NumWedges);
|
|
WedgeTangentY.AddZeroed(NumWedges);
|
|
}
|
|
|
|
//if (!bWedgeTSpace)
|
|
{
|
|
// we can use mikktspace to calculate the tangents
|
|
SMikkTSpaceContext MikkTContext;
|
|
MikkTContext.m_pInterface = BuildData->GetMikkTInterface();
|
|
MikkTContext.m_pUserData = BuildData->GetMikkTUserData();
|
|
//MikkTContext.m_bIgnoreDegenerates = bIgnoreDegenerateTriangles;
|
|
|
|
genTangSpaceDefault(&MikkTContext);
|
|
}
|
|
|
|
check(WedgeTangentX.Num() == NumWedges);
|
|
check(WedgeTangentY.Num() == NumWedges);
|
|
check(WedgeTangentZ.Num() == NumWedges);
|
|
}
|
|
|
|
bool PrepareSourceMesh(IMeshBuildData* BuildData)
|
|
{
|
|
check(Stage == EStage::Uninit);
|
|
|
|
BeginSlowTask();
|
|
|
|
TMultiMap<int32, int32>& OverlappingCorners = *new(LODOverlappingCorners)TMultiMap<int32, int32>;
|
|
|
|
float ComparisonThreshold = THRESH_POINTS_ARE_SAME;//GetComparisonThreshold(LODBuildSettings[LODIndex]);
|
|
int32 NumWedges = BuildData->GetNumWedges();
|
|
|
|
// Find overlapping corners to accelerate adjacency.
|
|
Skeletal_FindOverlappingCorners(OverlappingCorners, BuildData, ComparisonThreshold);
|
|
|
|
// Figure out if we should recompute normals and tangents.
|
|
bool bRecomputeNormals = BuildData->BuildOptions.bComputeNormals;
|
|
bool bRecomputeTangents = BuildData->BuildOptions.bComputeTangents;
|
|
|
|
// Dump normals and tangents if we are recomputing them.
|
|
if (bRecomputeTangents)
|
|
{
|
|
TArray<FVector>& TangentX = BuildData->GetTangentArray(0);
|
|
TArray<FVector>& TangentY = BuildData->GetTangentArray(1);
|
|
|
|
TangentX.Empty(NumWedges);
|
|
TangentX.AddZeroed(NumWedges);
|
|
TangentY.Empty(NumWedges);
|
|
TangentY.AddZeroed(NumWedges);
|
|
}
|
|
if (bRecomputeNormals)
|
|
{
|
|
TArray<FVector>& TangentZ = BuildData->GetTangentArray(2);
|
|
TangentZ.Empty(NumWedges);
|
|
TangentZ.AddZeroed(NumWedges);
|
|
}
|
|
|
|
// Compute any missing tangents. MikkTSpace should be use only when the user want to recompute the normals or tangents otherwise should always fallback on builtin
|
|
if (BuildData->BuildOptions.bUseMikkTSpace && (BuildData->BuildOptions.bComputeNormals || BuildData->BuildOptions.bComputeTangents))
|
|
{
|
|
Skeletal_ComputeTangents_MikkTSpace(BuildData, OverlappingCorners);
|
|
}
|
|
else
|
|
{
|
|
Skeletal_ComputeTangents(BuildData, OverlappingCorners);
|
|
}
|
|
|
|
// At this point the mesh will have valid tangents.
|
|
BuildData->ValidateTangentArraySize();
|
|
check(LODOverlappingCorners.Num() == 1);
|
|
|
|
EndSlowTask();
|
|
|
|
Stage = EStage::Prepared;
|
|
return true;
|
|
}
|
|
|
|
bool GenerateSkeletalRenderMesh(IMeshBuildData* InBuildData)
|
|
{
|
|
check(Stage == EStage::Prepared);
|
|
|
|
SkeletalMeshBuildData& BuildData = *(SkeletalMeshBuildData*)InBuildData;
|
|
|
|
BeginSlowTask();
|
|
|
|
// Find wedge influences.
|
|
TArray<int32> WedgeInfluenceIndices;
|
|
TMap<uint32, uint32> VertexIndexToInfluenceIndexMap;
|
|
|
|
for (uint32 LookIdx = 0; LookIdx < (uint32)BuildData.Influences.Num(); LookIdx++)
|
|
{
|
|
// Order matters do not allow the map to overwrite an existing value.
|
|
if (!VertexIndexToInfluenceIndexMap.Find(BuildData.Influences[LookIdx].VertIndex))
|
|
{
|
|
VertexIndexToInfluenceIndexMap.Add(BuildData.Influences[LookIdx].VertIndex, LookIdx);
|
|
}
|
|
}
|
|
|
|
for (int32 WedgeIndex = 0; WedgeIndex < BuildData.Wedges.Num(); WedgeIndex++)
|
|
{
|
|
uint32* InfluenceIndex = VertexIndexToInfluenceIndexMap.Find(BuildData.Wedges[WedgeIndex].iVertex);
|
|
|
|
if (InfluenceIndex)
|
|
{
|
|
WedgeInfluenceIndices.Add(*InfluenceIndex);
|
|
}
|
|
else
|
|
{
|
|
// we have missing influence vert, we weight to root
|
|
WedgeInfluenceIndices.Add(0);
|
|
|
|
// add warning message
|
|
if (BuildData.OutWarningMessages)
|
|
{
|
|
BuildData.OutWarningMessages->Add(FText::Format(FText::FromString("Missing influence on vert {0}. Weighting it to root."), FText::FromString(FString::FromInt(BuildData.Wedges[WedgeIndex].iVertex))));
|
|
if (BuildData.OutWarningNames)
|
|
{
|
|
BuildData.OutWarningNames->Add(FFbxErrors::SkeletalMesh_VertMissingInfluences);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
check(BuildData.Wedges.Num() == WedgeInfluenceIndices.Num());
|
|
|
|
TArray<FSkeletalMeshVertIndexAndZ> VertIndexAndZ;
|
|
TArray<FSoftSkinBuildVertex> RawVertices;
|
|
|
|
VertIndexAndZ.Empty(BuildData.Points.Num());
|
|
RawVertices.Reserve(BuildData.Points.Num());
|
|
|
|
for (int32 FaceIndex = 0; FaceIndex < BuildData.Faces.Num(); FaceIndex++)
|
|
{
|
|
// Only update the status progress bar if we are in the game thread and every thousand faces.
|
|
// Updating status is extremely slow
|
|
if (FaceIndex % 5000 == 0)
|
|
{
|
|
UpdateSlowTask(FaceIndex, BuildData.Faces.Num());
|
|
}
|
|
|
|
const FMeshFace& Face = BuildData.Faces[FaceIndex];
|
|
|
|
for (int32 VertexIndex = 0; VertexIndex < 3; VertexIndex++)
|
|
{
|
|
FSoftSkinBuildVertex Vertex;
|
|
const uint32 WedgeIndex = BuildData.GetWedgeIndex(FaceIndex, VertexIndex);
|
|
const FMeshWedge& Wedge = BuildData.Wedges[WedgeIndex];
|
|
|
|
Vertex.Position = BuildData.GetVertexPosition(FaceIndex, VertexIndex);
|
|
|
|
FVector TangentX, TangentY, TangentZ;
|
|
TangentX = BuildData.TangentX[WedgeIndex].GetSafeNormal();
|
|
TangentY = BuildData.TangentY[WedgeIndex].GetSafeNormal();
|
|
TangentZ = BuildData.TangentZ[WedgeIndex].GetSafeNormal();
|
|
|
|
/*if (BuildData.BuildOptions.bComputeNormals || BuildData.BuildOptions.bComputeTangents)
|
|
{
|
|
TangentX = BuildData.TangentX[VertexIndex].GetSafeNormal();
|
|
TangentY = BuildData.TangentY[VertexIndex].GetSafeNormal();
|
|
|
|
if( BuildData.BuildOptions.bComputeNormals )
|
|
{
|
|
TangentZ = BuildData.TangentZ[VertexIndex].GetSafeNormal();
|
|
}
|
|
else
|
|
{
|
|
//TangentZ = Face.TangentZ[VertexIndex];
|
|
}
|
|
|
|
TangentY -= TangentX * (TangentX | TangentY);
|
|
TangentY.Normalize();
|
|
|
|
TangentX -= TangentZ * (TangentZ | TangentX);
|
|
TangentY -= TangentZ * (TangentZ | TangentY);
|
|
|
|
TangentX.Normalize();
|
|
TangentY.Normalize();
|
|
}
|
|
else*/
|
|
{
|
|
//TangentX = Face.TangentX[VertexIndex];
|
|
//TangentY = Face.TangentY[VertexIndex];
|
|
//TangentZ = Face.TangentZ[VertexIndex];
|
|
|
|
// Normalize overridden tangents. Its possible for them to import un-normalized.
|
|
TangentX.Normalize();
|
|
TangentY.Normalize();
|
|
TangentZ.Normalize();
|
|
}
|
|
|
|
Vertex.TangentX = TangentX;
|
|
Vertex.TangentY = TangentY;
|
|
Vertex.TangentZ = TangentZ;
|
|
|
|
FMemory::Memcpy(Vertex.UVs, Wedge.UVs, sizeof(FVector2D)*MAX_TEXCOORDS);
|
|
Vertex.Color = Wedge.Color;
|
|
|
|
{
|
|
// Count the influences.
|
|
int32 InfIdx = WedgeInfluenceIndices[Face.iWedge[VertexIndex]];
|
|
int32 LookIdx = InfIdx;
|
|
|
|
uint32 InfluenceCount = 0;
|
|
while (BuildData.Influences.IsValidIndex(LookIdx) && (BuildData.Influences[LookIdx].VertIndex == Wedge.iVertex))
|
|
{
|
|
InfluenceCount++;
|
|
LookIdx++;
|
|
}
|
|
InfluenceCount = FMath::Min<uint32>(InfluenceCount, MAX_TOTAL_INFLUENCES);
|
|
|
|
// Setup the vertex influences.
|
|
Vertex.InfluenceBones[0] = 0;
|
|
Vertex.InfluenceWeights[0] = 255;
|
|
for (uint32 i = 1; i < MAX_TOTAL_INFLUENCES; i++)
|
|
{
|
|
Vertex.InfluenceBones[i] = 0;
|
|
Vertex.InfluenceWeights[i] = 0;
|
|
}
|
|
|
|
uint32 TotalInfluenceWeight = 0;
|
|
for (uint32 i = 0; i < InfluenceCount; i++)
|
|
{
|
|
FBoneIndexType BoneIndex = (FBoneIndexType)BuildData.Influences[InfIdx + i].BoneIndex;
|
|
if (BoneIndex >= BuildData.RefSkeleton.GetRawBoneNum())
|
|
continue;
|
|
|
|
Vertex.InfluenceBones[i] = BoneIndex;
|
|
Vertex.InfluenceWeights[i] = (uint8)(BuildData.Influences[InfIdx + i].Weight * 255.0f);
|
|
TotalInfluenceWeight += Vertex.InfluenceWeights[i];
|
|
}
|
|
Vertex.InfluenceWeights[0] += 255 - TotalInfluenceWeight;
|
|
}
|
|
|
|
// Add the vertex as well as its original index in the points array
|
|
Vertex.PointWedgeIdx = Wedge.iVertex;
|
|
|
|
int32 RawIndex = RawVertices.Add(Vertex);
|
|
|
|
// Add an efficient way to find dupes of this vertex later for fast combining of vertices
|
|
FSkeletalMeshVertIndexAndZ IAndZ;
|
|
IAndZ.Index = RawIndex;
|
|
IAndZ.Z = Vertex.Position.Z;
|
|
|
|
VertIndexAndZ.Add(IAndZ);
|
|
}
|
|
}
|
|
|
|
// Generate chunks and their vertices and indices
|
|
SkeletalMeshTools::BuildSkeletalMeshChunks(BuildData.Faces, RawVertices, VertIndexAndZ, BuildData.BuildOptions.bKeepOverlappingVertices, BuildData.Chunks, BuildData.bTooManyVerts);
|
|
|
|
// Chunk vertices to satisfy the requested limit.
|
|
const uint32 MaxGPUSkinBones = FGPUBaseSkinVertexFactory::GetMaxGPUSkinBones();
|
|
check(MaxGPUSkinBones <= FGPUBaseSkinVertexFactory::GHardwareMaxGPUSkinBones);
|
|
SkeletalMeshTools::ChunkSkinnedVertices(BuildData.Chunks, MaxGPUSkinBones);
|
|
|
|
EndSlowTask();
|
|
|
|
Stage = EStage::GenerateRendering;
|
|
return true;
|
|
}
|
|
|
|
void BeginSlowTask()
|
|
{
|
|
if (IsInGameThread())
|
|
{
|
|
GWarn->BeginSlowTask(NSLOCTEXT("UnrealEd", "ProcessingSkeletalTriangles", "Processing Mesh Triangles"), true);
|
|
}
|
|
}
|
|
|
|
void UpdateSlowTask(int32 Numerator, int32 Denominator)
|
|
{
|
|
if (IsInGameThread())
|
|
{
|
|
GWarn->StatusUpdate(Numerator, Denominator, NSLOCTEXT("UnrealEd", "ProcessingSkeletalTriangles", "Processing Mesh Triangles"));
|
|
}
|
|
}
|
|
|
|
void EndSlowTask()
|
|
{
|
|
if (IsInGameThread())
|
|
{
|
|
GWarn->EndSlowTask();
|
|
}
|
|
}
|
|
|
|
private:
|
|
enum class EStage
|
|
{
|
|
Uninit,
|
|
Prepared,
|
|
GenerateRendering,
|
|
};
|
|
|
|
TIndirectArray<TMultiMap<int32, int32> > LODOverlappingCorners;
|
|
EStage Stage;
|
|
};
|
|
|
|
bool FMeshUtilities::BuildSkeletalMesh(FStaticLODModel& LODModel, const FReferenceSkeleton& RefSkeleton, const TArray<FVertInfluence>& Influences, const TArray<FMeshWedge>& Wedges, const TArray<FMeshFace>& Faces, const TArray<FVector>& Points, const TArray<int32>& PointToOriginalMap, const MeshBuildOptions& BuildOptions, TArray<FText> * OutWarningMessages, TArray<FName> * OutWarningNames)
|
|
{
|
|
#if WITH_EDITORONLY_DATA
|
|
// Temporarily supporting both import paths
|
|
if (!BuildOptions.bUseMikkTSpace)
|
|
{
|
|
return BuildSkeletalMesh_Legacy(LODModel, RefSkeleton, Influences, Wedges, Faces, Points, PointToOriginalMap, BuildOptions.bKeepOverlappingVertices, BuildOptions.bComputeNormals, BuildOptions.bComputeTangents, OutWarningMessages, OutWarningNames);
|
|
}
|
|
|
|
SkeletalMeshBuildData BuildData(
|
|
LODModel,
|
|
RefSkeleton,
|
|
Influences,
|
|
Wedges,
|
|
Faces,
|
|
Points,
|
|
PointToOriginalMap,
|
|
BuildOptions,
|
|
OutWarningMessages,
|
|
OutWarningNames);
|
|
|
|
FSkeletalMeshUtilityBuilder Builder;
|
|
if (!Builder.PrepareSourceMesh(&BuildData))
|
|
{
|
|
return false;
|
|
}
|
|
|
|
if (!Builder.GenerateSkeletalRenderMesh(&BuildData))
|
|
{
|
|
return false;
|
|
}
|
|
|
|
// Build the skeletal model from chunks.
|
|
Builder.BeginSlowTask();
|
|
BuildSkeletalModelFromChunks(BuildData.LODModel, BuildData.RefSkeleton, BuildData.Chunks, BuildData.PointToOriginalMap);
|
|
Builder.EndSlowTask();
|
|
|
|
// Only show these warnings if in the game thread. When importing morph targets, this function can run in another thread and these warnings dont prevent the mesh from importing
|
|
if (IsInGameThread())
|
|
{
|
|
bool bHasBadSections = false;
|
|
for (int32 SectionIndex = 0; SectionIndex < BuildData.LODModel.Sections.Num(); SectionIndex++)
|
|
{
|
|
FSkelMeshSection& Section = BuildData.LODModel.Sections[SectionIndex];
|
|
bHasBadSections |= (Section.NumTriangles == 0);
|
|
|
|
// Log info about the section.
|
|
UE_LOG(LogSkeletalMesh, Log, TEXT("Section %u: Material=%u, %u triangles"),
|
|
SectionIndex,
|
|
Section.MaterialIndex,
|
|
Section.NumTriangles
|
|
);
|
|
}
|
|
if (bHasBadSections)
|
|
{
|
|
FText BadSectionMessage(NSLOCTEXT("UnrealEd", "Error_SkeletalMeshHasBadSections", "Input mesh has a section with no triangles. This mesh may not render properly."));
|
|
if (BuildData.OutWarningMessages)
|
|
{
|
|
BuildData.OutWarningMessages->Add(BadSectionMessage);
|
|
if (BuildData.OutWarningNames)
|
|
{
|
|
BuildData.OutWarningNames->Add(FFbxErrors::SkeletalMesh_SectionWithNoTriangle);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
FMessageDialog::Open(EAppMsgType::Ok, BadSectionMessage);
|
|
}
|
|
}
|
|
|
|
if (BuildData.bTooManyVerts)
|
|
{
|
|
FText TooManyVertsMessage(NSLOCTEXT("UnrealEd", "Error_SkeletalMeshTooManyVertices", "Input mesh has too many vertices. The generated mesh will be corrupt! Consider adding extra materials to split up the source mesh into smaller chunks."));
|
|
|
|
if (BuildData.OutWarningMessages)
|
|
{
|
|
BuildData.OutWarningMessages->Add(TooManyVertsMessage);
|
|
if (BuildData.OutWarningNames)
|
|
{
|
|
BuildData.OutWarningNames->Add(FFbxErrors::SkeletalMesh_TooManyVertices);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
FMessageDialog::Open(EAppMsgType::Ok, TooManyVertsMessage);
|
|
}
|
|
}
|
|
}
|
|
|
|
return true;
|
|
#else
|
|
if (OutWarningMessages)
|
|
{
|
|
OutWarningMessages->Add(FText::FromString(TEXT("Cannot call FMeshUtilities::BuildSkeletalMesh on a console!")));
|
|
}
|
|
else
|
|
{
|
|
UE_LOG(LogSkeletalMesh, Fatal, TEXT("Cannot call FMeshUtilities::BuildSkeletalMesh on a console!"));
|
|
}
|
|
return false;
|
|
#endif
|
|
}
|
|
|
|
//@TODO: The OutMessages has to be a struct that contains FText/FName, or make it Token and add that as error. Needs re-work. Temporary workaround for now.
|
|
bool FMeshUtilities::BuildSkeletalMesh_Legacy(FStaticLODModel& LODModel, const FReferenceSkeleton& RefSkeleton, const TArray<FVertInfluence>& Influences, const TArray<FMeshWedge>& Wedges, const TArray<FMeshFace>& Faces, const TArray<FVector>& Points, const TArray<int32>& PointToOriginalMap, bool bKeepOverlappingVertices, bool bComputeNormals, bool bComputeTangents, TArray<FText> * OutWarningMessages, TArray<FName> * OutWarningNames)
|
|
{
|
|
bool bTooManyVerts = false;
|
|
|
|
check(PointToOriginalMap.Num() == Points.Num());
|
|
|
|
// Calculate face tangent vectors.
|
|
TArray<FVector> FaceTangentX;
|
|
TArray<FVector> FaceTangentY;
|
|
FaceTangentX.AddUninitialized(Faces.Num());
|
|
FaceTangentY.AddUninitialized(Faces.Num());
|
|
|
|
if (bComputeNormals || bComputeTangents)
|
|
{
|
|
for (int32 FaceIndex = 0; FaceIndex < Faces.Num(); FaceIndex++)
|
|
{
|
|
FVector P1 = Points[Wedges[Faces[FaceIndex].iWedge[0]].iVertex],
|
|
P2 = Points[Wedges[Faces[FaceIndex].iWedge[1]].iVertex],
|
|
P3 = Points[Wedges[Faces[FaceIndex].iWedge[2]].iVertex];
|
|
FVector TriangleNormal = FPlane(P3, P2, P1);
|
|
FMatrix ParameterToLocal(
|
|
FPlane(P2.X - P1.X, P2.Y - P1.Y, P2.Z - P1.Z, 0),
|
|
FPlane(P3.X - P1.X, P3.Y - P1.Y, P3.Z - P1.Z, 0),
|
|
FPlane(P1.X, P1.Y, P1.Z, 0),
|
|
FPlane(0, 0, 0, 1)
|
|
);
|
|
|
|
float U1 = Wedges[Faces[FaceIndex].iWedge[0]].UVs[0].X,
|
|
U2 = Wedges[Faces[FaceIndex].iWedge[1]].UVs[0].X,
|
|
U3 = Wedges[Faces[FaceIndex].iWedge[2]].UVs[0].X,
|
|
V1 = Wedges[Faces[FaceIndex].iWedge[0]].UVs[0].Y,
|
|
V2 = Wedges[Faces[FaceIndex].iWedge[1]].UVs[0].Y,
|
|
V3 = Wedges[Faces[FaceIndex].iWedge[2]].UVs[0].Y;
|
|
|
|
FMatrix ParameterToTexture(
|
|
FPlane(U2 - U1, V2 - V1, 0, 0),
|
|
FPlane(U3 - U1, V3 - V1, 0, 0),
|
|
FPlane(U1, V1, 1, 0),
|
|
FPlane(0, 0, 0, 1)
|
|
);
|
|
|
|
FMatrix TextureToLocal = ParameterToTexture.Inverse() * ParameterToLocal;
|
|
FVector TangentX = TextureToLocal.TransformVector(FVector(1, 0, 0)).GetSafeNormal(),
|
|
TangentY = TextureToLocal.TransformVector(FVector(0, 1, 0)).GetSafeNormal(),
|
|
TangentZ;
|
|
|
|
TangentX = TangentX - TriangleNormal * (TangentX | TriangleNormal);
|
|
TangentY = TangentY - TriangleNormal * (TangentY | TriangleNormal);
|
|
|
|
FaceTangentX[FaceIndex] = TangentX.GetSafeNormal();
|
|
FaceTangentY[FaceIndex] = TangentY.GetSafeNormal();
|
|
}
|
|
}
|
|
|
|
TArray<int32> WedgeInfluenceIndices;
|
|
|
|
// Find wedge influences.
|
|
TMap<uint32, uint32> VertexIndexToInfluenceIndexMap;
|
|
|
|
for (uint32 LookIdx = 0; LookIdx < (uint32)Influences.Num(); LookIdx++)
|
|
{
|
|
// Order matters do not allow the map to overwrite an existing value.
|
|
if (!VertexIndexToInfluenceIndexMap.Find(Influences[LookIdx].VertIndex))
|
|
{
|
|
VertexIndexToInfluenceIndexMap.Add(Influences[LookIdx].VertIndex, LookIdx);
|
|
}
|
|
}
|
|
|
|
for (int32 WedgeIndex = 0; WedgeIndex < Wedges.Num(); WedgeIndex++)
|
|
{
|
|
uint32* InfluenceIndex = VertexIndexToInfluenceIndexMap.Find(Wedges[WedgeIndex].iVertex);
|
|
|
|
if (InfluenceIndex)
|
|
{
|
|
WedgeInfluenceIndices.Add(*InfluenceIndex);
|
|
}
|
|
else
|
|
{
|
|
// we have missing influence vert, we weight to root
|
|
WedgeInfluenceIndices.Add(0);
|
|
|
|
// add warning message
|
|
if (OutWarningMessages)
|
|
{
|
|
OutWarningMessages->Add(FText::Format(FText::FromString("Missing influence on vert {0}. Weighting it to root."), FText::FromString(FString::FromInt(Wedges[WedgeIndex].iVertex))));
|
|
if (OutWarningNames)
|
|
{
|
|
OutWarningNames->Add(FFbxErrors::SkeletalMesh_VertMissingInfluences);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
check(Wedges.Num() == WedgeInfluenceIndices.Num());
|
|
|
|
// Calculate smooth wedge tangent vectors.
|
|
|
|
if (IsInGameThread())
|
|
{
|
|
// Only update status if in the game thread. When importing morph targets, this function can run in another thread
|
|
GWarn->BeginSlowTask(NSLOCTEXT("UnrealEd", "ProcessingSkeletalTriangles", "Processing Mesh Triangles"), true);
|
|
}
|
|
|
|
|
|
// To accelerate generation of adjacency, we'll create a table that maps each vertex index
|
|
// to its overlapping vertices, and a table that maps a vertex to the its influenced faces
|
|
TMultiMap<int32, int32> Vert2Duplicates;
|
|
TMultiMap<int32, int32> Vert2Faces;
|
|
TArray<FSkeletalMeshVertIndexAndZ> VertIndexAndZ;
|
|
{
|
|
// Create a list of vertex Z/index pairs
|
|
VertIndexAndZ.Empty(Points.Num());
|
|
for (int32 i = 0; i < Points.Num(); i++)
|
|
{
|
|
FSkeletalMeshVertIndexAndZ iandz;
|
|
iandz.Index = i;
|
|
iandz.Z = Points[i].Z;
|
|
VertIndexAndZ.Add(iandz);
|
|
}
|
|
|
|
// Sorting function for vertex Z/index pairs
|
|
struct FCompareFSkeletalMeshVertIndexAndZ
|
|
{
|
|
FORCEINLINE bool operator()(const FSkeletalMeshVertIndexAndZ& A, const FSkeletalMeshVertIndexAndZ& B) const
|
|
{
|
|
return A.Z < B.Z;
|
|
}
|
|
};
|
|
|
|
// Sort the vertices by z value
|
|
VertIndexAndZ.Sort(FCompareFSkeletalMeshVertIndexAndZ());
|
|
|
|
// Search for duplicates, quickly!
|
|
for (int32 i = 0; i < VertIndexAndZ.Num(); i++)
|
|
{
|
|
// only need to search forward, since we add pairs both ways
|
|
for (int32 j = i + 1; j < VertIndexAndZ.Num(); j++)
|
|
{
|
|
if (FMath::Abs(VertIndexAndZ[j].Z - VertIndexAndZ[i].Z) > THRESH_POINTS_ARE_SAME)
|
|
{
|
|
// our list is sorted, so there can't be any more dupes
|
|
break;
|
|
}
|
|
|
|
// check to see if the points are really overlapping
|
|
if (PointsEqual(
|
|
Points[VertIndexAndZ[i].Index],
|
|
Points[VertIndexAndZ[j].Index]))
|
|
{
|
|
Vert2Duplicates.Add(VertIndexAndZ[i].Index, VertIndexAndZ[j].Index);
|
|
Vert2Duplicates.Add(VertIndexAndZ[j].Index, VertIndexAndZ[i].Index);
|
|
}
|
|
}
|
|
}
|
|
|
|
// we are done with this
|
|
VertIndexAndZ.Reset();
|
|
|
|
// now create a map from vert indices to faces
|
|
for (int32 FaceIndex = 0; FaceIndex < Faces.Num(); FaceIndex++)
|
|
{
|
|
const FMeshFace& Face = Faces[FaceIndex];
|
|
for (int32 VertexIndex = 0; VertexIndex < 3; VertexIndex++)
|
|
{
|
|
Vert2Faces.AddUnique(Wedges[Face.iWedge[VertexIndex]].iVertex, FaceIndex);
|
|
}
|
|
}
|
|
}
|
|
|
|
TArray<FSkinnedMeshChunk*> Chunks;
|
|
TArray<int32> AdjacentFaces;
|
|
TArray<int32> DupVerts;
|
|
TArray<int32> DupFaces;
|
|
|
|
// List of raw calculated vertices that will be merged later
|
|
TArray<FSoftSkinBuildVertex> RawVertices;
|
|
RawVertices.Reserve(Points.Num());
|
|
|
|
// Create a list of vertex Z/index pairs
|
|
|
|
for (int32 FaceIndex = 0; FaceIndex < Faces.Num(); FaceIndex++)
|
|
{
|
|
// Only update the status progress bar if we are in the gamethread and every thousand faces.
|
|
// Updating status is extremely slow
|
|
if (FaceIndex % 5000 == 0 && IsInGameThread())
|
|
{
|
|
// Only update status if in the game thread. When importing morph targets, this function can run in another thread
|
|
GWarn->StatusUpdate(FaceIndex, Faces.Num(), NSLOCTEXT("UnrealEd", "ProcessingSkeletalTriangles", "Processing Mesh Triangles"));
|
|
}
|
|
|
|
const FMeshFace& Face = Faces[FaceIndex];
|
|
|
|
FVector VertexTangentX[3],
|
|
VertexTangentY[3],
|
|
VertexTangentZ[3];
|
|
|
|
if (bComputeNormals || bComputeTangents)
|
|
{
|
|
for (int32 VertexIndex = 0; VertexIndex < 3; VertexIndex++)
|
|
{
|
|
VertexTangentX[VertexIndex] = FVector::ZeroVector;
|
|
VertexTangentY[VertexIndex] = FVector::ZeroVector;
|
|
VertexTangentZ[VertexIndex] = FVector::ZeroVector;
|
|
}
|
|
|
|
FVector TriangleNormal = FPlane(
|
|
Points[Wedges[Face.iWedge[2]].iVertex],
|
|
Points[Wedges[Face.iWedge[1]].iVertex],
|
|
Points[Wedges[Face.iWedge[0]].iVertex]
|
|
);
|
|
float Determinant = FVector::Triple(FaceTangentX[FaceIndex], FaceTangentY[FaceIndex], TriangleNormal);
|
|
|
|
// Start building a list of faces adjacent to this triangle
|
|
AdjacentFaces.Reset();
|
|
for (int32 VertexIndex = 0; VertexIndex < 3; VertexIndex++)
|
|
{
|
|
int32 vert = Wedges[Face.iWedge[VertexIndex]].iVertex;
|
|
DupVerts.Reset();
|
|
Vert2Duplicates.MultiFind(vert, DupVerts);
|
|
DupVerts.Add(vert); // I am a "dupe" of myself
|
|
for (int32 k = 0; k < DupVerts.Num(); k++)
|
|
{
|
|
DupFaces.Reset();
|
|
Vert2Faces.MultiFind(DupVerts[k], DupFaces);
|
|
for (int32 l = 0; l < DupFaces.Num(); l++)
|
|
{
|
|
AdjacentFaces.AddUnique(DupFaces[l]);
|
|
}
|
|
}
|
|
}
|
|
|
|
// Process adjacent faces
|
|
for (int32 AdjacentFaceIndex = 0; AdjacentFaceIndex < AdjacentFaces.Num(); AdjacentFaceIndex++)
|
|
{
|
|
int32 OtherFaceIndex = AdjacentFaces[AdjacentFaceIndex];
|
|
const FMeshFace& OtherFace = Faces[OtherFaceIndex];
|
|
FVector OtherTriangleNormal = FPlane(
|
|
Points[Wedges[OtherFace.iWedge[2]].iVertex],
|
|
Points[Wedges[OtherFace.iWedge[1]].iVertex],
|
|
Points[Wedges[OtherFace.iWedge[0]].iVertex]
|
|
);
|
|
float OtherFaceDeterminant = FVector::Triple(FaceTangentX[OtherFaceIndex], FaceTangentY[OtherFaceIndex], OtherTriangleNormal);
|
|
|
|
for (int32 VertexIndex = 0; VertexIndex < 3; VertexIndex++)
|
|
{
|
|
for (int32 OtherVertexIndex = 0; OtherVertexIndex < 3; OtherVertexIndex++)
|
|
{
|
|
if (PointsEqual(
|
|
Points[Wedges[OtherFace.iWedge[OtherVertexIndex]].iVertex],
|
|
Points[Wedges[Face.iWedge[VertexIndex]].iVertex]
|
|
))
|
|
{
|
|
if (Determinant * OtherFaceDeterminant > 0.0f && SkeletalMeshTools::SkeletalMesh_UVsEqual(Wedges[OtherFace.iWedge[OtherVertexIndex]], Wedges[Face.iWedge[VertexIndex]]))
|
|
{
|
|
VertexTangentX[VertexIndex] += FaceTangentX[OtherFaceIndex];
|
|
VertexTangentY[VertexIndex] += FaceTangentY[OtherFaceIndex];
|
|
}
|
|
|
|
// Only contribute 'normal' if the vertices are truly one and the same to obey hard "smoothing" edges baked into
|
|
// the mesh by vertex duplication
|
|
if (Wedges[OtherFace.iWedge[OtherVertexIndex]].iVertex == Wedges[Face.iWedge[VertexIndex]].iVertex)
|
|
{
|
|
VertexTangentZ[VertexIndex] += OtherTriangleNormal;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
for (int32 VertexIndex = 0; VertexIndex < 3; VertexIndex++)
|
|
{
|
|
FSoftSkinBuildVertex Vertex;
|
|
|
|
Vertex.Position = Points[Wedges[Face.iWedge[VertexIndex]].iVertex];
|
|
|
|
FVector TangentX, TangentY, TangentZ;
|
|
|
|
if (bComputeNormals || bComputeTangents)
|
|
{
|
|
TangentX = VertexTangentX[VertexIndex].GetSafeNormal();
|
|
TangentY = VertexTangentY[VertexIndex].GetSafeNormal();
|
|
|
|
if (bComputeNormals)
|
|
{
|
|
TangentZ = VertexTangentZ[VertexIndex].GetSafeNormal();
|
|
}
|
|
else
|
|
{
|
|
TangentZ = Face.TangentZ[VertexIndex];
|
|
}
|
|
|
|
TangentY -= TangentX * (TangentX | TangentY);
|
|
TangentY.Normalize();
|
|
|
|
TangentX -= TangentZ * (TangentZ | TangentX);
|
|
TangentY -= TangentZ * (TangentZ | TangentY);
|
|
|
|
TangentX.Normalize();
|
|
TangentY.Normalize();
|
|
}
|
|
else
|
|
{
|
|
TangentX = Face.TangentX[VertexIndex];
|
|
TangentY = Face.TangentY[VertexIndex];
|
|
TangentZ = Face.TangentZ[VertexIndex];
|
|
|
|
// Normalize overridden tangents. Its possible for them to import un-normalized.
|
|
TangentX.Normalize();
|
|
TangentY.Normalize();
|
|
TangentZ.Normalize();
|
|
}
|
|
|
|
Vertex.TangentX = TangentX;
|
|
Vertex.TangentY = TangentY;
|
|
Vertex.TangentZ = TangentZ;
|
|
|
|
FMemory::Memcpy(Vertex.UVs, Wedges[Face.iWedge[VertexIndex]].UVs, sizeof(FVector2D)*MAX_TEXCOORDS);
|
|
Vertex.Color = Wedges[Face.iWedge[VertexIndex]].Color;
|
|
|
|
{
|
|
// Count the influences.
|
|
|
|
int32 InfIdx = WedgeInfluenceIndices[Face.iWedge[VertexIndex]];
|
|
int32 LookIdx = InfIdx;
|
|
|
|
uint32 InfluenceCount = 0;
|
|
while (Influences.IsValidIndex(LookIdx) && (Influences[LookIdx].VertIndex == Wedges[Face.iWedge[VertexIndex]].iVertex))
|
|
{
|
|
InfluenceCount++;
|
|
LookIdx++;
|
|
}
|
|
InfluenceCount = FMath::Min<uint32>(InfluenceCount, MAX_TOTAL_INFLUENCES);
|
|
|
|
// Setup the vertex influences.
|
|
|
|
Vertex.InfluenceBones[0] = 0;
|
|
Vertex.InfluenceWeights[0] = 255;
|
|
for (uint32 i = 1; i < MAX_TOTAL_INFLUENCES; i++)
|
|
{
|
|
Vertex.InfluenceBones[i] = 0;
|
|
Vertex.InfluenceWeights[i] = 0;
|
|
}
|
|
|
|
uint32 TotalInfluenceWeight = 0;
|
|
for (uint32 i = 0; i < InfluenceCount; i++)
|
|
{
|
|
FBoneIndexType BoneIndex = (FBoneIndexType)Influences[InfIdx + i].BoneIndex;
|
|
if (BoneIndex >= RefSkeleton.GetRawBoneNum())
|
|
continue;
|
|
|
|
Vertex.InfluenceBones[i] = BoneIndex;
|
|
Vertex.InfluenceWeights[i] = (uint8)(Influences[InfIdx + i].Weight * 255.0f);
|
|
TotalInfluenceWeight += Vertex.InfluenceWeights[i];
|
|
}
|
|
Vertex.InfluenceWeights[0] += 255 - TotalInfluenceWeight;
|
|
}
|
|
|
|
// Add the vertex as well as its original index in the points array
|
|
Vertex.PointWedgeIdx = Wedges[Face.iWedge[VertexIndex]].iVertex;
|
|
|
|
int32 RawIndex = RawVertices.Add(Vertex);
|
|
|
|
// Add an efficient way to find dupes of this vertex later for fast combining of vertices
|
|
FSkeletalMeshVertIndexAndZ IAndZ;
|
|
IAndZ.Index = RawIndex;
|
|
IAndZ.Z = Vertex.Position.Z;
|
|
|
|
VertIndexAndZ.Add(IAndZ);
|
|
}
|
|
}
|
|
|
|
// Generate chunks and their vertices and indices
|
|
SkeletalMeshTools::BuildSkeletalMeshChunks(Faces, RawVertices, VertIndexAndZ, bKeepOverlappingVertices, Chunks, bTooManyVerts);
|
|
|
|
// Chunk vertices to satisfy the requested limit.
|
|
const uint32 MaxGPUSkinBones = FGPUBaseSkinVertexFactory::GetMaxGPUSkinBones();
|
|
check(MaxGPUSkinBones <= FGPUBaseSkinVertexFactory::GHardwareMaxGPUSkinBones);
|
|
SkeletalMeshTools::ChunkSkinnedVertices(Chunks, MaxGPUSkinBones);
|
|
|
|
// Build the skeletal model from chunks.
|
|
BuildSkeletalModelFromChunks(LODModel, RefSkeleton, Chunks, PointToOriginalMap);
|
|
|
|
if (IsInGameThread())
|
|
{
|
|
// Only update status if in the game thread. When importing morph targets, this function can run in another thread
|
|
GWarn->EndSlowTask();
|
|
}
|
|
|
|
// Only show these warnings if in the game thread. When importing morph targets, this function can run in another thread and these warnings dont prevent the mesh from importing
|
|
if (IsInGameThread())
|
|
{
|
|
bool bHasBadSections = false;
|
|
for (int32 SectionIndex = 0; SectionIndex < LODModel.Sections.Num(); SectionIndex++)
|
|
{
|
|
FSkelMeshSection& Section = LODModel.Sections[SectionIndex];
|
|
bHasBadSections |= (Section.NumTriangles == 0);
|
|
|
|
// Log info about the section.
|
|
UE_LOG(LogSkeletalMesh, Log, TEXT("Section %u: Material=%u, %u triangles"),
|
|
SectionIndex,
|
|
Section.MaterialIndex,
|
|
Section.NumTriangles
|
|
);
|
|
}
|
|
if (bHasBadSections)
|
|
{
|
|
FText BadSectionMessage(NSLOCTEXT("UnrealEd", "Error_SkeletalMeshHasBadSections", "Input mesh has a section with no triangles. This mesh may not render properly."));
|
|
if (OutWarningMessages)
|
|
{
|
|
OutWarningMessages->Add(BadSectionMessage);
|
|
if (OutWarningNames)
|
|
{
|
|
OutWarningNames->Add(FFbxErrors::SkeletalMesh_SectionWithNoTriangle);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
FMessageDialog::Open(EAppMsgType::Ok, BadSectionMessage);
|
|
}
|
|
}
|
|
|
|
if (bTooManyVerts)
|
|
{
|
|
FText TooManyVertsMessage(NSLOCTEXT("UnrealEd", "Error_SkeletalMeshTooManyVertices", "Input mesh has too many vertices. The generated mesh will be corrupt! Consider adding extra materials to split up the source mesh into smaller chunks."));
|
|
|
|
if (OutWarningMessages)
|
|
{
|
|
OutWarningMessages->Add(TooManyVertsMessage);
|
|
if (OutWarningNames)
|
|
{
|
|
OutWarningNames->Add(FFbxErrors::SkeletalMesh_TooManyVertices);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
FMessageDialog::Open(EAppMsgType::Ok, TooManyVertsMessage);
|
|
}
|
|
}
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
static bool NonOpaqueMaterialPredicate(UStaticMeshComponent* InMesh)
|
|
{
|
|
TArray<UMaterialInterface*> OutMaterials;
|
|
InMesh->GetUsedMaterials(OutMaterials);
|
|
for (auto Material : OutMaterials)
|
|
{
|
|
if (Material == nullptr || Material->GetBlendMode() != BLEND_Opaque)
|
|
{
|
|
return true;
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
static FIntPoint ConditionalImageResize(const FIntPoint& SrcSize, const FIntPoint& DesiredSize, TArray<FColor>& InOutImage, bool bLinearSpace)
|
|
{
|
|
const int32 NumDesiredSamples = DesiredSize.X*DesiredSize.Y;
|
|
if (InOutImage.Num() && InOutImage.Num() != NumDesiredSamples)
|
|
{
|
|
check(InOutImage.Num() == SrcSize.X*SrcSize.Y);
|
|
TArray<FColor> OutImage;
|
|
if (NumDesiredSamples > 0)
|
|
{
|
|
FImageUtils::ImageResize(SrcSize.X, SrcSize.Y, InOutImage, DesiredSize.X, DesiredSize.Y, OutImage, bLinearSpace);
|
|
}
|
|
Exchange(InOutImage, OutImage);
|
|
return DesiredSize;
|
|
}
|
|
|
|
return SrcSize;
|
|
}
|
|
|
|
static void RetrieveValidStaticMeshComponentsForMerging(AActor* InActor, TArray<UStaticMeshComponent*>& OutComponents)
|
|
{
|
|
TInlineComponentArray<UStaticMeshComponent*> Components;
|
|
InActor->GetComponents<UStaticMeshComponent>(Components);
|
|
// TODO: support derived classes from static component
|
|
Components.RemoveAll([](UStaticMeshComponent* Val){ return !(Val->GetClass() == UStaticMeshComponent::StaticClass() || Val->IsA(USplineMeshComponent::StaticClass())); });
|
|
|
|
// TODO: support non-opaque materials
|
|
//Components.RemoveAll(&NonOpaqueMaterialPredicate);
|
|
OutComponents.Append(Components);
|
|
}
|
|
|
|
static void CheckWrappingUVs(TArray<FRawMeshExt>& SourceMeshes, TArray<bool>& MeshShouldBakeVertexData)
|
|
{
|
|
const uint32 MeshCount = SourceMeshes.Num();
|
|
for (uint32 MeshIndex = 0; MeshIndex < MeshCount; ++MeshIndex)
|
|
{
|
|
FRawMeshExt& SourceMesh = SourceMeshes[MeshIndex];
|
|
const int32 LODIndex = SourceMeshes[MeshIndex].ExportLODIndex;
|
|
if (SourceMesh.bShouldExportLOD[LODIndex])
|
|
{
|
|
FRawMesh* RawMesh = SourceMesh.MeshLODData[LODIndex].RawMesh;
|
|
check(RawMesh);
|
|
|
|
for (uint32 ChannelIndex = 0; ChannelIndex < MAX_MESH_TEXTURE_COORDS; ++ChannelIndex)
|
|
{
|
|
bool bProcessed = false;
|
|
bool bHasCoordinates = (RawMesh->WedgeTexCoords[ChannelIndex].Num() != 0);
|
|
|
|
if (bHasCoordinates)
|
|
{
|
|
FVector2D Min(FLT_MAX, FLT_MAX);
|
|
FVector2D Max(-FLT_MAX, -FLT_MAX);
|
|
for (const FVector2D& Coordinate : RawMesh->WedgeTexCoords[ChannelIndex])
|
|
{
|
|
if ((FMath::IsNegativeFloat(Coordinate.X) || FMath::IsNegativeFloat(Coordinate.Y)) || (Coordinate.X > (1.0f + KINDA_SMALL_NUMBER) || Coordinate.Y > (1.0f + KINDA_SMALL_NUMBER)))
|
|
{
|
|
MeshShouldBakeVertexData[MeshIndex] = true;
|
|
bProcessed = true;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
if (bProcessed)
|
|
{
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
void FMeshUtilities::CreateProxyMesh(const TArray<AActor*>& InActors, const struct FMeshProxySettings& InMeshProxySettings, UPackage* InOuter, const FString& InProxyBasePackageName, const FGuid InGuid, FCreateProxyDelegate InProxyCreatedDelegate, const bool bAllowAsync, const float ScreenSize)
|
|
{
|
|
// Error/warning checking for input
|
|
if (MeshMerging == NULL)
|
|
{
|
|
UE_LOG(LogMeshUtilities, Log, TEXT("No automatic mesh merging module available"));
|
|
return;
|
|
}
|
|
|
|
// Check that the delegate has a func-ptr bound to it
|
|
if (!InProxyCreatedDelegate.IsBound())
|
|
{
|
|
UE_LOG(LogMeshUtilities, Log, TEXT("Invalid (unbound) delegate for returning generated proxy mesh"));
|
|
return;
|
|
}
|
|
|
|
// No actors given as input
|
|
if (InActors.Num() == 0)
|
|
{
|
|
UE_LOG(LogMeshUtilities, Log, TEXT("No actors specified to generate a proxy mesh for"));
|
|
return;
|
|
}
|
|
|
|
// Base asset name for a new assets
|
|
// In case outer is null ProxyBasePackageName has to be long package name
|
|
if (InOuter == nullptr && FPackageName::IsShortPackageName(InProxyBasePackageName))
|
|
{
|
|
UE_LOG(LogMeshUtilities, Warning, TEXT("Invalid long package name: '%s'."), *InProxyBasePackageName);
|
|
return;
|
|
}
|
|
|
|
FScopedSlowTask SlowTask(100.f, (LOCTEXT("CreateProxyMesh_CreateMesh", "Creating Mesh Proxy")));
|
|
SlowTask.MakeDialog();
|
|
|
|
// Retrieve static mesh components valid for merging from the given set of actors
|
|
TArray<UStaticMeshComponent*> ComponentsToMerge;
|
|
{
|
|
// Collect components to merge
|
|
for (AActor* Actor : InActors)
|
|
{
|
|
RetrieveValidStaticMeshComponentsForMerging(Actor, ComponentsToMerge);
|
|
}
|
|
}
|
|
|
|
// Check if there are actually any static mesh components to merge
|
|
if (ComponentsToMerge.Num() == 0)
|
|
{
|
|
UE_LOG(LogMeshUtilities, Log, TEXT("No valid static mesh components found in given set of Actors"));
|
|
return;
|
|
}
|
|
|
|
typedef FIntPoint FMeshIdAndLOD;
|
|
TArray<FRawMeshExt> SourceMeshes;
|
|
TArray<FSectionInfo> UniqueSections;
|
|
TMap<FMeshIdAndLOD, TArray<int32>> GlobalMaterialMap;
|
|
static const int32 ProxyMeshTargetLODLevel = 0;
|
|
|
|
FBoxSphereBounds EstimatedBounds(ForceInitToZero);
|
|
for (const UStaticMeshComponent* StaticMeshComponent : ComponentsToMerge)
|
|
{
|
|
EstimatedBounds = EstimatedBounds + StaticMeshComponent->Bounds;
|
|
}
|
|
|
|
static const float FOVRad = 90.0f * (float)PI / 360.0f;
|
|
static const FMatrix ProjectionMatrix = FPerspectiveMatrix(FOVRad, 1920, 1080, 0.01f);
|
|
FHierarchicalLODUtilitiesModule& Module = FModuleManager::LoadModuleChecked<FHierarchicalLODUtilitiesModule>("HierarchicalLODUtilities");
|
|
IHierarchicalLODUtilities* Utilities = Module.GetUtilities();
|
|
float EstimatedDistance = Utilities->CalculateDrawDistanceFromScreenSize(EstimatedBounds.SphereRadius, ScreenSize, ProjectionMatrix);
|
|
|
|
SlowTask.EnterProgressFrame(5.0f, LOCTEXT("CreateProxyMesh_CollectingMeshes", "Collecting Input Static Meshes"));
|
|
|
|
// Retrieve mesh / material data
|
|
for (const UStaticMeshComponent* StaticMeshComponent : ComponentsToMerge)
|
|
{
|
|
TArray<int32> StaticMeshGlobalMaterialMap;
|
|
FRawMesh* RawMesh = new FRawMesh();
|
|
FMemory::Memzero(RawMesh, sizeof(FRawMesh));
|
|
|
|
const int32 ProxyMeshSourceLODLevel = InMeshProxySettings.bCalculateCorrectLODModel ? Utilities->GetLODLevelForScreenSize(StaticMeshComponent, Utilities->CalculateScreenSizeFromDrawDistance(StaticMeshComponent->Bounds.SphereRadius, ProjectionMatrix, EstimatedDistance)) : 0;
|
|
// Proxy meshes should always propagate vertex colours for material baking
|
|
static const bool bPropagateVertexColours = true;
|
|
|
|
const bool bValidRawMesh = ConstructRawMesh(StaticMeshComponent, ProxyMeshSourceLODLevel, bPropagateVertexColours, *RawMesh, UniqueSections, StaticMeshGlobalMaterialMap);
|
|
|
|
if ( bValidRawMesh )
|
|
{
|
|
// Add constructed raw mesh to source mesh array
|
|
const int32 SourceMeshIndex = SourceMeshes.AddZeroed();
|
|
SourceMeshes[SourceMeshIndex].MeshLODData[ProxyMeshTargetLODLevel].RawMesh = RawMesh;
|
|
SourceMeshes[SourceMeshIndex].bShouldExportLOD[ProxyMeshTargetLODLevel] = true;
|
|
SourceMeshes[SourceMeshIndex].ExportLODIndex = ProxyMeshTargetLODLevel;
|
|
|
|
// Make sure we do now the bounds of our UVs
|
|
//CalculateTextureCoordinateBoundsForRawMesh(*SourceMeshes[SourceMeshIndex].MeshLODData[ProxyMeshTargetLODLevel].RawMesh, SourceMeshes[SourceMeshIndex].MeshLODData[ProxyMeshTargetLODLevel].TexCoordBounds);
|
|
|
|
// Append retrieved materials for this static mesh component to the global material map
|
|
GlobalMaterialMap.Add(FMeshIdAndLOD(SourceMeshIndex, ProxyMeshTargetLODLevel), StaticMeshGlobalMaterialMap);
|
|
}
|
|
}
|
|
|
|
if (SourceMeshes.Num() == 0)
|
|
{
|
|
UE_LOG(LogMeshUtilities, Log, TEXT("No valid (or completely culled) raw meshes constructed from static mesh components"));
|
|
return;
|
|
}
|
|
|
|
SlowTask.EnterProgressFrame(InMeshProxySettings.bUseLandscapeCulling ? 40.0f : 45.0f, LOCTEXT("CreateProxyMesh_RemapAndFlatten", "Remapping and Flattening Materials"));
|
|
TArray<bool> MeshShouldBakeVertexData;
|
|
MeshShouldBakeVertexData.AddZeroed(SourceMeshes.Num());
|
|
CheckWrappingUVs(SourceMeshes, MeshShouldBakeVertexData);
|
|
|
|
TMap<FMeshIdAndLOD, TArray<int32> > NewGlobalMaterialMap;
|
|
TArray<FSectionInfo> NewUniqueSections;
|
|
FMaterialUtilities::RemapUniqueMaterialIndices(
|
|
UniqueSections,
|
|
SourceMeshes,
|
|
GlobalMaterialMap,
|
|
InMeshProxySettings.MaterialSettings,
|
|
true, // Always need vertex data for baking materials
|
|
true, // Always want to merge materials
|
|
MeshShouldBakeVertexData,
|
|
NewGlobalMaterialMap,
|
|
NewUniqueSections);
|
|
// Use shared material data.
|
|
Exchange(GlobalMaterialMap, NewGlobalMaterialMap);
|
|
Exchange(UniqueSections, NewUniqueSections);
|
|
|
|
// Flatten Materials
|
|
TArray<FFlattenMaterial> FlattenedMaterials;
|
|
|
|
TArray<UMaterialInterface*> Materials;
|
|
for (const FSectionInfo& Section : UniqueSections)
|
|
{
|
|
Materials.Push(Section.Material);
|
|
}
|
|
|
|
|
|
FlattenMaterialsWithMeshData(Materials, SourceMeshes, GlobalMaterialMap, MeshShouldBakeVertexData, InMeshProxySettings.MaterialSettings, FlattenedMaterials);
|
|
|
|
for (FRawMeshExt& MeshData : SourceMeshes)
|
|
{
|
|
if (MeshData.MeshLODData[MeshData.ExportLODIndex].NewUVs.Num() == 0)
|
|
{
|
|
MeshData.MeshLODData[MeshData.ExportLODIndex].TexCoordBounds.Empty();
|
|
}
|
|
}
|
|
|
|
for (FFlattenMaterial& InMaterial : FlattenedMaterials)
|
|
{
|
|
FMaterialUtilities::OptimizeFlattenMaterial(InMaterial);
|
|
}
|
|
|
|
//For each raw mesh, re-map the material indices from Local to Global material indices space
|
|
for (int32 RawMeshIndex = 0; RawMeshIndex < SourceMeshes.Num(); ++RawMeshIndex)
|
|
{
|
|
const TArray<int32>& GlobalMaterialIndices = *GlobalMaterialMap.Find(FMeshIdAndLOD(RawMeshIndex, ProxyMeshTargetLODLevel));
|
|
TArray<int32>& MaterialIndices = SourceMeshes[RawMeshIndex].MeshLODData[ProxyMeshTargetLODLevel].RawMesh->FaceMaterialIndices;
|
|
int32 MaterialIndicesCount = MaterialIndices.Num();
|
|
|
|
for (int32 TriangleIndex = 0; TriangleIndex < MaterialIndicesCount; ++TriangleIndex)
|
|
{
|
|
int32 LocalMaterialIndex = MaterialIndices[TriangleIndex];
|
|
int32 GlobalMaterialIndex = GlobalMaterialIndices[LocalMaterialIndex];
|
|
|
|
//Assign the new material index to the raw mesh
|
|
MaterialIndices[TriangleIndex] = GlobalMaterialIndex;
|
|
}
|
|
}
|
|
|
|
// Build proxy mesh
|
|
|
|
// Landscape culling
|
|
TArray<FRawMesh*> CullingRawMeshes;
|
|
if (InMeshProxySettings.bUseLandscapeCulling)
|
|
{
|
|
SlowTask.EnterProgressFrame(5.0f, LOCTEXT("CreateProxyMesh_LandscapeCulling", "Applying Landscape Culling"));
|
|
|
|
// Extract landscape proxies and cull volumes from the world
|
|
TArray<ALandscapeProxy*> LandscapeActors;
|
|
TArray<AMeshMergeCullingVolume*> CullVolumes;
|
|
|
|
UWorld* InWorld = InActors[0]->GetWorld();
|
|
|
|
uint32 MaxLandscapeExportLOD = 0;
|
|
if (InWorld->IsValidLowLevel())
|
|
{
|
|
for (FConstLevelIterator Iterator = InWorld->GetLevelIterator(); Iterator; ++Iterator)
|
|
{
|
|
for (AActor* Actor : (*Iterator)->Actors)
|
|
{
|
|
if (Actor)
|
|
{
|
|
ALandscapeProxy* LandscapeProxy = Cast<ALandscapeProxy>(Actor);
|
|
if (LandscapeProxy && LandscapeProxy->bUseLandscapeForCullingInvisibleHLODVertices)
|
|
{
|
|
// Retrieve highest landscape LOD level possible
|
|
MaxLandscapeExportLOD = FMath::Max(MaxLandscapeExportLOD, FMath::CeilLogTwo(LandscapeProxy->SubsectionSizeQuads + 1) - 1);
|
|
LandscapeActors.Add(LandscapeProxy);
|
|
}
|
|
// Check for culling volumes
|
|
AMeshMergeCullingVolume* Volume = Cast<AMeshMergeCullingVolume>(Actor);
|
|
if (Volume)
|
|
{
|
|
// If the mesh's bounds intersect with the volume there is a possibility of culling
|
|
const bool bIntersecting = Volume->EncompassesPoint(EstimatedBounds.Origin, EstimatedBounds.SphereRadius, nullptr);
|
|
if (bIntersecting)
|
|
{
|
|
CullVolumes.Add(Volume);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// Setting determines the precision at which we should export the landscape for culling (highest, half or lowest)
|
|
const uint32 LandscapeExportLOD = ((float)MaxLandscapeExportLOD * (0.5f * (float)InMeshProxySettings.LandscapeCullingPrecision));
|
|
for (ALandscapeProxy* Landscape : LandscapeActors)
|
|
{
|
|
// Export the landscape to raw mesh format
|
|
FRawMesh* LandscapeRawMesh = new FRawMesh();
|
|
FBoxSphereBounds LandscapeBounds = EstimatedBounds;
|
|
Landscape->ExportToRawMesh(LandscapeExportLOD, *LandscapeRawMesh, LandscapeBounds);
|
|
if (LandscapeRawMesh->VertexPositions.Num())
|
|
{
|
|
CullingRawMeshes.Add(LandscapeRawMesh);
|
|
}
|
|
}
|
|
|
|
// Also add volume mesh data as culling meshes
|
|
for (AMeshMergeCullingVolume* Volume : CullVolumes)
|
|
{
|
|
// Export the landscape to raw mesh format
|
|
FRawMesh* VolumeMesh = new FRawMesh();
|
|
|
|
TArray<FStaticMaterial> VolumeMaterials;
|
|
GetBrushMesh(Volume, Volume->Brush, *VolumeMesh, VolumeMaterials);
|
|
|
|
// Offset vertices to correct world position;
|
|
FVector VolumeLocation = Volume->GetActorLocation();
|
|
for (FVector& Position : VolumeMesh->VertexPositions)
|
|
{
|
|
Position += VolumeLocation;
|
|
}
|
|
|
|
CullingRawMeshes.Add(VolumeMesh);
|
|
}
|
|
}
|
|
|
|
// Allocate merge complete data
|
|
FMergeCompleteData* Data = new FMergeCompleteData();
|
|
Data->InOuter = InOuter;
|
|
Data->InProxySettings = InMeshProxySettings;
|
|
Data->ProxyBasePackageName = InProxyBasePackageName;
|
|
Data->CallbackDelegate = InProxyCreatedDelegate;
|
|
|
|
// Add this proxy job to map
|
|
Processor->AddProxyJob(InGuid, Data);
|
|
|
|
// We are only using LOD level 0 (ProxyMeshTargetLODLevel)
|
|
TArray<FMeshMergeData> MergeData;
|
|
for (FRawMeshExt& SourceMesh : SourceMeshes)
|
|
{
|
|
MergeData.Add(SourceMesh.MeshLODData[ProxyMeshTargetLODLevel]);
|
|
}
|
|
|
|
// Populate landscape clipping geometry
|
|
for (FRawMesh* RawMesh : CullingRawMeshes)
|
|
{
|
|
FMeshMergeData ClipData;
|
|
ClipData.bIsClippingMesh = true;
|
|
ClipData.RawMesh = RawMesh;
|
|
MergeData.Add(ClipData);
|
|
}
|
|
|
|
SlowTask.EnterProgressFrame(50.0f, LOCTEXT("CreateProxyMesh_GenerateProxy", "Generating Proxy Mesh"));
|
|
// Choose Simplygon Swarm (if available) or local proxy lod method
|
|
if (DistributedMeshMerging != nullptr && GetDefault<UEditorPerProjectUserSettings>()->bUseSimplygonSwarm && bAllowAsync)
|
|
{
|
|
DistributedMeshMerging->ProxyLOD(MergeData, Data->InProxySettings, FlattenedMaterials, InGuid);
|
|
}
|
|
else
|
|
{
|
|
MeshMerging->ProxyLOD(MergeData, Data->InProxySettings, FlattenedMaterials, InGuid);
|
|
Processor->Tick(0); // make sure caller gets merging results
|
|
}
|
|
|
|
for (FMeshMergeData& DataToRelease : MergeData)
|
|
{
|
|
DataToRelease.ReleaseData();
|
|
}
|
|
}
|
|
|
|
void FMeshUtilities::CreateProxyMesh(const TArray<AActor*>& Actors, const struct FMeshProxySettings& InProxySettings, UPackage* InOuter, const FString& ProxyBasePackageName, TArray<UObject*>& OutAssetsToSync, FVector& OutProxyLocation)
|
|
{
|
|
CreateProxyMesh(Actors, InProxySettings, InOuter, ProxyBasePackageName, OutAssetsToSync);
|
|
}
|
|
|
|
void FMeshUtilities::CreateProxyMesh(const TArray<AActor*>& Actors, const struct FMeshProxySettings& InProxySettings, UPackage* InOuter, const FString& ProxyBasePackageName, TArray<UObject*>& OutAssetsToSync, const float ScreenAreaSize)
|
|
{
|
|
FCreateProxyDelegate Delegate;
|
|
|
|
FGuid JobGuid = FGuid::NewGuid();
|
|
Delegate.BindLambda(
|
|
[&](const FGuid Guid, TArray<UObject*>& InAssetsToSync)
|
|
{
|
|
if (JobGuid == Guid)
|
|
{
|
|
OutAssetsToSync.Append(InAssetsToSync);
|
|
}
|
|
}
|
|
);
|
|
|
|
CreateProxyMesh(Actors, InProxySettings, InOuter, ProxyBasePackageName, JobGuid, Delegate, false, ScreenAreaSize);
|
|
}
|
|
|
|
void FMeshUtilities::FlattenMaterialsWithMeshData(TArray<UMaterialInterface*>& InMaterials, TArray<FRawMeshExt>& InSourceMeshes, TMap<FMeshIdAndLOD, TArray<int32>>& InMaterialIndexMap, TArray<bool>& InMeshShouldBakeVertexData, const FMaterialProxySettings &InMaterialProxySettings, TArray<FFlattenMaterial> &OutFlattenedMaterials) const
|
|
{
|
|
FScopedSlowTask SlowTask(InMaterials.Num(), (LOCTEXT("FlattenMaterialsWithMeshData", "Flattening Materials With Mesh Data")));
|
|
SlowTask.MakeDialog();
|
|
|
|
// Prepare container for cached shaders.
|
|
TMap<UMaterialInterface*, FExportMaterialProxyCache> CachedShaders;
|
|
CachedShaders.Empty(InMaterials.Num());
|
|
|
|
bool bDitheredLODTransition = false;
|
|
|
|
for (int32 MaterialIndex = 0; MaterialIndex < InMaterials.Num(); MaterialIndex++)
|
|
{
|
|
UMaterialInterface* CurrentMaterial = InMaterials[MaterialIndex];
|
|
SlowTask.EnterProgressFrame(1.0f, FText::FromString(FString::Printf(TEXT("Flattening out %s"), *CurrentMaterial->GetName())));
|
|
|
|
// Store if any material uses dithered transitions
|
|
bDitheredLODTransition |= CurrentMaterial->IsDitheredLODTransition();
|
|
|
|
// Check if we already have cached compiled shader for this material.
|
|
FExportMaterialProxyCache* CachedShader = CachedShaders.Find(CurrentMaterial);
|
|
if (CachedShader == nullptr)
|
|
{
|
|
CachedShader = &CachedShaders.Add(CurrentMaterial);
|
|
}
|
|
|
|
FFlattenMaterial FlattenMaterial = FMaterialUtilities::CreateFlattenMaterialWithSettings(InMaterialProxySettings);
|
|
|
|
/* Find a mesh which uses the current material. Materials using vertex data are added for each individual mesh using it,
|
|
which is why baking down the materials like this works. :) */
|
|
int32 UsedMeshIndex = 0;
|
|
int32 LocalMaterialIndex = 0;
|
|
int32 LocalTextureBoundIndex = 0;
|
|
FMeshMergeData* MergeData = nullptr;
|
|
for (int32 MeshIndex = 0; MeshIndex < InSourceMeshes.Num() && MergeData == nullptr; MeshIndex++)
|
|
{
|
|
const int32 LODIndex = InSourceMeshes[MeshIndex].ExportLODIndex;
|
|
if (InSourceMeshes[MeshIndex].MeshLODData[LODIndex].RawMesh->VertexPositions.Num())
|
|
{
|
|
const TArray<int32>& GlobalMaterialIndices = *InMaterialIndexMap.Find(FMeshIdAndLOD(MeshIndex, LODIndex));
|
|
for (LocalMaterialIndex = 0; LocalMaterialIndex < GlobalMaterialIndices.Num(); LocalMaterialIndex++)
|
|
{
|
|
// Only need to set merge data if we need to bake out using vertex data for this specific mesh
|
|
if (InMeshShouldBakeVertexData[MeshIndex] && GlobalMaterialIndices[LocalMaterialIndex] == MaterialIndex)
|
|
{
|
|
UsedMeshIndex = MeshIndex;
|
|
MergeData = &InSourceMeshes[MeshIndex].MeshLODData[LODIndex];
|
|
LocalTextureBoundIndex = LocalMaterialIndex;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
break;
|
|
}
|
|
}
|
|
|
|
// If there is specific vertex data available and used in the material we should generate non-overlapping UVs
|
|
if (MergeData && InMeshShouldBakeVertexData[UsedMeshIndex])
|
|
{
|
|
// Generate new non-overlapping texture coordinates for mesh if needed
|
|
if (MergeData->TexCoordBounds.Num() == 0)
|
|
{
|
|
// Calculate the max bounds for this raw mesh
|
|
CalculateTextureCoordinateBoundsForRawMesh(*MergeData->RawMesh, MergeData->TexCoordBounds);
|
|
}
|
|
|
|
if (MergeData->NewUVs.Num() == 0)
|
|
{
|
|
// Generate unique UVs
|
|
GenerateUniqueUVsForStaticMesh(*MergeData->RawMesh, InMaterialProxySettings.TextureSize.GetMax(), MergeData->NewUVs);
|
|
}
|
|
|
|
FBox2D TextureBoundsForMesh(EForceInit::ForceInitToZero);
|
|
for (const FVector2D& UV : MergeData->NewUVs)
|
|
{
|
|
TextureBoundsForMesh += UV;
|
|
}
|
|
|
|
// Export the material using mesh data to support vertex based material properties
|
|
FMaterialUtilities::ExportMaterial(
|
|
CurrentMaterial,
|
|
MergeData->RawMesh,
|
|
LocalMaterialIndex,
|
|
TextureBoundsForMesh,
|
|
MergeData->NewUVs,
|
|
FlattenMaterial,
|
|
CachedShader);
|
|
}
|
|
else
|
|
{
|
|
// Export the material without vertex data
|
|
FMaterialUtilities::ExportMaterial(
|
|
CurrentMaterial,
|
|
FlattenMaterial,
|
|
CachedShader);
|
|
}
|
|
|
|
// Fill flatten material samples alpha values with 255 (for saving out textures correctly for Simplygon Swarm)
|
|
FlattenMaterial.FillAlphaValues(255);
|
|
|
|
// Add flattened material to outgoing array
|
|
OutFlattenedMaterials.Add(FlattenMaterial);
|
|
|
|
// Check if this material will be used later. If not - release shader.
|
|
bool bMaterialStillUsed = false;
|
|
for (int32 Index = MaterialIndex + 1; Index < InMaterials.Num(); Index++)
|
|
{
|
|
if (InMaterials[Index] == CurrentMaterial)
|
|
{
|
|
bMaterialStillUsed = true;
|
|
break;
|
|
}
|
|
}
|
|
if (!bMaterialStillUsed)
|
|
{
|
|
CachedShader->Release();
|
|
}
|
|
}
|
|
|
|
if (OutFlattenedMaterials.Num() > 1)
|
|
{
|
|
// Dither transition fix-up
|
|
for (FFlattenMaterial& FlatMaterial : OutFlattenedMaterials)
|
|
{
|
|
FlatMaterial.bDitheredLODTransition = bDitheredLODTransition;
|
|
}
|
|
|
|
// Start with determining maximum emissive scale
|
|
float MaxEmissiveScale = 0.0f;
|
|
for (FFlattenMaterial& FlatMaterial : OutFlattenedMaterials)
|
|
{
|
|
if (FlatMaterial.DoesPropertyContainData(EFlattenMaterialProperties::Emissive))
|
|
{
|
|
if (FlatMaterial.EmissiveScale > MaxEmissiveScale)
|
|
{
|
|
MaxEmissiveScale = FlatMaterial.EmissiveScale;
|
|
}
|
|
}
|
|
}
|
|
|
|
if (MaxEmissiveScale > 0.001f)
|
|
{
|
|
// Rescale all materials.
|
|
for (FFlattenMaterial& FlatMaterial : OutFlattenedMaterials)
|
|
{
|
|
const float Scale = FlatMaterial.EmissiveScale / MaxEmissiveScale;
|
|
if (FMath::Abs(Scale - 1.0f) < 0.01f)
|
|
{
|
|
// Difference is not noticeable for this material, or this material has maximal emissive level.
|
|
continue;
|
|
}
|
|
// Rescale emissive data.
|
|
TArray<FColor>& EmissiveSamples = FlatMaterial.GetPropertySamples(EFlattenMaterialProperties::Emissive);
|
|
for (int32 PixelIndex = 0; PixelIndex < EmissiveSamples.Num(); PixelIndex++)
|
|
{
|
|
FColor& C = EmissiveSamples[PixelIndex];
|
|
C.R = FMath::RoundToInt(C.R * Scale);
|
|
C.G = FMath::RoundToInt(C.G * Scale);
|
|
C.B = FMath::RoundToInt(C.B * Scale);
|
|
}
|
|
|
|
// Update emissive scale to maximum
|
|
FlatMaterial.EmissiveScale = MaxEmissiveScale;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// Exports static mesh LOD render data to a RawMesh
|
|
static void ExportStaticMeshLOD(const FStaticMeshLODResources& StaticMeshLOD, FRawMesh& OutRawMesh)
|
|
{
|
|
const int32 NumWedges = StaticMeshLOD.IndexBuffer.GetNumIndices();
|
|
const int32 NumVertexPositions = StaticMeshLOD.PositionVertexBuffer.GetNumVertices();
|
|
const int32 NumFaces = NumWedges / 3;
|
|
|
|
// Indices
|
|
StaticMeshLOD.IndexBuffer.GetCopy(OutRawMesh.WedgeIndices);
|
|
|
|
// Vertex positions
|
|
if (NumVertexPositions > 0)
|
|
{
|
|
OutRawMesh.VertexPositions.Empty(NumVertexPositions);
|
|
for (int32 PosIdx = 0; PosIdx < NumVertexPositions; ++PosIdx)
|
|
{
|
|
FVector Pos = StaticMeshLOD.PositionVertexBuffer.VertexPosition(PosIdx);
|
|
OutRawMesh.VertexPositions.Add(Pos);
|
|
}
|
|
}
|
|
|
|
// Vertex data
|
|
if (StaticMeshLOD.VertexBuffer.GetNumVertices() > 0)
|
|
{
|
|
OutRawMesh.WedgeTangentX.Empty(NumWedges);
|
|
OutRawMesh.WedgeTangentY.Empty(NumWedges);
|
|
OutRawMesh.WedgeTangentZ.Empty(NumWedges);
|
|
|
|
const int32 NumTexCoords = StaticMeshLOD.VertexBuffer.GetNumTexCoords();
|
|
for (int32 TexCoodIdx = 0; TexCoodIdx < NumTexCoords; ++TexCoodIdx)
|
|
{
|
|
OutRawMesh.WedgeTexCoords[TexCoodIdx].Empty(NumWedges);
|
|
}
|
|
|
|
for (int32 WedgeIndex : OutRawMesh.WedgeIndices)
|
|
{
|
|
FVector WedgeTangentX = StaticMeshLOD.VertexBuffer.VertexTangentX(WedgeIndex);
|
|
FVector WedgeTangentY = StaticMeshLOD.VertexBuffer.VertexTangentY(WedgeIndex);
|
|
FVector WedgeTangentZ = StaticMeshLOD.VertexBuffer.VertexTangentZ(WedgeIndex);
|
|
OutRawMesh.WedgeTangentX.Add(WedgeTangentX);
|
|
OutRawMesh.WedgeTangentY.Add(WedgeTangentY);
|
|
OutRawMesh.WedgeTangentZ.Add(WedgeTangentZ);
|
|
|
|
for (int32 TexCoodIdx = 0; TexCoodIdx < NumTexCoords; ++TexCoodIdx)
|
|
{
|
|
FVector2D WedgeTexCoord = StaticMeshLOD.VertexBuffer.GetVertexUV(WedgeIndex, TexCoodIdx);
|
|
OutRawMesh.WedgeTexCoords[TexCoodIdx].Add(WedgeTexCoord);
|
|
}
|
|
}
|
|
}
|
|
|
|
// Vertex colors
|
|
if (StaticMeshLOD.ColorVertexBuffer.GetNumVertices() > 0)
|
|
{
|
|
OutRawMesh.WedgeColors.Empty(NumWedges);
|
|
for (int32 WedgeIndex : OutRawMesh.WedgeIndices)
|
|
{
|
|
FColor VertexColor = StaticMeshLOD.ColorVertexBuffer.VertexColor(WedgeIndex);
|
|
OutRawMesh.WedgeColors.Add(VertexColor);
|
|
}
|
|
}
|
|
|
|
// Materials
|
|
{
|
|
OutRawMesh.FaceMaterialIndices.Empty(NumFaces);
|
|
OutRawMesh.FaceMaterialIndices.SetNumZeroed(NumFaces);
|
|
|
|
for (const FStaticMeshSection& Section : StaticMeshLOD.Sections)
|
|
{
|
|
uint32 FirstTriangle = Section.FirstIndex / 3;
|
|
for (uint32 TriangleIndex = 0; TriangleIndex < Section.NumTriangles; ++TriangleIndex)
|
|
{
|
|
OutRawMesh.FaceMaterialIndices[FirstTriangle + TriangleIndex] = Section.MaterialIndex;
|
|
}
|
|
}
|
|
}
|
|
|
|
// Smoothing masks
|
|
{
|
|
OutRawMesh.FaceSmoothingMasks.Empty(NumFaces);
|
|
OutRawMesh.FaceSmoothingMasks.SetNumUninitialized(NumFaces);
|
|
|
|
for (auto& SmoothingMask : OutRawMesh.FaceSmoothingMasks)
|
|
{
|
|
SmoothingMask = 1;
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
const bool IsLandscapeHit(const FVector& RayOrigin, const FVector& RayEndPoint, const UWorld* World, const TArray<ALandscapeProxy*>& LandscapeProxies, FVector& OutHitLocation)
|
|
{
|
|
static FName TraceTag = FName(TEXT("LandscapeTrace"));
|
|
TArray<FHitResult> Results;
|
|
// Each landscape component has 2 collision shapes, 1 of them is specific to landscape editor
|
|
// Trace only ECC_Visibility channel, so we do hit only Editor specific shape
|
|
World->LineTraceMultiByObjectType(Results, RayOrigin, RayEndPoint, FCollisionObjectQueryParams(ECollisionChannel::ECC_Visibility), FCollisionQueryParams(TraceTag, true));
|
|
|
|
bool bHitLandscape = false;
|
|
|
|
for (const FHitResult& HitResult : Results)
|
|
{
|
|
ULandscapeHeightfieldCollisionComponent* CollisionComponent = Cast<ULandscapeHeightfieldCollisionComponent>(HitResult.Component.Get());
|
|
if (CollisionComponent)
|
|
{
|
|
ALandscapeProxy* HitLandscape = CollisionComponent->GetLandscapeProxy();
|
|
if (HitLandscape && LandscapeProxies.Contains(HitLandscape))
|
|
{
|
|
// Could write a correct clipping algorithm, that clips the triangle to hit location
|
|
OutHitLocation = HitLandscape->LandscapeActorToWorld().InverseTransformPosition(HitResult.Location);
|
|
// Above landscape so visible
|
|
bHitLandscape = true;
|
|
}
|
|
}
|
|
}
|
|
|
|
return bHitLandscape;
|
|
}
|
|
|
|
|
|
void CullTrianglesFromVolumesAndUnderLandscapes(const UStaticMeshComponent* InMeshComponent, FRawMesh &OutRawMesh)
|
|
{
|
|
UWorld* World = InMeshComponent->GetWorld();
|
|
TArray<ALandscapeProxy*> Landscapes;
|
|
TArray<AMeshMergeCullingVolume*> CullVolumes;
|
|
|
|
FBox ComponentBox(InMeshComponent->Bounds.Origin - InMeshComponent->Bounds.BoxExtent, InMeshComponent->Bounds.Origin + InMeshComponent->Bounds.BoxExtent);
|
|
|
|
for (ULevel* Level : World->GetLevels())
|
|
{
|
|
for (AActor* Actor : Level->Actors)
|
|
{
|
|
ALandscape* Proxy = Cast<ALandscape>(Actor);
|
|
if (Proxy && Proxy->bUseLandscapeForCullingInvisibleHLODVertices)
|
|
{
|
|
FVector Origin, Extent;
|
|
Proxy->GetActorBounds(false, Origin, Extent);
|
|
FBox LandscapeBox(Origin - Extent, Origin + Extent);
|
|
|
|
// Ignore Z axis for 2d bounds check
|
|
if (LandscapeBox.IntersectXY(ComponentBox))
|
|
{
|
|
Landscapes.Add(Proxy->GetLandscapeActor());
|
|
}
|
|
}
|
|
|
|
// Check for culling volumes
|
|
AMeshMergeCullingVolume* Volume = Cast<AMeshMergeCullingVolume>(Actor);
|
|
if (Volume)
|
|
{
|
|
// If the mesh's bounds intersect with the volume there is a possibility of culling
|
|
const bool bIntersecting = Volume->EncompassesPoint(InMeshComponent->Bounds.Origin, InMeshComponent->Bounds.SphereRadius, nullptr);
|
|
if (bIntersecting)
|
|
{
|
|
CullVolumes.Add(Volume);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
TArray<bool> VertexVisible;
|
|
VertexVisible.AddZeroed(OutRawMesh.VertexPositions.Num());
|
|
int32 Index = 0;
|
|
|
|
for (const FVector& Position : OutRawMesh.VertexPositions)
|
|
{
|
|
// Start with setting visibility to true on all vertices
|
|
VertexVisible[Index] = true;
|
|
|
|
// Check if this vertex is culled due to being underneath a landscape
|
|
if (Landscapes.Num() > 0)
|
|
{
|
|
bool bVertexWithinLandscapeBounds = false;
|
|
|
|
for (ALandscapeProxy* Proxy : Landscapes)
|
|
{
|
|
FVector Origin, Extent;
|
|
Proxy->GetActorBounds(false, Origin, Extent);
|
|
FBox LandscapeBox(Origin - Extent, Origin + Extent);
|
|
bVertexWithinLandscapeBounds |= LandscapeBox.IsInsideXY(Position);
|
|
}
|
|
|
|
if (bVertexWithinLandscapeBounds)
|
|
{
|
|
const FVector Start = Position;
|
|
FVector End = Position - (WORLD_MAX * FVector::UpVector);
|
|
FVector OutHit;
|
|
const bool IsAboveLandscape = IsLandscapeHit(Start, End, World, Landscapes, OutHit);
|
|
|
|
End = Position + (WORLD_MAX * FVector::UpVector);
|
|
const bool IsUnderneathLandscape = IsLandscapeHit(Start, End, World, Landscapes, OutHit);
|
|
|
|
// Vertex is visible when above landscape (with actual landscape underneath) or if there is no landscape beneath or above the vertex (falls outside of landscape bounds)
|
|
VertexVisible[Index] = (IsAboveLandscape && !IsUnderneathLandscape);// || (!IsAboveLandscape && !IsUnderneathLandscape);
|
|
}
|
|
}
|
|
|
|
// Volume culling
|
|
for (AMeshMergeCullingVolume* Volume : CullVolumes)
|
|
{
|
|
const bool bVertexIsInsideVolume = Volume->EncompassesPoint(Position, 0.0f, nullptr);
|
|
if (bVertexIsInsideVolume)
|
|
{
|
|
// Inside a culling volume so invisible
|
|
VertexVisible[Index] = false;
|
|
}
|
|
}
|
|
|
|
Index++;
|
|
}
|
|
|
|
|
|
// We now know which vertices are below the landscape
|
|
TArray<bool> TriangleVisible;
|
|
int32 NumTriangles = OutRawMesh.WedgeIndices.Num() / 3;
|
|
TriangleVisible.AddZeroed(NumTriangles);
|
|
|
|
bool bCreateNewMesh = false;
|
|
|
|
// Determine which triangles of the mesh are visible
|
|
for (int32 TriangleIndex = 0; TriangleIndex < NumTriangles; TriangleIndex++)
|
|
{
|
|
bool AboveLandscape = false;
|
|
|
|
for (int32 WedgeIndex = 0; WedgeIndex < 3; ++WedgeIndex)
|
|
{
|
|
AboveLandscape |= VertexVisible[OutRawMesh.WedgeIndices[(TriangleIndex * 3) + WedgeIndex]];
|
|
}
|
|
TriangleVisible[TriangleIndex] = AboveLandscape;
|
|
bCreateNewMesh |= !AboveLandscape;
|
|
|
|
}
|
|
|
|
// Check whether or not we have to create a new mesh
|
|
if (bCreateNewMesh)
|
|
{
|
|
FRawMesh NewRawMesh;
|
|
TMap<int32, int32> VertexRemapping;
|
|
|
|
// Fill new mesh with data only from visible triangles
|
|
for (int32 TriangleIndex = 0; TriangleIndex < NumTriangles; ++TriangleIndex)
|
|
{
|
|
if (!TriangleVisible[TriangleIndex])
|
|
continue;
|
|
|
|
for (int32 WedgeIndex = 0; WedgeIndex < 3; ++WedgeIndex)
|
|
{
|
|
int32 OldIndex = OutRawMesh.WedgeIndices[(TriangleIndex * 3) + WedgeIndex];
|
|
|
|
int32 NewIndex;
|
|
|
|
int32* RemappedIndex = VertexRemapping.Find(Index);
|
|
if (RemappedIndex)
|
|
{
|
|
NewIndex = *RemappedIndex;
|
|
}
|
|
else
|
|
{
|
|
NewIndex = NewRawMesh.VertexPositions.Add(OutRawMesh.VertexPositions[OldIndex]);
|
|
VertexRemapping.Add(OldIndex, NewIndex);
|
|
}
|
|
|
|
NewRawMesh.WedgeIndices.Add(NewIndex);
|
|
if (OutRawMesh.WedgeColors.Num()) NewRawMesh.WedgeColors.Add(OutRawMesh.WedgeColors[(TriangleIndex * 3) + WedgeIndex]);
|
|
if (OutRawMesh.WedgeTangentX.Num()) NewRawMesh.WedgeTangentX.Add(OutRawMesh.WedgeTangentX[(TriangleIndex * 3) + WedgeIndex]);
|
|
if (OutRawMesh.WedgeTangentY.Num()) NewRawMesh.WedgeTangentY.Add(OutRawMesh.WedgeTangentY[(TriangleIndex * 3) + WedgeIndex]);
|
|
if (OutRawMesh.WedgeTangentZ.Num()) NewRawMesh.WedgeTangentZ.Add(OutRawMesh.WedgeTangentZ[(TriangleIndex * 3) + WedgeIndex]);
|
|
|
|
for (int32 UVIndex = 0; UVIndex < MAX_MESH_TEXTURE_COORDS; ++UVIndex)
|
|
{
|
|
if (OutRawMesh.WedgeTexCoords[UVIndex].Num())
|
|
{
|
|
NewRawMesh.WedgeTexCoords[UVIndex].Add(OutRawMesh.WedgeTexCoords[UVIndex][(TriangleIndex * 3) + WedgeIndex]);
|
|
}
|
|
}
|
|
}
|
|
|
|
NewRawMesh.FaceMaterialIndices.Add(OutRawMesh.FaceMaterialIndices[TriangleIndex]);
|
|
NewRawMesh.FaceSmoothingMasks.Add(OutRawMesh.FaceSmoothingMasks[TriangleIndex]);
|
|
}
|
|
|
|
OutRawMesh = NewRawMesh;
|
|
}
|
|
}
|
|
|
|
void PropagateSplineDeformationToRawMesh(const USplineMeshComponent* InSplineMeshComponent, FRawMesh &OutRawMesh)
|
|
{
|
|
// Apply spline deformation for each vertex's tangents
|
|
for (int32 iVert = 0; iVert < OutRawMesh.WedgeIndices.Num(); ++iVert)
|
|
{
|
|
uint32 Index = OutRawMesh.WedgeIndices[iVert];
|
|
float& AxisValue = USplineMeshComponent::GetAxisValue(OutRawMesh.VertexPositions[Index], InSplineMeshComponent->ForwardAxis);
|
|
FTransform SliceTransform = InSplineMeshComponent->CalcSliceTransform(AxisValue);
|
|
|
|
// Transform tangents first
|
|
if (OutRawMesh.WedgeTangentX.Num())
|
|
{
|
|
OutRawMesh.WedgeTangentX[iVert] = SliceTransform.TransformVector(OutRawMesh.WedgeTangentX[iVert]);
|
|
}
|
|
|
|
if (OutRawMesh.WedgeTangentY.Num())
|
|
{
|
|
OutRawMesh.WedgeTangentY[iVert] = SliceTransform.TransformVector(OutRawMesh.WedgeTangentY[iVert]);
|
|
}
|
|
|
|
if (OutRawMesh.WedgeTangentZ.Num())
|
|
{
|
|
OutRawMesh.WedgeTangentZ[iVert] = SliceTransform.TransformVector(OutRawMesh.WedgeTangentZ[iVert]);
|
|
}
|
|
}
|
|
|
|
// Apply spline deformation for each vertex position
|
|
for (int32 iVert = 0; iVert < OutRawMesh.VertexPositions.Num(); ++iVert)
|
|
{
|
|
float& AxisValue = USplineMeshComponent::GetAxisValue(OutRawMesh.VertexPositions[iVert], InSplineMeshComponent->ForwardAxis);
|
|
FTransform SliceTransform = InSplineMeshComponent->CalcSliceTransform(AxisValue);
|
|
AxisValue = 0.0f;
|
|
OutRawMesh.VertexPositions[iVert] = SliceTransform.TransformPosition(OutRawMesh.VertexPositions[iVert]);
|
|
}
|
|
}
|
|
|
|
|
|
void TransformRawMeshVertexData(const FTransform& InTransform, FRawMesh &OutRawMesh )
|
|
{
|
|
for (FVector& Vertex : OutRawMesh.VertexPositions)
|
|
{
|
|
Vertex = InTransform.TransformPosition(Vertex);
|
|
}
|
|
|
|
for (FVector& TangentX : OutRawMesh.WedgeTangentX)
|
|
{
|
|
TangentX = InTransform.TransformVectorNoScale(TangentX);
|
|
}
|
|
|
|
for (FVector& TangentY : OutRawMesh.WedgeTangentY)
|
|
{
|
|
TangentY = InTransform.TransformVectorNoScale(TangentY);
|
|
}
|
|
|
|
for (FVector& TangentZ : OutRawMesh.WedgeTangentZ)
|
|
{
|
|
TangentZ = InTransform.TransformVectorNoScale(TangentZ);
|
|
}
|
|
|
|
const bool bIsMirrored = InTransform.GetDeterminant() < 0.f;
|
|
if (bIsMirrored)
|
|
{
|
|
// Flip faces
|
|
for (int32 FaceIdx = 0; FaceIdx < OutRawMesh.WedgeIndices.Num() / 3; FaceIdx++)
|
|
{
|
|
int32 I0 = FaceIdx * 3 + 0;
|
|
int32 I2 = FaceIdx * 3 + 2;
|
|
Swap(OutRawMesh.WedgeIndices[I0], OutRawMesh.WedgeIndices[I2]);
|
|
|
|
// seems like vertex colors and UVs are not indexed, so swap values instead
|
|
if (OutRawMesh.WedgeColors.Num())
|
|
{
|
|
Swap(OutRawMesh.WedgeColors[I0], OutRawMesh.WedgeColors[I2]);
|
|
}
|
|
|
|
for (int32 i = 0; i < MAX_MESH_TEXTURE_COORDS; ++i)
|
|
{
|
|
if (OutRawMesh.WedgeTexCoords[i].Num())
|
|
{
|
|
Swap(OutRawMesh.WedgeTexCoords[i][I0], OutRawMesh.WedgeTexCoords[i][I2]);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
void RecomputeTangentsAndNormalsForRawMesh(bool bRecomputeTangents, bool bRecomputeNormals, const FMeshBuildSettings& InBuildSettings, FRawMesh &OutRawMesh )
|
|
{
|
|
const int32 NumWedges = OutRawMesh.WedgeIndices.Num();
|
|
|
|
// Dump normals and tangents if we are recomputing them.
|
|
if (bRecomputeTangents)
|
|
{
|
|
OutRawMesh.WedgeTangentX.Empty(NumWedges);
|
|
OutRawMesh.WedgeTangentX.AddZeroed(NumWedges);
|
|
OutRawMesh.WedgeTangentY.Empty(NumWedges);
|
|
OutRawMesh.WedgeTangentY.AddZeroed(NumWedges);
|
|
}
|
|
|
|
if (bRecomputeNormals)
|
|
{
|
|
OutRawMesh.WedgeTangentZ.Empty(NumWedges);
|
|
OutRawMesh.WedgeTangentZ.AddZeroed(NumWedges);
|
|
}
|
|
|
|
// Compute any missing tangents.
|
|
if (bRecomputeNormals || bRecomputeTangents)
|
|
{
|
|
float ComparisonThreshold = GetComparisonThreshold(InBuildSettings);
|
|
TMultiMap<int32, int32> OverlappingCorners;
|
|
FindOverlappingCorners(OverlappingCorners, OutRawMesh, ComparisonThreshold);
|
|
|
|
// Static meshes always blend normals of overlapping corners.
|
|
uint32 TangentOptions = ETangentOptions::BlendOverlappingNormals;
|
|
if (InBuildSettings.bRemoveDegenerates)
|
|
{
|
|
// If removing degenerate triangles, ignore them when computing tangents.
|
|
TangentOptions |= ETangentOptions::IgnoreDegenerateTriangles;
|
|
}
|
|
if (InBuildSettings.bUseMikkTSpace)
|
|
{
|
|
ComputeTangents_MikkTSpace(OutRawMesh, OverlappingCorners, TangentOptions);
|
|
}
|
|
else
|
|
{
|
|
ComputeTangents(OutRawMesh, OverlappingCorners, TangentOptions);
|
|
}
|
|
}
|
|
|
|
// At this point the mesh will have valid tangents.
|
|
check(OutRawMesh.WedgeTangentX.Num() == NumWedges);
|
|
check(OutRawMesh.WedgeTangentY.Num() == NumWedges);
|
|
check(OutRawMesh.WedgeTangentZ.Num() == NumWedges);
|
|
}
|
|
|
|
bool FMeshUtilities::ConstructRawMesh(
|
|
const UStaticMeshComponent* InMeshComponent,
|
|
int32 InLODIndex,
|
|
const bool bPropagateVertexColours,
|
|
FRawMesh& OutRawMesh,
|
|
TArray<FSectionInfo>& OutUniqueSections,
|
|
TArray<int32>& OutGlobalMaterialIndices) const
|
|
{
|
|
// Retrieve source static mesh
|
|
const UStaticMesh* SourceStaticMesh = InMeshComponent->GetStaticMesh();
|
|
|
|
if (SourceStaticMesh == NULL)
|
|
{
|
|
UE_LOG(LogMeshUtilities, Warning, TEXT("No static mesh actor found in component %s."), *InMeshComponent->GetName());
|
|
return false;
|
|
}
|
|
|
|
if (!SourceStaticMesh->SourceModels.IsValidIndex(InLODIndex))
|
|
{
|
|
UE_LOG(LogMeshUtilities, Log, TEXT("No mesh data found for LOD%d %s."), InLODIndex, *SourceStaticMesh->GetName());
|
|
return false;
|
|
}
|
|
|
|
if (!SourceStaticMesh->RenderData->LODResources.IsValidIndex(InLODIndex))
|
|
{
|
|
UE_LOG(LogMeshUtilities, Warning, TEXT("No mesh render data found for LOD%d %s."), InLODIndex, *SourceStaticMesh->GetName());
|
|
return false;
|
|
}
|
|
|
|
const FStaticMeshSourceModel& SourceStaticMeshModel = SourceStaticMesh->SourceModels[InLODIndex];
|
|
|
|
// Imported meshes will have a filled RawMeshBulkData set
|
|
const bool bImportedMesh = !SourceStaticMeshModel.RawMeshBulkData->IsEmpty();
|
|
// Check whether or not this mesh has been reduced in-engine
|
|
const bool bReducedMesh = (SourceStaticMeshModel.ReductionSettings.PercentTriangles < 1.0f);
|
|
// rying to retrieve rawmesh from SourceStaticMeshModel was giving issues, which causes a mismatch
|
|
const bool bRenderDataMismatch = (InLODIndex > 0);
|
|
|
|
// Determine whether we load the raw mesh data from (original) import data or from the generated render data resources
|
|
if (bImportedMesh && !InMeshComponent->IsA<USplineMeshComponent>() && !bReducedMesh && !bRenderDataMismatch)
|
|
{
|
|
SourceStaticMeshModel.RawMeshBulkData->LoadRawMesh(OutRawMesh);
|
|
}
|
|
else
|
|
{
|
|
ExportStaticMeshLOD(SourceStaticMesh->RenderData->LODResources[InLODIndex], OutRawMesh);
|
|
}
|
|
|
|
// Make sure the raw mesh is not irreparably malformed.
|
|
if (!OutRawMesh.IsValidOrFixable())
|
|
{
|
|
UE_LOG(LogMeshUtilities, Error, TEXT("Raw mesh (%s) is corrupt for LOD%d."), *SourceStaticMesh->GetName(), InLODIndex);
|
|
return false;
|
|
}
|
|
|
|
// Handle spline mesh deformation
|
|
if (InMeshComponent->IsA<USplineMeshComponent>())
|
|
{
|
|
const USplineMeshComponent* SplineMeshComponent = Cast<USplineMeshComponent>(InMeshComponent);
|
|
// Deform raw mesh data according to the Spline Mesh Component's data
|
|
PropagateSplineDeformationToRawMesh(SplineMeshComponent, OutRawMesh);
|
|
}
|
|
|
|
// Use build settings from base mesh for LOD entries that was generated inside Editor.
|
|
const FMeshBuildSettings& BuildSettings = bImportedMesh ? SourceStaticMeshModel.BuildSettings : SourceStaticMesh->SourceModels[0].BuildSettings;
|
|
|
|
// Transform raw mesh to world space
|
|
FTransform ComponentToWorldTransform = InMeshComponent->ComponentToWorld;
|
|
// Take into account build scale settings only for meshes imported from raw data
|
|
// meshes reconstructed from render data already have build scale applied
|
|
if (bImportedMesh)
|
|
{
|
|
ComponentToWorldTransform.SetScale3D(ComponentToWorldTransform.GetScale3D()*BuildSettings.BuildScale3D);
|
|
}
|
|
|
|
// If specified propagate painted vertex colors into our raw mesh
|
|
if (bPropagateVertexColours)
|
|
{
|
|
PropagatePaintedColorsToRawMesh(InMeshComponent, InLODIndex, OutRawMesh);
|
|
}
|
|
|
|
// Transform raw mesh vertex data by the Static Mesh Component's component to world transformation
|
|
TransformRawMeshVertexData(ComponentToWorldTransform, OutRawMesh);
|
|
|
|
// Culling triangles could lead to an entirely empty RawMesh (all vertices culled)
|
|
if (!OutRawMesh.IsValid())
|
|
{
|
|
return false;
|
|
}
|
|
|
|
// Figure out if we should recompute normals and tangents. By default generated LODs should not recompute normals
|
|
const bool bIsMirrored = ComponentToWorldTransform.GetDeterminant() < 0.f;
|
|
bool bRecomputeNormals = (bImportedMesh && BuildSettings.bRecomputeNormals) || OutRawMesh.WedgeTangentZ.Num() == 0 || bIsMirrored;
|
|
bool bRecomputeTangents = (bImportedMesh && BuildSettings.bRecomputeTangents) || OutRawMesh.WedgeTangentX.Num() == 0 || OutRawMesh.WedgeTangentY.Num() == 0 || bIsMirrored;
|
|
|
|
if (bRecomputeNormals || bRecomputeTangents)
|
|
{
|
|
RecomputeTangentsAndNormalsForRawMesh(bRecomputeTangents, bRecomputeNormals, BuildSettings, OutRawMesh);
|
|
}
|
|
|
|
// Retrieving materials
|
|
UMaterialInterface* DefaultMaterial = Cast<UMaterialInterface>(UMaterial::GetDefaultMaterial(MD_Surface));
|
|
|
|
//Need to store the unique material indices in order to re-map the material indices in each rawmesh
|
|
for (const FStaticMeshSection& Section : SourceStaticMesh->RenderData->LODResources[InLODIndex].Sections)
|
|
{
|
|
// Add material and store the material ID
|
|
UMaterialInterface* MaterialToAdd = InMeshComponent->GetMaterial(Section.MaterialIndex);
|
|
|
|
if (MaterialToAdd)
|
|
{
|
|
//Need to check if the resource exists
|
|
FMaterialResource* Resource = MaterialToAdd->GetMaterialResource(GMaxRHIFeatureLevel);
|
|
if (!Resource)
|
|
{
|
|
MaterialToAdd = DefaultMaterial;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
MaterialToAdd = DefaultMaterial;
|
|
}
|
|
|
|
FSectionInfo SectionInfo;
|
|
SectionInfo.Material = MaterialToAdd;
|
|
SectionInfo.bCollisionEnabled = Section.bEnableCollision;
|
|
SectionInfo.bShadowCastingEnabled = Section.bCastShadow;
|
|
const int32 MaterialIdx = OutUniqueSections.Add(SectionInfo);
|
|
|
|
const int32 MaterialMapIdx = OutGlobalMaterialIndices.Add(MaterialIdx);
|
|
|
|
// Update face material indices?
|
|
if (OutRawMesh.FaceMaterialIndices.Num())
|
|
{
|
|
for (int32& MaterialIndex : OutRawMesh.FaceMaterialIndices)
|
|
{
|
|
if (MaterialIndex == Section.MaterialIndex)
|
|
{
|
|
MaterialIndex = MaterialMapIdx;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
void FMeshUtilities::ExtractMeshDataForGeometryCache(FRawMesh& RawMesh, const FMeshBuildSettings& BuildSettings, TArray<FStaticMeshBuildVertex>& OutVertices, TArray<TArray<uint32> >& OutPerSectionIndices, int32 ImportVersion)
|
|
{
|
|
int32 NumWedges = RawMesh.WedgeIndices.Num();
|
|
|
|
// Figure out if we should recompute normals and tangents. By default generated LODs should not recompute normals
|
|
bool bRecomputeNormals = (BuildSettings.bRecomputeNormals) || RawMesh.WedgeTangentZ.Num() == 0;
|
|
bool bRecomputeTangents = (BuildSettings.bRecomputeTangents) || RawMesh.WedgeTangentX.Num() == 0 || RawMesh.WedgeTangentY.Num() == 0;
|
|
|
|
// Dump normals and tangents if we are recomputing them.
|
|
if (bRecomputeTangents)
|
|
{
|
|
RawMesh.WedgeTangentX.Empty(NumWedges);
|
|
RawMesh.WedgeTangentX.AddZeroed(NumWedges);
|
|
RawMesh.WedgeTangentY.Empty(NumWedges);
|
|
RawMesh.WedgeTangentY.AddZeroed(NumWedges);
|
|
}
|
|
|
|
if (bRecomputeNormals)
|
|
{
|
|
RawMesh.WedgeTangentZ.Empty(NumWedges);
|
|
RawMesh.WedgeTangentZ.AddZeroed(NumWedges);
|
|
}
|
|
|
|
// Compute any missing tangents.
|
|
TMultiMap<int32, int32> OverlappingCorners;
|
|
if (bRecomputeNormals || bRecomputeTangents)
|
|
{
|
|
float ComparisonThreshold = GetComparisonThreshold(BuildSettings);
|
|
FindOverlappingCorners(OverlappingCorners, RawMesh, ComparisonThreshold);
|
|
|
|
// Static meshes always blend normals of overlapping corners.
|
|
uint32 TangentOptions = ETangentOptions::BlendOverlappingNormals;
|
|
if (BuildSettings.bRemoveDegenerates)
|
|
{
|
|
// If removing degenerate triangles, ignore them when computing tangents.
|
|
TangentOptions |= ETangentOptions::IgnoreDegenerateTriangles;
|
|
}
|
|
if (BuildSettings.bUseMikkTSpace)
|
|
{
|
|
ComputeTangents_MikkTSpace(RawMesh, OverlappingCorners, TangentOptions);
|
|
}
|
|
else
|
|
{
|
|
ComputeTangents(RawMesh, OverlappingCorners, TangentOptions);
|
|
}
|
|
}
|
|
|
|
// At this point the mesh will have valid tangents.
|
|
check(RawMesh.WedgeTangentX.Num() == NumWedges);
|
|
check(RawMesh.WedgeTangentY.Num() == NumWedges);
|
|
check(RawMesh.WedgeTangentZ.Num() == NumWedges);
|
|
|
|
TArray<int32> OutWedgeMap;
|
|
|
|
int32 MaxMaterialIndex = 1;
|
|
for (int32 FaceIndex = 0; FaceIndex < RawMesh.FaceMaterialIndices.Num(); FaceIndex++)
|
|
{
|
|
MaxMaterialIndex = FMath::Max<int32>(RawMesh.FaceMaterialIndices[FaceIndex], MaxMaterialIndex);
|
|
}
|
|
|
|
TMap<uint32, uint32> MaterialToSectionMapping;
|
|
for (int32 i = 0; i <= MaxMaterialIndex; ++i)
|
|
{
|
|
OutPerSectionIndices.Push(TArray<uint32>());
|
|
MaterialToSectionMapping.Add(i, i);
|
|
}
|
|
|
|
BuildStaticMeshVertexAndIndexBuffers(OutVertices, OutPerSectionIndices, OutWedgeMap, RawMesh, OverlappingCorners, MaterialToSectionMapping, KINDA_SMALL_NUMBER, BuildSettings.BuildScale3D, ImportVersion);
|
|
|
|
if (RawMesh.WedgeIndices.Num() < 100000 * 3)
|
|
{
|
|
CacheOptimizeVertexAndIndexBuffer(OutVertices, OutPerSectionIndices, OutWedgeMap);
|
|
check(OutWedgeMap.Num() == RawMesh.WedgeIndices.Num());
|
|
}
|
|
}
|
|
|
|
/*------------------------------------------------------------------------------
|
|
Mesh merging
|
|
------------------------------------------------------------------------------*/
|
|
bool FMeshUtilities::PropagatePaintedColorsToRawMesh(const UStaticMeshComponent* StaticMeshComponent, int32 LODIndex, FRawMesh& RawMesh) const
|
|
{
|
|
UStaticMesh* StaticMesh = StaticMeshComponent->GetStaticMesh();
|
|
|
|
if (StaticMesh->SourceModels.IsValidIndex(LODIndex) &&
|
|
StaticMeshComponent->LODData.IsValidIndex(LODIndex) &&
|
|
StaticMeshComponent->LODData[LODIndex].OverrideVertexColors != nullptr)
|
|
{
|
|
FColorVertexBuffer& ColorVertexBuffer = *StaticMeshComponent->LODData[LODIndex].OverrideVertexColors;
|
|
FStaticMeshSourceModel& SrcModel = StaticMesh->SourceModels[LODIndex];
|
|
FStaticMeshRenderData& RenderData = *StaticMesh->RenderData;
|
|
FStaticMeshLODResources& RenderModel = RenderData.LODResources[LODIndex];
|
|
|
|
if (ColorVertexBuffer.GetNumVertices() == RenderModel.GetNumVertices())
|
|
{
|
|
int32 NumWedges = RawMesh.WedgeIndices.Num();
|
|
const bool bUseWedgeMap = RenderData.WedgeMap.Num() > 0 && RenderData.WedgeMap.Num() == NumWedges && !StaticMeshComponent->IsA<USplineMeshComponent>();
|
|
// If we have a wedge map
|
|
if (bUseWedgeMap)
|
|
{
|
|
if (RenderData.WedgeMap.Num() == NumWedges)
|
|
{
|
|
int32 NumExistingColors = RawMesh.WedgeColors.Num();
|
|
if (NumExistingColors < NumWedges)
|
|
{
|
|
RawMesh.WedgeColors.AddUninitialized(NumWedges - NumExistingColors);
|
|
}
|
|
|
|
for (int32 i = 0; i < NumWedges; ++i)
|
|
{
|
|
FColor WedgeColor = FColor::White;
|
|
int32 Index = RenderData.WedgeMap[i];
|
|
if (Index != INDEX_NONE)
|
|
{
|
|
WedgeColor = ColorVertexBuffer.VertexColor(Index);
|
|
}
|
|
|
|
RawMesh.WedgeColors[i] = WedgeColor;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
}
|
|
// No wedge map (this can happen when we poly reduce the LOD for example)
|
|
// Use index buffer directly
|
|
else
|
|
{
|
|
UE_LOG(LogMeshUtilities, Warning, TEXT("{%s} Wedge map size %d is wrong or empty. Expected %d. Falling back on using index buffer for propagating vertex painting"), *StaticMesh->GetName(), RenderData.WedgeMap.Num(), RawMesh.WedgeIndices.Num());
|
|
|
|
RawMesh.WedgeColors.SetNumUninitialized(NumWedges);
|
|
|
|
if (RawMesh.VertexPositions.Num() == ColorVertexBuffer.GetNumVertices())
|
|
{
|
|
for (int32 i = 0; i < NumWedges; ++i)
|
|
{
|
|
FColor WedgeColor = FColor::White;
|
|
uint32 VertIndex = RawMesh.WedgeIndices[i];
|
|
|
|
if (VertIndex < ColorVertexBuffer.GetNumVertices())
|
|
{
|
|
WedgeColor = ColorVertexBuffer.VertexColor(VertIndex);
|
|
}
|
|
RawMesh.WedgeColors[i] = WedgeColor;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
static void TransformPhysicsGeometry(const FTransform& InTransform, FKAggregateGeom& AggGeom)
|
|
{
|
|
FTransform NoScaleInTransform = InTransform;
|
|
NoScaleInTransform.SetScale3D(FVector(1, 1, 1));
|
|
|
|
for (FKSphereElem& Elem : AggGeom.SphereElems)
|
|
{
|
|
FTransform ElemTM = Elem.GetTransform();
|
|
Elem.SetTransform(ElemTM*NoScaleInTransform);
|
|
}
|
|
|
|
for (FKBoxElem& Elem : AggGeom.BoxElems)
|
|
{
|
|
FTransform ElemTM = Elem.GetTransform();
|
|
Elem.SetTransform(ElemTM*NoScaleInTransform);
|
|
}
|
|
|
|
for (FKSphylElem& Elem : AggGeom.SphylElems)
|
|
{
|
|
FTransform ElemTM = Elem.GetTransform();
|
|
Elem.SetTransform(ElemTM*NoScaleInTransform);
|
|
}
|
|
|
|
for (FKConvexElem& Elem : AggGeom.ConvexElems)
|
|
{
|
|
FTransform ElemTM = Elem.GetTransform();
|
|
Elem.SetTransform(ElemTM*InTransform);
|
|
}
|
|
|
|
// seems like all primitives except Convex need separate scaling pass
|
|
const FVector Scale3D = InTransform.GetScale3D();
|
|
if (!Scale3D.Equals(FVector(1.f)))
|
|
{
|
|
const float MinPrimSize = KINDA_SMALL_NUMBER;
|
|
|
|
for (FKSphereElem& Elem : AggGeom.SphereElems)
|
|
{
|
|
Elem.ScaleElem(Scale3D, MinPrimSize);
|
|
}
|
|
|
|
for (FKBoxElem& Elem : AggGeom.BoxElems)
|
|
{
|
|
Elem.ScaleElem(Scale3D, MinPrimSize);
|
|
}
|
|
|
|
for (FKSphylElem& Elem : AggGeom.SphylElems)
|
|
{
|
|
Elem.ScaleElem(Scale3D, MinPrimSize);
|
|
}
|
|
}
|
|
}
|
|
|
|
static void ExtractPhysicsGeometry(UStaticMeshComponent* InMeshComponent, FKAggregateGeom& OutAggGeom)
|
|
{
|
|
UStaticMesh* SrcMesh = InMeshComponent->GetStaticMesh();
|
|
if (SrcMesh == nullptr)
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (!SrcMesh->BodySetup)
|
|
{
|
|
return;
|
|
}
|
|
|
|
OutAggGeom = SrcMesh->BodySetup->AggGeom;
|
|
|
|
// Convert boxes to convex, so they can be sheared
|
|
for (int32 BoxIdx = 0; BoxIdx < OutAggGeom.BoxElems.Num(); BoxIdx++)
|
|
{
|
|
FKConvexElem* NewConvexColl = new(OutAggGeom.ConvexElems) FKConvexElem();
|
|
NewConvexColl->ConvexFromBoxElem(OutAggGeom.BoxElems[BoxIdx]);
|
|
}
|
|
OutAggGeom.BoxElems.Empty();
|
|
|
|
// we are not owner of this stuff
|
|
OutAggGeom.RenderInfo = nullptr;
|
|
for (FKConvexElem& Elem : OutAggGeom.ConvexElems)
|
|
{
|
|
Elem.SetConvexMesh(nullptr);
|
|
Elem.SetMirroredConvexMesh(nullptr);
|
|
}
|
|
|
|
// Transform geometry to world space
|
|
FTransform CtoM = InMeshComponent->ComponentToWorld;
|
|
TransformPhysicsGeometry(CtoM, OutAggGeom);
|
|
}
|
|
|
|
void FMeshUtilities::CalculateTextureCoordinateBoundsForRawMesh(const FRawMesh& InRawMesh, TArray<FBox2D>& OutBounds) const
|
|
{
|
|
const int32 NumWedges = InRawMesh.WedgeIndices.Num();
|
|
const int32 NumTris = NumWedges / 3;
|
|
|
|
OutBounds.Empty();
|
|
int32 WedgeIndex = 0;
|
|
for (int32 TriIndex = 0; TriIndex < NumTris; TriIndex++)
|
|
{
|
|
int MaterialIndex = InRawMesh.FaceMaterialIndices[TriIndex];
|
|
if (OutBounds.Num() <= MaterialIndex)
|
|
OutBounds.SetNumZeroed(MaterialIndex + 1);
|
|
{
|
|
int32 CachedWedgeIndex = WedgeIndex;
|
|
for (int32 UVIndex = 0; UVIndex < MAX_MESH_TEXTURE_COORDS; ++UVIndex)
|
|
{
|
|
WedgeIndex = CachedWedgeIndex;
|
|
if (InRawMesh.WedgeTexCoords[UVIndex].Num())
|
|
{
|
|
for (int32 CornerIndex = 0; CornerIndex < 3; CornerIndex++, WedgeIndex++)
|
|
{
|
|
OutBounds[MaterialIndex] += InRawMesh.WedgeTexCoords[UVIndex][WedgeIndex];
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
void FMeshUtilities::CalculateTextureCoordinateBoundsForSkeletalMesh(const FStaticLODModel& LODModel, TArray<FBox2D>& OutBounds) const
|
|
{
|
|
TArray<FSoftSkinVertex> Vertices;
|
|
FMultiSizeIndexContainerData IndexData;
|
|
LODModel.GetVertices(Vertices);
|
|
LODModel.MultiSizeIndexContainer.GetIndexBufferData(IndexData);
|
|
|
|
#if WITH_APEX_CLOTHING
|
|
const uint32 SectionCount = (uint32)LODModel.NumNonClothingSections();
|
|
#else
|
|
const uint32 SectionCount = LODModel.Sections.Num();
|
|
#endif // #if WITH_APEX_CLOTHING
|
|
|
|
check(OutBounds.Num() != 0);
|
|
|
|
for (uint32 SectionIndex = 0; SectionIndex < SectionCount; ++SectionIndex)
|
|
{
|
|
const FSkelMeshSection& Section = LODModel.Sections[SectionIndex];
|
|
const uint32 FirstIndex = Section.BaseIndex;
|
|
const uint32 LastIndex = FirstIndex + Section.NumTriangles * 3;
|
|
const int32 MaterialIndex = Section.MaterialIndex;
|
|
|
|
if (OutBounds.Num() <= MaterialIndex)
|
|
{
|
|
OutBounds.SetNumZeroed(MaterialIndex + 1);
|
|
}
|
|
|
|
for (uint32 Index = FirstIndex; Index < LastIndex; ++Index)
|
|
{
|
|
uint32 VertexIndex = IndexData.Indices[Index];
|
|
FSoftSkinVertex& Vertex = Vertices[VertexIndex];
|
|
|
|
FVector2D TexCoord = Vertex.UVs[0];
|
|
OutBounds[MaterialIndex] += TexCoord;
|
|
}
|
|
}
|
|
}
|
|
|
|
static void CopyTextureRect(const FColor* Src, const FIntPoint& SrcSize, FColor* Dst, const FIntPoint& DstSize, const FIntPoint& DstPos)
|
|
{
|
|
int32 RowLength = SrcSize.X*sizeof(FColor);
|
|
FColor* RowDst = Dst + DstSize.X*DstPos.Y;
|
|
const FColor* RowSrc = Src;
|
|
|
|
for (int32 RowIdx = 0; RowIdx < SrcSize.Y; ++RowIdx)
|
|
{
|
|
FMemory::Memcpy(RowDst + DstPos.X, RowSrc, RowLength);
|
|
|
|
RowDst += DstSize.X;
|
|
RowSrc += SrcSize.X;
|
|
}
|
|
}
|
|
|
|
static void SetTextureRect(const FColor& ColorValue, const FIntPoint& SrcSize, FColor* Dst, const FIntPoint& DstSize, const FIntPoint& DstPos)
|
|
{
|
|
FColor* RowDst = Dst + DstSize.X*DstPos.Y;
|
|
|
|
for (int32 RowIdx = 0; RowIdx < SrcSize.Y; ++RowIdx)
|
|
{
|
|
for (int32 ColIdx = 0; ColIdx < SrcSize.X; ++ColIdx)
|
|
{
|
|
RowDst[DstPos.X + ColIdx] = ColorValue;
|
|
}
|
|
|
|
RowDst += DstSize.X;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
struct FRawMeshUVTransform
|
|
{
|
|
FVector2D Offset;
|
|
FVector2D Scale;
|
|
|
|
bool IsValid() const
|
|
{
|
|
return (Scale != FVector2D::ZeroVector);
|
|
}
|
|
};
|
|
|
|
static FVector2D GetValidUV(const FVector2D& UV)
|
|
{
|
|
FVector2D NewUV = UV;
|
|
// first make sure they're positive
|
|
if (UV.X < 0.0f)
|
|
{
|
|
NewUV.X = UV.X + FMath::CeilToInt(FMath::Abs(UV.X));
|
|
}
|
|
|
|
if (UV.Y < 0.0f)
|
|
{
|
|
NewUV.Y = UV.Y + FMath::CeilToInt(FMath::Abs(UV.Y));
|
|
}
|
|
|
|
// now make sure they're within [0, 1]
|
|
if (UV.X > 1.0f)
|
|
{
|
|
NewUV.X = FMath::Fmod(NewUV.X, 1.0f);
|
|
}
|
|
|
|
if (UV.Y > 1.0f)
|
|
{
|
|
NewUV.Y = FMath::Fmod(NewUV.Y, 1.0f);
|
|
}
|
|
|
|
return NewUV;
|
|
}
|
|
|
|
static void MergeFlattenedMaterials(TArray<struct FFlattenMaterial>& InMaterialList, FFlattenMaterial& OutMergedMaterial, TArray<FRawMeshUVTransform>& OutUVTransforms)
|
|
{
|
|
OutUVTransforms.Reserve(InMaterialList.Num());
|
|
|
|
// Fill output UV transforms with invalid values
|
|
for (auto Material : InMaterialList)
|
|
{
|
|
|
|
// Invalid UV transform
|
|
FRawMeshUVTransform UVTransform;
|
|
UVTransform.Offset = FVector2D::ZeroVector;
|
|
UVTransform.Scale = FVector2D::ZeroVector;
|
|
OutUVTransforms.Add(UVTransform);
|
|
}
|
|
|
|
int32 AtlasGridSize = FMath::CeilToInt(FMath::Sqrt(InMaterialList.Num()));
|
|
FIntPoint AtlasTextureSize = OutMergedMaterial.GetPropertySize(EFlattenMaterialProperties::Diffuse);
|
|
FIntPoint ExportTextureSize = AtlasTextureSize / AtlasGridSize;
|
|
int32 AtlasNumSamples = AtlasTextureSize.X*AtlasTextureSize.Y;
|
|
|
|
|
|
for (int32 PropertyIndex = 0; PropertyIndex < (int32)EFlattenMaterialProperties::NumFlattenMaterialProperties; ++PropertyIndex)
|
|
{
|
|
EFlattenMaterialProperties Property = (EFlattenMaterialProperties)PropertyIndex;
|
|
if (OutMergedMaterial.ShouldGenerateDataForProperty(Property))
|
|
{
|
|
check(OutMergedMaterial.GetPropertySize(Property) == AtlasTextureSize);
|
|
TArray<FColor>& Samples = OutMergedMaterial.GetPropertySamples(Property);
|
|
Samples.SetNumZeroed(AtlasNumSamples);
|
|
}
|
|
}
|
|
|
|
int32 AtlasRowIdx = 0;
|
|
int32 AtlasColIdx = 0;
|
|
FIntPoint AtlasTargetPos = FIntPoint(0, 0);
|
|
|
|
bool bSamplesWritten[(uint32)EFlattenMaterialProperties::NumFlattenMaterialProperties];
|
|
FMemory::Memset(bSamplesWritten, 0);
|
|
|
|
// Flatten all materials and merge them into one material using texture atlases
|
|
for (int32 MatIdx = 0; MatIdx < InMaterialList.Num(); ++MatIdx)
|
|
{
|
|
FFlattenMaterial& FlatMaterial = InMaterialList[MatIdx];
|
|
for (int32 PropertyIndex = 0; PropertyIndex < (int32)EFlattenMaterialProperties::NumFlattenMaterialProperties; ++PropertyIndex)
|
|
{
|
|
EFlattenMaterialProperties Property = (EFlattenMaterialProperties)PropertyIndex;
|
|
if (OutMergedMaterial.ShouldGenerateDataForProperty(Property) && FlatMaterial.DoesPropertyContainData(Property))
|
|
{
|
|
TArray<FColor>& SourceSamples = FlatMaterial.GetPropertySamples(Property);
|
|
TArray<FColor>& TargetSamples = OutMergedMaterial.GetPropertySamples(Property);
|
|
if (FlatMaterial.IsPropertyConstant(Property))
|
|
{
|
|
SetTextureRect(SourceSamples[0], ExportTextureSize, TargetSamples.GetData(), AtlasTextureSize, AtlasTargetPos);
|
|
}
|
|
else
|
|
{
|
|
FIntPoint PropertySize = FlatMaterial.GetPropertySize(Property);
|
|
PropertySize = ConditionalImageResize(PropertySize, ExportTextureSize, SourceSamples, false);
|
|
CopyTextureRect(SourceSamples.GetData(), ExportTextureSize, TargetSamples.GetData(), AtlasTextureSize, AtlasTargetPos);
|
|
FlatMaterial.SetPropertySize(Property, PropertySize);
|
|
}
|
|
|
|
bSamplesWritten[PropertyIndex] |= true;
|
|
}
|
|
}
|
|
|
|
check(OutUVTransforms.IsValidIndex(MatIdx));
|
|
|
|
OutUVTransforms[MatIdx].Offset = FVector2D(
|
|
(float)AtlasTargetPos.X / AtlasTextureSize.X,
|
|
(float)AtlasTargetPos.Y / AtlasTextureSize.Y);
|
|
|
|
OutUVTransforms[MatIdx].Scale = FVector2D(
|
|
(float)ExportTextureSize.X / AtlasTextureSize.X,
|
|
(float)ExportTextureSize.Y / AtlasTextureSize.Y);
|
|
|
|
AtlasColIdx++;
|
|
if (AtlasColIdx >= AtlasGridSize)
|
|
{
|
|
AtlasColIdx = 0;
|
|
AtlasRowIdx++;
|
|
}
|
|
|
|
AtlasTargetPos = FIntPoint(AtlasColIdx*ExportTextureSize.X, AtlasRowIdx*ExportTextureSize.Y);
|
|
}
|
|
|
|
// Check if some properties weren't populated with data (which means we can empty them out)
|
|
for (int32 PropertyIndex = 0; PropertyIndex < (int32)EFlattenMaterialProperties::NumFlattenMaterialProperties; ++PropertyIndex)
|
|
{
|
|
if (!bSamplesWritten[PropertyIndex])
|
|
{
|
|
EFlattenMaterialProperties Property = (EFlattenMaterialProperties)PropertyIndex;
|
|
OutMergedMaterial.GetPropertySamples(Property).Empty();
|
|
OutMergedMaterial.SetPropertySize(Property, FIntPoint(0, 0));
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
static void FlattenBinnedMaterials(TArray<struct FFlattenMaterial>& InMaterialList, const TArray<FBox2D>& InMaterialBoxes, FFlattenMaterial& OutMergedMaterial, TArray<FRawMeshUVTransform>& OutUVTransforms)
|
|
{
|
|
OutUVTransforms.Reserve(InMaterialList.Num());
|
|
|
|
// We support merging only for opaque materials
|
|
// Fill output UV transforms with invalid values
|
|
for (auto Material : InMaterialList)
|
|
{
|
|
// Invalid UV transform
|
|
FRawMeshUVTransform UVTransform;
|
|
UVTransform.Offset = FVector2D::ZeroVector;
|
|
UVTransform.Scale = FVector2D::ZeroVector;
|
|
OutUVTransforms.Add(UVTransform);
|
|
}
|
|
|
|
// Merge all material properties
|
|
for (int32 Index = 0; Index < (int32)EFlattenMaterialProperties::NumFlattenMaterialProperties; ++Index)
|
|
{
|
|
const EFlattenMaterialProperties Property = (EFlattenMaterialProperties)Index;
|
|
const FIntPoint& OutTextureSize = OutMergedMaterial.GetPropertySize(Property);
|
|
if (OutTextureSize != FIntPoint::ZeroValue)
|
|
{
|
|
TArray<FColor>& OutSamples = OutMergedMaterial.GetPropertySamples(Property);
|
|
OutSamples.Reserve(OutTextureSize.X * OutTextureSize.Y);
|
|
OutSamples.SetNumZeroed(OutTextureSize.X * OutTextureSize.Y);
|
|
|
|
bool bMaterialsWritten = false;
|
|
for (int32 MaterialIndex = 0; MaterialIndex < InMaterialList.Num(); ++MaterialIndex)
|
|
{
|
|
// Determine output size and offset
|
|
FFlattenMaterial& FlatMaterial = InMaterialList[MaterialIndex];
|
|
|
|
if (FlatMaterial.DoesPropertyContainData(Property))
|
|
{
|
|
FBox2D MaterialBox = InMaterialBoxes[MaterialIndex];
|
|
const FIntPoint& InputSize = FlatMaterial.GetPropertySize(Property);
|
|
TArray<FColor>& InputSamples = FlatMaterial.GetPropertySamples(Property);
|
|
|
|
// Resize material to match output (area) size
|
|
FIntPoint OutputSize = FIntPoint(OutTextureSize.X * MaterialBox.GetSize().X, OutTextureSize.Y * MaterialBox.GetSize().Y);
|
|
ConditionalImageResize(InputSize, OutputSize, InputSamples, false);
|
|
|
|
// Copy material data to the merged 'atlas' texture
|
|
FIntPoint OutputPosition = FIntPoint(OutTextureSize.X * MaterialBox.Min.X, OutTextureSize.Y * MaterialBox.Min.Y);
|
|
CopyTextureRect(InputSamples.GetData(), OutputSize, OutSamples.GetData(), OutTextureSize, OutputPosition);
|
|
|
|
// Set the UV tranforms only once
|
|
if (Index == 0)
|
|
{
|
|
FRawMeshUVTransform& UVTransform = OutUVTransforms[MaterialIndex];
|
|
UVTransform.Offset = MaterialBox.Min;
|
|
UVTransform.Scale = MaterialBox.GetSize();
|
|
}
|
|
|
|
bMaterialsWritten = true;
|
|
}
|
|
}
|
|
|
|
if (!bMaterialsWritten)
|
|
{
|
|
OutSamples.Empty();
|
|
OutMergedMaterial.SetPropertySize(Property, FIntPoint(0, 0));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
void FMeshUtilities::MergeActors(
|
|
const TArray<AActor*>& SourceActors,
|
|
const FMeshMergingSettings& InSettings,
|
|
UPackage* InOuter,
|
|
const FString& InBasePackageName,
|
|
int32 UseLOD, // does not build all LODs but only use this LOD to create base mesh
|
|
TArray<UObject*>& OutAssetsToSync,
|
|
FVector& OutMergedActorLocation,
|
|
bool bSilent) const
|
|
{
|
|
MergeActors(SourceActors, InSettings, InOuter, InBasePackageName, OutAssetsToSync, OutMergedActorLocation, bSilent);
|
|
}
|
|
|
|
void FMeshUtilities::MergeActors(
|
|
const TArray<AActor*>& SourceActors,
|
|
const FMeshMergingSettings& InSettings,
|
|
UPackage* InOuter,
|
|
const FString& InBasePackageName,
|
|
TArray<UObject*>& OutAssetsToSync,
|
|
FVector& OutMergedActorLocation,
|
|
bool bSilent) const
|
|
{
|
|
checkf(SourceActors.Num(), TEXT("No actors supplied for merging"));
|
|
|
|
TArray<UStaticMeshComponent*> ComponentsToMerge;
|
|
ComponentsToMerge.Reserve(SourceActors.Num());
|
|
// Collect static mesh components
|
|
for (AActor* Actor : SourceActors)
|
|
{
|
|
TInlineComponentArray<UStaticMeshComponent*> Components;
|
|
Actor->GetComponents<UStaticMeshComponent>(Components);
|
|
|
|
// Filter out bad components
|
|
for (UStaticMeshComponent* MeshComponent : Components)
|
|
{
|
|
if (MeshComponent->GetStaticMesh() != nullptr &&
|
|
MeshComponent->GetStaticMesh()->SourceModels.Num() > 0)
|
|
{
|
|
ComponentsToMerge.Add(MeshComponent);
|
|
}
|
|
}
|
|
}
|
|
|
|
checkf(SourceActors.Num(), TEXT("No valid components found in actors supplied for merging"));
|
|
|
|
UWorld* World = SourceActors[0]->GetWorld();
|
|
checkf(World != nullptr, TEXT("Invalid world retrieved from Actor"));
|
|
const float ScreenSize = TNumericLimits<float>::Max();
|
|
MergeStaticMeshComponents(ComponentsToMerge, World, InSettings, InOuter, InBasePackageName, OutAssetsToSync, OutMergedActorLocation, ScreenSize, bSilent);
|
|
}
|
|
|
|
void FMeshUtilities::MergeStaticMeshComponents(const TArray<UStaticMeshComponent*>& ComponentsToMerge, UWorld* World, const FMeshMergingSettings& InSettings, UPackage* InOuter, const FString& InBasePackageName, TArray<UObject*>& OutAssetsToSync, FVector& OutMergedActorLocation, const float ScreenSize, bool bSilent /*= false*/) const
|
|
{
|
|
FScopedSlowTask SlowTask(100.f, (LOCTEXT("MergeStaticMeshComponents_BuildingMesh", "Merging Static Mesh Components")));
|
|
SlowTask.MakeDialog();
|
|
|
|
TArray<FSectionInfo> UniqueSections;
|
|
TMap<FMeshIdAndLOD, TArray<int32>> MaterialMap;
|
|
TArray<FRawMeshExt> SourceMeshes;
|
|
bool bWithVertexColors[MAX_STATIC_MESH_LODS] = {};
|
|
bool bOcuppiedUVChannels[MAX_STATIC_MESH_LODS][MAX_MESH_TEXTURE_COORDS] = {};
|
|
UBodySetup* BodySetupSource = nullptr;
|
|
|
|
checkf(ComponentsToMerge.Num(), TEXT("No valid components supplied for merging"));
|
|
|
|
SourceMeshes.AddZeroed(ComponentsToMerge.Num());
|
|
|
|
// Use first mesh for naming and pivot
|
|
FString MergedAssetPackageName;
|
|
FVector MergedAssetPivot;
|
|
|
|
int32 NumMaxLOD = 0;
|
|
for (int32 MeshId = 0; MeshId < ComponentsToMerge.Num(); ++MeshId)
|
|
{
|
|
UStaticMeshComponent* MeshComponent = ComponentsToMerge[MeshId];
|
|
|
|
// Determine the maximum number of LOD levels found in the source meshes
|
|
NumMaxLOD = FMath::Max(NumMaxLOD, MeshComponent->GetStaticMesh()->SourceModels.Num());
|
|
|
|
// Save the pivot and asset package name of the first mesh, will later be used for creating merged mesh asset
|
|
if (MeshId == 0)
|
|
{
|
|
// Mesh component pivot point
|
|
MergedAssetPivot = InSettings.bPivotPointAtZero ? FVector::ZeroVector : MeshComponent->ComponentToWorld.GetLocation();
|
|
// Source mesh asset package name
|
|
MergedAssetPackageName = MeshComponent->GetStaticMesh()->GetOutermost()->GetName();
|
|
}
|
|
}
|
|
|
|
// Cap the number of LOD levels to the max
|
|
NumMaxLOD = FMath::Min(NumMaxLOD, MAX_STATIC_MESH_LODS);
|
|
|
|
int32 BaseLODIndex = 0;
|
|
// Are we going to export a single LOD or not
|
|
if (InSettings.LODSelectionType == EMeshLODSelectionType::SpecificLOD && InSettings.SpecificLOD >= 0)
|
|
{
|
|
// Will export only one specified LOD as LOD0 for the merged mesh
|
|
BaseLODIndex = FMath::Max(0, FMath::Min(InSettings.SpecificLOD, MAX_STATIC_MESH_LODS));
|
|
}
|
|
|
|
const bool bMergeAllAvailableLODs = InSettings.LODSelectionType == EMeshLODSelectionType::AllLODs;
|
|
|
|
SlowTask.EnterProgressFrame(10.0f, LOCTEXT("MergeStaticMeshComponents_RetrievingStaticMeshes", "Collecting Source Static Meshes"));
|
|
for (int32 MeshId = 0; MeshId < ComponentsToMerge.Num(); ++MeshId)
|
|
{
|
|
UStaticMeshComponent* StaticMeshComponent = ComponentsToMerge[MeshId];
|
|
|
|
// LOD index will be overridden if the user has chosen to pick it according to the viewing distance
|
|
int32 CalculatedLODIndex = -1;
|
|
if (InSettings.LODSelectionType == EMeshLODSelectionType::CalculateLOD && FMath::IsWithinInclusive(ScreenSize, 0.0f, 1.0f))
|
|
{
|
|
FHierarchicalLODUtilitiesModule& Module = FModuleManager::LoadModuleChecked<FHierarchicalLODUtilitiesModule>("HierarchicalLODUtilities");
|
|
IHierarchicalLODUtilities* Utilities = Module.GetUtilities();
|
|
CalculatedLODIndex = Utilities->GetLODLevelForScreenSize(StaticMeshComponent, ScreenSize);
|
|
}
|
|
SourceMeshes[MeshId].SourceStaticMesh = StaticMeshComponent->GetStaticMesh();
|
|
|
|
// Retrieve the lowest available LOD level from the mesh
|
|
int32 StartLODIndex = InSettings.LODSelectionType == EMeshLODSelectionType::CalculateLOD ? CalculatedLODIndex : FMath::Min(BaseLODIndex, StaticMeshComponent->GetStaticMesh()->SourceModels.Num() - 1);
|
|
int32 EndLODIndex = bMergeAllAvailableLODs ? FMath::Min(StaticMeshComponent->GetStaticMesh()->SourceModels.Num(), MAX_STATIC_MESH_LODS) : StartLODIndex + 1;
|
|
|
|
SourceMeshes[MeshId].MaxLODExport = EndLODIndex - 1;
|
|
|
|
// Set export LOD index if we are exporting one specifically
|
|
SourceMeshes[MeshId].ExportLODIndex = !bMergeAllAvailableLODs ? StartLODIndex : -1;
|
|
|
|
for (int32 LODIndex = StartLODIndex; LODIndex < EndLODIndex; ++LODIndex)
|
|
{
|
|
// Store source static mesh and set LOD export flag
|
|
SourceMeshes[MeshId].SourceStaticMesh = StaticMeshComponent->GetStaticMesh();
|
|
SourceMeshes[MeshId].bShouldExportLOD[LODIndex] = false;
|
|
|
|
TArray<int32> MeshMaterialMap;
|
|
// Retrieve and construct raw mesh from source meshes
|
|
SourceMeshes[MeshId].MeshLODData[LODIndex].RawMesh = new FRawMesh();
|
|
FRawMesh* RawMeshLOD = SourceMeshes[MeshId].MeshLODData[LODIndex].RawMesh;
|
|
if ( ConstructRawMesh(StaticMeshComponent, LODIndex, InSettings.bBakeVertexDataToMesh || InSettings.bUseVertexDataForBakingMaterial, *RawMeshLOD, UniqueSections, MeshMaterialMap))
|
|
{
|
|
// Only flag the lod to be eligible for exporting if we found valid data
|
|
SourceMeshes[MeshId].bShouldExportLOD[LODIndex] = true;
|
|
|
|
// Check if vertex colours should be propagated
|
|
if (InSettings.bBakeVertexDataToMesh)
|
|
{
|
|
// Whether at least one of the meshes has vertex colors
|
|
bWithVertexColors[LODIndex] |= (RawMeshLOD->WedgeColors.Num() != 0);
|
|
}
|
|
|
|
// Which UV channels has data at least in one mesh
|
|
for (int32 ChannelIdx = 0; ChannelIdx < MAX_MESH_TEXTURE_COORDS; ++ChannelIdx)
|
|
{
|
|
bOcuppiedUVChannels[LODIndex][ChannelIdx] |= (RawMeshLOD->WedgeTexCoords[ChannelIdx].Num() != 0) && StaticMeshComponent->GetStaticMesh()->LightMapCoordinateIndex != ChannelIdx;
|
|
}
|
|
|
|
if ( InSettings.bUseLandscapeCulling )
|
|
{
|
|
// Landscape / volume culling
|
|
CullTrianglesFromVolumesAndUnderLandscapes(StaticMeshComponent, *RawMeshLOD);
|
|
|
|
if (!RawMeshLOD->IsValid())
|
|
{
|
|
RawMeshLOD = nullptr;
|
|
SourceMeshes[MeshId].bShouldExportLOD[LODIndex] = false;
|
|
}
|
|
}
|
|
|
|
if (SourceMeshes[MeshId].bShouldExportLOD[LODIndex])
|
|
{
|
|
MaterialMap.Add(FMeshIdAndLOD(MeshId, LODIndex), MeshMaterialMap);
|
|
//CalculateTextureCoordinateBoundsForRawMesh(*SourceMeshes[MeshId].MeshLODData[LODIndex].RawMesh, SourceMeshes[MeshId].MeshLODData[LODIndex].TexCoordBounds);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// Failed to retrieve static meshes/materials cannot merge anything
|
|
if (MaterialMap.Num() == 0)
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (InSettings.bMergePhysicsData)
|
|
{
|
|
for (int32 MeshId = 0; MeshId < ComponentsToMerge.Num(); ++MeshId)
|
|
{
|
|
UStaticMeshComponent* MeshComponent = ComponentsToMerge[MeshId];
|
|
ExtractPhysicsGeometry(MeshComponent, SourceMeshes[MeshId].AggGeom);
|
|
|
|
// We will use first valid BodySetup as a source of physics settings
|
|
if (BodySetupSource == nullptr)
|
|
{
|
|
BodySetupSource = MeshComponent->GetStaticMesh()->BodySetup;
|
|
}
|
|
}
|
|
}
|
|
|
|
const bool bShouldBakeOutMaterials = InSettings.bMergeMaterials && !bMergeAllAvailableLODs;
|
|
SlowTask.EnterProgressFrame(bShouldBakeOutMaterials ? 10.0f : 40.0f, LOCTEXT("MergeStaticMeshComponents_RemapMaterials", "Remapping Duplicate Materials"));
|
|
|
|
// Remap material indices regardless of baking out materials or not (could give a draw call decrease)
|
|
TArray<bool> MeshShouldBakeVertexData;
|
|
MeshShouldBakeVertexData.AddZeroed(SourceMeshes.Num());
|
|
|
|
if (bShouldBakeOutMaterials && InSettings.bUseVertexDataForBakingMaterial)
|
|
{
|
|
// If we have UVs outside of the UV boundaries we should use unique UVs to render out the materials
|
|
CheckWrappingUVs(SourceMeshes, MeshShouldBakeVertexData);
|
|
}
|
|
|
|
TMap<FMeshIdAndLOD, TArray<int32> > NewMaterialMap;
|
|
TArray<FSectionInfo> NewSections;
|
|
FMaterialUtilities::RemapUniqueMaterialIndices(
|
|
UniqueSections,
|
|
SourceMeshes,
|
|
MaterialMap,
|
|
InSettings.MaterialSettings,
|
|
InSettings.bUseVertexDataForBakingMaterial,
|
|
InSettings.bMergeMaterials,
|
|
MeshShouldBakeVertexData,
|
|
NewMaterialMap,
|
|
NewSections);
|
|
// Use shared material data.
|
|
Exchange(MaterialMap, NewMaterialMap);
|
|
Exchange(UniqueSections, NewSections);
|
|
|
|
if (bShouldBakeOutMaterials)
|
|
{
|
|
// Should merge flattened materials into one texture
|
|
SlowTask.EnterProgressFrame(30.0f, LOCTEXT("MergeStaticMeshComponents_BakingDownMaterials", "Rendering out Materials"));
|
|
|
|
// Flatten Materials
|
|
TArray<FFlattenMaterial> FlattenedMaterials;
|
|
TArray<UMaterialInterface*> Materials;
|
|
for (const FSectionInfo& Section : UniqueSections)
|
|
{
|
|
Materials.Push(Section.Material);
|
|
}
|
|
|
|
FlattenMaterialsWithMeshData(Materials, SourceMeshes, MaterialMap, MeshShouldBakeVertexData, InSettings.MaterialSettings, FlattenedMaterials);
|
|
|
|
// Try to optimize materials where possible
|
|
for (FFlattenMaterial& InMaterial : FlattenedMaterials)
|
|
{
|
|
FMaterialUtilities::OptimizeFlattenMaterial(InMaterial);
|
|
}
|
|
|
|
FIntPoint AtlasTextureSize = InSettings.MaterialSettings.TextureSize;
|
|
FFlattenMaterial MergedFlatMaterial;
|
|
MergedFlatMaterial.SetPropertySize(EFlattenMaterialProperties::Diffuse, AtlasTextureSize);
|
|
MergedFlatMaterial.SetPropertySize(EFlattenMaterialProperties::Normal, InSettings.MaterialSettings.bNormalMap ? AtlasTextureSize : FIntPoint::ZeroValue);
|
|
MergedFlatMaterial.SetPropertySize(EFlattenMaterialProperties::Metallic, InSettings.MaterialSettings.bMetallicMap ? AtlasTextureSize : FIntPoint::ZeroValue);
|
|
MergedFlatMaterial.SetPropertySize(EFlattenMaterialProperties::Roughness, InSettings.MaterialSettings.bRoughnessMap ? AtlasTextureSize : FIntPoint::ZeroValue);
|
|
MergedFlatMaterial.SetPropertySize(EFlattenMaterialProperties::Specular, InSettings.MaterialSettings.bSpecularMap ? AtlasTextureSize : FIntPoint::ZeroValue);
|
|
MergedFlatMaterial.SetPropertySize(EFlattenMaterialProperties::Emissive, InSettings.MaterialSettings.bEmissiveMap ? AtlasTextureSize : FIntPoint::ZeroValue);
|
|
MergedFlatMaterial.SetPropertySize(EFlattenMaterialProperties::Opacity, InSettings.MaterialSettings.bOpacityMap ? AtlasTextureSize : FIntPoint::ZeroValue);
|
|
|
|
TArray<FRawMeshUVTransform> UVTransforms;
|
|
|
|
if (InSettings.bUseTextureBinning)
|
|
{
|
|
TArray<float> MaterialImportance;
|
|
FMaterialUtilities::DetermineMaterialImportance(Materials, MaterialImportance);
|
|
TArray<FBox2D> MaterialBoxes;
|
|
FMaterialUtilities::GeneratedBinnedTextureSquares(FVector2D(1.0f, 1.0f), MaterialImportance, MaterialBoxes);
|
|
FlattenBinnedMaterials(FlattenedMaterials, MaterialBoxes, MergedFlatMaterial, UVTransforms);
|
|
}
|
|
else
|
|
{
|
|
MergeFlattenedMaterials(FlattenedMaterials, MergedFlatMaterial, UVTransforms);
|
|
}
|
|
|
|
FMaterialUtilities::OptimizeFlattenMaterial(MergedFlatMaterial);
|
|
|
|
// Adjust UVs and remap material indices
|
|
for (int32 MeshIndex = 0; MeshIndex < SourceMeshes.Num(); ++MeshIndex)
|
|
{
|
|
const int32 LODIndex = SourceMeshes[MeshIndex].ExportLODIndex;
|
|
FRawMesh& RawMesh = *SourceMeshes[MeshIndex].MeshLODData[LODIndex].RawMesh;
|
|
if (RawMesh.VertexPositions.Num())
|
|
{
|
|
const TArray<int32> MaterialIndices = MaterialMap[FMeshIdAndLOD(MeshIndex, LODIndex)];
|
|
|
|
// If we end up in the situation where we have two of the same meshes which require baking vertex data (thus unique UVs), the first one to be found in the array will be used to bake out the material and generate new uvs for it. The other one however will not have the new UVs and thus the baked out material does not match up with its uvs which makes the mesh be UVed incorrectly with the new baked material.
|
|
if (!SourceMeshes[MeshIndex].MeshLODData[LODIndex].NewUVs.Num() && MeshShouldBakeVertexData[MeshIndex])
|
|
{
|
|
// Calculate the max bounds for this raw mesh
|
|
CalculateTextureCoordinateBoundsForRawMesh(*SourceMeshes[MeshIndex].MeshLODData[LODIndex].RawMesh, SourceMeshes[MeshIndex].MeshLODData[LODIndex].TexCoordBounds);
|
|
|
|
// Generate unique UVs
|
|
GenerateUniqueUVsForStaticMesh(*SourceMeshes[MeshIndex].MeshLODData[LODIndex].RawMesh, InSettings.MaterialSettings.TextureSize.GetMax(), SourceMeshes[MeshIndex].MeshLODData[LODIndex].NewUVs);
|
|
}
|
|
|
|
for (int32 UVChannelIdx = 0; UVChannelIdx < MAX_MESH_TEXTURE_COORDS; ++UVChannelIdx)
|
|
{
|
|
// Determine if we should use original or non-overlapping generated UVs
|
|
TArray<FVector2D>& UVs = SourceMeshes[MeshIndex].MeshLODData[LODIndex].NewUVs.Num() ? SourceMeshes[MeshIndex].MeshLODData[LODIndex].NewUVs : RawMesh.WedgeTexCoords[UVChannelIdx];
|
|
if (RawMesh.WedgeTexCoords[UVChannelIdx].Num() > 0)
|
|
{
|
|
int32 UVIdx = 0;
|
|
for (int32 FaceMaterialIndex : RawMesh.FaceMaterialIndices)
|
|
{
|
|
const FRawMeshUVTransform& UVTransform = UVTransforms[MaterialIndices[FaceMaterialIndex]];
|
|
if (UVTransform.IsValid())
|
|
{
|
|
FVector2D UV0 = GetValidUV(UVs[UVIdx + 0]);
|
|
FVector2D UV1 = GetValidUV(UVs[UVIdx + 1]);
|
|
FVector2D UV2 = GetValidUV(UVs[UVIdx + 2]);
|
|
RawMesh.WedgeTexCoords[UVChannelIdx][UVIdx + 0] = UV0 * UVTransform.Scale + UVTransform.Offset;
|
|
RawMesh.WedgeTexCoords[UVChannelIdx][UVIdx + 1] = UV1 * UVTransform.Scale + UVTransform.Offset;
|
|
RawMesh.WedgeTexCoords[UVChannelIdx][UVIdx + 2] = UV2 * UVTransform.Scale + UVTransform.Offset;
|
|
}
|
|
|
|
UVIdx += 3;
|
|
}
|
|
}
|
|
}
|
|
|
|
// Reset material indexes
|
|
for (int32& FaceMaterialIndex : RawMesh.FaceMaterialIndices)
|
|
{
|
|
FaceMaterialIndex = 0;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
break;
|
|
}
|
|
}
|
|
|
|
// Create merged material asset
|
|
FString MaterialAssetName;
|
|
FString MaterialPackageName;
|
|
if (InBasePackageName.IsEmpty())
|
|
{
|
|
MaterialAssetName = TEXT("M_MERGED_") + FPackageName::GetShortName(MergedAssetPackageName);
|
|
MaterialPackageName = FPackageName::GetLongPackagePath(MergedAssetPackageName) + TEXT("/") + MaterialAssetName;
|
|
}
|
|
else
|
|
{
|
|
MaterialAssetName = TEXT("M_") + FPackageName::GetShortName(InBasePackageName);
|
|
MaterialPackageName = FPackageName::GetLongPackagePath(InBasePackageName) + TEXT("/") + MaterialAssetName;
|
|
}
|
|
|
|
UPackage* MaterialPackage = InOuter;
|
|
if (MaterialPackage == nullptr)
|
|
{
|
|
MaterialPackage = CreatePackage(nullptr, *MaterialPackageName);
|
|
check(MaterialPackage);
|
|
MaterialPackage->FullyLoad();
|
|
MaterialPackage->Modify();
|
|
}
|
|
|
|
UMaterialInstanceConstant* MergedMaterial = ProxyMaterialUtilities::CreateProxyMaterialInstance(MaterialPackage, InSettings.MaterialSettings, MergedFlatMaterial, MaterialAssetName, MaterialPackageName, OutAssetsToSync);
|
|
// Set material static lighting usage flag if project has static lighting enabled
|
|
static const auto AllowStaticLightingVar = IConsoleManager::Get().FindTConsoleVariableDataInt(TEXT("r.AllowStaticLighting"));
|
|
const bool bAllowStaticLighting = (!AllowStaticLightingVar || AllowStaticLightingVar->GetValueOnGameThread() != 0);
|
|
if (bAllowStaticLighting)
|
|
{
|
|
MergedMaterial->CheckMaterialUsage(MATUSAGE_StaticLighting);
|
|
}
|
|
|
|
// Only end up with one material so clear array first
|
|
UniqueSections.Empty();
|
|
|
|
FSectionInfo NewSection;
|
|
NewSection.Material = MergedMaterial;
|
|
NewSection.bShadowCastingEnabled = true;
|
|
NewSection.bCollisionEnabled = false;
|
|
|
|
UniqueSections.Add(NewSection);
|
|
}
|
|
|
|
FRawMeshExt MergedMesh;
|
|
FMemory::Memset(&MergedMesh, 0, sizeof(MergedMesh));
|
|
|
|
// Flatten out the occupied UV channel flags, we need this to ensure the same amount of uv sets written out for each mesh
|
|
bool bFlattenedOcuppiedUVChannels[MAX_MESH_TEXTURE_COORDS];
|
|
FMemory::Memset(bFlattenedOcuppiedUVChannels, 0, sizeof(bool) * MAX_MESH_TEXTURE_COORDS);
|
|
bFlattenedOcuppiedUVChannels[0] = true; // Should always have one valid texture coordinate channel
|
|
for (int CoordinateIndex = 0; CoordinateIndex < MAX_MESH_TEXTURE_COORDS; ++CoordinateIndex)
|
|
{
|
|
for (int32 LODIndex = 0; LODIndex < MAX_STATIC_MESH_LODS; ++LODIndex)
|
|
{
|
|
bFlattenedOcuppiedUVChannels[CoordinateIndex] |= bOcuppiedUVChannels[LODIndex][CoordinateIndex];
|
|
}
|
|
}
|
|
|
|
SlowTask.EnterProgressFrame(40.0f, LOCTEXT("MergeStaticMeshComponents_MergingMeshesTogether", "Generating Output Static Mesh"));
|
|
FMeshSectionInfoMap SectionInfoMap;
|
|
int32 MaxExportLODs = bMergeAllAvailableLODs ? NumMaxLOD : 1;
|
|
// Merge meshes into single mesh
|
|
for (int32 SourceMeshIdx = 0; SourceMeshIdx < SourceMeshes.Num(); ++SourceMeshIdx)
|
|
{
|
|
for (int32 TargetLODIndex = 0; TargetLODIndex < MaxExportLODs; ++TargetLODIndex)
|
|
{
|
|
int32 SourceLODIndex = SourceMeshes[SourceMeshIdx].bShouldExportLOD[TargetLODIndex] ? TargetLODIndex : (SourceMeshes[SourceMeshIdx].MaxLODExport);
|
|
|
|
if (!bMergeAllAvailableLODs)
|
|
{
|
|
SourceLODIndex = SourceMeshes[SourceMeshIdx].ExportLODIndex;
|
|
}
|
|
|
|
// Allocate raw meshes where needed
|
|
if (MergedMesh.MeshLODData[TargetLODIndex].RawMesh == nullptr)
|
|
{
|
|
MergedMesh.MeshLODData[TargetLODIndex].RawMesh = new FRawMesh();
|
|
}
|
|
|
|
// Merge vertex data from source mesh list into single mesh
|
|
const FRawMesh& SourceRawMesh = *SourceMeshes[SourceMeshIdx].MeshLODData[SourceLODIndex].RawMesh;
|
|
|
|
if (SourceRawMesh.VertexPositions.Num() == 0)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
const TArray<int32> MaterialIndices = MaterialMap[FMeshIdAndLOD(SourceMeshIdx, SourceLODIndex)];
|
|
check(MaterialIndices.Num() > 0);
|
|
|
|
FRawMesh& TargetRawMesh = *MergedMesh.MeshLODData[TargetLODIndex].RawMesh;
|
|
TargetRawMesh.FaceSmoothingMasks.Append(SourceRawMesh.FaceSmoothingMasks);
|
|
|
|
if (InSettings.bMergeMaterials && !bMergeAllAvailableLODs)
|
|
{
|
|
TargetRawMesh.FaceMaterialIndices.AddZeroed(SourceRawMesh.FaceMaterialIndices.Num());
|
|
}
|
|
else
|
|
{
|
|
for (const int32 Index : SourceRawMesh.FaceMaterialIndices)
|
|
{
|
|
TargetRawMesh.FaceMaterialIndices.Add(MaterialIndices[Index]);
|
|
}
|
|
}
|
|
|
|
int32 IndicesOffset = TargetRawMesh.VertexPositions.Num();
|
|
|
|
for (int32 Index : SourceRawMesh.WedgeIndices)
|
|
{
|
|
TargetRawMesh.WedgeIndices.Add(Index + IndicesOffset);
|
|
}
|
|
|
|
for (FVector VertexPos : SourceRawMesh.VertexPositions)
|
|
{
|
|
TargetRawMesh.VertexPositions.Add(VertexPos - MergedAssetPivot);
|
|
}
|
|
|
|
TargetRawMesh.WedgeTangentX.Append(SourceRawMesh.WedgeTangentX);
|
|
TargetRawMesh.WedgeTangentY.Append(SourceRawMesh.WedgeTangentY);
|
|
TargetRawMesh.WedgeTangentZ.Append(SourceRawMesh.WedgeTangentZ);
|
|
|
|
// Deal with vertex colors
|
|
// Some meshes may have it, in this case merged mesh will be forced to have vertex colors as well
|
|
if (InSettings.bBakeVertexDataToMesh)
|
|
{
|
|
if (bWithVertexColors[SourceLODIndex] && SourceRawMesh.WedgeColors.Num())
|
|
{
|
|
TargetRawMesh.WedgeColors.Append(SourceRawMesh.WedgeColors);
|
|
}
|
|
else
|
|
{
|
|
// In case this source mesh does not have vertex colors, fill target with 0xFF
|
|
int32 ColorsOffset = TargetRawMesh.WedgeColors.Num();
|
|
int32 ColorsNum = SourceRawMesh.WedgeIndices.Num();
|
|
TargetRawMesh.WedgeColors.AddUninitialized(ColorsNum);
|
|
FMemory::Memset(&TargetRawMesh.WedgeColors[ColorsOffset], 0xFF, ColorsNum*TargetRawMesh.WedgeColors.GetTypeSize());
|
|
}
|
|
}
|
|
|
|
|
|
// Merge all other UV channels
|
|
for (int32 ChannelIdx = 0; ChannelIdx < MAX_MESH_TEXTURE_COORDS; ++ChannelIdx)
|
|
{
|
|
// Whether this channel has data
|
|
if (bFlattenedOcuppiedUVChannels[ChannelIdx])
|
|
{
|
|
const TArray<FVector2D>& SourceChannel = SourceRawMesh.WedgeTexCoords[ChannelIdx];
|
|
TArray<FVector2D>& TargetChannel = TargetRawMesh.WedgeTexCoords[ChannelIdx];
|
|
|
|
// Whether source mesh has data in this channel
|
|
if (SourceChannel.Num())
|
|
{
|
|
TargetChannel.Append(SourceChannel);
|
|
}
|
|
else
|
|
{
|
|
// Fill with zero coordinates if source mesh has no data for this channel
|
|
const int32 TexCoordNum = SourceRawMesh.WedgeIndices.Num();
|
|
for (int32 CoordIdx = 0; CoordIdx < TexCoordNum; ++CoordIdx)
|
|
{
|
|
TargetChannel.Add(FVector2D::ZeroVector);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// Populate mesh section map
|
|
for (int32 TargetLODIndex = 0; TargetLODIndex < MaxExportLODs; ++TargetLODIndex)
|
|
{
|
|
TArray<uint32> UniqueMaterialIndices;
|
|
FRawMesh& TargetRawMesh = *MergedMesh.MeshLODData[TargetLODIndex].RawMesh;
|
|
|
|
for (uint32 MaterialIndex : TargetRawMesh.FaceMaterialIndices)
|
|
{
|
|
UniqueMaterialIndices.AddUnique(MaterialIndex);
|
|
}
|
|
|
|
for (int32 Index = 0; Index < UniqueMaterialIndices.Num(); ++Index)
|
|
{
|
|
const uint32 MaterialIndex = UniqueMaterialIndices[Index];
|
|
FSectionInfo StoredSectionInfo = UniqueSections[MaterialIndex];
|
|
|
|
FMeshSectionInfo SectionInfo;
|
|
SectionInfo.bCastShadow = StoredSectionInfo.bShadowCastingEnabled;
|
|
SectionInfo.bEnableCollision = StoredSectionInfo.bCollisionEnabled;
|
|
SectionInfo.MaterialIndex = MaterialIndex;
|
|
SectionInfoMap.Set(TargetLODIndex, Index, SectionInfo);
|
|
}
|
|
}
|
|
|
|
// Transform physics primitives to merged mesh pivot
|
|
if (InSettings.bMergePhysicsData && !MergedAssetPivot.IsZero())
|
|
{
|
|
FTransform PivotTM(-MergedAssetPivot);
|
|
for (auto& SourceMesh : SourceMeshes)
|
|
{
|
|
TransformPhysicsGeometry(PivotTM, SourceMesh.AggGeom);
|
|
}
|
|
}
|
|
|
|
// Compute target lightmap channel for each LOD, by looking at the first empty UV channel
|
|
int32 LightMapUVChannel = InSettings.bGenerateLightMapUV ? -1 : 0;
|
|
if (InSettings.bGenerateLightMapUV)
|
|
{
|
|
for (int32 ChannelIdx = 0; ChannelIdx < MAX_MESH_TEXTURE_COORDS; ++ChannelIdx)
|
|
{
|
|
bool bOccupied = false;
|
|
if (bFlattenedOcuppiedUVChannels[ChannelIdx])
|
|
{
|
|
continue;
|
|
}
|
|
else
|
|
{
|
|
LightMapUVChannel = ChannelIdx;
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (LightMapUVChannel == -1)
|
|
{
|
|
// Output warning message
|
|
UE_LOG(LogMeshUtilities, Log, TEXT("Failed to find available lightmap uv channel"));
|
|
LightMapUVChannel = 0;
|
|
}
|
|
}
|
|
|
|
//
|
|
//Create merged mesh asset
|
|
//
|
|
SlowTask.EnterProgressFrame(10.0f, LOCTEXT("MergeStaticMeshComponents_CreatingAsset", "Creating Output Assets"));
|
|
{
|
|
FString AssetName;
|
|
FString PackageName;
|
|
if (InBasePackageName.IsEmpty())
|
|
{
|
|
AssetName = TEXT("SM_MERGED_") + FPackageName::GetShortName(MergedAssetPackageName);
|
|
PackageName = FPackageName::GetLongPackagePath(MergedAssetPackageName) + TEXT("/") + AssetName;
|
|
}
|
|
else
|
|
{
|
|
AssetName = FPackageName::GetShortName(InBasePackageName);
|
|
PackageName = InBasePackageName;
|
|
}
|
|
|
|
UPackage* Package = InOuter;
|
|
if (Package == nullptr)
|
|
{
|
|
Package = CreatePackage(NULL, *PackageName);
|
|
check(Package);
|
|
Package->FullyLoad();
|
|
Package->Modify();
|
|
}
|
|
|
|
UStaticMesh* StaticMesh = NewObject<UStaticMesh>(Package, *AssetName, RF_Public | RF_Standalone);
|
|
StaticMesh->InitResources();
|
|
|
|
FString OutputPath = StaticMesh->GetPathName();
|
|
|
|
// make sure it has a new lighting guid
|
|
StaticMesh->LightingGuid = FGuid::NewGuid();
|
|
if (InSettings.bGenerateLightMapUV)
|
|
{
|
|
StaticMesh->LightMapResolution = InSettings.TargetLightMapResolution;
|
|
StaticMesh->LightMapCoordinateIndex = LightMapUVChannel;
|
|
}
|
|
|
|
for (int32 LODIndex = 0; LODIndex < NumMaxLOD; ++LODIndex)
|
|
{
|
|
if (MergedMesh.MeshLODData[LODIndex].RawMesh != nullptr)
|
|
{
|
|
FRawMesh& MergedMeshLOD = *MergedMesh.MeshLODData[LODIndex].RawMesh;
|
|
if (MergedMeshLOD.VertexPositions.Num() > 0)
|
|
{
|
|
FStaticMeshSourceModel* SrcModel = new (StaticMesh->SourceModels) FStaticMeshSourceModel();
|
|
/*Don't allow the engine to recalculate normals*/
|
|
SrcModel->BuildSettings.bRecomputeNormals = false;
|
|
SrcModel->BuildSettings.bRecomputeTangents = false;
|
|
SrcModel->BuildSettings.bRemoveDegenerates = false;
|
|
SrcModel->BuildSettings.bUseHighPrecisionTangentBasis = false;
|
|
SrcModel->BuildSettings.bUseFullPrecisionUVs = false;
|
|
SrcModel->BuildSettings.bGenerateLightmapUVs = InSettings.bGenerateLightMapUV;
|
|
SrcModel->BuildSettings.MinLightmapResolution = InSettings.TargetLightMapResolution;
|
|
SrcModel->BuildSettings.SrcLightmapIndex = 0;
|
|
SrcModel->BuildSettings.DstLightmapIndex = LightMapUVChannel;
|
|
|
|
SrcModel->RawMeshBulkData->SaveRawMesh(MergedMeshLOD);
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
// Assign materials
|
|
for (const FSectionInfo& Section : UniqueSections)
|
|
{
|
|
UMaterialInterface* Material = Section.Material;
|
|
if (Material && !Material->IsAsset())
|
|
{
|
|
Material = nullptr; // do not save non-asset materials
|
|
}
|
|
|
|
StaticMesh->StaticMaterials.Add(FStaticMaterial(Material));
|
|
}
|
|
|
|
if (InSettings.bMergePhysicsData)
|
|
{
|
|
StaticMesh->CreateBodySetup();
|
|
if (BodySetupSource)
|
|
{
|
|
StaticMesh->BodySetup->CopyBodyPropertiesFrom(BodySetupSource);
|
|
}
|
|
|
|
StaticMesh->BodySetup->AggGeom = FKAggregateGeom();
|
|
// Copy collision from the source meshes
|
|
for (const FRawMeshExt& SourceMesh : SourceMeshes)
|
|
{
|
|
StaticMesh->BodySetup->AddCollisionFrom(SourceMesh.AggGeom);
|
|
}
|
|
|
|
// Bake rotation into verts of convex hulls, so they scale correctly after rotation
|
|
for (FKConvexElem& ConvexElem : StaticMesh->BodySetup->AggGeom.ConvexElems)
|
|
{
|
|
ConvexElem.BakeTransformToVerts();
|
|
}
|
|
}
|
|
|
|
StaticMesh->SectionInfoMap.CopyFrom(SectionInfoMap);
|
|
|
|
//Set the Imported version before calling the build
|
|
StaticMesh->ImportVersion = EImportStaticMeshVersion::LastVersion;
|
|
|
|
StaticMesh->Build(bSilent);
|
|
StaticMesh->PostEditChange();
|
|
|
|
OutAssetsToSync.Add(StaticMesh);
|
|
OutMergedActorLocation = MergedAssetPivot;
|
|
}
|
|
|
|
for (FRawMeshExt& SourceMesh : SourceMeshes)
|
|
{
|
|
for (FMeshMergeData& Mergedata : SourceMesh.MeshLODData)
|
|
{
|
|
Mergedata.ReleaseData();
|
|
}
|
|
}
|
|
|
|
for (FMeshMergeData& Mergedata : MergedMesh.MeshLODData)
|
|
{
|
|
Mergedata.ReleaseData();
|
|
}
|
|
}
|
|
|
|
void FMeshUtilities::MergeStaticMeshComponents(const TArray<UStaticMeshComponent*>& ComponentsToMerge, UWorld* World, const FMeshMergingSettings& InSettings, UPackage* InOuter, const FString& InBasePackageName, int32 UseLOD, /* does not build all LODs but only use this LOD to create base mesh */ TArray<UObject*>& OutAssetsToSync, FVector& OutMergedActorLocation, const float ScreenAreaSize, bool bSilent /*= false*/) const
|
|
{
|
|
MergeStaticMeshComponents(ComponentsToMerge, World, InSettings, InOuter, InBasePackageName, OutAssetsToSync, OutMergedActorLocation, ScreenAreaSize, bSilent);
|
|
}
|
|
|
|
bool FMeshUtilities::RemoveBonesFromMesh(USkeletalMesh* SkeletalMesh, int32 LODIndex, const TArray<FName>* BoneNamesToRemove) const
|
|
{
|
|
IMeshBoneReductionModule& MeshBoneReductionModule = FModuleManager::Get().LoadModuleChecked<IMeshBoneReductionModule>("MeshBoneReduction");
|
|
IMeshBoneReduction * MeshBoneReductionInterface = MeshBoneReductionModule.GetMeshBoneReductionInterface();
|
|
|
|
return MeshBoneReductionInterface->ReduceBoneCounts(SkeletalMesh, LODIndex, BoneNamesToRemove);
|
|
}
|
|
|
|
/*------------------------------------------------------------------------------
|
|
Mesh reduction .
|
|
------------------------------------------------------------------------------*/
|
|
|
|
IMeshReduction* FMeshUtilities::GetStaticMeshReductionInterface()
|
|
{
|
|
return StaticMeshReduction;
|
|
}
|
|
|
|
IMeshReduction* FMeshUtilities::GetSkeletalMeshReductionInterface()
|
|
{
|
|
return SkeletalMeshReduction;
|
|
}
|
|
|
|
/*------------------------------------------------------------------------------
|
|
Mesh merging.
|
|
------------------------------------------------------------------------------*/
|
|
IMeshMerging* FMeshUtilities::GetMeshMergingInterface()
|
|
{
|
|
return MeshMerging;
|
|
}
|
|
|
|
class FMeshSimplifcationSettingsCustomization : public IDetailCustomization
|
|
{
|
|
public:
|
|
static TSharedRef<IDetailCustomization> MakeInstance()
|
|
{
|
|
return MakeShareable( new FMeshSimplifcationSettingsCustomization );
|
|
}
|
|
|
|
virtual void CustomizeDetails( IDetailLayoutBuilder& DetailBuilder ) override
|
|
{
|
|
MeshReductionModuleProperty = DetailBuilder.GetProperty(GET_MEMBER_NAME_CHECKED(UMeshSimplificationSettings, MeshReductionModuleName));
|
|
|
|
IDetailCategoryBuilder& Category = DetailBuilder.EditCategory(TEXT("General"));
|
|
|
|
IDetailPropertyRow& PropertyRow = Category.AddProperty(MeshReductionModuleProperty);
|
|
|
|
FDetailWidgetRow& WidgetRow = PropertyRow.CustomWidget();
|
|
WidgetRow.NameContent()
|
|
[
|
|
MeshReductionModuleProperty->CreatePropertyNameWidget()
|
|
];
|
|
|
|
WidgetRow.ValueContent()
|
|
.MaxDesiredWidth(0)
|
|
[
|
|
SNew(SComboButton)
|
|
.OnGetMenuContent(this, &FMeshSimplifcationSettingsCustomization::GenerateMeshSimplifierMenu)
|
|
.ContentPadding(FMargin(2.0f, 2.0f))
|
|
.ButtonContent()
|
|
[
|
|
SNew(STextBlock)
|
|
.Font(IDetailLayoutBuilder::GetDetailFont())
|
|
.Text(this, &FMeshSimplifcationSettingsCustomization::GetCurrentMeshSimplifierName)
|
|
]
|
|
];
|
|
}
|
|
|
|
private:
|
|
FText GetCurrentMeshSimplifierName() const
|
|
{
|
|
if(MeshReductionModuleProperty->IsValidHandle())
|
|
{
|
|
FText Name;
|
|
MeshReductionModuleProperty->GetValueAsDisplayText(Name);
|
|
|
|
return Name;
|
|
}
|
|
else
|
|
{
|
|
return LOCTEXT("AutomaticMeshReductionPlugin", "Automatic");
|
|
}
|
|
}
|
|
|
|
TSharedRef<SWidget> GenerateMeshSimplifierMenu() const
|
|
{
|
|
FMenuBuilder MenuBuilder(true, nullptr);
|
|
|
|
TArray<FName> ModuleNames;
|
|
FModuleManager::Get().FindModules(TEXT("*MeshReduction"), ModuleNames);
|
|
|
|
MenuBuilder.BeginSection(NAME_None, LOCTEXT("AvailableReductionPluginsMenuSection", "Available Plugins"));
|
|
if(ModuleNames.Num() > 0)
|
|
{
|
|
for(FName ModuleName : ModuleNames)
|
|
{
|
|
FUIAction UIAction;
|
|
UIAction.ExecuteAction.BindSP(this, &FMeshSimplifcationSettingsCustomization::OnMeshSimplificationModuleChosen, ModuleName);
|
|
UIAction.GetActionCheckState.BindSP(this, &FMeshSimplifcationSettingsCustomization::IsMeshSimplificationModuleChosen, ModuleName);
|
|
|
|
MenuBuilder.AddMenuEntry( FText::FromName(ModuleName), FText::GetEmpty(), FSlateIcon(), UIAction, NAME_None, EUserInterfaceActionType::RadioButton );
|
|
|
|
}
|
|
|
|
MenuBuilder.AddMenuSeparator();
|
|
}
|
|
|
|
FUIAction OpenMarketplaceAction;
|
|
OpenMarketplaceAction.ExecuteAction.BindSP(this, &FMeshSimplifcationSettingsCustomization::OnFindReductionPluginsClicked);
|
|
FSlateIcon Icon = FSlateIcon(FEditorStyle::Get().GetStyleSetName(), "LevelEditor.OpenMarketplace.Menu");
|
|
MenuBuilder.AddMenuEntry( LOCTEXT("FindMoreReductionPluginsLink", "Search the Marketplace"), LOCTEXT("FindMoreReductionPluginsLink_Tooltip", "Opens the Marketplace to find more mesh reduction plugins"), Icon, OpenMarketplaceAction);
|
|
return MenuBuilder.MakeWidget();
|
|
}
|
|
|
|
void OnMeshSimplificationModuleChosen(FName ModuleName)
|
|
{
|
|
if(MeshReductionModuleProperty->IsValidHandle())
|
|
{
|
|
MeshReductionModuleProperty->SetValue(ModuleName);
|
|
}
|
|
}
|
|
|
|
ECheckBoxState IsMeshSimplificationModuleChosen(FName ModuleName)
|
|
{
|
|
if(MeshReductionModuleProperty->IsValidHandle())
|
|
{
|
|
FName CurrentModuleName;
|
|
MeshReductionModuleProperty->GetValue(CurrentModuleName);
|
|
return CurrentModuleName == ModuleName ? ECheckBoxState::Checked : ECheckBoxState::Unchecked;
|
|
}
|
|
|
|
return ECheckBoxState::Unchecked;
|
|
}
|
|
|
|
void OnFindReductionPluginsClicked()
|
|
{
|
|
FString URL;
|
|
FUnrealEdMisc::Get().GetURL(TEXT("MeshSimplificationPluginsURL"), URL);
|
|
|
|
FUnrealEdMisc::Get().OpenMarketplace(URL);
|
|
}
|
|
private:
|
|
TSharedPtr<IPropertyHandle> MeshReductionModuleProperty;
|
|
};
|
|
|
|
/*------------------------------------------------------------------------------
|
|
Module initialization / teardown.
|
|
------------------------------------------------------------------------------*/
|
|
|
|
void FMeshUtilities::StartupModule()
|
|
{
|
|
check(StaticMeshReduction == NULL);
|
|
check(SkeletalMeshReduction == NULL);
|
|
check(MeshMerging == NULL);
|
|
|
|
Processor = new FProxyGenerationProcessor();
|
|
|
|
FPropertyEditorModule& PropertyEditorModule = FModuleManager::Get().LoadModuleChecked<FPropertyEditorModule>("PropertyEditor");
|
|
|
|
PropertyEditorModule.RegisterCustomClassLayout("MeshSimplificationSettings", FOnGetDetailCustomizationInstance::CreateStatic(&FMeshSimplifcationSettingsCustomization::MakeInstance));
|
|
|
|
// This module could be launched very early by static meshes loading before the settings class that stores this value has had a chance to load. Have to read from the config file early in the startup process
|
|
FString MeshReductionModuleName;
|
|
GConfig->GetString(TEXT("/Script/Engine.MeshSimplificationSettings"), TEXT("r.MeshReductionModule"), MeshReductionModuleName, GEngineIni);
|
|
CVarMeshReductionModule->Set(*MeshReductionModuleName);
|
|
|
|
// Initially load the mesh reduction module that was previously saved in the settings
|
|
UpdateMeshReductionModule();
|
|
|
|
{
|
|
TArray<FName> SwarmModuleNames;
|
|
FModuleManager::Get().FindModules(TEXT("*SimplygonSwarm"), SwarmModuleNames);
|
|
|
|
// Look for MeshReduction interface
|
|
|
|
|
|
for (int32 Index = 0; Index < SwarmModuleNames.Num(); Index++)
|
|
{
|
|
IMeshReductionModule& MeshReductionModule = FModuleManager::LoadModuleChecked<IMeshReductionModule>(SwarmModuleNames[Index]);
|
|
|
|
// Look for distributed mesh merging interface
|
|
if (DistributedMeshMerging == NULL)
|
|
{
|
|
DistributedMeshMerging = MeshReductionModule.GetMeshMergingInterface();
|
|
|
|
if (DistributedMeshMerging)
|
|
{
|
|
UE_LOG(LogMeshUtilities, Log, TEXT("Using %s for distributed automatic mesh merging"), *SwarmModuleNames[Index].ToString());
|
|
}
|
|
}
|
|
}
|
|
|
|
if (!StaticMeshReduction)
|
|
{
|
|
UE_LOG(LogMeshUtilities, Log, TEXT("No automatic static mesh reduction module available"));
|
|
}
|
|
if (!SkeletalMeshReduction)
|
|
{
|
|
UE_LOG(LogMeshUtilities, Log, TEXT("No automatic skeletal mesh reduction module available"));
|
|
}
|
|
|
|
if (!MeshMerging)
|
|
{
|
|
UE_LOG(LogMeshUtilities, Log, TEXT("No automatic mesh merging module available"));
|
|
}
|
|
else
|
|
{
|
|
MeshMerging->CompleteDelegate.BindRaw(Processor, &FProxyGenerationProcessor::ProxyGenerationComplete);
|
|
MeshMerging->FailedDelegate.BindRaw(Processor, &FProxyGenerationProcessor::ProxyGenerationFailed);
|
|
}
|
|
|
|
if (!DistributedMeshMerging)
|
|
{
|
|
UE_LOG(LogMeshUtilities, Log, TEXT("No distributed automatic mesh merging module available"));
|
|
}
|
|
else
|
|
{
|
|
DistributedMeshMerging->CompleteDelegate.BindRaw(Processor, &FProxyGenerationProcessor::ProxyGenerationComplete);
|
|
DistributedMeshMerging->FailedDelegate.BindRaw(Processor, &FProxyGenerationProcessor::ProxyGenerationFailed);
|
|
}
|
|
}
|
|
|
|
bDisableTriangleOrderOptimization = (CVarTriangleOrderOptimization.GetValueOnGameThread() == 2);
|
|
|
|
bUsingNvTriStrip = !bDisableTriangleOrderOptimization && (CVarTriangleOrderOptimization.GetValueOnGameThread() == 0);
|
|
|
|
// Construct and cache the version string for the mesh utilities module.
|
|
VersionString = FString::Printf(
|
|
TEXT("%s%s%s"),
|
|
MESH_UTILITIES_VER,
|
|
StaticMeshReduction ? *StaticMeshReduction->GetVersionString() : TEXT(""),
|
|
bUsingNvTriStrip ? TEXT("_NvTriStrip") : TEXT("")
|
|
);
|
|
bUsingSimplygon = VersionString.Contains(TEXT("Simplygon"));
|
|
|
|
// hook up level editor extension for skeletal mesh conversion
|
|
ModuleLoadedDelegateHandle = FModuleManager::Get().OnModulesChanged().AddLambda([this](FName InModuleName, EModuleChangeReason InChangeReason)
|
|
{
|
|
if (InChangeReason == EModuleChangeReason::ModuleLoaded)
|
|
{
|
|
if (InModuleName == "LevelEditor")
|
|
{
|
|
AddLevelViewportMenuExtender();
|
|
}
|
|
else if (InModuleName == "AnimationBlueprintEditor")
|
|
{
|
|
AddAnimationBlueprintEditorToolbarExtender();
|
|
}
|
|
else if (InModuleName == "AnimationEditor")
|
|
{
|
|
AddAnimationEditorToolbarExtender();
|
|
}
|
|
else if (InModuleName == "SkeletalMeshEditor")
|
|
{
|
|
AddSkeletalMeshEditorToolbarExtender();
|
|
}
|
|
else if (InModuleName == "SkeletonEditor")
|
|
{
|
|
AddSkeletonEditorToolbarExtender();
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
void FMeshUtilities::ShutdownModule()
|
|
{
|
|
static const FName PropertyEditorModuleName("PropertyEditor");
|
|
if(FModuleManager::Get().IsModuleLoaded(PropertyEditorModuleName))
|
|
{
|
|
FPropertyEditorModule& PropertyEditorModule = FModuleManager::Get().GetModuleChecked<FPropertyEditorModule>(PropertyEditorModuleName);
|
|
|
|
PropertyEditorModule.UnregisterCustomClassLayout("MeshSimplificationSettings");
|
|
}
|
|
|
|
RemoveLevelViewportMenuExtender();
|
|
RemoveAnimationBlueprintEditorToolbarExtender();
|
|
RemoveAnimationEditorToolbarExtender();
|
|
RemoveSkeletalMeshEditorToolbarExtender();
|
|
RemoveSkeletonEditorToolbarExtender();
|
|
FModuleManager::Get().OnModulesChanged().Remove(ModuleLoadedDelegateHandle);
|
|
StaticMeshReduction = NULL;
|
|
SkeletalMeshReduction = NULL;
|
|
MeshMerging = NULL;
|
|
VersionString.Empty();
|
|
}
|
|
|
|
bool FMeshUtilities::GenerateUniqueUVsForStaticMesh(const FRawMesh& RawMesh, int32 TextureResolution, TArray<FVector2D>& OutTexCoords) const
|
|
{
|
|
// Create a copy of original mesh
|
|
FRawMesh TempMesh = RawMesh;
|
|
|
|
// Find overlapping corners for UV generator. Allow some threshold - this should not produce any error in a case if resulting
|
|
// mesh will not merge these vertices.
|
|
TMultiMap<int32, int32> OverlappingCorners;
|
|
FindOverlappingCorners(OverlappingCorners, RawMesh, THRESH_POINTS_ARE_SAME);
|
|
|
|
// Generate new UVs
|
|
FLayoutUV Packer(&TempMesh, 0, 1, FMath::Clamp(TextureResolution / 4, 32, 512));
|
|
Packer.FindCharts(OverlappingCorners);
|
|
|
|
bool bPackSuccess = Packer.FindBestPacking();
|
|
if (bPackSuccess)
|
|
{
|
|
Packer.CommitPackedUVs();
|
|
// Save generated UVs
|
|
OutTexCoords = TempMesh.WedgeTexCoords[1];
|
|
}
|
|
return bPackSuccess;
|
|
}
|
|
|
|
bool FMeshUtilities::GenerateUniqueUVsForSkeletalMesh(const FStaticLODModel& LODModel, int32 TextureResolution, TArray<FVector2D>& OutTexCoords) const
|
|
{
|
|
// Get easy to use SkeletalMesh data
|
|
TArray<FSoftSkinVertex> Vertices;
|
|
FMultiSizeIndexContainerData IndexData;
|
|
LODModel.GetVertices(Vertices);
|
|
LODModel.MultiSizeIndexContainer.GetIndexBufferData(IndexData);
|
|
|
|
int32 NumCorners = IndexData.Indices.Num();
|
|
|
|
// Generate FRawMesh from FStaticLODModel
|
|
FRawMesh TempMesh;
|
|
TempMesh.WedgeIndices.AddUninitialized(NumCorners);
|
|
TempMesh.WedgeTexCoords[0].AddUninitialized(NumCorners);
|
|
TempMesh.VertexPositions.AddUninitialized(NumCorners);
|
|
|
|
// Prepare vertex to wedge map
|
|
// PrevCorner[i] points to previous corner which shares the same wedge
|
|
TArray<int32> LastWedgeCorner;
|
|
LastWedgeCorner.AddUninitialized(Vertices.Num());
|
|
TArray<int32> PrevCorner;
|
|
PrevCorner.AddUninitialized(NumCorners);
|
|
for (int32 Index = 0; Index < Vertices.Num(); Index++)
|
|
{
|
|
LastWedgeCorner[Index] = -1;
|
|
}
|
|
|
|
for (int32 Index = 0; Index < NumCorners; Index++)
|
|
{
|
|
// Copy static vertex data
|
|
int32 VertexIndex = IndexData.Indices[Index];
|
|
FSoftSkinVertex& Vertex = Vertices[VertexIndex];
|
|
TempMesh.WedgeIndices[Index] = Index; // rudimental data, not really used by FLayoutUV - but array size matters
|
|
TempMesh.WedgeTexCoords[0][Index] = Vertex.UVs[0];
|
|
TempMesh.VertexPositions[Index] = Vertex.Position;
|
|
// Link all corners belonging to a single wedge into list
|
|
int32 PrevCornerIndex = LastWedgeCorner[VertexIndex];
|
|
LastWedgeCorner[VertexIndex] = Index;
|
|
PrevCorner[Index] = PrevCornerIndex;
|
|
}
|
|
|
|
// return GenerateUniqueUVsForStaticMesh(TempMesh, TextureResolution, OutTexCoords);
|
|
|
|
// Build overlapping corners map
|
|
TMultiMap<int32, int32> OverlappingCorners;
|
|
for (int32 Index = 0; Index < NumCorners; Index++)
|
|
{
|
|
int VertexIndex = IndexData.Indices[Index];
|
|
for (int32 CornerIndex = LastWedgeCorner[VertexIndex]; CornerIndex >= 0; CornerIndex = PrevCorner[CornerIndex])
|
|
{
|
|
if (CornerIndex != Index)
|
|
{
|
|
OverlappingCorners.Add(Index, CornerIndex);
|
|
}
|
|
}
|
|
}
|
|
|
|
// Generate new UVs
|
|
FLayoutUV Packer(&TempMesh, 0, 1, FMath::Clamp(TextureResolution / 4, 32, 512));
|
|
Packer.FindCharts(OverlappingCorners);
|
|
|
|
bool bPackSuccess = Packer.FindBestPacking();
|
|
if (bPackSuccess)
|
|
{
|
|
Packer.CommitPackedUVs();
|
|
// Save generated UVs
|
|
OutTexCoords = TempMesh.WedgeTexCoords[1];
|
|
}
|
|
return bPackSuccess;
|
|
}
|
|
|
|
void FMeshUtilities::CalculateTangents(const TArray<FVector>& InVertices, const TArray<uint32>& InIndices, const TArray<FVector2D>& InUVs, const TArray<uint32>& InSmoothingGroupIndices, const uint32 InTangentOptions, TArray<FVector>& OutTangentX, TArray<FVector>& OutTangentY, TArray<FVector>& OutNormals) const
|
|
{
|
|
const float ComparisonThreshold = (InTangentOptions & ETangentOptions::IgnoreDegenerateTriangles ) ? THRESH_POINTS_ARE_SAME : 0.0f;
|
|
|
|
TMultiMap<int32, int32> OverlappingCorners;
|
|
FindOverlappingCorners(OverlappingCorners, InVertices, InIndices, ComparisonThreshold);
|
|
ComputeTangents(InVertices, InIndices, InUVs, InSmoothingGroupIndices, OverlappingCorners, OutTangentX, OutTangentY, OutNormals, InTangentOptions);
|
|
}
|
|
|
|
void FMeshUtilities::AddAnimationBlueprintEditorToolbarExtender()
|
|
{
|
|
IAnimationBlueprintEditorModule& AnimationBlueprintEditorModule = FModuleManager::Get().LoadModuleChecked<IAnimationBlueprintEditorModule>("AnimationBlueprintEditor");
|
|
auto& ToolbarExtenders = AnimationBlueprintEditorModule.GetAllAnimationBlueprintEditorToolbarExtenders();
|
|
|
|
ToolbarExtenders.Add(IAnimationBlueprintEditorModule::FAnimationBlueprintEditorToolbarExtender::CreateRaw(this, &FMeshUtilities::GetAnimationBlueprintEditorToolbarExtender));
|
|
AnimationBlueprintEditorExtenderHandle = ToolbarExtenders.Last().GetHandle();
|
|
}
|
|
|
|
void FMeshUtilities::RemoveAnimationBlueprintEditorToolbarExtender()
|
|
{
|
|
IAnimationBlueprintEditorModule* AnimationBlueprintEditorModule = FModuleManager::Get().GetModulePtr<IAnimationBlueprintEditorModule>("AnimationBlueprintEditor");
|
|
if (AnimationBlueprintEditorModule)
|
|
{
|
|
typedef IAnimationBlueprintEditorModule::FAnimationBlueprintEditorToolbarExtender DelegateType;
|
|
AnimationBlueprintEditorModule->GetAllAnimationBlueprintEditorToolbarExtenders().RemoveAll([=](const DelegateType& In) { return In.GetHandle() == AnimationBlueprintEditorExtenderHandle; });
|
|
}
|
|
}
|
|
|
|
TSharedRef<FExtender> FMeshUtilities::GetAnimationBlueprintEditorToolbarExtender(const TSharedRef<FUICommandList> CommandList, TSharedRef<IAnimationBlueprintEditor> InAnimationBlueprintEditor)
|
|
{
|
|
TSharedRef<FExtender> Extender = MakeShareable(new FExtender);
|
|
|
|
UMeshComponent* MeshComponent = Cast<UMeshComponent>(InAnimationBlueprintEditor->GetPersonaToolkit()->GetPreviewMeshComponent());
|
|
|
|
Extender->AddToolBarExtension(
|
|
"Asset",
|
|
EExtensionHook::After,
|
|
CommandList,
|
|
FToolBarExtensionDelegate::CreateRaw(this, &FMeshUtilities::HandleAddConvertComponentToStaticMeshToToolbar, MeshComponent)
|
|
);
|
|
|
|
return Extender;
|
|
}
|
|
|
|
void FMeshUtilities::AddAnimationEditorToolbarExtender()
|
|
{
|
|
IAnimationEditorModule& AnimationEditorModule = FModuleManager::Get().LoadModuleChecked<IAnimationEditorModule>("AnimationEditor");
|
|
auto& ToolbarExtenders = AnimationEditorModule.GetAllAnimationEditorToolbarExtenders();
|
|
|
|
ToolbarExtenders.Add(IAnimationEditorModule::FAnimationEditorToolbarExtender::CreateRaw(this, &FMeshUtilities::GetAnimationEditorToolbarExtender));
|
|
AnimationEditorExtenderHandle = ToolbarExtenders.Last().GetHandle();
|
|
}
|
|
|
|
void FMeshUtilities::RemoveAnimationEditorToolbarExtender()
|
|
{
|
|
IAnimationEditorModule* AnimationEditorModule = FModuleManager::Get().GetModulePtr<IAnimationEditorModule>("AnimationEditor");
|
|
if (AnimationEditorModule)
|
|
{
|
|
typedef IAnimationEditorModule::FAnimationEditorToolbarExtender DelegateType;
|
|
AnimationEditorModule->GetAllAnimationEditorToolbarExtenders().RemoveAll([=](const DelegateType& In) { return In.GetHandle() == AnimationEditorExtenderHandle; });
|
|
}
|
|
}
|
|
|
|
TSharedRef<FExtender> FMeshUtilities::GetAnimationEditorToolbarExtender(const TSharedRef<FUICommandList> CommandList, TSharedRef<IAnimationEditor> InAnimationEditor)
|
|
{
|
|
TSharedRef<FExtender> Extender = MakeShareable(new FExtender);
|
|
|
|
UMeshComponent* MeshComponent = Cast<UMeshComponent>(InAnimationEditor->GetPersonaToolkit()->GetPreviewMeshComponent());
|
|
|
|
Extender->AddToolBarExtension(
|
|
"Asset",
|
|
EExtensionHook::After,
|
|
CommandList,
|
|
FToolBarExtensionDelegate::CreateRaw(this, &FMeshUtilities::HandleAddConvertComponentToStaticMeshToToolbar, MeshComponent)
|
|
);
|
|
|
|
return Extender;
|
|
}
|
|
|
|
void FMeshUtilities::AddSkeletalMeshEditorToolbarExtender()
|
|
{
|
|
ISkeletalMeshEditorModule& SkeletalMeshEditorModule = FModuleManager::Get().LoadModuleChecked<ISkeletalMeshEditorModule>("SkeletalMeshEditor");
|
|
auto& ToolbarExtenders = SkeletalMeshEditorModule.GetAllSkeletalMeshEditorToolbarExtenders();
|
|
|
|
ToolbarExtenders.Add(ISkeletalMeshEditorModule::FSkeletalMeshEditorToolbarExtender::CreateRaw(this, &FMeshUtilities::GetSkeletalMeshEditorToolbarExtender));
|
|
SkeletalMeshEditorExtenderHandle = ToolbarExtenders.Last().GetHandle();
|
|
}
|
|
|
|
void FMeshUtilities::RemoveSkeletalMeshEditorToolbarExtender()
|
|
{
|
|
ISkeletalMeshEditorModule* SkeletalMeshEditorModule = FModuleManager::Get().GetModulePtr<ISkeletalMeshEditorModule>("SkeletalMeshEditor");
|
|
if (SkeletalMeshEditorModule)
|
|
{
|
|
typedef ISkeletalMeshEditorModule::FSkeletalMeshEditorToolbarExtender DelegateType;
|
|
SkeletalMeshEditorModule->GetAllSkeletalMeshEditorToolbarExtenders().RemoveAll([=](const DelegateType& In) { return In.GetHandle() == SkeletalMeshEditorExtenderHandle; });
|
|
}
|
|
}
|
|
|
|
TSharedRef<FExtender> FMeshUtilities::GetSkeletalMeshEditorToolbarExtender(const TSharedRef<FUICommandList> CommandList, TSharedRef<ISkeletalMeshEditor> InSkeletalMeshEditor)
|
|
{
|
|
TSharedRef<FExtender> Extender = MakeShareable(new FExtender);
|
|
|
|
UMeshComponent* MeshComponent = Cast<UMeshComponent>(InSkeletalMeshEditor->GetPersonaToolkit()->GetPreviewMeshComponent());
|
|
|
|
Extender->AddToolBarExtension(
|
|
"Asset",
|
|
EExtensionHook::After,
|
|
CommandList,
|
|
FToolBarExtensionDelegate::CreateRaw(this, &FMeshUtilities::HandleAddConvertComponentToStaticMeshToToolbar, MeshComponent)
|
|
);
|
|
|
|
return Extender;
|
|
}
|
|
|
|
void FMeshUtilities::AddSkeletonEditorToolbarExtender()
|
|
{
|
|
ISkeletonEditorModule& SkeletonEditorModule = FModuleManager::Get().LoadModuleChecked<ISkeletonEditorModule>("SkeletonEditor");
|
|
auto& ToolbarExtenders = SkeletonEditorModule.GetAllSkeletonEditorToolbarExtenders();
|
|
|
|
ToolbarExtenders.Add(ISkeletonEditorModule::FSkeletonEditorToolbarExtender::CreateRaw(this, &FMeshUtilities::GetSkeletonEditorToolbarExtender));
|
|
SkeletonEditorExtenderHandle = ToolbarExtenders.Last().GetHandle();
|
|
}
|
|
|
|
void FMeshUtilities::RemoveSkeletonEditorToolbarExtender()
|
|
{
|
|
ISkeletonEditorModule* SkeletonEditorModule = FModuleManager::Get().GetModulePtr<ISkeletonEditorModule>("SkeletonEditor");
|
|
if (SkeletonEditorModule)
|
|
{
|
|
typedef ISkeletonEditorModule::FSkeletonEditorToolbarExtender DelegateType;
|
|
SkeletonEditorModule->GetAllSkeletonEditorToolbarExtenders().RemoveAll([=](const DelegateType& In) { return In.GetHandle() == SkeletonEditorExtenderHandle; });
|
|
}
|
|
}
|
|
|
|
TSharedRef<FExtender> FMeshUtilities::GetSkeletonEditorToolbarExtender(const TSharedRef<FUICommandList> CommandList, TSharedRef<ISkeletonEditor> InSkeletonEditor)
|
|
{
|
|
TSharedRef<FExtender> Extender = MakeShareable(new FExtender);
|
|
|
|
UMeshComponent* MeshComponent = Cast<UMeshComponent>(InSkeletonEditor->GetPersonaToolkit()->GetPreviewMeshComponent());
|
|
|
|
Extender->AddToolBarExtension(
|
|
"Asset",
|
|
EExtensionHook::After,
|
|
CommandList,
|
|
FToolBarExtensionDelegate::CreateRaw(this, &FMeshUtilities::HandleAddConvertComponentToStaticMeshToToolbar, MeshComponent)
|
|
);
|
|
|
|
return Extender;
|
|
}
|
|
|
|
|
|
void FMeshUtilities::HandleAddConvertComponentToStaticMeshToToolbar(FToolBarBuilder& ParentToolbarBuilder, UMeshComponent* InMeshComponent)
|
|
{
|
|
ParentToolbarBuilder.AddToolBarButton(
|
|
FUIAction(FExecuteAction::CreateLambda([this, InMeshComponent]()
|
|
{
|
|
ConvertMeshesToStaticMesh(TArray<UMeshComponent*>({ InMeshComponent }), InMeshComponent->GetComponentToWorld());
|
|
})),
|
|
NAME_None,
|
|
LOCTEXT("MakeStaticMesh", "Make Static Mesh"),
|
|
LOCTEXT("MakeStaticMeshTooltip", "Make a new static mesh out of the preview's current pose."),
|
|
FSlateIcon("EditorStyle", "Persona.ConvertToStaticMesh")
|
|
);
|
|
}
|
|
|
|
void FMeshUtilities::AddLevelViewportMenuExtender()
|
|
{
|
|
FLevelEditorModule& LevelEditorModule = FModuleManager::Get().LoadModuleChecked<FLevelEditorModule>("LevelEditor");
|
|
auto& MenuExtenders = LevelEditorModule.GetAllLevelViewportContextMenuExtenders();
|
|
|
|
MenuExtenders.Add(FLevelEditorModule::FLevelViewportMenuExtender_SelectedActors::CreateRaw(this, &FMeshUtilities::GetLevelViewportContextMenuExtender));
|
|
LevelViewportExtenderHandle = MenuExtenders.Last().GetHandle();
|
|
}
|
|
|
|
void FMeshUtilities::RemoveLevelViewportMenuExtender()
|
|
{
|
|
if (LevelViewportExtenderHandle.IsValid())
|
|
{
|
|
FLevelEditorModule* LevelEditorModule = FModuleManager::Get().GetModulePtr<FLevelEditorModule>("LevelEditor");
|
|
if (LevelEditorModule)
|
|
{
|
|
typedef FLevelEditorModule::FLevelViewportMenuExtender_SelectedActors DelegateType;
|
|
LevelEditorModule->GetAllLevelViewportContextMenuExtenders().RemoveAll([=](const DelegateType& In) { return In.GetHandle() == LevelViewportExtenderHandle; });
|
|
}
|
|
}
|
|
}
|
|
|
|
/** Util for getting all MeshComponents from a supplied set of Actors */
|
|
void GetSkinnedAndStaticMeshComponentsFromActors(const TArray<AActor*> InActors, TArray<UMeshComponent*>& OutMeshComponents)
|
|
{
|
|
for (AActor* Actor : InActors)
|
|
{
|
|
// add all components from this actor
|
|
TInlineComponentArray<UMeshComponent*> ActorComponents(Actor);
|
|
for (UMeshComponent* ActorComponent : ActorComponents)
|
|
{
|
|
if (ActorComponent->IsA(USkinnedMeshComponent::StaticClass()) || ActorComponent->IsA(UStaticMeshComponent::StaticClass()))
|
|
{
|
|
OutMeshComponents.AddUnique(ActorComponent);
|
|
}
|
|
}
|
|
|
|
// add all attached actors
|
|
TArray<AActor*> AttachedActors;
|
|
Actor->GetAttachedActors(AttachedActors);
|
|
for (AActor* AttachedActor : AttachedActors)
|
|
{
|
|
TInlineComponentArray<UMeshComponent*> AttachedActorComponents(AttachedActor);
|
|
for (UMeshComponent* AttachedActorComponent : AttachedActorComponents)
|
|
{
|
|
if (AttachedActorComponent->IsA(USkinnedMeshComponent::StaticClass()) || AttachedActorComponent->IsA(UStaticMeshComponent::StaticClass()))
|
|
{
|
|
OutMeshComponents.AddUnique(AttachedActorComponent);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
TSharedRef<FExtender> FMeshUtilities::GetLevelViewportContextMenuExtender(const TSharedRef<FUICommandList> CommandList, const TArray<AActor*> InActors)
|
|
{
|
|
TSharedRef<FExtender> Extender = MakeShareable(new FExtender);
|
|
|
|
if (InActors.Num() > 0)
|
|
{
|
|
TArray<UMeshComponent*> Components;
|
|
GetSkinnedAndStaticMeshComponentsFromActors(InActors, Components);
|
|
if (Components.Num() > 0)
|
|
{
|
|
FText ActorName = InActors.Num() == 1 ? FText::Format(LOCTEXT("ActorNameSingular", "\"{0}\""), FText::FromString(InActors[0]->GetActorLabel())) : LOCTEXT("ActorNamePlural", "Actors");
|
|
|
|
FLevelEditorModule& LevelEditor = FModuleManager::GetModuleChecked<FLevelEditorModule>(TEXT("LevelEditor"));
|
|
TSharedRef<FUICommandList> LevelEditorCommandBindings = LevelEditor.GetGlobalLevelEditorActions();
|
|
|
|
Extender->AddMenuExtension("ActorControl", EExtensionHook::After, LevelEditorCommandBindings, FMenuExtensionDelegate::CreateLambda(
|
|
[this, ActorName, InActors](FMenuBuilder& MenuBuilder) {
|
|
|
|
MenuBuilder.AddMenuEntry(
|
|
FText::Format(LOCTEXT("ConvertSelectedActorsToStaticMeshText", "Convert {0} To Static Mesh"), ActorName),
|
|
LOCTEXT("ConvertSelectedActorsToStaticMeshTooltip", "Convert the selected actor's meshes to a new Static Mesh asset. Supports static and skeletal meshes."),
|
|
FSlateIcon(),
|
|
FUIAction(FExecuteAction::CreateRaw(this, &FMeshUtilities::ConvertActorMeshesToStaticMesh, InActors))
|
|
);
|
|
})
|
|
);
|
|
}
|
|
}
|
|
|
|
return Extender;
|
|
}
|
|
|
|
void FMeshUtilities::ConvertActorMeshesToStaticMesh(const TArray<AActor*> InActors)
|
|
{
|
|
TArray<UMeshComponent*> MeshComponents;
|
|
|
|
GetSkinnedAndStaticMeshComponentsFromActors(InActors, MeshComponents);
|
|
|
|
auto GetActorRootTransform = [](AActor* InActor)
|
|
{
|
|
FTransform RootTransform(FTransform::Identity);
|
|
if (ACharacter* Character = Cast<ACharacter>(InActor))
|
|
{
|
|
RootTransform = Character->GetTransform();
|
|
RootTransform.SetLocation(RootTransform.GetLocation() - FVector(0.0f, 0.0f, Character->GetCapsuleComponent()->GetScaledCapsuleHalfHeight()));
|
|
}
|
|
else
|
|
{
|
|
// otherwise just use the actor's origin
|
|
RootTransform = InActor->GetTransform();
|
|
}
|
|
|
|
return RootTransform;
|
|
};
|
|
|
|
// now pick a root transform
|
|
FTransform RootTransform(FTransform::Identity);
|
|
if (InActors.Num() == 1)
|
|
{
|
|
RootTransform = GetActorRootTransform(InActors[0]);
|
|
}
|
|
else
|
|
{
|
|
// multiple actors use the average of their origins, with Z being the min of all origins. Rotation is identity for simplicity
|
|
FVector Location(FVector::ZeroVector);
|
|
float MinZ = FLT_MAX;
|
|
for (AActor* Actor : InActors)
|
|
{
|
|
FTransform ActorTransform(GetActorRootTransform(Actor));
|
|
Location += ActorTransform.GetLocation();
|
|
MinZ = FMath::Min(ActorTransform.GetLocation().Z, MinZ);
|
|
}
|
|
Location /= (float)InActors.Num();
|
|
Location.Z = MinZ;
|
|
|
|
RootTransform.SetLocation(Location);
|
|
}
|
|
|
|
ConvertMeshesToStaticMesh(MeshComponents, RootTransform);
|
|
}
|
|
|
|
#undef LOCTEXT_NAMESPACE
|
|
|
|
|