Beispiel #1
0
        public void Handle(ElementMeshReader.Result meshReaderResult)
        {
            // Color is TBGR form
            // see https://imodeljs.github.io/iModelJs-docs-output//reference/imodeljs-common/symbology/colordef/
            var key = new MaterialKey {
                Color = meshReaderResult.Color, TextureId = meshReaderResult.TextureId
            };

            if (!_cachedMaterials.TryGetValue(key, out Material material))
            {
                var color = new Color32((byte)(meshReaderResult.Color & 0xFF),
                                        (byte)((meshReaderResult.Color >> 8) & 0xFF),
                                        (byte)((meshReaderResult.Color >> 16) & 0xFF),
                                        (byte)(255 - ((meshReaderResult.Color >> 24) & 0xFF)));
                material = new Material(color.a > 235 ? _opaqueTemplate : _transparentTemplate)
                {
                    color = color
                };
                if (!string.IsNullOrEmpty(key.TextureId))
                {
                    material.mainTexture = _textureCache.GetById(key.TextureId);
                }

                _cachedMaterials[key] = material;
            }

            IndexFormat indexFormat = meshReaderResult.Vertices.Length > 65000 ? IndexFormat.UInt32 : IndexFormat.UInt16;
            var         unityMesh   = new Mesh
            {
                indexFormat = indexFormat,
                vertices    = meshReaderResult.Vertices.ToArray(),
                normals     = meshReaderResult.Normals.ToArray(),
                uv          = meshReaderResult.UVs.ToArray(),
                triangles   = meshReaderResult.Indices.ToArray()
            };

            // Object name is element ID for use with metadata queries on selection
            var go = new GameObject {
                name = meshReaderResult.ElementId
            };

            go.transform.SetParent(_rootGameObject.transform, false);

            var collider = go.AddComponent <MeshCollider>();

            collider.cookingOptions = MeshColliderCookingOptions.None;
            collider.sharedMesh     = unityMesh;

            go.AddComponent <MeshFilter>().sharedMesh       = unityMesh;
            go.AddComponent <MeshRenderer>().sharedMaterial = material;

            meshReaderResult.Dispose();
        }
Beispiel #2
0
        public void Handle(ElementMeshReader.Result mesh)
        {
            if (_meshesByColor.TryGetValue(mesh.Color, out List <ElementMeshReader.Result> meshList))
            {
                meshList.Add(mesh);
                return;
            }

            meshList = new List <ElementMeshReader.Result> {
                mesh
            };
            _meshesByColor[mesh.Color] = meshList;
        }