XbimEssentials icon indicating copy to clipboard operation
XbimEssentials copied to clipboard

Support for Texture Mapping

Open Ozymandias1 opened this issue 5 years ago • 3 comments

i try to make some geometry using IfcTriangulatedFaceSet with texture mapping.

i searching for example or sample but i can't find how to create geometry with texture mapping for ifc format.

and i wrote some code with ifc and xbim documents.

when i create ifc file use my code, geometry are shows up in Xbim Xplorer but can't see texture mapping.

am i use wrong approach?

or is there any way for create ifc geometry with texture mapping?

best regards.

using (var txn = model.BeginTransaction(string.Format("Create Ifc Polygon Object: {0}", data.name)))
{
    var coordinates = model.Instances.New<IfcCartesianPointList3D>();
    var tfs = model.Instances.New<IfcTriangulatedFaceSet>(fs =>
        {
            fs.Closed = false;
            fs.Coordinates = coordinates;
        }
    );
    var texMap = model.Instances.New<IfcIndexedTriangleTextureMap>();
    texMap.MappedTo = tfs; // connect to face set

    var imageTexture = model.Instances.New<IfcImageTexture>();
    imageTexture.URLReference = "./MySampleTexture.jpg";
    texMap.Maps.Add(imageTexture);

    // convert position vertex set to double[] list
    var points = new List<double[]>();
    for (int vdx = 0; vdx < data.vertices.Length; vdx++)
    {
        var v0 = data.vertices[vdx];
        points.Add(new double[] { v0.x, v0.z * -1.0, v0.y }); // invert zy
    }
    for (int vdx = 0; vdx < points.Count; vdx++)
    {
        var values = points[vdx].Select(v0 => new IfcLengthMeasure(v0));
        coordinates.CoordList.GetAt(vdx).AddRange(values);
    }
    // convert face index set to long[] list
    var indices = new List<long[]>();
    for (int idx = 0; idx < data.indices.Length; idx += 3)
    {
        // index are starting 1, not 0.
        var i0 = data.indices[idx + 0] + 1;
        var i1 = data.indices[idx + 1] + 1;
        var i2 = data.indices[idx + 2] + 1;
        indices.Add(new long[] { i0, i1, i2 });
    }
    for (int idx = 0; idx < indices.Count; idx++)
    {
        var values = indices[idx].Select(i0 => new IfcPositiveInteger(i0));
        tfs.CoordIndex.GetAt(idx).AddRange(values);
        texMap.TexCoordIndex.GetAt(idx).AddRange(values); // uv index are same as face index
    }

    // UV
    var texVtxList = model.Instances.New<IfcTextureVertexList>();
    texMap.TexCoords = texVtxList;
    var uvs = new List<double[]>();
    for(int vdx = 0; vdx < data.uvs.Length; vdx++)
    {
        var v0 = data.uvs[vdx];
        uvs.Add(new double[] { v0.x, v0.y });
    }
    for(int vdx = 0; vdx < uvs.Count; vdx++)
    {
        var values = uvs[vdx].Select(v0 => new IfcParameterValue(v0));
        texVtxList.TexCoordsList.GetAt(vdx).AddRange(values);
    }

    // create ifcshape, representation
    var shape = model.Instances.New<IfcShapeRepresentation>();
    var context = model.Instances.OfType<IfcGeometricRepresentationContext>().FirstOrDefault();
    shape.ContextOfItems = context;
    shape.RepresentationType = "Tessellation";
    shape.RepresentationIdentifier = "Body";
    shape.Items.Add(tfs);

    var rep = model.Instances.New<IfcProductDefinitionShape>();
    rep.Representations.Add(shape);
    
    var column = model.Instances.New<IfcColumn>();
    column.Name = data.name;
    column.Representation = rep;
    ifcParentGroup.RelatedElements.Add(column);    

    txn.Commit();
}

image

Ozymandias1 avatar May 12 '20 09:05 Ozymandias1

I'm sorry but our viewer doesn't handle textures. We only show colours. To be fair, I don't think you will find many IFC tools which would handle textures. If you know about any, let us know please.

martin1cerny avatar May 12 '20 09:05 martin1cerny

There was some experimental work by @Noranius to support texture mapping last year https://github.com/xBimTeam/XbimWindowsUI/issues/127

I don't know how far he got. It might be a good start to look at IfcBlobTexture support

andyward avatar May 12 '20 10:05 andyward

Yes I tried to achieve some results there. However, I could not reach the final target. see xBimTeam/XbimWindowsUI#140. In general I've made changes to the essentials and the presentation. But the final result does not display the expected texture. Whereby, i could check that the texture, respectively the jpg, is loaded successfully.

Noranius avatar May 13 '20 12:05 Noranius