Advertisement
expired6978

Head Export

Jul 5th, 2014
345
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.78 KB | None | 0 0
  1. void SKSETaskExportHead::Run()
  2. {
  3.     if (!m_actor)
  4.         return;
  5.  
  6.     BSFaceGenNiNode * faceNode = m_actor->GetFaceGenNiNode();
  7.     TESNPC * actorBase = DYNAMIC_CAST(m_actor->baseForm, TESForm, TESNPC);
  8.     if (!actorBase)
  9.         return;
  10.  
  11.     if (!faceNode)
  12.         return;
  13.  
  14.     BSFaceGenAnimationData * animationData = m_actor->GetFaceGenAnimationData();
  15.     if (animationData) {
  16.         FaceGen::GetSingleton()->isReset = 0;
  17.         for (UInt32 t = BSFaceGenAnimationData::kKeyframeType_Expression; t <= BSFaceGenAnimationData::kKeyframeType_Phoneme; t++)
  18.         {
  19.             BSFaceGenKeyframeMultiple * keyframe = &animationData->keyFrames[t];
  20.             for (UInt32 i = 0; i < keyframe->count; i++)
  21.                 keyframe->values[i] = 0.0;
  22.             keyframe->isUpdated = 0;
  23.         }
  24.         UpdateModelFace(faceNode);
  25.     }
  26.  
  27.     IFileStream::MakeAllDirs(m_nifPath.data);
  28.  
  29.     BSFadeNode * rootNode = BSFadeNode::Create();
  30.     rootNode->IncRef();
  31.     NiNode * skinnedNode = NiNode::Create(0);
  32.     skinnedNode->m_name = BSFixedString("BSFaceGenNiNodeSkinned").data;
  33.  
  34.     std::map<NiAVObject*, NiAVObject*> boneMap;
  35.  
  36.     for (UInt32 i = 0; i < faceNode->m_children.m_size; i++)
  37.     {
  38.         NiAVObject * object = faceNode->m_children.m_data[i];
  39.         if (NiTriShape * trishape = object->GetAsNiTriShape()) {
  40.             NiTriShapeData * trishapeData = niptr_cast<NiTriShapeData>(trishape->m_spModelData);
  41.             NiTriShapeData * newTrishapeData = NULL;
  42.             if (trishapeData)
  43.                 CALL_MEMBER_FN(trishapeData, DeepCopy)((NiObject **)&newTrishapeData);
  44.  
  45.             NiProperty * trishapeEffect = niptr_cast<NiProperty>(trishape->m_spEffectState);
  46.             NiProperty * newTrishapeEffect = NULL;
  47.             if (trishapeEffect)
  48.                 CALL_MEMBER_FN(trishapeEffect, DeepCopy)((NiObject **)&newTrishapeEffect);
  49.  
  50.             NiProperty * trishapeProperty = niptr_cast<NiProperty>(trishape->m_spPropertyState);
  51.             NiProperty * newTrishapeProperty = NULL;
  52.             if (trishapeProperty)
  53.                 CALL_MEMBER_FN(trishapeProperty, DeepCopy)((NiObject **)&newTrishapeProperty);
  54.  
  55.             NiSkinInstance * skinInstance = niptr_cast<NiSkinInstance>(trishape->m_spSkinInstance);
  56.             NiSkinInstance * newSkinInstance = NULL;
  57.             if (skinInstance) {
  58.                 BSDismemberSkinInstance * dismemberSkinInstance = ni_cast(skinInstance, BSDismemberSkinInstance);
  59.                 if (dismemberSkinInstance) {
  60.                     newSkinInstance = BSDismemberSkinInstance::Create();
  61.                 }
  62.                 else {
  63.                     newSkinInstance = NiSkinInstance::Create();
  64.                 }
  65.  
  66.                 BSDismemberSkinInstance * newDismemberSkinInstance = ni_cast(newSkinInstance, BSDismemberSkinInstance);
  67.                 if (newDismemberSkinInstance) {
  68.                     newDismemberSkinInstance->numPartitions = dismemberSkinInstance->numPartitions;
  69.                     newDismemberSkinInstance->partitionFlags = (UInt32*)FormHeap_Allocate(newDismemberSkinInstance->numPartitions * sizeof(UInt32));
  70.                     memcpy(newDismemberSkinInstance->partitionFlags, dismemberSkinInstance->partitionFlags, newDismemberSkinInstance->numPartitions * sizeof(UInt32));
  71.                 }
  72.  
  73.                 newSkinInstance->m_pkRootParent = skinnedNode;
  74.  
  75.                 UInt32 numBones = 0;
  76.                 NiSkinData * skinData = niptr_cast<NiSkinData>(skinInstance->m_spSkinData);
  77.                 NiSkinData * newSkinData = NULL;
  78.                 if (skinData) {
  79.                     numBones = skinData->m_uiBones;
  80.                     CALL_MEMBER_FN(skinData, DeepCopy)((NiObject **)&newSkinData);
  81.                 }
  82.  
  83.                 NiSkinPartition * skinPartition = niptr_cast<NiSkinPartition>(skinInstance->m_spSkinPartition);
  84.                 NiSkinPartition * newSkinPartition = NULL;
  85.                 if (skinPartition)
  86.                     CALL_MEMBER_FN(skinPartition, DeepCopy)((NiObject **)&newSkinPartition);
  87.  
  88.                 newSkinInstance->m_spSkinData = newSkinData;
  89.                 newSkinInstance->m_spSkinPartition = newSkinPartition;
  90.  
  91.                 // Remap the bones to new NiNode instances
  92.                 if (numBones > 0) {
  93.                     newSkinInstance->m_ppkBones = (NiAVObject**)FormHeap_Allocate(numBones * sizeof(NiAVObject*));
  94.                     for (UInt32 i = 0; i < numBones; i++)
  95.                     {
  96.                         NiAVObject * bone = skinInstance->m_ppkBones[i];
  97.                         auto it = boneMap.find(bone);
  98.                         if (it == boneMap.end()) {
  99.                             NiNode * newBone = NiNode::Create();
  100.                             newBone->m_name = bone->m_name;
  101.                             newBone->m_flags = bone->m_flags;
  102.                             boneMap.insert(std::make_pair(bone, newBone));
  103.                             newSkinInstance->m_ppkBones[i] = newBone;
  104.                         }
  105.                         else
  106.                             newSkinInstance->m_ppkBones[i] = it->second;
  107.                     }
  108.                 }
  109.             }
  110.  
  111.             NiTriShape * newTrishape = NiTriShape::Create(newTrishapeData);
  112.             newTrishape->m_localTransform = trishape->m_localTransform;
  113.             newTrishape->m_name = trishape->m_name;
  114.             memcpy(&newTrishape->unk88, &trishape->unk88, 0x1F);
  115.             newTrishape->SetEffectState(newTrishapeEffect);
  116.             newTrishape->SetPropertyState(newTrishapeProperty);
  117.             newTrishape->SetSkinInstance(newSkinInstance);
  118.  
  119.             auto textureData = GetTextureSetForPartByName(actorBase, newTrishape->m_name);
  120.             if (textureData.first && textureData.second) {
  121.                 BSShaderProperty * shaderProperty = niptr_cast<BSShaderProperty>(newTrishape->m_spEffectState);
  122.                 if (shaderProperty) {
  123.                     if (shaderProperty->GetRTTI() == NiRTTI_BSLightingShaderProperty) {
  124.                         BSLightingShaderProperty * lightingShader = (BSLightingShaderProperty *)shaderProperty;
  125.                         BSLightingShaderMaterial * material = (BSLightingShaderMaterial *)shaderProperty->material;
  126.                         for (UInt32 i = 0; i < BGSTextureSet::kNumTextures; i++)
  127.                             material->textureSet->SetTexturePath(i, textureData.first->textureSet.GetTexturePath(i));
  128.  
  129.                         if (textureData.second->type == BGSHeadPart::kTypeFace)
  130.                             material->textureSet->SetTexturePath(6, m_ddsPath.data);
  131.                     }
  132.                 }
  133.  
  134.                 // Save the previous tint mask
  135.                 BSShaderProperty * originalShaderProperty = niptr_cast<BSShaderProperty>(trishape->m_spEffectState);
  136.                 if (originalShaderProperty) {
  137.                     if (originalShaderProperty->GetRTTI() == NiRTTI_BSLightingShaderProperty) {
  138.                         BSLightingShaderProperty * lightingShader = (BSLightingShaderProperty *)originalShaderProperty;
  139.                         BSLightingShaderMaterial * material = (BSLightingShaderMaterial *)originalShaderProperty->material;
  140.                         if (material->GetShaderType() == BSShaderMaterial::kShaderType_FaceTint) {
  141.                             BSMaskedShaderMaterial * maskedMaterial = (BSMaskedShaderMaterial *)material;
  142.                             SaveRenderedDDS(maskedMaterial->renderedTexture, m_ddsPath.data);
  143.                         }
  144.                     }
  145.                 }
  146.             }
  147.  
  148.             skinnedNode->AttachChild(newTrishape, true);
  149.         }
  150.     }
  151.  
  152.     for (auto & bones : boneMap)
  153.         rootNode->AttachChild(bones.second, true);
  154.  
  155.     rootNode->AttachChild(skinnedNode, true);
  156.  
  157.     UInt8 niStreamMemory[0x5B4];
  158.     memset(niStreamMemory, 0, 0x5B4);
  159.     NiStream * niStream = (NiStream *)niStreamMemory;
  160.     CALL_MEMBER_FN(niStream, ctor)();
  161.     CALL_MEMBER_FN(niStream, AddObject)(rootNode);
  162.     niStream->SavePath(m_nifPath.data);
  163.     CALL_MEMBER_FN(niStream, dtor)();
  164.  
  165.     rootNode->DecRef();
  166.  
  167.     if (animationData) {
  168.         animationData->overrideFlag = 0;
  169.         CALL_MEMBER_FN(animationData, Reset)(1.0, 1, 1, 0, 0);
  170.         FaceGen::GetSingleton()->isReset = 1;
  171.         UpdateModelFace(faceNode);
  172.     }
  173. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement