변형의 본질을 더 잘 파악하기 위해 우리는 막다른 골목 없이 360도 3차원 회전 효과를 얻을 수 있는 3차원 큐브 모델을 완성하기로 결정했습니다.
그런데 회전할 때 각 면의 보기 순서를 결정하기가 어렵고 아직 완벽하게 해결되지 않은 부분에 대해 누군가 답변해 주셨으면 좋겠습니다!
소스코드는 직접 기여하였습니다:
<script> <br>/**<br>* 이 버전에는 다음과 같은 문제가 있습니다. <br>* 3차원 회전의 zIndex 계산에 문제가 있습니다. <br>* 또한 다양한 모델이 부족하며 일반적인 모델에는 선, 표면, 원뿔, 구, 타원체 등 <br>*/ <br>함수 cuboidModel(left_init,top_init,long_init,width_init,height_init) <br>{ <br>// ///////////////////////////////////// <br>//개인 변수 초기화<br>/ ////////////////////////////////////// <br>//직육면체 초기화 위치 및 크기<br>var left = left_init; <br>var long = long_init; <br>var height = height_init; 변환 각도, 기본값은 0입니다. <br>varrotateX = 0; <br>varrotateY = 0;<br>var zIndex = 0; 직육면체의 측면 <br>var div_behind; <br>var div_right <br>var div_bottom; ///////// ////////////////////////// <br>//직육면체 초기화<br>/// ////////// /////////////////////////// <br>//초기값을 기준으로 6개의 면을 구성합니다. 위치. <br>this.init = function() { <br>//앞면 div 만들기 <br>div_front = document.createElement("div") <br>div_front.className = "cuboid_side_div" <br>div_front.innerHTML = "div 앞"; <br>div_front.style.BackgroundColor="#f1b2b2"; <br>document.body.appendChild(div_front) <br>//div 뒤에 만들기 <br>div_behind = document.createElement(" div"); <br>div_behind.className = "cuboid_side_div"; <br>div_behind.innerHTML = "div 뒤"; <br>div_behind.style.BackgroundColor="#bd91eb"; <br>document.body.appendChild( div_behind);//왼쪽 div 만들기 <br>div_left = document.createElement("div"); <br>div_left.className = "cuboid_side_div" <br>div_left.innerHTML = "div left"; >div_left.style.groundColor="#64a3c3"; <br>document.body.appendChild(div_left) <br>//오른쪽 div 만들기 <br>div_right = document.createElement("div") <br> .className = "cuboid_side_div"; <br>div_right.innerHTML = "div 오른쪽"; <br>div_right.style.BackgroundColor="#78e797"; <br>document.body.appendChild(div_right)// 상단 div 만들기 <br>div_top = document.createElement("div"); <br>div_top.className = "cuboid_side_div"; <br>div_top.innerHTML = "div top" <br>div_top.style.BackgroundColor=" #e7db78"; <br>document.body.appendChild(div_top); <br>//하단 div 생성 <br>div_bottom = document.createElement("div"); <br>div_bottom.className = "cuboid_side_div"; <br>div_bottom.innerHTML = "div 하단"; <br>div_bottom.style.BackgroundColor="#e79c78"; <br>document.body.appendChild(div_bottom) <br>this.refresh() <br>} ; <br>//Redraw<br>this.refresh = function() <br>{ <br>//div_front 스타일 정의 <br>div_front.style.left = left "px" <br>div_front.style; top = 상단 "px"; <br>div_front.style.width = 긴 "px"; <br>div_front.style.height = 높이 "px" <br>div_front.style.webkitTransformOrigin = "50% 50%" ((-1)*너비 / 2) "px"; <br>//div_behind 스타일 정의<br>div_behind.style.left = 왼쪽 "px"; <br>div_behind.style.top = 상단 "px" ; <br>div_behind.style.width = div_front.style.width; <br>div_behind.style.height = div_front.style.height; <br>div_behind.style.webkitTransformOrigin = "50% 50% " ((-1 ) *너비 / 2) "px"; <br>//div_left 스타일 정의 <br>div_left.style.left = 왼쪽 ((긴 - 높이) / 2) "px" <br>div_left.style.top = 상단 ((높이 - 너비) / 2) "px"; <br>div_left.style.width = 높이 "px"; <br>div_left.style.height = 너비 "px" <br>div_left.style.webkitTransformOrigin "50% 50%" ((-1) * long /2 ) "px"; <br>//div_right 스타일 정의<br>div_right.style.left = div_left.style.left <br>div_right.style; top = div_left.style.top; <br>div_right.style.width = div_left.style.width; <br>div_right.style.height = div_left.style.height; <br>div_right.style.webkitTransformOrigin = "50% 50% " ((-1) * 긴 /2 ) "px"; <br>//div_top 스타일 정의<br>div_top.style.left = 왼쪽 "px"; <br>div_top.style.top = 상단( (높이 - 너비)/ 2) "px"; <br>div_top.style.width = 긴 "px"; <br>div_top.style.height = 너비 "px" <br>div_top.style.webkitTransformOrigin = " 50% 50% " ((-1) * 높이 /2 ) "px"; <br>//div_bottom 스타일 정의 <br>div_bottom.style.left = div_top.style.left; <br>div_bottom.style.top = div_top.style.top; <br>div_bottom.style.width = div_top.style.width; <br>div_bottom.style.height = div_top.style.height <br>div_bottom.style.webkitTransformOrigin; % " ((-1) * 높이 /2 ) "px"; <br>this.rotate(rotateX,rotateY,rotateZ); <br>}; <br>//큐브 회전<br>this.rotate = function(x,y,z) { <br>rotateX = x; <br>rotateY = y; <br>rotateZ = z <br>varrotateX_front =rotateX;var RotateY_front = 회전Y; <br>var RotateZ_front = 회전Z; varrotateY_behind=rotateY_front *(-1); <br>var RotateZ_behind = RotateZ_front * (-1); <br>varrotateX_top =rotateX_front 90; <br>varrotateY_top=rotateZ_front; <br>varrotateZ_top =rotateY_front *(-1); <br>varrotateX_bottom=rotateX_front-90; <br>var RotateY_bottom = RotateZ_front * (-1); <br>varrotateZ_bottom=rotateY_front; <br>varrotateX_left =rotateX_front 90; <br>varrotateY_left =rotateZ_front - 90; <br>varrotateZ_left =rotateY_front *(-1); <br>varrotateX_right=rotateX_front 90; <br>varrotateY_right=rotateZ_front 90; <br>varrotateZ_right=rotateY_front *(-1); <br>//判断各个면적 z轴显示顺序 <br>var zIndex_front_default = -1; <br>var zIndex_behind_default = -6; <br>var zIndex_top_default = -5; <br>var zIndex_bottom_default = -2; <br>var zIndex_left_default = -4; <br>var zIndex_right_default = -3; <br>var xI = (rotateX_front / 90) % 4; <br>var yI = (rotateY_front / 90) % 4; <br>var zI = (rotateZ_front / 90) % 4; <br>var zIndex_matrix = new Array(); <br>for(var i = 0; i < 3;i ) { <br />zIndex_matrix.push(new Array()); <br />} <br />zIndex_matrix = [["","zIndex_top",""], <br />["zIndex_left","zIndex_front","zIndex_right"], <br />["","zIndex_bottom", ""]]; <br />var zIndex_matrix_behind = "zIndex_behind"; <br />//计算zIndex <br />if((xI >= 0 && xI < 1) ||(xI >= -4 && xI < -3)) { <br />} else if(( xI >= 1 && xI < 2) ||(xI >= -3 && xI < -2)) { <br />var zIndex_matrix_tmp = zIndex_matrix[0][1]; <br />zIndex_matrix[0][1] = zIndex_matrix[1][1]; <br />zIndex_matrix[1][1] = zIndex_matrix[1][2]; <br />zIndex_matrix[1][2] = zIndex_matrix_behind; <br />zIndex_matrix_behind = zIndex_matrix_tmp; <br />} else if((xI >= 2 && xI < 3) ||(xI >= -2 && xI < -1)) { <br />var zIndex_matrix_tmp = zIndex_matrix[0][1] ; <br />zIndex_matrix[0][1] = zIndex_matrix[2][1]; <br />zIndex_matrix[2][1] = zIndex_matrix_tmp; <br />zIndex_matrix_tmp = zIndex_matrix[1][1]; <br />zIndex_matrix[1][1] = zIndex_matrix_behind; <br />zIndex_matrix_behind = zIndex_matrix_tmp; <br />} else if((xI >= 3 && xI < 4) ||(xI >= -1 && xI < 0)) { <br />var zIndex_matrix_tmp = zIndex_matrix[0][1]; <br />zIndex_matrix[0][1] = zIndex_matrix_behind; <br />zIndex_matrix_behind = zIndex_matrix[2][1]; <br />zIndex_matrix[2][1] = zIndex_matrix[1][1]; <br />zIndex_matrix[1][1] = zIndex_matrix_tmp; <br />} <br />if((yI > 0 && yI <= 1) ||(yI > -4 && yI <= -3)) { <br />var zIndex_matrix_tmp = zIndex_matrix[1][ 0]; <br />zIndex_matrix[1][0] = zIndex_matrix_behind; <br />zIndex_matrix_behind = zIndex_matrix[1][2]; <br />zIndex_matrix[1][2] = zIndex_matrix[1][1]; <br />zIndex_matrix[1][1] = zIndex_matrix_tmp; <br />} else if((yI > 1 && yI <= 2) ||(yI > -3 && yI <= -2)) { <br />var zIndex_matrix_tmp = zIndex_matrix[1][0] ; <br />zIndex_matrix[1][0] = zIndex_matrix[1][2]; <br />zIndex_matrix[1][2] = zIndex_matrix_tmp; <br />zIndex_matrix_tmp = zIndex_matrix[1][1]; <br />zIndex_matrix[1][1] = zIndex_matrix_behind; <br />zIndex_matrix_behind = zIndex_matrix_tmp; <br />} else if((yI > 2 && yI <= 3) ||(yI > -2 && yI <= -1)) { <br />var zIndex_matrix_tmp = zIndex_matrix[1][0] ; <br />zIndex_matrix[1][0] = zIndex_matrix[1][1]; <br />zIndex_matrix[1][1] = zIndex_matrix[1][2]; <br />zIndex_matrix[1][2] = zIndex_matrix_behind; <br />zIndex_matrix_behind = zIndex_matrix_tmp; <br />} else if((yI > 3 && yI <= 4) ||(yI > -1 && yI <= 0)) { <br />} <br /><br />if((zI > 0 && zI <= 1) ||(zI > -4 && zI <= -3)) { <br />var zIndex_matrix_tmp = zIndex_matrix[0][1]; <br />zIndex_matrix[0][1] = zIndex_matrix[1][0]; <br />zIndex_matrix[1][0] = zIndex_matrix[2][1]; <br />zIndex_matrix[2][1] = zIndex_matrix[1][2]; <br />zIndex_matrix[1][2] = zIndex_matrix_tmp; <br />} else if((zI > 1 && zI <= 2) ||(zI > -3 && zI <= -2)) { <br />var zIndex_matrix_tmp = zIndex_matrix[0][1] ; <br />zIndex_matrix[0][1] = zIndex_matrix[2][1]; <br />zIndex_matrix[2][1] = zIndex_matrix_tmp; <br />zIndex_matrix_tmp = zIndex_matrix[1][0]; <br />zIndex_matrix[1][0] = zIndex_matrix[1][2]; <br />zIndex_matrix[1][2] = zIndex_matrix_tmp; <br />} else if((zI > 2 && zI <= 3) ||(zI > -2 && zI <= -1)) { <br />var zIndex_matrix_tmp = zIndex_matrix[0][1] ; <br />zIndex_matrix[0][1] = zIndex_matrix[1][2]; <br />zIndex_matrix[1][2] = zIndex_matrix[2][1]; <br />zIndex_matrix[2][1] = zIndex_matrix[1][0]; <br />zIndex_matrix[1][0] = zIndex_matrix_tmp; <br />} else if((zI > 3 && zI <= 4) ||(zI > -1 && zI <= 0)) { <br />} <br />//赋值zIndex <br /> eval(zIndex_matrix[0][1] "=" zIndex_top_default); <br />eval(zIndex_matrix[1][0] "=" zIndex_left_default); <br />eval(zIndex_matrix[1][1] "=" zIndex_front_default); <br />eval(zIndex_matrix[1][2] "=" zIndex_right_default); <br />eval(zIndex_matrix[2][1] "=" zIndex_bottom_default); <br />eval(zIndex_matrix_behind "=" zIndex_behind_default); <br />//front <br />varTransform_rotate_front = "perspective(500px)rotateX("rotateX_front <br />"deg)rotateY("rotateY_front<br />"deg)rotateZ("rotateZ_front "deg)"; <br />div_front.style.webkitTransform = 변환_회전_전면; <br />div_front.style.zIndex = zIndex_front; <br />//뒤 <br />varTransform_rotate_behind = "원근(500px)rotateX("rotateX_behind<br />"deg)rotateY("rotateY_behind<br />"deg)rotateZ("rotateZ_behind "deg)"; <br />div_behind.style.webkitTransform =Transform_rotate_behind; <br />div_behind.style.zIndex = zIndex_behind; <br />//left <br />varTransform_rotate_left = "perspective(500px)rotateX("rotateX_left <br />"deg)rotateZ("rotateZ_left<br />"deg)rotateY("rotateY_left "deg)"; <br />div_left.style.webkitTransform = 변환_회전_왼쪽; <br />div_left.style.zIndex = zIndex_left; <br />//right <br />varTransform_rotate_right = "perspective(500px)rotateX("rotateX_right<br />"deg)rotateZ("rotateZ_right<br />"deg)rotateY("rotateY_right "deg)"; <br />div_right.style.webkitTransform = 변환_회전_오른쪽; <br />div_right.style.zIndex = zIndex_right; <br />//top <br />varTransform_rotate_top = "perspective(500px)rotateX("rotateX_top <br />"deg)rotateZ("rotateZ_top<br />"deg)rotateY("rotateY_top "deg)"; <br />div_top.style.webkitTransform =Transform_rotate_top; <br />div_top.style.zIndex = zIndex_top; <br />//bottom <br />varTransform_rotate_bottom = "perspective(500px)rotateX("rotateX_bottom <br />"deg)rotateZ("rotateZ_bottom<br />"deg)rotateY("rotateY_bottom "deg)"; <br />div_bottom.style.webkitTransform =Transform_rotate_bottom; <br />div_bottom.style.zIndex = zIndex_bottom; <br />}; <br />//중요한 방법, 宽, 高 <br />this.resize = function(new_long, new_width, new_height) <br />{ <br />long = new_long; <br />너비 = new_width; <br />높이 = new_height; <br />this.refresh(); <br />}; <br />//중요한 투자 방식 <br />this.move = function(new_left,new_top) { <br />top = new_top; <br />왼쪽 = new_left; <br />this.refresh(); <br />}; <br />} <br /><br />함수 변환() { <br />cuboid.resize(parseInt(document.getElementById("long").value), <br />parseInt(document.getElementById("width"). 값), <br />parseInt(document.getElementById("height").value)); <br />cuboid.move(parseInt(document.getElementById("left").value), <br />parseInt(document.getElementById("top").value)); <br />cuboid.rotate(parseInt(document.getElementById("rotatex").value), <br />parseInt(document.getElementById("rotatey").value), <br />parseInt(document.getElementById("rotatez" ).값)); <br />//cuboid.refresh(); <br />} <br /></script>
왼쪽:px
상단:< ;/input>px
긴:px
너비:px
높이:px
rotateX: deg
rotateY: deg
rotateZ: deg
<스크립트>
var cuboid = new cuboidModel(parseInt(document.getElementById("left").value),
parseInt(document.getElementById("top").value),
parseInt(document.getElementById(" long").value),
parseInt(document.getElementById("width").value),
parseInt(document.getElementById("height").value));
cuboid.init();