private static Color ParseColor(AseParser parser) { var r = (int)(255.0f * parser.ConsumeFloat()); var g = (int)(255.0f * parser.ConsumeFloat()); var b = (int)(255.0f * parser.ConsumeFloat()); var fromArgb = Color.FromArgb(255, r, g, b); return fromArgb; }
private void ParseScene(AseParser parser, Scene scene) { parser.Consume("{"); for (;;) { var attr = parser.Consume(); if (attr == null || attr == "}") { break; } if (0 == string.Compare(attr, "*SCENE_FILENAME", StringComparison.InvariantCultureIgnoreCase)) { scene.Id = parser.Consume(); continue; } if (0 == string.Compare(attr, "*SCENE_FIRSTFRAME", StringComparison.InvariantCultureIgnoreCase)) { parser.ConsumeInt(); continue; } if (0 == string.Compare(attr, "*SCENE_LASTFRAME", StringComparison.InvariantCultureIgnoreCase)) { parser.ConsumeInt(); continue; } if (0 == string.Compare(attr, "*SCENE_FRAMESPEED", StringComparison.InvariantCultureIgnoreCase)) { parser.ConsumeFloat(); continue; } if (0 == string.Compare(attr, "*SCENE_TICKSPERFRAME", StringComparison.InvariantCultureIgnoreCase)) { parser.ConsumeFloat(); continue; } if (0 == string.Compare(attr, "*SCENE_BACKGROUND_STATIC", StringComparison.InvariantCultureIgnoreCase)) { parser.ConsumeFloat(); parser.ConsumeFloat(); parser.ConsumeFloat(); continue; } if (0 == string.Compare(attr, "*SCENE_AMBIENT_STATIC", StringComparison.InvariantCultureIgnoreCase)) { parser.ConsumeFloat(); parser.ConsumeFloat(); parser.ConsumeFloat(); continue; } parser.UnknownLexemError(); } }
private IMaterial ParseMaterial(AseParser parser, Scene scene) { var sceneEffect = new SceneEffect() {CullMode = CullMode.Front}; var m = new SceneMaterial { Effect = sceneEffect }; parser.Consume("{"); for (;;) { var attr = parser.Consume(); if (attr == null || attr == "}") { break; } if (0 == string.Compare(attr, "*MATERIAL_NAME", StringComparison.InvariantCultureIgnoreCase)) { m.Id = m.Name = sceneEffect.Id = sceneEffect.Name = parser.Consume(); continue; } if (0 == string.Compare(attr, "*MATERIAL_CLASS", StringComparison.InvariantCultureIgnoreCase)) { parser.Consume(); continue; } if (0 == string.Compare(attr, "*MATERIAL_AMBIENT", StringComparison.InvariantCultureIgnoreCase)) { sceneEffect.Ambient = new SolidColorSource { Color = ParseColor(parser) }; continue; } if (0 == string.Compare(attr, "*MATERIAL_DIFFUSE", StringComparison.InvariantCultureIgnoreCase)) { sceneEffect.Diffuse = new SolidColorSource { Color = ParseColor(parser) }; continue; } if (0 == string.Compare(attr, "*MATERIAL_SPECULAR", StringComparison.InvariantCultureIgnoreCase)) { sceneEffect.Specular = new SolidColorSource { Color = ParseColor(parser) }; continue; } if (0 == string.Compare(attr, "*MATERIAL_SHINE", StringComparison.InvariantCultureIgnoreCase)) { sceneEffect.Shininess = parser.ConsumeFloat(); continue; } if (0 == string.Compare(attr, "*MATERIAL_SHINESTRENGTH", StringComparison.InvariantCultureIgnoreCase)) { parser.ConsumeFloat(); continue; } if (0 == string.Compare(attr, "*MATERIAL_TRANSPARENCY", StringComparison.InvariantCultureIgnoreCase)) { sceneEffect.Transparency = parser.ConsumeFloat(); continue; } if (0 == string.Compare(attr, "*MATERIAL_WIRESIZE", StringComparison.InvariantCultureIgnoreCase)) { parser.ConsumeFloat(); continue; } if (0 == string.Compare(attr, "*MATERIAL_XP_FALLOFF", StringComparison.InvariantCultureIgnoreCase)) { parser.ConsumeFloat(); continue; } if (0 == string.Compare(attr, "*MATERIAL_SELFILLUM", StringComparison.InvariantCultureIgnoreCase)) { parser.ConsumeFloat(); continue; } if (0 == string.Compare(attr, "*MATERIAL_SHADING", StringComparison.InvariantCultureIgnoreCase)) { parser.Consume(); continue; } if (0 == string.Compare(attr, "*MATERIAL_FALLOFF", StringComparison.InvariantCultureIgnoreCase)) { parser.Consume(); continue; } if (0 == string.Compare(attr, "*MATERIAL_XP_TYPE", StringComparison.InvariantCultureIgnoreCase)) { parser.Consume(); continue; } if (0 == string.Compare(attr, "*MAP_AMBIENT", StringComparison.InvariantCultureIgnoreCase)) { sceneEffect.Ambient = this.ParseMap(parser, scene); continue; } if (0 == string.Compare(attr, "*MAP_DIFFUSE", StringComparison.InvariantCultureIgnoreCase)) { sceneEffect.Diffuse = this.ParseMap(parser, scene); continue; } if (0 == string.Compare(attr, "*NUMSUBMTLS", StringComparison.InvariantCultureIgnoreCase)) { var numMaterials = parser.ConsumeInt(); continue; } if (0 == string.Compare(attr, "*SUBMATERIAL", StringComparison.InvariantCultureIgnoreCase)) { var index = parser.ConsumeInt(); while (m.Submaterials.Count <= index) m.Submaterials.Add(null); var subm = ParseMaterial(parser, scene); m.Submaterials[index] = subm; continue; } parser.UnknownLexemError(); } return m; }
private IColorSource ParseMap(AseParser parser, Scene scene) { var image = new FileReferenceImage(); var texture = new ImageColorSource { Image = image }; parser.Consume("{"); for (;;) { var attr = parser.Consume(); if (attr == null || attr == "}") { break; } if (0 == string.Compare(attr, "*MAP_NAME", StringComparison.InvariantCultureIgnoreCase)) { image.Id = image.Name = parser.Consume(); continue; } if (0 == string.Compare(attr, "*MAP_CLASS", StringComparison.InvariantCultureIgnoreCase)) { parser.Consume(); continue; } if (0 == string.Compare(attr, "*MAP_SUBNO", StringComparison.InvariantCultureIgnoreCase)) { parser.Consume(); continue; } if (0 == string.Compare(attr, "*MAP_AMOUNT", StringComparison.InvariantCultureIgnoreCase)) { parser.ConsumeFloat(); continue; } if (0 == string.Compare(attr, "*BITMAP", StringComparison.InvariantCultureIgnoreCase)) { image.Path = Path.Combine(this.basePath, parser.Consume()); continue; } if (0 == string.Compare(attr, "*MAP_TYPE", StringComparison.InvariantCultureIgnoreCase)) { parser.Consume(); continue; } if (0 == string.Compare(attr, "*UVW_U_OFFSET", StringComparison.InvariantCultureIgnoreCase)) { parser.ConsumeFloat(); continue; } if (0 == string.Compare(attr, "*UVW_V_OFFSET", StringComparison.InvariantCultureIgnoreCase)) { parser.ConsumeFloat(); continue; } if (0 == string.Compare(attr, "*UVW_U_TILING", StringComparison.InvariantCultureIgnoreCase)) { parser.ConsumeFloat(); continue; } if (0 == string.Compare(attr, "*UVW_V_TILING", StringComparison.InvariantCultureIgnoreCase)) { parser.ConsumeFloat(); continue; } if (0 == string.Compare(attr, "*UVW_ANGLE", StringComparison.InvariantCultureIgnoreCase)) { parser.ConsumeFloat(); continue; } if (0 == string.Compare(attr, "*UVW_BLUR", StringComparison.InvariantCultureIgnoreCase)) { parser.ConsumeFloat(); continue; } if (0 == string.Compare(attr, "*UVW_BLUR_OFFSET", StringComparison.InvariantCultureIgnoreCase)) { parser.ConsumeFloat(); continue; } if (0 == string.Compare(attr, "*UVW_NOUSE_AMT", StringComparison.InvariantCultureIgnoreCase)) { parser.ConsumeFloat(); continue; } if (0 == string.Compare(attr, "*UVW_NOISE_SIZE", StringComparison.InvariantCultureIgnoreCase)) { parser.ConsumeFloat(); continue; } if (0 == string.Compare(attr, "*UVW_NOISE_LEVEL", StringComparison.InvariantCultureIgnoreCase)) { parser.ConsumeFloat(); continue; } if (0 == string.Compare(attr, "*UVW_NOISE_PHASE", StringComparison.InvariantCultureIgnoreCase)) { parser.ConsumeFloat(); continue; } if (0 == string.Compare(attr, "*BITMAP_FILTER", StringComparison.InvariantCultureIgnoreCase)) { parser.Consume(); continue; } parser.UnknownLexemError(); } return texture; }
private void ParseColList(AseParser parser, IList<Color> colors) { parser.Consume("{"); for (;;) { var attr = parser.Consume(); if (attr == null || attr == "}") { break; } if (0 == string.Compare(attr, "*MESH_VERTCOL", StringComparison.InvariantCultureIgnoreCase)) { var index = parser.ConsumeInt(); var a = parser.ConsumeFloat(); var b = parser.ConsumeFloat(); var c = parser.ConsumeFloat(); colors[index] = Color.FromArgb(255, this.ClampCol(a), this.ClampCol(b), this.ClampCol(c)); continue; } parser.UnknownLexemError(); } }
private void ParsSubMesh(AseParser parser, Scene scene, Node node) { var mesh = new SeparateStreamsMesh(); var submesh = mesh.CreateSubmesh(); node.Mesh = mesh; scene.Geometries.Add(mesh); ArrayMeshStream<Float3> vertices = null; ListMeshStream<Float3> normalStream = null; FaceNormal[] normals = null; ArrayMeshStream<Float3> tvertices = null; ArrayMeshStream<Color> cols = null; AseFace[] faces = null; AseTFace[] tfaces = null; Tuple<int, int, int>[] colFaces = null; //TODO: submesh can have it's own vertex streams parser.Consume("{"); for (;;) { var attr = parser.Consume(); if (attr == null || attr == "}") { break; } if (0 == string.Compare(attr, "*TIMEVALUE", StringComparison.InvariantCultureIgnoreCase)) { parser.ConsumeFloat(); continue; } if (0 == string.Compare(attr, "*MESH_NUMVERTEX", StringComparison.InvariantCultureIgnoreCase)) { vertices = new ArrayMeshStream<Float3>(parser.ConsumeInt(), converterFactory); mesh.SetStream(Streams.Position, 0, vertices); continue; } if (0 == string.Compare(attr, "*MESH_VERTEX_LIST", StringComparison.InvariantCultureIgnoreCase)) { this.ParseVertexList(parser, vertices); continue; } if (0 == string.Compare(attr, "*MESH_NUMTVERTEX", StringComparison.InvariantCultureIgnoreCase)) { tvertices = new ArrayMeshStream<Float3>(parser.ConsumeInt(), converterFactory); mesh.SetStream(Streams.TexCoord, 0, tvertices); continue; } if (0 == string.Compare(attr, "*MESH_TVERTLIST", StringComparison.InvariantCultureIgnoreCase)) { this.ParseTVertList(parser, tvertices); continue; } if (0 == string.Compare(attr, "*MESH_NUMTVFACES", StringComparison.InvariantCultureIgnoreCase)) { tfaces = new AseTFace[parser.ConsumeInt()]; continue; } if (0 == string.Compare(attr, "*MESH_TFACELIST", StringComparison.InvariantCultureIgnoreCase)) { this.ParseTFaceList(parser, tfaces); continue; } if (0 == string.Compare(attr, "*MESH_NUMCVERTEX", StringComparison.InvariantCultureIgnoreCase)) { cols = new ArrayMeshStream<Color>(parser.ConsumeInt(), converterFactory); mesh.SetStream(Streams.Color, 0, cols); continue; } if (0 == string.Compare(attr, "*MESH_CVERTLIST", StringComparison.InvariantCultureIgnoreCase)) { this.ParseColList(parser, cols); continue; } if (0 == string.Compare(attr, "*MESH_NUMCVFACES", StringComparison.InvariantCultureIgnoreCase)) { colFaces = new Tuple<int, int, int>[parser.ConsumeInt()]; continue; } if (0 == string.Compare(attr, "*MESH_CFACELIST", StringComparison.InvariantCultureIgnoreCase)) { this.ParseColFaceList(parser, colFaces); continue; } if (0 == string.Compare(attr, "*MESH_NORMALS", StringComparison.InvariantCultureIgnoreCase)) { normals = new FaceNormal[faces.Length]; this.ParsNormalList(parser, normals); continue; } if (0 == string.Compare(attr, "*MESH_NUMFACES", StringComparison.InvariantCultureIgnoreCase)) { faces = new AseFace[parser.ConsumeInt()]; continue; } if (0 == string.Compare(attr, "*MESH_FACE_LIST", StringComparison.InvariantCultureIgnoreCase)) { this.ParseFaceList(parser, faces); continue; } parser.UnknownLexemError(); } ListMeshStream<int> positionIndices = null; if (vertices != null) { positionIndices = new ListMeshStream<int>(faces.Length * 3,converterFactory); submesh.SetIndexStream(Streams.Position, 0, positionIndices); } ListMeshStream<int> normalIndices = null; if (normals != null) { normalStream = new ListMeshStream<Float3>(faces.Length * 3, converterFactory); mesh.SetStream(Streams.Normal, 0, normalStream); normalIndices = new ListMeshStream<int>(faces.Length * 3, converterFactory); submesh.SetIndexStream(Streams.Normal, 0, normalIndices); } ListMeshStream<int> colorIndices = null; if (cols != null) { colorIndices = new ListMeshStream<int>(faces.Length * 3, converterFactory); submesh.SetIndexStream(Streams.Color, 0, colorIndices); } ListMeshStream<int> texCoordIndices = null; if (tvertices != null) { texCoordIndices = new ListMeshStream<int>(faces.Length * 3, converterFactory); submesh.SetIndexStream(Streams.TexCoord, 0, texCoordIndices); } for (int i = 0; i < faces.Length; ++i) { // -------------------------------------------------- // if (positionIndices != null) { positionIndices.Add(faces[i].A); } if (normalIndices != null) { normalIndices.Add(normalStream.Count); normalStream.Add(normals[i].A.Normal); } if (colorIndices != null) { colorIndices.Add(colFaces[i].Item1); } if (texCoordIndices != null) { texCoordIndices.Add(tfaces[i].A); } // -------------------------------------------------- // if (positionIndices != null) { positionIndices.Add(faces[i].C); } if (normalIndices != null) { normalIndices.Add(normalStream.Count); normalStream.Add(normals[i].C.Normal); } if (colorIndices != null) { colorIndices.Add(colFaces[i].Item3); } if (texCoordIndices != null) { texCoordIndices.Add(tfaces[i].C); } // -------------------------------------------------- // if (positionIndices != null) { positionIndices.Add(faces[i].B); } if (normalIndices != null) { normalIndices.Add(normalStream.Count); normalStream.Add(normals[i].B.Normal); } if (colorIndices != null) { colorIndices.Add(colFaces[i].Item2); } if (texCoordIndices != null) { texCoordIndices.Add(tfaces[i].B); } // -------------------------------------------------- // } }
private void ParsSubMesh(AseParser parser, Scene scene, Node node,string attr) { if (0 == string.Compare(attr, "*MESH", StringComparison.InvariantCultureIgnoreCase)) { ParsSubMesh(parser, scene, node); return; } if (0 == string.Compare(attr, "*MESH_ANIMATION", StringComparison.InvariantCultureIgnoreCase)) { this.ParsMeshAnimation(parser, scene, node); return; } if (0 == string.Compare(attr, "*PROP_MOTIONBLUR", StringComparison.InvariantCultureIgnoreCase)) { parser.ConsumeFloat(); return; } if (0 == string.Compare(attr, "*PROP_CASTSHADOW", StringComparison.InvariantCultureIgnoreCase)) { parser.ConsumeInt(); return; } if (0 == string.Compare(attr, "*PROP_RECVSHADOW", StringComparison.InvariantCultureIgnoreCase)) { parser.ConsumeInt(); return; } if (0 == string.Compare(attr, "*MATERIAL_REF", StringComparison.InvariantCultureIgnoreCase)) { var materialId = parser.ConsumeInt(); if (scene.Materials != null && scene.Materials.Count > 0) { foreach (var submesh in node.Mesh.Submeshes) { submesh.Material = scene.Materials[materialId]; } } return; } if (0 == string.Compare(attr, "*TM_ANIMATION", StringComparison.InvariantCultureIgnoreCase)) { this.SkipBlock(parser); return; } if (0 == string.Compare(attr, "*WIREFRAME_COLOR", StringComparison.InvariantCultureIgnoreCase)) { parser.ConsumeFloat(); parser.ConsumeFloat(); parser.ConsumeFloat(); return; } parser.UnknownLexemError(); }
private void ParsNormalList(AseParser parser, FaceNormal[] normals) { int faceIndex = 0; int faceVertexIndex = 0; parser.Consume("{"); for (;;) { var attr = parser.Consume(); if (attr == null || attr == "}") { break; } if (0 == string.Compare(attr, "*MESH_FACENORMAL", StringComparison.InvariantCultureIgnoreCase)) { faceIndex = parser.ConsumeInt(); var x = parser.ConsumeFloat(); var y = parser.ConsumeFloat(); var z = parser.ConsumeFloat(); normals[faceIndex].A.Normal = normals[faceIndex].B.Normal = normals[faceIndex].C.Normal = normals[faceIndex].Normal = new Float3(x, y, z); faceVertexIndex = 0; continue; } if (0 == string.Compare(attr, "*MESH_VERTEXNORMAL", StringComparison.InvariantCultureIgnoreCase)) { var index = parser.ConsumeInt(); var x = parser.ConsumeFloat(); var y = parser.ConsumeFloat(); var z = parser.ConsumeFloat(); Float3 v = new Float3(x, y, z); switch (faceVertexIndex) { case 0: normals[faceIndex].A.Index = index; normals[faceIndex].A.Normal = v; break; case 1: normals[faceIndex].B.Index = index; normals[faceIndex].B.Normal = v; break; case 2: normals[faceIndex].C.Index = index; normals[faceIndex].C.Normal = v; break; } ++faceVertexIndex; continue; } parser.UnknownLexemError(); } }
private void ParseVertexList(AseParser parser, IList<Float3> vertices) { parser.Consume("{"); for (;;) { var attr = parser.Consume(); if (attr == null || attr == "}") { break; } if (0 == string.Compare(attr, "*MESH_VERTEX", StringComparison.InvariantCultureIgnoreCase)) { var index = parser.ConsumeInt(); var x = parser.ConsumeFloat(); var y = parser.ConsumeFloat(); var z = parser.ConsumeFloat(); vertices[index] = new Float3(x, y, z); continue; } parser.UnknownLexemError(); } }