13
|
1 // ループさせる処理を入れる
|
|
2 function MainLoop(ctx,sgroot,w,h){
|
|
3 collision_object(sg_exec_tree,sgroot,w,h)
|
|
4 move_object(sg_exec_tree,sgroot,w,h)
|
|
5 draw_object(ctx,sg_exec_tree,sgroot)
|
17
|
6 camera_object(ctx, sgroot, sgroot.camera)
|
13
|
7 }
|
|
8
|
|
9
|
|
10 /* 再帰的に呼び出して全てのnodeのcollisionを実行する */
|
|
11 function collision_object(node,sgroot,w,h){
|
|
12 if(node.collision != null){
|
|
13 node.collision(node,sgroot,w,h)
|
|
14 node.frame++
|
|
15 }
|
16
|
16
|
|
17 for(num in node.child){
|
|
18 collision_object(node.child[num],sgroot,w,h)
|
|
19 }
|
13
|
20 }
|
|
21
|
|
22
|
|
23 /* 再帰的に呼び出して全てのnodeのmoveを実行する */
|
|
24 function move_object(node,sgroot,w,h){
|
16
|
25 if(node.move != null){
|
|
26 node.move(node,sgroot,w,h)
|
|
27 }
|
|
28
|
13
|
29 for(num in node.child){
|
|
30 move_object(node.child[num],sgroot,w,h)
|
|
31 }
|
|
32 }
|
|
33
|
|
34
|
|
35 /* 再帰的に呼び出して全てのnodeをdrawする */
|
|
36 function draw_object(ctx,node,sgroot){
|
16
|
37 if(node.id != null){
|
|
38 drawObject_link(ctx, node, node.angle, node.xyz, node.scale, sgroot.sg_src[node.id])
|
|
39 }else if(node.id != null && node.link == true){
|
|
40 drawObject_link(ctx, node, node.angle, node.xyz, node.scale, sgroot.sg_src[node.id])
|
|
41 }else{
|
|
42 create_matrix(node, node.angle, node.xyz, node.scale)
|
|
43 }
|
|
44
|
13
|
45 for(num in node.child){
|
|
46 draw_object(ctx,node.child[num],sgroot)
|
|
47 }
|
|
48 }
|
|
49
|
|
50
|
17
|
51 function camera_object(ctx, sgroot, node){
|
|
52 sgroot.getCamera(ctx, node)
|
|
53 }
|
|
54
|
|
55
|
16
|
56 hand_mat = function(node){
|
|
57 for(var num in node.child){
|
|
58 node.child[num].parents_mat = new J3DIMatrix4()
|
|
59 //node.child[num].parents_mat = node.mat
|
|
60 node.mat.getAsArrayMatrix(node.child[num].parents_mat.$matrix)
|
|
61 }
|
|
62 }
|
|
63
|
|
64
|
|
65 SceneGraph = function(){
|
13
|
66 this.next = null
|
|
67 this.prev = null
|
|
68 this.last = null
|
|
69 this.parents = null
|
|
70 this.children = null
|
|
71 this.lastChild = null
|
|
72
|
|
73 this.xyz = new Array(0,0,0)
|
|
74 this.angle = new Array(0,0,0)
|
|
75 this.stack_xyz = new Array(0,0,0)
|
|
76 this.stack_angle = new Array(0,0,0)
|
|
77 this.w = 0
|
|
78 this.h = 0
|
|
79 this.scale = 1.0
|
|
80 this.dx = 1.0
|
|
81 this.dy = 1.0
|
|
82 this.dz = 1.0
|
16
|
83 this.id = null
|
13
|
84 this.num = 0
|
16
|
85 this.parents_mat = null
|
|
86 this.mat = null
|
|
87 this.link = false
|
13
|
88
|
|
89 this.move = null
|
|
90 this.collision = null
|
|
91 this.child = new Array()
|
|
92
|
|
93
|
|
94 this.flag_remove = 0
|
|
95 this.flag_drawable = 1
|
|
96
|
|
97 this.frame = 0
|
|
98 }
|
|
99
|
|
100
|
|
101 // SceneGraphを削除する
|
|
102 SceneGraph.prototype.remove = function(node){
|
|
103 if(node.parents != null){
|
|
104 var length = node.parents.child.length
|
|
105
|
|
106
|
|
107 if(length != 0){
|
|
108 // 親の書き換え
|
|
109 for(num in node.child){
|
|
110 node.child[num].parents = node.parents
|
|
111 }
|
|
112
|
|
113 // 要素を取り除く
|
|
114 node.parents.child.splice(node.num, 1)
|
|
115
|
|
116 // 取り除いたnodeのchildをnodeのparentsに追加
|
|
117 for(num in node.child){
|
|
118 if(node.child[num].id != "LOCK"){
|
|
119 node.parents.child.push(node.child[num])
|
|
120 }
|
|
121 }
|
|
122
|
|
123
|
|
124 // idの振りなおし
|
|
125 for(num in node.parents.child){
|
|
126 node.parents.child[num].num = num
|
|
127 }
|
|
128 }else{
|
|
129 node.parents.child.length = 0
|
|
130 }
|
|
131 }
|
|
132 }
|
|
133
|
|
134
|
|
135 /* moveとcollisionをsetする */
|
|
136 SceneGraph.prototype.set_move_collision = function(new_move, new_collision){
|
|
137 this.move = new_move
|
|
138 this.collision = new_collision
|
|
139 }
|
|
140
|
|
141
|
|
142 SceneGraph.prototype.addBrother = function(bro){
|
|
143 if(this.parents != null){
|
|
144 parents.addChild(bro)
|
|
145 }else{
|
|
146 alert("error : doesn't have parent")
|
|
147 }
|
|
148
|
|
149 return bro
|
|
150 }
|
|
151
|
|
152
|
|
153 /* 子どもの追加 */
|
|
154 SceneGraph.prototype.addChild = function(node){
|
|
155 var num = this.child.length
|
|
156 this.child.push(node)
|
|
157 node.parents = this
|
|
158 node.num = num
|
|
159 }
|
|
160
|
|
161
|
|
162
|
|
163
|
|
164
|