using System.Linq ;
using UnityEngine ;
using UnityEditor ;
using Unity.Collections ;
using Unity.Collections.LowLevel.Unsafe ;
using Unity.Jobs ;
namespace Unity.DemoTeam.DigitalHuman
{
void FittedIndices_SetFromTargetMesh ( object userData )
{
var clip = userData as SkinDeformationClip ;
var iarr = SkinDeformationFitting . GetBlendShapeIndices ( clip . importS ettings. transferTarget ) ;
var iarr = SkinDeformationFitting . GetBlendShapeIndices ( clip . s ettings. transferTarget ) ;
clip . importS ettings. fittedIndices = String . Join ( "," , iarr . Select ( i = > i . ToString ( ) ) . ToArray ( ) ) ;
clip . s ettings. fittedIndices = String . Join ( "," , iarr . Select ( i = > i . ToString ( ) ) . ToArray ( ) ) ;
}
void FittedIndices_SetPrecomputed ( object userData )
0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 1 0 , 1 1 , 1 2 , 1 3 , 1 4 , 1 5 , 1 6 , 1 7 , 1 8 , 1 9 , 2 0 , 2 1 , 2 2 , 2 4 , 2 5 , 2 6 , 2 7 , 2 8 , 2 9 , 3 0 , 3 1 , 3 2 , 3 3 , 3 4 , 3 5 , 3 6 , 3 7 , 3 8 , 3 9 , 4 0 , 4 1 , 4 2 , 4 3 , 4 4 , 4 5 , 4 6 , 4 7 , 4 8 , 4 9 , 5 0 , 5 1 , 5 2 , 5 3 , 5 4 , 5 5 , 5 6 , 5 7 , 5 8 , 5 9 , 6 0 , 6 1 , 6 2 , 6 3 , 6 4 , 6 5 , 6 6 , 6 7 , 6 8 , 6 9 , 7 0 , 7 1 , 7 2 , 7 3 , 7 4 , 7 5 , 7 6 , 7 7 , 7 8 , 7 9 , 8 0 , 8 1 , 8 2 , 8 3 , 8 4 , 8 5 , 8 6 , 8 7 , 8 8 , 8 9 , 9 0 , 9 1 , 9 2 , 9 3 , 9 4 , 9 5 , 9 6 , 9 7 , 9 8 , 9 9 , 1 0 0 , 1 0 1 , 1 0 2 , 1 0 3 , 1 0 4 , 1 0 5 , 1 1 0 , 1 1 1 , 1 1 2 , 1 1 3 , 1 1 4 , 1 1 5 , 1 1 6 , 1 1 7 , 1 1 8 , 1 1 9 , 1 2 0 , 1 2 1 , 1 2 2 , 1 2 3 , 1 2 4 , 1 2 5 , 1 2 6 , 1 2 7 , 1 2 8 , 1 2 9 , 1 3 0 , 1 3 1 , 1 3 2 , 1 3 3 , 1 3 4 , 1 3 5 , 1 3 6 , 1 3 7 , 1 3 8 , 1 3 9 , 1 4 0 , 1 4 1 , 1 4 2 , 1 4 3 , 1 4 4 , 1 4 5 , 1 4 6 , 1 4 7 , 1 4 8 , 1 4 9 , 1 5 0 , 1 5 1 , 1 5 2 , 1 5 3 , 1 5 4 , 1 5 5 , 1 5 6 , 1 5 7 , 1 5 8 , 1 5 9 , 1 6 0 , 1 6 1 , 1 6 2 , 1 6 3 , 1 6 4 , 1 6 5 , 1 6 6 , 1 6 7 , 1 6 8 , 1 6 9 , 1 7 0 , 1 7 1 , 1 7 2 , 1 7 3 , 1 7 4 , 1 7 5 , 1 8 0 , 1 8 1 , 1 8 2 , 1 8 3 , 1 8 4 , 1 8 5 , 1 8 6 , 1 8 7 , 1 8 8 , 1 8 9 , 1 9 0 , 1 9 1 , 1 9 2 , 1 9 3 , 1 9 4 , 1 9 5 , 1 9 6 , 1 9 7 , 1 9 8 , 1 9 9 , 2 0 0 , 2 0 1 , 2 0 2 , 2 0 3 , 2 0 4 , 2 0 5 , 2 0 6 , 2 0 7 , 2 0 8 , 2 0 9 , 2 1 0 , 2 1 1 , 2 1 2 , 2 1 3 , 2 1 4 , 2 1 5 , 2 1 6 , 2 1 7 , 2 1 8 , 2 1 9 , 2 2 0 , 2 2 1 , 2 2 2 , 2 2 3 , 2 2 4 , 2 2 5 , 2 2 6 , 2 2 7 , 2 2 8 , 2 2 9 , 2 3 0 , 2 3 1 , 2 3 2 , 2 3 3 , 2 3 4 , 2 3 5 , 2 3 6 , 2 3 7 , 2 3 8 , 2 3 9 , 2 4 0 , 2 4 1 , 2 4 2 , 2 4 3 , 2 4 4 , 2 4 5 , 2 4 6 , 2 4 7 , 2 4 8 , 2 4 9 , 2 5 0 , 2 5 1 , 2 5 2 , 2 5 3 , 2 5 4 , 2 5 5 , 2 5 6 , 2 5 7 , 2 5 8 , 2 5 9 , 2 6 0 , 2 6 1 , 2 6 2 , 2 6 3 , 2 6 4 , 2 6 5 , 2 6 6 , 2 6 7 , 2 6 8 , 2 6 9 , 2 7 0 , 2 7 1 , 2 7 2 , 2 7 3 , 2 7 4 , 2 7 5 , 2 7 6 , 2 7 7 , 2 7 8 , 2 7 9 , 2 8 0 , 2 8 1 , 2 8 2 , 2 8 3 , 2 8 4 , 2 8 5 , 2 8 6 , 2 8 7 , 2 8 8 , 2 8 9 , 2 9 0 , 2 9 1 , 2 9 2 , 2 9 3 , 2 9 4 , 2 9 5 , 3 0 0 , 3 0 1 , 3 0 2 , 3 0 3 , 3 0 4 , 3 0 5 , 3 0 6 , 3 0 7 , 3 0 8 , 3 0 9 , 3 1 0 , 3 1 1 , 3 1 2 , 3 1 3 , 3 1 4 , 3 1 5 , 3 1 6 , 3 1 7
} ;
clip . importS ettings. fittedIndices = String . Join ( "," , iarr . Select ( i = > i . ToString ( ) ) . ToArray ( ) ) ;
clip . s ettings. fittedIndices = String . Join ( "," , iarr . Select ( i = > i . ToString ( ) ) . ToArray ( ) ) ;
}
void FittedIndices_SetPrecomputedWrinkles ( object userData )
1 , 1 4 , 1 5 , 4 4 , 4 8 , 5 1 , 5 4 , 5 9 , 6 2 , 6 7 , 7 2 , 8 0 , 8 5 , 8 7 , 8 9 , 9 1 , 9 3 , 9 5 , 9 7 , 1 1 0 , 1 1 3 , 1 1 6 , 1 1 8 , 1 1 9 , 1 2 1 , 1 2 3 , 1 2 6 , 1 2 9 , 1 3 2 , 1 3 5 , 1 3 8 , 1 4 1 , 1 4 4 , 1 4 7 , 1 5 0 , 1 5 8 , 1 6 3 , 1 8 1 , 1 8 3 , 1 8 4 , 1 8 7 , 1 9 1 , 1 9 4 , 1 9 7 , 2 0 0 , 2 0 7 , 2 0 9 , 2 1 9 , 2 2 1 , 2 2 7 , 2 2 9 , 2 3 0 , 2 3 1 , 2 3 2 , 2 3 3 , 2 3 5 , 2 3 7 , 2 4 0 , 2 4 1 , 2 5 0 , 2 5 1 , 2 5 2 , 2 5 3 , 2 5 4 , 2 5 5 , 2 5 6 , 2 5 7 , 2 5 8 , 2 5 9 , 2 6 4 , 2 6 5 , 2 9 0 , 2 9 1 , 2 9 2 , 2 9 3 , 2 9 4 , 2 9 5
} ;
clip . importS ettings. fittedIndices = String . Join ( "," , iarr . Select ( i = > i . ToString ( ) ) . ToArray ( ) ) ;
clip . s ettings. fittedIndices = String . Join ( "," , iarr . Select ( i = > i . ToString ( ) ) . ToArray ( ) ) ;
var istr = clip . importS ettings. fittedIndices ;
var istr = clip . s ettings. fittedIndices ;
clip . importS ettings. fittedIndices = String . Join ( "," , iarr . Select ( i = > i . ToString ( ) ) . ToArray ( ) ) ;
clip . s ettings. fittedIndices = String . Join ( "," , iarr . Select ( i = > i . ToString ( ) ) . ToArray ( ) ) ;
var istr = clip . importS ettings. fittedIndices ;
var istr = clip . s ettings. fittedIndices ;
iarr = SkinDeformationFitting . ComputeLinearlyIndependentBlendShapeIndices ( clip . importS ettings. transferTarget , iarr ) ;
iarr = SkinDeformationFitting . ComputeLinearlyIndependentBlendShapeIndices ( clip . s ettings. transferTarget , iarr ) ;
if ( iarr = = null )
{
clip . importS ettings. fittedIndices = String . Join ( "," , iarr . Select ( i = > i . ToString ( ) ) . ToArray ( ) ) ;
clip . s ettings. fittedIndices = String . Join ( "," , iarr . Select ( i = > i . ToString ( ) ) . ToArray ( ) ) ;
}
void OnDisable ( )
{
SkinDeformationClip clip = ( SkinDeformationClip ) target ;
if ( clip . importS ettings. solveRegionPreview )
if ( clip . s ettings. solveRegionPreview )
SkinDeformationClipRegions . Enable ( clip ) ;
else
SkinDeformationClipRegions . Disable ( ) ;
return indices ;
}
static void AssertClipSources ( SkinDeformationClip clip )
{
//TODO validation
}
static SharedJobData sharedJobData ;
struct SharedJobData
{
public string [ ] paths ;
public NativeMeshObjLoader . VertexAttribs vertexAttribs ;
public NativeMeshObjLoader . VertexOrder vertexOrder ;
public NativeMeshSOA [ ] result ;
public bool [ ] resultTouched ;
}
struct ObjLoaderJob : IJobParallelFor
{
public void Execute ( int i )
{
sharedJobData . resultTouched [ i ] = true ;
using ( var sourceObj_i = NativeMeshObjLoader . Parse ( sharedJobData . paths [ i ] , Allocator . Temp , sharedJobData . vertexAttribs , sharedJobData . vertexOrder ) )
{
sharedJobData . result [ i ] . CopyFrom ( sourceObj_i ) ;
}
}
}
var sourceObjsPreloaded = null as NativeMeshSOA [ ] ;
try
{
var progressTitle = "Importing '" + clip . name + "'" ;
var sourceObjPaths = null as string [ ] ;
var sourceMeshAssets = null as Mesh [ ] ;
var sourceAlbedoAssets = null as Texture2D [ ] ;
var referenceObjPath = clip . settings . referenceObjPath ;
var referenceMeshAsset = clip . settings . referenceMeshAsset ;
var referenceIsFirstFrame = clip . settings . referenceIsFirstFrame ;
int frameFaceIndicesCount = 0 ;
var useExternalLoader = ( clip . importSettings . readFrom = = SkinDeformationClip . InputType . ExternalObj ) ;
var useExternalLoader = ( clip . settings . sourceFrom = = SkinDeformationClip . SourceType . ExternalObj ) ;
sourceObjPaths = GetFilesAtPath ( clip . importSettings . externalObjPath , clip . importSettings . externalObjPattern ) ;
Debug . Assert ( sourceObjPaths . Length > 0 , "source .obj count == 0 (check import settings)" ) ;
sourceObjPaths = GetFilesAtPath ( clip . settings . externalObjPath , clip . settings . externalObjPattern ) ;
Debug . Assert ( sourceObjPaths . Length > 0 , "source obj count == 0 (check import settings)" ) ;
if ( referenceIsFirstFrame )
{
referenceObjPath = sourceObjPaths [ 0 ] ;
sourceObjPaths = sourceObjPaths . Skip ( 1 ) . ToArray ( ) ;
}
using ( var nativeMesh = NativeMeshObjLoader . Parse ( sourceObjPaths [ 0 ] ) )
using ( var nativeMesh = NativeMeshObjLoader . Parse ( referenceObjPath ) )
frameFaceIndicesCount = nativeMesh . faceIndicesCount ;
}
if ( clip . settings . externalObjPreloadThreaded )
{
sharedJobData . paths = sourceObjPaths ;
sharedJobData . vertexAttribs = NativeMeshObjLoader . VertexAttribs . Position ; // ignored for now
sharedJobData . vertexOrder = NativeMeshObjLoader . VertexOrder . ByDefinition ;
sharedJobData . result = new NativeMeshSOA [ sourceObjPaths . Length ] ;
sharedJobData . resultTouched = new bool [ sourceObjPaths . Length ] ;
for ( int i = 0 ; i ! = sharedJobData . result . Length ; i + + )
{
sharedJobData . result [ i ] . Allocate ( frameVertexCount , frameFaceIndicesCount , Allocator . Persistent ) ;
}
unsafe
{
fixed ( bool * resultTouched = sharedJobData . resultTouched )
{
var jobDef = new ObjLoaderJob ( ) ;
var job = jobDef . Schedule ( sourceObjPaths . Length , 1 ) ;
JobHandle . ScheduleBatchedJobs ( ) ;
while ( true )
{
int numTotal = sourceObjPaths . Length ;
int numTouched = 0 ;
for ( int i = 0 ; i ! = numTotal ; i + + )
{
if ( resultTouched [ i ] )
{
numTouched + + ;
}
}
EditorUtility . DisplayProgressBar ( progressTitle , "Loading assets (" + numTouched + " / " + numTotal + ")" , ( progressIndex - 1 + ( ( float ) numTouched / numTotal ) ) / progressCount ) ;
if ( job . IsCompleted )
break ;
}
job . Complete ( ) ;
}
}
sourceObjsPreloaded = sharedJobData . result ;
sourceMeshAssets = GetAssetsAtPath < Mesh > ( clip . importSettings . meshAssetPath , clip . importSettings . meshAssetPrefix ) ;
Debug . Assert ( sourceMeshAssets . Length > 0 , "mesh count == 0 (check import settings)" ) ;
sourceMeshAssets = GetAssetsAtPath < Mesh > ( clip . settings . meshAssetFolder , clip . settings . meshAssetPrefix ) ;
Debug . Assert ( sourceMeshAssets . Length > 0 , "source mesh count == 0 (check import settings)" ) ;
sourceAlbedoAssets = GetAssetsAtPath < Texture2D > ( clip . importSettings . albedoAssetPath , clip . importSettings . albedoAssetPrefix ) ;
sourceAlbedoAssets = GetAssetsAtPath < Texture2D > ( clip . settings . albedoAssetFolder , clip . settings . albedoAssetPrefix ) ;
Debug . LogWarning ( "mesh asset count != albedo asset count: skipping albedos" ) ;
Debug . LogWarning ( "source albedo count != mesh count (SKIPPING albedo assets)" ) ;
}
if ( referenceIsFirstFrame )
{
referenceMeshAsset = sourceMeshAssets [ 0 ] ;
sourceMeshAssets = sourceMeshAssets . Skip ( 1 ) . ToArray ( ) ;
if ( sourceAlbedoAssets ! = null )
sourceAlbedoAssets = sourceAlbedoAssets . Skip ( 1 ) . ToArray ( ) ;
Debug . Assert ( referenceMeshAsset ! = null ) ;
frameVertexCount = sourceMeshAssets [ 0 ] . vertexCount ;
frameVertexCount = referenceMeshAsset . vertexCount ;
Debug . Log ( "frameCount: " + frameCount ) ;
Debug . Log ( "frameVertexCount: " + frameVertexCount ) ;
int frameFittedWeightsCount = 0 ; // modified later
var frames = new SkinDeformation [ frameCount ] ;
MeshBuffers buffersFrame0 = new MeshBuffers ( frameVertexCount ) ;
MeshBuffers buffersFrameX = new MeshBuffers ( frameVertexCount ) ;
MeshBuffers buffersTarget = buffersFrame0 ;
if ( clip . importSettings . transferTarget ! = null )
{
buffersTarget = new MeshBuffers ( clip . importSettings . transferTarget ) ;
}
MeshBuffers meshBuffers = new MeshBuffers ( frameVertexCount ) ;
MeshBuffers meshBuffersReference = new MeshBuffers ( frameVertexCount ) ;
MeshAdjacency weldedAdjacency = new MeshAdjacency ( buffersTarget , clip . importSettings . solveWelded ) ;
MeshAdjacency weldedAdjacency = new MeshAdjacency ( meshBuffersReference , clip . settings . solveWelded ) ;
var sourceRotation = Quaternion . Euler ( clip . importSettings . applyRotation ) ;
var sourceScale = clip . importSettings . applyScale ;
var sourceRotation = Quaternion . Euler ( clip . settings . applyRotation ) ;
var sourceScale = clip . settings . applyScaling ;
using ( var nativeMesh = NativeMeshObjLoader . Parse ( sourceObjPaths [ 0 ] ) )
using ( var referenceObj = NativeMeshObjLoader . Parse ( referenceObjPath ) )
buffersFrame0 . LoadFrom ( nativeMesh ) ;
buffersFrame0 . ApplyRotation ( sourceRotation ) ;
buffersFrame0 . ApplyScale ( sourceScale ) ;
meshBuffersReference . LoadFrom ( referenceObj ) ;
meshBuffersReference . ApplyRotation ( sourceRotation ) ;
meshBuffersReference . ApplyScale ( sourceScale ) ;
buffersFrame0 . LoadFrom ( sourceMeshAssets [ 0 ] ) ;
buffersFrame0 . ApplyRotation ( sourceRotation ) ;
buffersFrame0 . ApplyScale ( sourceScale ) ;
meshBuffersReference . LoadFrom ( referenceMeshAsset ) ;
meshBuffersReference . ApplyRotation ( sourceRotation ) ;
meshBuffersReference . ApplyScale ( sourceScale ) ;
var denoiseIndices = ResolveIndexArrayFromVertexSelectionArray ( clip . importSettings . denoiseRegions , weldedAdjacency ) ;
var denoiseFactor = clip . importSettings . denoiseStrength ;
var denoiseIndices = ResolveIndexArrayFromVertexSelectionArray ( clip . settings . denoiseRegions , weldedAdjacency ) ;
var denoiseFactor = clip . settings . denoiseStrength ;
var transplantIndices = ResolveIndexArrayFromVertexSelectionArray ( clip . importSettings . transplantRegions , weldedAdjacency ) ;
var transplantFactor = clip . importSettings . transplantStrength ;
var transplantSource = clip . importSettings . transferTarget ;
if ( transplantFactor < float . Epsilon | | transplantSource = = null )
var transplantIndices = ResolveIndexArrayFromVertexSelectionArray ( clip . settings . transplantRegions , weldedAdjacency ) ;
var transplantFactor = clip . settings . transplantStrength ;
if ( transplantFactor < float . Epsilon )
transplantIndices = new int [ 0 ] ;
#if SOLVE_FULL_LAPLACIAN
#endif
#if SOLVE_FULL_LAPLACIAN
var meshLaplacianTransform = null as MeshLaplacianTransform ;
var laplacianTransform = null as MeshLaplacianTransform ;
var meshLaplacianTransform = null as MeshLaplacianTransformROI ;
var laplacianTransform = null as MeshLaplacianTransformROI ;
var transplantBuffers = new MeshBuffers ( frameVertexCount ) ;
var transplantLaplacian = new MeshLaplacian ( ) ;
var meshLaplacianReference = new MeshLaplacian ( ) ;
meshLaplacianTransform = new MeshLaplacianTransform ( weldedAdjacency , laplacianConstraintIndices ) ;
laplacianTransform = new MeshLaplacianTransform ( weldedAdjacency , laplacianConstraintIndices ) ;
meshLaplacianTransform = new MeshLaplacianTransformROI ( weldedAdjacency , laplacianROIIndices , 0 ) ;
laplacianTransform = new MeshLaplacianTransformROI ( weldedAdjacency , laplacianROIIndices , 0 ) ;
denoiseIndices [ i ] = meshLaplacianTransform . internalFromExternal [ denoiseIndices [ i ] ] ;
denoiseIndices [ i ] = laplacianTransform . internalFromExternal [ denoiseIndices [ i ] ] ;
transplantIndices [ i ] = meshLaplacianTransform . internalFromExternal [ transplantIndices [ i ] ] ;
transplantIndices [ i ] = laplacianTransform . internalFromExternal [ transplantIndices [ i ] ] ;
meshLaplacianTransform . ComputeMeshLaplacian ( meshLaplacianDenoised , buffersFrame0 ) ;
if ( transplantIndices . Length > 0 & & transplantSource ! = null )
{
transplantBuffers . LoadFrom ( transplantSource ) ;
meshLaplacianTransform . ComputeMeshLaplacian ( transplantLaplacian , transplantBuffers ) ;
}
laplacianTransform . ComputeMeshLaplacian ( meshLaplacianDenoised , meshBuffersReference ) ;
laplacianTransform . ComputeMeshLaplacian ( meshLaplacianReference , meshBuffersReference ) ;
EditorUtility . DisplayProgressBar ( progressTitle , "Importing frames" , ( progressIndex - 1 + ( ( float ) i / frameCount ) ) / progressCount ) ;
EditorUtility . DisplayProgressBar ( progressTitle , "Importing frames (" + ( i + 1 ) + " / " + frameCount + ")" , ( progressIndex - 1 + ( ( float ) i / frameCount ) ) / progressCount ) ;
using ( var nativeMesh = NativeMeshObjLoader . Parse ( sourceObjPaths [ i ] ) )
if ( clip . settings . externalObjPreloadThreaded )
buffersFrameX . LoadFrom ( nativeMesh ) ;
buffersFrameX . ApplyRotation ( sourceRotation ) ;
buffersFrameX . ApplyScale ( sourceScale ) ;
meshBuffers . LoadFrom ( sourceObjsPreloaded [ i ] ) ;
}
else using ( var sourceObj_i = NativeMeshObjLoader . Parse ( sourceObjPaths [ i ] ) )
{
meshBuffers . LoadFrom ( sourceObj_i ) ;
meshBuffers . ApplyRotation ( sourceRotation ) ;
meshBuffers . ApplyScale ( sourceScale ) ;
buffersFrameX . LoadFrom ( sourceMeshAssets [ i ] ) ;
buffersFrameX . ApplyRotation ( sourceRotation ) ;
buffersFrameX . ApplyScale ( sourceScale ) ;
meshBuffers . LoadFrom ( sourceMeshAssets [ i ] ) ;
meshBuffers . ApplyRotation ( sourceRotation ) ;
meshBuffers . ApplyScale ( sourceScale ) ;
}
if ( meshBuffers . vertexCount ! = frameVertexCount )
{
Debug . LogWarning ( "frame " + i + " has " + meshBuffers . vertexCount + " vertices (expected " + frameVertexCount + ")" ) ;
meshLaplacianTransform . ComputeMeshLaplacian ( meshLaplacian , buffersFrameX ) ;
laplacianTransform . ComputeMeshLaplacian ( meshLaplacian , meshBuffers ) ;
double historyFactor = denoiseFactor ;
foreach ( int j in denoiseIndices )
foreach ( int j in transplantIndices )
{
meshLaplacian . vertexDifferentialX [ j ] = transplantFactor * transplantLaplacian . vertexDifferentialX [ j ] + ( 1.0 - transplantFactor ) * meshLaplacian . vertexDifferentialX [ j ] ;
meshLaplacian . vertexDifferentialY [ j ] = transplantFactor * transplantLaplacian . vertexDifferentialY [ j ] + ( 1.0 - transplantFactor ) * meshLaplacian . vertexDifferentialY [ j ] ;
meshLaplacian . vertexDifferentialZ [ j ] = transplantFactor * transplantLaplacian . vertexDifferentialZ [ j ] + ( 1.0 - transplantFactor ) * meshLaplacian . vertexDifferentialZ [ j ] ;
meshLaplacian . vertexDifferentialX [ j ] = transplantFactor * meshLaplacianReference . vertexDifferentialX [ j ] + ( 1.0 - transplantFactor ) * meshLaplacian . vertexDifferentialX [ j ] ;
meshLaplacian . vertexDifferentialY [ j ] = transplantFactor * meshLaplacianReference . vertexDifferentialY [ j ] + ( 1.0 - transplantFactor ) * meshLaplacian . vertexDifferentialY [ j ] ;
meshLaplacian . vertexDifferentialZ [ j ] = transplantFactor * meshLaplacianReference . vertexDifferentialZ [ j ] + ( 1.0 - transplantFactor ) * meshLaplacian . vertexDifferentialZ [ j ] ;
meshLaplacianTransform . ResolveMeshBuffers ( buffersFrameX , meshLaplacian ) ;
laplacianTransform . ResolveMeshBuffers ( meshBuffers , meshLaplacian ) ;
buffersFrameX . RecalculateNormals ( weldedAdjacency ) ;
buffersFrameX . ApplyWeldedChanges ( weldedAdjacency ) ;
meshBuffers . RecalculateNormals ( weldedAdjacency ) ;
meshBuffers . ApplyWeldedChanges ( weldedAdjacency ) ;
frames [ i ] . SetAlbedo ( ( sourceAlbedoAssets ! = null ) ? sourceAlbedoAssets [ i ] : null ) ;
frames [ i ] . SetDeltas ( buffersFrame0 , buffersFrameX ) ;
var targetVertexCount = buffersFrame0 . vertexCount ;
if ( targetVertexCount ! = buffersFrameX . vertexCount )
{
Debug . LogWarning ( "frame " + i + " has different vertexCount (" + buffersFrameX . vertexCount + " vs " + targetVertexCount + " in frame 0)" ) ;
}
frames [ i ] . SetAlbedo ( sourceAlbedoAssets ! = null ? sourceAlbedoAssets [ i ] : null ) ;
frames [ i ] . SetDeltas ( meshBuffersReference , meshBuffers ) ;
}
for ( int i = 0 ; i ! = subframeCount ; i + + )
subframes [ i ] . fractionHi = 1.0f ;
}
if ( clip . importSettings . keyframes )
if ( clip . settings . keyframes )
ImportFrameIntervalsFromCSV ( clip . importSettings . keyframesCSV , frameCount - 1 , ref subframeCount , ref subframes ) ;
ImportFrameIntervalsFromCSV ( clip . settings . keyframesCSV , frameCount - 1 , ref subframeCount , ref subframes ) ;
EditorUtility . DisplayProgressBar ( progressTitle , "Retargeting frames" , progressIndex + + / progressCount ) ;
EditorUtility . DisplayProgressBar ( progressTitle , "Transferring frames" , progressIndex + + / progressCount ) ;
if ( clip . settings . transferTarget ! = null )
var targetBuffers = new MeshBuffers ( clip . settings . transferTarget ) ;
var targetVertexCount = targetBuffers . vertexCount ;
var targetVertexResolve = new int [ targetVertexCount ] ;
Debug . LogFormat ( "transferMode: {0}" , clip . settings . transferMode ) ;
Debug . LogFormat ( "targetVertexCount: {0}" , targetVertexCount ) ;
switch ( clip . settings . transferMode )
{
case SkinDeformationClip . ImportSettings . TransferMode . ByVertexIndex :
{
Debug . Assert ( frameVertexCount = = targetVertexCount , "target vertex count does not match reference vertex count" ) ;
for ( int i = 0 ; i ! = targetVertexCount ; i + + )
{
targetVertexResolve [ i ] = i ;
}
}
break ;
case SkinDeformationClip . ImportSettings . TransferMode . ByVertexPosition :
{
var referenceBSP = new KdTree3 ( meshBuffersReference . vertexPositions , meshBuffersReference . vertexCount ) ;
var referenceDelta2Max = 0.0f ;
var referenceDelta2Count = 0 ;
var referenceDelta2Threshold = 1e-10f ;
for ( int i = 0 ; i ! = targetVertexCount ; i + + )
{
targetVertexResolve [ i ] = referenceBSP . FindNearest ( ref targetBuffers . vertexPositions [ i ] ) ;
}
for ( int i = 0 ; i ! = targetVertexCount ; i + + )
{
Vector3 posTarget = targetBuffers . vertexPositions [ i ] ;
Vector3 posReference = meshBuffersReference . vertexPositions [ targetVertexResolve [ i ] ] ;
var delta2 = Vector3 . SqrMagnitude ( posTarget - posReference ) ;
if ( delta2 > referenceDelta2Threshold )
{
referenceDelta2Max = Mathf . Max ( delta2 , referenceDelta2Max ) ;
referenceDelta2Count + + ;
}
}
if ( referenceDelta2Count > 0 )
{
Debug . LogWarning ( "some (" + referenceDelta2Count + ") target vertices were far from reference (max delta: " + referenceDelta2Max + ")" ) ;
}
}
break ;
}
var targetDeltaPositions = new Vector3 [ targetVertexCount ] ;
var targetDeltaNormals = new Vector3 [ targetVertexCount ] ;
for ( int frameIndex = 0 ; frameIndex ! = frameCount ; frameIndex + + )
{
EditorUtility . DisplayProgressBar ( progressTitle , "Transferring frames (" + ( frameIndex + 1 ) + " / " + frameCount + ")" , ( progressIndex - 1 + ( ( float ) frameIndex / frameCount ) ) / progressCount ) ;
for ( int i = 0 ; i ! = targetVertexCount ; i + + )
{
int j = targetVertexResolve [ i ] ; // reference index
targetDeltaPositions [ i ] = frames [ frameIndex ] . deltaPositions [ j ] ;
targetDeltaNormals [ i ] = frames [ frameIndex ] . deltaNormals [ j ] ;
}
ArrayUtils . ResizeChecked ( ref frames [ frameIndex ] . deltaPositions , targetVertexCount ) ;
ArrayUtils . ResizeChecked ( ref frames [ frameIndex ] . deltaNormals , targetVertexCount ) ;
ArrayUtils . CopyChecked ( targetDeltaPositions , ref frames [ frameIndex ] . deltaPositions , targetVertexCount ) ;
ArrayUtils . CopyChecked ( targetDeltaNormals , ref frames [ frameIndex ] . deltaNormals , targetVertexCount ) ;
}
frameVertexCount = targetVertexCount ;
/ *
switch ( clip . importSettings . transferMode )
{
case SkinDeformationClip . TransferMode . PassThrough :
}
break ;
}
* /
if ( clip . settings . transferTarget ! = null )
if ( clip . importSettings . fitToBlendShapes )
frameFittedWeightsCount = clip . importSettings . transferTarget . blendShapeCount ;
if ( clip . settings . fitToBlendShapes )
frameFittedWeightsCount = clip . settings . transferTarget . blendShapeCount ;
else
frameFittedWeightsCount = 0 ;
if ( frameFittedWeightsCount > 0 )
{
var blendShapeIndicesCommaSep = clip . importSettings . fittedIndices ;
var blendShapeIndicesCommaSep = clip . settings . fittedIndices ;
SkinDeformationFitting . FitFramesToBlendShapes ( frames , clip . importSettings . transferTarget , blendShapeIndices , clip . importSettings . fittingMethod , clip . importSettings . fittingParam ) ;
SkinDeformationFitting . FitFramesToBlendShapes ( frames , clip . settings . transferTarget , blendShapeIndices , clip . settings . fittingMethod , clip . settings . fittingParam ) ;
else
{
for ( int i = 0 ; i ! = frameCount ; i + + )
frames [ i ] . fittedWeights = new float [ 0 ] ;
}
clip . lastImport = clip . importSettings . Clone ( ) ;
clip . settingsLastImported = clip . settings . Clone ( ) ;
clip . frameCount = frameCount ;
clip . frameVertexCount = frameVertexCount ;
clip . frameFittedWeightsCount = frameFittedWeightsCount ;
}
finally
{
if ( sourceObjsPreloaded ! = null )
{
for ( int i = 0 ; i ! = sourceObjsPreloaded . Length ; i + + )
{
sourceObjsPreloaded [ i ] . Dispose ( ) ;
}
sourceObjsPreloaded = null ;
}
EditorUtility . ClearProgressBar ( ) ;
}
}