Merge branch 'AssimpWIP' of https://github.com/Areloch/Torque3D into development

This commit is contained in:
Areloch 2019-05-04 16:25:16 -05:00
commit 951594259f
909 changed files with 367086 additions and 2721 deletions

View file

@ -0,0 +1,346 @@
//-----------------------------------------------------------------------------
// Copyright (c) 2012 GarageGames, LLC
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
//-----------------------------------------------------------------------------
//#define TORQUE_PBR_MATERIALS
#include "platform/platform.h"
#include "ts/loader/appSequence.h"
#include "ts/assimp/assimpAppMaterial.h"
#include "ts/assimp/assimpAppMesh.h"
#include "materials/materialManager.h"
#include "ts/tsMaterialList.h"
// assimp include files.
#include <assimp/cimport.h>
#include <assimp/scene.h>
#include <assimp/postprocess.h>
#include <assimp/types.h>
String AppMaterial::cleanString(const String& str)
{
String cleanStr(str);
// Replace invalid characters with underscores
const String badChars(" -,.+=*/[]%$~;:");
for (String::SizeType i = 0; i < badChars.length(); i++)
cleanStr.replace(badChars[i], '_');
// Prefix with an underscore if string starts with a number
if ((cleanStr[0] >= '0') && (cleanStr[0] <= '9'))
cleanStr.insert(0, '_');
return cleanStr;
}
AssimpAppMaterial::AssimpAppMaterial(const char* matName)
{
name = matName;
// Set some defaults
flags |= TSMaterialList::S_Wrap;
flags |= TSMaterialList::T_Wrap;
}
AssimpAppMaterial::AssimpAppMaterial(aiMaterial* mtl) :
mAIMat(mtl)
{
aiString matName;
mtl->Get(AI_MATKEY_NAME, matName);
name = matName.C_Str();
if (name.isEmpty())
{
name = cleanString(TSShapeLoader::getShapePath().getFileName());;
name += "_defMat";
}
Con::printf("[ASSIMP] Loading Material: %s", name.c_str());
#ifdef TORQUE_DEBUG
enumerateMaterialProperties(mtl);
#endif
}
Material* AssimpAppMaterial::createMaterial(const Torque::Path& path) const
{
// The filename and material name are used as TorqueScript identifiers, so
// clean them up first
String cleanFile = cleanString(TSShapeLoader::getShapePath().getFileName());
String cleanName = cleanString(getName());
// Create the Material definition
const String oldScriptFile = Con::getVariable("$Con::File");
Con::setVariable("$Con::File", path.getFullPath()); // modify current script path so texture lookups are correct
Material *newMat = MATMGR->allocateAndRegister(cleanName, getName());
Con::setVariable("$Con::File", oldScriptFile); // restore script path
initMaterial(path, newMat);
return newMat;
}
void AssimpAppMaterial::initMaterial(const Torque::Path& path, Material* mat) const
{
String cleanFile = cleanString(TSShapeLoader::getShapePath().getFileName());
String cleanName = cleanString(getName());
// Determine the blend mode and transparency for this material
Material::BlendOp blendOp = Material::None;
bool translucent = false;
float opacity = 1.0f;
if (AI_SUCCESS == mAIMat->Get(AI_MATKEY_OPACITY, opacity))
{
if (opacity != 1.0f)
{
translucent = true;
int blendInt;
blendOp = Material::LerpAlpha;
if (AI_SUCCESS == mAIMat->Get(AI_MATKEY_BLEND_FUNC, blendInt))
{
if (blendInt == aiBlendMode_Additive)
blendOp = Material::Add;
}
}
}
else
{ // No opacity key, see if it's defined as a gltf property
aiString opacityMode;
if (AI_SUCCESS == mAIMat->Get("$mat.gltf.alphaMode", 0, 0, opacityMode))
{
if (dStrcmp("MASK", opacityMode.C_Str()) == 0)
{
translucent = true;
blendOp = Material::LerpAlpha;
float cutoff;
if (AI_SUCCESS == mAIMat->Get("$mat.gltf.alphaCutoff", 0, 0, cutoff))
{
mat->mAlphaRef = (U32)(cutoff * 255); // alpha ref 0-255
mat->mAlphaTest = true;
}
}
else if (dStrcmp("OPAQUE", opacityMode.C_Str()) != 0)
{
translucent = true;
blendOp = Material::LerpAlpha;
}
}
}
mat->mTranslucent = translucent;
mat->mTranslucentBlendOp = blendOp;
// Assign color values.
LinearColorF diffuseColor(1.0f, 1.0f, 1.0f, 1.0f);
aiColor3D read_color(1.f, 1.f, 1.f);
if (AI_SUCCESS == mAIMat->Get(AI_MATKEY_COLOR_DIFFUSE, read_color))
diffuseColor.set(read_color.r, read_color.g, read_color.b, opacity);
mat->mDiffuse[0] = diffuseColor;
aiString texName;
String torquePath;
if (AI_SUCCESS == mAIMat->Get(AI_MATKEY_TEXTURE(aiTextureType_DIFFUSE, 0), texName))
{
torquePath = texName.C_Str();
if (!torquePath.isEmpty())
mat->mDiffuseMapFilename[0] = cleanTextureName(torquePath, cleanFile);
}
if (AI_SUCCESS == mAIMat->Get(AI_MATKEY_TEXTURE(aiTextureType_NORMALS, 0), texName))
{
torquePath = texName.C_Str();
if (!torquePath.isEmpty())
mat->mNormalMapFilename[0] = cleanTextureName(torquePath, cleanFile);
}
#ifdef TORQUE_PBR_MATERIALS
float floatVal;
if (AI_SUCCESS == mAIMat->Get("$mat.gltf.pbrMetallicRoughness.roughnessFactor", 0, 0, floatVal))
{ // The shape has pbr material definitions
String aoName, rmName; // occlusion and roughness/metalness maps
if (AI_SUCCESS == mAIMat->Get(AI_MATKEY_TEXTURE(aiTextureType_LIGHTMAP, 0), texName))
aoName = texName.C_Str();
if (AI_SUCCESS == mAIMat->Get(AI_MATKEY_TEXTURE(aiTextureType_UNKNOWN, 0), texName))
rmName = texName.C_Str();
//if (aoName.isNotEmpty() && (aoName == rmName))
// mat->mOrmMapFilename[0] = cleanTextureName(aoName, cleanFile); // It's an ORM map
//else if (aoName.isNotEmpty() || rmName.isNotEmpty())
if (aoName.isNotEmpty() || rmName.isNotEmpty())
{ // If we have either map, fill all three slots
if (rmName.isNotEmpty())
{
mat->mRoughMapFilename[0] = cleanTextureName(rmName, cleanFile); // Roughness
mat->mSmoothnessChan[0] = 1.0f;
mat->mInvertSmoothness = (floatVal == 1.0f);
mat->mMetalMapFilename[0] = cleanTextureName(rmName, cleanFile); // Metallic
mat->mMetalChan[0] = 2.0f;
}
if (aoName.isNotEmpty())
{
mat->mAOMapFilename[0] = cleanTextureName(aoName, cleanFile); // occlusion
mat->mAOChan[0] = 0.0f;
}
else
{
mat->mAOMapFilename[0] = cleanTextureName(rmName, cleanFile); // occlusion
mat->mAOChan[0] = 0.0f;
}
}
}
#else
if (AI_SUCCESS == mAIMat->Get(AI_MATKEY_TEXTURE(aiTextureType_SPECULAR, 0), texName))
{
torquePath = texName.C_Str();
if (!torquePath.isEmpty())
mat->mSpecularMapFilename[0] = cleanTextureName(torquePath, cleanFile);
}
LinearColorF specularColor(1.0f, 1.0f, 1.0f, 1.0f);
if (AI_SUCCESS == mAIMat->Get(AI_MATKEY_COLOR_SPECULAR, read_color))
specularColor.set(read_color.r, read_color.g, read_color.b, opacity);
mat->mSpecular[0] = specularColor;
// Specular Power
F32 specularPower = 1.0f;
if (AI_SUCCESS == mAIMat->Get(AI_MATKEY_SHININESS_STRENGTH, specularPower))
mat->mSpecularPower[0] = specularPower;
// Specular
F32 specularStrength = 0.0f;
if (AI_SUCCESS == mAIMat->Get(AI_MATKEY_SHININESS, specularStrength))
mat->mSpecularStrength[0] = specularStrength;
#endif
// Double-Sided
bool doubleSided = false;
S32 dbl_sided = 0;
if (AI_SUCCESS == mAIMat->Get(AI_MATKEY_TWOSIDED, dbl_sided))
doubleSided = (dbl_sided != 0);
mat->mDoubleSided = doubleSided;
}
String AssimpAppMaterial::cleanTextureName(String& texName, String& shapeName)
{
String cleanStr;
if (texName[0] == '*')
{
cleanStr = shapeName;
cleanStr += "_cachedTex";
cleanStr += texName.substr(1);
}
else
{
cleanStr = texName;
cleanStr.replace('\\', '/');
}
return cleanStr;
}
#ifdef TORQUE_DEBUG
void AssimpAppMaterial::enumerateMaterialProperties(aiMaterial* mtl)
{
for (U32 i = 0; i < mtl->mNumProperties; ++i)
{
aiMaterialProperty* matProp = mtl->mProperties[i];
String outText;
if (matProp)
{
outText = String::ToString(" Key: %s, Index: %d, Semantic: ", matProp->mKey.C_Str(), matProp->mIndex);
switch (matProp->mSemantic)
{
case aiTextureType_NONE:
outText += "aiTextureType_NONE";
break;
case aiTextureType_DIFFUSE:
outText += "aiTextureType_DIFFUSE";
break;
case aiTextureType_SPECULAR:
outText += "aiTextureType_SPECULAR";
break;
case aiTextureType_AMBIENT:
outText += "aiTextureType_AMBIENT";
break;
case aiTextureType_EMISSIVE:
outText += "aiTextureType_EMISSIVE";
break;
case aiTextureType_HEIGHT:
outText += "aiTextureType_HEIGHT";
break;
case aiTextureType_NORMALS:
outText += "aiTextureType_NORMALS";
break;
case aiTextureType_SHININESS:
outText += "aiTextureType_SHININESS";
break;
case aiTextureType_OPACITY:
outText += "aiTextureType_OPACITY";
break;
case aiTextureType_DISPLACEMENT:
outText += "aiTextureType_DISPLACEMENT";
break;
case aiTextureType_LIGHTMAP:
outText += "aiTextureType_LIGHTMAP";
break;
case aiTextureType_REFLECTION:
outText += "aiTextureType_REFLECTION";
break;
default:
outText += "aiTextureType_UNKNOWN";
break;
}
aiString stringProp;
F32* floatProp;
double* doubleProp;
S32* intProp;
switch (matProp->mType)
{
case aiPTI_Float:
floatProp = (F32*)matProp->mData;
for (U32 j = 0; j < matProp->mDataLength / sizeof(F32); ++j)
outText += String::ToString(", %0.4f", floatProp[j]);
break;
case aiPTI_Double:
doubleProp = (double*)matProp->mData;
for (U32 j = 0; j < matProp->mDataLength / sizeof(double); ++j)
outText += String::ToString(", %0.4lf", doubleProp[j]);
break;
case aiPTI_String:
aiGetMaterialString(mtl, matProp->mKey.C_Str(), matProp->mSemantic, matProp->mIndex, &stringProp);
outText += String::ToString(", %s", stringProp.C_Str());
break;
case aiPTI_Integer:
intProp = (S32*)matProp->mData;
for (U32 j = 0; j < matProp->mDataLength / sizeof(S32); ++j)
outText += String::ToString(", %d", intProp[j]);
break;
case aiPTI_Buffer:
outText += ", aiPTI_Buffer format data";
break;
default:
outText += ", Unknown data type";
}
Con::printf("%s", outText.c_str());
}
}
}
#endif

View file

@ -0,0 +1,56 @@
//-----------------------------------------------------------------------------
// Copyright (c) 2012 GarageGames, LLC
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
//-----------------------------------------------------------------------------
#ifndef _ASSIMP_APPMATERIAL_H_
#define _ASSIMP_APPMATERIAL_H_
#ifndef _APPMATERIAL_H_
#include "ts/loader/appMaterial.h"
#endif
#include <assimp/scene.h>
class Material;
class AssimpAppMaterial : public AppMaterial
{
typedef AppMaterial Parent;
String name;
aiMaterial* mAIMat;
#ifdef TORQUE_DEBUG
void enumerateMaterialProperties(aiMaterial* mtl);
#endif
static String cleanTextureName(String& texName, String& shapeName);
public:
AssimpAppMaterial(const char* matName);
AssimpAppMaterial(aiMaterial* mtl);
~AssimpAppMaterial() { }
String getName() const { return name; }
Material* createMaterial(const Torque::Path& path) const;
void initMaterial(const Torque::Path& path, Material* mat) const;
};
#endif // _ASSIMP_APPMATERIAL_H_

View file

@ -0,0 +1,277 @@
//-----------------------------------------------------------------------------
// Copyright (c) 2012 GarageGames, LLC
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
//-----------------------------------------------------------------------------
#include "platform/platform.h"
#include "ts/collada/colladaExtensions.h"
#include "ts/assimp/assimpAppMesh.h"
#include "ts/assimp/assimpAppNode.h"
// assimp include files.
#include <assimp/cimport.h>
#include <assimp/scene.h>
#include <assimp/postprocess.h>
#include <assimp/types.h>
bool AssimpAppMesh::fixedSizeEnabled = false;
S32 AssimpAppMesh::fixedSize = 2;
//------------------------------------------------------------------------------
AssimpAppMesh::AssimpAppMesh(const struct aiMesh* mesh, AssimpAppNode* node)
: mMeshData(mesh), appNode(node)
{
Con::printf("[ASSIMP] Mesh Created: %s", getName());
// See if it's a skinned mesh
mIsSkinMesh = false;
for (U32 b = 0; b < mesh->mNumBones; b++)
if (mMeshData->mBones[b]->mNumWeights > 0)
{
mIsSkinMesh = true;
break;
}
}
const char* AssimpAppMesh::getName(bool allowFixed)
{
// Some exporters add a 'PIVOT' or unnamed node between the mesh and the
// actual object node. Detect this and return the object node name instead
// of the pivot node.
const char* nodeName = appNode->getName();
if ( dStrEqual(nodeName, "null") || dStrEndsWith(nodeName, "PIVOT") )
nodeName = appNode->getParentName();
// If all geometry is being fixed to the same size, append the size
// to the name
return allowFixed && fixedSizeEnabled ? avar("%s %d", nodeName, fixedSize) : nodeName;
}
MatrixF AssimpAppMesh::getMeshTransform(F32 time)
{
return appNode->getNodeTransform(time);
}
void AssimpAppMesh::lockMesh(F32 t, const MatrixF& objOffset)
{
// After this function, the following are expected to be populated:
// points, normals, uvs, primitives, indices
// There is also colors and uv2s but those don't seem to be required.
points.reserve(mMeshData->mNumVertices);
uvs.reserve(mMeshData->mNumVertices);
normals.reserve(mMeshData->mNumVertices);
bool flipNormals = Con::getBoolVariable("$Assimp::FlipNormals", false);
bool noUVFound = false;
for (U32 i = 0; i<mMeshData->mNumVertices; i++)
{
// Points and Normals
aiVector3D pt = mMeshData->mVertices[i];
aiVector3D nrm;
if (mMeshData->HasNormals())
nrm = mMeshData->mNormals[i];
else
nrm.Set(0, 0, 0);
Point3F tmpVert;
Point3F tmpNormal;
tmpVert = Point3F(pt.x, pt.y, pt.z);
tmpNormal = Point3F(nrm.x, nrm.y, nrm.z);
if (flipNormals)
tmpNormal *= -1.0f;
objOffset.mulP(tmpVert);
points.push_back(tmpVert);
if (mMeshData->HasTextureCoords(0))
{
uvs.push_back(Point2F(mMeshData->mTextureCoords[0][i].x, mMeshData->mTextureCoords[0][i].y));
}
else
{
// I don't know if there's any solution to this issue.
// If it's not mapped, it's not mapped.
noUVFound = true;
uvs.push_back(Point2F(1, 1));
}
// UV2s
if (mMeshData->HasTextureCoords(1))
{
uv2s.push_back(Point2F(mMeshData->mTextureCoords[1][i].x, mMeshData->mTextureCoords[1][i].y));
}
// Vertex Colors
if (mMeshData->HasVertexColors(0))
{
LinearColorF vColor(mMeshData->mColors[0][i].r,
mMeshData->mColors[0][i].g,
mMeshData->mColors[0][i].b,
mMeshData->mColors[0][i].a);
colors.push_back(vColor.toColorI());
}
//uvs.push_back(mModel->mVerts[i].texcoord);
normals.push_back(tmpNormal);
//edgeVerts.push_back(mModel->mVerts[i].edge);
}
U32 numFaces = mMeshData->mNumFaces;
//primitives.reserve(numFaces);
//Fetch the number of indices
U32 indicesCount = 0;
for (U32 i = 0; i < numFaces; i++)
{
indicesCount += mMeshData->mFaces[i].mNumIndices;
}
indices.reserve(indicesCount);
// Create TSMesh primitive
primitives.increment();
TSDrawPrimitive& primitive = primitives.last();
primitive.start = 0;
primitive.matIndex = (TSDrawPrimitive::Triangles | TSDrawPrimitive::Indexed) | (S32)mMeshData->mMaterialIndex;
primitive.numElements = indicesCount;
for ( U32 n = 0; n < mMeshData->mNumFaces; ++n)
{
const struct aiFace* face = &mMeshData->mFaces[n];
if ( face->mNumIndices == 3 )
{
U32 indexCount = face->mNumIndices;
for (U32 ind = 0; ind < indexCount; ind++)
{
U32 index = face->mIndices[ind];
indices.push_back(index);
}
}
else
{
Con::printf("[ASSIMP] Non-Triangle Face Found. Indices: %d", face->mNumIndices);
}
}
U32 boneCount = mMeshData->mNumBones;
bones.setSize(boneCount);
// Count the total number of weights for all of the bones.
U32 totalWeights = 0;
U32 nonZeroWeights = 0;
for (U32 b = 0; b < boneCount; b++)
totalWeights += mMeshData->mBones[b]->mNumWeights;
// Assimp gives weights sorted by bone index. We need them in vertex order.
Vector<F32> tmpWeight;
Vector<S32> tmpBoneIndex;
Vector<S32> tmpVertexIndex;
tmpWeight.setSize(totalWeights);
tmpBoneIndex.setSize(totalWeights);
tmpVertexIndex.setSize(totalWeights);
for (U32 b = 0; b < boneCount; b++)
{
String name = mMeshData->mBones[b]->mName.C_Str();
aiNode* nodePtr = AssimpAppNode::findChildNodeByName(mMeshData->mBones[b]->mName.C_Str(), appNode->mScene->mRootNode);
if (!nodePtr)
bones[b] = new AssimpAppNode(appNode->mScene, appNode->mNode);
else
bones[b] = new AssimpAppNode(appNode->mScene, nodePtr);
MatrixF boneTransform;
AssimpAppNode::assimpToTorqueMat(mMeshData->mBones[b]->mOffsetMatrix, boneTransform);
Point3F boneScale = boneTransform.getScale();
if (boneScale != Point3F::One)
{
Point3F scaleMult = Point3F::One / boneScale;
Point3F scalePos = boneTransform.getPosition();
boneTransform.scale(scaleMult);
scalePos /= scaleMult;
boneTransform.setPosition(scalePos);
}
initialTransforms.push_back(boneTransform);
//Weights
U32 numWeights = mMeshData->mBones[b]->mNumWeights;
for (U32 w = 0; w < numWeights; ++w)
{
aiVertexWeight* aiWeight = &mMeshData->mBones[b]->mWeights[w];
if (aiWeight->mWeight > 0.0f)
{
tmpWeight[nonZeroWeights] = aiWeight->mWeight;
tmpVertexIndex[nonZeroWeights] = aiWeight->mVertexId;
tmpBoneIndex[nonZeroWeights] = b;
nonZeroWeights++;
}
}
}
weight.setSize(nonZeroWeights);
vertexIndex.setSize(nonZeroWeights);
boneIndex.setSize(nonZeroWeights);
// Copy the weights to our vectors in vertex order and
// normalize vertex weights (force weights for each vert to sum to 1)
U32 nextWeight = 0;
for (U32 i = 0; i < mMeshData->mNumVertices; ++i)
{
U32 vertStart = nextWeight;
F32 invTotalWeight = 0;
for (U32 ind = 0; ind < nonZeroWeights; ++ind)
{
if (tmpVertexIndex[ind] == i)
{
weight[nextWeight] = tmpWeight[ind];
invTotalWeight += tmpWeight[ind];
vertexIndex[nextWeight] = tmpVertexIndex[ind];
boneIndex[nextWeight] = tmpBoneIndex[ind];
nextWeight++;
}
}
// Now normalize the vertex weights
if (invTotalWeight > 0.0)
{
invTotalWeight = 1.0f / invTotalWeight;
for (U32 ind = vertStart; ind < nextWeight; ++ind)
weight[ind] *= invTotalWeight;
}
}
if ( noUVFound )
Con::warnf("[ASSIMP] No UV Data for mesh.");
}
void AssimpAppMesh::lookupSkinData()
{ // This function is intentionally left blank. The skin data - bones, weights and indexes are
// processed in lockMesh() with the rest of the mesh data.
}
F32 AssimpAppMesh::getVisValue(F32 t)
{
return 1.0f;
}

View file

@ -0,0 +1,125 @@
//-----------------------------------------------------------------------------
// Copyright (c) 2012 GarageGames, LLC
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
//-----------------------------------------------------------------------------
#ifndef _ASSIMP_APPMESH_H_
#define _ASSIMP_APPMESH_H_
#ifndef _APPMESH_H_
#include "ts/loader/appMesh.h"
#endif
#ifndef _TSSHAPELOADER_H_
#include "ts/loader/tsShapeLoader.h"
#endif
#ifndef _ASSIMP_APPNODE_H_
#include "ts/assimp/assimpAppNode.h"
#endif
class AssimpAppMesh : public AppMesh
{
typedef AppMesh Parent;
protected:
class AssimpAppNode* appNode; ///< Pointer to the node that owns this mesh
const struct aiMesh* mMeshData;
bool mIsSkinMesh;
static bool fixedSizeEnabled; ///< Set to true to fix the detail size to a particular value for all geometry
static S32 fixedSize; ///< The fixed detail size value for all geometry
public:
AssimpAppMesh(const struct aiMesh* mesh, AssimpAppNode* node);
~AssimpAppMesh()
{
//delete geomExt;
}
void lookupSkinData();
static void fixDetailSize(bool fixed, S32 size=2)
{
fixedSizeEnabled = fixed;
fixedSize = size;
}
/// Get the name of this mesh
///
/// @return A string containing the name of this mesh
const char *getName(bool allowFixed=true);
//-----------------------------------------------------------------------
/// Get a floating point property value
///
/// @param propName Name of the property to get
/// @param defaultVal Reference to variable to hold return value
///
/// @return True if a value was set, false if not
bool getFloat(const char *propName, F32 &defaultVal)
{
return appNode->getFloat(propName,defaultVal);
}
/// Get an integer property value
///
/// @param propName Name of the property to get
/// @param defaultVal Reference to variable to hold return value
///
/// @return True if a value was set, false if not
bool getInt(const char *propName, S32 &defaultVal)
{
return appNode->getInt(propName,defaultVal);
}
/// Get a boolean property value
///
/// @param propName Name of the property to get
/// @param defaultVal Reference to variable to hold return value
///
/// @return True if a value was set, false if not
bool getBool(const char *propName, bool &defaultVal)
{
return appNode->getBool(propName,defaultVal);
}
/// Return true if this mesh is a skin
bool isSkin()
{
return mIsSkinMesh;
}
/// Generate the vertex, normal and triangle data for the mesh.
///
/// @param time Time at which to generate the mesh data
/// @param objectOffset Transform to apply to the generated data (bounds transform)
void lockMesh(F32 time, const MatrixF& objOffset);
/// Get the transform of this mesh at a certain time
///
/// @param time Time at which to get the transform
///
/// @return The mesh transform at the specified time
MatrixF getMeshTransform(F32 time);
F32 getVisValue(F32 t);
};
#endif // _COLLADA_APPMESH_H_

View file

@ -0,0 +1,328 @@
//-----------------------------------------------------------------------------
// Copyright (c) 2012 GarageGames, LLC
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
//-----------------------------------------------------------------------------
#include "platform/platform.h"
#include "ts/loader/appSequence.h"
#include "ts/assimp/assimpAppNode.h"
#include "ts/assimp/assimpAppMesh.h"
// assimp include files.
#include <assimp/cimport.h>
#include <assimp/scene.h>
#include <assimp/postprocess.h>
#include <assimp/types.h>
aiAnimation* AssimpAppNode::sActiveSequence = NULL;
F32 AssimpAppNode::sTimeMultiplier = 1.0f;
AssimpAppNode::AssimpAppNode(const struct aiScene* scene, const struct aiNode* node, AssimpAppNode* parent)
: mInvertMeshes(false),
mLastTransformTime(TSShapeLoader::DefaultTime - 1),
mDefaultTransformValid(false)
{
mScene = scene;
mNode = node;
appParent = parent;
mName = dStrdup(mNode->mName.C_Str());
if ( dStrlen(mName) == 0 )
{
const char* defaultName = "null";
mName = dStrdup(defaultName);
}
mParentName = dStrdup(parent ? parent->getName() : "ROOT");
assimpToTorqueMat(node->mTransformation, mNodeTransform);
Con::printf("[ASSIMP] Node Created: %s, Parent: %s", mName, mParentName);
}
// Get all child nodes
void AssimpAppNode::buildChildList()
{
if (!mNode)
{
mNode = mScene->mRootNode;
}
for (U32 n = 0; n < mNode->mNumChildren; ++n) {
mChildNodes.push_back(new AssimpAppNode(mScene, mNode->mChildren[n], this));
}
}
// Get all geometry attached to this node
void AssimpAppNode::buildMeshList()
{
for (U32 n = 0; n < mNode->mNumMeshes; ++n)
{
const struct aiMesh* mesh = mScene->mMeshes[mNode->mMeshes[n]];
mMeshes.push_back(new AssimpAppMesh(mesh, this));
}
}
MatrixF AssimpAppNode::getTransform(F32 time)
{
// Check if we can use the last computed transform
if (time == mLastTransformTime)
return mLastTransform;
if (appParent) {
// Get parent node's transform
mLastTransform = appParent->getTransform(time);
}
else {
// no parent (ie. root level) => scale by global shape <unit>
mLastTransform.identity();
if (!isBounds())
convertMat(mLastTransform);
//mLastTransform.scale(ColladaUtils::getOptions().unit);
}
// If this node is animated in the active sequence, fetch the animated transform
if (sActiveSequence)
{
MatrixF mat(true);
getAnimatedTransform(mat, time, sActiveSequence);
mLastTransform.mul(mat);
}
else
mLastTransform.mul(mNodeTransform);
mLastTransformTime = time;
return mLastTransform;
}
void AssimpAppNode::getAnimatedTransform(MatrixF& mat, F32 t, aiAnimation* animSeq)
{
// Find the channel for this node
for (U32 i = 0; i < animSeq->mNumChannels; ++i)
{
if (strcmp(mName, animSeq->mChannels[i]->mNodeName.C_Str()) == 0)
{
aiNodeAnim *nodeAnim = animSeq->mChannels[i];
Point3F trans(Point3F::Zero);
Point3F scale(Point3F::One);
QuatF rot;
rot.identity();
// Transform
if (nodeAnim->mNumPositionKeys == 1)
trans.set(nodeAnim->mPositionKeys[0].mValue.x, nodeAnim->mPositionKeys[0].mValue.y, nodeAnim->mPositionKeys[0].mValue.z);
else
{
Point3F curPos, lastPos;
F32 lastT = 0.0;
for (U32 key = 0; key < nodeAnim->mNumPositionKeys; ++key)
{
F32 curT = sTimeMultiplier * (F32)nodeAnim->mPositionKeys[key].mTime;
curPos.set(nodeAnim->mPositionKeys[key].mValue.x, nodeAnim->mPositionKeys[key].mValue.y, nodeAnim->mPositionKeys[key].mValue.z);
if ((curT > t) && (key > 0))
{
F32 factor = (t - lastT) / (curT - lastT);
trans.interpolate(lastPos, curPos, factor);
break;
}
else if ((curT >= t) || (key == nodeAnim->mNumPositionKeys - 1))
{
trans = curPos;
break;
}
lastT = curT;
lastPos = curPos;
}
}
// Rotation
if (nodeAnim->mNumRotationKeys == 1)
rot.set(nodeAnim->mRotationKeys[0].mValue.x, nodeAnim->mRotationKeys[0].mValue.y,
nodeAnim->mRotationKeys[0].mValue.z, nodeAnim->mRotationKeys[0].mValue.w);
else
{
QuatF curRot, lastRot;
F32 lastT = 0.0;
for (U32 key = 0; key < nodeAnim->mNumRotationKeys; ++key)
{
F32 curT = sTimeMultiplier * (F32)nodeAnim->mRotationKeys[key].mTime;
curRot.set(nodeAnim->mRotationKeys[key].mValue.x, nodeAnim->mRotationKeys[key].mValue.y,
nodeAnim->mRotationKeys[key].mValue.z, nodeAnim->mRotationKeys[key].mValue.w);
if ((curT > t) && (key > 0))
{
F32 factor = (t - lastT) / (curT - lastT);
rot.interpolate(lastRot, curRot, factor);
break;
}
else if ((curT >= t) || (key == nodeAnim->mNumRotationKeys - 1))
{
rot = curRot;
break;
}
lastT = curT;
lastRot = curRot;
}
}
// Scale
if (nodeAnim->mNumScalingKeys == 1)
scale.set(nodeAnim->mScalingKeys[0].mValue.x, nodeAnim->mScalingKeys[0].mValue.y, nodeAnim->mScalingKeys[0].mValue.z);
else
{
Point3F curScale, lastScale;
F32 lastT = 0.0;
for (U32 key = 0; key < nodeAnim->mNumScalingKeys; ++key)
{
F32 curT = sTimeMultiplier * (F32)nodeAnim->mScalingKeys[key].mTime;
curScale.set(nodeAnim->mScalingKeys[key].mValue.x, nodeAnim->mScalingKeys[key].mValue.y, nodeAnim->mScalingKeys[key].mValue.z);
if ((curT > t) && (key > 0))
{
F32 factor = (t - lastT) / (curT - lastT);
scale.interpolate(lastScale, curScale, factor);
break;
}
else if ((curT >= t) || (key == nodeAnim->mNumScalingKeys - 1))
{
scale = curScale;
break;
}
lastT = curT;
lastScale = curScale;
}
}
rot.setMatrix(&mat);
mat.inverse();
mat.setPosition(trans);
mat.scale(scale);
return;
}
}
// Node not found in the animation channels
mat = mNodeTransform;
}
bool AssimpAppNode::animatesTransform(const AppSequence* appSeq)
{
return false;
}
/// Get the world transform of the node at the specified time
MatrixF AssimpAppNode::getNodeTransform(F32 time)
{
// Avoid re-computing the default transform if possible
if (mDefaultTransformValid && time == TSShapeLoader::DefaultTime)
{
return mDefaultNodeTransform;
}
else
{
MatrixF nodeTransform = getTransform(time);
// Check for inverted node coordinate spaces => can happen when modelers
// use the 'mirror' tool in their 3d app. Shows up as negative <scale>
// transforms in the collada model.
if (m_matF_determinant(nodeTransform) < 0.0f)
{
// Mark this node as inverted so we can mirror mesh geometry, then
// de-invert the transform matrix
mInvertMeshes = true;
nodeTransform.scale(Point3F(1, 1, -1));
}
// Cache the default transform
if (time == TSShapeLoader::DefaultTime)
{
mDefaultTransformValid = true;
mDefaultNodeTransform = nodeTransform;
}
return nodeTransform;
}
}
void AssimpAppNode::assimpToTorqueMat(const aiMatrix4x4& inAssimpMat, MatrixF& outMat)
{
outMat.setRow(0, Point4F((F32)inAssimpMat.a1, (F32)inAssimpMat.a2,
(F32)inAssimpMat.a3, (F32)inAssimpMat.a4));
outMat.setRow(1, Point4F((F32)inAssimpMat.b1, (F32)inAssimpMat.b2,
(F32)inAssimpMat.b3, (F32)inAssimpMat.b4));
outMat.setRow(2, Point4F((F32)inAssimpMat.c1, (F32)inAssimpMat.c2,
(F32)inAssimpMat.c3, (F32)inAssimpMat.c4));
outMat.setRow(3, Point4F((F32)inAssimpMat.d1, (F32)inAssimpMat.d2,
(F32)inAssimpMat.d3, (F32)inAssimpMat.d4));
}
void AssimpAppNode::convertMat(MatrixF& outMat)
{
MatrixF rot(true);
// This is copied directly from ColladaUtils::convertTransform()
// ColladaUtils::getOptions().upAxis has been temporarily replaced with $Assimp::OverrideUpAxis for testing
// We need a plan for how the full set of assimp import options and settings is going to be managed.
switch (Con::getIntVariable("$Assimp::OverrideUpAxis", 2))
{
case 0: //UPAXISTYPE_X_UP:
// rotate 90 around Y-axis, then 90 around Z-axis
rot(0, 0) = 0.0f; rot(1, 0) = 1.0f;
rot(1, 1) = 0.0f; rot(2, 1) = 1.0f;
rot(0, 2) = 1.0f; rot(2, 2) = 0.0f;
// pre-multiply the transform by the rotation matrix
outMat.mulL(rot);
break;
case 1: //UPAXISTYPE_Y_UP:
// rotate 180 around Y-axis, then 90 around X-axis
rot(0, 0) = -1.0f;
rot(1, 1) = 0.0f; rot(2, 1) = 1.0f;
rot(1, 2) = 1.0f; rot(2, 2) = 0.0f;
// pre-multiply the transform by the rotation matrix
outMat.mulL(rot);
break;
case 2: //UPAXISTYPE_Z_UP:
default:
// nothing to do
break;
}
}
aiNode* AssimpAppNode::findChildNodeByName(const char* nodeName, aiNode* rootNode)
{
aiNode* retNode = NULL;
if (strcmp(nodeName, rootNode->mName.C_Str()) == 0)
return rootNode;
for (U32 i = 0; i < rootNode->mNumChildren; ++i)
{
retNode = findChildNodeByName(nodeName, rootNode->mChildren[i]);
if (retNode)
return retNode;
}
return nullptr;
}

View file

@ -0,0 +1,118 @@
//-----------------------------------------------------------------------------
// Copyright (c) 2012 GarageGames, LLC
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
//-----------------------------------------------------------------------------
#ifndef _ASSIMP_APPNODE_H_
#define _ASSIMP_APPNODE_H_
#ifndef _TDICTIONARY_H_
#include "core/tDictionary.h"
#endif
#ifndef _APPNODE_H_
#include "ts/loader/appNode.h"
#endif
#ifndef _COLLADA_EXTENSIONS_H_
#include "ts/collada/colladaExtensions.h"
#endif
#ifndef AI_TYPES_H_INC
#include <assimp/types.h>
#endif
#include <assimp/scene.h>
class AssimpAppNode : public AppNode
{
typedef AppNode Parent;
friend class AssimpAppMesh;
MatrixF getTransform(F32 time);
void getAnimatedTransform(MatrixF& mat, F32 t, aiAnimation* animSeq);
void buildMeshList();
void buildChildList();
protected:
const struct aiScene* mScene;
const struct aiNode* mNode; ///< Pointer to the assimp scene node
AssimpAppNode* appParent; ///< Parent node
MatrixF mNodeTransform; ///< Scene node transform converted to TorqueSpace (filled for ALL nodes)
bool mInvertMeshes; ///< True if this node's coordinate space is inverted (left handed)
F32 mLastTransformTime; ///< Time of the last transform lookup (getTransform)
MatrixF mLastTransform; ///< Last transform lookup (getTransform) (Only Non-Dummy Nodes)
bool mDefaultTransformValid; ///< Flag indicating whether the defaultNodeTransform is valid
MatrixF mDefaultNodeTransform; ///< Transform at DefaultTime (Only Non-Dummy Nodes)
public:
AssimpAppNode(const struct aiScene* scene, const struct aiNode* node, AssimpAppNode* parent = 0);
virtual ~AssimpAppNode()
{
//
}
static aiAnimation* sActiveSequence;
static F32 sTimeMultiplier;
//-----------------------------------------------------------------------
const char *getName() { return mName; }
const char *getParentName() { return mParentName; }
bool isEqual(AppNode* node)
{
const AssimpAppNode* appNode = dynamic_cast<const AssimpAppNode*>(node);
return (appNode && (appNode->mNode == mNode));
}
// Property look-ups: only float properties are stored, the rest are
// converted from floats as needed
bool getFloat(const char* propName, F32& defaultVal)
{
//Map<StringTableEntry,F32>::Iterator itr = mProps.find(propName);
//if (itr != mProps.end())
// defaultVal = itr->value;
return false;
}
bool getInt(const char* propName, S32& defaultVal)
{
F32 value = defaultVal;
bool ret = getFloat(propName, value);
defaultVal = (S32)value;
return ret;
}
bool getBool(const char* propName, bool& defaultVal)
{
F32 value = defaultVal;
bool ret = getFloat(propName, value);
defaultVal = (value != 0);
return ret;
}
MatrixF getNodeTransform(F32 time);
bool animatesTransform(const AppSequence* appSeq);
bool isParentRoot() { return (appParent == NULL); }
static void assimpToTorqueMat(const aiMatrix4x4& inAssimpMat, MatrixF& outMat);
static void convertMat(MatrixF& outMat);
static aiNode* findChildNodeByName(const char* nodeName, aiNode* rootNode);
};
#endif // _ASSIMP_APPNODE_H_

View file

@ -0,0 +1,117 @@
#include "ts/assimp/assimpShapeLoader.h"
#include "console/console.h"
#include "core/stream/fileStream.h"
#include "core/stringTable.h"
#include "math/mathIO.h"
#include "ts/tsShape.h"
#include "ts/tsShapeInstance.h"
#include "materials/materialManager.h"
#include "console/persistenceManager.h"
#include "ts/assimp/assimpAppMaterial.h"
#include "ts/assimp/assimpAppSequence.h"
#include "ts/assimp/assimpAppNode.h"
AssimpAppSequence::AssimpAppSequence(aiAnimation *a) :
seqStart(0.0f),
mAnim(a)
{
mSequenceName = mAnim->mName.C_Str();
if (mSequenceName.isEmpty())
mSequenceName = "ambient";
Con::printf("\n[Assimp] Adding %s animation", mSequenceName.c_str());
fps = (mAnim->mTicksPerSecond > 0) ? mAnim->mTicksPerSecond : 30.0f;
U32 maxKeys = 0;
F32 maxEndTime = 0;
F32 minFrameTime = 100000.0f;
// Detect the frame rate (minimum time between keyframes) and max sequence time
for (U32 i = 0; i < mAnim->mNumChannels; ++i)
{
aiNodeAnim *nodeAnim = mAnim->mChannels[i];
maxKeys = getMax(maxKeys, nodeAnim->mNumPositionKeys);
maxKeys = getMax(maxKeys, nodeAnim->mNumRotationKeys);
maxKeys = getMax(maxKeys, nodeAnim->mNumScalingKeys);
if (nodeAnim->mNumPositionKeys)
maxEndTime = getMax(maxEndTime, (F32) nodeAnim->mPositionKeys[nodeAnim->mNumPositionKeys-1].mTime);
if (nodeAnim->mNumRotationKeys)
maxEndTime = getMax(maxEndTime, (F32) nodeAnim->mRotationKeys[nodeAnim->mNumRotationKeys-1].mTime);
if (nodeAnim->mNumScalingKeys)
maxEndTime = getMax(maxEndTime, (F32) nodeAnim->mScalingKeys[nodeAnim->mNumScalingKeys-1].mTime);
for (U32 key = 1; key < nodeAnim->mNumPositionKeys; ++key)
{
F32 deltaT = nodeAnim->mPositionKeys[key].mTime - nodeAnim->mPositionKeys[key-1].mTime;
minFrameTime = getMin(minFrameTime, deltaT);
}
for (U32 key = 1; key < nodeAnim->mNumRotationKeys; ++key)
{
F32 deltaT = nodeAnim->mRotationKeys[key].mTime - nodeAnim->mRotationKeys[key-1].mTime;
minFrameTime = getMin(minFrameTime, deltaT);
}
for (U32 key = 1; key < nodeAnim->mNumScalingKeys; ++key)
{
F32 deltaT = nodeAnim->mScalingKeys[key].mTime - nodeAnim->mScalingKeys[key-1].mTime;
minFrameTime = getMin(minFrameTime, deltaT);
}
}
S32 timeFactor = Con::getIntVariable("$Assimp::AnimTiming", 1);
S32 fpsRequest = Con::getIntVariable("$Assimp::AnimFPS", 30);
if (timeFactor == 0)
{ // Timing specified in frames
fps = mClamp(fpsRequest, 5 /*TSShapeLoader::MinFrameRate*/, TSShapeLoader::MaxFrameRate);
maxKeys = getMax(maxKeys, (U32)maxEndTime); // Keys won't be assigned for every frame.
seqEnd = maxKeys / fps;
mTimeMultiplier = 1.0f / fps;
}
else
{ // Timing specified in seconds or ms depending on format
if (maxEndTime > 1000.0f || mAnim->mDuration > 1000.0f)
{
timeFactor = 1000.0f; // If it's more than 1000 seconds, assume it's ms.
Con::setIntVariable("$Assimp::AnimTiming", 1000);
}
timeFactor = mClamp(timeFactor, 1, 1000);
minFrameTime /= (F32)timeFactor;
maxEndTime /= (F32)timeFactor;
fps = (minFrameTime > 0.0f) ? 1.0f / minFrameTime : fps;
fps = mClamp(fpsRequest, 5 /*TSShapeLoader::MinFrameRate*/, TSShapeLoader::MaxFrameRate);
seqEnd = maxEndTime;
mTimeMultiplier = 1.0f / timeFactor;
}
}
AssimpAppSequence::~AssimpAppSequence()
{
}
void AssimpAppSequence::setActive(bool active)
{
if (active)
{
AssimpAppNode::sActiveSequence = mAnim;
AssimpAppNode::sTimeMultiplier = mTimeMultiplier;
}
else
{
if (AssimpAppNode::sActiveSequence == mAnim)
AssimpAppNode::sActiveSequence = NULL;
}
}
U32 AssimpAppSequence::getFlags() const
{
return TSShape::Blend;
}
F32 AssimpAppSequence::getPriority() const
{
return 5;
}
F32 AssimpAppSequence::getBlendRefTime() const
{
return -1.0f;
}

View file

@ -0,0 +1,51 @@
#pragma once
#ifndef _MMATH_H_
#include "math/mMath.h"
#endif
#ifndef _TVECTOR_H_
#include "core/util/tVector.h"
#endif
#ifndef _TSSHAPE_H_
#include "ts/tsShape.h"
#endif
#ifndef _APPNODE_H_
#include "ts/loader/appNode.h"
#endif
#ifndef _APPMESH_H_
#include "ts/loader/appMesh.h"
#endif
#ifndef _APPSEQUENCE_H_
#include "ts/loader/appSequence.h"
#endif
#include <assimp/scene.h>
class AssimpAppSequence : public AppSequence
{
String mSequenceName;
F32 seqStart;
F32 seqEnd;
F32 mTimeMultiplier; // The factor needed to convert the sequence data timestamp to seconds
public:
AssimpAppSequence(aiAnimation *a);
~AssimpAppSequence();
aiAnimation *mAnim;
virtual void setActive(bool active);
virtual S32 getNumTriggers() const { return 0; }
virtual void getTrigger(S32 index, TSShape::Trigger& trigger) const { trigger.state = 0; }
virtual const char* getName() const { return mSequenceName.c_str(); }
F32 getStart() const { return seqStart; }
F32 getEnd() const { return seqEnd; }
void setEnd(F32 end) { seqEnd = end; }
virtual U32 getFlags() const;
virtual F32 getPriority() const;
virtual F32 getBlendRefTime() const;
};

View file

@ -0,0 +1,566 @@
//-----------------------------------------------------------------------------
// Copyright (c) 2012 GarageGames, LLC
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
//-----------------------------------------------------------------------------
/*
Resource stream -> Buffer
Buffer -> Collada DOM
Collada DOM -> TSShapeLoader
TSShapeLoader installed into TSShape
*/
//-----------------------------------------------------------------------------
#include "platform/platform.h"
#include "ts/assimp/assimpShapeLoader.h"
#include "ts/assimp/assimpAppNode.h"
#include "ts/assimp/assimpAppMesh.h"
#include "ts/assimp/assimpAppMaterial.h"
#include "ts/assimp/assimpAppSequence.h"
#include "core/util/tVector.h"
#include "core/strings/findMatch.h"
#include "core/stream/fileStream.h"
#include "core/fileObject.h"
#include "ts/tsShape.h"
#include "ts/tsShapeInstance.h"
#include "materials/materialManager.h"
#include "console/persistenceManager.h"
#include "ts/tsShapeConstruct.h"
#include "core/util/zip/zipVolume.h"
#include "gfx/bitmap/gBitmap.h"
#include "gui/controls/guiTreeViewCtrl.h"
// assimp include files.
#include <assimp/cimport.h>
#include <assimp/scene.h>
#include <assimp/postprocess.h>
#include <assimp/types.h>
#include <assimp/config.h>
#include <exception>
#include <assimp/Importer.hpp>
MODULE_BEGIN( AssimpShapeLoader )
MODULE_INIT_AFTER( ShapeLoader )
MODULE_INIT
{
TSShapeLoader::addFormat("DirectX X", "x");
TSShapeLoader::addFormat("Autodesk FBX", "fbx");
TSShapeLoader::addFormat("Blender 3D", "blend" );
TSShapeLoader::addFormat("3ds Max 3DS", "3ds");
TSShapeLoader::addFormat("3ds Max ASE", "ase");
TSShapeLoader::addFormat("Wavefront Object", "obj");
TSShapeLoader::addFormat("Industry Foundation Classes (IFC/Step)", "ifc");
TSShapeLoader::addFormat("Stanford Polygon Library", "ply");
TSShapeLoader::addFormat("AutoCAD DXF", "dxf");
TSShapeLoader::addFormat("LightWave", "lwo");
TSShapeLoader::addFormat("LightWave Scene", "lws");
TSShapeLoader::addFormat("Modo", "lxo");
TSShapeLoader::addFormat("Stereolithography", "stl");
TSShapeLoader::addFormat("AC3D", "ac");
TSShapeLoader::addFormat("Milkshape 3D", "ms3d");
TSShapeLoader::addFormat("TrueSpace COB", "cob");
TSShapeLoader::addFormat("TrueSpace SCN", "scn");
TSShapeLoader::addFormat("Ogre XML", "xml");
TSShapeLoader::addFormat("Irrlicht Mesh", "irrmesh");
TSShapeLoader::addFormat("Irrlicht Scene", "irr");
TSShapeLoader::addFormat("Quake I", "mdl" );
TSShapeLoader::addFormat("Quake II", "md2" );
TSShapeLoader::addFormat("Quake III Mesh", "md3");
TSShapeLoader::addFormat("Quake III Map/BSP", "pk3");
TSShapeLoader::addFormat("Return to Castle Wolfenstein", "mdc");
TSShapeLoader::addFormat("Doom 3", "md5" );
TSShapeLoader::addFormat("Valve SMD", "smd");
TSShapeLoader::addFormat("Valve VTA", "vta");
TSShapeLoader::addFormat("Starcraft II M3", "m3");
TSShapeLoader::addFormat("Unreal", "3d");
TSShapeLoader::addFormat("BlitzBasic 3D", "b3d" );
TSShapeLoader::addFormat("Quick3D Q3D", "q3d");
TSShapeLoader::addFormat("Quick3D Q3S", "q3s");
TSShapeLoader::addFormat("Neutral File Format", "nff");
TSShapeLoader::addFormat("Object File Format", "off");
TSShapeLoader::addFormat("PovRAY Raw", "raw");
TSShapeLoader::addFormat("Terragen Terrain", "ter");
TSShapeLoader::addFormat("3D GameStudio (3DGS)", "mdl");
TSShapeLoader::addFormat("3D GameStudio (3DGS) Terrain", "hmp");
TSShapeLoader::addFormat("Izware Nendo", "ndo");
TSShapeLoader::addFormat("gltf", "gltf");
TSShapeLoader::addFormat("gltf binary", "glb");
}
MODULE_END;
//-----------------------------------------------------------------------------
AssimpShapeLoader::AssimpShapeLoader()
{
mScene = NULL;
}
AssimpShapeLoader::~AssimpShapeLoader()
{
}
void AssimpShapeLoader::releaseImport()
{
aiReleaseImport(mScene);
}
void AssimpShapeLoader::enumerateScene()
{
TSShapeLoader::updateProgress(TSShapeLoader::Load_ReadFile, "Reading File");
Con::printf("[ASSIMP] Attempting to load file: %s", shapePath.getFullPath().c_str());
// Post-Processing
unsigned int ppsteps =
Con::getBoolVariable("$Assimp::ConvertToLeftHanded", false) ? aiProcess_ConvertToLeftHanded : 0 |
Con::getBoolVariable("$Assimp::CalcTangentSpace", false) ? aiProcess_CalcTangentSpace : 0 |
Con::getBoolVariable("$Assimp::JoinIdenticalVertices", false) ? aiProcess_JoinIdenticalVertices : 0 |
Con::getBoolVariable("$Assimp::ValidateDataStructure", false) ? aiProcess_ValidateDataStructure : 0 |
Con::getBoolVariable("$Assimp::ImproveCacheLocality", false) ? aiProcess_ImproveCacheLocality : 0 |
Con::getBoolVariable("$Assimp::RemoveRedundantMaterials", false) ? aiProcess_RemoveRedundantMaterials : 0 |
Con::getBoolVariable("$Assimp::FindDegenerates", false) ? aiProcess_FindDegenerates : 0 |
Con::getBoolVariable("$Assimp::FindInvalidData", false) ? aiProcess_FindInvalidData : 0 |
Con::getBoolVariable("$Assimp::GenUVCoords", false) ? aiProcess_GenUVCoords : 0 |
Con::getBoolVariable("$Assimp::TransformUVCoords", false) ? aiProcess_TransformUVCoords : 0 |
Con::getBoolVariable("$Assimp::FindInstances", false) ? aiProcess_FindInstances : 0 |
Con::getBoolVariable("$Assimp::LimitBoneWeights", false) ? aiProcess_LimitBoneWeights : 0 |
Con::getBoolVariable("$Assimp::OptimizeMeshes", false) ? aiProcess_OptimizeMeshes | aiProcess_OptimizeGraph : 0 |
0;
if(Con::getBoolVariable("$Assimp::FlipUVs", true))
ppsteps |= aiProcess_FlipUVs;
if(Con::getBoolVariable("$Assimp::FlipWindingOrder", false))
ppsteps |= aiProcess_FlipWindingOrder;
if(Con::getBoolVariable("$Assimp::Triangulate", true))
ppsteps |= aiProcess_Triangulate;
if (Con::getBoolVariable("$Assimp::OptimizeMeshes", false))
ppsteps |= aiProcess_OptimizeMeshes | aiProcess_OptimizeGraph;
if (Con::getBoolVariable("$Assimp::SplitLargeMeshes", false))
ppsteps |= aiProcess_SplitLargeMeshes;
//aiProcess_SortByPType | // make 'clean' meshes which consist of a single typ of primitives
aiPropertyStore* props = aiCreatePropertyStore();
//aiSetImportPropertyInteger(props, AI_CONFIG_IMPORT_TER_MAKE_UVS, 1);
//aiSetImportPropertyInteger(props, AI_CONFIG_PP_SBP_REMOVE, (aiProcessPreset_TargetRealtime_Quality
// | aiProcess_FlipWindingOrder | aiProcess_FlipUVs
// | aiProcess_CalcTangentSpace
// | aiProcess_FixInfacingNormals)
// & ~aiProcess_RemoveRedundantMaterials);
//aiSetImportPropertyInteger(props, AI_CONFIG_GLOB_MEASURE_TIME, 1);
//aiSetImportPropertyFloat(props, AI_CONFIG_PP_GSN_MAX_SMOOTHING_ANGLE, 80.f);
//aiSetImportPropertyInteger(props,AI_CONFIG_PP_PTV_KEEP_HIERARCHY,1);
struct aiLogStream shapeLog;
shapeLog = aiGetPredefinedLogStream(aiDefaultLogStream_FILE, "assimp.log");
aiAttachLogStream(&shapeLog);
#ifdef TORQUE_DEBUG
aiEnableVerboseLogging(true);
#endif
//c = aiGetPredefinedLogStream(aiDefaultLogStream_STDOUT, NULL);
//aiAttachLogStream(&c);
// Attempt to import with Assimp.
//mScene = importer.ReadFile(shapePath.getFullPath().c_str(), (aiProcessPreset_TargetRealtime_Quality | aiProcess_FlipWindingOrder | aiProcess_FlipUVs | aiProcess_CalcTangentSpace)
// & ~aiProcess_RemoveRedundantMaterials);
mScene = (aiScene*)aiImportFileExWithProperties(shapePath.getFullPath().c_str(), ppsteps, NULL, props);
aiReleasePropertyStore(props);
if ( mScene )
{
Con::printf("[ASSIMP] Mesh Count: %d", mScene->mNumMeshes);
Con::printf("[ASSIMP] Material Count: %d", mScene->mNumMaterials);
// Extract embedded textures
for (U32 i = 0; i < mScene->mNumTextures; ++i)
extractTexture(i, mScene->mTextures[i]);
// Load all the materials.
for ( U32 i = 0; i < mScene->mNumMaterials; i++ )
AppMesh::appMaterials.push_back(new AssimpAppMaterial(mScene->mMaterials[i]));
// Setup LOD checks
detectDetails();
// Define the root node, and process down the chain.
AssimpAppNode* node = new AssimpAppNode(mScene, mScene->mRootNode, 0);
if (!processNode(node))
delete node;
// Check for animations and process those.
processAnimations();
}
else
{
TSShapeLoader::updateProgress(TSShapeLoader::Load_Complete, "Import failed");
Con::printf("[ASSIMP] Import Error: %s", aiGetErrorString());
}
aiDetachLogStream(&shapeLog);
}
void AssimpShapeLoader::processAnimations()
{
for(U32 n = 0; n < mScene->mNumAnimations; ++n)
{
Con::printf("[ASSIMP] Animation Found: %s", mScene->mAnimations[n]->mName.C_Str());
AssimpAppSequence* newAssimpSeq = new AssimpAppSequence(mScene->mAnimations[n]);
appSequences.push_back(newAssimpSeq);
}
}
void AssimpShapeLoader::computeBounds(Box3F& bounds)
{
TSShapeLoader::computeBounds(bounds);
// Check if the model origin needs adjusting
bool adjustCenter = Con::getBoolVariable("$Assimp::adjustCenter", false); //ColladaUtils::getOptions().adjustCenter
bool adjustFloor = Con::getBoolVariable("$Assimp::adjustFloor", false); //ColladaUtils::getOptions().adjustFloor
if (bounds.isValidBox() && (adjustCenter || adjustFloor))
{
// Compute shape offset
Point3F shapeOffset = Point3F::Zero;
if (adjustCenter)
{
bounds.getCenter(&shapeOffset);
shapeOffset = -shapeOffset;
}
if (adjustFloor)
shapeOffset.z = -bounds.minExtents.z;
// Adjust bounds
bounds.minExtents += shapeOffset;
bounds.maxExtents += shapeOffset;
// Now adjust all positions for root level nodes (nodes with no parent)
for (S32 iNode = 0; iNode < shape->nodes.size(); iNode++)
{
if (!appNodes[iNode]->isParentRoot())
continue;
// Adjust default translation
shape->defaultTranslations[iNode] += shapeOffset;
// Adjust animated translations
for (S32 iSeq = 0; iSeq < shape->sequences.size(); iSeq++)
{
const TSShape::Sequence& seq = shape->sequences[iSeq];
if (seq.translationMatters.test(iNode))
{
for (S32 iFrame = 0; iFrame < seq.numKeyframes; iFrame++)
{
S32 index = seq.baseTranslation + seq.translationMatters.count(iNode)*seq.numKeyframes + iFrame;
shape->nodeTranslations[index] += shapeOffset;
}
}
}
}
}
}
void AssimpShapeLoader::updateMaterialsScript(const Torque::Path &path)
{
Torque::Path scriptPath(path);
scriptPath.setFileName("materials");
scriptPath.setExtension("cs");
// First see what materials we need to update
PersistenceManager persistMgr;
for ( U32 iMat = 0; iMat < AppMesh::appMaterials.size(); iMat++ )
{
AssimpAppMaterial *mat = dynamic_cast<AssimpAppMaterial*>( AppMesh::appMaterials[iMat] );
if ( mat )
{
Material *mappedMat;
if ( Sim::findObject( MATMGR->getMapEntry( mat->getName() ), mappedMat ) )
{
// Only update existing materials if forced to
if (Con::getBoolVariable("$Assimp::ForceUpdateMats", false))
{
mat->initMaterial(scriptPath, mappedMat);
persistMgr.setDirty(mappedMat);
}
}
else
{
// Create a new material definition
persistMgr.setDirty( mat->createMaterial( scriptPath ), scriptPath.getFullPath() );
}
}
}
if ( persistMgr.getDirtyList().empty() )
return;
persistMgr.saveDirty();
}
/// Check if an up-to-date cached DTS is available for this DAE file
bool AssimpShapeLoader::canLoadCachedDTS(const Torque::Path& path)
{
// Generate the cached filename
Torque::Path cachedPath(path);
cachedPath.setExtension("cached.dts");
// Check if a cached DTS newer than this file is available
FileTime cachedModifyTime;
if (Platform::getFileTimes(cachedPath.getFullPath(), NULL, &cachedModifyTime))
{
bool forceLoad = Con::getBoolVariable("$assimp::forceLoad", false);
FileTime daeModifyTime;
if (!Platform::getFileTimes(path.getFullPath(), NULL, &daeModifyTime) ||
(!forceLoad && (Platform::compareFileTimes(cachedModifyTime, daeModifyTime) >= 0) ))
{
// Original file not found, or cached DTS is newer
return true;
}
}
return false;
}
bool AssimpShapeLoader::ignoreNode(const String& name)
{
// Do not add AssimpFbx dummy nodes to the TSShape. See: Assimp::FBX::ImportSettings::preservePivots
// https://github.com/assimp/assimp/blob/master/code/FBXImportSettings.h#L116-L135
if (name.find("_$AssimpFbx$_") != String::NPos)
return true;
return false;
}
void AssimpShapeLoader::detectDetails()
{
// Set LOD option
bool singleDetail = true;
switch (Con::getIntVariable("$Assimp::lodType", 0))
{
case ColladaUtils::ImportOptions::DetectDTS:
// Check for a baseXX->startXX hierarchy at the top-level, if we find
// one, use trailing numbers for LOD, otherwise use a single size
for (S32 iNode = 0; singleDetail && (iNode < mScene->mRootNode->mNumChildren); iNode++) {
aiNode* node = mScene->mRootNode->mChildren[iNode];
if (node && dStrStartsWith(node->mName.C_Str(), "base")) {
for (S32 iChild = 0; iChild < node->mNumChildren; iChild++) {
aiNode* child = node->mChildren[iChild];
if (child && dStrStartsWith(child->mName.C_Str(), "start")) {
singleDetail = false;
break;
}
}
}
}
break;
case ColladaUtils::ImportOptions::SingleSize:
singleDetail = true;
break;
case ColladaUtils::ImportOptions::TrailingNumber:
singleDetail = false;
break;
default:
break;
}
AssimpAppMesh::fixDetailSize(singleDetail, Con::getIntVariable("$Assimp::singleDetailSize", 2));
}
void AssimpShapeLoader::extractTexture(U32 index, aiTexture* pTex)
{ // Cache an embedded texture to disk
updateProgress(Load_EnumerateScene, "Extracting Textures...", mScene->mNumTextures, index);
Con::printf("[Assimp] Extracting Texture %s, W: %d, H: %d, %d of %d, format hint: (%s)", pTex->mFilename.C_Str(),
pTex->mWidth, pTex->mHeight, index, mScene->mNumTextures, pTex->achFormatHint);
// Create the texture filename
String cleanFile = AppMaterial::cleanString(TSShapeLoader::getShapePath().getFileName());
String texName = String::ToString("%s_cachedTex%d", cleanFile.c_str(), index);
Torque::Path texPath = shapePath;
texPath.setFileName(texName);
if (pTex->mHeight == 0)
{ // Compressed format, write the data directly to disc
texPath.setExtension(pTex->achFormatHint);
FileStream *outputStream;
if ((outputStream = FileStream::createAndOpen(texPath.getFullPath(), Torque::FS::File::Write)) != NULL)
{
outputStream->setPosition(0);
outputStream->write(pTex->mWidth, pTex->pcData);
outputStream->close();
delete outputStream;
}
}
else
{ // Embedded pixel data, fill a bitmap and save it.
GFXTexHandle shapeTex;
shapeTex.set(pTex->mWidth, pTex->mHeight, GFXFormatR8G8B8A8_SRGB, &GFXDynamicTextureSRGBProfile,
String::ToString("AssimpShapeLoader (%s:%i)", __FILE__, __LINE__), 1, 0);
GFXLockedRect *rect = shapeTex.lock();
for (U32 y = 0; y < pTex->mHeight; ++y)
{
for (U32 x = 0; x < pTex->mWidth; ++x)
{
U32 targetIndex = (y * rect->pitch) + (x * 4);
U32 sourceIndex = ((y * pTex->mWidth) + x) * 4;
rect->bits[targetIndex] = pTex->pcData[sourceIndex].r;
rect->bits[targetIndex + 1] = pTex->pcData[sourceIndex].g;
rect->bits[targetIndex + 2] = pTex->pcData[sourceIndex].b;
rect->bits[targetIndex + 3] = pTex->pcData[sourceIndex].a;
}
}
shapeTex.unlock();
texPath.setExtension("png");
shapeTex->dumpToDisk("PNG", texPath.getFullPath());
}
}
//-----------------------------------------------------------------------------
/// This function is invoked by the resource manager based on file extension.
TSShape* assimpLoadShape(const Torque::Path &path)
{
// TODO: add .cached.dts generation.
// Generate the cached filename
Torque::Path cachedPath(path);
cachedPath.setExtension("cached.dts");
// Check if an up-to-date cached DTS version of this file exists, and
// if so, use that instead.
if (AssimpShapeLoader::canLoadCachedDTS(path))
{
FileStream cachedStream;
cachedStream.open(cachedPath.getFullPath(), Torque::FS::File::Read);
if (cachedStream.getStatus() == Stream::Ok)
{
TSShape *shape = new TSShape;
bool readSuccess = shape->read(&cachedStream);
cachedStream.close();
if (readSuccess)
{
#ifdef TORQUE_DEBUG
Con::printf("Loaded cached shape from %s", cachedPath.getFullPath().c_str());
#endif
return shape;
}
else
delete shape;
}
Con::warnf("Failed to load cached shape from %s", cachedPath.getFullPath().c_str());
}
if (!Torque::FS::IsFile(path))
{
// File does not exist, bail.
return NULL;
}
AssimpShapeLoader loader;
TSShape* tss = loader.generateShape(path);
if (tss)
{
TSShapeLoader::updateProgress(TSShapeLoader::Load_Complete, "Import complete");
Con::printf("[ASSIMP] Shape created successfully.");
// Cache the model to a DTS file for faster loading next time.
FileStream dtsStream;
if (dtsStream.open(cachedPath.getFullPath(), Torque::FS::File::Write))
{
Con::printf("Writing cached shape to %s", cachedPath.getFullPath().c_str());
tss->write(&dtsStream);
}
loader.updateMaterialsScript(path);
}
loader.releaseImport();
return tss;
}
DefineEngineFunction(GetShapeInfo, GuiTreeViewCtrl*, (String filePath), ,
"Returns a list of supported shape formats in filter form.\n"
"Example output: DSQ Files|*.dsq|COLLADA Files|*.dae|")
{
Assimp::Importer importer;
GuiTreeViewCtrl* treeObj = new GuiTreeViewCtrl();
treeObj->registerObject();
Torque::Path path = Torque::Path(filePath);
// Attempt to import with Assimp.
const aiScene* shapeScene = importer.ReadFile(path.getFullPath().c_str(), (aiProcessPreset_TargetRealtime_Quality | aiProcess_CalcTangentSpace)
& ~aiProcess_RemoveRedundantMaterials & ~aiProcess_GenSmoothNormals);
//Populate info
S32 meshItem = treeObj->insertItem(0, "Shape", String::ToString("%i", shapeScene->mNumMeshes));
S32 matItem = treeObj->insertItem(0, "Materials", String::ToString("%i", shapeScene->mNumMaterials));
S32 animItem = treeObj->insertItem(0, "Animations", String::ToString("%i", shapeScene->mNumAnimations));
S32 lightsItem = treeObj->insertItem(0, "Lights", String::ToString("%i", shapeScene->mNumLights));
S32 texturesItem = treeObj->insertItem(0, "Textures", String::ToString("%i", shapeScene->mNumTextures));
//S32 meshItem = ->insertItem(0, "Cameras", String::ToString("%s", shapeScene->mNumCameras));
//Details!
for (U32 i = 0; i < shapeScene->mNumMeshes; i++)
{
treeObj->insertItem(meshItem, String::ToString("%s", shapeScene->mMeshes[i]->mName.C_Str()));
}
for (U32 i = 0; i < shapeScene->mNumMaterials; i++)
{
aiMaterial* aiMat = shapeScene->mMaterials[i];
aiString matName;
aiMat->Get(AI_MATKEY_NAME, matName);
aiString texPath;
aiMat->GetTexture(aiTextureType::aiTextureType_DIFFUSE, 0, &texPath);
treeObj->insertItem(matItem, String::ToString("%s", matName.C_Str()), String::ToString("%s", texPath.C_Str()));
}
for (U32 i = 0; i < shapeScene->mNumAnimations; i++)
{
treeObj->insertItem(animItem, String::ToString("%s", shapeScene->mAnimations[i]->mName.C_Str()));
}
/*for (U32 i = 0; i < shapeScene->mNumLights; i++)
{
treeObj->insertItem(lightsItem, String::ToString("%s", shapeScene->mLights[i]->mType));
}*/
return treeObj;
}

View file

@ -0,0 +1,57 @@
//-----------------------------------------------------------------------------
// Copyright (c) 2012 GarageGames, LLC
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
//-----------------------------------------------------------------------------
#ifndef _ASSIMP_SHAPELOADER_H_
#define _ASSIMP_SHAPELOADER_H_
#ifndef _TSSHAPELOADER_H_
#include "ts/loader/tsShapeLoader.h"
#endif
#include <assimp/texture.h>
//-----------------------------------------------------------------------------
class AssimpShapeLoader : public TSShapeLoader
{
friend TSShape* assimpLoadShape(const Torque::Path &path);
protected:
const struct aiScene* mScene;
virtual bool ignoreNode(const String& name);
void detectDetails();
void extractTexture(U32 index, aiTexture* pTex);
public:
AssimpShapeLoader();
~AssimpShapeLoader();
void releaseImport();
void enumerateScene();
void updateMaterialsScript(const Torque::Path &path);
void processAnimations();
void computeBounds(Box3F& bounds);
static bool canLoadCachedDTS(const Torque::Path& path);
};
#endif // _ASSIMP_SHAPELOADER_H_

View file

@ -30,7 +30,9 @@
using namespace ColladaUtils;
String cleanString(const String& str)
#ifndef TORQUE_ASSIMP
String AppMaterial::cleanString(const String& str)
{
String cleanStr(str);
@ -46,6 +48,8 @@ String cleanString(const String& str)
return cleanStr;
}
#endif // !TORQUE_ASSIMP
//------------------------------------------------------------------------------
ColladaAppMaterial::ColladaAppMaterial(const char* matName)

View file

@ -35,6 +35,8 @@ struct AppMaterial
virtual String getName() const { return "unnamed"; }
virtual U32 getFlags() { return flags; }
virtual F32 getReflectance() { return reflectance; }
static String cleanString(const String& str);
};
#endif // _APPMATERIAL_H_

View file

@ -36,7 +36,7 @@
class AppSequence
{
public:
S32 fps;
F32 fps;
public:
AppSequence() { }

View file

@ -1325,6 +1325,17 @@ String TSShapeLoader::getFormatFilters()
return output.end();
}
bool TSShapeLoader::isSupportedFormat(String extension)
{
String extLower = String::ToLower(extension);
for (U32 n = 0; n < smFormats.size(); ++n)
{
if (smFormats[n].mExtension.equal(extLower))
return true;
}
return false;
}
DefineEngineFunction( getFormatExtensions, const char*, ( ),,
"Returns a list of supported shape format extensions separated by tabs."
"Example output: *.dsq TAB *.dae TAB")
@ -1338,3 +1349,8 @@ DefineEngineFunction( getFormatFilters, const char*, ( ),,
{
return Con::getReturnBuffer(TSShapeLoader::getFormatFilters());
}
DefineEngineFunction(isSupportedFormat, bool, (const char* extension), , "")
{
return TSShapeLoader::isSupportedFormat(extension);
}

View file

@ -57,6 +57,7 @@ public:
static void addFormat(String name, String extension);
static String getFormatExtensions();
static String getFormatFilters();
static bool isSupportedFormat(String extension);
public:
enum eLoadPhases

View file

@ -41,6 +41,10 @@
extern TSShape* loadColladaShape(const Torque::Path &path);
#endif
#ifdef TORQUE_ASSIMP
extern TSShape* assimpLoadShape(const Torque::Path &path);
#endif
/// most recent version -- this is the version we write
S32 TSShape::smVersion = 28;
/// the version currently being read...valid only during a read
@ -2163,9 +2167,23 @@ template<> void *Resource<TSShape>::create(const Torque::Path &path)
}
else
{
Con::errorf( "Resource<TSShape>::create - '%s' has an unknown file format", path.getFullPath().c_str() );
delete ret;
return NULL;
//Con::errorf( "Resource<TSShape>::create - '%s' has an unknown file format", path.getFullPath().c_str() );
//delete ret;
//return NULL;
// andrewmac: Open Asset Import Library
#ifdef TORQUE_ASSIMP
ret = assimpLoadShape(path);
readSuccess = (ret != NULL);
#endif
// andrewmac : I could have used another conditional macro but I think this is suffice:
if (!readSuccess)
{
Con::errorf("Resource<TSShape>::create - '%s' has an unknown file format", path.getFullPath().c_str());
delete ret;
return NULL;
}
}
if( !readSuccess )