Beispiel #1
0
    public static void BarkMapping(MaterialData mat, TriangleData triangle, List <Vector2> uvs)
    {
        Vector3 a      = TriangleData.GetVector(triangle, 0);
        Vector3 b      = TriangleData.GetVector(triangle, 1);
        Vector3 c      = TriangleData.GetVector(triangle, 2);
        Vector3 normal = Vector3.Cross(a - b, a - c).normalized;

        Vector2 uva = Vector2.zero;
        Vector2 uvb = Vector2.zero;
        Vector2 uvc = Vector2.zero;

        // Sides are 0, top/bottom is 1. This info should be provided by the
        // MaterialData object. Won't work for bark materials, will it?
        // Don't implement this yet. I need to see how MaterialData matures.
        // MaterialIndex.GetTileIndex(mi, materialData, SIDE);
        int tile = 0;

        if (IsAlignedX(normal, axisAlignThreshold))
        {
            uva = ProjectX(a, mat.halfSize, mat.size);
            uvb = ProjectX(b, mat.halfSize, mat.size);
            uvc = ProjectX(c, mat.halfSize, mat.size);
            // Pick a vertex and see if it is a part of the bark
            tile = (a.x < -(mat.halfSize.x - mat.barkThickness.x)) ||
                   (a.x > mat.halfSize.x - mat.barkThickness.x)
                ? 0
                : 1;
        }

        if (IsAlignedZ(normal, axisAlignThreshold))
        {
            uva  = ProjectZ(a, mat.halfSize, mat.size);
            uvb  = ProjectZ(b, mat.halfSize, mat.size);
            uvc  = ProjectZ(c, mat.halfSize, mat.size);
            tile = (a.z < -(mat.halfSize.z - mat.barkThickness.x)) ||
                   (a.z > mat.halfSize.z - mat.barkThickness.x)
                ? 0
                : 1;
        }

        if (IsAlignedY(normal, axisAlignThreshold))
        {
            uva  = ProjectY(a, mat.halfSize, mat.size);
            uvb  = ProjectY(b, mat.halfSize, mat.size);
            uvc  = ProjectY(c, mat.halfSize, mat.size);
            tile = (a.y < -(mat.halfSize.y - mat.barkThickness.y)) ||
                   (a.y > mat.halfSize.y - mat.barkThickness.y)
                ? 2
                : 3;
        }

        uvs[triangle.indices[0]] = RemapUVCoordToTile(mat, uva, tile);
        uvs[triangle.indices[1]] = RemapUVCoordToTile(mat, uvb, tile);
        uvs[triangle.indices[2]] = RemapUVCoordToTile(mat, uvc, tile);
    }
Beispiel #2
0
    // Projects textures along the cardinal axis.
    // The bark texture is applied to the sides, no
    // matter how far into the mesh you slice.
    public static void ProjectionMapping(MaterialData mat, TriangleData triangle, List <Vector2> uvs)
    {
        Vector3 a      = TriangleData.GetVector(triangle, 0);
        Vector3 b      = TriangleData.GetVector(triangle, 1);
        Vector3 c      = TriangleData.GetVector(triangle, 2);
        Vector3 normal = Vector3.Cross(a - b, a - c).normalized;

        Vector2 uva = Vector2.zero;
        Vector2 uvb = Vector2.zero;
        Vector2 uvc = Vector2.zero;

        // Sides are 0, top/bottom is 1.
        // This info should be provided by the
        // MaterialData object.
        // MaterialIndex.GetTileIndex(mi, materialData, SIDE);

        // Tile 0 is the side with bark
        int tile = 0;

        // Check if the normal is within a range of +/- 85 deg
        // of the world Y axis.
        if (IsAlignedY(normal, 85f))
        {
            uva = ProjectY(a, mat.halfSize, mat.size);
            uvb = ProjectY(b, mat.halfSize, mat.size);
            uvc = ProjectY(c, mat.halfSize, mat.size);
            // Tile 3 is the edge without bark
            tile = 3;
        }

        // These checks fail when slicing at an angle outside
        // axisAlignThreshold. Could improve the IsAligned functions
        // to accept an axis to project along. That way I could
        // project the texture along the normal of the triangle.
        // how would that translate to UV coords?
        if (IsAlignedX(normal, axisAlignThreshold))
        {
            uva = ProjectX(a, mat.halfSize, mat.size);
            uvb = ProjectX(b, mat.halfSize, mat.size);
            uvc = ProjectX(c, mat.halfSize, mat.size);
        }

        if (IsAlignedZ(normal, axisAlignThreshold))
        {
            uva = ProjectZ(a, mat.halfSize, mat.size);
            uvb = ProjectZ(b, mat.halfSize, mat.size);
            uvc = ProjectZ(c, mat.halfSize, mat.size);
        }

        uvs[triangle.indices[0]] = RemapUVCoordToTile(mat, uva, tile);
        uvs[triangle.indices[1]] = RemapUVCoordToTile(mat, uvb, tile);
        uvs[triangle.indices[2]] = RemapUVCoordToTile(mat, uvc, tile);
    }