Example #1
0
    private void ReflectFunc(Ray ray, RaycastHit hit, MicrofacetModel BRDF, int depth, float multi, ref Color slightColor, ref Color dlightColor)
    {
        //反射
        var   V        = -ray.direction;
        var   N        = hit.normal;
        var   dirs     = BRDF.ReflectDirections(N, V, reflectCount);
        float countInv = 1.0f / dirs.Count;

        foreach (var reflectDir in dirs)
        {
            var L      = reflectDir;
            Ray refRay = new Ray(hit.point + N * 0.001f, reflectDir);
            //if (FixNLV(ref N, L, V) == false) continue;

            var s = BRDF.getSIntensity(N, L, V);
            var d = BRDF.getDIntensity();

            Color      reflectColor = Color.black;
            RaycastHit reflecthit;
            if (RayTracing(refRay, out reflectColor, out reflecthit, depth + 1, multi * Math.Max(s, d)))
            {
                float intensity = countInv;

                reflectColor *= intensity;

                //高光计算
                slightColor += reflectColor * s;

                //漫反射计算
                dlightColor += reflectColor * d;
            }
        }
    }
Example #2
0
    private void Lighting(Ray ray, RaycastHit hit, MicrofacetModel BRDF, ref Color slightColor, ref Color dlightColor)
    {
        foreach (var light in lights)
        {
            switch (light.type)
            {
            case LightType.Spot:
            {
            }
            break;

            case LightType.Directional:
                break;

            case LightType.Point:
            {
                var L   = light.transform.position - hit.point;
                var len = L.magnitude;
                L.Normalize();

                //点到光源之间被遮挡,阴影特效
                if (len > light.range || Physics.Raycast(hit.point + hit.normal * 0.001f, L, len))
                {
                    break;
                }
                var N = hit.normal;
                var V = -ray.direction;

                //在碰撞点上面的光强
                var intensity = light.intensity;
                intensity *= (float)(Math.Pow(1 - len / light.range, 2)); //光根据距离衰减
                intensity *= Math.Max(0, Vector3.Dot(N, L));              //光投影到面的衰减

                //if (FixNLV(ref N, L, V) == false) continue;

                //高光计算
                slightColor += light.color * intensity * BRDF.getSIntensity(N, L, V);

                //漫反射计算
                dlightColor += light.color * intensity * BRDF.getDIntensity();        //在半球上进行均匀漫反射 d*1/(2*pi)
            }
            break;

            case LightType.Area:
                break;

            default:
                break;
            }
        }
    }