WebGL中使用三角形网格的光线跟踪因3k +个三角形而失败

时间:2019-03-07 07:25:33

标签: javascript opengl-es glsl webgl raytracing

我正在测试我为一些fps反馈编写的光线跟踪器。使用3k +三角形的网格,可以正确渲染约2秒钟,然后整个画布变成红色(红色是场景中的原色)。如果我有一些我无法理解的基本错误,或者是因为我的光线跟踪路径没有任何加速方法(BVH或kd-tree),我将在此处发布有关发生的情况的反馈,并且现在我检查所有三角形的交点。 Smalls场景(1-1.5k三角形)我可以成功渲染它们,所以我无法想象出什么问题了。 我以为它会变慢。

我在openGL ES中发布了射线追踪器的2个主要功能

bool hitScene(Ray R_, out vec3 hitPos, out vec3 normal, out Material material, Sphere lightSource){ 

    vec4 a = vec4(0.0), b = vec4(0.0), c = vec4(0.0), aN = vec4(0.0),bN= vec4(0.0),cN= vec4(0.0);
    float mindist = 1000.;
    bool weHitSomething = false;
    vec3 hitPos1 = vec3(0.),triangleNormal = vec3(0.,0.,0.), sphereNormal, barycentricCoord;


    int alg = 2;
    if (alg == 2) {

        //here we chck all the mesh if we hit a triangle if the mesh and we keep the closest hitpoint

        for (int i = 0; i < vertsCount; i += 3) {

            a = texelFetch(uMeshData, ivec2(i, 0), 0);
            b = texelFetchOffset(uMeshData, ivec2(i, 0), 0, ivec2(1, 0));
            c = texelFetchOffset(uMeshData, ivec2(i, 0), 0, ivec2(2, 0));

            aN = texelFetch(uNormData, ivec2(i, 0), 0);
            bN = texelFetchOffset(uNormData, ivec2(i, 0), 0, ivec2(1, 0));
            cN = texelFetchOffset(uNormData, ivec2(i, 0), 0, ivec2(2, 0));

            vec3 uvt;
            vec3 intersect;
            float z;
            bool isHit = hitTriangleSecond(R_.orig, R_.dir, a.xyz, b.xyz, c.xyz, uvt, triangleNormal, intersect, z);
            if (isHit) {

                if (z<mindist && z > 0.001) {
                    hitPos1 = intersect;

                    mindist = z;
                    weHitSomething = true;
                    material.type = DIEL;
                    material.albedo = vec3(.8, .3, .4);
                    normal = normalize(aN.xyz*uvt.x + bN.xyz*uvt.y + cN.xyz*uvt.z);
                    hitPos = hitPos1;            
                }
            }      
        } 

    }

   return weHitSomething;
}

//Trace is the main function of the max bounces
vec3 Trace(out Ray ray, Sphere lightSource){

    vec3 hitPos, normal;
    bool isShpere;
    Material material;
    vec3 color = vec3(1.);
    vec3 attenuation = vec3(1.);
    vec3 light = vec3(1.,1.,1.), shadow = vec3(1.,1.,1.);

        for(int i=0; i< 3; i++){

            // we check if we hit something
            if(hitScene(ray, hitPos, normal, material, lightSource)){
                if (material.type == METAL) {
                    //we calculate the new direction
                    vec3 direction = normalize(reflect(ray.dir, normal));


                    ray = Ray(hitPos, direction); 
                    light = getLight(color, lightSource,hitPos, normal);
                    //shadow = calcShadow(lightSource, hitPos);
                    color *= material.albedo * light*shadow;
                     attenuation *= material.albedo;   
                     //color = normal *light; 
                }
                if (material.type == DIEL) {
                    //we calculate the new direction
                    vec3 direction = normalize(reflect(ray.dir, normal));

                    ray = Ray(hitPos, direction); 
                    light = getLight(color, lightSource,hitPos, normal);
                    //shadow = calcShadow(lightSource, hitPos);
                    color *= material.albedo * light*shadow;
                    attenuation *= material.albedo;   

                }
            } else {
                //color = attenuation * vec3(.2,.2, .2);
            }

        }

    return color;
}

我编写了一个对象解析器,以在python中保持每个顶点的三角形法线和三角形顶点的法线。然后将它们加载到hitScene()函数中

0 个答案:

没有答案