private static void outputX3D(X3D root, Shape shape, ElevationGrid elevation) { X3DSceneGraphXMLSerializer serializer; string xml; shape.Children.Add(elevation); // Build XML from all the SceneGraphNodes serializer = new X3DSceneGraphXMLSerializer(root); xml = serializer.Serialize(); Console.WriteLine("\nDone."); //Console.WriteLine("\n~~~ X3D Generated Below ~~~\n"); //Console.WriteLine(xml); // Save output XML to x3d file string fileX3d, filePng; fileX3d = newIndexedFile(X3dGenOutputFileLocation); filePng = newIndexedFile(PngGenOutputFileLocation); File.WriteAllText(fileX3d, xml); largePerlinImage.Save(filePng); Console.WriteLine("See Auto Generated X3D file in {0}", fileX3d); Console.WriteLine("See Auto Generated perlin noise texture in {0}", filePng); Console.ReadLine(); }
public void Write_MetaDataObjects() { const string file = @"..\..\..\Sample\Core\metadata.x3d"; var x3d = new X3D(); var scene = x3d.Scene; var meta1 = new MetadataDouble(); var meta2 = new MetadataFloat(); var meta3 = new MetadataInteger(); var meta4 = new MetadataSet(); var meta5 = new MetadataString(); meta1.Name = "double"; meta1.Value = "1 1 1 1 1"; meta1.Reference = "reference"; scene.ChildNodes.Add(meta1); scene.ChildNodes.Add(meta2); scene.ChildNodes.Add(meta3); scene.ChildNodes.Add(meta4); scene.ChildNodes.Add(meta5); var submeta1 = new MetadataDouble(); var submeta2 = new MetadataFloat(); var submeta3 = new MetadataInteger(); var submeta4 = new MetadataString(); meta4.Value.Add(submeta1); meta4.Value.Add(submeta2); meta4.Value.Add(submeta3); meta4.Value.Add(submeta4); Write(file, x3d); }
public void Write_Meta() { const string file = @"..\..\..\Sample\Core\meta.x3d"; var x3d = new X3D(); var head = new Head(); x3d.Head = head; var meta1 = new Meta(); var meta2 = new Meta(); var meta3 = new Meta(); var meta4 = new Meta(); meta1.Name = "library"; meta2.Name = "author"; meta3.Name = "name"; meta1.Content = "X3D.NET"; meta2.Content = "Alexander Dong Back Kim"; meta3.Content = "content"; meta3.Direction = MetaDirectionValues.RightToLeft; meta3.HttpEquivalant = "http-equiv"; meta3.Language = "lang"; meta3.Scheme = "scheme"; x3d.Head.Meta.Add(meta1); x3d.Head.Meta.Add(meta2); x3d.Head.Meta.Add(meta3); x3d.Head.Meta.Add(meta4); Write(file, x3d); }
public void Write_Simple() { const string file = @"..\..\..\Sample\Core\simple.x3d"; var x3d = new X3D(); Write(file, x3d); }
public void Write_EmptyHead() { const string file = @"..\..\..\Sample\Core\empty_head.x3d"; var x3d = new X3D(); x3d.Head = new Head(); Write(file, x3d); }
private static void Write(string file, X3D x3d) { var settings = new XmlWriterSettings(); settings.Indent = true; using (var writer = XmlWriter.Create(file, settings)) { writer.WriteStartDocument(); x3d.Write(writer); writer.WriteEndDocument(); } }
public void Write_WorldInfo() { const string file = @"..\..\..\Sample\Core\WorldInfo.x3d"; var x3d = new X3D(); var wi1 = new WorldInfo(); var wi2 = new WorldInfo(); wi2.Title = "Hello"; var wi3 = new WorldInfo(); wi3.Metadata = new MetadataDouble(); wi3.ContainerField = "Test"; var wi4 = new WorldInfo(); wi4.Info = "\"A\" \"B\" \"C\""; var wi5 = new WorldInfo(); wi5.DEF = "HelloWorld"; x3d.Scene.ChildNodes.Add(wi1); x3d.Scene.ChildNodes.Add(wi2); x3d.Scene.ChildNodes.Add(wi3); x3d.Scene.ChildNodes.Add(wi4); x3d.Scene.ChildNodes.Add(wi5); var wi6 = new WorldInfo(); wi6.IS = new IS(); wi6.IS.Connections.Add(new Connect()); wi6.IS.Connections.Add(new Connect()); wi6.IS.Connections[0].NodeField = "a"; wi6.IS.Connections[0].ProtoField = "b"; wi6.IS.Connections[1].NodeField = "c"; wi6.IS.Connections[1].ProtoField = "d"; x3d.Scene.ChildNodes.Add(wi6); Write(file, x3d); }
public void Write_Components() { const string file = @"..\..\..\Sample\Core\components.x3d"; var x3d = new X3D(); var head = new Head(); x3d.Head = head; head.Components.Add(new Component(ComponentNames.CADGeometry, 1)); head.Components.Add(new Component(ComponentNames.Core, 1)); head.Components.Add(new Component(ComponentNames.CubeMapTexturing, 1)); head.Components.Add(new Component(ComponentNames.DIS, 1)); head.Components.Add(new Component(ComponentNames.EnvironmentalEffects, 1)); head.Components.Add(new Component(ComponentNames.EnvironmentalSensor, 1)); head.Components.Add(new Component(ComponentNames.EventUtilities, 1)); head.Components.Add(new Component(ComponentNames.Followers, 1)); head.Components.Add(new Component(ComponentNames.Geometry2D, 1)); head.Components.Add(new Component(ComponentNames.Geometry3D, 1)); head.Components.Add(new Component(ComponentNames.Geospatial, 1)); head.Components.Add(new Component(ComponentNames.Grouping, 1)); head.Components.Add(new Component(ComponentNames.H_Anim, 1)); head.Components.Add(new Component(ComponentNames.Interpolation, 1)); head.Components.Add(new Component(ComponentNames.KeyDeviceSensor, 1)); head.Components.Add(new Component(ComponentNames.Layering, 1)); head.Components.Add(new Component(ComponentNames.Layout, 1)); head.Components.Add(new Component(ComponentNames.Lighting, 1)); head.Components.Add(new Component(ComponentNames.NURBS, 1)); head.Components.Add(new Component(ComponentNames.Navigation, 1)); head.Components.Add(new Component(ComponentNames.Networking, 1)); head.Components.Add(new Component(ComponentNames.ParticleSystems, 1)); head.Components.Add(new Component(ComponentNames.PickingSensor, 1)); head.Components.Add(new Component(ComponentNames.PointingDeviceSensor, 1)); head.Components.Add(new Component(ComponentNames.Rendering, 1)); head.Components.Add(new Component(ComponentNames.RigidBodyPhysics, 1)); head.Components.Add(new Component(ComponentNames.Scripting, 1)); head.Components.Add(new Component(ComponentNames.Shaders, 1)); head.Components.Add(new Component(ComponentNames.Shape, 1)); head.Components.Add(new Component(ComponentNames.Sound, 1)); head.Components.Add(new Component(ComponentNames.Text, 1)); head.Components.Add(new Component(ComponentNames.Texturing, 1)); head.Components.Add(new Component(ComponentNames.Texturing3D, 1)); head.Components.Add(new Component(ComponentNames.Time, 1)); Write(file, x3d); }
internal void BuildShapeDom(out X3D root, out Shape shape) { head head = new head(); meta meta = new meta(); Scene scene = new Scene(); root = new X3D(); shape = new Shape(); meta.name = "generator"; meta.content = BuilderApplication.AppInfo; scene.Children.Add(shape); head.Children.Add(meta); root.Children.Add(scene); root.Children.Add(head); }
public static void GenerateModelFrom3DS(string path) { var x3d = new X3D(); var xmlSerializer = new XmlSerializer(x3d.GetType()); var reader = new StreamReader(path); x3d = (X3D)xmlSerializer.Deserialize(reader); reader.Close(); var scene = new X3DScene(); scene = x3d.Scene[0]; var transf0 = new Transform(); transf0 = scene.Transform[0]; var transf1 = new Transform(); transf1 = transf0.Transform1[0]; var transfGroup = new TransformGroup(); transfGroup = transf1.Group[0]; var transfGroupShape = new TransformGroupShape(); transfGroupShape = transfGroup.Shape[0]; var shapeIndexedFaceSet = new TransformGroupShapeIndexedFaceSet(); // indices shapeIndexedFaceSet = transfGroupShape.IndexedFaceSet[0]; // position coords TransformGroupShapeIndexedFaceSetCoordinate positionCoords = new TransformGroupShapeIndexedFaceSetCoordinate(); positionCoords = shapeIndexedFaceSet.Coordinate[0]; // texture coords TransformGroupShapeIndexedFaceSetTextureCoordinate texCoords = new TransformGroupShapeIndexedFaceSetTextureCoordinate(); texCoords = shapeIndexedFaceSet.TextureCoordinate[0]; /* * string output = shapeIndexedFaceSet.coordIndex + "\n"; * output += shapeIndexedFaceSet.texCoordIndex + "\n"; * output += positionCoords.point + "\n"; * output += texCoords.point + "\n"; */ var positionsList = new List <Vector3>(); string[] positionCoordsStr = positionCoords.point.Split(' '); for (int i = 0; i < positionCoordsStr.Length - 1;) { var vector3 = new Vector3( Convert.ToSingle(positionCoordsStr[i++], CultureInfo.InvariantCulture), Convert.ToSingle(positionCoordsStr[i++], CultureInfo.InvariantCulture), Convert.ToSingle(positionCoordsStr[i++], CultureInfo.InvariantCulture)); positionsList.Add(vector3); } var texCoordsList = new List <Vector2>(); string[] texCoordsStr = texCoords.point.Split(' '); // OpenTK flips V texCoord. Used (U, 1-V) to convert from X3D to OpenTK for (int i = 0; i < texCoordsStr.Length - 1;) { var vector2 = new Vector2( Convert.ToSingle(texCoordsStr[i++], CultureInfo.InvariantCulture), 1 - Convert.ToSingle(texCoordsStr[i++], CultureInfo.InvariantCulture)); texCoordsList.Add(vector2); } // generate model xml var vertexsIndicesData = new VertexsIndicesData(); vertexsIndicesData.VertexFormat = VertexFormat.XYZ_NORMAL_UV_COLOR; var vertexs = new List <Vertex>(); int idx = 0; var texCoordIndexs = new List <uint>(); var coordIndexs = new List <uint>(); string[] coordsIndexStr = shapeIndexedFaceSet.coordIndex.Split(' '); string[] texCoordsIndexStr = shapeIndexedFaceSet.texCoordIndex.Split(' '); int vertCount = 0; // check if is 4 or 3 when -1 arrives int v = 0; for (int ic = 0; ic < coordsIndexStr.Length; ic++) { if (coordsIndexStr[ic] == "-1") { switch (vertCount) { case 4: #region quad var vertex0 = new Vertex(); vertex0.Color = Color4.Chocolate; vertex0.Normal = Vector3.Zero; vertex0.Position = new Vector3(positionsList[(int)coordIndexs[idx]]); vertex0.TexCoord = new Vector2(texCoordsList[(int)texCoordIndexs[idx]]); vertexs.Add(vertex0); v++; var vertex1 = new Vertex(); vertex1.Color = Color4.Chocolate; vertex1.Normal = Vector3.Zero; vertex1.Position = new Vector3(positionsList[(int)coordIndexs[idx + 1]]); vertex1.TexCoord = new Vector2(texCoordsList[(int)texCoordIndexs[idx + 1]]); vertexs.Add(vertex1); v++; var vertex2 = new Vertex(); vertex2.Color = Color4.Chocolate; vertex2.Normal = Vector3.Zero; vertex2.Position = new Vector3(positionsList[(int)coordIndexs[idx + 2]]); vertex2.TexCoord = new Vector2(texCoordsList[(int)texCoordIndexs[idx + 2]]); vertexs.Add(vertex2); v++; // normal Vector3 edge1 = Vector3.Subtract(vertex1.Position, vertex0.Position); Vector3 edge2 = Vector3.Subtract(vertex2.Position, vertex0.Position); vertex0.Normal = Vector3.Cross(edge1, edge2); vertex1.Normal = vertex0.Normal; vertex2.Normal = vertex0.Normal; Vertex vertex3 = new Vertex(); vertex3.Color = Color4.Chocolate; vertex3.Normal = Vector3.Zero; vertex3.Position = new Vector3(positionsList[(int)coordIndexs[idx + 2]]); vertex3.TexCoord = new Vector2(texCoordsList[(int)texCoordIndexs[idx + 2]]); vertexs.Add(vertex3); v++; Vertex vertex4 = new Vertex(); vertex4.Color = Color4.Chocolate; vertex4.Normal = Vector3.Zero; vertex4.Position = new Vector3(positionsList[(int)coordIndexs[idx + 3]]); vertex4.TexCoord = new Vector2(texCoordsList[(int)texCoordIndexs[idx + 3]]); vertexs.Add(vertex4); v++; Vertex vertex5 = new Vertex(); vertex5.Color = Color4.Chocolate; vertex5.Normal = Vector3.Zero; vertex5.Position = new Vector3(positionsList[(int)coordIndexs[idx]]); vertex5.TexCoord = new Vector2(texCoordsList[(int)texCoordIndexs[idx]]); vertexs.Add(vertex5); v++; idx += 4; // normal Vector3 edge3 = Vector3.Subtract(vertex4.Position, vertex3.Position); Vector3 edge4 = Vector3.Subtract(vertex5.Position, vertex3.Position); vertex3.Normal = Vector3.Cross(edge3, edge4); vertex4.Normal = vertex3.Normal; vertex5.Normal = vertex3.Normal; #endregion break; case 3: #region triangle Vertex vertex6 = new Vertex(); vertex6.Color = Color4.Chocolate; vertex6.Normal = Vector3.Zero; vertex6.Position = new Vector3(positionsList[(int)coordIndexs[idx]]); vertex6.TexCoord = new Vector2(texCoordsList[(int)texCoordIndexs[idx]]); vertexs.Add(vertex6); v++; Vertex vertex7 = new Vertex(); vertex7.Color = Color4.Chocolate; vertex7.Normal = Vector3.Zero; vertex7.Position = new Vector3(positionsList[(int)coordIndexs[idx + 1]]); vertex7.TexCoord = new Vector2(texCoordsList[(int)texCoordIndexs[idx + 1]]); vertexs.Add(vertex7); v++; Vertex vertex8 = new Vertex(); vertex8.Color = Color4.Chocolate; vertex8.Normal = Vector3.Zero; vertex8.Position = new Vector3(positionsList[(int)coordIndexs[idx + 2]]); vertex8.TexCoord = new Vector2(texCoordsList[(int)texCoordIndexs[idx + 2]]); vertexs.Add(vertex8); v++; idx += 3; // normal Vector3 edge5 = Vector3.Subtract(vertex7.Position, vertex6.Position); Vector3 edge6 = Vector3.Subtract(vertex8.Position, vertex6.Position); vertex6.Normal = Vector3.Cross(edge5, edge6); vertex7.Normal = vertex6.Normal; vertex8.Normal = vertex6.Normal; #endregion break; default: break; } vertCount = 0; continue; } if (coordsIndexStr[ic] != String.Empty) { coordIndexs.Add(Convert.ToUInt16(coordsIndexStr[ic])); } if (texCoordsIndexStr[ic] != String.Empty) { texCoordIndexs.Add(Convert.ToUInt16(texCoordsIndexStr[ic])); } vertCount++; } // bug! there are triangles also vertexsIndicesData.VertexCount = (coordIndexs.Count / 4) * 6; vertexsIndicesData.IndicesCount = (coordIndexs.Count / 4) * 6; var indices = new List <uint>(); for (int i = 0; i < vertexsIndicesData.IndicesCount; i++) { indices.Add((uint)i); } vertexsIndicesData.Vertexs = vertexs; vertexsIndicesData.Indices = indices; var xmlSerializerModel = new XmlSerializer(typeof(VertexsIndicesData)); var textWriter = new StringWriter(); xmlSerializerModel.Serialize(textWriter, vertexsIndicesData); File.WriteAllText(path + "_out", textWriter.ToString()); //File.WriteAllText(@"Models\test.txt", output); // smooth shadow var solvedVertexesPositions = new List <Vector3>(); for (int i = 0; i < vertexsIndicesData.Vertexs.Count; i++) { if (solvedVertexesPositions.Contains( vertexsIndicesData.Vertexs[i].Position)) { continue; } solvedVertexesPositions. Add(vertexsIndicesData.Vertexs[i].Position); var normal = new Vector3(); normal = vertexsIndicesData.Vertexs[i].Normal; var vertexes2Solve = new Stack <Vertex>(); vertexes2Solve.Push(vertexsIndicesData.Vertexs[i]); for (int j = 0; j < vertexsIndicesData.Vertexs.Count; j++) { if (vertexsIndicesData.Vertexs[i].Position == vertexsIndicesData.Vertexs[j].Position && i != j) { normal += vertexsIndicesData.Vertexs[j].Normal; vertexes2Solve.Push(vertexsIndicesData.Vertexs[j]); } } normal.Normalize(); while (vertexes2Solve.Count > 0) { vertexes2Solve.Pop().Normal = normal; } } var xmlSerializerModelSmooth = new XmlSerializer(typeof(VertexsIndicesData)); var textWriter1 = new StringWriter(); xmlSerializerModelSmooth.Serialize(textWriter1, vertexsIndicesData); File.WriteAllText(path + "_out_smooth", textWriter1.ToString()); }