Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
protected createDisplayParams(json: any): DisplayParams | undefined {
const type = JsonUtils.asInt(json.type, DisplayParams.Type.Mesh);
const lineColor = new ColorDef(JsonUtils.asInt(json.lineColor));
const fillColor = new ColorDef(JsonUtils.asInt(json.fillColor));
const width = JsonUtils.asInt(json.lineWidth);
const linePixels = JsonUtils.asInt(json.linePixels, LinePixels.Solid);
const fillFlags = JsonUtils.asInt(json.fillFlags, FillFlags.None);
const ignoreLighting = JsonUtils.asBool(json.ignoreLighting);
// Material will always contain its own texture if it has one
const materialKey = json.materialId;
const material = undefined !== materialKey ? this.materialFromJson(materialKey) : undefined;
// We will only attempt to include the texture if material is undefined
let textureMapping;
if (!material) {
const textureJson = json.texture;
textureMapping = undefined !== textureJson ? this.textureMappingFromJson(textureJson) : undefined;
if (undefined === textureMapping) {
// Look for a gradient. If defined, create a texture mapping. No reason to pass the Gradient.Symb to the DisplayParams once we have the texture.
const gradientProps = json.gradient as Gradient.SymbProps;
const gradient = undefined !== gradientProps ? Gradient.Symb.fromJSON(gradientProps) : undefined;
if (undefined !== gradient) {
constructor(props: ModelProps, iModel: IModelDb) {
super(props, iModel);
this.name = props.name ? props.name : ""; // NB this isn't really a property of Model (it's the code.value of the modeled element), but it comes in ModelProps because it's often needed
this.isPrivate = JsonUtils.asBool(props.isPrivate);
this.isTemplate = JsonUtils.asBool(props.isTemplate);
this.jsonProperties = Object.assign({}, props.jsonProperties); // make sure we have our own copy
}
const name = JsonUtils.asString(json.name);
const namedTex = 0 !== name.length ? this._namedTextures[name] : undefined;
const texture = undefined !== namedTex ? namedTex.renderTexture as RenderTexture : undefined;
if (undefined === texture) {
assert(false, "bad texture mapping json");
return undefined;
}
const paramsJson = json.params;
const tf = paramsJson.transform;
const paramProps: TextureMapping.ParamProps = {
textureMat2x3: new TextureMapping.Trans2x3(tf[0][0], tf[0][1], tf[0][2], tf[1][0], tf[1][1], tf[1][2]),
textureWeight: JsonUtils.asDouble(paramsJson.weight, 1.0),
mapMode: JsonUtils.asInt(paramsJson.mode),
worldMapping: JsonUtils.asBool(paramsJson.worldMapping),
};
return new TextureMapping(texture, new TextureMapping.Params(paramProps));
}
materialParams.diffuse = JsonUtils.asDouble(materialJson.diffuse);
materialParams.specularColor = this.colorDefFromMaterialJson(materialJson.specularColor);
if (materialJson.specular !== undefined)
materialParams.specular = JsonUtils.asDouble(materialJson.specular);
materialParams.reflectColor = this.colorDefFromMaterialJson(materialJson.reflectColor);
if (materialJson.reflect !== undefined)
materialParams.reflect = JsonUtils.asDouble(materialJson.reflect);
if (materialJson.specularExponent !== undefined)
materialParams.specularExponent = materialJson.specularExponent;
if (undefined !== materialJson.transparency)
materialParams.alpha = 1.0 - materialJson.transparency;
materialParams.refract = JsonUtils.asDouble(materialJson.refract);
materialParams.shadows = JsonUtils.asBool(materialJson.shadows);
materialParams.ambient = JsonUtils.asDouble(materialJson.ambient);
if (undefined !== materialJson.textureMapping)
materialParams.textureMapping = this.textureMappingFromJson(materialJson.textureMapping.texture);
material = this._system.createMaterial(materialParams, this._iModel);
}
return material;
}
private readMeshGraphic(primitive: any): RenderGraphic | undefined {
const materialName = JsonUtils.asString(primitive.material);
const materialValue = 0 < materialName.length ? JsonUtils.asObject(this._materialValues[materialName]) : undefined;
const displayParams = undefined !== materialValue ? this.createDisplayParams(materialValue) : undefined;
if (undefined === displayParams)
return undefined;
const vertices = this.readVertexTable(primitive);
if (undefined === vertices) {
assert(false, "bad vertex table in tile data.");
return undefined;
}
const isPlanar = JsonUtils.asBool(primitive.isPlanar);
const primitiveType = JsonUtils.asInt(primitive.type, Mesh.PrimitiveType.Mesh);
const instances = this.readInstances(primitive);
switch (primitiveType) {
case Mesh.PrimitiveType.Mesh:
return this.createMeshGraphic(primitive, displayParams, vertices, isPlanar, this.readAuxChannelTable(primitive), instances);
case Mesh.PrimitiveType.Polyline:
return this.createPolylineGraphic(primitive, displayParams, vertices, isPlanar, instances);
case Mesh.PrimitiveType.Point:
return this.createPointStringGraphic(primitive, displayParams, vertices, instances);
}
assert(false, "unhandled primitive type");
return undefined;
}
const indices = this.readVertexIndices(surf.indices);
if (undefined === indices)
return undefined;
const type = JsonUtils.asInt(surf.type, -1);
if (!isValidSurfaceType(type))
return undefined;
const texture = undefined !== displayParams.textureMapping ? displayParams.textureMapping.texture : undefined;
let material: SurfaceMaterial | undefined;
const atlas = mesh.vertices.materialAtlas;
const numColors = mesh.vertices.numColors;
if (undefined !== atlas && undefined !== numColors) {
material = {
isAtlas: true,
hasTranslucency: JsonUtils.asBool(atlas.hasTranslucency),
overridesAlpha: JsonUtils.asBool(atlas.overridesAlpha, false),
vertexTableOffset: JsonUtils.asInt(numColors),
numMaterials: JsonUtils.asInt(atlas.numMaterials),
};
} else {
material = createSurfaceMaterial(displayParams.material);
}
return {
type,
indices,
fillFlags: displayParams.fillFlags,
hasBakedLighting: false,
hasFixedNormals: false,
material,
texture,
materialParams.diffuseColor = this.colorDefFromMaterialJson(materialJson.diffuseColor);
if (materialJson.diffuse !== undefined)
materialParams.diffuse = JsonUtils.asDouble(materialJson.diffuse);
materialParams.specularColor = this.colorDefFromMaterialJson(materialJson.specularColor);
if (materialJson.specular !== undefined)
materialParams.specular = JsonUtils.asDouble(materialJson.specular);
materialParams.reflectColor = this.colorDefFromMaterialJson(materialJson.reflectColor);
if (materialJson.reflect !== undefined)
materialParams.reflect = JsonUtils.asDouble(materialJson.reflect);
if (materialJson.specularExponent !== undefined)
materialParams.specularExponent = materialJson.specularExponent;
if (materialJson.transparency !== undefined)
materialParams.transparency = materialJson.transparency;
materialParams.refract = JsonUtils.asDouble(materialJson.refract);
materialParams.shadows = JsonUtils.asBool(materialJson.shadows);
materialParams.ambient = JsonUtils.asDouble(materialJson.ambient);
if (undefined !== materialJson.textureMapping)
materialParams.textureMapping = this.textureMappingFromJson(materialJson.textureMapping.texture);
material = this._system.createMaterial(materialParams, this._iModel);
}
return material;
}