unity – Cross texture array into shader and paint mesh vertices relying on vertexattributes

unity – Cross texture array into shader and paint mesh vertices relying on vertexattributes

[ad_1]

Properly,

I am a beginner with shaders, I am simply making an attempt to color a mesh relying on the vertex attribute handed with one or one other texture.

The thought is to realize this, for instance:

...

For this, I’ve been studying some documentation.

So, if I perceive high-quality, I want a vertex subshader with the next construction:

        struct Enter
        {
            float2 uv_MainTex;
            float arrayIndex;
        };

Then go its info from the vert out parameter of sort Enter, for use within the surf shader as an Enter IN param:

        void vert(inout appdata_full v, out Enter o)
        {
            o.uv_MainTex = v.texcoord.xy;
            o.arrayIndex = v.texcoord.z;
        }

        void surf(Enter IN, inout SurfaceOutputStandard o)
        {
            fixed4 c = UNITY_SAMPLE_TEX2DARRAY(_Top, float3(IN.uv_MainTex, IN.arrayIndex)) * _Color;
            o.Albedo = c.rgb;

            // Metallic and smoothness come from slider variables
            o.Metallic = _Metallic;
            o.Smoothness = _Glossiness;
            o.Alpha = c.a;
        }

The complete code of the shader:

Shader "Customized/z3nth10n/MultiTriplanar"
{
    Properties
    {
        _Top("Prime Foremost Texture", 2DArray) = "" { }
        _Color("Colour", Colour) = (1,1,1,1)
        _Glossiness("Smoothness", Vary(0,1)) = 0.5
        _Metallic("Metallic", Vary(0,1)) = 0.0
        _ZOffset("Z Buffer Offset", Float) = 0
    }
    
    SubShader
    {
        Tags { "RenderType" = "Opaque" }
        LOD 200
        Offset[_ZOffset],[_ZOffset]

            CGPROGRAM
        #pragma floor surf Commonplace fullforwardshadows vertex:vert
        #pragma vertex vert
        #pragma require 2darray

        struct Enter
        {
            float2 uv_MainTex;
            float arrayIndex;
        };

        UNITY_DECLARE_TEX2DARRAY(_Top);

        half _Glossiness;
        half _Metallic;
        fixed4 _Color;

        void vert(inout appdata_full v, out Enter o)
        {
            o.uv_MainTex = v.texcoord.xy;
            o.arrayIndex = v.texcoord.z;
        }

        void surf(Enter IN, inout SurfaceOutputStandard o)
        {
            fixed4 c = UNITY_SAMPLE_TEX2DARRAY(_Top, float3(IN.uv_MainTex, IN.arrayIndex)) * _Color;
            o.Albedo = c.rgb;

            // Metallic and smoothness come from slider variables
            o.Metallic = _Metallic;
            o.Smoothness = _Glossiness;
            o.Alpha = c.a;
        }
        ENDCG
    }
        FallBack "Diffuse"
}

That is the total challenge: https://drive.google.com/file/d/1wJ9QyvkSA_rBAXfN1rFowDpWY5EhqEDV/view?usp=sharing

When you open the Customized/z3nth10n/MultiTriplanar shader you’ll discover that there’s extra code, I wish to obtain tri-planar implementation with NativeArrays and VertexAttributes later, however now I am specializing in this half.

As you may see the mesh is exhibiting like this:

...

It has no tiling and any info has handed by the VertexAttribute Mesh Knowledge Buffers.

Additionally, there may be my implementation in C# to point out you ways I go the vertex information to the mesh buffers:

utilizing System.Linq;
utilizing System.Runtime.InteropServices;
utilizing UnityEngine;
utilizing UnityEngine.Rendering;

public class PlaneVerticesTest : MonoBehaviour
{
    [StructLayout(LayoutKind.Sequential)]
    non-public struct BiomeVertexLayout
    {
        public Vector3 texcoord;
    }

    public Texture2DArray texArray;

    // Begin is named earlier than the primary body replace
    non-public void Begin()
    {
        //var texArray = Sources.Load<Texture2DArray>("TextureArrays/Biome1DiffuseTextureArray");
        var textureCount = texArray.depth;

        var mesh = GetComponent<MeshFilter>().mesh;

        Debug.Log(SystemInfo.SupportsVertexAttributeFormat(VertexAttributeFormat.Float32, 4));

        var format = new[]
        {
            new VertexAttributeDescriptor(VertexAttribute.TexCoord0) //, VertexAttributeFormat.Float32, 4)
        };
        mesh.SetVertexBufferParams(mesh.vertexCount, format);

        var information = Enumerable.Vary(0, mesh.vertexCount).Choose(i => new Vector3(mesh.uv[i].x, mesh.uv[i].y, i % textureCount)).ToArray();
        mesh.SetVertexBufferData(information, 0, 0, mesh.vertexCount);
    }

    // Replace is named as soon as per body
    non-public void Replace()
    {
    }
}

As you may see I am utilizing var information = Enumerable.Vary(0, mesh.vertexCount).Choose(i => new Vector3(mesh.uv[i].x, mesh.uv[i].y, i % textureCount)).ToArray();

That implies that per every vertex index, I am simply passing the identical arrayIndex to the shader Enter object (treating the textureCount half with a modulus operator).

Can anybody give me some steerage on this?

[ad_2]

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply