function stringContains(ary, str) {
    for (let i = 0; i < ary.length; i++) {
        if (ary[i] == str) {
            return true
        }
    }
    return false
}
var BuildCtrl = function (scene) {
    var _inst = this;

    this.init = function () {
    }
    this.loadBuildModel = function (path, name, onProgress, onFinish){
        let allProgress = 0, tempProgress=0;
        BABYLON.SceneLoader.ImportMesh("", path, name, scene, function(meshes){
            let dimian = null;
            let dimianList1 = [];
            for(let i = 0; i < meshes.length; i++) {
                let mesh = meshes[i]
                
                if (stringContains(ModelConfig.visibleCollider, mesh.name)) { // 可见的碰撞体
                    mesh.checkCollisions = true;
                    mesh.fRay = true;
                } else if (stringContains(ModelConfig.hiddenCollider, mesh.name)) { // 隐藏的碰撞体
                    mesh.checkCollisions = true;
                    mesh.fRay = true;
                    mesh.visibility = 0;
                } else if (stringContains(ModelConfig.glasses, mesh.name)) { // 玻璃
                    mesh.material.transparencyMode = BABYLON.ALPHABLEND;
                    mesh.visibility = 0.5;
                }
                if (ModelConfig.ground == mesh.name) {
                    dimian = mesh;
                } else {
                    dimianList1.push(mesh);
                }
                // 先隐藏展品框
                if (mesh.name.startsWith("PIC_")) {
                    mesh.setEnabled(false)
                }
            }
            if(!ToolMgr.isMac()) {
                if (ModelConfig.type == 1) { // 现代
                    dimian.position.y += 0.2;
                }
                dimian.computeWorldMatrix(true);
                var glass_worldMatrix = dimian.getWorldMatrix();

                //Obtain normals for plane and assign one of them as the normal
                var glass_vertexData = dimian.getVerticesData("normal");
                var glassNormal = new BABYLON.Vector3(glass_vertexData[0], glass_vertexData[1], glass_vertexData[2]);
                //Use worldMatrix to transform normal into its current value
                glassNormal = new BABYLON.Vector3.TransformNormal(glassNormal, glass_worldMatrix)

                //Create reflecting surface for mirror surface
                var reflector = new BABYLON.Plane.FromPositionAndNormal(dimian.absolutePosition, glassNormal.scale(-1));
                //Create the mirror material
                var mirrorMaterial = new BABYLON.StandardMaterial("mirror", scene);
                mirrorMaterial.reflectionTexture = new BABYLON.MirrorTexture("mirror", 1024, scene, true);
                mirrorMaterial.reflectionTexture.mirrorPlane = reflector;
                mirrorMaterial.reflectionTexture.renderList = dimianList1;
                mirrorMaterial.reflectionTexture.level = 0.1;
                mirrorMaterial.emissiveTexture = dimian.material.albedoTexture;
                mirrorMaterial.reflectionTexture.adaptiveBlurKernel = 10;
                mirrorMaterial.disableLighting = true;
                mirrorMaterial.environmentIntensity = 0;

                dimian.material = mirrorMaterial;
            }

            if(onFinish){
                onFinish();
            }
        },function (progress) {
            if(tempProgress < 1)
                allProgress -= tempProgress;
            allProgress += progress.loaded/progress.total;
            tempProgress = progress.loaded/progress.total;

            if(onProgress){
                onProgress(Math.round(allProgress*50));
            }
        });
    }
    this.init();
}
