學(xué)習(xí)筆記(5)ht for web----關(guān)于吸附父圖元

知識回顧:
Node#getHost()和Node#setHost(node)獲取和設(shè)置吸附的圖元對象
Node#getAttaches()返回目前吸附到該圖元的所有對象义黎,返回ht.List鏈表對象族檬,無吸附對象時返回空
Node#isHostOn(node)判斷該圖元是否吸附到指定圖元對象上
Node#isLoopedHostOn(node)判斷該圖元是否與指定圖元相互形成環(huán)狀吸附聚至,例如A吸附B,B吸附C戴已,C又吸附回A固该,則A,B和C圖元相互環(huán)狀吸附

參看代碼解釋:
效果圖:


Paste_Image.png
var createInsideWall = function(dataModel) {



    ht.Default.setImage('door', 'images/door_left.png');
    ht.Default.setImage('ldoor', 'images/ldoor.jpg');
    ht.Default.setImage('rdoor', 'images/rdoor.jpg');
    ht.Default.setImage('winImage', 'images/chuang.jpg');   
    ht.Default.setImage('ceramic_tile', 'images/ceramic_tile.png');
    
    
    //  wall.setPoints({})里面的左邊點要按順序繪制
    //在2d平面圖上糖儡,正方向是向右伐坏、向下
    //setPoints結(jié)合setTall方法類似于s3的作用
    //內(nèi)墻相關(guān)參數(shù)

    var wall = new ht.CSGShape();
    //參數(shù)對應(yīng)關(guān)系x:width、y:height;    
    var x1=-2000,x2=2000,x3=2000,x4=-2000,y1=-1500,y2=-1500,y3=1500,y4=1500;
    wall.setPoints([{
        x: x1,
        y: y1
    }, {
        x: x2,
        y: y2
    }, {
        x: x3,
        y: y3
    }, {
        x: x4,
        y: y4
    }]);
    wall.setPosition(0,-1500);
    wall.setTall(500); //設(shè)置墻體高度
    wall.setElevation(wall.getTall() / 2); //把墻體往地板上部上移tall的一半高度
    wall.setClosePath(true);
    wall.setThickness(50);
    wall.s({
        'shape.border.width': 20,
        'shape.border.color': '#898989',
        'all.color':'white',
        'back.image': 'ceramic_tile',
        'back.uv.scale': [1, 10],
        'repeat.uv.length': 80,//改變圖片重復(fù)問題握联,同樣可以解決機器區(qū)域的圖片問題--重復(fù)貼墻紙
        'front.color': 'lightgray',
        'csg.color': '#DDDDDD'
    });
    dataModel.add(wall);
    
    
    /**************************挖空一半正前方的墻體***************************/
    var frontWall = new ht.CSGNode();
    frontWall.setHost(wall);
    frontWall.setParent(wall);
    frontWall.setWidth(4000);
    frontWall.setHeight(40);
    frontWall.setTall(wall.getTall()/2);
    frontWall.setElevation(wall.getTall()*0.75);
    frontWall.s({
        'attach.index':2,
        'attach.offset.relative':true,
        'attach.offset':0.5,
        'all.visible':false
    });
    dataModel.add(frontWall);

/*
 
 * 因為ht.CSGNode繼承于ht.Node,
 * 因為ht.DoorWindow繼承于ht.CSGNode,
 * 所以ht.DoorWindow具有前兩者的所有屬性方法桦沉,包括挖空墻的方法,
 * 所以在創(chuàng)建窗戶時拴疤,不需要再進行挖空的操作永部。
 * 
 * */

//  //挖空門
//  var HollowDoor = new ht.CSGNode();
//  HollowDoor.setHost(wall);
//  HollowDoor.setParent(wall);
//  HollowDoor.setWidth(240);
//  HollowDoor.setHeight(30);
//  HollowDoor.setTall(200);
////  HollowDoor.s3(240,200,30)
//  HollowDoor.setElevation(70);
//  HollowDoor.s({
//      'attach.index': 2, 
//      'attach.offset.relative': true,
//      'attach.offset': 0.864, 
//      'all.visible': false
//  });
//  dataModel.add(HollowDoor);


    var door_left = new ht.DoorWindow();
    door_left.setHost(wall);
    //  doorleft.setParent(wall);
    door_left.setWidth(120);
    door_left.setTall(200);
    door_left.setElevation(door_left.getTall() / 2+10); 
    door_left.s({

        'attach.index': 2,//根據(jù)setPoints的左邊點的順序來確定index的值
        'attach.offset.relative': true,
        'attach.offset': 0.864,//根據(jù)繪制的順序,距離上一個點的距離(0~1)
        'front.image': 'ldoor',
        'back.image': 'ldoor',
        'all.color': 'white',
//      'all.blend': '#A95A10',
        'front.uv': [1, 0, 1, 1, 0, 1, 0, 0],
        'back.transparent': true,
        'dw.axis': 'right',
        'dw.s3': [1, 1, 0.1]

    })
    dataModel.add(door_left);




    var door_right = new ht.DoorWindow();
    door_right.setHost(wall);
    //  doorleft.setParent(wall);
    door_right.setWidth(120);
    door_right.setTall(200);
    door_right.setElevation(door_right.getTall()/2+10);
    door_right.s({
        'attach.index': 2,
        'attach.offset.relative': true,//相對定位
        'attach.offset': 0.81,
        'front.image': 'rdoor',
        'back.image': 'rdoor',
        'all.color': 'white',
        'front.uv': [0,1, 0,0, 1,0, 1,1],
        'back.uv': [1,0, 1,1, 0,1, 0,0],
        'dw.axis': 'right',//右手邊打開門
        'dw.s3': [1, 1, 0.1]//窗戶的透明度的參數(shù)

    })
    dataModel.add(door_right);
    
    
    /***************************************創(chuàng)建窗戶和窗臺相關(guān)操作*********************/
    //挖空窗
    
    for(i=0;i<3;i++){
        var offset=0.17, offsetX=0.32;
         offset+=offsetX*i;
         createWindowOnWall(offset);
    }
    
     function createWindowOnWall(offset){
    
    var windowH=400;
    var windowW=600;    
    var insideWindow =new ht.CSGNode();
    insideWindow.setHost(wall);
    insideWindow.setParent(wall);
    insideWindow.setWidth(windowW);
    insideWindow.setHeight(30);
    insideWindow.setTall(windowH);
    insideWindow.setElevation(insideWindow.getTall()/2+25);
    insideWindow.s({
        'attach.index':0,
        'attach.offset.relative':true,
        'attach.offset':offset,
        'all.visible':false
    })
    dataModel.add(insideWindow);

   //創(chuàng)建窗臺
    var table =new ht.Node();
       noteStyle = {
                    'note': '英雄聯(lián)盟',
                    'note.position': 17,
                    'note.offset.x': 0,
                    'note.offset.y': 0                   
                };
    table.setHost(wall);
    table.setParent(wall);
    table.s3(windowW,10,40);
    table.setElevation(20);
    table.s(noteStyle);
    table.s({
        'attach.index':0,
        'attach.offset.relative':true,
        'attach.offset':offset,
        'all.color':'white',
         'attach.gap': -20//z軸偏移
    })
    dataModel.add(table);
    
    //創(chuàng)建玻璃
    var windowInside =new ht.Node();
    windowInside.setHost(wall);
    windowInside.setParent(wall);
//  windowInside.setImage('winImage');//注意區(qū)分setImage方法與all.image之間的區(qū)別
    windowInside.setWidth(windowW);
    windowInside.setHeight(15);
    windowInside.setTall(windowH);
    windowInside.open = false;
    //旋轉(zhuǎn)角度
    windowInside.angle = Math.PI / 4;
    windowInside.window = true; //旋轉(zhuǎn)方向
    windowInside.setElevation(windowInside.getTall()/2+25);
    windowInside.s({    
        'attach.index': 0,
        'attach.offset.relative': true,//相對定位
        'attach.offset':offset,
        'front.image':'winImage',
        'back.image':'winImage',        
        'dw.s3': [1, 1, 0.1],//窗戶的透明度的參數(shù)
    })
    dataModel.add(windowInside);
    
    }
/************************************************************/  
    
    
    //************************創(chuàng)建柱子*************************
    for(i=0;i<4;i++){
        var pillarX=-1925,pillar=1280,pillarZ=-2970;
        
        pillarX+=pillar*i;
        createPillar(pillarX,250,pillarZ);
    
    }

    function createPillar(x,y,z){
        var node=new ht.Node();
        node.setHost(wall);
        node.setParent(wall);
        node.p3(x,y,z);
        node.s3(150,495,100);
        node.s({
            'all.color':'white'
        });
        dataModel.add(node);
        
    }
    /****************************************************************************/
    
            
    /*****************************創(chuàng)建左右兩側(cè)的大門**********************/
    var index_l=3,index_r=1;    
    for(i=0;i<2;i++){
        if(i==0){
            createBigGate(index_r);
        }
        if(i==1){
            createBigGate(index_l);
        }
    }
    function createBigGate(index){
    var bigGate1 =new ht.DoorWindow();
    bigGate1.setHost(wall);
    bigGate1.setParent(wall);
    bigGate1.setWidth(300);
    bigGate1.setHeight(30);
    bigGate1.setTall(380);
    bigGate1.setToolTip('Double click to open the window');
    bigGate1.setElevation(bigGate1.getTall()/2+15);
    bigGate1.s({
        'attach.index': index,
        'attach.offset.relative': true,//相對定位
        'attach.offset': 0.2,
        'front.image': 'ldoor',
        'back.image': 'ldoor',
        'all.color': 'white',
        'front.uv': [0,1, 0,0, 1,0, 1,1],
        'back.uv': [1,0, 1,1, 0,1, 0,0],
        'dw.axis': 'left',
        'dw.s3': [1, 1, 0.1]//窗戶的透明度的參數(shù)
    });
    
    dataModel.add(bigGate1);
    
    var bigGate2 =new ht.DoorWindow();
    bigGate2.setHost(wall);
    bigGate2.setParent(wall);
    bigGate2.setWidth(300);
    bigGate2.setHeight(30);
    bigGate2.setTall(380);
    bigGate2.setElevation(bigGate2.getTall()/2+15);
    bigGate2.s({
        'attach.index': index,
        'attach.offset.relative': true,
        'attach.offset': 0.3,
        'front.image': 'rdoor',
        'back.image': 'rdoor',
        'all.color': 'white',
        'front.uv': [0,1, 0,0, 1,0, 1,1],
        'back.uv': [1,0, 1,1, 0,1, 0,0],
        'dw.axis': 'right',
        'dw.s3': [1, 1, 0.1]
    });
    
    dataModel.add(bigGate2);
    
    
    var bigGate3 =new ht.DoorWindow();
    bigGate3.setHost(wall);
    bigGate3.setParent(wall);
    bigGate3.setWidth(300);
    bigGate3.setHeight(30);
    bigGate3.setTall(380);
    bigGate3.setElevation(bigGate3.getTall()/2+15);
    bigGate3.s({
            'attach.index': index,
        'attach.offset.relative': true,//相對定位
        'attach.offset': 0.7,
        'front.image': 'ldoor',
        'back.image': 'ldoor',
        'all.color': 'white',
        'front.uv': [0,1, 0,0, 1,0, 1,1],
        'back.uv': [1,0, 1,1, 0,1, 0,0],
        'dw.axis': 'left',
        'dw.s3': [1, 1, 0.1]//窗戶的透明度的參數(shù)
    });
    
    dataModel.add(bigGate3);
    
    var bigGate4 =new ht.DoorWindow();
    bigGate4.setHost(wall);
    bigGate4.setParent(wall);
    bigGate4.setWidth(300);
    bigGate4.setHeight(30);
    bigGate4.setTall(380);
    bigGate4.setElevation(bigGate4.getTall()/2+15);
    bigGate4.s({
        'attach.index': index,
        'attach.offset.relative': true,
        'attach.offset': 0.8,
        'front.image': 'rdoor',
        'back.image': 'rdoor',
        'all.color': 'white',
//      'all.blend': '#A95A10',
        'front.uv': [0,1, 0,0, 1,0, 1,1],
        'back.uv': [1,0, 1,1, 0,1, 0,0],
//      'back.transparent': true,
//      'front.transparent': true,
//      'all.reverse.cull': true,
        'dw.axis': 'right',
        'dw.s3': [1, 1, 0.1]
    });
    
    dataModel.add(bigGate4);
   }
    
}

把host作為參數(shù)傳遞的使用案例;

      host = createNode([0, 51, 0], [100, 100, 100]).s({  
                    'label': 'Cube',
                    'label.visible': false,
                    'all.image': 'dice',                    
                    'front.uv': [0.25, 0.75, 0.25, 1, 0.5, 1, 0.5, 0.75],                    
                    'back.uv': [0.25, 0.25, 0.25, 0.5, 0.5, 0.5, 0.5, 0.25],
                    'bottom.uv': [0.25, 0, 0.25, 0.25, 0.5, 0.25, 0.5, 0],
                    'left.uv': [0, 0.75, 0, 1, 0.25, 1, 0.25, 0.75],
                    'right.uv': [0.5, 0.75, 0.5, 1, 0.75, 1, 0.75, 0.75],
                    'front.opacity': 0.5,
                    'front.transparent': true,                      
                    'front.blend': 'red',
                    'top.visible': false,                    
                    'all.reverse.flip': true,
                    'note': 'A good example about customizing uv',
                    'note.face': 'right',
                    'note.autorotate': true,
                    'note2': 'A fixed note',
                    'note2.face': 'front',                    
                    'note2.position': 6,
                    'icons': {
                        ICONS1: {
                            names: ['node_icon', 'group_icon', 'subGraph_icon', 'grid_icon', 'shape_icon', 'edge_icon'],                            
                            position: 5
                        },
                        ICONS2: {
                            names: ['earth', 'colors', 'fab', 'dice'],
                            position: 26,                            
                            width: 30,
                            height: 30,
                            gap: 5,                            
                            direction: 'north', 
                            face: 'left',
                            r3: [Math.PI/2, Math.PI/2, 0],
                            rotationMode: 'yxz',
                            t3: [-15, 15, 0]
                        }
                    }
                }); 

                createNode([0, 51, -50], [50, 50, 50], host).s({
                    'shape3d': 'sphere',
                    'shape3d.image': 'earth',
                    'body.color': 'red',
                    'label': 'Sphere',
                    'label.background': 'yellow',
                    'label2': 'Sphere',
                    'label2.background': 'blue',
                    'label2.position': 3,
                    'label2.face': 'back'
                });      




 function createNode(p3, s3, host){
                var node = new ht.Node();
                node.p3(p3);
                node.s3(s3);
                node.setHost(host);
                dataModel.add(node);
                return node;
            }         
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末呐矾,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子懦砂,更是在濱河造成了極大的恐慌蜒犯,老刑警劉巖组橄,帶你破解...
    沈念sama閱讀 222,252評論 6 516
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異罚随,居然都是意外死亡玉工,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,886評論 3 399
  • 文/潘曉璐 我一進店門淘菩,熙熙樓的掌柜王于貴愁眉苦臉地迎上來遵班,“玉大人,你說我怎么就攤上這事潮改∠林#” “怎么了?”我有些...
    開封第一講書人閱讀 168,814評論 0 361
  • 文/不壞的土叔 我叫張陵汇在,是天一觀的道長翰萨。 經(jīng)常有香客問我,道長糕殉,這世上最難降的妖魔是什么亩鬼? 我笑而不...
    開封第一講書人閱讀 59,869評論 1 299
  • 正文 為了忘掉前任,我火速辦了婚禮阿蝶,結(jié)果婚禮上雳锋,老公的妹妹穿的比我還像新娘。我一直安慰自己羡洁,他們只是感情好魄缚,可當(dāng)我...
    茶點故事閱讀 68,888評論 6 398
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著焚廊,像睡著了一般冶匹。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上咆瘟,一...
    開封第一講書人閱讀 52,475評論 1 312
  • 那天嚼隘,我揣著相機與錄音,去河邊找鬼袒餐。 笑死飞蛹,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的灸眼。 我是一名探鬼主播卧檐,決...
    沈念sama閱讀 41,010評論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼焰宣!你這毒婦竟也來了霉囚?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,924評論 0 277
  • 序言:老撾萬榮一對情侶失蹤匕积,失蹤者是張志新(化名)和其女友劉穎盈罐,沒想到半個月后榜跌,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,469評論 1 319
  • 正文 獨居荒郊野嶺守林人離奇死亡盅粪,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,552評論 3 342
  • 正文 我和宋清朗相戀三年钓葫,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片票顾。...
    茶點故事閱讀 40,680評論 1 353
  • 序言:一個原本活蹦亂跳的男人離奇死亡础浮,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出奠骄,到底是詐尸還是另有隱情豆同,我是刑警寧澤,帶...
    沈念sama閱讀 36,362評論 5 351
  • 正文 年R本政府宣布戚揭,位于F島的核電站诱告,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏民晒。R本人自食惡果不足惜精居,卻給世界環(huán)境...
    茶點故事閱讀 42,037評論 3 335
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望潜必。 院中可真熱鬧靴姿,春花似錦、人聲如沸磁滚。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,519評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽垂攘。三九已至维雇,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間晒他,已是汗流浹背吱型。 一陣腳步聲響...
    開封第一講書人閱讀 33,621評論 1 274
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留陨仅,地道東北人津滞。 一個月前我還...
    沈念sama閱讀 49,099評論 3 378
  • 正文 我出身青樓,卻偏偏與公主長得像灼伤,于是被迫代替她去往敵國和親触徐。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,691評論 2 361

推薦閱讀更多精彩內(nèi)容

  • 《裕語言》速成開發(fā)手冊3.0 官方用戶交流:iApp開發(fā)交流(1) 239547050iApp開發(fā)交流(2) 10...
    葉染柒丶閱讀 27,041評論 5 19
  • 1. 關(guān)于診斷X線機準(zhǔn)直器的作用狐赡,錯誤的是()撞鹉。 (6.0 分) A. 顯示照射野 B. 顯示中心線 C. 屏蔽多...
    我們村我最帥閱讀 10,578評論 0 5
  • 1. Java基礎(chǔ)部分 基礎(chǔ)部分的順序:基本語法,類相關(guān)的語法,內(nèi)部類的語法孔祸,繼承相關(guān)的語法隆敢,異常的語法发皿,線程的語...
    子非魚_t_閱讀 31,664評論 18 399
  • {1}消息 一般來說崔慧,把消息分為:好消息和壞消息。 在七月的最后一天穴墅,一個突如其來的壞消息惶室。那天妹妹來我出租屋,我...
    之之了閱讀 874評論 0 2
  • 我有二個女兒,一個快三歲松捉,一個快一歲夹界。二個女兒都很漂亮,而且聰明可人(估計所有的爸爸都會這樣認為自己的女兒隘世,呵呵)...
    4ccd352c5b0e閱讀 608評論 3 3