} // onPaint().fim // [--- private void gerarModeloRetalhado(ref Mesh obj3d, float nivelTesselacao) { // Produz o mesh do tipo PatchMesh que vai retalhar o objeto // 3d original. PatchMesh retalhador = PatchMesh.CreateNPatchMesh(obj3d); // Calcula a nova quantidade de vértices e faces para o novo mesh int nFaces = (int)(obj3d.NumberFaces * Math.Pow(nivelTesselacao, 3)); int nVerts = (int)(obj3d.NumberVertices * Math.Pow(nivelTesselacao, 3)); // Obtém o formato de vértice do objeto 3d original VertexFormats obj_vfm = obj3d.VertexFormat; // O objeto 3d original não é mais necessário e precisa // ser dispensado neste ponto obj3d.Dispose(); // Recria o objeto com espaço necessário para as novas faces // e vértices obj3d = new Mesh(nFaces, nVerts, MeshFlags.Managed | MeshFlags.Use32Bit, obj_vfm, device); // Retalha o objeto (obj3d) de acordo com o nível de retalhamento // dado por nivelTesselacao retalhador.Tessellate(nivelTesselacao, obj3d); // O objeto retalhador não é mais necessário retalhador.Dispose(); } // gerarModeloRetalhado().fim
/// <summary> /// Generates the patch mesh and assigns it to the MeshFilter of this GameObject. /// </summary> /// <param name="controlPoints"></param> /// <param name="width">Number of control points in x direction</param> /// <param name="height">Number of control points in y direction</param> public void UpdatePatchMesh(List <Vector3> controlPoints, int width, int height) { Mesh patchMesh = PatchMesh.GeneratePatchMesh(controlPoints, width, height, this.resolutionWidth, this.resolutionHeight); Mesh oldMesh = this.GetComponent <MeshFilter>().sharedMesh; Destroy(oldMesh); this.GetComponent <MeshFilter>().mesh = patchMesh; }
public void GeneratePatchVerticesSmall_Parallel([NUnit.Framework.Range(3, 10)] int length) { List <Vector3> controlPoints = GenerateControlPoints(4, length); Measure.Method(() => { PatchMesh.GenerateVerticesOfPatch_Parallel(controlPoints, 4, length, 4, 4); }).Run(); }
public void GeneratePatchVertices_Parallel([Values(10, 50, 100)] int length) { List <Vector3> controlPoints = GenerateControlPoints(4, length); Measure.Method(() => { PatchMesh.GenerateVerticesOfPatch_Parallel(controlPoints, 4, length, 4, 4); }).Run(); }
/// <summary> /// Generates the patch mesh and assigns it to the MeshFilter of this GameObject. /// </summary> /// <param name="controlPoints"></param> /// <param name="width">Number of control points in x direction</param> /// <param name="height">Number of control points in y direction</param> private void UpdatePatchMesh(List <Vector3> controlPoints, int width, int height) { if (height < 3 || width < 3 || controlPoints.Count / width != height) { Debug.LogWarning("The amount of control points is invalid! \n There must at least be 3x3 control points. Amount of control ponits must be a multiple of width."); SetMesh(null); return; } Mesh patchMesh = PatchMesh.GeneratePatchMesh(controlPoints, width, height, this.ResolutionWidth, this.ResolutionHeight); SetMesh(patchMesh); }
public CDLODTerrainRenderer(GraphicsDevice graphicsDevice, CDLODSettings settings) { if (graphicsDevice == null) throw new ArgumentNullException("graphicsDevice"); GraphicsDevice = graphicsDevice; this.settings = settings; instanceVertexBuffer = new WritableVertexBuffer<PatchInstanceVertex>(GraphicsDevice, CDLODSelection.MaxSelectedNodeCount * 2); // TODO: I want to change a patch resolution at runtime. // patchGridSize = leafNodeSize * patchResolution; patchMesh = new PatchMesh(GraphicsDevice, settings.PatchGridSize); }
// seg.bmpはどこに格納する?patchmesh?pathcmeshrenderer? // => pathcMeshRendererResourcesに格納する static PatchSkeletalMesh ToPatchSkeletalMesh(Segment seg, Dictionary <string, Bitmap> bitmaps, int pathPointInterval) { PatchMesh patchMesh = ToPatchMesh(seg, pathPointInterval); PatchSkeleton patchSkeleton = ToPatchSkeleton(seg); List <PatchSection> patchSections = ToPatchSections(seg, patchMesh.pathIndices.Select(i => patchMesh.vertices[i].position).ToList()); // セグメントが使用するビットマップ画像をすべてコピーする。 // この関数外でSharpDXHelper.ToTexture(bmp)を使って各画像をSharpDX描画用のテクスチャに変換する if (seg.bmp != null) { bitmaps[PatchMeshRenderResources.GenerateResourceKey(patchMesh, seg.name)] = new Bitmap(seg.bmp); } PatchSkeletalMesh skeletalMesh = new PatchSkeletalMesh(patchMesh, patchSkeleton, patchSections); return(skeletalMesh); }
static PatchMesh ToPatchMesh(Segment seg, int pathPointInterval) { List <PointF> rawPath = new List <PointF>(seg.path); // 輪郭からメッシュを生成 var shiftPath = FLib.FMath.ShiftPath(seg.path, -seg.offset.X, -seg.offset.Y); var subdividedPath = PathSubdivision.Subdivide(shiftPath, pathPointInterval); subdividedPath.RemoveAt(subdividedPath.Count - 1); // 終点(始点と同じ)は消す List <PointF> vertices = new List <PointF>(); List <int> triIndices = new List <int>(); FLib.Triangle.Triangulate(subdividedPath, vertices, triIndices, FLib.Triangle.Parameters.Default); List <int> path = Enumerable.Range(0, subdividedPath.Count).ToList(); // // seg.partingLineから各頂点が属すpartを計算 // List <int> vert2part = new List <int>(); var partingLine = new List <PointF>(); if (seg.partingLine != null) { partingLine = FLib.FMath.ShiftPath(seg.partingLine, -seg.offset.X, -seg.offset.Y); } PartingMeshes(vertices, triIndices, partingLine, vert2part); string patchKey = seg.name; SizeF textureSize = seg.bmp.Size; var patch = new PatchMesh(vertices, triIndices, path, vert2part, patchKey, textureSize); return(patch); }
// --- Protected Override Methods --- #region CreateScene() // Just override the mandatory create scene method protected override void CreateScene() { // Set ambient light scene.AmbientLight = new ColorEx(0.2f, 0.2f, 0.2f); // Create point light Light light = scene.CreateLight("MainLight"); // Accept default settings: point light, white diffuse, just set position. // I could attach the light to a SceneNode if I wanted it to move automatically with // other objects, but I don't. light.Type = LightType.Directional; light.Direction = new Vector3(-0.5f, -0.5f, 0); // Create patch with positions, normals, and 1 set of texcoords patchDeclaration = HardwareBufferManager.Instance.CreateVertexDeclaration(); patchDeclaration.AddElement(0, 0, VertexElementType.Float3, VertexElementSemantic.Position); patchDeclaration.AddElement(0, 12, VertexElementType.Float3, VertexElementSemantic.Normal); patchDeclaration.AddElement(0, 24, VertexElementType.Float2, VertexElementSemantic.TexCoords, 0); // Patch data PatchVertex[] patchVertices = new PatchVertex[9]; patchVertices[0].X = -500; patchVertices[0].Y = 200; patchVertices[0].Z = -500; patchVertices[0].Nx = -0.5f; patchVertices[0].Ny = 0.5f; patchVertices[0].Nz = 0; patchVertices[0].U = 0; patchVertices[0].V = 0; patchVertices[1].X = 0; patchVertices[1].Y = 500; patchVertices[1].Z = -750; patchVertices[1].Nx = 0; patchVertices[1].Ny = 0.5f; patchVertices[1].Nz = 0; patchVertices[1].U = 0.5f; patchVertices[1].V = 0; patchVertices[2].X = 500; patchVertices[2].Y = 1000; patchVertices[2].Z = -500; patchVertices[2].Nx = 0.5f; patchVertices[2].Ny = 0.5f; patchVertices[2].Nz = 0; patchVertices[2].U = 1; patchVertices[2].V = 0; patchVertices[3].X = -500; patchVertices[3].Y = 0; patchVertices[3].Z = 0; patchVertices[3].Nx = -0.5f; patchVertices[3].Ny = 0.5f; patchVertices[3].Nz = 0; patchVertices[3].U = 0; patchVertices[3].V = 0.5f; patchVertices[4].X = 0; patchVertices[4].Y = 500; patchVertices[4].Z = 0; patchVertices[4].Nx = 0; patchVertices[4].Ny = 0.5f; patchVertices[4].Nz = 0; patchVertices[4].U = 0.5f; patchVertices[4].V = 0.5f; patchVertices[5].X = 500; patchVertices[5].Y = -50; patchVertices[5].Z = 0; patchVertices[5].Nx = 0.5f; patchVertices[5].Ny = 0.5f; patchVertices[5].Nz = 0; patchVertices[5].U = 1; patchVertices[5].V = 0.5f; patchVertices[6].X = -500; patchVertices[6].Y = 0; patchVertices[6].Z = 500; patchVertices[6].Nx = -0.5f; patchVertices[6].Ny = 0.5f; patchVertices[6].Nz = 0; patchVertices[6].U = 0; patchVertices[6].V = 1; patchVertices[7].X = 0; patchVertices[7].Y = 500; patchVertices[7].Z = 500; patchVertices[7].Nx = 0; patchVertices[7].Ny = 0.5f; patchVertices[7].Nz = 0; patchVertices[7].U = 0.5f; patchVertices[7].V = 1; patchVertices[8].X = 500; patchVertices[8].Y = 200; patchVertices[8].Z = 800; patchVertices[8].Nx = 0.5f; patchVertices[8].Ny = 0.5f; patchVertices[8].Nz = 0; patchVertices[8].U = 1; patchVertices[8].V = 1; patch = MeshManager.Instance.CreateBezierPatch("Bezier1", patchVertices, patchDeclaration, 3, 3, 5, 5, VisibleSide.Both, BufferUsage.StaticWriteOnly, BufferUsage.DynamicWriteOnly, true, true); // Start patch a 0 detail patch.SetSubdivision(0); // Create entity based on patch patchEntity = scene.CreateEntity("Entity1", "Bezier1"); Material material = (Material)MaterialManager.Instance.Create("TextMat"); material.GetTechnique(0).GetPass(0).CreateTextureUnitState("BumpyMetal.jpg"); patchEntity.MaterialName = "TextMat"; // Attach the entity to the root of the scene scene.RootSceneNode.AttachObject(patchEntity); camera.Position = new Vector3(500, 500, 1500); camera.LookAt(new Vector3(0, 200, -300)); }
internal static global::System.Runtime.InteropServices.HandleRef getCPtr(PatchMesh obj) { return((obj == null) ? new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero) : obj.swigCPtr); }
protected override void SetupContent() { ResourceGroupManager.Instance.InitializeAllResourceGroups(); // setup some basic lighting for our scene SceneManager.AmbientLight = new ColorEx(0.5f, 0.5f, 0.5f); SceneManager.CreateLight("BezierLight").Position = new Vector3(100, 100, 100); // define the control point vertices for our patch // Patch data var patchVertices = new PatchVertex[9]; patchVertices[0].X = -500; patchVertices[0].Y = 200; patchVertices[0].Z = -500; patchVertices[0].Nx = -0.5f; patchVertices[0].Ny = 0.5f; patchVertices[0].Nz = 0; patchVertices[0].U = 0; patchVertices[0].V = 0; patchVertices[1].X = 0; patchVertices[1].Y = 500; patchVertices[1].Z = -750; patchVertices[1].Nx = 0; patchVertices[1].Ny = 0.5f; patchVertices[1].Nz = 0; patchVertices[1].U = 0.5f; patchVertices[1].V = 0; patchVertices[2].X = 500; patchVertices[2].Y = 1000; patchVertices[2].Z = -500; patchVertices[2].Nx = 0.5f; patchVertices[2].Ny = 0.5f; patchVertices[2].Nz = 0; patchVertices[2].U = 1; patchVertices[2].V = 0; patchVertices[3].X = -500; patchVertices[3].Y = 0; patchVertices[3].Z = 0; patchVertices[3].Nx = -0.5f; patchVertices[3].Ny = 0.5f; patchVertices[3].Nz = 0; patchVertices[3].U = 0; patchVertices[3].V = 0.5f; patchVertices[4].X = 0; patchVertices[4].Y = 500; patchVertices[4].Z = 0; patchVertices[4].Nx = 0; patchVertices[4].Ny = 0.5f; patchVertices[4].Nz = 0; patchVertices[4].U = 0.5f; patchVertices[4].V = 0.5f; patchVertices[5].X = 500; patchVertices[5].Y = -50; patchVertices[5].Z = 0; patchVertices[5].Nx = 0.5f; patchVertices[5].Ny = 0.5f; patchVertices[5].Nz = 0; patchVertices[5].U = 1; patchVertices[5].V = 0.5f; patchVertices[6].X = -500; patchVertices[6].Y = 0; patchVertices[6].Z = 500; patchVertices[6].Nx = -0.5f; patchVertices[6].Ny = 0.5f; patchVertices[6].Nz = 0; patchVertices[6].U = 0; patchVertices[6].V = 1; patchVertices[7].X = 0; patchVertices[7].Y = 500; patchVertices[7].Z = 500; patchVertices[7].Nx = 0; patchVertices[7].Ny = 0.5f; patchVertices[7].Nz = 0; patchVertices[7].U = 0.5f; patchVertices[7].V = 1; patchVertices[8].X = 500; patchVertices[8].Y = 200; patchVertices[8].Z = 800; patchVertices[8].Nx = 0.5f; patchVertices[8].Ny = 0.5f; patchVertices[8].Nz = 0; patchVertices[8].U = 1; patchVertices[8].V = 1; // specify a vertex format declaration for our patch: 3 floats for position, 3 floats for normal, 2 floats for UV this.patchDeclaration = HardwareBufferManager.Instance.CreateVertexDeclaration(); this.patchDeclaration.AddElement(0, 0, VertexElementType.Float3, VertexElementSemantic.Position); this.patchDeclaration.AddElement(0, 12, VertexElementType.Float3, VertexElementSemantic.Normal); this.patchDeclaration.AddElement(0, 24, VertexElementType.Float2, VertexElementSemantic.TexCoords, 0); // create a patch mesh using vertices and declaration this.patch = MeshManager.Instance.CreateBezierPatch("patch", ResourceGroupManager.DefaultResourceGroupName, patchVertices, this.patchDeclaration, 3, 3, 5, 5, VisibleSide.Both, BufferUsage.StaticWriteOnly, BufferUsage.DynamicWriteOnly, true, true); // Start patch at 0 detail this.patch.Subdivision = 0; // Create entity based on patch this.patchEntity = SceneManager.CreateEntity("Entity1", "patch"); var material = (Material)MaterialManager.Instance.Create("TextMat", ResourceGroupManager.DefaultResourceGroupName, null); material.GetTechnique(0).GetPass(0).CreateTextureUnitState("BumpyMetal.jpg"); this.patchEntity.MaterialName = "TextMat"; this.patchPass = material.GetTechnique(0).GetPass(0); // Attach the entity to the root of the scene SceneManager.RootSceneNode.AttachObject(this.patchEntity); // save the main pass of the material so we can toggle wireframe on it if (material != null) { this.patchPass = material.GetTechnique(0).GetPass(0); // use an orbit style camera CameraManager.setStyle(CameraStyle.Orbit); CameraManager.SetYawPitchDist(0, 0, 250); TrayManager.ShowCursor(); // create slider to adjust detail and checkbox to toggle wireframe Slider slider = TrayManager.CreateThickSlider(TrayLocation.TopLeft, "Detail", "Detail", 120, 44, 0, 1, 6); CheckBox box = TrayManager.CreateCheckBox(TrayLocation.TopLeft, "Wireframe", "Wireframe", 120); slider.SliderMoved += new SliderMovedHandler(slider_SliderMoved); box.CheckChanged += new CheckChangedHandler(box_CheckChanged); } }