Megasplat Integration

Questions & AnswersCategory: ProgramingMegasplat Integration
Landong asked 5 years ago

https://ibb.co/muPfqp
Screenshot
Megasplat
 
I created an integration for Megasplat, some of the source code needs to be modified for it to work.
MeshData.cs needs the following replacements

        List newUVs;
        List newUVs2;
        List newUVs3;

Then in VertexData.cs

    class VertexData
    {
        private bool needColorRecompute;
        private Color32 color;

        public Vector3 Vertex;
        public Vector3 Normal { get; private set; }
        public Vector4 Uv;
        public Vector4 Uv2;
        public Vector4 Uv3;
        public Vector4 Tangent;
        public float Blockiness { get; private set; }
        public Color32 Color {
            get {
                if (needColorRecompute) {
                    this.Color = VoxelType.Functions.GetVertexColor(Normal);
                    this.Uv = VoxelType.Functions.ModVertexUVW(this.Uv);
                    this.Uv2 = VoxelType.Functions.ModVertexUVW2(this.Uv2);
                    this.Uv3 = VoxelType.Functions.ModVertexUVW3(this.Uv3);
                }

Then in the FreeToMeshLists Function add

            if (newUVs2 != null)
            {
                meshDataListsPools.FreeListVector4(newUVs2);
                newUVs2 = null;
            }
            if (newUVs3 != null)
            {
                meshDataListsPools.FreeListVector4(newUVs3);
                newUVs3 = null;
            }

Then in PrepareToMesh Function Add these lines 

            UProfiler.Begin("Get lists from pool");
            newVertices = meshDataListsPools.GetListVector3(count);
            newColors = meshDataListsPools.GetListColor(count);
            newNormals = meshDataListsPools.GetListVector3(count);
            newUVs = meshDataListsPools.GetListVector4(count);
            newUVs2 = meshDataListsPools.GetListVector4(count);
            newUVs3 = meshDataListsPools.GetListVector4(count);
            newTangents = meshDataListsPools.GetListVector4(count);
            UProfiler.End();

 
As well as these

            UProfiler.Begin("Fill lists");
            for (int i = 0; i < count; ++i) {
                VertexData v = Vertices[i];
                newVertices.Add(v.Vertex);
                newColors.Add(v.Color);
                newNormals.Add(v.Normal);
                newUVs.Add(v.Uv);
                newUVs2.Add(v.Uv2);
                newUVs3.Add(v.Uv3);
                newTangents.Add(v.Tangent);
            }
            UProfiler.End();

 
AbstractVoxelTypeFunctions.cs

using UnityEngine;
using System;


namespace UltimateTerrains
{
	[Serializable]
	public abstract class AbstractVoxelTypeFunctions : ScriptableObject, IOnGUI
	{
		public abstract Color32 GetVertexColor (Vector3 vertexNormal);
        public abstract Vector4 ModVertexUVW(Vector4 vertexUVW);
        public abstract Vector4 ModVertexUVW2(Vector4 vertexUVW);
        public abstract Vector4 ModVertexUVW3(Vector4 vertexUVW);
        public abstract void OnGUI ();
	}
}

 
 
MegaSplatVoxelFunction.cs

using UnityEngine;
using System;
#if UNITY_EDITOR
using UnityEditor;
#endif

namespace UltimateTerrains
{
    [Serializable]
    public class MegaSplatVoxelFunction : AbstractVoxelTypeFunctions
    {
        [SerializeField]
        Color32
            vertexColor = Color.clear;
        [SerializeField]
        float Wetness = 0.0f;
        [SerializeField]
        float Flow = 0.0f;
        [SerializeField]
        float Direction = 0.0f;
        [SerializeField]
        float LayerBlend = 0.0f;
        [SerializeField]
        float Displacement = 0.0f;
        [SerializeField]
        float PuddleAmount = 0.0f;
        [SerializeField]
        float TopLayer = 0.0f;
        public override Color32 GetVertexColor(Vector3 vertexNormal)
        {
            return vertexColor;
        }

        public override Vector4 ModVertexUVW(Vector4 vertexUVW)
        {
            //throw new NotImplementedException();
            vertexUVW.w = Wetness;
            return vertexUVW;
        }
        public override Vector4 ModVertexUVW2(Vector4 vertexUVW)
        {
            //throw new NotImplementedException();
            vertexUVW.z = Flow;
            vertexUVW.w = Direction;
            return vertexUVW;
        }
        public override Vector4 ModVertexUVW3(Vector4 vertexUVW)
        {
            //throw new NotImplementedException();
            vertexUVW.x = LayerBlend;
            vertexUVW.y = Displacement;
            vertexUVW.z = PuddleAmount;
            vertexUVW.w = (float)Mathf.RoundToInt(TopLayer);
            return vertexUVW;
        }
        public override void OnGUI()
        {
#if UNITY_EDITOR
            vertexColor = EditorGUILayout.ColorField("Vertex Color:", vertexColor);
            Wetness = EditorGUILayout.Slider("Wetness",Wetness, 0.0f, 1.0f);
            Flow = EditorGUILayout.Slider("Flow", Flow, 0.0f, 1.0f);
            Direction = EditorGUILayout.Slider("Direction", Direction, 0.0f, 1.0f);
            LayerBlend = EditorGUILayout.Slider("Layer Blend", LayerBlend, 0.0f, 1.0f);
            Displacement = EditorGUILayout.Slider("Displacement", Displacement, 0.0f, 1.0f);
            PuddleAmount = EditorGUILayout.Slider("Puddle Amount", PuddleAmount, 0.0f, 1.0f);
            TopLayer = EditorGUILayout.Slider("Top Layer", TopLayer, 0, 255);
#endif
        }
    }
}
4 Answers
Landong answered 5 years ago

I do have one outstanding issue with the integration at the moment. i have these little artifacts, i made a small override script that essentially places a red, green and blue color for each vertex in the newColors list in the MeshData.cs file. I think the problem is that the indices on the triangle are causing some triangles to have two of one color in spots. See screenshot.

Any idea on a good way to get around that?
 
Artifacts

Landong answered 5 years ago

Is the grass generator tied to face vertex colors? because this is happening after i mod the tris to work with megasplat.
 
 
Rainbow Grass

Landong replied 5 years ago

poop i found where.. hmm i had to make a duplicate shader that doesn’t do the tint :/

Landong answered 5 years ago

there was one more thing i forgot to add in, but basically in order to be compatible with the shader each face must have 1 red vertex, 1 green vertex and 1 blue vertex.. otherwise some edge artifacts appear. 
i did update the source a little more to reflect this. If you want i can email the changed source files.

uTerrains Staff answered 5 years ago

Very interesting! Thanks for sharing! I will definitely include this in a future update.