0
\$\begingroup\$

I'm dealing with a concerning bug in my Unity build.

I have a terrain system which populates its detail layer with procedural grass textures. The textures are on transparency in the editor, as they should be.

grass in-editor

However, after a build (and I've tried both OpenGL and Vulkan) all of the transparency is replaced with matte black.

grass in-build

As you can see, the only thing affected is textures; the meshes of the trees and flowers behave normally.

This is clearly a really big problem for me. It's specifically happening on the Linux platform, but I'm doubting that that is relevant here. I've reported it as a bug, but does anyone else who understands terrain details have a potential solution for me?

I am currently using the LTS build (2021.3.1f LTS). I'm going to try a more recent version of the same build, but that first load takes time...

ADDENDUM: I tried 2021.3.6f LTS and got the same result; but then I attempted to add painted-on grass instead of just using the procedurally generated stuff. I got the transparency in the build! This leads me to believe that there may be a setting that is getting automatically toggled for the editor, but not for the build?

To clarify, my code for adding the grass is thus:

                //detail mesh generation
                DetailPrototype flower = new DetailPrototype();
                flower.useInstancing = true;
                flower.usePrototypeMesh = true;
                flower.renderMode = DetailRenderMode.VertexLit;
                flower.prototype = Resources.Load<GameObject>("flower.02");
                
                //detail texture generation
                DetailPrototype grass = new DetailPrototype();
                grass.prototypeTexture = Resources.Load<Texture2D>("Grass"); 
                 
                _data.detailPrototypes = new DetailPrototype[]{
                    grass,
                    flower
                    };
                
                //generate field for detail mesh
                int[] grassMap = flattenDetails(_data.GetDetailLayer(0, 0, _data.detailWidth, _data.detailHeight, 0));
                int[] flowerMap = flattenDetails(_data.GetDetailLayer(0, 0, _data.detailWidth, _data.detailHeight, 1));

                //plan: Create a grid array that matches each point on the map to plant maturity/health. Use this to generate grass and
                //flower distribution. Finally, mask with dotted slope normal.
                float[] fertility = new float[_data.detailWidth * _data.detailHeight];
                float totalFertility = 0f;
                
                float scale = 100.0f;
                float origin_x = transform.position.x / _data.size.x;
                float origin_z = transform.position.z / _data.size.z;
                float row_width = _data.detailWidth;
                float row_height = grassMap.Length/_data.detailHeight;
                int index = 0;
                for(int z = 0; z < row_height; z++) {
                    for(int x = 0; x < row_width; x++) {
                        index = z * _data.detailWidth + x;
                        //grassMap[z * _data.detailWidth + x] = (int)(Mathf.PerlinNoise(origin_x + x/row_width, origin_z + z/row_height) * 12f);
                        
                        fertility[index] = Mathf.PerlinNoise(scale * (origin_x + x/row_width), scale * (origin_z + z/row_height));
                        grassMap[index] = (int)(fertility[index] * 3f);
                        flowerMap[index] = fertility[index] > 0.5f ? (int)(((fertility[index] - 0.5f)/0.5f) * 3f) : 0;
                        
                        totalFertility += fertility[index];
                    }
                }

As a reminder, the flowers are rendering fine; but the grass isn't. (I'm basically mapping the relative soil fertility on the map to the plant concentration, so it looks like it makes sense.)

\$\endgroup\$
3
  • \$\begingroup\$ This appears to be because alpha blending is not turned on. You can check how the material of the grass generated by two way is different. Maybe one of the shaders is not supported by the target platform, so fallback is used. \$\endgroup\$
    – Mangata
    Jul 14, 2022 at 3:13
  • \$\begingroup\$ Unfortunately there's no direct shader involved; this is a Unity default. I'll look into alpha blending access and see if I can explicitly turn it on. \$\endgroup\$ Jul 14, 2022 at 14:38
  • \$\begingroup\$ As a follow up, I've gotten it to work via a workaround. This only happens when I'm loading textures in C#; if it's already part of a prefab, it works fine. I'm not yet certain what isn't getting called, but I'm keeping Unity apprised of anything I find. \$\endgroup\$ Jul 16, 2022 at 3:18

0

You must log in to answer this question.