URP decal bug.Error when last decal is in distance

DecalCreateDrawCallSystem.cs
line 180 should be like this.Otherwise if last decal is in distance, all other decals will be discarded.

                    float distanceToDecal = (cameraPosition - boundingSphere.position).magnitude;
                    float cullDistance = math.min(drawDistance.x, maxDrawDistance) + boundingSphere.radius;
                    if (distanceToDecal <= cullDistance)
                    {

                        decalToWorldsDraw[instanceIndex] = decalToWorlds[decalIndex];

                        float fadeFactorScaler = fadeFactors[decalIndex];
                        float2 angleFade = angleFades[decalIndex];
                        float4 uvScaleBias = uvScaleBiases[decalIndex];

                        float4x4 normalToDecals = normalToWorlds[decalIndex];
                        // NormalToWorldBatchis a Matrix4x4x but is a Rotation matrix so bottom row and last column can be used for other data to save space
                        float fadeFactor = fadeFactorScaler * math.clamp((cullDistance - distanceToDecal) / (cullDistance * (1.0f - drawDistance.y)), 0.0f, 1.0f);
                        normalToDecals.c0.w = uvScaleBias.x;
                        normalToDecals.c1.w = uvScaleBias.y;
                        normalToDecals.c2.w = uvScaleBias.z;
                        normalToDecals.c3 = new float4(fadeFactor * 1.0f, angleFade.x, angleFade.y, uvScaleBias.w);
                        normalToDecalsDraw[instanceIndex] = normalToDecals;

                        instanceIndex++;
                    }

                    int instanceCount = instanceIndex - instanceStart;
                    bool isReachedMaximumBatchSize = instanceCount >= 250;
                    bool isLastDecal = i == visibleDecalCount - 1;
                    if (isReachedMaximumBatchSize || isLastDecal)
                    {
                        subCalls[subCallIndex++] = new DecalSubDrawCall()
                        {
                            start = instanceStart,
                            end = instanceIndex,
                        };
                        instanceStart = instanceIndex;
                    }
1 Like

DecalDrawSystem.cs line 159 should change like this:

Graphics.DrawMesh(mesh, decalDrawCallChunk.decalToWorlds[j], material, decalCachedChunk.layerMasks[j], cameraData.camera, 0, decalCachedChunk.propertyBlock);