public PlanetScene() { var sphere = Object3dInfo.LoadFromObjSingle(Media.Get("planet.obj")); var planet = new Mesh3d(sphere, new GenericMaterial(Color.Red)); planet.Scale(30000); (planet.MainMaterial as GenericMaterial).Type = GenericMaterial.MaterialType.PlanetSurface; (planet.MainMaterial as GenericMaterial).TesselationMultiplier = 0.007f; planet.Translate(0, -30500, 0); Add(planet); }
public FortressScene() { Object3dInfo skydomeInfo = Object3dInfo.LoadFromObjSingle(Media.Get("usky.obj")); var skydomeMaterial = GenericMaterial.FromMedia("skyreal.png"); var skydome = new Mesh3d(skydomeInfo, skydomeMaterial); skydome.Scale(55000); skydome.Translate(0, -100, 0); //skydome.IgnoreLighting = true; //skydome.DiffuseComponent = 0.2f; Add(skydome); /* var sun = new Sun(new Vector3(0.1f, -1, 0).ToQuaternion(Vector3.UnitY), new Vector4(1, 0.97f, 0.92f, 120), 300, 100, 70, 40, 10, 1); GLThread.OnUpdate += (o, e) => { var kb = OpenTK.Input.Keyboard.GetState(); if(kb.IsKeyDown(OpenTK.Input.Key.U)) { var quat = Quaternion.FromAxisAngle(sun.Orientation.GetTangent(MathExtensions.TangentDirection.Left), -0.01f); sun.Orientation = Quaternion.Multiply(sun.Orientation, quat); } if(kb.IsKeyDown(OpenTK.Input.Key.J)) { var quat = Quaternion.FromAxisAngle(sun.Orientation.GetTangent(MathExtensions.TangentDirection.Left), 0.01f); sun.Orientation = Quaternion.Multiply(sun.Orientation, quat); } if(kb.IsKeyDown(OpenTK.Input.Key.H)) { var quat = Quaternion.FromAxisAngle(Vector3.UnitY, -0.01f); sun.Orientation = Quaternion.Multiply(sun.Orientation, quat); } if(kb.IsKeyDown(OpenTK.Input.Key.K)) { var quat = Quaternion.FromAxisAngle(Vector3.UnitY, 0.01f); sun.Orientation = Quaternion.Multiply(sun.Orientation, quat); } };*/ List<Mesh3d> nodes1 = new List<Mesh3d>(); List<Mesh3d> leaves1 = new List<Mesh3d>(); Random rand = new Random(); for(int x = 0; x < 3; x++) { for(int z = 0; z < 3; z++) { var tree = TreeGenerator.CreateTree(MathHelper.DegreesToRadians(30), MathHelper.DegreesToRadians(45), 4, 4, 6666, 0.3f, true); var scale = (float)rand.NextDouble() * 2 + 1; var tx = (float)rand.NextDouble() * 2 + 4; var ty = (float)rand.NextDouble() * 2 + 4; var mergedNodes = tree[0].Merge(); mergedNodes.Translate(x * 5 + tx, 0, z * 5 + ty); mergedNodes.Scale(scale); nodes1.Add(mergedNodes); var mergedLeaves = tree[1].Merge(); mergedLeaves.Translate(x * 5 + tx, 0, z * 5 + ty); mergedLeaves.Scale(scale); leaves1.Add(mergedLeaves); //tree[0].ObjectInfo.FreeCPUMemory(); //tree[1].ObjectInfo.FreeCPUMemory(); } } Mesh3d singleNodes = Mesh3d.Merge(nodes1); Mesh3d singleLeaves = Mesh3d.Merge(leaves1); foreach(var o in nodes1) o.MainObjectInfo.FreeCPUMemory(); foreach(var o in leaves1) o.MainObjectInfo.FreeCPUMemory(); InstancedMesh3d nodes = new InstancedMesh3d(singleNodes.MainObjectInfo, singleNodes.MainMaterial); InstancedMesh3d leaves = new InstancedMesh3d(singleLeaves.MainObjectInfo, singleLeaves.MainMaterial); for(int x = 0; x < 1; x++) { for(int z = 0; z < 1; z++) { nodes.Transformations.Add(new TransformationManager(new Vector3(x * 50, 0, z * 50))); leaves.Transformations.Add(new TransformationManager(new Vector3(x * 50, 0, z * 50))); } } nodes.UpdateMatrix(); leaves.UpdateMatrix(); Add(nodes); Add(leaves); /*GLThread.CreateTimer(() => { GLThread.Invoke(() => read()); }, 2000).Start();*/ Object3dInfo waterInfo = Object3dGenerator.CreateTerrain(new Vector2(-200, -200), new Vector2(200, 200), new Vector2(100, 100), Vector3.UnitY, 333, (x, y) => 0); var color = GenericMaterial.FromMedia("checked.png"); //color.SetBumpMapFromMedia("lightref.png"); Mesh3d water = new Mesh3d(waterInfo, color); water.SetMass(0); water.Translate(0, 0, 0); water.SetCollisionShape(new BulletSharp.StaticPlaneShape(Vector3.UnitY, 0)); Add(water); }
public static List<Mesh3d> LoadSceneFromCollada(string infile) { List<Mesh3d> infos = new List<Mesh3d>(); XDocument xml = XDocument.Load(infile); var colladaNode = xml.Elements().First(); var lib = colladaNode.SelectSingle("library_geometries"); var geometries = lib.SelectMany("geometry"); foreach(var geom in geometries) { try { string geoID = geom.Attribute("id").Value; string geoName = geom.Attribute("name").Value; List<float> xyzs = geom.SelectSingle("mesh").SelectMany("source").ElementAt(0).SelectSingle("float_array").Value.Split(new char[] { ' ' }).Select<string, float>((a) => float.Parse(a, System.Globalization.NumberFormatInfo.InvariantInfo)).ToList(); List<float> normals = geom.SelectSingle("mesh").SelectMany("source").ElementAt(1).SelectSingle("float_array").Value.Trim().Split(new char[] { ' ' }).Select<string, float>((a) => float.Parse(a, System.Globalization.NumberFormatInfo.InvariantInfo)).ToList(); List<float> uvs = null; try { uvs = geom.SelectSingle("mesh").SelectMany("source").ElementAt(2).SelectSingle("float_array").Value.Trim().Split(new char[] { ' ' }).Select<string, float>((a) => float.Parse(a, System.Globalization.NumberFormatInfo.InvariantInfo)).ToList(); } catch { uvs = new List<float>(); } List<int> indices = geom.SelectSingle("mesh").SelectSingle("polylist").SelectSingle("p").Value.Trim().Split(new char[] { ' ' }).Select<string, int>((a) => int.Parse(a)).ToList(); List<float> VBO = new List<float>(); List<uint> indicesNew = new List<uint>(); uint vcount = 0; for(int i = 0; i < indices.Count; ) { int vid = indices[i] * 3; int nid = indices[i + 1] * 3; int uid = indices[i + 2] * 2; indicesNew.Add(vcount++); VBO.AddRange(new float[] { -xyzs[vid + 1], xyzs[vid + 2], -xyzs[vid] }); if(uvs.Count > 0) { VBO.AddRange(new float[] { uvs[uid], uvs[uid + 1] }); i += 3; } else { VBO.AddRange(new float[] { 0, 0 }); i += 2; } VBO.AddRange(new float[] { -normals[nid + 1], normals[nid + 2], -normals[nid] }); } var objinfo = new Object3dInfo(VBO, indicesNew); var transformationNode = colladaNode.SelectSingle("library_visual_scenes").SelectSingle("visual_scene").SelectMany("node").First((a) => a.SelectSingle("instance_geometry").Attribute("url").Value == "#" + geoID); var mesh = new Mesh3d(objinfo, new GenericMaterial(Color.White)); List<float> transVector = transformationNode.SelectMany("translate").First((a) => a.Attribute("sid").Value == "location").Value.Trim().Split(new char[] { ' ' }).Select<string, float>((a) => float.Parse(a, System.Globalization.NumberFormatInfo.InvariantInfo)).ToList(); List<List<float>> rots = transformationNode.SelectMany("rotate").Select<XElement, List<float>>((a) => a.Value.Trim().Split(new char[] { ' ' }).Select<string, float>((ax) => float.Parse(ax, System.Globalization.NumberFormatInfo.InvariantInfo)).ToList()).ToList(); List<float> scale = transformationNode.SelectMany("scale").First((a) => a.Attribute("sid").Value == "scale").Value.Trim().Split(new char[] { ' ' }).Select<string, float>((a) => float.Parse(a, System.Globalization.NumberFormatInfo.InvariantInfo)).ToList(); mesh.Translate(-transVector[1], transVector[2], -transVector[0]); foreach(var r in rots) mesh.Rotate(Quaternion.FromAxisAngle(new Vector3(-r[1], r[2], -r[0]), MathHelper.DegreesToRadians(r[3]))); mesh.Scale(scale[1], scale[2], scale[0]); infos.Add(mesh); } catch { } } return infos; }
public SculptScene() { var sun = new Sun(new Vector3(0.1f, -1, 0).ToQuaternion(Vector3.UnitY), new Vector4(1, 0.97f, 0.92f, 120), 300, 100, 70, 40, 10, 1); GLThread.OnUpdate += (o, e) => { var kb = OpenTK.Input.Keyboard.GetState(); if(kb.IsKeyDown(OpenTK.Input.Key.U)) { var quat = Quaternion.FromAxisAngle(sun.Orientation.GetTangent(MathExtensions.TangentDirection.Left), -0.01f); sun.Orientation = Quaternion.Multiply(sun.Orientation, quat); } if(kb.IsKeyDown(OpenTK.Input.Key.J)) { var quat = Quaternion.FromAxisAngle(sun.Orientation.GetTangent(MathExtensions.TangentDirection.Left), 0.01f); sun.Orientation = Quaternion.Multiply(sun.Orientation, quat); } if(kb.IsKeyDown(OpenTK.Input.Key.H)) { var quat = Quaternion.FromAxisAngle(Vector3.UnitY, -0.01f); sun.Orientation = Quaternion.Multiply(sun.Orientation, quat); } if(kb.IsKeyDown(OpenTK.Input.Key.K)) { var quat = Quaternion.FromAxisAngle(Vector3.UnitY, 0.01f); sun.Orientation = Quaternion.Multiply(sun.Orientation, quat); } }; var skysphere = Object3dInfo.LoadFromObjSingle(Media.Get("skyicosphere.obj")); var skymesh = new Mesh3d(skysphere, new GenericMaterial(Color.SkyBlue)); skymesh.Scale(8000); skymesh.MainMaterial.IgnoreLighting = true; Add(skymesh); var dragon3dInfo = Object3dInfo.LoadFromObjSingle(Media.Get("apple.obj")); dragon3dInfo.ScaleUV(0.1f); var mat = GenericMaterial.FromMedia("skin.jpg"); var dragon = new Mesh3d(dragon3dInfo, mat); //mat.Type = GenericMaterial.MaterialType.WetDrops; dragon.Translate(0, 0, 20); //dragon.Scale(5); dragon.SetMass(0); dragon.SetCollisionShape(dragon3dInfo.GetAccurateCollisionShape()); Add(dragon); /* var planeinfo = Object3dGenerator.CreateTerrain(new Vector2(-100, -100), new Vector2(100, 100), new Vector2(50, 50), Vector3.UnitY, 300, (x, y) => 0); var plane = new Mesh3d(planeinfo, new GenericMaterial(Color.Gainsboro)); (plane.MainMaterial as GenericMaterial).SetBumpMapFromMedia("bumpy.jpg"); Add(plane);*/ //var text = new Text(0.0f, 0.5f, "Hello żółć 汉语 / 漢語; Hànyǔ or 中文; Zhōngwén", "Segoe UI", 24, Color.White); //World.Root.UI.Elements.Add(text); /* var tree = TreeGenerator.CreateTreeSingle(MathHelper.DegreesToRadians(30), MathHelper.DegreesToRadians(45), 5, 5, 6666, 0.3f, false, true); Mesh3d nodes = tree[0]; foreach(var t in tree) Add(t); Random rand = new Random(); GLThread.OnUpdate += (o, e) => { foreach(var b in nodes.Bones) { if(b.Name == "root" || b.Name == "rootofroot") continue; var orient = b.Orientation; var randomQuat = Quaternion.Multiply(Quaternion.FromAxisAngle(Vector3.UnitX, (float)rand.NextDouble() - 0.5f), Quaternion.FromAxisAngle(Vector3.UnitZ, (float)rand.NextDouble() - 0.5f)); var neworient = Quaternion.Slerp(orient, randomQuat, 0.11f); b.Orientation = neworient; } };*/ }