Skip to content

Commit

Permalink
Copy shader controllers correctly (not children yet)
Browse files Browse the repository at this point in the history
  • Loading branch information
ousnius committed Jul 16, 2015
1 parent 66b0c97 commit a6f14a7
Showing 1 changed file with 54 additions and 40 deletions.
94 changes: 54 additions & 40 deletions NifFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -618,12 +618,6 @@ void NifFile::CopyShader(const string& shapeDest, BSLightingShaderProperty* srcS
dataRef = shape->dataRef;
}

// Get source block info
BSShaderTextureSet* srcTexSet = dynamic_cast<BSShaderTextureSet*>(srcNif.GetBlock(srcShader->textureSetRef));
bool texSetFound = false;
if (srcTexSet)
texSetFound = true;

// Create destination shader block and copy
BSLightingShaderProperty* destShader = new BSLightingShaderProperty((*srcShader));
destShader->header = &hdr;
Expand All @@ -634,38 +628,43 @@ void NifFile::CopyShader(const string& shapeDest, BSLightingShaderProperty* srcS
blocks.push_back(destShader);
hdr.numBlocks++;

// Texture Set
BSShaderTextureSet* destTexSet = nullptr;
int texsetId;

if (texSetFound) {
BSShaderTextureSet* srcTexSet = dynamic_cast<BSShaderTextureSet*>(srcNif.GetBlock(srcShader->textureSetRef));
bool texSetFound = false;
if (srcTexSet) {
// Create texture set block and copy
texSetFound = true;
destTexSet = new BSShaderTextureSet((*srcTexSet));
destTexSet->header = &hdr;

// Add texture block to nif
texsetId = blocks.size();
int texsetId = blocks.size();
blocks.push_back(destTexSet);
hdr.numBlocks++;

// Assign textureset block id to shader
// Assign texture set block id to shader
destShader->textureSetRef = texsetId;
}

// Controller
NiUnknown* destController = nullptr;
NiUnknown* srcController = dynamic_cast<NiUnknown*>(srcNif.GetBlock(srcShader->controllerRef));
bool controllerFound = false;
if (srcController) {
NiUnknown* destController = new NiUnknown(srcController->CalcBlockSize());
controllerFound = true;
destController = new NiUnknown(srcController->CalcBlockSize());
destController->Clone(srcController);
int controllerId = blocks.size();
blocks.push_back(destController);
hdr.numBlocks++;
hdr.blockSizes.push_back(destController->CalcBlockSize());
hdr.blockIndex.push_back(AddOrFindBlockTypeId(srcNif.hdr.blockTypes[srcNif.hdr.blockIndex[srcShader->controllerRef]].str));
destShader->controllerRef = controllerId;
}

(*props1) = shaderId;
if (destShader->IsSkinShader() && hdr.userVersion >= 12) { // Kill normals, set numUVSets to 1

// Kill normals, set numUVSets to 1
if (destShader->IsSkinShader() && hdr.userVersion >= 12) {
if (isStrips) {
NiTriStripsData* stripsData = ((NiTriStripsData*)blocks[dataRef]);
stripsData->hasNormals = 0;
Expand All @@ -686,11 +685,15 @@ void NifFile::CopyShader(const string& shapeDest, BSLightingShaderProperty* srcS
}
}

// Add shader and textureset sizes to block size info
// Add shader and other sizes to block size info
hdr.blockSizes.push_back(destShader->CalcBlockSize());
if (texSetFound) {

if (texSetFound)
hdr.blockSizes.push_back(destTexSet->CalcBlockSize());
}

if (controllerFound)
hdr.blockSizes.push_back(destController->CalcBlockSize());

if (addAlpha) {
NiAlphaProperty* alphaProp = new NiAlphaProperty(hdr);
(*props2) = blocks.size();
Expand All @@ -699,16 +702,20 @@ void NifFile::CopyShader(const string& shapeDest, BSLightingShaderProperty* srcS
hdr.blockSizes.push_back(alphaProp->CalcBlockSize());
}

ushort shaderTypeId = AddOrFindBlockTypeId("BSLightingShaderProperty");
// Record the block type in the block type index.
ushort shaderTypeId = AddOrFindBlockTypeId("BSLightingShaderProperty");
hdr.blockIndex.push_back(shaderTypeId);

if (texSetFound) {
ushort texSetTypeId = AddOrFindBlockTypeId("BSShaderTextureSet");
// Record the block type in the block type index
hdr.blockIndex.push_back(texSetTypeId);
}

if (controllerFound) {
ushort controllerTypeId = AddOrFindBlockTypeId(srcNif.hdr.blockTypes[srcNif.hdr.blockIndex[srcShader->controllerRef]].str);
hdr.blockIndex.push_back(controllerTypeId);
}

if (addAlpha) {
ushort alphaBlockTypeId = AddOrFindBlockTypeId("NiAlphaProperty");
hdr.blockIndex.push_back(alphaBlockTypeId);
Expand Down Expand Up @@ -765,12 +772,6 @@ void NifFile::CopyShaderPP(const string& shapeDest, BSShaderPPLightingProperty*
dataRef = shape->dataRef;
}

// Get source block info
BSShaderTextureSet* srcTexSet = dynamic_cast<BSShaderTextureSet*>(srcNif.GetBlock(srcShader->textureSetRef));
bool texSetFound = false;
if (srcTexSet)
texSetFound = true;

// Create destination shader block and copy
BSShaderPPLightingProperty* destShader = new BSShaderPPLightingProperty((*srcShader));
destShader->header = &hdr;
Expand All @@ -781,38 +782,43 @@ void NifFile::CopyShaderPP(const string& shapeDest, BSShaderPPLightingProperty*
blocks.push_back(destShader);
hdr.numBlocks++;

// Texture Set
BSShaderTextureSet* destTexSet = nullptr;
int texsetId;

if (texSetFound) {
BSShaderTextureSet* srcTexSet = dynamic_cast<BSShaderTextureSet*>(srcNif.GetBlock(srcShader->textureSetRef));
bool texSetFound = false;
if (srcTexSet) {
// Create texture set block and copy
texSetFound = true;
destTexSet = new BSShaderTextureSet((*srcTexSet));
destTexSet->header = &hdr;

// Add texture block to nif
texsetId = blocks.size();
int texsetId = blocks.size();
blocks.push_back(destTexSet);
hdr.numBlocks++;

// Assign textureset block id to shader
// Assign texture set block id to shader
destShader->textureSetRef = texsetId;
}

// Controller
NiUnknown* destController = nullptr;
NiUnknown* srcController = dynamic_cast<NiUnknown*>(srcNif.GetBlock(srcShader->controllerRef));
bool controllerFound = false;
if (srcController) {
NiUnknown* destController = new NiUnknown(srcController->CalcBlockSize());
controllerFound = true;
destController = new NiUnknown(srcController->CalcBlockSize());
destController->Clone(srcController);
int controllerId = blocks.size();
blocks.push_back(destController);
hdr.numBlocks++;
hdr.blockSizes.push_back(destController->CalcBlockSize());
hdr.blockIndex.push_back(AddOrFindBlockTypeId(srcNif.hdr.blockTypes[srcNif.hdr.blockIndex[srcShader->controllerRef]].str));
destShader->controllerRef = controllerId;
}

(*props1) = shaderId;
if (destShader->IsSkinShader() && hdr.userVersion >= 12) { // Kill normals, set numUVSets to 1

// Kill normals, set numUVSets to 1
if (destShader->IsSkinShader() && hdr.userVersion >= 12) {
if (isStrips) {
NiTriStripsData* stripsData = ((NiTriStripsData*)blocks[dataRef]);
stripsData->hasNormals = 0;
Expand All @@ -833,11 +839,15 @@ void NifFile::CopyShaderPP(const string& shapeDest, BSShaderPPLightingProperty*
}
}

// Add shader and textureset sizes to block size info
// Add shader and other sizes to block size info
hdr.blockSizes.push_back(destShader->CalcBlockSize());
if (texSetFound) {

if (texSetFound)
hdr.blockSizes.push_back(destTexSet->CalcBlockSize());
}

if (controllerFound)
hdr.blockSizes.push_back(destController->CalcBlockSize());

if (addAlpha) {
NiAlphaProperty* alphaProp = new NiAlphaProperty(hdr);
(*props2) = blocks.size();
Expand All @@ -846,16 +856,20 @@ void NifFile::CopyShaderPP(const string& shapeDest, BSShaderPPLightingProperty*
hdr.blockSizes.push_back(alphaProp->CalcBlockSize());
}

ushort shaderTypeId = AddOrFindBlockTypeId("BSShaderPPLightingProperty");
// Record the block type in the block type index.
ushort shaderTypeId = AddOrFindBlockTypeId("BSShaderPPLightingProperty");
hdr.blockIndex.push_back(shaderTypeId);

if (texSetFound) {
ushort texSetTypeId = AddOrFindBlockTypeId("BSShaderTextureSet");
// Record the block type in the block type index
hdr.blockIndex.push_back(texSetTypeId);
}

if (controllerFound) {
ushort controllerTypeId = AddOrFindBlockTypeId(srcNif.hdr.blockTypes[srcNif.hdr.blockIndex[srcShader->controllerRef]].str);
hdr.blockIndex.push_back(controllerTypeId);
}

if (addAlpha) {
ushort alphaBlockTypeId = AddOrFindBlockTypeId("NiAlphaProperty");
hdr.blockIndex.push_back(alphaBlockTypeId);
Expand Down

0 comments on commit a6f14a7

Please sign in to comment.