Can normal maps be generated from a texture?

前端 未结 4 491
情深已故
情深已故 2021-01-31 23:56

If I have a texture, is it then possible to generate a normal-map for this texture, so it can be used for bump-mapping?

Or how are normal maps usually made?

4条回答
  •  粉色の甜心
    2021-02-01 00:33

    I suggest starting with OpenCV, due to its richness in algorithms. Here's one I wrote that iteratively blurs the normal map and weights those to the overall value, essentially creating more of a topological map.

    #define ROW_PTR(img, y) ((uchar*)((img).data + (img).step * y))
    cv::Mat normalMap(const cv::Mat& bwTexture, double pStrength)
    {
        // assume square texture, not necessarily true in real code
        int scale = 1.0;
        int delta = 127;
    
        cv::Mat sobelZ, sobelX, sobelY;
        cv::Sobel(bwTexture, sobelX, CV_8U, 1, 0, 13, scale, delta, cv::BORDER_DEFAULT);
        cv::Sobel(bwTexture, sobelY, CV_8U, 0, 1, 13, scale, delta, cv::BORDER_DEFAULT);
        sobelZ = cv::Mat(bwTexture.rows, bwTexture.cols, CV_8UC1);
    
        for(int y=0; yplanes;
    
        planes.push_back(sobelX);
        planes.push_back(sobelY);
        planes.push_back(sobelZ);
    
        cv::Mat normalMap;
        cv::merge(planes, normalMap);
    
        cv::Mat originalNormalMap = normalMap.clone();
    
        cv::Mat normalMapBlurred;
    
        for (int i=0; i<3; i++) {
            cv::GaussianBlur(normalMap, normalMapBlurred, cv::Size(13, 13), 5, 5);
            addWeighted(normalMap, 0.4, normalMapBlurred, 0.6, 0, normalMap);
        }
        addWeighted(originalNormalMap, 0.3, normalMapBlurred, 0.7, 0, normalMap);
    
        return normalMap;
    }
    

提交回复
热议问题