public static Entity3D Quad(int w, int h) { Entity3D r = new Entity3D(); Mesh3D mesh = new Mesh3D(6, 4); Vector3 v1 = new Vector3(-w, -h, 0); Vector3 v2 = new Vector3(w, -h, 0); Vector3 v3 = new Vector3(w, h, 0); Vector3 v4 = new Vector3(-w, h, 0); Vector3 z = new Vector3(0, 0, 0); mesh.SetVertex(0, v1, z, z, z, new Vector2(0, 0)); mesh.SetVertex(1, v2, z, z, z, new Vector2(1, 0)); mesh.SetVertex(2, v3, z, z, z, new Vector2(1, 1)); mesh.SetVertex(3, v4, z, z, z, new Vector2(0, 1)); mesh.SetTri(0, 0, 1, 2); mesh.SetTri(1, 2, 3, 0); /* * mesh.SetIndex(0, 0); * mesh.SetIndex(1, 1); * mesh.SetIndex(2, 2); * mesh.SetIndex(3, 2); * mesh.SetIndex(4, 3); * mesh.SetIndex(5, 0); */ mesh.Final( ); r.AddMesh(mesh); return(r); }
public Particle(int w, int h) { W = w; H = h; Mesh3D mesh = new Mesh3D(12, 4); Vector3 p1 = new Vector3(-w / 2, -h / 2, 0); Vector3 p2 = new Vector3(w / 2, -h / 2, 0); Vector3 p3 = new Vector3(w / 2, h / 2, 0); Vector3 p4 = new Vector3(-w / 2, h / 2, 0); Vector2 uv1 = new Vector2(0, 0); Vector2 uv2 = new Vector2(1, 0); Vector2 uv3 = new Vector2(1, 1); Vector2 uv4 = new Vector2(0, 1); Vector3 z = Vector3.Zero; mesh.SetVertex(0, p1, z, z, z, uv1); mesh.SetVertex(1, p2, z, z, z, uv2); mesh.SetVertex(2, p3, z, z, z, uv3); mesh.SetVertex(3, p4, z, z, z, uv4); mesh.SetIndex(0, 0); mesh.SetIndex(1, 1); mesh.SetIndex(2, 2); mesh.SetIndex(3, 2); mesh.SetIndex(4, 3); mesh.SetIndex(5, 0); mesh.SetIndex(6, 2); mesh.SetIndex(7, 1); mesh.SetIndex(8, 0); mesh.SetIndex(9, 0); mesh.SetIndex(10, 3); mesh.SetIndex(11, 2); mesh.Final(); Meshes.Add(mesh); }
public void MakeQuad(float size) { Mesh3D m = new Mesh3D(12, 4); m.SetVertex(0, new Vector3(-size, 0, -size), Vector3.Zero, Vector3.Zero, Vector3.Zero, new Vector2(0, 0)); m.SetVertex(1, new Vector3(size, 0, -size), Vector3.Zero, Vector3.Zero, Vector3.Zero, new Vector2(1, 0)); m.SetVertex(2, new Vector3(size, 0, size), Vector3.Zero, Vector3.Zero, Vector3.Zero, new Vector2(1, 1)); m.SetVertex(3, new Vector3(-size, 0, size), Vector3.Zero, Vector3.Zero, Vector3.Zero, new Vector2(0, 1)); m.SetTri(0, 0, 1, 2); m.SetTri(1, 2, 3, 0); m.SetTri(2, 0, 2, 1); m.SetTri(3, 2, 0, 3); m.Material = new Material3D(); m.Final(); AddMesh(m); Renderer = new VRNoFx(); }
public override Node3D LoadNode(string path) { if (NormBlank == null) { NormBlank = new Texture.Texture2D("data/tex/normblank.png", Texture.LoadMethod.Single, false); DiffBlank = new Texture.Texture2D("data/tex/diffblank.png", Texture.LoadMethod.Single, false); SpecBlank = new Texture.Texture2D("data/tex/specblank.png", Texture.LoadMethod.Single, false); } string ip = path; int ic = ip.LastIndexOf("/"); if (ic < 1) { ic = ip.LastIndexOf("\\"); } if (ic > 0) { IPath = ip.Substring(0, ic); } Entity3D root = new Entity3D(); string file = path; AssimpContext e = new Assimp.AssimpContext(); Assimp.Configs.NormalSmoothingAngleConfig c1 = new Assimp.Configs.NormalSmoothingAngleConfig(75); e.SetConfig(c1); Console.WriteLine("Impporting:" + file); Assimp.Scene s = null; try { s = e.ImportFile(file, PostProcessSteps.OptimizeGraph | PostProcessSteps.FindInvalidData | PostProcessSteps.FindDegenerates | PostProcessSteps.Triangulate | PostProcessSteps.ValidateDataStructure | PostProcessSteps.CalculateTangentSpace | PostProcessSteps.GenerateNormals | PostProcessSteps.FixInFacingNormals | PostProcessSteps.GenerateSmoothNormals); if (s.HasAnimations) { return(LoadAnimNode(path)); } } catch (AssimpException ae) { Console.WriteLine(ae); Console.WriteLine("Failed to import"); Environment.Exit(-1); } Console.WriteLine("Imported."); Dictionary <string, Mesh3D> ml = new Dictionary <string, Mesh3D>(); List <Mesh3D> ml2 = new List <Mesh3D>(); Console.WriteLine("animCount:" + s.AnimationCount); Matrix4x4 tf = s.RootNode.Transform; tf.Inverse(); root.GlobalInverse = ToTK(tf); Dictionary <uint, List <VertexWeight> > boneToWeight = new Dictionary <uint, List <VertexWeight> >(); //root.Animator = new Animation.Animator(); //s.Animations[0].NodeAnimationChannels[0]. //s.Animations[0].anim // root.Animator.InitAssImp(model); foreach (Mesh m in s.Meshes) { Console.WriteLine("M:" + m.Name + " Bones:" + m.BoneCount); Console.WriteLine("AA:" + m.HasMeshAnimationAttachments); Material.Material3D vm = new Material.Material3D { ColorMap = DiffBlank, NormalMap = NormBlank, SpecularMap = SpecBlank }; Mesh3D m2 = new Mesh3D(m.GetIndices().Length, m.VertexCount); ml2.Add(m2); // ml.Add(m.Name, m2); for (int b = 0; b < m.BoneCount; b++) { string name = m.Bones[b].Name; } m2.Material = vm; // root.AddMesh(m2); m2.Name = m.Name; Assimp.Material mat = s.Materials[m.MaterialIndex]; TextureSlot t1; int sc = mat.GetMaterialTextureCount(TextureType.Unknown); Console.WriteLine("SC:" + sc); if (mat.HasColorDiffuse) { vm.Diff = CTV(mat.ColorDiffuse); Console.WriteLine("Diff:" + vm.Diff); } if (mat.HasColorSpecular) { // vm.Spec = CTV ( mat.ColorSpecular ); Console.WriteLine("Spec:" + vm.Spec); } if (mat.HasShininess) { //vm.Shine = 0.3f+ mat.Shininess; Console.WriteLine("Shine:" + vm.Shine); } Console.WriteLine("Spec:" + vm.Spec); //for(int ic = 0; ic < sc; ic++) ///{ if (sc > 0) { TextureSlot tex2 = mat.GetMaterialTextures(TextureType.Unknown)[0]; // vm.SpecularMap = new Texture.Texture2D ( IPath + "/" + tex2.FilePath, Texture.LoadMethod.Single, false ); } if (mat.GetMaterialTextureCount(TextureType.Normals) > 0) { TextureSlot ntt = mat.GetMaterialTextures(TextureType.Normals)[0]; Console.WriteLine("Norm:" + ntt.FilePath); vm.NormalMap = new Texture.Texture2D(IPath + "/" + ntt.FilePath, Vivid.Texture.LoadMethod.Single, false); } if (mat.GetMaterialTextureCount(TextureType.Diffuse) > 0) { t1 = mat.GetMaterialTextures(TextureType.Diffuse)[0]; Console.WriteLine("DiffTex:" + t1.FilePath); if (t1.FilePath != null) { //Console.WriteLine ( "Tex:" + t1.FilePath ); // Console.Write("t1:" + t1.FilePath); vm.ColorMap = new Texture.Texture2D(IPath + "/" + t1.FilePath.Replace(".dds", ".png"), Texture.LoadMethod.Single, false); if (File.Exists(IPath + "/" + "norm_" + t1.FilePath)) { vm.NormalMap = new Texture.Texture2D(IPath + "/" + "norm_" + t1.FilePath, Texture.LoadMethod.Single, false); } } } for (int i = 0; i < m2.NumVertices; i++) { Vector3D v = m.Vertices[i];// * new Vector3D(15, 15, 15); Vector3D n = new Vector3D(0, 1, 0); if (m.Normals != null && m.Normals.Count > i) { n = m.Normals[i]; } List <Vector3D> t = m.TextureCoordinateChannels[0]; Vector3D tan, bi; if (m.Tangents != null && m.Tangents.Count > 0) { tan = m.Tangents[i]; bi = m.BiTangents[i]; } else { tan = new Vector3D(0, 0, 0); bi = new Vector3D(0, 0, 0); } if (t.Count() == 0) { m2.SetVertex(i, Cv(v), Cv(tan), Cv(bi), Cv(n), Cv2(new Vector3D(0, 0, 0))); } else { Vector3D tv = t[i]; tv.Y = 1.0f - tv.Y; m2.SetVertex(i, Cv(v), Cv(tan), Cv(bi), Cv(n), Cv2(tv)); } //var v = new PosNormalTexTanSkinned(pos, norm.ToVector3(), texC.ToVector2(), tan.ToVector3(), weights.First(), boneIndices); //verts.Add(v); } int[] id = m.GetIndices(); uint[] nd = new uint[id.Length]; for (int i = 0; i < id.Length; i += 3) { //Tri t = new Tri(); //t.V0 = (int)nd[i]; // t.V1 = (int)nd[i + 1]; // t.v2 = (int)nd[i + 2]; // nd[i] = (uint)id[i]; if (i + 2 < id.Length) { m2.SetTri(i / 3, id[i], id[i + 1], id[i + 2]); } } m2.Indices = nd; //m2.Scale(AssImpImport.ScaleX, AssImpImport.ScaleY, AssImpImport.ScaleZ); //m2.GenerateTangents ( ); m2.Final(); } ProcessNode(root, s.RootNode, ml2); /* * while (true) * { * } */ return(root as Node3D); }
public Terrain3D(float w, float h, float y, int xsegs, int ysegs, Texture.Texture2D tex) { float xr = tex.W / w; float yr = tex.H / h; byte[] data = tex.RawData; tp = new Vector3 [xsegs, ysegs]; tuv = new Vector3 [xsegs, ysegs]; tnorm = new Vector3 [xsegs, ysegs]; tbi = new Vector3 [xsegs, ysegs]; tan = new Vector3 [xsegs, ysegs]; List <int> indices = new List <int>(); float vx = -w / 2.0f; float vz = -h / 2.0f; float vy = y; float xi = w / xsegs; float zi = h / ysegs; float ui, vi; ui = 0.1f; vi = 0.1f; float vu, vv; vu = 0.0f; vv = 0.0f; for (int sy = 0; sy < ysegs; sy++) { for (int sx = 0; sx < xsegs; sx++) { float ny = 0.0f; float ax = w / 2 + vx; float ay = h / 2 + vz; float tx = ax * xr; float ty = ay * yr; // int loc = (int)((float)ty * (float)tex.W * 3)+ (tx * 3); int loc = (int)ty * (tex.W * 3); loc += ( int )tx * 3; ny = data [loc]; ny = ny * 3; tp [sx, sy] = new Vector3(vx, vy + ny, vz); tuv [sx, sy] = new Vector3(vu, vv, 0); tnorm [sx, sy] = new Vector3(0, 1, 0); vu += ui; //vv += vi; vx += xi; } vu = 0; vv += vi; vx = -w / 2.0f; vz += zi; } for (int sy = 0; sy < ysegs - 1; sy++) { for (int sx = 0; sx < xsegs - 1; sx++) { indices.Add(GetI(sx, sy, xsegs, ysegs)); indices.Add(GetI(sx + 1, sy + 1, xsegs, ysegs)); indices.Add(GetI(sx + 1, sy, xsegs, ysegs)); indices.Add(GetI(sx + 1, sy + 1, xsegs, ysegs)); indices.Add(GetI(sx, sy, xsegs, ysegs)); indices.Add(GetI(sx, sy + 1, xsegs, ysegs)); } } TMesh = new Mesh3D(indices.Count, xsegs * ysegs); int vid = 0; for (int sy = 0; sy < ysegs; sy++) { for (int sx = 0; sx < xsegs; sx++) { TMesh.SetVertex(vid, tp [sx, sy], tan [sx, sy], tbi [sx, sy], tnorm [sx, sy], new Vector2(tuv [sx, sy].X, tuv [sx, sy].Y)); vid++; } } uint[] mi = new uint[indices.Count]; for (int i = 0; i < indices.Count; i++) { mi [i] = ( uint )indices [i]; } for (int i = 0; i < indices.Count / 3; i++) { TMesh.SetTri(i, indices [i * 3], indices [i * 3 + 1], indices [i * 3 + 2]); } TMesh.Indices = mi; TMesh.Mat = new Material.Material3D( ); TMesh.GenerateTangents( ); TMesh.Final( ); Meshes.Add(TMesh); Renderer = new Visuals.VRTerrain( ); Name = "Terrain"; }
private void GenFlat(float w, float h, float y, int xsegs, int ysegs) { tp = new Vector3 [xsegs, ysegs]; tuv = new Vector3 [xsegs, ysegs]; tnorm = new Vector3 [xsegs, ysegs]; tbi = new Vector3 [xsegs, ysegs]; tan = new Vector3 [xsegs, ysegs]; List <int> indices = new List <int>(); float vx = -w / 2.0f; float vz = -h / 2.0f; float vy = y; float xi = w / xsegs; float zi = h / ysegs; float ui, vi; ui = 0.1f; vi = 0.1f; float vu, vv; vu = 0.0f; vv = 0.0f; for (int sy = 0; sy < ysegs; sy++) { for (int sx = 0; sx < xsegs; sx++) { tp [sx, sy] = new Vector3(vx, vy, vz); tuv [sx, sy] = new Vector3(vu, vv, 0); tnorm [sx, sy] = new Vector3(0, 1, 0); vu += ui; //vv += vi; vx += xi; } vu = 0; vv += vi; vx = -w / 2.0f; vz += zi; } for (int sy = 0; sy < ysegs - 1; sy++) { for (int sx = 0; sx < xsegs - 1; sx++) { indices.Add(GetI(sx, sy, xsegs, ysegs)); indices.Add(GetI(sx + 1, sy + 1, xsegs, ysegs)); indices.Add(GetI(sx + 1, sy, xsegs, ysegs)); indices.Add(GetI(sx + 1, sy + 1, xsegs, ysegs)); indices.Add(GetI(sx, sy, xsegs, ysegs)); indices.Add(GetI(sx, sy + 1, xsegs, ysegs)); } } TMesh = new Mesh3D(indices.Count, xsegs * ysegs); int vid = 0; for (int sy = 0; sy < ysegs; sy++) { for (int sx = 0; sx < xsegs; sx++) { TMesh.SetVertex(vid, tp [sx, sy], tan [sx, sy], tbi [sx, sy], tnorm [sx, sy], new Vector2(tuv [sx, sy].X, tuv [sx, sy].Y)); vid++; } } uint[] mi = new uint[indices.Count]; for (int i = 0; i < indices.Count; i++) { mi [i] = ( uint )indices [i]; } for (int i = 0; i < indices.Count / 3; i++) { TMesh.SetTri(i, indices [i * 3], indices [i * 3 + 1], indices [i * 3 + 2]); } TMesh.Indices = mi; TMesh.Mat = new Material.Material3D( ); TMesh.GenerateTangents( ); TMesh.Final( ); Meshes.Add(TMesh); Renderer = new Visuals.VRTerrain( ); Name = "Terrain"; }