1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
package sample3d; import javafx.application.Application; import javafx.scene.paint.Color; import my3d._math3d; import my3d._math3d.*; import my3d._shape3d; import my3d._slider_dialog; import my3d._slider_dialog.*; import my3d._viewer3d4; import my3d._viewport3d; import my3d._viewport3d2; public class _sample10 extends _viewer3d4 { public static void main(String... args) { Application.launch(args); } @Override public int get_view_width() { return 400; } @Override public int get_view_height() { return 400; } private static final Color SELECTED_COLOR = Color.RED; protected static final int PART_NUM = 6, JOINT_NUM = 10, POSE_NUM = 4; private static int get_index(int index) { return (index < 2) ? index : (index / 2 + 1); } protected static class _pose { private double[][] all_params; public _quaternion[] joints; public _pose() { all_params = new double[PART_NUM][3]; joints = new _quaternion[JOINT_NUM]; for (int i = 0; i < JOINT_NUM; i++) joints[i] = new _quaternion(); for (int i = 0; i < PART_NUM; i++) calc_joint(i); } public double[] get_params(int index) { return all_params[index]; } public double[] get_params(int index, double[] params) { System.arraycopy(all_params[index], 0, params, 0, 3); return params; } public void set_params(int index, double[] params) { System.arraycopy(params, 0, all_params[index], 0, 3); calc_joint(index); } private static _vector v_buf = new _vector(); private static _quaternion q_buf1 = new _quaternion(), q_buf2 = new _quaternion(); private void calc_joint(int index) { double[] params = all_params[index]; double t1, t2, t3; t1 = params[0] * Math.PI / 180; t2 = params[1] * Math.PI / 180; t3 = params[2] * Math.PI / 180; if (index == 0) { _quaternion.euler(t1, t2, t3, joints[0]); } else if (index == 1) { _math3d.mul( _quaternion.rot_y(t2, joints[1]), _math3d.mul( _quaternion.rot_x(t1, q_buf1), _quaternion.rot_y(t3, q_buf2), q_buf2), joints[1]); } else { _math3d.mul( _quaternion.rot_v( v_buf.set(Math.cos(t1), 0, -Math.sin(t1)), t2, q_buf1), _quaternion.rot_y(t1, q_buf2), joints[2 * index - 2]); _quaternion.rot_x(t3, joints[2 * index - 1]); } } } private static final String[] PART_NAMES = { "胴体", "頭", "右腕", "左腕", "右足", "左足" }; private static final _slider_data[][] PART_DATA_LIST = { { new _slider_data("垂直", 180, -180, 0), new _slider_data("水平", 180, -180, 0), new _slider_data("傾き", -180, 180, 0), }, { new _slider_data("曲げ(縦)", -20, 80, 0), new _slider_data("曲げ(横)", 90, -90, 0), new _slider_data("ねじれ", 90, -90, 0), }, { new _slider_data("ねじれ", 90, -90, 0), new _slider_data("曲げ(上腕)", -225, 75, 0), new _slider_data("曲げ(前腕)", -180, 0, 0), }, { new _slider_data("ねじれ", 90, -90, 0), new _slider_data("曲げ(太もも)", -180, 20, 0), new _slider_data("曲げ(すね)", 0, 180, 0), } }; protected _pose[] all_pose = new _pose[POSE_NUM]; protected _pose curr_pose; private _slider_dialog[] part_dlgs = new _slider_dialog[PART_NUM]; private _shape3d[] shapes = new _shape3d[PART_NUM]; private _transform[] tf_joints = new _transform[JOINT_NUM]; private int part_index; @Override public void init_scene(_viewport3d vp) { set_animation_enable(true); _viewport3d2 vp2 = (_viewport3d2)vp; set_scene_angle(-30, 210); set_horz_slider_range(360, 0); vp2.sun_vector.set(1, -3, -2); vp2.scene_transform.position.set(0, 0, 25); vp2.set_zsort_enable(true); add_app_menu((index) -> { set_anim_pos(index / 4.0); }, "rポーズ1", "rポーズ2", "rポーズ3", "rポーズ4" ); for (int i = 0; i < POSE_NUM; i++) all_pose[i] = new _pose(); _shape3d shape; // 胴体 shape = _shape3d.create_box(2, 4, 1); shape.transform(new _transform(new _vector(0, 4, 0))); shapes[0] = shape; tf_joints[0] = new _transform(); // 頭 shape = _shape3d.create_box(1, 1, 1); shape.transform(new _transform(new _vector(0, 1, 0))); shapes[1] = shape; tf_joints[1] = new _transform(new _vector(0, 8, 0)); // 腕 shape = _shape3d.create_box(0.6, 2, 0.6); shape.transform(new _transform(new _vector(0, -2, 0))); shapes[2] = shape; shapes[3] = (_shape3d)shape.clone(); // 脚 shape = _shape3d.create_box(1, 2.5, 1); shape.transform(new _transform(new _vector(0, -2.5, 0))); shapes[4] = shape; shapes[5] = (_shape3d)shape.clone(); for (int i = 0; i < 2; i++) { int sign = (i == 0) ? 1 : -1; tf_joints[i * 2 + 2] = new _transform(new _vector(sign * 2.6, 8, 0)); tf_joints[i * 2 + 3] = new _transform(new _vector(0, -4, 0)); tf_joints[i * 2 + 6] = new _transform(new _vector(sign, 0, 0)); tf_joints[i * 2 + 7] = new _transform(new _vector(0, -5, 0)); } // ダイアログ _slider_dialog dlg; for (int i = 0; i < PART_NUM; i++) { dlg = new _slider_dialog(stage, PART_NAMES[i], (params) -> { if (curr_pose == null) return; curr_pose.set_params(part_index, params); repaint(); }, PART_DATA_LIST[get_index(i)] ); dlg.focusedProperty().addListener((ov, prev, curr) -> { if (curr) { for (int j = 0; j < PART_NUM; j++) { if (part_dlgs[j].isFocused()) { shapes[j].set_color(SELECTED_COLOR); part_index = j; } } } else { shapes[part_index].set_color(null); } repaint(); }); part_dlgs[i] = dlg; } view.setOnMouseClicked((evt) -> { vp2.pick(evt.getX(), evt.getY(), (shape_index, face_index) -> { if (shape_index == -1) return; int index = get_index(shape_index); if (!part_dlgs[index].isShowing()) part_dlgs[index].showAndWait(); else part_dlgs[index].requestFocus(); }); repaint(); }); } private _quaternion q_buf = new _quaternion(); @Override public void render(_viewport3d vp) { double pos = get_anim_pos() * 4; int index = (int) pos; pos -= index; index %= 4; if (pos == 0) { if (curr_pose == null || (curr_pose != all_pose[index])) { curr_pose = all_pose[index]; for (int i = 0; i < PART_NUM; i++) part_dlgs[i].init_values(curr_pose.get_params(i)); } } else if (curr_pose != null) { curr_pose = null; } _quaternion[] qa1 = all_pose[index].joints; _quaternion[] qa2 = all_pose[(index + 1) % 4].joints; for (int i = 0; i < JOINT_NUM; i++) _math3d.slerp(qa1[i], qa2[i], pos, q_buf).to_matrix( tf_joints[i].orientation); // 胴体 vp.push_transform(tf_joints[0]); vp.render_shape(shapes[0]); // 頭 vp.push_transform(tf_joints[1]); vp.render_shape(shapes[1]); vp.pop_transform(); // 腕と脚 for (int i = 0; i < 4; i++) { vp.push_transform(tf_joints[2 * i + 2]); vp.render_shape(shapes[i + 2]); vp.push_transform(tf_joints[2 * i + 3]); vp.render_shape(shapes[i + 2]); vp.pop_transform(); vp.pop_transform(); } } }