![]() Public STerrainData GetTerrainDataFromVoxelColor(Color color)įloat bestDifference = float.PositiveInfinity įoreach (STerrainData data in terrainImportData)įloat newDifference = Mathf.Abs(color.r - ) Ret = textureToSample.GetPixelBilinear(uv.x, uv.y) ĭebug.LogError("Could not get RenderTexture for " + material.name) TextureToSample.ReadPixels(new Rect(0, 0, blitTexture.width, blitTexture.height), destX: 0, destY: 0) ĬachedRenderTextures.Add(material, textureToSample) TextureToSample = new Texture2D(blitTexture.width, blitTexture.height, materialTexture.format, mipmap: false, linear: true) Graphics.Blit(materialTexture, blitTexture) RenderTexture blitTexture = new RenderTexture(materialTexture.width, materialTexture.height, depth: 0, format: RenderTextureFormat.Default, readWrite: RenderTextureReadWrite.Linear) Texture2D materialTexture = material.mainTexture as Texture2D If (!cachedRenderTextures.TryGetValue(material, out textureToSample)) Copy the material's Texture2D to a new Texture2D because it imports into Unity as non-readalbe by default. Public Color GetColorFromMaterial(Material material, Vector2 uv) Private static Dictionary cachedRenderTextures = new Dictionary() Cache created RenderTextures so we don't have to blit the texture every time we check for a color. Public class MagicaVoxelRuntimeMeshSplitterData : MonoBehaviour This data prefab can be assigned as a field to other meshes we import from MagicaVoxel, so that we don’t have to set up the data separately for each mesh’s prefab. This seems like a good thing to automate, so we’ll have a data prefab that maps color values to materials. ![]() ![]() Great! Now we just need to split these out to assign separate materials and physics materials to them. When we run the scene, the meshes get split out by color like this: Disable the original so that it doesn't overlap with the split meshes. MeshCollider newCollider = newMeshObject.GetComponent() MeshFilter newMeshObject = GameObject.Instantiate(originalMeshFilter) MeshDataDictionary.Add(triangle) įoreach (Vector2 key in meshDataDictionary.Keys) If (!meshDataDictionary.ContainsKey(triangle.uv)) Split the triangles out into meshes based on their texture UVs.įoreach (STriangle triangle in allTriangles) Convert the mesh into a triangles list.įor (int i = 0 i meshDataDictionary = new Dictionary() Vector2 rawUvs = rawMesh.uv // Color coordinates on the material's palette. Int rawTris = iangles // Triplets of indices into the vertex array. MeshFilter originalMeshFilter = GetComponentInChildren() Public Vector2 uv // We can assume that all three points on a triangle from Magicavoxel point at the same texture coordinate. ![]() Public class MagicaVoxelRuntimeMeshSplitter : MonoBehaviour I decided the simplest way to implement this was using a component to split the mesh out into separate submeshes. (Note: See the list of problems with my approach at the bottom of this blog post.) Obviously, the solution was to add a whole new import feature to my asset pipeline so that my placeholder art could support the cosmetic water ripples feature. obj terrain file imported from MagicaVoxel was all one mesh, so I couldn’t assign different render materials and physics materials to each surface type. This worked fine in my first prototype scene of cubes with manually assigned materials, but the. I also had plans for making water reflect the sky. My Unity character controller had code for the player to make ripples on water physics materials. It was looking pretty good.īut then I ran into an annoying problem. In August I was using MagicaVoxel to prototype a videogame’s 3d overworld. This is an overview of a Unity 3D Engine prototype for processing MagicaVoxel OBJ meshes into submeshes for each voxel color so that they can be assigned different materials, physics materials, collider layers, etc.
0 Comments
Leave a Reply. |