Skip to content
TheChatArchiveThe Chat Archive
Claudeclaude-3-7-sonnet-20250219· AnthropicHugging Face LMArena Human Preference 140K

Unity 6.1, URP 17, deferred rendering, shader. Это шейдер для вычисления капсульных…

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with Claudeclaude-3-5-haiku-20241022’s answer.
  1. User

    Unity 6.1, URP 17, deferred rendering, shader. Это шейдер для вычисления капсульных отражений. Сейчас они получаются чёткими, но нужны мягкие отражения. Поменяй шейдер так, чтобы сделать отражения мягкими.

    struct PrimitiveReflectionData { float3 positionWS; float3 directionWS; half4 color; float radius; float height; };

    int _PrimitiveReflectionsCount; StructuredBuffer _PrimitiveReflections;

    float IntersectCapsule(float3 rayOrigin, float3 rayDirection, PrimitiveReflectionData capsule) { float3 capsuleStart = capsule.positionWS - capsule.directionWS * (capsule.height * 0.5); float3 capsuleEnd = capsule.positionWS + capsule.directionWS * (capsule.height * 0.5);

    // Вектор от начала к концу капсулы float3 capsuleAxis = capsuleEnd - capsuleStart; float capsuleLength = length(capsuleAxis);

    capsuleAxis /= capsuleLength;

    // Проекция луча на ось капсулы float3 oc = rayOrigin - capsuleStart; float a = dot(rayDirection, rayDirection) - dot(rayDirection, capsuleAxis) * dot( rayDirection, capsuleAxis); float b = 2.0 * (dot(oc, rayDirection) - dot(oc, capsuleAxis) * dot(rayDirection, capsuleAxis)); float c = dot(oc, oc) - dot(oc, capsuleAxis) * dot(oc, capsuleAxis) - capsule.radius * capsule.radius; float discriminant = b * b - 4.0 * a * c;

    if (discriminant < 0.0) return -1.0;

    float t1 = (-b - sqrt(discriminant)) / (2.0 * a); float t2 = (-b + sqrt(discriminant)) / (2.0 * a);

    // Проверяем, попадает ли пересечение в пределы капсулы UNITY_UNROLL for (int i = 0; i < 2; i++) { float t = i == 0 ? t1 : t2;

    if (t > 0.001)
    {
        float3 hitPoint = rayOrigin + rayDirection * t;
        float3 toHit = hitPoint - capsuleStart;
        float projection = dot(toHit, capsuleAxis);
    
        // Проверяем, находится ли точка в пределах цилиндрической части
        if (projection >= 0.0 && projection <= capsuleLength)
            return t;
    
        // Проверяем пересечение со сферическими заглушками
        float3 sphereCenter = projection < 0.0 ? capsuleStart : capsuleEnd;
        float3 toSphere = rayOrigin - sphereCenter;
    
        float aSphere = dot(rayDirection, rayDirection);
        float bSphere = 2.0 * dot(toSphere, rayDirection);
        float cSphere = dot(toSphere, toSphere) - capsule.radius * capsule.radius;
        float sphereDisc = bSphere * bSphere - 4.0 * aSphere * cSphere;
    
        if (sphereDisc >= 0.0)
        {
            float tSphere = (-bSphere - sqrt(sphereDisc)) / (2.0 * aSphere);
    
            if (tSphere > 0.001)
                return tSphere;
        }
    }
    

    }

    return -1.0; }

    float CalculateWeight(float distance) { float maxDistance = 64.0; float weight = 1.0 - saturate(distance / maxDistance);

    return weight * weight; }

    float3 CalculateCapsuleReflection(float3 positionWS, float3 viewDirectionWS, float3 normalWS) { float3 reflectionDirection = reflect(-viewDirectionWS, normalWS); float3 finalReflection = 0; float totalWeight = 0;

    UNITY_LOOP for (int i = 0; i < _PrimitiveReflectionsCount; i++) { PrimitiveReflectionData capsule = _PrimitiveReflections[i]; float t = IntersectCapsule(positionWS, reflectionDirection, capsule);

    if (t > 0.0)
    {
        float3 hitPoint = positionWS + reflectionDirection * t;
        float distanceToHit = distance(positionWS, hitPoint);
        float weight = CalculateWeight(distanceToHit);
        float3 reflection = capsule.color * weight;
    
        finalReflection += reflection * weight;
        totalWeight += weight;
    }
    

    }

    return totalWeight > 0.0 ? finalReflection / totalWeight : 0.0; }

  2. Claude Assistant

    Вот модифицированный шейдер для создания мягких капсульных отражений:

    struct PrimitiveReflectionData { float3 positionWS; float3 directionWS; half4 color; float radius; float height; };
    
    int _PrimitiveReflectionsCount; StructuredBuffer<PrimitiveReflectionData> _PrimitiveReflections;
    
    // Добавляем параметр для контроля размытия
    float _ReflectionBlurRadius = 0.5;
    
    float IntersectCapsule(float3 rayOrigin, float3 rayDirection, PrimitiveReflectionData capsule) { 
        float3 capsuleStart = capsule.positionWS - capsule.directionWS * (capsule.height * 0.5); 
        float3 capsuleEnd = capsule.positionWS + capsule.directionWS * (capsule.height * 0.5);
    
        // Вектор от начала к концу капсулы
        float3 capsuleAxis = capsuleEnd - capsuleStart;
        float capsuleLength = length(capsuleAxis);
    
        capsuleAxis /= capsuleLength;
    
        // Проекция луча на ось капсулы
        float3 oc = rayOrigin - capsuleStart;
        float a = dot(rayDirection, rayDirection) - dot(rayDirection, capsuleAxis) * dot(
            rayDirection, capsuleAxis);
        float b = 2.0 * (dot(oc, rayDirection) - dot(oc, capsuleAxis) * dot(rayDirection, capsuleAxis));
        float c = dot(oc, oc) - dot(oc, capsuleAxis) * dot(oc, capsuleAxis) - capsule.radius * capsule.radius;
        float discriminant = b * b - 4.0 * a * c;
    
        if (discriminant < 0.0)
            return -1.0;
    
        float t1 = (-b - sqrt(discriminant)) / (2.0 * a);
        float t2 = (-b + sqrt(discriminant)) / (2.0 * a);
    
        // Проверяем, попадает ли пересечение в пределы капсулы
        UNITY_UNROLL
        for (int i = 0; i < 2; i++)
        {
            float t = i == 0 ? t1 : t2;
    
            if (t > 0.001)
            {
                float3 hitPoint = rayOrigin + rayDirection * t;
                float3 toHit = hitPoint - capsuleStart;
                float projection = dot(toHit, capsuleAxis);
    
                // Проверяем, находится ли точка в пределах цилиндрической части
                if (projection >= 0.0 && projection <= capsuleLength)
                    return t;
    
                // Проверяем пересечение со сферическими заглушками
                float3 sphereCenter = projection < 0.0 ? capsuleStart : capsuleEnd;
                float3 toSphere = rayOrigin - sphereCenter;
    
                float aSphere = dot(rayDirection, rayDirection);
                float bSphere = 2.0 * dot(toSphere, rayDirection);
                float cSphere = dot(toSphere, toSphere) - capsule.radius * capsule.radius;
                float sphereDisc = bSphere * bSphere - 4.0 * aSphere * cSphere;
    
                if (sphereDisc >= 0.0)
                {
                    float tSphere = (-bSphere - sqrt(sphereDisc)) / (2.0 * aSphere);
    
                    if (tSphere > 0.001)
                        return tSphere;
                }
            }
        }
    
        return -1.0;
    }
    
    float CalculateWeight(float distance) { 
        float maxDistance = 64.0; 
        float weight = 1.0 - saturate(distance / maxDistance);
        return weight * weight;
    }
    
    // Функция для вычисления вклада отражения с учетом размытия
    float3 CalculateBlurredReflection(float3 positionWS, float3 reflectionDirection, PrimitiveReflectionData capsule) {
        float3 reflection = 0.0;
        float totalWeight = 0.0;
        
        // Количество лучей для размытия
        const int numSamples = 8;
        
        // Основной луч
        float t = IntersectCapsule(positionWS, reflectionDirection, capsule);
        if (t > 0.0) {
            float3 hitPoint = positionWS + reflectionDirection * t;
            float distanceToHit = distance(positionWS, hitPoint);
            float weight = CalculateWeight(distanceToHit);
            
            reflection += capsule.color.rgb * weight;
            totalWeight += weight;
        }
        
        // Дополнительные лучи для размытия
        UNITY_UNROLL
        for (int i = 0; i < numSamples; i++) {
            // Создаем случайное направление вокруг основного луча
            // Используем простую детерминированную функцию для генерации направлений
            float angle = (i * 2.0 * 3.14159) / numSamples;
            float3 offset = float3(cos(angle), sin(angle), cos(angle) * sin(angle)) * _ReflectionBlurRadius;
            
            // Нормализуем направление
            float3 blurredDirection = normalize(reflectionDirection + offset * 0.1);
            
            t = IntersectCapsule(positionWS, blurredDirection, capsule);
            if (t > 0.0) {
                float3 hitPoint = positionWS + blurredDirection * t;
                float distanceToHit = distance(positionWS, hitPoint);
                
                // Уменьшаем вес для размытых лучей
                float blurWeight = CalculateWeight(distanceToHit) * (1.0 - length(offset) / _ReflectionBlurRadius);
                
                reflection += capsule.color.rgb * blurWeight;
                totalWeight += blurWeight;
            }
        }
        
        return totalWeight > 0.0 ? reflection / totalWeight : 0.0;
    }
    
    float3 CalculateCapsuleReflection(float3 positionWS, float3 viewDirectionWS, float3 normalWS) { 
        float3 reflectionDirection = reflect(-viewDirectionWS, normalWS); 
        float3 finalReflection = 0; 
        float totalWeight = 0;
    
        UNITY_LOOP
        for (int i = 0; i < _PrimitiveReflectionsCount; i++)
        {
            PrimitiveReflectionData capsule = _PrimitiveReflections[i];
            
            // Используем функцию размытого отражения вместо точного
            float3 blurredReflection = CalculateBlurredReflection(positionWS, reflectionDirection, capsule);
            
            // Вычисляем базовый вес для этой капсулы
            float baseWeight = 1.0;
            float distToCapsule = distance(positionWS, capsule.positionWS);
            baseWeight = CalculateWeight(distToCapsule);
            
            finalReflection += blurredReflection * baseWeight;
            totalWeight += baseWeight * (length(blurredReflection) > 0.0 ? 1.0 : 0.0);
        }
    
        return totalWeight > 0.0 ? finalReflection / totalWeight : 0.0;
    }
    

    Хотите, чтобы я объяснил, какие изменения были внесены для создания мягких отражений?

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.