public void Update() { if (this.needsUpdate && !this.original.IsNull()) { if (this.type == MeshSmoothType.Laplacian) { //this.current = MeshSmoothing.LaplacianFilter(this.original.Copy(),this.iterations); this.current = this.original.Copy(); for (int index = 0; index < this.iterations; ++index) { this.current.vertices = SmoothFilter.laplacianFilter(this.current.vertices, this.current.triangles); } } if (this.type == MeshSmoothType.HCLaplacian) { //this.current = MeshSmoothing.HCFilter(this.original.Copy(),this.iterations,this.hcAlpha,this.hcBeta); this.current = this.original.Copy(); for (int index = 0; index < this.iterations; ++index) { this.current.vertices = SmoothFilter.hcFilter(this.original.vertices, this.current.vertices, this.current.triangles, this.hcAlpha, this.hcBeta); } } this.source.SetMesh(this.current); this.needsUpdate = false; } }
//not used public static Mesh Smooth(MeshFilter meshfilter) { Mesh sourceMesh; Mesh workingMesh; // Clone the cloth mesh to work on sourceMesh = new Mesh(); // Get the sourceMesh from the originalSkinnedMesh sourceMesh = meshfilter.mesh; // Clone the sourceMesh workingMesh = CloneMesh(sourceMesh); // Reference workingMesh to see deformations meshfilter.mesh = workingMesh; // Apply Laplacian Smoothing Filter to Mesh int iterations = 1; for (int i = 0; i < iterations; i++) { //workingMesh.vertices = SmoothFilter.laplacianFilter(workingMesh.vertices, workingMesh.triangles); workingMesh.vertices = SmoothFilter.hcFilter(sourceMesh.vertices, workingMesh.vertices, workingMesh.triangles, 0.0f, 0.5f); } return(workingMesh); }
// Use this for initialization void Start() { MeshFilter meshfilter = gameObject.GetComponentInChildren <MeshFilter>(); sourceMesh = new Mesh(); // Get the sourceMesh from the originalSkinnedMesh sourceMesh = meshfilter.mesh; // Clone the sourceMesh workingMesh = MeshUtils.CloneMesh(sourceMesh); // Reference workingMesh to see deformations meshfilter.mesh = workingMesh; workingMesh.vertices = SmoothFilter.hcFilter(sourceMesh.vertices, workingMesh.vertices, workingMesh.triangles, 0.0f, 0.5f); }
public static Mesh SmoothMesh(Mesh mesh, int power, Filter filterType) { for (int i = 0; i < power; ++i) { if (filterType == Filter.HC) { mesh.vertices = SmoothFilter.hcFilter(mesh.vertices, mesh.vertices, mesh.triangles, 0.0f, 0.5f); } if (filterType == Filter.Laplacian) { mesh.vertices = SmoothFilter.laplacianFilter(mesh.vertices, mesh.triangles); } } return(mesh); }
/// <summary> /// Creates the surface objects. /// </summary> /// <param name='voxels'> /// Voxels, i.e. the scalar field used to compute the surface. /// </param> /// <param name='threshold'> /// The threshold on which the isosurface is based. /// </param> /// <param name='delta'> /// Delta parameter from the grid, basically the size of each cell. /// </param> /// <param name='origin'> /// Origin of the grid. /// </param> /// <param name='colors'> /// Colors. Kept from previous implementation, but doesn't do anything here. I'm only /// keeping it because I'm not sure what it was used for. --- Alexandre /// </param> /// <param name='tag'> /// Tag for the objects to be created. /// </param> /// <param name='electro'> /// True if this is an electrostatic field isosurface. /// </param> public static void CreateSurfaceObjects(float[,,] voxels, float threshold, Vector3 delta, Vector3 origin, Color[] colors, string tag = "SurfaceManager", bool electro = false) { ELECTRO = electro; Debug.Log(ELECTRO.ToString()); if (ELECTRO) { ReadDX readDX = UI.GUIMoleculeController.readdx; origin = readDX.GetOrigin(); delta = readDX.GetDelta(); } InitGenMesh(voxels, threshold, delta, origin, tag); SetDims(); float bMCTime = Time.realtimeSinceStartup; MeshData mData = MarchingCubes.CreateMesh(VOXELS, 0, XDIM, 0, YDIM, 0, ZDIM); Debug.Log("Entire surface contains " + mData.vertices.Length.ToString() + " vertices."); float elapsed = 10f * (Time.realtimeSinceStartup - bMCTime); Debug.Log("GenerateMesh::MarchingCubes time: " + elapsed.ToString()); OffsetVertices(mData); float bSmooth = Time.realtimeSinceStartup; AdjacencySets adjacencySets = new AdjacencySets(mData.triangles.Length); adjacencySets.AddAllTriangles(mData.triangles); SmoothFilter.AdjSetsSmoother(mData, adjacencySets); elapsed = Time.realtimeSinceStartup - bSmooth; Debug.Log("Smoothing time: " + elapsed.ToString()); ProperlyCalculateNormals(mData); // Necessary for electrostatic fields isosurfaces Debug.Log(threshold.ToString()); if (threshold < 0) { FlipTriangles(mData); } Splitting splitting = new Splitting(); List <Mesh> meshes = splitting.Split(mData); CreateSurfaceObjects(meshes); }
private Vector3[] GenerateVertices() { int smoothingPasses = (noise.overtones * smoothings) + 1; HeightMap map = new HeightMap(SmoothFilter.ComputeStartSize(size + smoothingPasses, smoothingPasses)); SmoothFilter smooth = new SmoothFilter(); noise.ClearOvertoneFilters(); for (int i = 0; i != smoothings; ++i) { noise.AddOvertoneFilter(smooth); } map = smooth.Filter(weathering.Filter(typeFilter.Filter(noise.Filter(map)))); int dif = map.Size - size; int offset = dif >> 1; Vector3[] vertices = map.ToVertices(offset, offset, size); ClampToOrigin(vertices); return(vertices); }
public void Smooth() { // Get the sourceMesh from the originalSkinnedMesh Mesh sourceMesh = meshFilter.sharedMesh; // Clone the sourceMesh Mesh workingMesh = MeshUtils.CloneMesh(sourceMesh); // Reference workingMesh to see deformations meshFilter.sharedMesh = workingMesh; // Apply Laplacian Smoothing Filter to Mesh int iterations = 1; for (int i = 0; i < iterations; i++) { //workingMesh.vertices = SmoothFilter.laplacianFilter(workingMesh.vertices, workingMesh.triangles); sourceMesh.vertices = SmoothFilter.hcFilter(workingMesh.vertices, sourceMesh.vertices, workingMesh.triangles, 0.0f, 0.5f); } UpdateMountain(); }
void Start() { MeshFilter meshfilter = gameObject.GetComponentInChildren <MeshFilter>(); // Clone the cloth mesh to work on sourceMesh = new Mesh(); // Get the sourceMesh from the originalSkinnedMesh sourceMesh = meshfilter.mesh; // Clone the sourceMesh workingMesh = CloneMesh(sourceMesh); // Reference workingMesh to see deformations meshfilter.mesh = workingMesh; // Apply Laplacian Smoothing Filter to Mesh int iterations = 1; for (int i = 0; i < iterations; i++) { //workingMesh.vertices = SmoothFilter.laplacianFilter(workingMesh.vertices, workingMesh.triangles); workingMesh.vertices = SmoothFilter.hcFilter(sourceMesh.vertices, workingMesh.vertices, workingMesh.triangles, 0.0f, 0.5f); } }
void Start() { skin = GetComponent <SkinnedMeshRenderer>(); mesh = skin.sharedMesh; meshForCPUOutput = Instantiate(mesh); deformedMesh = new DeformedMesh(mesh.vertexCount); adjacencyMatrix = GetCachedAdjacencyMatrix(mesh, adjacencyMatchingVertexTolerance); // Compute if (SystemInfo.supportsComputeShaders && computeShader && ductTapedShader) { verticesCB = new ComputeBuffer(mesh.vertices.Length, 3 * sizeof(float)); normalsCB = new ComputeBuffer(mesh.vertices.Length, 3 * sizeof(float)); weightsCB = new ComputeBuffer(mesh.vertices.Length, 4 * sizeof(float) + 4 * sizeof(int)); verticesCB.SetData(mesh.vertices); normalsCB.SetData(mesh.normals); weightsCB.SetData(mesh.boneWeights); adjacencyCB = new ComputeBuffer(adjacencyMatrix.Length, sizeof(int)); var adjArray = new int[adjacencyMatrix.Length]; Buffer.BlockCopy(adjacencyMatrix, 0, adjArray, 0, adjacencyMatrix.Length * sizeof(int)); adjacencyCB.SetData(adjArray); bonesCB = new ComputeBuffer(skin.bones.Length, 16 * sizeof(float)); deltavCB = new ComputeBuffer(mesh.vertices.Length, 3 * sizeof(float)); deltanCB = new ComputeBuffer(mesh.vertices.Length, 3 * sizeof(float)); outputCB[0] = new ComputeBuffer(mesh.vertices.Length, 6 * sizeof(float)); outputCB[1] = new ComputeBuffer(mesh.vertices.Length, 6 * sizeof(float)); outputCB[2] = new ComputeBuffer(mesh.vertices.Length, 6 * sizeof(float)); deformKernel = computeShader.FindKernel("DeformMesh"); computeShader.SetBuffer(deformKernel, "Vertices", verticesCB); computeShader.SetBuffer(deformKernel, "Normals", normalsCB); computeShader.SetBuffer(deformKernel, "Weights", weightsCB); computeShader.SetBuffer(deformKernel, "Bones", bonesCB); computeShader.SetInt("VertexCount", mesh.vertices.Length); laplacianKernel = GetSmoothKernel(); computeShader.SetBuffer(laplacianKernel, "Adjacency", adjacencyCB); computeShader.SetInt("AdjacentNeighborCount", adjacencyMatrix.GetLength(1)); uint threadGroupSizeX, threadGroupSizeY, threadGroupSizeZ; computeShader.GetKernelThreadGroupSizes(deformKernel, out threadGroupSizeX, out threadGroupSizeY, out threadGroupSizeZ); computeThreadGroupSizeX = (int)threadGroupSizeX; computeShader.GetKernelThreadGroupSizes(laplacianKernel, out threadGroupSizeX, out threadGroupSizeY, out threadGroupSizeZ); Debug.Assert(computeThreadGroupSizeX == (int)threadGroupSizeX); ductTapedMaterial = new Material(ductTapedShader); ductTapedMaterial.CopyPropertiesFromMaterial(skin.sharedMaterial); } else { useCompute = false; } UpdateDeltaVectors(); // Experiment with blending bone weights BoneWeight[] bw = mesh.boneWeights; prefilteredBoneWeights = new float[mesh.vertexCount, skin.bones.Length]; for (int i = 0; i < mesh.vertexCount; ++i) { prefilteredBoneWeights[i, bw[i].boneIndex0] = bw[i].weight0; prefilteredBoneWeights[i, bw[i].boneIndex1] = bw[i].weight1; prefilteredBoneWeights[i, bw[i].boneIndex2] = bw[i].weight2; prefilteredBoneWeights[i, bw[i].boneIndex3] = bw[i].weight3; } for (int i = 0; i < iterations; i++) { prefilteredBoneWeights = SmoothFilter.distanceWeightedLaplacianFilter(mesh.vertices, prefilteredBoneWeights, adjacencyMatrix); } var boneCount = skin.bones.Length; for (int i = 0; i < mesh.vertexCount; ++i) { float l = 0.0f; for (int b = 0; b < boneCount; ++b) { l += prefilteredBoneWeights[i, b]; } for (int b = 0; b < boneCount; ++b) { prefilteredBoneWeights[i, b] += prefilteredBoneWeights[i, b] * (1.0f - l); } } }