// standard global variables
var container, scene, camera, renderer, controls;
var keyboard;
// custom global variables
var cube;
var floor;
var defaultTextute;
var wallTexture;
var collumTextute;
var baconTextute;
var doorTexute;
var door2wTexute;
var windowTexture;
var roofTex, sphereTex;
var glassTexture;
var inside = false;
var floor_width = 1283;
var floor_height = 960;
var isViewHandrail = false;

var wallPaintTexture;
var wallPaint = [];
var loader = new THREE.TextureLoader();
var wallObj, pillarObj, doorObj, windowObj, baconObj, floorObj, floorInfo, baconInfo, roofSlice, isDraw, roofSlice1, roofSlice2, woodWindowObj, roof_2roof_obj, roof_1roof_obj, roof3D, handrailObj, stepperObj;

var floor3D = {
    'height_startpoint': converMMtoPX(0),
    'height_endpoint': converMMtoPX(100),
};
var bacon3D = {
    'height_startpoint': converMMtoPX(0),
    'height_endpoint': converMMtoPX(100),
};


// MR 1340 - START
var handrail3D = {
    'height_startpoint': converMMtoPX(300),
    'height_endpoint': converMMtoPX(1000),
};
// MR 1340 - END
var stepper3D = {
    'height_startpoint': converMMtoPX(0),
    'height_endpoint': converMMtoPX(250),
};

var wall3D = {
    'height_startpoint': converMMtoPX(0),
    'height_endpoint': converMMtoPX(2330),
};
var column3D = {
    'height_startpoint': converMMtoPX(0),
    'height_endpoint': converMMtoPX(2330),
};
var door3D = {
    'height_startpoint': converMMtoPX(100),
    'height_endpoint': converMMtoPX(1780),
};
var window3D = {
    'height_startpoint': converMMtoPX(580),
    'height_endpoint': converMMtoPX(1780),
};
// initialization

var viewHandrailEvent= function () {
	console.log("viewHandrailEvent");
	isViewHandrail = !isViewHandrail;
	view3d(false);
}
// animation loop / game loop
var view2d = function () {
    document.getElementById('canvas').style.display = 'block';
    document.getElementById('view2d').style.display = 'none';
    document.getElementById('canvas3d').style.display = 'none';
    document.getElementById('inside').style.display = 'none';
    document.getElementById('outside').style.display = 'none';
    document.getElementById('no-roof').style.display = 'none';
    document.getElementById('wallColor').style.display = 'none';
    document.getElementById('wall-select').style.display = 'none';
    document.getElementById('viewHandrail').style.display = 'none';
    var canvas3d = document.getElementById('canvas3d');
    var remove = canvas3d.getElementsByTagName('canvas');
    canvas3d.removeChild(remove[0]);
    isChoose = 0;
    stage.removeChild(opacityShapeBox);
};
var view3d = function (isInside) {
    if (transitionState != STATE_TRANSITION_SET_3DVIEW) return;
    if (isInside == true) {
        inside = true;
    }
    if (isInside == false)
        inside = false;

    var canvas3d = document.getElementById('canvas3d');
    var remove = canvas3d.getElementsByTagName('canvas');
    if (remove[0])
        canvas3d.removeChild(remove[0]);
    document.getElementById('canvas').style.display = 'none';
    document.getElementById('view2d').style.display = 'block';
    document.getElementById('inside').style.display = 'block';
    document.getElementById('outside').style.display = 'block';
    if (!inside) {
        document.getElementById('no-roof').style.display = 'block';
    } else {
        document.getElementById('no-roof').style.display = 'none';
    }
    document.getElementById('wallColor').style.display = 'block';
    document.getElementById('wall-select').style.display = 'block';
    document.getElementById('viewHandrail').style.display = 'block';

    document.getElementById('canvas3d').style.display = 'block';
    defaultTextute = new loader.load('images/wood-floor.jpg');
    doorTexute = new loader.load('images/door1wings.jpg');
    door2wTexute = new loader.load('images/door2wings.jpg');
    glassTexture = new loader.load('images/blue.png');
    keyboard = new THREEx.KeyboardState();
    wallObj = [];
    pillarObj = [];
    doorObj = [];
    windowObj = [];
    baconObj = new THREE.Object3D();

    floorObj = new THREE.Object3D();
    woodWindowObj = [];
    roofSlice = null;
    roofSlice1 = null;
    roofSlice2 = null;
    // MR 1340 - START
    handrailObj = [];
    // MR 1340 - END
    stepperObj = [];
    wallPaint = [];


    // Load wall pain texture
    wallPaintTexture = loader.load('images/kabe.jpg');
    wallPaintTexture.wrapS = THREE.RepeatWrapping;
    wallPaintTexture.wrapT = THREE.RepeatWrapping;
    wallPaintTexture.repeat.set(5, 5);  
    
    // Load default wall color
    var selectColor = document.getElementById('wall_option').selectedIndex;  
    wallTexture = new loader.load('images/' + wallColor[selectColor][2] + '.PNG');
    wallTexture.wrapT = THREE.RepeatWrapping;
    wallTexture.repeat.set(5, 5);
    

    initializeScene();
    animate()
};
function initializeScene() {

    scene = new THREE.Scene();


    var SCREEN_WIDTH = window.innerWidth, SCREEN_HEIGHT = window.innerHeight;
    var VIEW_ANGLE = 45, ASPECT = SCREEN_WIDTH / SCREEN_HEIGHT, NEAR = 0.1, FAR = 20000;
    camera = new THREE.PerspectiveCamera(VIEW_ANGLE, ASPECT, NEAR, FAR);
    scene.add(camera);
    if (inside) {
        camera.position.set(1, 0, 1);
    } else {
        camera.position.set(0, 700, 1400);
    }

    camera.lookAt(scene.position);


    if (Detector.webgl)
        renderer = new THREE.WebGLRenderer({antialias: true});
    else
        renderer = new THREE.CanvasRenderer();

    renderer.setSize(SCREEN_WIDTH, SCREEN_HEIGHT);

    container = document.getElementById('canvas3d');
    container.appendChild(renderer.domElement);


    // automatically resize renderer
    THREEx.WindowResize(renderer, camera);
    // toggle full-screen on given key press
    THREEx.FullScreen.bindKey({charCode: 'm'.charCodeAt(0)});


    controls = new THREE.OrbitControls(camera, renderer.domElement);


    var ambientLight = new THREE.AmbientLight(0x111111);
    // floor
    var floorList = getArray3dCadByType(1);
    if (floorList.length != 0) {
        floorList = floorList[0];
        var _3dxy = {
            'start_X': converMMtoPX(floorList[2][0] - widthPillar/2),
            'start_Y': converMMtoPX(floorList[2][1] - widthPillar/2),
            'width_X': converMMtoPX(floorList[2][2] + widthPillar + wallWidth),
            'width_Y': converMMtoPX(floorList[2][3] + widthPillar + wallWidth),
            'height_startpoint': converMMtoPX(floorList[2][4]),
            'height_endpoint': converMMtoPX(floorList[2][5]),
        };

        var _3dobject = floorList[3];
        floorInfo = floorList;
        drawFloor3D(scene, _3dxy, _3dobject);
    }

    // bacon
    var baconList = getArray3dCadByType(2);
    if (baconList.length != 0) {
        baconList = baconList[0];
        var _3dxy = {
            'start_X': converMMtoPX(baconList[2][0] - widthPillar/2),
            'start_Y': converMMtoPX(baconList[2][1] - widthPillar/2 ),
            'width_X': converMMtoPX(baconList[2][2] + widthPillar + wallWidth ),
            'width_Y': converMMtoPX(baconList[2][3] + widthPillar + wallWidth ),
            'height_startpoint': converMMtoPX(baconList[2][4]),
            'height_endpoint': converMMtoPX(baconList[2][5]),
        };

        var _3dobject = baconList[3];
        baconInfo = baconList;
        drawBacon(scene, _3dxy, _3dobject);
    }


    // MR 1340 - START
    // handrail
    if( isViewHandrail == true) showHanrail();
    // MR 1340 - END

    // stepper
    var stepperList = getArray3dCadByType(TYPE_STEPPER);
    for (var i = 0; i < stepperList.length; i++) {
        stepper = stepperList[i];
        var _3dxy = {
            'start_X': converMMtoPX(stepper[2][0]),
            'start_Y': converMMtoPX(stepper[2][1]),
            'width_X': converMMtoPX(stepper[2][2]),
            'width_Y': converMMtoPX(stepper[2][3]),
            'height_startpoint': converMMtoPX(-180),
            'height_endpoint': converMMtoPX(stepper[2][5] - 180),
        };

        var _3dobject = stepper[3];
        drawStepper(scene, _3dxy, _3dobject);
    }


    if (!inside) {
        // roof 101
        var roofList = getArray3dCadByType(101);
        if (roofList.length != 0) {
            roofList = roofList[0];
            var _3dxy = {
                'start_X': (roofList[2][0]),
                'start_Y': (roofList[2][1]),
                'width_X': (roofList[2][2]),
                'width_Y': (roofList[2][3]),
                'height_startpoint': converMMtoPX(2110),
                'height_endpoint': converMMtoPX(2110 + 50),
            };
            var _3dobject = roofList[3];
            console.log(baconInfo);
            drawRoof3D(scene, _3dxy, _3dobject, 101);
        }
        // roof 102
        var roofList = getArray3dCadByType(102);
        if (roofList.length != 0) {
            roofList = roofList[0];
            var _3dxy = {
                'start_X': (roofList[2][0]),
                'start_Y': (roofList[2][1]),
                'width_X': (roofList[2][2]),
                'width_Y': (roofList[2][3]),
                'height_startpoint': converMMtoPX(2110),
                'height_endpoint': converMMtoPX(2110 + 50),
            };
            var _3dobject = roofList[3];
            drawRoof3D(scene, _3dxy, _3dobject, 102);
        }
        // roof 103
        var roofList = getArray3dCadByType(103);
        if (roofList.length != 0) {
            roofList = roofList[0];
            var _3dxy = {
                'start_X': (roofList[2][0]),
                'start_Y': (roofList[2][1]),
                'width_X': (roofList[2][2]),
                'width_Y': (roofList[2][3]),
                'height_startpoint': converMMtoPX(2110),
                'height_endpoint': converMMtoPX(2110 + 50),
            };
            var _3dobject = roofList[3];
            drawRoof3D(scene, _3dxy, _3dobject, 103);
        }
        // roof 104
        var roofList = getArray3dCadByType(104);
        if (roofList.length != 0) {
            roofList = roofList[0];
            var _3dxy = {
                'start_X': (roofList[2][0]),
                'start_Y': (roofList[2][1]),
                'width_X': (roofList[2][2]),
                'width_Y': (roofList[2][3]),
                'height_startpoint': converMMtoPX(2110),
                'height_endpoint': converMMtoPX(2110 + 50),
            };
            var _3dobject = roofList[3];
            drawRoof3D(scene, _3dxy, _3dobject, 104);
        }
    }
    var doorList = getArray3dCadByType(5);
    if (doorList) {
        doorList.forEach(function (d) {
            var _3dobject = d[3];
            var _3dxy = {
                'start_X': (d[2][0]),
                'start_Y': (d[2][1]),
                'width_X': (d[2][2]),
                'width_Y': (d[2][3]),
                'height_startpoint': (d[2][4]),
                'height_endpoint': (d[2][5]),
            };
            drawDoor(scene, _3dxy, _3dobject);
        });
    }
    array3dCad.forEach(function (v) {
        var _3dxy = {
            'start_X': converMMtoPX(v[2][0]),
            'start_Y': converMMtoPX(v[2][1]),
            'width_X': converMMtoPX(v[2][2]),
            'width_Y': converMMtoPX(v[2][3]),
            'height_startpoint': converMMtoPX(v[2][4]),
            'height_endpoint': converMMtoPX(v[2][5]),
        };
        var _3dobject = v[3];

        // if (v[1] == 7) {
        //     var _3dxy = {
        //         'start_X': (v[2][0]),
        //         'start_Y': (v[2][1]),
        //         'width_X': (v[2][2]),
        //         'width_Y': (v[2][3]),
        //         'height_startpoint': (v[2][4]),
        //         'height_endpoint': (v[2][5]),
        //     };
        //     drawWindow(scene, _3dxy, _3dobject);
        // }
        if (v[1] == 5) {

        }
        if (v[1] == 3) {
            drawWall(scene, _3dxy, _3dobject);
        }
        if (v[1] == 4) {
            drawPillar(scene, _3dxy, _3dobject);
        }
        if (v[1] == 6) {
            var _3dxy = {
                'start_X': (v[2][0]),
                'start_Y': (v[2][1]),
                'width_X': (v[2][2]),
                'width_Y': (v[2][3]),
                'height_startpoint': (v[2][4]),
                'height_endpoint': (v[2][5]),
            };
            drawWindow(scene, _3dxy, _3dobject);
        }


    });
    var arrWall = [];
    wallObj.forEach(function (val, key) {
        var wallCSG = new ThreeBSP(val);
        windowObj.forEach(function (v, k) {
            var windowCSG = new ThreeBSP(v);
            wallCSG = wallCSG.subtract(windowCSG);
        });

        if (roofSlice) {
            var roofCSG = new ThreeBSP(roofSlice);
            wallCSG = wallCSG.subtract(roofCSG);
        }
        if (roofSlice1) {
            var roofCSG = new ThreeBSP(roofSlice1);
            wallCSG = wallCSG.subtract(roofCSG);
        }
        if (roofSlice2) {
            var roofCSG = new ThreeBSP(roofSlice2);
            wallCSG = wallCSG.subtract(roofCSG);
        }
        doorObj.forEach(function (dr, k) {
            var doorCSG = new ThreeBSP(dr);
            wallCSG = wallCSG.subtract(doorCSG);
        });
        arrWall.push(wallCSG.toMesh());
    });
    arrWall.forEach(function (item, index) {
        var ceilingMaterial = new THREE.MeshBasicMaterial({
            map: wallTexture,
            shading: THREE.FlatShading,
            side: THREE.DoubleSide,
        });
        item.material = ceilingMaterial;
        scene.add(item);
    });

// MR 1347 - START
    var arrWallPaint = [];
    wallPaint.forEach(function (val, key) {
        var wallCSG = new ThreeBSP(val);
        windowObj.forEach(function (v, k) {
            var windowCSG = new ThreeBSP(v);
            wallCSG = wallCSG.subtract(windowCSG);
        });

        if (roofSlice) {
            var roofCSG = new ThreeBSP(roofSlice);
            wallCSG = wallCSG.subtract(roofCSG);
        }
        if (roofSlice1) {
            var roofCSG = new ThreeBSP(roofSlice1);
            wallCSG = wallCSG.subtract(roofCSG);
        }
        if (roofSlice2) {
            var roofCSG = new ThreeBSP(roofSlice2);
            wallCSG = wallCSG.subtract(roofCSG);
        }
        doorObj.forEach(function (dr, k) {
            var doorCSG = new ThreeBSP(dr);
            wallCSG = wallCSG.subtract(doorCSG);
        });
        arrWallPaint.push(wallCSG.toMesh());
    });
    arrWallPaint.forEach(function (item, index) {
        var ceilingMaterial = new THREE.MeshBasicMaterial({
            map: wallPaintTexture,
            shading: THREE.FlatShading,
            side: THREE.DoubleSide,
        });
        item.material = ceilingMaterial;
        scene.add(item);
    });

// MR 1347 - END

    var arrPillar = [];
    pillarObj.forEach(function (v, k) {
        var pillarCSG = new ThreeBSP(v);
        if (roofSlice) {
            var roofCSG = new ThreeBSP(roofSlice);
            pillarCSG = pillarCSG.subtract(roofCSG);
        }
        if (roofSlice1) {
            var roofCSG = new ThreeBSP(roofSlice1);
            pillarCSG = pillarCSG.subtract(roofCSG);
        }
        if (roofSlice2) {
            var roofCSG = new ThreeBSP(roofSlice2);
            pillarCSG = pillarCSG.subtract(roofCSG);
        }
        arrPillar.push(pillarCSG.toMesh());

    });
    arrPillar.forEach(function (pillar) {
        var ceilingMaterial = new THREE.MeshBasicMaterial({
            map: wallTexture,
            shading: THREE.FlatShading,
            side: THREE.DoubleSide,
        });
        pillar.material = ceilingMaterial;
        scene.add(pillar);
    });
    scene.add(baconObj);
    scene.add(floorObj);
    //windowObj.forEach(function (w, k) {
    //    scene.add(w);
    //});
    woodWindowObj.forEach(function (ww, k) {
        scene.add(ww);
    });
    doorObj.forEach(function (dobj) {
        scene.add(dobj);
    });


    // MR 1340 - START
    handrailObj.forEach(function (ww, k) {
        scene.add(ww);
    });


    stepperObj.forEach(function (ww, k) {
        scene.add(ww);
    });

    //var floorTexture = new loader.load('images/3dhouse.jpg');
    //floorTexture.wrapS = floorTexture.wrapT = THREE.RepeatWrapping;
    //floorTexture.repeat.set(100, 100);
    //var floorMaterial = new THREE.MeshBasicMaterial({map: floorTexture, side: THREE.DoubleSide});
    //var floorGeometry = new THREE.PlaneGeometry(10000, 10000, 1, 1);
    //floor = new THREE.Mesh(floorGeometry, floorMaterial);
    //floor.position.y = -0.5;
    //floor.rotation.x = -Math.PI / 2;
    ////
    //if (inside) {
    //    floor.position.y = -converMMtoPX(300 + 300);
    //    floor.position.x = converMMtoPX(floorInfo[2][0]);
    //    floor.position.z = converMMtoPX(floorInfo[2][1]);
    //}


    //
    scene.add(floorObj);


    var imagePrefix = "images/dawnmountain-";
    var directions = ["xpos", "xneg", "ypos", "yneg", "zpos", "zneg"];
    var imageSuffix = ".png";
    var skyGeometry = new THREE.CubeGeometry(6000, 6000, 6000);

    var materialArray = [];
    for (var i = 0; i < 6; i++)
        materialArray.push(new THREE.MeshBasicMaterial({
            map: loader.load(imagePrefix + directions[i] + imageSuffix),
            side: THREE.BackSide
        }));
    var skyMaterial = new THREE.MeshFaceMaterial(materialArray);
    var skyBox = new THREE.Mesh(skyGeometry, skyMaterial);
    skyBox.position.y = 3000 - converMMtoPX(180);
    if (inside) {
        skyBox.position.y = 3000 - converMMtoPX(300 + 300 + 180);
        skyBox.position.x = converMMtoPX(floorInfo[2][0]);
        skyBox.position.z = converMMtoPX(floorInfo[2][1]);
    }
    scene.add(skyBox);

    //RM 1423
    drawBeamUnderFloor(scene);


    //var skyBoxGeometry = new THREE.CubeGeometry(10000, 10000, 10000);
    //
    //var skyTex = new loader.load('images/sky.jpg');
    //skyTex.wrapS = skyTex.wrapT = THREE.RepeatWrapping;
    //skyTex.repeat.set(1, 1);
    //
    //var skyBoxMaterial = new THREE.MeshBasicMaterial({map: skyTex, color: 0xFFFFFF, side: THREE.BackSide});
    //var skyBox = new THREE.Mesh(skyBoxGeometry, skyBoxMaterial);
    //scene.add(skyBox);
    //
    //
    //scene.fog = new THREE.FogExp2(0x9999ff, 0.000025);
}

function isExistStepper(handrail3dxy)
{
    var existFlag = false;
    var stepArr = [];
    // Get step info
    var stepperList = getArray3dCadByType(TYPE_STEPPER);
    if(stepperList.length > 0)
    {
        for(var j = 0; j < stepperList.length; j++) 
        {  
            var stepper = stepperList[j];
            var stepper3dxy =  {
                'start_X': converMMtoPX(stepper[2][0]),
                'start_Y': converMMtoPX(stepper[2][1]),
                'width_X': converMMtoPX(stepper[2][2]),
                'width_Y': converMMtoPX(stepper[2][3]),
                'height_startpoint': converMMtoPX(stepper[2][4]),
                'height_endpoint': converMMtoPX(stepper[2][5]),
            };

            //if both horizontal
            if(stepper3dxy.width_X > stepper3dxy.width_Y && handrail3dxy.width_X > handrail3dxy.width_Y)
            {
                if( handrail3dxy.start_X <= stepper3dxy.start_X + stepper3dxy.width_X/2
                &&  stepper3dxy.start_X + stepper3dxy.width_X/2 <= handrail3dxy.start_X + handrail3dxy.width_X
                && stepper3dxy.start_Y >= handrail3dxy.start_Y )
                {
                    existFlag = true;
                    stepArr.push(stepper3dxy)
                    // return {
                    //     'isExist': true,
                    //     'stepper3dxy': stepper3dxy
                    // };
                }
            }
            else if(stepper3dxy.width_X < stepper3dxy.width_Y && handrail3dxy.width_X < handrail3dxy.width_Y)
            {
                if( handrail3dxy.start_Y <= stepper3dxy.start_Y + stepper3dxy.width_Y/2 
                    && stepper3dxy.start_Y + stepper3dxy.width_Y/2 <= handrail3dxy.start_Y + handrail3dxy.width_Y
                    && Math.abs(handrail3dxy.start_X - stepper3dxy.start_X) <= handrail3dxy.width_X + stepper3dxy.width_X)
                {
                    existFlag = true;
                    stepArr.push(stepper3dxy)
                    // return {
                    //     'isExist': true,
                    //     'stepper3dxy': stepper3dxy
                    // };
                }
            }
        }   
    }


    return {
        'isExist': existFlag,
        'stepArr': stepArr
    };
}
function checkAndDrawHandrail(handrail3dxy, handrail3dObject)
{
    var drawArr = []; //arrary to draw child handrail
    var result = isExistStepper(handrail3dxy);
    // If exist handrail than need to canculate
    if(result.stepArr.length > 0 && result.isExist)
    {
        // If horizontal
        if(handrail3dxy.width_X > handrail3dxy.width_Y)
        {
            drawArr.push(handrail3dxy.start_X);
            // Sort step by xtart_x
            result.stepArr.sort(function(a, b) {
                return a.start_X - b.start_X;
            });
            result.stepArr.forEach(function(item){
                drawArr.push(item.start_X);
                drawArr.push(item.start_X + item.width_X);
            });
            drawArr.push(handrail3dxy.start_X + handrail3dxy.width_X);
            for (var i = 0; i < drawArr.length; i += 2) {
                if(drawArr[i+1] > drawArr[i])
                {
                    //Draw handrail for this 
                    var child3dxy = JSON.parse(JSON.stringify(handrail3dxy));
                    child3dxy.start_X = drawArr[i];
                    child3dxy.width_X = drawArr[i + 1] - drawArr[i];
                    drawHandrail(scene, child3dxy, handrail3dObject); 
                }
            }
        }
        else
        {
            drawArr.push(handrail3dxy.start_Y);
            // Sort step by xtart_x
            result.stepArr.sort(function(a, b) {
                return a.start_Y - b.start_Y;
            });
            result.stepArr.forEach(function(item){
                drawArr.push(item.start_Y);
                drawArr.push(item.start_Y + item.width_Y);
            });
            drawArr.push(handrail3dxy.start_Y + handrail3dxy.width_Y);
            for (var i = 0; i < drawArr.length; i += 2) {
                if(drawArr[i+1] - drawArr[i] > 150)
                {
                    //Draw handrail for this 
                    var child3dxy = JSON.parse(JSON.stringify(handrail3dxy));
                    child3dxy.start_Y = drawArr[i];
                    child3dxy.width_Y = drawArr[i + 1] - drawArr[i];
                    drawHandrail(scene, child3dxy, handrail3dObject); 
                }
            }
        }
    }
    else
    {
        // draw normally
        drawHandrail(scene, handrail3dxy, handrail3dObject);
    }
}


function animate() {
    requestAnimationFrame(animate);
    render();
    update();
}

function update() {
    controls.update();
}
function noRoof() {
    if (roof3D) {
        scene.remove(roof3D);
    }
    if (roof_2roof_obj) {
        scene.remove(roof_2roof_obj);
    }
    if (roof_1roof_obj) {
        scene.remove(roof_1roof_obj);
    }
    document.getElementById('no-roof').style.display = 'none';
    document.getElementById('has-roof').style.display = 'block';

    animate();
}
function hasRoof() {
    if (roof3D) {
        scene.add(roof3D);
    } else {
        if (roof_2roof_obj) {
            scene.add(roof_2roof_obj);
        }
        if (roof_1roof_obj) {
            scene.add(roof_1roof_obj);
        }
    }

    document.getElementById('no-roof').style.display = 'block';
    document.getElementById('has-roof').style.display = 'none';
    animate();
}
function render() {
    //renderer.shadowMapType = THREE.PCFSoftShadowMap;
    //renderer.shadowMapEnabled = true;
    renderer.render(scene, camera);
}
function converMMtoPX(mm) {
    return px = mm * 100 / 455;
}

function converPxtoMm(px) {
    return mm = px * 455 / (  100);
}

var painterWidth = 2;
var wallWidth = 40; // mm
// Draw Wall
function drawWall(scene, _3dxy, _3dobject) {
    if (!wallTexture) {
        wallTexture = new loader.load('images/kabe.jpg');
        wallTexture.wrapT = THREE.RepeatWrapping;
        wallTexture.repeat.set(5, 5);
    }
    if (_3dxy.width_X < _3dxy.width_Y) {
        _3dxy.width_X = converMMtoPX(wallWidth);
    } else {
        _3dxy.width_Y = converMMtoPX(wallWidth);
    }
    _3dxy.height_endpoint = wall3D.height_endpoint + floor3D.height_endpoint;
    _3dxy.height_startpoint = wall3D.height_startpoint + floor3D.height_endpoint;
    var material = {
        'tex': wallTexture,
    };
    isDraw = 'wall';
    var wall = drawObj(scene, _3dxy, _3dobject, material);

    wallObj.push(wall);
    drawWallPaintInside(scene, _3dxy, _3dobject);
}
// MR 1347 - START
// draw wall paint Inside
function drawWallPaintInside(scene, _3dxy, _3dobject) {
    var _paint3dxy = {
        'start_X': _3dxy.start_X,
        'start_Y': _3dxy.start_Y,
        'width_X': _3dxy.width_X,
        'width_Y': _3dxy.width_Y,
        'height_startpoint': _3dxy.height_startpoint,
        'height_endpoint': _3dxy.height_endpoint,
    };
    var pos = getWallPosition(_3dxy);
    switch (pos) {
        case 1:
            // top
            _paint3dxy.start_Y += _3dxy.width_Y;
            _paint3dxy.width_Y = painterWidth;

            break;
        case 2:
            // bottom
            _paint3dxy.start_Y -= painterWidth;
            _paint3dxy.width_Y = painterWidth;
            break;
        case 3:
            // left
            _paint3dxy.start_X += _3dxy.width_X;
            _paint3dxy.width_X = painterWidth;

            break;
        case 4:
            // right
            _paint3dxy.start_X -= painterWidth;
            _paint3dxy.width_X = painterWidth;

            break;
        default:
            console.log("Unknown position: " + pos);
            break;
    }

    var material = {
        'tex': wallPaintTexture,
    };
    isDraw = 'wall';
    var wall = drawObj(scene, _paint3dxy, _3dobject, material);

    wallPaint.push(wall);

}


// get wall position: return: 1: top 	2: bôtm		3: left		4: right
function getWallPosition(_3dxy) {
    var floorList = getArray3dCadByType(TYPE_FLOOR);
    if (floorList.length != 0) {
        var floor = floorList[0];
        if (_3dxy.width_X > _3dxy.width_Y) {
            if (_3dxy.start_Y <= converMMtoPX(floor[2][1]) + _3dxy.width_Y) {
                // top
                return 1;
            }
            else {
                //bottom
                return 2;
            }
        }
        else {
            if (_3dxy.start_X <= converMMtoPX(floor[2][0]) + _3dxy.width_X) {
                // left
                return 3;
            }
            else {
                // right
                return 4;
            }

        }
    }

}

// MR 1347 - END

//Draw Pillar
function drawPillar(scene, _3dxy, _3dobject) {
    _3dxy.height_endpoint = column3D.height_endpoint + floor3D.height_endpoint;
    _3dxy.height_startpoint = floor3D.height_endpoint;
    
    drawPillarWithPaintInside(scene, _3dxy, _3dobject) 
        
}


// MR 1508 - START
// draw pillar paint outside
function drawPillarWithPaintInside(scene, _3dxy, _3dobject) {
    var _paint3dxy = {
        'start_X': _3dxy.start_X,
        'start_Y': _3dxy.start_Y,
        'width_X': _3dxy.width_X,
        'width_Y': _3dxy.width_Y,
        'height_startpoint': _3dxy.height_startpoint,
        'height_endpoint': _3dxy.height_endpoint,
    };
    if(checkPillarInsideFloor(_3dxy) == true)
    {
	    if(checkPillarInLeftOrRightWall(_3dxy) == 1)
	    {
	        // left
			if( checkPillarInAngleWall(_3dxy) == false)
			{
		        _paint3dxy.start_X = _3dxy.start_X + _3dxy.width_X;
		        _paint3dxy.width_X = painterWidth;
		        _paint3dxy.start_Y = _3dxy.start_Y - painterWidth/2;
		        _paint3dxy.width_Y = _3dxy.width_Y + painterWidth;
		        
			    painter(_paint3dxy, _3dobject);
		    }
	        
	    }
	    else if(checkPillarInLeftOrRightWall(_3dxy) == 2)
	    {
	        // right
			_3dxy.start_X = _3dxy.start_X + converMMtoPX(40); /* adjust pos by wall width*/
	        
			if( checkPillarInAngleWall(_3dxy) == false)
			{
		        _paint3dxy.start_X = _3dxy.start_X - painterWidth;
		        _paint3dxy.width_X = painterWidth;
		        _paint3dxy.start_Y = _3dxy.start_Y - painterWidth/2;
		        _paint3dxy.width_Y = _3dxy.width_Y + painterWidth;
		        
			    painter(_paint3dxy, _3dobject);
		    }
	    
	    }
	    
	    // check top Bottom
	    
	    if(checkPillarInTopOrBottomWall(_3dxy) == 1)
	    {
	        // top
			if( checkPillarInAngleWall(_3dxy) == false)
			{
		        _paint3dxy.start_X = _3dxy.start_X - painterWidth/2;
		        _paint3dxy.width_X = _3dxy.width_X + painterWidth;
		        _paint3dxy.start_Y = _3dxy.start_Y + _3dxy.width_Y;
		        _paint3dxy.width_Y = painterWidth;
		        
			    painter(_paint3dxy, _3dobject);
		    }
	        
	    }
	    else if(checkPillarInTopOrBottomWall(_3dxy) == 2)
	    {
		    
	        // bottom
			_3dxy.start_Y = _3dxy.start_Y + converMMtoPX(40); /* adjust pos by wall width*/
			
			if( checkPillarInAngleWall(_3dxy) == false)
			{
		        _paint3dxy.start_X = _3dxy.start_X - painterWidth/2;
		        _paint3dxy.width_X = _3dxy.width_X + painterWidth;
		        _paint3dxy.start_Y = _3dxy.start_Y -painterWidth;
		        _paint3dxy.width_Y = painterWidth;
		        
			    painter(_paint3dxy, _3dobject);
			}
	    }
    }
    var material = {};
    var pillar = drawObj(scene, _3dxy, _3dobject, material);
    pillarObj.push(pillar);
    
}

// Check Pillar on angle if floor in side floor
function checkPillarInAngleWall(_3dxy) {
    var floorList = getArray3dCadByType(TYPE_FLOOR);
    if (floorList.length != 0) {
        var floor = floorList[0];
        if((_3dxy.start_Y <= converMMtoPX(floor[2][1])  &&_3dxy.start_X <= converMMtoPX(floor[2][0]) ) || 
           (_3dxy.start_Y + _3dxy.width_Y >= converMMtoPX(floor[2][1] + floor[2][3] )  &&_3dxy.start_X <= converMMtoPX(floor[2][0]) ) || 
           (_3dxy.start_Y <= converMMtoPX(floor[2][1])  &&_3dxy.start_X + _3dxy.width_X >= converMMtoPX(floor[2][0] + floor[2][2] ) ) || 
           (_3dxy.start_Y + _3dxy.width_Y >= converMMtoPX(floor[2][1] + floor[2][3] )   &&_3dxy.start_X + _3dxy.width_X >= converMMtoPX(floor[2][0] + floor[2][2] ) ) )
        {
        	return true;
        }
    }
    return false;
}


// get pillar position: return: 1: left walll; 1: right: 0: midle
function checkPillarInLeftOrRightWall(_3dxy) {
    var floorList = getArray3dCadByType(TYPE_FLOOR);
    if (floorList.length != 0) {
        var floor = floorList[0];
        if(_3dxy.start_X <= converMMtoPX(floor[2][0]) && _3dxy.start_X + _3dxy.width_X >= converMMtoPX(floor[2][0]) )
        {
        	return 1;
        }
        if(_3dxy.start_X <= converMMtoPX(floor[2][0] +floor[2][2] ) && _3dxy.start_X + _3dxy.width_X >= converMMtoPX(floor[2][0] + floor[2][2]) )
        {
        	return 2;
        }
    	return 0;
    }

}
// get pillar position: return: 1: top walll; 2: bottom: 0: midle
function checkPillarInTopOrBottomWall(_3dxy) {
    var floorList = getArray3dCadByType(TYPE_FLOOR);
    if (floorList.length != 0) {
        var floor = floorList[0];
        if(_3dxy.start_Y <= converMMtoPX(floor[2][1]) && _3dxy.start_Y + _3dxy.width_Y >= converMMtoPX(floor[2][1]) )
        {
        	return 1;
        }
        if(_3dxy.start_Y <= converMMtoPX(floor[2][1]+floor[2][3]) && _3dxy.start_Y + _3dxy.width_Y >= converMMtoPX(floor[2][1]+floor[2][3]) )
        {
        	return 2;
        }
        return 0;
    }
}

// check pillar not in floor: true: in/ false: out
function checkPillarInsideFloor(_3dxy) {
    var floorList = getArray3dCadByType(TYPE_FLOOR);
    if (floorList.length != 0) {
        var floor = floorList[0];
        if(_3dxy.start_Y <= converMMtoPX(floor[2][1]+floor[2][3]) && _3dxy.start_Y + _3dxy.width_Y >= converMMtoPX(floor[2][1]) && 
        	_3dxy.start_X <= converMMtoPX(floor[2][0] +floor[2][2] ) && _3dxy.start_X + _3dxy.width_X >= converMMtoPX(floor[2][0]) )
        {
        	return true;
        }
        return false;
    }
}

function painter( _3dxy, _3dobject)
{
    var _3dPainterXY = {
        'start_X': _3dxy.start_X,
        'start_Y': _3dxy.start_Y,
        'width_X': _3dxy.width_X,
        'width_Y': _3dxy.width_Y,
        'height_startpoint': _3dxy.height_startpoint,
        'height_endpoint': _3dxy.height_endpoint,
    };
    var material = {
        'tex': wallPaintTexture,
    };
    var paint = drawObj(scene, _3dPainterXY, _3dobject, material);

    wallPaint.push(paint);
}

//Draw bacon
function drawBacon(scene, _3dxy, _3dobject) {
    _3dxy.height_endpoint = bacon3D.height_endpoint;
    _3dxy.height_startpoint = bacon3D.height_startpoint;
    
    var material = {
        'tex': wallTexture
    };
    var bacon = drawObj(scene, _3dxy, _3dobject, material);
    baconObj.add(bacon);
}

//Draw handrail3D -- MR 1340 - START
function drawHandrail(scene, _3dxy, _3dobject) {
    _3dxy.height_endpoint = _3dxy.height_endpoint + bacon3D.height_endpoint;
    _3dxy.height_startpoint = _3dxy.height_startpoint + bacon3D.height_endpoint;
    var balconyList = getArray3dCadByType(TYPE_BALCONY);
    if (balconyList.length != 0) {
        balcony = balconyList[0];
        balcony_start_X = converMMtoPX(balcony[2][0]);
        balcony_start_Y = converMMtoPX(balcony[2][1]);
        balcony_width_X = converMMtoPX(balcony[2][2]);
        balcony_width_Y = converMMtoPX(balcony[2][3]); 
        
    }
    //var materialWoodW = {};
    var material = {
        'tex': wallTexture,
    };
    // main handrail
    var mainHanrail = JSON.parse(JSON.stringify(_3dxy));
    mainHanrail.height_endpoint = converMMtoPX(800);
    mainHanrail.height_startpoint = converMMtoPX(700);
    var woodHandrail = drawObj(scene, mainHanrail, _3dobject, material);
    handrailObj.push(woodHandrail);

    var subHanrail1 = JSON.parse(JSON.stringify(_3dxy));
    subHanrail1.height_endpoint = converMMtoPX(650);
    subHanrail1.height_startpoint = converMMtoPX(550);
    var woodSubHanrail1 = drawObj(scene, subHanrail1, _3dobject, material);
    handrailObj.push(woodSubHanrail1);

    var subHanrail2 = JSON.parse(JSON.stringify(_3dxy));
    subHanrail2.height_endpoint = converMMtoPX(250);
    subHanrail2.height_startpoint = converMMtoPX(150);
    var woodSubHanrail2 = drawObj(scene, subHanrail2, _3dobject, material);
    handrailObj.push(woodSubHanrail2);
    // Horizontal
    if (mainHanrail.width_X > mainHanrail.width_Y) {
        //If start of bacony than dont need to draw big Vhanrail
        // draw big subVhanrail (width =100mm) at begin and end of subVhanrail
        if(Math.abs((mainHanrail.start_X - balcony_start_X)) > converMMtoPX(50))
        {
            var bigVHanrail = JSON.parse(JSON.stringify(_3dxy)); 
            bigVHanrail.height_endpoint = converMMtoPX(1000);
            bigVHanrail.height_startpoint = converMMtoPX(0);
            bigVHanrail.start_X = mainHanrail.start_X;
            bigVHanrail.width_X = converMMtoPX(100);

            var woodbigVHanrail = drawObj(scene, bigVHanrail, _3dobject, material);
            handrailObj.push(woodbigVHanrail);
        }
        // Draw at end
        // if end of handrail is end of balcony than dont need to draw end handrail
        if( Math.abs(((mainHanrail.start_X + mainHanrail.width_X) - (balcony_start_X + balcony_width_X))) > converMMtoPX(50))
        {
            var bigVHanrailEnd = JSON.parse(JSON.stringify(_3dxy)); 
            bigVHanrailEnd.height_endpoint = converMMtoPX(1000);
            bigVHanrailEnd.height_startpoint = converMMtoPX(0);
            bigVHanrailEnd.start_X = mainHanrail.start_X + mainHanrail.width_X - converMMtoPX(100);
            bigVHanrailEnd.width_X = converMMtoPX(100);

            var woodbigVHanrailEnd = drawObj(scene, bigVHanrailEnd, _3dobject, material);
            handrailObj.push(woodbigVHanrailEnd);
        }
        //Update mainHanrail widthX
        mainHanrail.start_X = mainHanrail.start_X + converMMtoPX(50);
        mainHanrail.width_X = mainHanrail.width_X - converMMtoPX(200);

        for (var k = 1; k < mainHanrail.width_X / converMMtoPX(150); k++) {
            var subVHanrail = JSON.parse(JSON.stringify(_3dxy));
            subVHanrail.height_endpoint = converMMtoPX(250);
            subVHanrail.height_startpoint = converMMtoPX(550);
            subVHanrail.start_X = mainHanrail.start_X + converMMtoPX(k * 150 - 25);
            //subVHanrail.start_Y = mainHanrail.start_Y + converMMtoPX( k * 150 - 25 );

            subVHanrail.width_X = converMMtoPX(50);
            //subVHanrail.width_Y = converMMtoPX( 50 );

            var woodSubVHanrail = drawObj(scene, subVHanrail, _3dobject, material);
            handrailObj.push(woodSubVHanrail);

        }
        
    }
    else {
        //If start of bacony than dont need to draw big Vhanrail
        //draw big subVhanrail (width =100mm) at begin and end of subVhanrail
        if(Math.abs((mainHanrail.start_Y - balcony_start_Y)) > converMMtoPX(50))
        {
            var bigVHanrail = JSON.parse(JSON.stringify(_3dxy)); 
            bigVHanrail.height_endpoint = converMMtoPX(1000);
            bigVHanrail.height_startpoint = converMMtoPX(0);
            bigVHanrail.start_Y = mainHanrail.start_Y;
            bigVHanrail.width_Y = converMMtoPX(100);

            var woodbigVHanrail = drawObj(scene, bigVHanrail, _3dobject, material);
            handrailObj.push(woodbigVHanrail);
        }
        // Draw at end
        // if end of handrail is end of balcony than dont need to draw end handrail
        if( Math.abs(((mainHanrail.start_Y + mainHanrail.width_Y) - (balcony_start_Y + balcony_width_Y))) > converMMtoPX(50))
        {
            var bigVHanrailEnd = JSON.parse(JSON.stringify(_3dxy)); 
            bigVHanrailEnd.height_endpoint = converMMtoPX(1000);
            bigVHanrailEnd.height_startpoint = converMMtoPX(0);
            bigVHanrailEnd.start_Y = mainHanrail.start_Y + mainHanrail.width_Y - converMMtoPX(100);
            bigVHanrailEnd.width_Y = converMMtoPX(100);

            var woodbigVHanrailEnd = drawObj(scene, bigVHanrailEnd, _3dobject, material);
            handrailObj.push(woodbigVHanrailEnd);
        }
        //Update mainHanrail widthX
        mainHanrail.start_Y = mainHanrail.start_Y + converMMtoPX(50);
        mainHanrail.width_Y = mainHanrail.width_Y - converMMtoPX(200);



        for (var k = 1; k < mainHanrail.width_Y / converMMtoPX(150); k++) {
            var subVHanrail = JSON.parse(JSON.stringify(_3dxy));
            subVHanrail.height_endpoint = converMMtoPX(250);
            subVHanrail.height_startpoint = converMMtoPX(550);
            //subVHanrail.start_X = mainHanrail.start_X +  converMMtoPX( k * 150 - 25 );
            subVHanrail.start_Y = mainHanrail.start_Y + converMMtoPX(k * 150 - 25);

            //subVHanrail.width_X = converMMtoPX( 50 );
            subVHanrail.width_Y = converMMtoPX(50);

            var woodSubVHanrail = drawObj(scene, subVHanrail, _3dobject, material);
            handrailObj.push(woodSubVHanrail);

        }

    }

}

// MR 1340 - END
//Draw stepper
function drawStepper(scene, _3dxy, _3dobject) {
    
    var material = {
        'tex': wallTexture,
    };
    
    if(_3dxy.width_X > _3dxy.width_Y)
    {
        var ALIGN_LONG = (converMMtoPX(1200) - _3dxy.width_X)/2;
    }
    else
    {
        var ALIGN_LONG = (converMMtoPX(1200) - _3dxy.width_Y)/2;
    }
    
    BODY_H_LONG = converMMtoPX(1200);
    BODY_V_LONG = converMMtoPX(165);
    BODY_HEIGHT = converMMtoPX(50);
    BEGIN_STEPPER_TO_LEG = converMMtoPX(100);
    //LEG_H_LONG = BODY_V_LONG;
    //LEG_V_LONG = BODY_V_LONG;
    LEG_HEIGHT_START = converMMtoPX(-180);
    LEG_HEIGHT_END = converMMtoPX(-100);
    
    var floor_start_X;
    var floor_start_Y;
    
    var floorList = getArray3dCadByType(TYPE_FLOOR);
    if (floorList.length != 0) {
        floor = floorList[0];
        floor_start_X = converMMtoPX(floor[2][0]);
        floor_start_Y = converMMtoPX(floor[2][1]);
        floor_width_X = converMMtoPX(floor[2][2]);
        floor_width_Y = converMMtoPX(floor[2][3]);
    }

    var balconyList = getArray3dCadByType(TYPE_BALCONY);
    if (balconyList.length != 0) {
        balcony = balconyList[0];
        balcony_start_X = converMMtoPX(balcony[2][0]);
        balcony_start_Y = converMMtoPX(balcony[2][1]);
        balcony_width_X = converMMtoPX(balcony[2][2]);
        balcony_width_Y = converMMtoPX(balcony[2][3]);
    }
    // Draw body
    var _body3dxy = JSON.parse(JSON.stringify(_3dxy));
    if(_body3dxy.width_X > _body3dxy.width_Y)
    {
        BODY_V_LONG = BODY_H_LONG * _body3dxy.width_Y/_body3dxy.width_X;
    }
    else
    {
        BODY_V_LONG = BODY_H_LONG * _body3dxy.width_X/_body3dxy.width_Y;
    }
    
    // If horizontal step
    if(_body3dxy.width_X > _body3dxy.width_Y)
    {
        _body3dxy.start_X =_body3dxy.start_X - ALIGN_LONG;
        // Check if steper is top of floor
        if(_body3dxy.start_Y < floor_start_Y)
        {
            _body3dxy.start_Y = _body3dxy.start_Y + _body3dxy.width_Y/2 - BODY_V_LONG - converMMtoPX(50);
        }
        else
        {
            _body3dxy.start_Y = _body3dxy.start_Y - _body3dxy.width_Y/2 + converMMtoPX(50);
        }
        
        _body3dxy.width_X = BODY_H_LONG;
        _body3dxy.width_Y = BODY_V_LONG;
        
    }
    else
    {
        //if step is left of balcony or floor
        if(_body3dxy.start_X < floor_start_X)
        {
            _body3dxy.start_X = _body3dxy.start_X + _body3dxy.width_X/2 - BODY_V_LONG - converMMtoPX(50);
        }
        else
        {
            _body3dxy.start_X = _body3dxy.start_X - _body3dxy.width_X/2 + converMMtoPX(50);
        }

        _body3dxy.start_Y = _body3dxy.start_Y - ALIGN_LONG;
        
        _body3dxy.width_X = BODY_V_LONG;
        _body3dxy.width_Y = BODY_H_LONG;
    }

    _body3dxy.height_startpoint = LEG_HEIGHT_END;
    _body3dxy.height_endpoint = LEG_HEIGHT_END + BODY_HEIGHT;
    var body = drawObj(scene, _body3dxy, _3dobject, material);
    stepperObj.push(body)


    // Draw left leg
    var _left3dxy = JSON.parse(JSON.stringify(_3dxy));

    // If horizontal stepper
    if (_left3dxy.width_X > _left3dxy.width_Y) {
        _left3dxy.start_X = _body3dxy.start_X + BEGIN_STEPPER_TO_LEG;
        _left3dxy.start_Y = _body3dxy.start_Y
        _left3dxy.width_X = BODY_V_LONG;
        _left3dxy.width_Y = BODY_V_LONG;
        
    } else {
        _left3dxy.start_X = _body3dxy.start_X;
        _left3dxy.start_Y = _body3dxy.start_Y + BEGIN_STEPPER_TO_LEG;
        _left3dxy.width_X = BODY_V_LONG;
        _left3dxy.width_Y = BODY_V_LONG;
    }

    _left3dxy.height_startpoint = LEG_HEIGHT_START;
    _left3dxy.height_endpoint = LEG_HEIGHT_END;
    var leftLeg = drawObj(scene, _left3dxy, _3dobject, material);
    stepperObj.push(leftLeg)


    //Draw right leg
    var _right3dxy = JSON.parse(JSON.stringify(_3dxy));
    // If horizontal stepper
    if (_right3dxy.width_X > _right3dxy.width_Y) {
        _right3dxy.start_X = _body3dxy.start_X + (BODY_H_LONG - BEGIN_STEPPER_TO_LEG - BODY_V_LONG);
        _right3dxy.start_Y = _body3dxy.start_Y;
        _right3dxy.width_X = BODY_V_LONG;
        _right3dxy.width_Y = BODY_V_LONG;
    } else {
        _right3dxy.start_X = _body3dxy.start_X;
        _right3dxy.start_Y = _body3dxy.start_Y + (BODY_H_LONG - BEGIN_STEPPER_TO_LEG - BODY_V_LONG);
        _right3dxy.width_X = BODY_V_LONG;
        _right3dxy.width_Y = BODY_V_LONG;
    }
    _right3dxy.height_startpoint = LEG_HEIGHT_START;
    _right3dxy.height_endpoint = LEG_HEIGHT_END;
    var rightLeg = drawObj(scene, _right3dxy, _3dobject, material);
    stepperObj.push(rightLeg);

}

// Draw Floor 3D
function drawFloor3D(scene, _3dxy, _3dobject) {
    _3dxy.height_endpoint = floor3D.height_endpoint;
    _3dxy.height_startpoint = floor3D.height_startpoint;
    
    
    var cubeMaterialArray = [];

    cubeMaterialArray.push(new THREE.MeshBasicMaterial({
        map: wallTexture,
        side: THREE.DoubleSide
    }));
    cubeMaterialArray.push(new THREE.MeshBasicMaterial({
        map: wallTexture,
        side: THREE.DoubleSide
    }));
    cubeMaterialArray.push(new THREE.MeshBasicMaterial({
        map: defaultTextute,
        side: THREE.DoubleSide
    }));
    cubeMaterialArray.push(new THREE.MeshBasicMaterial({
        map: wallTexture,
        side: THREE.DoubleSide
    }));

    cubeMaterialArray.push(new THREE.MeshBasicMaterial({
        map: wallTexture,
        side: THREE.DoubleSide
    }));
    
    cubeMaterialArray.push(new THREE.MeshBasicMaterial({
        map: wallTexture,
        side: THREE.DoubleSide
    }));
    
    var floor = drawObjDiffTexture(scene, _3dxy, _3dobject, cubeMaterialArray);
    floorObj.add(floor);
}
// Draw Door
function drawDoor(scene, _3dxy, _3dobject) {
    _3dxy.height_endpoint = door3D.height_endpoint + floor3D.height_endpoint;
    _3dxy.height_startpoint = door3D.height_startpoint + floor3D.height_endpoint;
    var sizePillar = 91 / 2;
    if (_3dxy.width_X > _3dxy.width_Y) {
        _3dxy.start_X = _3dxy.start_X;// + converMMtoPX(sizePillar);
        _3dxy.width_X = _3dxy.width_X - converMMtoPX(sizePillar * 2);
        _3dxy.start_Y = _3dxy.start_Y - converMMtoPX(10);
    } else {
        _3dxy.start_Y = _3dxy.start_Y;// + converMMtoPX(sizePillar);
        _3dxy.width_Y = _3dxy.width_Y - converMMtoPX(sizePillar * 2);
        _3dxy.start_X = _3dxy.start_X - converMMtoPX(10);
    }
    // if (_3dobject[0] == '1wingDoor') {
    //     var material = {
    //         'tex': doorTexute,
    //         'transparent': false,
    //         'opacity': 0.1
    //     };
    // }
    // if (_3dobject[0] == '2wingDoor') {
    //     var material = {
    //         'tex': door2wTexute,
    //         'transparent': false,
    //         'opacity': 0.1
    //     };
    // }
    if (!windowTexture) {
        windowTexture = new loader.load('images/windowTex.jpg');
        windowTexture.wrapS = windowTexture.wrapT = THREE.RepeatWrapping;
        windowTexture.repeat.set(1, 1);
    }
    if (!wallTexture) {
        wallTexture = new loader.load('images/kabe.jpg');
        wallTexture.wrapT = THREE.RepeatWrapping;
        wallTexture.repeat.set(5, 5);
    }
    var materialBorder = {};
    var borderDoor = JSON.parse(JSON.stringify(_3dxy));
    var bDoor = drawObj(scene, borderDoor, _3dobject, materialBorder);
    var doorW = JSON.parse(JSON.stringify(_3dxy));
    if (doorW.width_X < doorW.width_Y) {
        doorW.start_Y = doorW.start_Y + converMMtoPX(30);
        doorW.width_Y = doorW.width_Y - converMMtoPX(60);

    } else {
        doorW.start_X = doorW.start_X + converMMtoPX(30);
        doorW.width_X = doorW.width_X - converMMtoPX(60);
    }
    doorW.height_startpoint = doorW.height_startpoint + converMMtoPX(30);
    doorW.height_endpoint = doorW.height_endpoint - converMMtoPX(30);

    var materialDoor = {};
    var door = drawObj(scene, doorW, _3dobject, materialDoor);
    //vẽ viền cửa
    var materialDoorB = {
        'tex': windowTexture,
        'transparent': false,
        'opacity': 0.1

    };
    var wBorder = 140;
    var hBoder = 20;
    var doorBLeft = JSON.parse(JSON.stringify(doorW));
    var doorBRight = JSON.parse(JSON.stringify(doorW));
    var doorBTop = JSON.parse(JSON.stringify(doorW));
    var doorBBottom = JSON.parse(JSON.stringify(doorW));
    var doorBMid = JSON.parse(JSON.stringify(doorW));
    if (doorW.width_X > doorW.width_Y) {
        //border left
        doorBLeft.start_Y = doorBLeft.start_Y + doorBLeft.width_Y;
        doorBLeft.width_X = converMMtoPX(wBorder);
        doorBLeft.width_Y = converMMtoPX(hBoder);
        //border Right
        doorBRight.start_X = doorBRight.start_X + doorBRight.width_X - converMMtoPX(140);
        doorBRight.start_Y = doorBRight.start_Y + doorBRight.width_Y;
        doorBRight.width_X = converMMtoPX(wBorder);
        doorBRight.width_Y = converMMtoPX(hBoder);
        //border Top
        doorBTop.start_X = doorBTop.start_X + converMMtoPX(wBorder);
        doorBTop.start_Y = doorBTop.start_Y + doorBTop.width_Y;
        doorBTop.width_X = doorBTop.width_X - converMMtoPX(wBorder) * 2;
        doorBTop.width_Y = converMMtoPX(hBoder);
        doorBTop.height_startpoint = doorBTop.height_endpoint - converMMtoPX(wBorder);
        //border Bottom
        doorBBottom.start_X = doorBBottom.start_X + converMMtoPX(wBorder);
        doorBBottom.start_Y = doorBBottom.start_Y + doorBBottom.width_Y;
        doorBBottom.width_X = doorBBottom.width_X - converMMtoPX(wBorder) * 2;
        doorBBottom.width_Y = converMMtoPX(hBoder);
        doorBBottom.height_endpoint = doorBBottom.height_startpoint + converMMtoPX(wBorder);
        //border Mid
        doorBMid.start_X = doorBMid.start_X + converMMtoPX(wBorder);
        doorBMid.start_Y = doorBMid.start_Y + doorBMid.width_Y;
        doorBMid.width_X = doorBMid.width_X - converMMtoPX(wBorder) * 2;
        doorBMid.width_Y = converMMtoPX(hBoder);
        doorBMid.height_startpoint = (doorW.height_endpoint - doorW.height_startpoint - converMMtoPX(wBorder)) / 2 + floor3D.height_endpoint;
        doorBMid.height_endpoint = (doorW.height_endpoint - doorW.height_startpoint + converMMtoPX(wBorder)) / 2 + floor3D.height_endpoint;
    } else {
        //border left
        doorBLeft.start_X = doorBLeft.start_X + doorBLeft.width_X;
        doorBLeft.width_Y = converMMtoPX(wBorder);
        doorBLeft.width_X = converMMtoPX(hBoder);
        //border Right
        doorBRight.start_Y = doorBRight.start_Y + doorBRight.width_Y - converMMtoPX(wBorder);
        doorBRight.start_X = doorBRight.start_X + doorBRight.width_X;
        doorBRight.width_Y = converMMtoPX(wBorder);
        doorBRight.width_X = converMMtoPX(hBoder);
        //border Top
        doorBTop.start_Y = doorBTop.start_Y + converMMtoPX(wBorder);
        doorBTop.start_X = doorBTop.start_X + doorBTop.width_X;
        doorBTop.width_Y = doorBTop.width_Y - converMMtoPX(wBorder) * 2;
        doorBTop.width_X = converMMtoPX(hBoder);
        doorBTop.height_startpoint = doorBTop.height_endpoint - converMMtoPX(wBorder);
        //border Bottom
        doorBBottom.start_Y = doorBBottom.start_Y + converMMtoPX(wBorder);
        doorBBottom.start_X = doorBBottom.start_X + doorBBottom.width_X;
        doorBBottom.width_Y = doorBBottom.width_Y - converMMtoPX(wBorder) * 2;
        doorBBottom.width_X = converMMtoPX(hBoder);
        doorBBottom.height_endpoint = doorBBottom.height_startpoint + converMMtoPX(wBorder);
        //border Mid
        doorBMid.start_Y = doorBMid.start_Y + converMMtoPX(wBorder);
        doorBMid.start_X = doorBMid.start_X + doorBMid.width_X;
        doorBMid.width_Y = doorBMid.width_Y - converMMtoPX(wBorder) * 2;
        doorBMid.width_X = converMMtoPX(hBoder);
        doorBMid.height_startpoint = (doorW.height_endpoint - doorW.height_startpoint - converMMtoPX(wBorder)) / 2 + floor3D.height_endpoint;
        doorBMid.height_endpoint = (doorW.height_endpoint - doorW.height_startpoint + converMMtoPX(wBorder)) / 2 + floor3D.height_endpoint;

    }
    var doorBLeft3D = drawObj(scene, doorBLeft, _3dobject, materialDoorB);
    var doorBRight3D = drawObj(scene, doorBRight, _3dobject, materialDoorB);
    var doorBTop3D = drawObj(scene, doorBTop, _3dobject, materialDoorB);
    var doorBBottom3D = drawObj(scene, doorBBottom, _3dobject, materialDoorB);
    var doorBMid3D = drawObj(scene, doorBMid, _3dobject, materialDoorB);


    var doorBLeft_1 = JSON.parse(JSON.stringify(doorBLeft));
    var doorBRight_1 = JSON.parse(JSON.stringify(doorBRight));
    var doorBTop_1 = JSON.parse(JSON.stringify(doorBTop));
    var doorBBottom_1 = JSON.parse(JSON.stringify(doorBBottom));
    var doorBMid_1 = JSON.parse(JSON.stringify(doorBMid));
    if (doorW.width_X > doorW.width_Y) {
        doorBLeft_1.start_Y = doorBLeft_1.start_Y - doorW.width_Y - doorBLeft_1.width_Y;
        doorBRight_1.start_Y = doorBRight_1.start_Y - doorW.width_Y - doorBRight_1.width_Y;
        doorBTop_1.start_Y = doorBTop_1.start_Y - doorW.width_Y - doorBTop_1.width_Y;
        doorBBottom_1.start_Y = doorBBottom_1.start_Y - doorW.width_Y - doorBBottom_1.width_Y;
        doorBMid_1.start_Y = doorBMid_1.start_Y - doorW.width_Y - doorBMid_1.width_Y;
    } else {
        doorBLeft_1.start_X = doorBLeft_1.start_X - doorW.width_X - doorBLeft_1.width_X;
        doorBRight_1.start_X = doorBRight_1.start_X - doorW.width_X - doorBRight_1.width_X;
        doorBTop_1.start_X = doorBTop_1.start_X - doorW.width_X - doorBTop_1.width_X;
        doorBBottom_1.start_X = doorBBottom_1.start_X - doorW.width_X - doorBBottom_1.width_X;
        doorBMid_1.start_X = doorBMid_1.start_X - doorW.width_X - doorBMid_1.width_X;
    }
    var doorBLeft3D_1 = drawObj(scene, doorBLeft_1, _3dobject, materialDoorB);
    var doorBRight3D_1 = drawObj(scene, doorBRight_1, _3dobject, materialDoorB);
    var doorBTop3D_1 = drawObj(scene, doorBTop_1, _3dobject, materialDoorB);
    var doorBBottom3D_1 = drawObj(scene, doorBBottom_1, _3dobject, materialDoorB);
    var doorBMid3D_1 = drawObj(scene, doorBMid_1, _3dobject, materialDoorB);


    scene.add(doorBLeft3D);
    scene.add(doorBRight3D);
    scene.add(doorBTop3D);
    scene.add(doorBBottom3D);
    scene.add(doorBMid3D);

    scene.add(doorBLeft3D_1);
    scene.add(doorBRight3D_1);
    scene.add(doorBTop3D_1);
    scene.add(doorBBottom3D_1);
    scene.add(doorBMid3D_1);
    //glass Door
    var borderGlass = 50;
    var gDoor_3dxy = JSON.parse(JSON.stringify(doorW));
    if (gDoor_3dxy.width_X < gDoor_3dxy.width_Y) {
        gDoor_3dxy.start_Y = doorBLeft.start_Y + doorBLeft.width_Y + converMMtoPX(borderGlass);
        gDoor_3dxy.width_Y = doorBRight.start_Y - (doorBLeft.start_Y + doorBLeft.width_Y) - converMMtoPX(borderGlass) * 2;
    } else {
        gDoor_3dxy.start_X = doorBLeft.start_X + doorBLeft.width_X + converMMtoPX(borderGlass);
        gDoor_3dxy.width_X = doorBRight.start_X - (doorBLeft.start_X + doorBLeft.width_X) - converMMtoPX(borderGlass) * 2;
    }
    gDoor_3dxy.height_endpoint = doorBTop.height_startpoint - converMMtoPX(borderGlass);
    gDoor_3dxy.height_startpoint = doorBMid.height_endpoint + converMMtoPX(borderGlass);
    var materialGlass_door = {
        'tex': glassTexture,
        'transparent': true,
        'opacity': 0.5
    };
    var glassDoor = drawObj(scene, gDoor_3dxy, _3dobject, materialGlass_door);
    scene.add(glassDoor);
    //
    var doorBSP = new ThreeBSP(door);
    var boderBSP = new ThreeBSP(bDoor);
    var glassBSP = new ThreeBSP(glassDoor);
    doorBSP = doorBSP.subtract(glassBSP);
    boderBSP = boderBSP.subtract(glassBSP);
    boderBSP = boderBSP.subtract(doorBSP);

    bDoor = boderBSP.toMesh();
    var bTex = new THREE.MeshBasicMaterial({
        map: windowTexture,
        shading: THREE.FlatShading,
        side: THREE.DoubleSide,
    });

    bDoor.material = bTex;
    var doorToMesh = doorBSP.toMesh();
    var dTex = new THREE.MeshBasicMaterial({
        map: windowTexture,
        shading: THREE.FlatShading,
        side: THREE.DoubleSide,
    });
    doorToMesh.material = dTex;
    doorObj.push(bDoor);
    doorObj.push(doorToMesh);
    doorObj.push(glassDoor);
    if (!sphereTex) {
        sphereTex = new loader.load('images/sphere.png');
        sphereTex.wrapS = sphereTex.wrapT = THREE.RepeatWrapping;
        sphereTex.repeat.set(1, 1);
    }
    if (_3dobject[0] == '1wingDoorLeft') {
        var sphereGeometry = new THREE.SphereGeometry(converMMtoPX(50), 32, 32);
        var sphereMaterial = new THREE.MeshBasicMaterial({
            map: sphereTex,
            shading: THREE.FlatShading,
            side: THREE.DoubleSide,
        });
        var sphere1 = new THREE.Mesh(sphereGeometry, sphereMaterial);
        var sphere2 = new THREE.Mesh(sphereGeometry, sphereMaterial);
        if (_3dxy.width_X > _3dxy.width_Y) {
            sphere1.position.x = doorBLeft.start_X + (doorBLeft.width_X / 2) - floor_width / 2;
            sphere1.position.z = _3dxy.start_Y + _3dxy.width_Y - floor_height / 2 + converMMtoPX(40 + 10);
            sphere1.position.y = (_3dxy.height_endpoint - _3dxy.height_startpoint) / 2 + converMMtoPX(300);
        } else {
            sphere1.position.z = doorBLeft.start_Y + (doorBLeft.width_Y / 2) - floor_height / 2;
            sphere1.position.x = _3dxy.start_X + _3dxy.width_X - floor_width / 2 + converMMtoPX(40 + 10);
            sphere1.position.y = (_3dxy.height_endpoint - _3dxy.height_startpoint) / 2 + converMMtoPX(300);
        }
        if (_3dxy.width_X > _3dxy.width_Y) {
            sphere2.position.x = doorBLeft.start_X + (doorBLeft.width_X / 2) - floor_width / 2;
            sphere2.position.z = _3dxy.start_Y - floor_height / 2 - converMMtoPX(40);
            sphere2.position.y = (_3dxy.height_endpoint - _3dxy.height_startpoint) / 2 + converMMtoPX(300);
        } else {
            sphere2.position.z = doorBLeft.start_Y + (doorBLeft.width_Y / 2) - floor_height / 2;
            sphere2.position.x = _3dxy.start_X - floor_width / 2 - converMMtoPX(40);
            sphere2.position.y = (_3dxy.height_endpoint - _3dxy.height_startpoint) / 2 + converMMtoPX(300);
        }
    }
    if (_3dobject[0] == '1wingDoorRight') {
        var sphereGeometry = new THREE.SphereGeometry(converMMtoPX(50), 32, 32);
        var sphereMaterial = new THREE.MeshBasicMaterial({
            map: sphereTex,
            shading: THREE.FlatShading,
            side: THREE.DoubleSide,
        });
        var sphere1 = new THREE.Mesh(sphereGeometry, sphereMaterial);
        var sphere2 = new THREE.Mesh(sphereGeometry, sphereMaterial);
        if (_3dxy.width_X > _3dxy.width_Y) {
            sphere1.position.x = doorBRight.start_X + (doorBRight.width_X / 2) - floor_width / 2;
            sphere1.position.z = _3dxy.start_Y + _3dxy.width_Y - floor_height / 2 + converMMtoPX(40 + 10);
            sphere1.position.y = (_3dxy.height_endpoint - _3dxy.height_startpoint) / 2 + converMMtoPX(300);
        } else {
            sphere1.position.z = doorBRight.start_Y + (doorBRight.width_Y / 2) - floor_height / 2;
            sphere1.position.x = _3dxy.start_X + _3dxy.width_X - floor_width / 2 + converMMtoPX(40 + 10);
            sphere1.position.y = (_3dxy.height_endpoint - _3dxy.height_startpoint) / 2 + converMMtoPX(300);
        }
        if (_3dxy.width_X > _3dxy.width_Y) {
            sphere2.position.x = doorBRight.start_X + (doorBRight.width_X / 2) - floor_width / 2;
            sphere2.position.z = _3dxy.start_Y - floor_height / 2 - converMMtoPX(40);
            sphere2.position.y = (_3dxy.height_endpoint - _3dxy.height_startpoint) / 2 + converMMtoPX(300);
        } else {
            sphere2.position.z = doorBRight.start_Y + (doorBRight.width_Y / 2) - floor_height / 2;
            sphere2.position.x = _3dxy.start_X - floor_width / 2 - converMMtoPX(40);
            sphere2.position.y = (_3dxy.height_endpoint - _3dxy.height_startpoint) / 2 + converMMtoPX(300);
        }

    }
    scene.add(sphere1);
    scene.add(sphere2);
    // scene.add(doorToMesh);
}
// Draw Window
function drawWindow(scene, _3dxy, _3dobject) {
    if (!windowTexture) {
        windowTexture = new loader.load('images/windowTex.jpg');
        windowTexture.wrapS = windowTexture.wrapT = THREE.RepeatWrapping;
        windowTexture.repeat.set(1, 1);
    }
    _3dxy.height_endpoint = window3D.height_endpoint + floor3D.height_endpoint;
    _3dxy.height_startpoint = window3D.height_startpoint + floor3D.height_endpoint;
    var sizePillar = 91 / 2 + 20;
    if (_3dxy.width_X > _3dxy.width_Y) {
        _3dxy.start_X = _3dxy.start_X + 5;// + converMMtoPX(sizePillar);  // chinh lai cua so hard code
        _3dxy.width_X = _3dxy.width_X - converMMtoPX(sizePillar * 2);
    } else {
        _3dxy.start_Y = _3dxy.start_Y + 5;// + converMMtoPX(sizePillar); // chinh lai cua so hard code
        _3dxy.width_Y = _3dxy.width_Y - converMMtoPX(sizePillar * 2);
    }
    if (_3dobject[0] == 'smallwindow') {
        draw1wingWindow(scene, _3dxy, _3dobject);
    }
    if (_3dobject[0] == 'normalwindow') {
        window1wings = JSON.parse(JSON.stringify(_3dxy));
        // if (_3dxy.width_X < _3dxy.width_Y) {
        //     window1wings.width_Y = _3dxy.width_Y / 2;
        //     window1wings.width_X = _3dxy.width_X / 2;
        //     // window1wings.start_X = _3dxy.start_X - converMMtoPX(75);
        // } else {
        //     window1wings.width_Y = _3dxy.width_Y / 2;
        //     window1wings.width_X = _3dxy.width_X / 2;
        //     // window1wings.start_Y = _3dxy.start_Y - converMMtoPX(75);
        // }
        // // draw1wingWindow(scene, window1wings, _3dobject);
        // window2wings = JSON.parse(JSON.stringify(_3dxy));
        // if (_3dxy.width_X < _3dxy.width_Y) {
        //     window2wings.width_Y = _3dxy.width_Y / 2;
        //     window2wings.width_X = _3dxy.width_X / 2;
        //     // window2wings.start_X = _3dxy.start_X + converMMtoPX(25);
        //     window2wings.start_Y = _3dxy.start_Y + _3dxy.width_Y / 2;
        // } else {
        //     window2wings.width_X = _3dxy.width_X / 2;
        //     window2wings.width_Y = _3dxy.width_Y / 2;
        //     // window2wings.start_Y = _3dxy.start_Y + converMMtoPX(25);
        //     window2wings.start_X = _3dxy.start_X + _3dxy.width_X / 2;
        // }
        if (_3dxy.width_X < _3dxy.width_Y) {
            window1wings.width_X = _3dxy.width_X / 2;
        } else {
            window1wings.width_Y = _3dxy.width_Y / 2;
        }
        draw1wingWindow(scene, window1wings, _3dobject);
    }
    //var material = {
    //    'transparent': true,
    //    'opacity': 0.0001
    //};
    //var window = drawObj(scene, _3dxy, _3dobject, material);

    //windowObj.push(window);
}
function draw1wingWindow(scene, Dxy, _3dobject) {
    var materialWoodW = {
        'tex': windowTexture,
        'transparent': false,
        'opacity': 0.0001
    };
    var borderWindow = 100;
    wWindow = JSON.parse(JSON.stringify(Dxy));
    if (wWindow.width_X > wWindow.width_Y) {
        wWindow.start_Y = wWindow.start_Y - wWindow.width_Y;
        wWindow.start_X = wWindow.start_X - converMMtoPX(20);
        wWindow.width_X = wWindow.width_X + converMMtoPX(40);
        wWindow.width_Y = wWindow.width_Y * 2;
    } else {
        wWindow.start_X = wWindow.start_X - wWindow.width_X;
        wWindow.start_Y = wWindow.start_Y - converMMtoPX(20);
        wWindow.width_Y = wWindow.width_Y + converMMtoPX(40);
        wWindow.width_X = wWindow.width_X * 2;
    }
    wWindow.height_endpoint = wWindow.height_endpoint + converMMtoPX(20);
    wWindow.height_startpoint = wWindow.height_startpoint - converMMtoPX(20);
    var woodWindow = drawObj(scene, wWindow, _3dobject, materialWoodW);
    windowObj.push(woodWindow);
    // Border Window
    var border_window = JSON.parse(JSON.stringify(wWindow));
    var materialBorder = {
        'map': windowTexture,
        'transparent': false,
        'opacity': 1
    };
    var drawWindow_border = drawObj(scene, border_window, _3dobject, materialBorder);
    //Wood Window
    var wood_window = JSON.parse(JSON.stringify(Dxy));
    if (wood_window.width_X > wood_window.width_Y) {
        //wood_window.start_X = wood_window.start_X - converMMtoPX(100);
        wood_window.start_Y = wWindow.start_Y - wWindow.width_Y;
        wood_window.width_Y = wWindow.width_Y * 2;
    } else {
        //wood_window.start_Y = wood_window.start_Y - converMMtoPX(100);
        wood_window.start_X = wWindow.start_X - wWindow.width_X;
        wood_window.width_X = wWindow.width_X * 2;
    }
    var materialWood_window = {};
    var drawWindow_wood_window = drawObj(scene, wood_window, _3dobject, materialWood_window);
    // swing 1
    var window_dxy1 = JSON.parse(JSON.stringify(Dxy));
    if (window_dxy1.width_X > window_dxy1.width_Y) {
        window_dxy1.start_Y = window_dxy1.start_Y - window_dxy1.width_Y;
        window_dxy1.width_X = window_dxy1.width_X / 2 + converMMtoPX(borderWindow / 2);
    } else {
        window_dxy1.start_X = window_dxy1.start_X - window_dxy1.width_X;
        window_dxy1.width_Y = window_dxy1.width_Y / 2 + converMMtoPX(borderWindow / 2);
    }
    var draw_window_swing1 = drawObj(scene, window_dxy1, _3dobject, materialWood_window);

    //glass swing 1

    var glassW1 = JSON.parse(JSON.stringify(window_dxy1));
    glassW1.height_endpoint = Dxy.height_endpoint - converMMtoPX(borderWindow);
    glassW1.height_startpoint = Dxy.height_startpoint + converMMtoPX(borderWindow);
    if (glassW1.width_X > glassW1.width_Y) {
        glassW1.start_X = glassW1.start_X + converMMtoPX(borderWindow);
        glassW1.width_X = glassW1.width_X - converMMtoPX(borderWindow * 2);
    } else {
        glassW1.start_Y = glassW1.start_Y + converMMtoPX(borderWindow);
        glassW1.width_Y = (glassW1.width_Y - converMMtoPX(borderWindow * 2));
    }
    var material_glass = {
        'tex': glassTexture,
        'transparent': true,
        'opacity': 0.5
    };
    var glassW1_window = drawObj(scene, glassW1, _3dobject, material_glass);
    // swing 2
    var window_dxy2 = JSON.parse(JSON.stringify(Dxy));
    if (window_dxy2.width_X > window_dxy2.width_Y) {

        window_dxy2.start_Y = window_dxy2.start_Y;
        window_dxy2.width_X = window_dxy2.width_X / 2 + converMMtoPX(borderWindow / 2);
        window_dxy2.start_X = window_dxy2.start_X + window_dxy2.width_X - converMMtoPX(borderWindow);
    } else {
        window_dxy2.start_X = window_dxy2.start_X;
        window_dxy2.width_Y = window_dxy2.width_Y / 2 + converMMtoPX(borderWindow / 2);
        window_dxy2.start_Y = window_dxy2.start_Y + window_dxy2.width_Y - converMMtoPX(borderWindow);
    }
    var draw_window_swing2 = drawObj(scene, window_dxy2, _3dobject, materialWood_window);
    //Glass 2
    var glassW2 = JSON.parse(JSON.stringify(window_dxy2));
    glassW2.height_endpoint = Dxy.height_endpoint - converMMtoPX(borderWindow);
    glassW2.height_startpoint = Dxy.height_startpoint + converMMtoPX(borderWindow);
    if (glassW2.width_X > glassW2.width_Y) {
        glassW2.start_X = glassW2.start_X + converMMtoPX(borderWindow);
        glassW2.width_X = glassW2.width_X - converMMtoPX(borderWindow * 2);
    } else {
        glassW2.start_Y = glassW2.start_Y + converMMtoPX(borderWindow);
        glassW2.width_Y = (glassW2.width_Y - converMMtoPX(borderWindow * 2));
    }
    var material_glass = {
        'tex': glassTexture,
        'transparent': true,
        'opacity': 0.5
    };
    var glassW2_window = drawObj(scene, glassW2, _3dobject, material_glass);

    //slice Window
    var drawWindow_border_BSP = new ThreeBSP(drawWindow_border);
    var drawWindow_wood_window_BSP = new ThreeBSP(drawWindow_wood_window);
    drawWindow_border_BSP = drawWindow_border_BSP.subtract(drawWindow_wood_window_BSP);


    //add Window
    var drawWindow_border_Mesh = drawWindow_border_BSP.toMesh();
    var materialBorder = new THREE.MeshBasicMaterial({
        map: windowTexture,
        shading: THREE.FlatShading,
        side: THREE.DoubleSide,
    });
    drawWindow_border_Mesh.material = materialBorder;
    woodWindowObj.push(drawWindow_border_Mesh);

    var draw_window_swing1_BSP = new ThreeBSP(draw_window_swing1);
    var glassW1_BSP = new ThreeBSP(glassW1_window);
    draw_window_swing1_BSP = draw_window_swing1_BSP.subtract(glassW1_BSP);
    var draw_window_swing1_Mesh = draw_window_swing1_BSP.toMesh();
    draw_window_swing1_Mesh.material = materialBorder;
    woodWindowObj.push(draw_window_swing1_Mesh);
    woodWindowObj.push(glassW1_window);
    //
    var draw_window_swing2_BSP = new ThreeBSP(draw_window_swing2);
    var glassW2_BSP = new ThreeBSP(glassW2_window);
    draw_window_swing2_BSP = draw_window_swing2_BSP.subtract(glassW2_BSP);
    var draw_window_swing2_Mesh = draw_window_swing2_BSP.toMesh();
    draw_window_swing2_Mesh.material = materialBorder;
    woodWindowObj.push(draw_window_swing2_Mesh);
    woodWindowObj.push(glassW2_window);

}

// Draw Object with differene texture
function drawObjDiffTexture(scene, _3dxy, _3dobject, materialArr) {

    var cubeMaterials = new THREE.MeshFaceMaterial(materialArr);

    var cubeGeometry = new THREE.CubeGeometry(_3dxy.width_X, _3dxy.height_endpoint - _3dxy.height_startpoint, _3dxy.width_Y, 10, 10, 10);
    cube = new THREE.Mesh(cubeGeometry, cubeMaterials);
    if (inside) {
        cube.position.set((_3dxy.width_X ) / 2 + _3dxy.start_X - converMMtoPX(floorInfo[2][0]) - converMMtoPX(floorInfo[2][2]) / 2, (_3dxy.height_endpoint + _3dxy.height_startpoint) / 2 - converMMtoPX(300 + 300), (_3dxy.width_Y ) / 2 + _3dxy.start_Y - converMMtoPX(floorInfo[2][1]) - converMMtoPX(floorInfo[2][3]) / 2);

    } else {
        cube.position.set((_3dxy.width_X - floor_width) / 2 + _3dxy.start_X, (_3dxy.height_endpoint + _3dxy.height_startpoint) / 2, (_3dxy.width_Y - floor_height) / 2 + _3dxy.start_Y);
    }

    return cube;
}

// Draw Obj
function drawObj(scene, _3dxy, _3dobject, material) {
    // Create an array of materials to be used in a cube, one for each side
    var cubeMaterialArray = [];
    // order to add materials: x+,x-,y+,y-,z+,z-
    if (material.tex == undefined || !material.tex)
        material.tex = defaultTextute;
    if (!material.transparent || material.transparent == undefined)
        material.transparent = false;
    if (!material.opacity || material.opacity == undefined)
        material.opacity = 1;
    cubeMaterialArray.push(new THREE.MeshBasicMaterial({
        map: material.tex,
        side: THREE.DoubleSide,
        transparent: material.transparent,
        opacity: material.opacity
    }));
    cubeMaterialArray.push(new THREE.MeshBasicMaterial({
        map: material.tex,
        side: THREE.DoubleSide,
        transparent: material.transparent,
        opacity: material.opacity
    }));
    cubeMaterialArray.push(new THREE.MeshBasicMaterial({
        map: material.tex,
        side: THREE.DoubleSide,
        transparent: material.transparent,
        opacity: material.opacity
    }));
    cubeMaterialArray.push(new THREE.MeshBasicMaterial({
        map: material.tex,
        side: THREE.DoubleSide,
        transparent: material.transparent,
        opacity: material.opacity
    }));
    cubeMaterialArray.push(new THREE.MeshBasicMaterial({
        map: material.tex,
        side: THREE.DoubleSide,
        transparent: material.transparent,
        opacity: material.opacity
    }));
    cubeMaterialArray.push(new THREE.MeshBasicMaterial({
        map: material.tex,
        side: THREE.DoubleSide,
        transparent: material.transparent,
        opacity: material.opacity
    }));
    var cubeMaterials = new THREE.MeshFaceMaterial(cubeMaterialArray);

    var cubeGeometry = new THREE.CubeGeometry(_3dxy.width_X, _3dxy.height_endpoint - _3dxy.height_startpoint, _3dxy.width_Y, 10, 10, 10);
    if (isDraw == 'roof') {


    }
    if (isDraw == 'wall') {

    }
    cube = new THREE.Mesh(cubeGeometry, cubeMaterials);
    if (inside) {
        cube.position.set((_3dxy.width_X ) / 2 + _3dxy.start_X - converMMtoPX(floorInfo[2][0]) - converMMtoPX(floorInfo[2][2]) / 2, (_3dxy.height_endpoint + _3dxy.height_startpoint) / 2 - converMMtoPX(300 + 300), (_3dxy.width_Y ) / 2 + _3dxy.start_Y - converMMtoPX(floorInfo[2][1]) - converMMtoPX(floorInfo[2][3]) / 2);

    } else {
        cube.position.set((_3dxy.width_X - floor_width) / 2 + _3dxy.start_X, (_3dxy.height_endpoint + _3dxy.height_startpoint) / 2, (_3dxy.width_Y - floor_height) / 2 + _3dxy.start_Y);
    }
    //cube.castShadow = true;
    //cube.receiveShadow = false;
    return cube;
}
function drawRoof3D(scene, _3dxy, _3dobject, type) {
    var url = 'images/' + _3dobject[0] + '.jpg';
    roofTex = new loader.load(url);
    roofTex.wrapS = roofTex.wrapT = THREE.RepeatWrapping;
    roofTex.repeat.set(1, 1);
    var material = {
        'tex': roofTex,
    };
    isDraw = 'roof';
    if (typeBalcony == TYPE_BALCONY_NONE) {
        baconInfo = [];
        baconInfo[2] = [];
        baconInfo[2][3] = 0;
    }
    var typeRoof = _3dobject[0].split('-');
    typeRoof = typeRoof[0] + '-' + typeRoof[1];
    var heightRoof = 300;
    //
    var rType = JSON.parse(JSON.stringify(roofType));
    if (rotate == 1)
        type = 101;
    if (rotate == 2)
        type = 102;
    if (rotate == 3)
        type = 103;
    if (rotate == 4)
        type = 104;
    //
    if (typeRoof == 'roof-2') {

        roof3D = drawObj(scene, _3dxy, _3dobject, material);
        roof_2roof_obj = null;
        roof_1roof_obj = null;
        // set rotate roof

        _3dxyRoof = JSON.parse(JSON.stringify(_3dxy));
        ////
        if (type == 101) {
            var tan = (heightRoof) / (floorInfo[2][3]);
            var b = 2030 +tan*(floorInfo[2][3]-(converPxtoMm(_3dxyRoof.width_Y)/2 - 300)) + 50 / 2;
            // var b = 1820 + converPxtoMm(tan * (floorInfo[2][3])-(_3dxyRoof.width_Y/2-converMMtoPX(300))) + 50 / 2;
            roof3D.rotation.x = Math.atan(tan);
        }
        else if (type == 102) {
            var tan = (heightRoof) / (floorInfo[2][3]);
            var b = 2030 +tan*(floorInfo[2][3]-(converPxtoMm(_3dxyRoof.width_Y)/2 - 300)) + 50 / 2;
            roof3D.rotation.x = -Math.atan(tan);
        } else if (type == 103) {
            var tan = (heightRoof) / (floorInfo[2][2]);
            var b = 2030 +tan*(floorInfo[2][2]-(converPxtoMm(_3dxyRoof.width_X)/2 - 300)) + 50 / 2;
            roof3D.rotation.z = -Math.atan(tan);
        } else if (type == 104) {
            var tan = (heightRoof) / (floorInfo[2][2]);
            var b = 2030 +tan*(floorInfo[2][2]-(converPxtoMm(_3dxyRoof.width_X)/2 - 300)) + 50 / 2;
            roof3D.rotation.z = Math.atan(tan);
        }
        roof3D.position.y = converMMtoPX(b) + floor3D.height_endpoint - floor3D.height_startpoint;

        drawBeamsRoof(scene, _3dxy, tan, type, typeRoof);
        scene.add(roof3D);
        // roof slice
        _3dxy_roofSlice = JSON.parse(JSON.stringify(_3dxy));
        var kc = 200;
        _3dxy_roofSlice.height_startpoint = _3dxy.height_endpoint;
        _3dxy_roofSlice.height_endpoint = _3dxy.height_endpoint + kc;
        var material = {
            'tex': windowTexture,
            'transparent': true,
            'opacity': 0.000001
        };
        roofSlice = drawObj(scene, _3dxy_roofSlice, _3dobject, material);
        if (type == 101) {
            roofSlice.rotation.x = Math.atan(tan);
        }
        else if (type == 102) {
            roofSlice.rotation.x = -Math.atan(tan);
        } else if (type == 103) {
            roofSlice.rotation.z = -Math.atan(tan);
        } else if (type == 104) {
            roofSlice.rotation.z = Math.atan(tan);
        }
        roofSlice.position.y = converMMtoPX(b) + floor3D.height_endpoint - floor3D.height_startpoint + kc / 2;
        scene.add(roofSlice);
    } else if (typeRoof == 'roof-1') {
        var diff = 300;
        var heightRoof = 300;
        roof3D = null;
        if (type == 101 || type === 102) {
            var roof_1roof = JSON.parse(JSON.stringify(_3dxy));
            //roof1
            roof_1roof.width_Y = roof_1roof.width_Y / 2;
            roof_1roof_obj = drawObj(scene, roof_1roof, _3dobject, material);
            var tan = converMMtoPX(heightRoof) / (roof_1roof.width_Y);
            roof_1roof_obj.rotation.x = -Math.atan(tan);
            var c = 2030 + 300 - tan * roof_1roof.width_Y * 2;
            roof_1roof_obj.position.y = converMMtoPX(c) + floor3D.height_endpoint - floor3D.height_startpoint;
            drawBeamsRoof(scene, _3dxy, tan, type, typeRoof);
            scene.add(roof_1roof_obj);
            //roof1_slice
            var _3dxy_roofSlice1 = JSON.parse(JSON.stringify(roof_1roof));
            var kc = 200;
            _3dxy_roofSlice1.height_startpoint = roof_1roof.height_endpoint;
            _3dxy_roofSlice1.height_endpoint = roof_1roof.height_endpoint + kc;
            var material1 = {
                'tex': windowTexture,
                'transparent': true,
                'opacity': 0.0001
            };
            roofSlice1 = drawObj(scene, _3dxy_roofSlice1, _3dobject, material1);
            roofSlice1.rotation.x = -Math.atan(tan);
            roofSlice1.position.y = converMMtoPX(c) + floor3D.height_endpoint - floor3D.height_startpoint + kc / 2;
            scene.add(roofSlice1);
            //roof2

            var roof_2roof = JSON.parse(JSON.stringify(_3dxy));

            // Check is roof cover balcony
            roof_2roof.width_Y = roof_2roof.width_Y / 2;
            roof_2roof.start_Y = roof_2roof.start_Y + _3dxy.width_Y / 2 - converMMtoPX(50);
            roof_2roof_obj = drawObj(scene, roof_2roof, _3dobject, material);
            var tan = converMMtoPX(heightRoof) / (roof_1roof.width_Y);
            roof_2roof_obj.rotation.x = Math.atan(tan);

            // Check is roof cover balcony

            var d = 2030 + 300 - tan * roof_2roof.width_Y * 2;
            // console.log(c);
            roof_2roof_obj.position.y = converMMtoPX(d) + floor3D.height_endpoint - floor3D.height_startpoint;
            drawBeamsRoof(scene, _3dxy, tan, type, typeRoof);
            scene.add(roof_2roof_obj);


            //roof2_slice
            var _3dxy_roofSlice2 = JSON.parse(JSON.stringify(roof_2roof));
            var kc = 200;
            _3dxy_roofSlice2.height_startpoint = roof_2roof.height_endpoint;
            _3dxy_roofSlice2.height_endpoint = roof_2roof.height_endpoint + kc;
            var material1 = {
                'tex': windowTexture,
                'transparent': true,
                'opacity': 0.0001
            };
            roofSlice2 = drawObj(scene, _3dxy_roofSlice2, _3dobject, material1);
            roofSlice2.rotation.x = Math.atan(tan);
            roofSlice2.position.y = converMMtoPX(d) + floor3D.height_endpoint - floor3D.height_startpoint + kc / 2;
            scene.add(roofSlice2);

        } else if (type == 103 || type == 104) {
            var roof_1roof = JSON.parse(JSON.stringify(_3dxy));
            roof_1roof.width_X = roof_1roof.width_X / 2;
            //roof1
            roof_1roof_obj = drawObj(scene, roof_1roof, _3dobject, material);
            var tan = converMMtoPX(heightRoof) / roof_1roof.width_X;
            roof_1roof_obj.rotation.z = Math.atan(tan);
            var c = 2030 + 300 - tan * roof_1roof.width_X * 2;
            roof_1roof_obj.position.y = converMMtoPX(c) + floor3D.height_endpoint - floor3D.height_startpoint;
            drawBeamsRoof(scene, _3dxy, tan, type);
            scene.add(roof_1roof_obj);
            //roof1_slice
            var _3dxy_roofSlice1 = JSON.parse(JSON.stringify(roof_1roof));
            var kc = 200;
            _3dxy_roofSlice1.height_startpoint = roof_1roof.height_endpoint;
            _3dxy_roofSlice1.height_endpoint = roof_1roof.height_endpoint + kc;
            var material1 = {
                'tex': windowTexture,
                'transparent': true,
                'opacity': 0.0001
            };
            roofSlice1 = drawObj(scene, _3dxy_roofSlice1, _3dobject, material1);
            roofSlice1.rotation.z = Math.atan(tan);
            roofSlice1.position.y = converMMtoPX(c) + floor3D.height_endpoint - floor3D.height_startpoint + kc / 2;
            scene.add(roofSlice1);
            //roof2

            var roof_2roof = JSON.parse(JSON.stringify(_3dxy));

            // Check is roof cover balcony

            roof_2roof.width_X = roof_2roof.width_X / 2;

            roof_2roof.start_X = roof_2roof.start_X + roof_2roof.width_X - converMMtoPX(50);
            roof_2roof_obj = drawObj(scene, roof_2roof, _3dobject, material);
            var tan = converMMtoPX(heightRoof) / roof_1roof.width_X;
            roof_2roof_obj.rotation.z = -Math.atan(tan);

            var d = 2030 + 300 - tan * roof_2roof.width_X * 2;
            // console.log(c);
            roof_2roof_obj.position.y = converMMtoPX(d) + floor3D.height_endpoint - floor3D.height_startpoint;
            drawBeamsRoof(scene, _3dxy, tan, type);
            scene.add(roof_2roof_obj);


            //roof2_slice
            var _3dxy_roofSlice2 = JSON.parse(JSON.stringify(roof_2roof));
            var kc = 200;
            _3dxy_roofSlice2.height_startpoint = roof_2roof.height_endpoint;
            _3dxy_roofSlice2.height_endpoint = roof_2roof.height_endpoint + kc;
            var material1 = {
                'tex': windowTexture,
                'transparent': true,
                'opacity': 0.0001
            };
            roofSlice2 = drawObj(scene, _3dxy_roofSlice2, _3dobject, material1);
            roofSlice2.rotation.z = -Math.atan(tan);
            roofSlice2.position.y = converMMtoPX(d) + floor3D.height_endpoint - floor3D.height_startpoint + kc / 2;
            scene.add(roofSlice2);
        }

    }
}
function drawBeamsRoof(scene, _3dxy, tan, type, typeRoof) {
    var width_beam1 = 90;
    var height_beam1 = 90;
    if (typeRoof == 'roof-2') {
        if (type == 101 || type == 102) {
            var height_startpoint = converMMtoPX(2030 + 300) + floor3D.height_endpoint - tan * (_3dxy.width_Y - converMMtoPX(250 * 2) + converMMtoPX(width_beam1));
        } else {
            var height_startpoint = converMMtoPX(2030 + 300) + floor3D.height_endpoint - tan * (_3dxy.width_X - converMMtoPX(250 * 2) + converMMtoPX(width_beam1));

        }
    } else {
        if (type == 101 || type == 102) {
            var height_startpoint = converMMtoPX(2030 + 300) + floor3D.height_endpoint - tan * (_3dxy.width_X / 2 + converMMtoPX(width_beam1 * 2)) - converMMtoPX(50);

        } else {
            var height_startpoint = converMMtoPX(2030 + 300) + floor3D.height_endpoint - tan * (_3dxy.width_Y / 2 + converMMtoPX(width_beam1 * 2)) - converMMtoPX(50);
        }

    }
    height_startpoint = height_startpoint - converMMtoPX(20);
    // beam 1
    var beam1 = JSON.parse(JSON.stringify(_3dxy));
    beam1.start_X = beam1.start_X + converMMtoPX(250);
    beam1.start_Y = beam1.start_Y + converMMtoPX(250);
    beam1.width_X = converMMtoPX(width_beam1);
    beam1.width_Y = beam1.width_Y - converMMtoPX(250 * 2);
    beam1.height_startpoint = height_startpoint;
    beam1.height_endpoint = beam1.height_startpoint + converMMtoPX(width_beam1);
    var materialBeams = {
        'tex': wallTexture
    };
    var beam1_3D = drawObj(scene, beam1, null, materialBeams);
    scene.add(beam1_3D);
    // beam 2
    var beam2 = JSON.parse(JSON.stringify(_3dxy));
    beam2.start_X = beam2.start_X + converMMtoPX(250);
    beam2.start_Y = beam2.start_Y + converMMtoPX(250);
    beam2.width_Y = converMMtoPX(width_beam1);
    beam2.width_X = beam2.width_X - converMMtoPX(250 * 2);
    beam2.height_startpoint = height_startpoint;
    beam2.height_endpoint = beam2.height_startpoint + converMMtoPX(width_beam1);
    var materialBeams = {
        'tex': wallTexture
    };
    var beam2_3D = drawObj(scene, beam2, null, materialBeams);
    scene.add(beam2_3D);

    // beam 3
    var beam3 = JSON.parse(JSON.stringify(_3dxy));
    beam3.start_X = beam1.start_X + beam2.width_X - converMMtoPX(width_beam1);
    beam3.start_Y = beam3.start_Y + converMMtoPX(250);
    beam3.width_X = converMMtoPX(width_beam1);
    beam3.width_Y = beam3.width_Y - converMMtoPX(250 * 2);
    beam3.height_startpoint = height_startpoint;
    beam3.height_endpoint = beam3.height_startpoint + converMMtoPX(width_beam1);
    var materialBeams = {
        'tex': wallTexture
    };
    var beam3_3D = drawObj(scene, beam3, null, materialBeams);
    scene.add(beam3_3D);

    // beam 4
    var beam4 = JSON.parse(JSON.stringify(_3dxy));
    beam4.start_X = beam4.start_X + converMMtoPX(250);
    beam4.start_Y = beam2.start_Y + beam1.width_Y - converMMtoPX(width_beam1);
    beam4.width_Y = converMMtoPX(width_beam1);
    beam4.width_X = beam4.width_X - converMMtoPX(250 * 2);
    beam4.height_startpoint = height_startpoint;
    beam4.height_endpoint = beam4.height_startpoint + converMMtoPX(width_beam1);
    var materialBeams = {
        'tex': wallTexture
    };
    var beam4_3D = drawObj(scene, beam4, null, materialBeams);
    scene.add(beam4_3D);

}
de2ra = function (degree) {
    return degree * (Math.PI / 180);
};

// wall color		attribute			name							immage
var wallColor = [
    ["wall_S5-3500", "S5-3500 オーク", "wall_S5-3500"],    // 9b7e6c oak/gỗ sồi
    ["wall_S5-3700", "S5-3700 チーク", "wall_S5-3700"],    // cea27b teak/màu gỗ tếch
    ["wall_S5-4900", "S5-4900 メイズ", "wall_S5-4900"],    // d9bf51 maize/vàng nhạt
    ["wall_S5-5900", "S5-5900 黒", "wall_S5-5900"]         // 3d3d41 đen
];

//function updateColor()
//{
//
//    var selectColor = document.getElementById('wall_option').selectedIndex;
//    
//    wallPaintTexture = new loader.load('images/'+ wallColor[selectColor][2] + '.PNG');
//    wallPaintTexture.wrapT = THREE.RepeatWrapping;
//    wallPaintTexture.repeat.set(5, 5);
//    
//    // Create an array of materials to be used in a cube, one for each side
//    var cubeMaterialArray = [];
//    
//    cubeMaterialArray.push(new THREE.MeshBasicMaterial({
//        map: wallPaintTexture,
//        side: THREE.DoubleSide,
//        transparent: material.transparent,
//        opacity: material.opacity
//    }));
//    cubeMaterialArray.push(new THREE.MeshBasicMaterial({
//        map: wallPaintTexture,
//        side: THREE.DoubleSide,
//        transparent: material.transparent,
//        opacity: material.opacity
//    }));
//    cubeMaterialArray.push(new THREE.MeshBasicMaterial({
//        map: wallPaintTexture,
//        side: THREE.DoubleSide,
//        transparent: material.transparent,
//        opacity: material.opacity
//    }));
//    cubeMaterialArray.push(new THREE.MeshBasicMaterial({
//        map: wallPaintTexture,
//        side: THREE.DoubleSide,
//        transparent: material.transparent,
//        opacity: material.opacity
//    }));
//    cubeMaterialArray.push(new THREE.MeshBasicMaterial({
//        map: wallPaintTexture,
//        side: THREE.DoubleSide,
//        transparent: material.transparent,
//        opacity: material.opacity
//    }));
//    cubeMaterialArray.push(new THREE.MeshBasicMaterial({
//        map: wallPaintTexture,
//        side: THREE.DoubleSide,
//        transparent: material.transparent,
//        opacity: material.opacity
//    }));
//    var cubeMaterials = new THREE.MeshFaceMaterial(cubeMaterialArray);
//    for ( var i = 0; i < handrailObj.length; i++)
//    {
//    	handrailObj[i].material = new THREE.MeshFaceMaterial(cubeMaterialArray);
//    	handrailObj[i].material.map.needsUpdate = true;
//    }
//
//}

function drawBeamUnderFloor(scene) {
    var type;
    if (baconInfo) {
        if (floorInfo[2][0] == baconInfo[2][0]) {
            if (floorInfo[2][1] < baconInfo[2][1]) {
                type = 1;
                var start_X = floorInfo[2][0];
                var start_Y = floorInfo[2][1];
            } else {
                type = 2;
                var start_X = baconInfo[2][0];
                var start_Y = baconInfo[2][1];
            }
            var width = floorInfo[2][2];
            var height = floorInfo[2][3] + baconInfo[2][3];

        }
        if (floorInfo[2][1] == baconInfo[2][1]) {
            if (floorInfo[2][0] < baconInfo[2][0]) {
                type = 3;
                var start_X = floorInfo[2][0];
                var start_Y = floorInfo[2][1];
            } else {
                type = 4;
                var start_X = baconInfo[2][0];
                var start_Y = baconInfo[2][1];
            }
            var width = floorInfo[2][2] + baconInfo[2][2];
            var height = floorInfo[2][3];
        }

    } else {
        type = 1;
        var width = floorInfo[2][2];
        var height = floorInfo[2][3];
        var start_X = floorInfo[2][0];
        var start_Y = floorInfo[2][1];
    }
    var material_beamUF = {
        'tex': wallTexture,
    };
    //if (type == 1 || type == 2) {
    //
    if (width < 2730) {
        var dxy_1 = {
            'start_X': converMMtoPX(start_X),
            'start_Y': converMMtoPX(start_Y - 30),
            'width_X': converMMtoPX(90),
            'width_Y': converMMtoPX(height),
            'height_startpoint': converMMtoPX(-180),
            'height_endpoint': converMMtoPX(-90)
        };
        var beamUF_1 = drawObj(scene, dxy_1, null, material_beamUF);
        scene.add(beamUF_1);

        var dxy_2 = {
            'start_X': converMMtoPX(start_X + width - 90),
            'start_Y': converMMtoPX(start_Y - 30),
            'width_X': converMMtoPX(90),
            'width_Y': converMMtoPX(height + 60),
            'height_startpoint': converMMtoPX(-180),
            'height_endpoint': converMMtoPX(-90)
        };
        var beamUF_2 = drawObj(scene, dxy_2, null, material_beamUF);
        scene.add(beamUF_2);
    }
    if (width >= 2730 && width < 3640) {
        var dxy_1 = {
            'start_X': converMMtoPX(start_X + 275),
            'start_Y': converMMtoPX(start_Y - 30),
            'width_X': converMMtoPX(90),
            'width_Y': converMMtoPX(height + 60),
            'height_startpoint': converMMtoPX(-180),
            'height_endpoint': converMMtoPX(-90)
        };
        var beamUF_1 = drawObj(scene, dxy_1, null, material_beamUF);
        scene.add(beamUF_1);

        var dxy_2 = {
            'start_X': converMMtoPX(start_X + width - 90 - 275),
            'start_Y': converMMtoPX(start_Y - 30),
            'width_X': converMMtoPX(90),
            'width_Y': converMMtoPX(height + 60),
            'height_startpoint': converMMtoPX(-180),
            'height_endpoint': converMMtoPX(-90)
        };
        var beamUF_2 = drawObj(scene, dxy_2, null, material_beamUF);
        scene.add(beamUF_2);
    }
    if (width >= 3640 && width < 4550) {
        var dxy_1 = {
            'start_X': converMMtoPX(start_X + 420),
            'start_Y': converMMtoPX(start_Y - 30),
            'width_X': converMMtoPX(90),
            'width_Y': converMMtoPX(height + 60),
            'height_startpoint': converMMtoPX(-180),
            'height_endpoint': converMMtoPX(-90)
        };
        var beamUF_1 = drawObj(scene, dxy_1, null, material_beamUF);
        scene.add(beamUF_1);

        var dxy_2 = {
            'start_X': converMMtoPX(start_X + width - 90 - 420),
            'start_Y': converMMtoPX(start_Y - 30),
            'width_X': converMMtoPX(90),
            'width_Y': converMMtoPX(height + 60),
            'height_startpoint': converMMtoPX(-180),
            'height_endpoint': converMMtoPX(-90)
        };
        var beamUF_2 = drawObj(scene, dxy_2, null, material_beamUF);
        scene.add(beamUF_2);
    }
    if (width >= 4550) {
        var dxy_1 = {
            'start_X': converMMtoPX(start_X + 420),
            'start_Y': converMMtoPX(start_Y - 30),
            'width_X': converMMtoPX(90),
            'width_Y': converMMtoPX(height + 60),
            'height_startpoint': converMMtoPX(-180),
            'height_endpoint': converMMtoPX(-90)
        };
        var beamUF_1 = drawObj(scene, dxy_1, null, material_beamUF);
        scene.add(beamUF_1);

        var dxy_2 = {
            'start_X': converMMtoPX(start_X + width - 90 - 420),
            'start_Y': converMMtoPX(start_Y - 30),
            'width_X': converMMtoPX(90),
            'width_Y': converMMtoPX(height + 60),
            'height_startpoint': converMMtoPX(-180),
            'height_endpoint': converMMtoPX(-90)
        };
        var beamUF_2 = drawObj(scene, dxy_2, null, material_beamUF);
        scene.add(beamUF_2);
        var dxy_3 = {
            'start_X': converMMtoPX(width + height + 90) / 2,
            'start_Y': converMMtoPX(start_Y - 30),
            'width_X': converMMtoPX(90),
            'width_Y': converMMtoPX(height + 60),
            'height_startpoint': converMMtoPX(-180),
            'height_endpoint': converMMtoPX(-90)
        };
        var beamUF_3 = drawObj(scene, dxy_3, null, material_beamUF);
        scene.add(beamUF_3);
    }
    //
    var dxy_h_1 = {
        'start_X': converMMtoPX(start_X - 30),
        'start_Y': converMMtoPX(start_Y - 45),
        'width_X': converMMtoPX(width + 60),
        'width_Y': converMMtoPX(90),
        'height_startpoint': converMMtoPX(-90),
        'height_endpoint': converMMtoPX(0)
    };
    var beamUF_h_1 = drawObj(scene, dxy_h_1, null, material_beamUF);
    scene.add(beamUF_h_1);
    var dxy_h_2 = {
        'start_X': converMMtoPX(start_X - 30),
        'start_Y': converMMtoPX(start_Y + height - 45),
        'width_X': converMMtoPX(width + 60),
        'width_Y': converMMtoPX(90),
        'height_startpoint': converMMtoPX(-90),
        'height_endpoint': converMMtoPX(0)
    };
    var beamUF_h_2 = drawObj(scene, dxy_h_2, null, material_beamUF);
    scene.add(beamUF_h_2);
    if (height >= 3640 && height < 5915) {
        var dxy_h_3 = {
            'start_X': converMMtoPX(start_X - 30),
            'start_Y': converMMtoPX(start_Y + height / 2 - 45),
            'width_X': converMMtoPX(width + 60),
            'width_Y': converMMtoPX(90),
            'height_startpoint': converMMtoPX(-90),
            'height_endpoint': converMMtoPX(0)
        };
        var beamUF_h_3 = drawObj(scene, dxy_h_3, null, material_beamUF);
        scene.add(beamUF_h_3);
    }
    if (height >= 5915) {
        var dxy_h_3 = {
            'start_X': converMMtoPX(start_X - 30),
            'start_Y': converMMtoPX(start_Y + height / 3 - 45),
            'width_X': converMMtoPX(width + 60),
            'width_Y': converMMtoPX(90),
            'height_startpoint': converMMtoPX(-90),
            'height_endpoint': converMMtoPX(0)
        };
        var beamUF_h_3 = drawObj(scene, dxy_h_3, null, material_beamUF);
        scene.add(beamUF_h_3);
        var dxy_h_4 = {
            'start_X': converMMtoPX(start_X - 30),
            'start_Y': converMMtoPX(start_Y + height / 3 * 2 - 45),
            'width_X': converMMtoPX(width + 60),
            'width_Y': converMMtoPX(90),
            'height_startpoint': converMMtoPX(-90),
            'height_endpoint': converMMtoPX(0)
        };
        var beamUF_h_4 = drawObj(scene, dxy_h_4, null, material_beamUF);
        scene.add(beamUF_h_4);
    }
    //}else{
    //
    //}
}

function showHanrail(){
    var handrailList = getArray3dCadByType(TYPE_HANDRAIL);
    for (var i = 0; i < handrailList.length; i++) {
        handrail = handrailList[i];
        var _3dxy = {
            'start_X': converMMtoPX(handrail[2][0]),
            'start_Y': converMMtoPX(handrail[2][1]),
            'width_X': converMMtoPX(handrail[2][2]),
            'width_Y': converMMtoPX(handrail[2][3]),
            'height_startpoint': converMMtoPX(handrail[2][4]),
            'height_endpoint': converMMtoPX(handrail[2][5]),
        };

        var _3dobject = handrail[3];
        //drawHandrail(scene, _3dxy, _3dobject);
        // Check if have step
        checkAndDrawHandrail(_3dxy, _3dobject);

    }
}
