/Game/Src/ColladaMeshFormat/ColladaMeshFormatLoader.cs
C# | 3578 lines | 2797 code | 634 blank | 147 comment | 483 complexity | b9461053347adf456fb7fc9c081bed39 MD5 | raw file
Large files files are truncated, but you can click here to view the full file
- // Copyright (C) 2006-2011 NeoAxis Group Ltd.
- using System;
- using System.Collections.Generic;
- using System.Runtime.InteropServices;
- using System.IO;
- using Engine;
- using Engine.FileSystem;
- using Engine.Renderer;
- using Engine.MathEx;
- using Engine.Utils;
- using GAPLA_COMMON;
- using System.Xml;
- using GeneralMeshUtils;
-
- namespace ColladaMeshFormat
- {
- [CustomMeshFormatLoaderExtensions( new string[] { "dae" } )]
- public class ColladaMeshFormatLoader : CustomMeshFormatLoader
- {
- string currentFileName;
- Mesh currentMesh;
-
- float globalScale = 1;
- bool yAxisUp = true;
- ColladaExportingTools colladaExportingTool = ColladaExportingTools.Unknown;
- //parsed material info
- Dictionary<string, string> generatedEffects;//<effectId, materialId>
- Dictionary<string, string> generatedBindedMaterials;//<materialId, originalMaterialName>
- Dictionary<string, MySceneMaterial> generatedMaterials;//<material name, material>
- Dictionary<string, string> generatedImages;//<imageId, textureFilePath>
- Dictionary<string, GeometryItem> generatedGeometries;
- List<MySceneSubMesh> generatedSubMeshes;
-
- /////////////////////////////////////////////////////////////////////////////////////////////
-
- class MySceneMaterial : ISceneMaterial
- {
- string name;
- ColorValue diffuseColor = new ColorValue( 1, 1, 1 );
- string diffuse1Map = "";
- string diffuse2Map = "";
- string diffuse3Map = "";
- string diffuse4Map = "";
- string diffuse1TexCoord = "";
- string diffuse2TexCoord = "";
- string diffuse3TexCoord = "";
- string diffuse4TexCoord = "";
- string diffuse2MapBlending = "";
- string diffuse3MapBlending = "";
- string diffuse4MapBlending = "";
- string specularMap = "";
- string specularTexCoord;
- ColorValue specularColor;
- string emissionMap = "";
- string emissionTexCoord;
- ColorValue emissionColor;
- float shininess;
- float transparency;
- ColorValue transparentColor;
- bool opaque;
- bool culling = true;
-
- //
-
- public MySceneMaterial( string name )
- {
- this.name = name;
- }
-
- public string Name
- {
- get { return name; }
- set { name = value; }
- }
-
- public ColorValue DiffuseColor
- {
- get { return diffuseColor; }
- set { diffuseColor = value; }
- }
-
- public string Diffuse1Map
- {
- get { return diffuse1Map; }
- set { diffuse1Map = value; }
- }
-
- public string Diffuse2Map
- {
- get { return diffuse2Map; }
- set { diffuse2Map = value; }
- }
-
- public string Diffuse3Map
- {
- get { return diffuse3Map; }
- set { diffuse3Map = value; }
- }
-
- public string Diffuse4Map
- {
- get { return diffuse4Map; }
- set { diffuse4Map = value; }
- }
-
- public string Diffuse1TexCoord
- {
- get { return diffuse1TexCoord; }
- set { diffuse1TexCoord = value; }
- }
-
- public string Diffuse2TexCoord
- {
- get { return diffuse2TexCoord; }
- set { diffuse2TexCoord = value; }
- }
-
- public string Diffuse3TexCoord
- {
- get { return diffuse3TexCoord; }
- set { diffuse3TexCoord = value; }
- }
-
- public string Diffuse4TexCoord
- {
- get { return diffuse4TexCoord; }
- set { diffuse4TexCoord = value; }
- }
-
- public string Diffuse2MapBlending
- {
- get { return diffuse2MapBlending; }
- set { diffuse2MapBlending = value; }
- }
-
- public string Diffuse3MapBlending
- {
- get { return diffuse3MapBlending; }
- set { diffuse3MapBlending = value; }
- }
-
- public string Diffuse4MapBlending
- {
- get { return diffuse4MapBlending; }
- set { diffuse4MapBlending = value; }
- }
-
- public float Transparency
- {
- get { return transparency; }
- set { transparency = value; }
- }
-
- public ColorValue TransparentColor
- {
- get { return transparentColor; }
- set { transparentColor = value; }
- }
-
- public bool Opaque
- {
- get { return opaque; }
- set { opaque = value; }
- }
-
- public float GetAlpha()
- {
- if( opaque )
- return transparency;
- if( transparentColor.Red > 0 || transparentColor.Green > 0 || transparentColor.Blue > 0 )
- return 1.0f - transparency;
- return 1;
- }
-
- public string SpecularMap
- {
- get { return specularMap; }
- set { specularMap = value; }
- }
-
- public string SpecularTexCoord
- {
- get { return specularTexCoord; }
- set { specularTexCoord = value; }
- }
-
- public ColorValue SpecularColor
- {
- get { return specularColor; }
- set { specularColor = value; }
- }
-
- public float Shininess
- {
- get { return shininess; }
- set { shininess = value; }
- }
-
- public string EmissionMap
- {
- get { return emissionMap; }
- set { emissionMap = value; }
- }
-
- public string EmissionTexCoord
- {
- get { return emissionTexCoord; }
- set { emissionTexCoord = value; }
- }
-
- public ColorValue EmissionColor
- {
- get { return emissionColor; }
- set { emissionColor = value; }
- }
-
- public bool Culling
- {
- get { return culling; }
- set { culling = value; }
- }
- }
-
- /////////////////////////////////////////////////////////////////////////////////////////////
-
- class GeometryItem
- {
- string id;
- SubMesh[] subMeshes;
-
- //
-
- public class SubMesh
- {
- SubMeshVertex[] vertices;
- int[] indices;
- int textureCoordCount;
- bool vertexColors;
- MySceneMaterial material;
-
- //
-
- public SubMesh( SubMeshVertex[] vertices, int[] indices, int textureCoordCount,
- bool vertexColors, MySceneMaterial material )
- {
- this.vertices = vertices;
- this.indices = indices;
- this.textureCoordCount = textureCoordCount;
- this.vertexColors = vertexColors;
- this.material = material;
- }
-
- public SubMeshVertex[] Vertices
- {
- get { return vertices; }
- }
-
- public int[] Indices
- {
- get { return indices; }
- }
-
- public int TextureCoordCount
- {
- get { return textureCoordCount; }
- }
-
- public bool VertexColors
- {
- get { return vertexColors; }
- }
-
- public MySceneMaterial Material
- {
- get { return material; }
- }
- }
-
- //
-
- public GeometryItem( string id, SubMesh[] subMeshes )
- {
- this.id = id;
- this.subMeshes = subMeshes;
- }
-
- public string Id
- {
- get { return id; }
- }
-
- public SubMesh[] SubMeshes
- {
- get { return subMeshes; }
- }
- }
-
- /////////////////////////////////////////////////////////////////////////////////////////////
-
- class MySceneSubMesh : ISceneSubMesh
- {
- SubMeshVertex[] vertices;
- int[] indices;
- int textureCoordCount;
- bool vertexColors;
- MySceneMaterial material;
-
- //
-
- public MySceneSubMesh( SubMeshVertex[] vertices, int[] indices, int textureCoordCount,
- bool vertexColors, MySceneMaterial material )
- {
- this.vertices = vertices;
- this.indices = indices;
- this.textureCoordCount = textureCoordCount;
- this.vertexColors = vertexColors;
- this.material = material;
- }
-
- public void GetGeometry( out SubMeshVertex[] vertices, out int[] indices )
- {
- vertices = this.vertices;
- indices = this.indices;
- }
-
- public int GetTextureCoordCount()
- {
- return textureCoordCount;
- }
-
- public bool VertexColors
- {
- get { return vertexColors; }
- }
-
- public ISceneMaterial Material
- {
- get { return material; }
- }
-
- public SceneBoneAssignmentItem[] GetVertexBoneAssignment( int vertexIndex )
- {
- return null;
- }
-
- public void GetVerticesByTime( float timeFrame, out Vec3[] vertices, bool skeletonOn )
- {
- vertices = null;
- skeletonOn = false;
- }
-
- public bool HasPoses()
- {
- return false;
- }
-
- public void GetPoses( out PoseInfo[] poses )
- {
- poses = null;
- }
-
- public void GetPoseReferenceByTime( float timeFrame, out PoseReference[] poseReferences )
- {
- poseReferences = null;
- }
-
- public bool AllowCollision
- {
- get { return true; }
- }
-
- public UVUnwrapChannels UVUnwrapChannel
- {
- get { return UVUnwrapChannels.None; }
- }
-
- public bool HasVertexColors()
- {
- return vertexColors;
- }
- }
-
- /////////////////////////////////////////////////////////////////////////////////////////////
-
- class MyMeshSceneObject : IMeshSceneObject
- {
- MySceneSubMesh[] subMeshes;
-
- //
-
- public MyMeshSceneObject( MySceneSubMesh[] subMeshes )
- {
- this.subMeshes = subMeshes;
- }
-
-
- public ISceneSubMesh[] SubMeshes
- {
- get { return subMeshes; }
- }
-
- public IList<ISceneBone> SkinBones
- {
- get
- {
- return new ISceneBone[ 0 ];
- }
- }
-
- public int AnimationFrameRate
- {
- get
- {
- return 0;
- }
- }
-
- public void GetBoundsByTime( float timeFrame, out Bounds bounds, out float radius )
- {
- Log.Fatal( "GetBoundsByTime" );
- bounds = Bounds.Cleared;
- radius = -1;
- }
- }
-
- /////////////////////////////////////////////////////////////////////////////////////////////
-
- class SourceItem
- {
- string id;
- float[] data;
- int offset;
- int stride;
-
- //
-
- public SourceItem( string id, float[] data, int offset, int stride )
- {
- this.id = id;
- this.data = data;
- this.offset = offset;
- this.stride = stride;
- }
-
- public string Id
- {
- get { return id; }
- }
-
- public float[] Data
- {
- get { return data; }
- }
-
- public int Offset
- {
- get { return offset; }
- }
-
- public int Stride
- {
- get { return stride; }
- }
-
- public Vec2 GetItemVec2( int index )
- {
- int index2 = offset + index * stride;
- return new Vec2(
- data[ index2 + 0 ],
- data[ index2 + 1 ] );
- }
-
- public Vec3 GetItemVec3( int index )
- {
- int index2 = offset + index * stride;
- return new Vec3(
- data[ index2 + 0 ],
- data[ index2 + 1 ],
- data[ index2 + 2 ] );
- }
- }
-
- /////////////////////////////////////////////////////////////////////////////////////////////
-
- enum ChannelTypes
- {
- Unknown,
-
- POSITION,
- NORMAL,
- COLOR,
- TEXCOORD0,
- TEXCOORD1,
- TEXCOORD2,
- TEXCOORD3,
- TANGENT,
- }
-
- /////////////////////////////////////////////////////////////////////////////////////////////
-
- enum ColladaExportingTools
- {
- Unknown,
-
- FBX,
- XSI,
- Blender,
- Unwrap3d,
- NeoAxis,
- }
-
- /////////////////////////////////////////////////////////////////////////////////////////////
-
- void Error( string format, params object[] args )
- {
- Log.Warning(
- string.Format( "ColladaMeshFormatLoader: Cannot load file \"{0}\". ", currentFileName ) +
- string.Format( format, args ) );
- }
-
- static float[] ConvertStringToFloatArray( string str )
- {
- if( string.IsNullOrEmpty( str ) )
- return new float[ 0 ];
-
- string fixedStr = str.Replace( ',', '.' );
-
- string[] strings = fixedStr.Split( new char[] { ' ', '\n', '\t', '\r' },
- StringSplitOptions.RemoveEmptyEntries );
-
- float[] array = new float[ strings.Length ];
-
- for( int n = 0; n < strings.Length; n++ )
- {
- float value;
- if( !float.TryParse( strings[ n ], out value ) )
- return null;
-
- array[ n ] = value;
- }
-
- return array;
- }
-
- static ColorValue ConvertStringToColorValue( string str )
- {
- if( string.IsNullOrEmpty( str ) )
- return new ColorValue();
-
- string fixedStr = str.Replace( ',', '.' );
-
- string[] strings = fixedStr.Split( new char[] { ' ', '\n', '\t', '\r' },
- StringSplitOptions.RemoveEmptyEntries );
-
- ColorValue color = new ColorValue();
-
- if( strings.Length >= 3 )
- {
- float red;
- if( !float.TryParse( strings[ 0 ], out red ) )
- return color;
-
- float green;
- if( !float.TryParse( strings[ 1 ], out green ) )
- return color;
-
- float blue;
- if( !float.TryParse( strings[ 2 ], out blue ) )
- return color;
-
- color.Red = red;
- color.Green = green;
- color.Blue = blue;
-
- if( strings.Length == 4 )
- {
- float alpha;
- if( !float.TryParse( strings[ 3 ], out alpha ) )
- return color;
- color.Alpha = alpha;
- }
- }
-
- return color;
- }
-
- static int[] ConvertStringToIntArray( string str )
- {
- if( string.IsNullOrEmpty( str ) )
- return new int[ 0 ];
-
- string fixedStr = str.Replace( ',', '.' );
-
- string[] strings = fixedStr.Split( new char[] { ' ', '\n', '\t', '\r' },
- StringSplitOptions.RemoveEmptyEntries );
-
- int[] array = new int[ strings.Length ];
-
- for( int n = 0; n < strings.Length; n++ )
- {
- int value;
- if( !int.TryParse( strings[ n ], out value ) )
- return null;
-
- array[ n ] = value;
- }
-
- return array;
- }
-
- static Vec2 ConvertColladaTexCoordToNeoAxisTexCoord( Vec2 colladaTexCoord )
- {
- return new Vec2( colladaTexCoord.X, 1 - colladaTexCoord.Y );
- }
-
- ColladaExportingTools GetColladaExportingTool( string authoringTool )
- {
- if( authoringTool.Contains( "FBX" ) )
- return ColladaExportingTools.FBX;
-
- if( authoringTool.Contains( "Softimage" ) )
- return ColladaExportingTools.XSI;
-
- if( authoringTool.Contains( "Blender" ) )
- return ColladaExportingTools.Blender;
-
- if( authoringTool.Contains( "Unwrap3D" ) )
- return ColladaExportingTools.Unwrap3d;
-
- if( authoringTool.Contains( "NeoAxis" ) )
- return ColladaExportingTools.NeoAxis;
-
- return ColladaExportingTools.Unknown;
- }
-
- string GetVirtualPath( string colladaFileDirectory, string path )
- {
- if( path.IndexOf( "file://" ) >= 0 )
- path = path.Remove( 0, 7 );
- if( path.Length > 0 && ( path[ 0 ] == '/' || path[ 0 ] == '\\' ) )
- path = path.Remove( 0, 1 );
-
- if( Path.IsPathRooted( path ) )
- {
- string virtualPath = VirtualFileSystem.GetVirtualPathByReal( path );
- if( string.IsNullOrEmpty( virtualPath ) )
- virtualPath = Path.Combine( colladaFileDirectory, Path.GetFileName( path ) );
- return virtualPath;
- }
- else
- {
- if( path.Length > 1 && path[ 0 ] == '.' && ( path[ 1 ] == '/' || path[ 1 ] == '\\' ) )
- path = path.Remove( 0, 2 );
- return Path.Combine( colladaFileDirectory, path );
- }
- }
-
- ShaderBaseMaterial.DiffuseMapItem.MapBlendingTypes GetMapBlendingType( string blendingMode )
- {
- if( blendingMode == "ADD" )
- return ShaderBaseMaterial.DiffuseMapItem.MapBlendingTypes.Add;
-
- if( blendingMode == "BLEND" )
- return ShaderBaseMaterial.DiffuseMapItem.MapBlendingTypes.AlphaBlend;
-
- return ShaderBaseMaterial.DiffuseMapItem.MapBlendingTypes.Modulate;
- }
-
- ShaderBaseMaterial.TexCoordIndexes GetNeoAxisTexCoordByColladaTexCoord( string texCoord )
- {
- if( texCoord == "CHANNEL1" )
- return ShaderBaseMaterial.TexCoordIndexes.TexCoord1;
- if( texCoord == "CHANNEL2" )
- return ShaderBaseMaterial.TexCoordIndexes.TexCoord2;
- if( texCoord == "CHANNEL3" )
- return ShaderBaseMaterial.TexCoordIndexes.TexCoord3;
-
- return ShaderBaseMaterial.TexCoordIndexes.TexCoord0;
- }
-
- MySceneMaterial GetOrCreateMaterial( string materialName )
- {
- MySceneMaterial material;
- if( !generatedMaterials.TryGetValue( materialName, out material ) )
- {
- material = new MySceneMaterial( materialName );
- generatedMaterials.Add( material.Name, material );
- }
- return material;
- }
-
- bool ParseSource( XmlNode sourceNode, out SourceItem source )
- {
- source = null;
-
- string id = XmlUtils.GetAttribute( sourceNode, "id" );
-
- XmlNode floatArrayNode = XmlUtils.FindChildNode( sourceNode, "float_array" );
- if( floatArrayNode == null )
- {
- Error( "\"float_array\" node is not exists. Source: \"{0}\".", id );
- return false;
- }
-
- int floatsCount;
- if( !int.TryParse( XmlUtils.GetAttribute( floatArrayNode, "count" ), out floatsCount ) )
- {
- Error( "Invalid \"count\" attribute of floats array. Source: \"{0}\".", id );
- return false;
- }
-
- float[] data = ConvertStringToFloatArray( floatArrayNode.InnerText );
- if( data == null )
- {
- Error( "Cannot read array with name \"{0}\".", XmlUtils.GetAttribute( floatArrayNode, "id" ) );
- return false;
- }
-
- if( data.Length != floatsCount )
- {
- Error( "Invalid amount of items in \"float_array\". Required amount: \"{0}\". " +
- "Real amount: \"{1}\". Source: \"{2}\".", floatsCount, data.Length, id );
- return false;
- }
-
- XmlNode techniqueCommonNode = XmlUtils.FindChildNode( sourceNode, "technique_common" );
- if( techniqueCommonNode == null )
- {
- Error( "\"technique_common\" node is not exists. Source: \"{0}\".", id );
- return false;
- }
-
- XmlNode accessorNode = XmlUtils.FindChildNode( techniqueCommonNode, "accessor" );
- if( accessorNode == null )
- {
- Error( "\"accessor\" node is not exists. Source: \"{0}\".", id );
- return false;
- }
-
- int offset = 0;
- {
- string offsetAttribute = XmlUtils.GetAttribute( accessorNode, "offset" );
- if( !string.IsNullOrEmpty( offsetAttribute ) )
- {
- if( !int.TryParse( offsetAttribute, out offset ) )
- {
- Error( "Invalid \"offset\" attribute of accessor. Source: \"{0}\".", id );
- return false;
- }
- }
- }
-
- int stride = 1;
- {
- string strideAttribute = XmlUtils.GetAttribute( accessorNode, "stride" );
- if( !string.IsNullOrEmpty( strideAttribute ) )
- {
- if( !int.TryParse( strideAttribute, out stride ) )
- {
- Error( "Invalid \"stride\" attribute of accessor. Source: \"{0}\".", id );
- return false;
- }
- }
- }
-
- int count;
- if( !int.TryParse( XmlUtils.GetAttribute( accessorNode, "count" ), out count ) )
- {
- Error( "Invalid \"count\" attribute of accessor. Source: \"{0}\".", id );
- return false;
- }
-
- source = new SourceItem( id, data, offset, stride );
- return true;
- }
-
- bool ParseMeshSources( XmlNode meshNode, out List<SourceItem> sources )
- {
- sources = new List<SourceItem>();
-
- foreach( XmlNode childNode in meshNode.ChildNodes )
- {
- if( childNode.Name != "source" )
- continue;
-
- SourceItem source;
- if( !ParseSource( childNode, out source ) )
- return false;
-
- sources.Add( source );
- }
-
- return true;
- }
-
- string GetIdFromURL( string url )
- {
- if( string.IsNullOrEmpty( url ) )
- return "";
- if( url[ 0 ] != '#' )
- return "";
- return url.Substring( 1 );
- }
-
- bool ParseInputNode( string geometryId, Dictionary<string, string> vertexSemanticDictionary,
- XmlNode inputNode, out int offset, out string sourceId, out ChannelTypes channelType )
- {
- offset = 0;
- sourceId = null;
- channelType = ChannelTypes.Unknown;
-
- //offset
- if( !int.TryParse( XmlUtils.GetAttribute( inputNode, "offset" ), out offset ) )
- {
- Error( "Invalid \"offset\" attribute. Geometry: \"{0}\".", geometryId );
- return false;
- }
-
- string semantic = XmlUtils.GetAttribute( inputNode, "semantic" );
-
- //channelType
- {
- int set = 0;
- {
- string setAsString = XmlUtils.GetAttribute( inputNode, "set" );
- if( !string.IsNullOrEmpty( setAsString ) )
- {
- if( !int.TryParse( setAsString, out set ) )
- {
- Error( "Invalid \"set\" attribute. Geometry: \"{0}\".", geometryId );
- return false;
- }
- }
- }
-
- if( semantic == "VERTEX" )
- channelType = ChannelTypes.POSITION;
- else if( semantic == "POSITION" )
- channelType = ChannelTypes.POSITION;
- else if( semantic == "NORMAL" )
- channelType = ChannelTypes.NORMAL;
- else if( semantic == "COLOR" )
- channelType = ChannelTypes.COLOR;
- else if( semantic == "TEXCOORD" )
- {
- switch( set )
- {
- case 0: channelType = ChannelTypes.TEXCOORD0; break;
- case 1: channelType = ChannelTypes.TEXCOORD1; break;
- case 2: channelType = ChannelTypes.TEXCOORD2; break;
- case 3: channelType = ChannelTypes.TEXCOORD3; break;
- default: channelType = ChannelTypes.Unknown; break;
- }
- }
- else if( semantic == "TANGENT" )
- channelType = ChannelTypes.TANGENT;
- else
- {
- channelType = ChannelTypes.Unknown;
- }
- }
-
- //sourceId
- {
- string sourceUrl = XmlUtils.GetAttribute( inputNode, "source" );
-
- sourceId = GetIdFromURL( sourceUrl );
- if( string.IsNullOrEmpty( sourceId ) )
- {
- Error( "Invalid \"source\" attribute for input node. Source url: \"{0}\". Geometry: \"{1}\".",
- sourceUrl, geometryId );
- return false;
- }
-
- if( semantic == "VERTEX" )
- {
- string newSourceId;
- if( !vertexSemanticDictionary.TryGetValue( sourceId, out newSourceId ) )
- {
- Error( "Cannot find vertices node with \"{0}\". Geometry: \"{1}\".",
- sourceId, geometryId );
- return false;
- }
- sourceId = newSourceId;
- }
- }
-
- return true;
- }
-
- bool ParseInputs( string geometryId, Dictionary<string, SourceItem> sourceDictionary,
- Dictionary<string, string> vertexSemanticDictionary, XmlNode primitiveElementsNode,
- out Pair<ChannelTypes, SourceItem>[] inputs )
- {
- inputs = null;
-
- List<Pair<ChannelTypes, SourceItem>> inputList = new List<Pair<ChannelTypes, SourceItem>>();
-
- foreach( XmlNode inputNode in primitiveElementsNode.ChildNodes )
- {
- if( inputNode.Name != "input" )
- continue;
-
- int offset;
- string sourceId;
- ChannelTypes channelType;
-
- if( !ParseInputNode( geometryId, vertexSemanticDictionary, inputNode, out offset, out sourceId,
- out channelType ) )
- {
- return false;
- }
-
- while( inputList.Count < offset + 1 )
- inputList.Add( new Pair<ChannelTypes, SourceItem>( ChannelTypes.Unknown, null ) );
-
- if( channelType != ChannelTypes.Unknown )
- {
- if( inputList[ offset ].Second != null )
- {
- Error( "Input with offset \"{0}\" is already defined.", offset );
- return false;
- }
-
- SourceItem source;
- if( !sourceDictionary.TryGetValue( sourceId, out source ) )
- {
- Error( "Source with id \"{0}\" is not exists.", sourceId );
- return false;
- }
-
- inputList[ offset ] = new Pair<ChannelTypes, SourceItem>( channelType, source );
- }
- }
-
- inputs = inputList.ToArray();
-
- return true;
- }
-
- SubMeshVertex[] GenerateSubMeshVertices( Pair<ChannelTypes, SourceItem>[] inputs, int vertexCount,
- int[] indices, int startIndex )
- {
- SubMeshVertex[] itemVertices = new SubMeshVertex[ vertexCount ];
-
- int currentIndex = startIndex;
-
- for( int nVertex = 0; nVertex < itemVertices.Length; nVertex++ )
- {
- SubMeshVertex vertex = new SubMeshVertex();
-
- foreach( Pair<ChannelTypes, SourceItem> input in inputs )
- {
- ChannelTypes channelType = input.First;
- SourceItem source = input.Second;
-
- int indexValue = indices[ currentIndex ];
- currentIndex++;
-
- switch( channelType )
- {
- case ChannelTypes.POSITION:
- vertex.position = source.GetItemVec3( indexValue );
- break;
-
- case ChannelTypes.NORMAL:
- vertex.normal = source.GetItemVec3( indexValue );
- break;
-
- case ChannelTypes.TEXCOORD0:
- vertex.texCoord0 = ConvertColladaTexCoordToNeoAxisTexCoord(
- source.GetItemVec2( indexValue ) );
- break;
-
- case ChannelTypes.TEXCOORD1:
- vertex.texCoord1 = ConvertColladaTexCoordToNeoAxisTexCoord(
- source.GetItemVec2( indexValue ) );
- break;
-
- case ChannelTypes.TEXCOORD2:
- vertex.texCoord2 = ConvertColladaTexCoordToNeoAxisTexCoord(
- source.GetItemVec2( indexValue ) );
- break;
-
- case ChannelTypes.TEXCOORD3:
- vertex.texCoord3 = ConvertColladaTexCoordToNeoAxisTexCoord(
- source.GetItemVec2( indexValue ) );
- break;
-
- case ChannelTypes.COLOR:
- {
- Vec3 c = source.GetItemVec3( indexValue ); ;
- vertex.color = new ColorValue( c.X, c.Y, c.Z, 1 );
- }
- break;
-
- //maybe need use "TEXTANGENT".
- //case ChannelTypes.TANGENT:
- // vertex.tangent = source.GetItemVec3( indexValue );
- // break;
-
- }
- }
-
- itemVertices[ nVertex ] = vertex;
- }
-
- return itemVertices;
- }
-
- bool ParseMeshNode( string geometryId, XmlNode meshNode )
- {
- List<SourceItem> sources;
- if( !ParseMeshSources( meshNode, out sources ) )
- return false;
-
- Dictionary<string, SourceItem> sourceDictionary = new Dictionary<string, SourceItem>();
- foreach( SourceItem source in sources )
- sourceDictionary.Add( source.Id, source );
-
- //vertexSemanticDictionary
- Dictionary<string, string> vertexSemanticDictionary = new Dictionary<string, string>();
- {
- foreach( XmlNode verticesNode in meshNode.ChildNodes )
- {
- if( verticesNode.Name != "vertices" )
- continue;
-
- string id = XmlUtils.GetAttribute( verticesNode, "id" );
-
- XmlNode inputNode = XmlUtils.FindChildNode( verticesNode, "input" );
- if( inputNode == null )
- {
- Error( "\"input\" node is not defined for vertices node \"{0}\". Geometry: \"{1}\".",
- id, geometryId );
- return false;
- }
-
- string sourceUrl = XmlUtils.GetAttribute( inputNode, "source" );
-
- string sourceId = GetIdFromURL( sourceUrl );
- if( string.IsNullOrEmpty( sourceId ) )
- {
- Error( "Invalid \"source\" attribute for vertices node \"{0}\". Source url: \"{1}\". " +
- "Geometry: \"{2}\".", id, sourceUrl, geometryId );
- return false;
- }
-
- vertexSemanticDictionary.Add( id, sourceId );
- }
- }
-
- List<GeometryItem.SubMesh> geometrySubMeshes = new List<GeometryItem.SubMesh>();
-
- //polygons, triangles, ...
- foreach( XmlNode primitiveElementsNode in meshNode.ChildNodes )
- {
- bool lines = primitiveElementsNode.Name == "lines";
- bool linestrips = primitiveElementsNode.Name == "linestrips";
- bool polygons = primitiveElementsNode.Name == "polygons";
- bool polylist = primitiveElementsNode.Name == "polylist";
- bool triangles = primitiveElementsNode.Name == "triangles";
- bool trifans = primitiveElementsNode.Name == "trifans";
- bool tristrips = primitiveElementsNode.Name == "tristrips";
-
- if( lines || linestrips || trifans || tristrips )
- {
- Log.Warning( "\"{0}\" primitive element is not supported. Geometry: \"{1}\". " +
- "\n\nElement was skipped.", primitiveElementsNode.Name, geometryId );
- continue;
- }
-
- if( polygons || triangles || polylist )
- {
- int itemCount;
- if( !int.TryParse( XmlUtils.GetAttribute( primitiveElementsNode, "count" ), out itemCount ) )
- {
- Error( "Invalid \"count\" attribute of \"{0}\". Geometry: \"{1}\".",
- primitiveElementsNode.Name, geometryId );
- return false;
- }
-
- Pair<ChannelTypes, SourceItem>[] inputs;
- if( !ParseInputs( geometryId, sourceDictionary, vertexSemanticDictionary,
- primitiveElementsNode, out inputs ) )
- {
- return false;
- }
-
- int textureCoordCount = 0;
- bool vertexColors = false;
- {
- foreach( Pair<ChannelTypes, SourceItem> input in inputs )
- {
- ChannelTypes channelType = input.First;
-
- if( channelType >= ChannelTypes.TEXCOORD0 && channelType <= ChannelTypes.TEXCOORD3 )
- {
- int v = channelType - ChannelTypes.TEXCOORD0;
- if( ( v + 1 ) > textureCoordCount )
- textureCoordCount = v + 1;
- }
-
- if( channelType == ChannelTypes.COLOR )
- vertexColors = true;
- }
- }
-
- List<SubMeshVertex> vertices = new List<SubMeshVertex>( itemCount );
-
- if( polygons )
- {
- foreach( XmlNode pNode in primitiveElementsNode.ChildNodes )
- {
- if( pNode.Name != "p" )
- continue;
-
- int[] indexValues = ConvertStringToIntArray( pNode.InnerText );
- if( indexValues == null )
- {
- Error( "Cannot read index array of geometry \"{0}\".", geometryId );
- return false;
- }
-
- int vertexCount = indexValues.Length / inputs.Length;
- SubMeshVertex[] itemVertices = GenerateSubMeshVertices( inputs,
- vertexCount, indexValues, 0 );
-
- //generate triangles
- for( int n = 1; n < itemVertices.Length - 1; n++ )
- {
- vertices.Add( itemVertices[ 0 ] );
- vertices.Add( itemVertices[ n ] );
- vertices.Add( itemVertices[ n + 1 ] );
- }
- }
- }
-
- if( triangles )
- {
- XmlNode pNode = XmlUtils.FindChildNode( primitiveElementsNode, "p" );
-
- if( pNode != null )
- {
- int[] indexValues = ConvertStringToIntArray( pNode.InnerText );
- if( indexValues == null )
- {
- Error( "Cannot read \"p\" node of geometry \"{0}\".", geometryId );
- return false;
- }
-
- int vertexCount = indexValues.Length / inputs.Length;
-
- if( itemCount != vertexCount / 3 )
- {
- Error( "Invalid item amount of \"p\" node of geometry \"{0}\".", geometryId );
- return false;
- }
-
- SubMeshVertex[] itemVertices = GenerateSubMeshVertices( inputs,
- vertexCount, indexValues, 0 );
-
- //generate triangles
- for( int n = 0; n < vertexCount; n++ )
- vertices.Add( itemVertices[ n ] );
- }
- }
-
- if( polylist )
- {
- XmlNode vCountNode = XmlUtils.FindChildNode( primitiveElementsNode, "vcount" );
- XmlNode pNode = XmlUtils.FindChildNode( primitiveElementsNode, "p" );
-
- if( vCountNode != null && pNode != null )
- {
- int[] vCount = ConvertStringToIntArray( vCountNode.InnerText );
- if( vCount == null )
- {
- Error( "Cannot read \"vcount\" node of geometry \"{0}\".", geometryId );
- return false;
- }
-
- if( vCount.Length != itemCount )
- {
- Error( "Invalid item amount of \"vcount\" node of geometry \"{0}\".", geometryId );
- return false;
- }
-
- int[] indexValues = ConvertStringToIntArray( pNode.InnerText );
- if( indexValues == null )
- {
- Error( "Cannot read \"p\" node of geometry \"{0}\".", geometryId );
- return false;
- }
-
- int currentIndex = 0;
-
- foreach( int polyCount in vCount )
- {
- SubMeshVertex[] itemVertices = GenerateSubMeshVertices( inputs, polyCount,
- indexValues, currentIndex );
- currentIndex += polyCount * inputs.Length;
-
- //generate triangles
- for( int n = 1; n < itemVertices.Length - 1; n++ )
- {
- vertices.Add( itemVertices[ 0 ] );
- vertices.Add( itemVertices[ n ] );
- vertices.Add( itemVertices[ n + 1 ] );
- }
-
- }
-
- if( currentIndex != indexValues.Length )
- {
- Error( "Invalid indices of geometry \"{0}\".", geometryId );
- return false;
- }
- }
- }
-
- if( vertices.Count != 0 )
- {
- int[] indices = new int[ vertices.Count ];
- for( int n = 0; n < indices.Length; n++ )
- indices[ n ] = n;
-
- MySceneMaterial material = null;
- {
- string materialName = XmlUtils.GetAttribute( primitiveElementsNode, "material" );
- if( !string.IsNullOrEmpty( materialName ) )
- material = GetOrCreateMaterial( materialName );
- }
-
- //add to geometrySubMeshes
- GeometryItem.SubMesh geometrySubMesh = new GeometryItem.SubMesh(
- vertices.ToArray(), indices, textureCoordCount, vertexColors, material );
- geometrySubMeshes.Add( geometrySubMesh );
- }
-
- continue;
- }
- }
-
- //add to generatedGeometries
- {
- GeometryItem geometry = new GeometryItem( geometryId, geometrySubMeshes.ToArray() );
- generatedGeometries.Add( geometry.Id, geometry );
- }
-
- return true;
- }
-
- bool ParseGeometry( XmlNode geometryNode )
- {
- string geometryId = XmlUtils.GetAttribute( geometryNode, "id" );
-
- //find mesh node
- XmlNode meshNode = XmlUtils.FindChildNode( geometryNode, "mesh" );
- if( meshNode == null )
- {
- Error( "Mesh node is not exists for geometry \"{0}\".", geometryId );
- return false;
- }
-
- if( !ParseMeshNode( geometryId, meshNode ) )
- return false;
-
- return true;
- }
-
- bool ParseGeometries( XmlNode colladaNode )
- {
- foreach( XmlNode libraryGeometriesNode in colladaNode.ChildNodes )
- {
- if( libraryGeometriesNode.Name != "library_geometries" )
- continue;
-
- foreach( XmlNode geometryNode in libraryGeometriesNode.ChildNodes )
- {
- if( geometryNode.Name != "geometry" )
- continue;
-
- if( !ParseGeometry( geometryNode ) )
- return false;
- }
- }
-
- return true;
- }
-
- bool ParseNodeInstanceGeometry( Mat4 nodeTransform, XmlNode instanceGeometry )
- {
- string url = XmlUtils.GetAttribute( instanceGeometry, "url" );
-
- bool nodeTransformIdentity = nodeTransform.Equals( Mat4.Identity, .0001f );
-
- string geometryId = GetIdFromURL( url );
- if( string.IsNullOrEmpty( geometryId ) )
- {
- Error( "Invalid \"url\" attribute specified for \"instance_geometry\". Url: \"{0}\".", url );
- return false;
- }
-
- GeometryItem geometry;
- if( !generatedGeometries.TryGetValue( geometryId, out geometry ) )
- {
- Error( "Geometry with id \"{0}\" is not exists.", geometryId );
- return false;
- }
-
- foreach( GeometryItem.SubMesh geometrySubMesh in geometry.SubMeshes )
- {
- SubMeshVertex[] newVertices = new SubMeshVertex[ geometrySubMesh.Vertices.Length ];
-
- for( int n = 0; n < newVertices.Length; n++ )
- {
- SubMeshVertex vertex = geometrySubMesh.Vertices[ n ];
-
- if( !nodeTransformIdentity )
- {
- Vec3 oldPosition = vertex.position;
- vertex.position = nodeTransform * vertex.position;
- Vec3 p2 = nodeTransform * ( oldPosition + vertex.normal );
- vertex.normal = ( p2 - vertex.position ).GetNormalize();
- }
-
- newVertices[ n ] = vertex;
- }
-
- MySceneSubMesh sceneSubMesh = new MySceneSubMesh( newVertices, geometrySubMesh.Indices,
- geometrySubMesh.TextureCoordCount, geometrySubMesh.VertexColors, geometrySubMesh.Material );
- generatedSubMeshes.Add( sceneSubMesh );
- }
-
- XmlNode bindMaterialNode = XmlUtils.FindChildNode( instanceGeometry, "bind_material" );
- if( bindMaterialNode != null )
- {
- XmlNode techniqueCommonNode = XmlUtils.FindChildNode( bindMaterialNode, "technique_common" );
- if( techniqueCommonNode == null )
- {
- Error( "Technique_common node is not exists for geometry \"{0}\".", geometryId );
- return false;
- }
-
- foreach( XmlNode instanceMaterialNode in techniqueCommonNode.ChildNodes )
- {
- if( instanceMaterialNode.Name == "instance_material" )
- {
- string target = GetIdFromURL( XmlUtils.GetAttribute( instanceMaterialNode, "target" ) );
- string symbol = XmlUtils.GetAttribute( instanceMaterialNode, "symbol" );
- if( !generatedBindedMaterials.ContainsKey( target ) )
- generatedBindedMaterials.Add( target, symbol );
- }
- }
- }
-
- return true;
- }
-
- bool ParseNode( Mat4 nodeTransform, XmlNode node )
- {
- string nodeId = XmlUtils.GetAttribute( node, "id" );
-
- Mat4 currentTransform = nodeTransform;
-
- foreach( XmlNode childNode in node.ChildNodes )
- {
- if( childNode.Name == "matrix" )
- {
- float[] values = ConvertStringToFloatArray( childNode.InnerText );
- if( values == null || values.Length != 16 )
- {
- Error( "Invalid format of \"matrix\" node. Node \"{0}\".", nodeId );
- return false;
- }
- Mat4 matrix = new Mat4( values );
- currentTransform *= matrix;
- continue;
- }
-
- if( childNode.Name == "translate" )
- {
- float[] values = ConvertStringToFloatArray( childNode.InnerText );
- if( values == null || values.Length != 3 )
- {
- Error( "Invalid format of \"translate\" node. Node \"{0}\".", nodeId );
- return false;
- }
- Vec3 translate = new Vec3( values[ 0 ], values[ 1 ], values[ 2 ] );
- currentTransform *= Mat4.FromTranslate( translate );
- continue;
- }
-
- if( childNode.Name == "rotate" )
- {
- float[] values = ConvertStringToFloatArray( childNode.InnerText );
- if( values == null || values.Length != 4 )
- {
- Error( "Invalid format of \"rotate\" node. Node \"{0}\".", nodeId );
- return false;
- }
-
- Vec3 axis = new Vec3( values[ 0 ], values[ 1 ], values[ 2 ] );
- Radian angle = new Degree( values[ 3 ] ).InRadians();
-
- if( axis != Vec3.Zero )
- {
- axis.Normalize();
- float halfAngle = .5f * angle;
- float sin = MathFunctions.Sin( halfAngle );
- float cos = MathFunctions.Cos( halfAngle );
- Quat r = new Quat( axis * sin, cos );
- r.Normalize();
-
- currentTransform *= r.ToMat3().ToMat4();
- }
-
- continue;
- }
-
- if( childNode.Name == "scale" )
- {
- float[] values = ConvertStringToFloatArray( childNode.InnerText );
- if( values == null || values.Length != 3 )
- {
- Error( "Invalid format of \"scale\" node. Node \"{0}\".", nodeId );
- return false;
- }
- Vec3 scale = new Vec3( values[ 0 ], values[ 1 ], values[ 2 ] );
- currentTransform *= Mat3.FromScale( scale ).ToMat4();
- continue;
- }
-
- if( childNode.Name == "node" )
- {
- if( !ParseNode( currentTransform, childNode ) )
- return false;
-
- continue;
- }
-
- if( childNode.Name == "instance_geometry" )
- {
- if( !ParseNodeInstanceGeometry( currentTransform, childNode ) )
- return false;
-
- continue;
- }
- }
-
- return true;
- }
-
- bool ParseImage( XmlNode imageNode )
- {
- string imageId = XmlUtils.GetAttribute( imageNode, "id" );
-
- //find profile node
- XmlNode initFromNode = XmlUtils.FindChildNode( imageNode, "init_from" );
- if( initFromNode == null )
- {
- Error( "Init_from node is not exists for image \"{0}\".", imageId );
- return false;
- }
-
- generatedImages.Add( imageId, initFromNode.InnerText );
- return true;
- }
-
- bool ParseImages( XmlNode colladaNode )
- {
- foreach( XmlNode libraryImagesNode in colladaNode.ChildNodes )
- {
- if( libraryImagesNode.Name != "library_images" )
- continue;
-
- foreach( XmlNode imageNode in libraryImagesNode.ChildNodes )
- {
- if( imageNode.Name != "image" )
- continue;
-
- if( !ParseImage( imageNode ) )
- return false;
- }
- }
-
- return true;
- }
-
- bool ParseMaterial( XmlNode materialNode )
- {
- string materialId = XmlUtils.GetAttribute( materialNode, "id" );
- string materialName = XmlUtils.GetAttribute( materialNode, "name" );
-
- //find profile node
- XmlNode instanceEffectNode = XmlUtils.FindChildNode( materialNode, "instance_effect" );
- if( instanceEffectNode == null )
- {
- Error( "Instance_effect node is not exists for material \"{0}\".", materialId );
- return false;
- }
-
- string effectId = GetIdFromURL( XmlUtils.GetAttribute( instanceEffectNode, "url" ) );
-
- generatedEffects.Add( effectId, materialId );
- return true;
- }
-
- bool ParseMaterials( XmlNode colladaNode )
- {
- foreach( XmlNode libraryMaterialsNode in colladaNode.ChildNodes )
- {
- if( libraryMaterialsNode.Name != "library_materials" )
- continue;
-
- foreach( XmlNode materialNode in libraryMaterialsNode.ChildNodes )
- {
- if( materialNode.Name != "material" )
- continue;
-
- if( !ParseMaterial( materialNode ) )
- return false;
- }
- }
-
- return true;
- }
-
- bool ParseTextureBlendingMode( XmlNode textureNode, out string blendingMode,
- string materialAttributeName )
- {
- blendingMode = "";
-
- XmlNode extraNode = XmlUtils.FindChildNode( textureNode, "extra" );
- if( extraNode == null )
- {
- return true;
- }
-
- XmlNode techniqueNode = XmlUtils.FindChildNode( extraNode, "technique" );
- if( techniqueNode == null )
- {
- Error( "Technique node is not exists for texture node of matterial attribute \"{0}\".",
- materialAttributeName );
- return false;
- }
-
- XmlNode blendMode = XmlUtils.FindChildNode( techniqueNode, "blend_mode" );
- if( techniqueNode == null )
- {
- Error( "Blend_mode node is not exists for technique node of matterial attribute \"{0}\".",
- materialAttributeName );
- return false;
- }
-
- blendingMode = blendMode.InnerText;
-
- return true;
- }
-
- bool ParseEffectAttribute( XmlNode materialAttributeNode, ref ColorValue color,
- ref string textureId1, ref string textureCoord1, ref string textureId2,
- ref string textureCoord2, ref string blendingMode2, ref string textureId3,
- ref string textureCoord3, ref string blendingMode3, ref string textureId4,
- ref string textureCoord4, ref string blendingMode4 )
- {
- if( materialAttributeNode.ChildNodes.Count == 0 )
- {
- Error( "Parameter is not exists for matterial attribute \"{0}\".",
- materialAttributeNode.Name );
- return false;
- }
-
- int texturesCount = 0;
-
- foreach( XmlNode parameterNode in materialAttributeNode.ChildNodes )
- {
- if( parameterNode.Name == "texture" )
- {
- string texCoord = XmlUtils.GetAttribute( parameterNode, "texcoord" );
- string textureId = XmlUtils.GetAttribute( parameterNode, "texture" );
- string blendingMode;
- if( !ParseTextureBlendingMode( parameterNode, out blendingMode,
- materialAttributeNode.Name ) )
- return false;
-
- switch( texturesCount )
- {
- case 0:
- textureId1 = textureId;
- textureCoord1 = texCoord;
- break;
- case 1:
- textureId2 = textureId;
- textureCoord2 = texCoord;
- blendingMode2 = blendingMode;
- break;
- case 2:
- textureId3 = textureId;
- textureCoord3 = texCoord;
- blendingMode3 = blendingMode;
- break;
- case 3:
- textureId4 = textureId;
- textureCoord4 = texCoord;
- blendingMode4 = blendingMode;
- break;
- }
-
- texturesCount++;
- }
-
- if( parameterNode.Name == "color" )
- color = ConvertStringToColorValue( parameterNode.InnerText );
- }
-
- return true;
- }
-
- string GetTextureFileName( XmlNode profileNode, string imageId )
- {
- string textureFileName;
-
- //XSI specified
- foreach( XmlNode newParamNode in profileNode )
- {
- if( newParamNode.Name != "newparam" )
- continue;
-
- if( XmlUtils.GetAttribute( newParamNode, "sid" ) == imageId )
- {
- XmlNode sampler2D = XmlUtils.FindChildNode( newParamNode, "sampler2D" );
- if( sampler2D == null )
- continue;
-
- XmlNode source = XmlUtils.FindChildNode( sampler2D, "source" );
- if( source == null )
- continue;
-
- string surfaceId = source.InnerText;
-
- foreach( XmlNode newParamSurfaceNode in profileNode )
- {
- if( newParamNode.Name != "newparam" )
- continue;
-
- if( XmlUtils.GetAttribute( newParamSurfaceNode, "sid" ) == surfaceId )
- {
- XmlNode surface = XmlUtils.FindChildNode( newParamSurfaceNode, "surface" );
- if( surface == null )
- continue;
-
- XmlNode initFrom = XmlUtils.FindChildNode( surface, "init_from" );
- if( initFrom == null )
- continue;
-
- if( generatedImages.TryGetValue( initFrom.InnerText, out textureFileName ) )
- return textureFileName;
- }
- }
- }
- }
-
- //Max\Maya specified
- if( generatedImages.TryGetValue( imageId, out textureFileName ) )
- return textureFileName;
-
- return "";
- }
-
- bool ParseEffectNode( XmlNode profileNode, XmlNode effectNode, ref MySceneMaterial material )
- {
- foreach( XmlNode materialAttributeNode in effectNode.ChildNodes )
- {
- if( materialAttributeNode.Name == "diffuse" )
- {
- ColorValue diffuseColor = new ColorValue( 1, 1, 1 );
- string textureId1 = "";
- string textureId2 = "";
- string textureId3 = "";
- string textureId4 = "";
- string textureCoord1 = "";
- string textureCoord2 = "";
- string textureCoord3 = "";
- string textureCoord4 = "";
- string blendingMode2 = "";
- string blendingMode3 = "";
- string blendingMode4 = "";
-
- if( !ParseEffectAttribute( materialAttributeNode, ref diffuseColor, ref textureId1,
- ref textureCoord1, ref textureId2, ref textureCoord2, ref blendingMode2,
- ref textureId3, ref textureCoord3, ref blendingMode3, ref textureId4,
- ref textureCoord4, ref blendingMode4 ) )
- return false;
-
- material.DiffuseColor = diffuseColor;
-
- if( !string.IsNullOrEmpty( textureId1 ) )
- {
- material.Diffuse1Map = GetTextureFileName( profileNode, textureId1 );
- material.Diffuse1TexCoord = textureCoord1;
- }
- if( !string.IsNullOrEmpty( textureId2 ) )
- {
- material.Diffuse2Map = GetTextureFileName( profileNode, textureId2 );
- material.Diffuse2TexCoord = textureCoord2;
- material.Diffuse2MapBlending = blendingMode2;
- }
- if( !string.IsNullOrEmpty( textureId3 ) )
- {
- material.Diffuse3Map = GetTextureFileName( profileNode, textureId3 );
- material.Diffuse3TexCoord = textureCoord3;
- material.Diffuse3MapBlending = blendingMode3;
- }
- if( !string.IsNullOrEmpty( textureId4 ) )
- {
- material.Diffuse4Map = GetTextureFileName( profileNode, textureId4 );
- material.Diffuse4TexCoord = textureCoord4;
- material.Diffuse4MapBlending = blendingMode4;
- }
- }
-
- if( materialAttributeNode.Name == "specular" )
- {
- ColorValue specularColor = new ColorValue( 0, 0, 0 );
- string textureId1 = "";
- string textureId2 = "";
- string textureId3 = "";
- string textureId4 = "";
- string textureCoord1 = "";
- string textureCoord2 = "";
- string textureCoord3 = "";
- string textureCoord4 = "";
- string blendingMode2 = "";
- string blendingMode3 = "";
- string blendingMode4 = "";
-
- if( !ParseEffectAttribute( materialAttributeNode, ref specularColor, ref textureId1,
- ref textureCoord1, ref textureId2, ref textureCoord2, ref blendingMode2,
- ref textureId3, ref textureCoord3, ref blendingMode3, ref textureId4,
- ref textureCoord4, ref blendingMode4 ) )
- return false;
-
- material.SpecularColor = specularColor;
-
- if( !string.IsNullOrEmpty( textureId1 ) )
- {
- material.SpecularMap = GetTextureFileName( profileNode, textureId1 );
- material.SpecularTexCoord = textureCoord1;
- }
- }
-
- if( materialAttributeNode.Name == "shininess" )
- {
- XmlNode floatNode = XmlUtils.FindChildNode( materialAttributeNode, "float" );
-
- if( floatNode == null )
- {
- Error( "\"float\" node is not exists for material attribute \"{0}\".",
- materialAttributeNode.Name );
- return false;
- }
-
- float shininess;
- if( !float.TryParse( floatNode.InnerText, out shininess ) )
- {
- Error( "Invalid \"float\" attribute of \"shininess\" node." );
- return false;
- }
- material.Shininess = shininess;
- }
-
- if( materialAttributeNode.Name == "emission" )
- {
- ColorValue emissionColor = new ColorValue( 0, 0, 0 );
- string textureId1 = "";
- string textureId2 = "";
- string textureId3 = "";
- string textureId4 = "";
- string textureCoord1 = "";
- string textureCoord2 = "";
- string textureCoord3 = "";
- string textureCoord4 = "";
- string blendingMode2 = "";
- string blendingMode3 = "";
- string blendingMode4 = "";
-
- if( !ParseEffectAttribute( materialAttributeNode, ref emissionColor, ref textureId1,
- ref textureCoord1, ref textureId2, ref textureCoord2, ref blendingMode2,
- ref textureId3, ref textureCoord3, ref blendingMode3, ref textureId4,
- ref textureCoord4, ref blendingMode4 ) )
- return false;
-
- material.EmissionColor = emissionColor;
-
- if( !string.IsNullOrEmpty( textureId1 ) )
- {
- material.EmissionMap = GetTextureFileName( profileNode, textureId1 );
- material.EmissionTexCoord = textureCoord1;
- }
- }
-
- if( materialAttributeNode.Name == "transparent" )
- {
- string opaque = XmlUtils.GetAttribute( materialAttributeNode, "opaque" );
- if( opaque == "RGB_ZERO" )
- {
- material.Opaque = true;
- }
-
- XmlNode colorNode = XmlUtils.FindChildNode( materialAttributeNode, "color" );
- if( colorNode == null )
- {
- material.TransparentColor = new ColorValue( 1, 1, 1 );
- }
- else
- {
- material.TransparentColor = ConvertStringToColorValue( colorNode.InnerText );
- }
- }
-
- if( materialAttributeNode.Name == "transparency" )
- {
- XmlNode floatNode = XmlUtils.FindChildNode( materialAttributeNode, "float" );
-
- if( floatNode == null )
- {
- Error( "\"float\" node is not exists for material attribute \"{0}\".",
- materialAttributeNode.Name );
- return false;
- }
-
- float transparency;
- if( !float.TryParse( floatNode.InnerText, out transparency ) )
- {
- Error( "Invalid \"float\" attribute of \"transparency\" node." );
- return false;
- }
- material.Transparency = transparency;
- }
- }
-
- return true;
- }
-
- bool ParseEffectExtraNode( XmlNode extraNode, string effectId, ref MySceneMaterial material )
- {
- foreach( XmlNode extraAttributeNode in extraNode.ChildNodes )
- {
- if( extraAttributeNode.Name == "technique" )
- {
- …
Large files files are truncated, but you can click here to view the full file