Skip to content

Instantly share code, notes, and snippets.

@MisterFixx
Created April 10, 2019 12:03
Show Gist options
  • Save MisterFixx/67e99bf2679029bca955c0020fa780c3 to your computer and use it in GitHub Desktop.
Save MisterFixx/67e99bf2679029bca955c0020fa780c3 to your computer and use it in GitHub Desktop.
<?php
class render3DPlayer{
public function __construct($src, $displayHair, $headOnly, $ratio) {
$this->playerSkin = $src;
$this->head_only = ($headOnly == 'true');
$this->display_hair = ($displayHair != 'false');
$this->ratio = $ratio;
}
public function get3DRender() {
$this->hd_ratio = imagesx($this->playerSkin) / 64;
$this->isNewSkinType = imagesx($this->playerSkin) == imagesy($this->playerSkin) ? 1 : 0;
$this->calculateAngles();
$this->facesDetermination();
$this->generatePolygons();
$this->memberRotation();
$this->createProjectionPlan();
return $this->displayImage();
}
private function calculateAngles() {
global $cos_alpha,$sin_alpha,$cos_omega,$sin_omega,$minX,$maxX,$minY,$maxY;
$this->members_angles['torso'] = ['cos_alpha' => 1, 'sin_alpha' => 0, 'cos_omega' => 1, 'sin_omega' => 0];
$this->members_angles['head'] = ['cos_alpha' => 1, 'sin_alpha' => 0, 'cos_omega' => 1, 'sin_omega' => 0];
$this->members_angles['rightArm'] = ['cos_alpha' => 1, 'sin_alpha' => 0, 'cos_omega' => 1, 'sin_omega' => 0];
$this->members_angles['leftArm'] = ['cos_alpha' => 1, 'sin_alpha' => 0, 'cos_omega' => 1, 'sin_omega' => 0];
$this->members_angles['rightLeg'] = ['cos_alpha' => 1, 'sin_alpha' => 0, 'cos_omega' => 1, 'sin_omega' => 0];
$this->members_angles['leftLeg'] = ['cos_alpha' => 1, 'sin_alpha' => 0, 'cos_omega' => 1, 'sin_omega' => 0];
$cos_alpha = 0.90630778703665;
$sin_alpha = -0.4226182617407;
$cos_omega = 0.70710678118655;
$sin_omega = 0.70710678118655;
$minX = 0;
$maxX = 0;
$minY = 0;
$maxY = 0;
}
private function facesDetermination() {
$this->visible_faces = ['head' => ['front' => [], 'back' => []], 'torso' => ['front' => [], 'back' => []], 'rightArm' => ['front' => [], 'back' => []], 'leftArm' => ['front' => [], 'back' => []], 'rightLeg' => ['front' => [], 'back' => []], 'leftLeg' => ['front' => [], 'back' => []]];
$this->all_faces = ['back','right','top','front','left','bottom'];
foreach($this->visible_faces as $k => &$v) {
$this->setCubePoints();
foreach($this->cube_points as $cube_point) {
$cube_point[0]->preProject(0, 0, 0, $this->members_angles[$k]['cos_alpha'], $this->members_angles[$k]['sin_alpha'], $this->members_angles[$k]['cos_omega'], $this->members_angles[$k]['sin_omega']);
$cube_point[0]->project();
if(!isset($cube_max_depth_faces)) $cube_max_depth_faces = $cube_point;
else if($cube_max_depth_faces[0]->getDepth() > $cube_point[0]->getDepth()) $cube_max_depth_faces = $cube_point;
}
$v['back'] = $cube_max_depth_faces[1];
$v['front'] = array_diff($this->all_faces, $v['back']);
}
$this->setCubePoints();
foreach($this->cube_points as $cube_point) {
$cube_point[0]->project();
if(!isset($cube_max_depth_faces)) $cube_max_depth_faces = $cube_point;
else if($cube_max_depth_faces[0]->getDepth() > $cube_point[0]->getDepth()) $cube_max_depth_faces = $cube_point;
$this->back_faces = $cube_max_depth_faces[1];
$this->front_faces = array_diff($this->all_faces, $this->back_faces);
}
}
private function setCubePoints() {
$this->cube_points = [];
$this->cube_points[] = [new Point(['x' => 0, 'y' => 0, 'z' => 0]), ['back', 'right', 'top']];
$this->cube_points[] = [new Point(['x' => 0, 'y' => 0, 'z' => 1]), ['front', 'right', 'top']];
$this->cube_points[] = [new Point(['x' => 0, 'y' => 1, 'z' => 0]), ['back', 'right', 'bottom']];
$this->cube_points[] = [new Point(['x' => 0, 'y' => 1, 'z' => 1]), ['front', 'right', 'bottom']];
$this->cube_points[] = [new Point(['x' => 1, 'y' => 0, 'z' => 0]), ['back', 'left', 'top']];
$this->cube_points[] = [new Point(['x' => 1, 'y' => 0, 'z' => 1]), ['front', 'left', 'top']];
$this->cube_points[] = [new Point(['x' => 1, 'y' => 1, 'z' => 0]), ['back', 'left', 'bottom']];
$this->cube_points[] = [new Point(['x' => 1, 'y' => 1, 'z' => 1]), ['front', 'left', 'bottom']];
}
private function generatePolygons() {
$depths_of_face = [];
$this->polygons = [];
$cube_faces_array = ['front' => [], 'back' => [], 'top' => [], 'bottom' => [], 'right' => [], 'left' => []];
$this->polygons = ['helmet' => $cube_faces_array, 'head' => $cube_faces_array, 'torso' => $cube_faces_array, 'rightArm' => $cube_faces_array, 'leftArm' => $cube_faces_array, 'rightLeg' => $cube_faces_array, 'leftLeg' => $cube_faces_array ];
$hd_ratio = $this->hd_ratio;
$img_png = $this->playerSkin;
for($i = 0; $i < 9 * $hd_ratio; $i++) {
for($j=0 ; $j < 9 * $hd_ratio; $j++) {
if(!isset($volume_points[$i][$j][-2 * $hd_ratio])) {
$volume_points[$i][$j][-2 * $hd_ratio] = new Point(['x'=> $i, 'y' => $j, 'z' => -2 * $hd_ratio]);
}
if(!isset($volume_points[$i][$j][6 * $hd_ratio])) $volume_points[$i][$j][6 * $hd_ratio] = new Point(['x' => $i, 'y' => $j, 'z' => 6 * $hd_ratio]);
}
}
for($j = 0; $j < 9 * $hd_ratio; $j++) {
for($k=- 2 * $hd_ratio; $k < 7 * $hd_ratio; $k++) {
if(!isset($volume_points[0][$j][$k])) $volume_points[0][$j][$k] = new Point(['x'=> 0, 'y' => $j, 'z' => $k]);
if(!isset($volume_points[8 * $hd_ratio][$j][$k])) $volume_points[8 * $hd_ratio][$j][$k] = new Point(['x' => 8 * $hd_ratio, 'y' => $j, 'z' => $k]);
}
}
for($i = 0; $i < 9 * $hd_ratio; $i++) {
for($k=- 2 * $hd_ratio; $k < 7 * $hd_ratio; $k++) {
if(!isset($volume_points[$i][0][$k])) $volume_points[$i][0][$k] = new Point(['x'=> $i, 'y' => 0, 'z' => $k]);
if(!isset($volume_points[$i][8 * $hd_ratio][$k])) $volume_points[$i][8 * $hd_ratio][$k] = new Point(['x' => $i, 'y' => 8 * $hd_ratio, 'z' => $k]);
}
}
for($i = 0; $i < 8 * $hd_ratio; $i++) {
for($j=0 ; $j < 8 * $hd_ratio; $j++) {
$this->polygons['head']['back'][] = new Polygon([$volume_points[$i][$j][-2 * $hd_ratio], $volume_points[$i + 1][$j][-2 * $hd_ratio], $volume_points[$i + 1][$j + 1][-2 * $hd_ratio], $volume_points[$i][$j + 1][-2 * $hd_ratio]], imagecolorat($img_png, (32 * $hd_ratio - 1) - $i, 8 * $hd_ratio + $j));
$this->polygons['head']['front'][] = new Polygon([$volume_points[$i][$j][6 * $hd_ratio], $volume_points[$i + 1][$j][6 * $hd_ratio], $volume_points[$i + 1][$j + 1][6 * $hd_ratio], $volume_points[$i][$j + 1][6 * $hd_ratio]], imagecolorat($img_png, 8 * $hd_ratio + $i, 8 * $hd_ratio + $j));
}
}
for($j = 0; $j < 8 * $hd_ratio; $j++) {
for($k=- 2 * $hd_ratio; $k < 6 * $hd_ratio; $k++) {
$this->polygons['head']['right'][] = new Polygon([$volume_points[0][$j][$k], $volume_points[0][$j][$k + 1], $volume_points[0][$j + 1][$k + 1], $volume_points[0][$j + 1][$k]], imagecolorat($img_png, $k + 2 * $hd_ratio, 8 * $hd_ratio + $j));
$this->polygons['head']['left'][] = new Polygon([$volume_points[8 * $hd_ratio][$j][$k], $volume_points[8 * $hd_ratio][$j][$k + 1], $volume_points[8 * $hd_ratio][$j + 1][$k + 1], $volume_points[8 * $hd_ratio][$j + 1][$k]], imagecolorat($img_png, (24 * $hd_ratio - 1) - $k - 2 * $hd_ratio, 8 * $hd_ratio + $j));
}
}
for($i = 0; $i < 8 * $hd_ratio; $i++) {
for($k=- 2 * $hd_ratio; $k < 6 * $hd_ratio; $k++) {
$this->polygons['head']['top'][] = new Polygon([$volume_points[$i][0][$k], $volume_points[$i + 1][0][$k], $volume_points[$i + 1][0][$k + 1], $volume_points[$i][0][$k + 1]], imagecolorat($img_png, 8 * $hd_ratio + $i, $k + 2 * $hd_ratio));
$this->polygons['head']['bottom'][] = new Polygon([$volume_points[$i][8 * $hd_ratio][$k], $volume_points[$i + 1][8 * $hd_ratio][$k], $volume_points[$i + 1][8 * $hd_ratio][$k + 1], $volume_points[$i][8 * $hd_ratio][$k + 1]], imagecolorat($img_png, 16 * $hd_ratio + $i, 2 * $hd_ratio + $k));
}
}
if($this->display_hair) {
$volume_points = [];
$ho = 0.5;
for($i = 0; $i < 9 * $hd_ratio; $i++) {
for($j=0 ; $j < 9 * $hd_ratio; $j++) {
if(!isset($volume_points[$i][$j][-2 * $hd_ratio])) $volume_points[$i][$j][-2 * $hd_ratio] = new Point(['x'=> $i * 9 / 8 - $ho * $hd_ratio, 'y' => $j * 9 / 8 - $ho * $hd_ratio, 'z' => -2.5 * $hd_ratio]);
if(!isset($volume_points[$i][$j][6 * $hd_ratio])) $volume_points[$i][$j][6 * $hd_ratio] = new Point(['x' => $i * 9 / 8 - $ho * $hd_ratio, 'y' => $j * 9 / 8 - $ho * $hd_ratio, 'z' => 6.5 * $hd_ratio]);
}
}
for($j = 0; $j < 9 * $hd_ratio; $j++) {
for($k=- 2 * $hd_ratio; $k < 7 * $hd_ratio; $k++) {
if(!isset($volume_points[0][$j][$k])) $volume_points[0][$j][$k] = new Point(['x'=> - $ho * $hd_ratio, 'y' => $j * 9 / 8 - $ho * $hd_ratio, 'z' => $k * 9 / 8 - $ho * $hd_ratio]);
if(!isset($volume_points[8 * $hd_ratio][$j][$k])) $volume_points[8 * $hd_ratio][$j][$k] = new Point(['x' => 8.5 * $hd_ratio, 'y' => $j * 9 / 8 - $ho * $hd_ratio, 'z' => $k * 9 / 8 - $ho * $hd_ratio]);
}
}
for($i = 0; $i < 9 * $hd_ratio; $i++) {
for($k=- 2 * $hd_ratio; $k < 7 * $hd_ratio; $k++) {
if(!isset($volume_points[$i][0][$k])) $volume_points[$i][0][$k] = new Point(['x'=> $i * 9 / 8 - $ho * $hd_ratio, 'y' => - $ho * $hd_ratio, 'z' => $k * 9 / 8 - $ho * $hd_ratio]);
if(!isset($volume_points[$i][8 * $hd_ratio][$k])) $volume_points[$i][8 * $hd_ratio][$k] = new Point(['x' => $i * 9 / 8 - $ho * $hd_ratio, 'y' => 8.5 * $hd_ratio, 'z' => $k * 9 / 8 - $ho * $hd_ratio]);
}
}
for($i = 0; $i < 8 * $hd_ratio; $i++) {
for($j=0 ; $j < 8 * $hd_ratio; $j++) {
$this->polygons['helmet']['back'][] = new Polygon([$volume_points[$i][$j][-2 * $hd_ratio], $volume_points[$i + 1][$j][-2 * $hd_ratio], $volume_points[$i + 1][$j + 1][-2 * $hd_ratio], $volume_points[$i][$j + 1][-2 * $hd_ratio]], imagecolorat($img_png, 32 * $hd_ratio + (32 * $hd_ratio - 1) - $i, 8 * $hd_ratio + $j));
$this->polygons['helmet']['front'][] = new Polygon([$volume_points[$i][$j][6 * $hd_ratio], $volume_points[$i + 1][$j][6 * $hd_ratio], $volume_points[$i + 1][$j + 1][6 * $hd_ratio], $volume_points[$i][$j + 1][6 * $hd_ratio]], imagecolorat($img_png, 32 * $hd_ratio + 8 * $hd_ratio + $i, 8 * $hd_ratio + $j));
}
}
for($j = 0; $j < 8 * $hd_ratio; $j++) {
for($k=- 2 * $hd_ratio; $k < 6 * $hd_ratio; $k++) {
$this->polygons['helmet']['right'][] = new Polygon([$volume_points[0][$j][$k], $volume_points[0][$j][$k + 1], $volume_points[0][$j + 1][$k + 1], $volume_points[0][$j + 1][$k]], imagecolorat($img_png, 32 * $hd_ratio + $k + 2 * $hd_ratio, 8 * $hd_ratio + $j));
$this->polygons['helmet']['left'][] = new Polygon([$volume_points[8 * $hd_ratio][$j][$k], $volume_points[8 * $hd_ratio][$j][$k + 1], $volume_points[8 * $hd_ratio][$j + 1][$k + 1], $volume_points[8 * $hd_ratio][$j + 1][$k]], imagecolorat($img_png, 32 * $hd_ratio + (24 * $hd_ratio - 1) - $k - 2 * $hd_ratio, 8 * $hd_ratio + $j));
}
}
for($i = 0; $i < 8 * $hd_ratio; $i++) {
for($k=- 2 * $hd_ratio; $k < 6 * $hd_ratio; $k++) {
$this->polygons['helmet']['top'][] = new Polygon([$volume_points[$i][0][$k], $volume_points[$i + 1][0][$k], $volume_points[$i + 1][0][$k + 1], $volume_points[$i][0][$k + 1]], imagecolorat($img_png, 32 * $hd_ratio + 8 * $hd_ratio + $i, $k + 2 * $hd_ratio));
$this->polygons['helmet']['bottom'][] = new Polygon([$volume_points[$i][8 * $hd_ratio][$k], $volume_points[$i + 1][8 * $hd_ratio][$k], $volume_points[$i + 1][8 * $hd_ratio][$k + 1], $volume_points[$i][8 * $hd_ratio][$k + 1]], imagecolorat($img_png, 32 * $hd_ratio + 16 * $hd_ratio + $i, 2 * $hd_ratio + $k));
}
}
}
if(!$this->head_only) {
$volume_points = [];
for($i = 0; $i < 9 * $hd_ratio; $i++) {
for($j=0 ; $j < 13 * $hd_ratio; $j++) {
if(!isset($volume_points[$i][$j][0])) $volume_points[$i][$j][0] = new Point(['x'=> $i, 'y' => $j + 8 * $hd_ratio, 'z' => 0]);
if(!isset($volume_points[$i][$j][4 * $hd_ratio])) $volume_points[$i][$j][4 * $hd_ratio] = new Point(['x' => $i, 'y' => $j + 8 * $hd_ratio, 'z' => 4 * $hd_ratio]);
}
}
for($j = 0; $j < 13 * $hd_ratio; $j++) {
for($k=0 ; $k < 5 * $hd_ratio; $k++) {
if(!isset($volume_points[0][$j][$k])) $volume_points[0][$j][$k] = new Point(['x'=> 0, 'y' => $j + 8 * $hd_ratio, 'z' => $k]);
if(!isset($volume_points[8 * $hd_ratio][$j][$k])) $volume_points[8 * $hd_ratio][$j][$k] = new Point(['x' => 8 * $hd_ratio, 'y' => $j + 8 * $hd_ratio, 'z' => $k]);
}
}
for($i = 0; $i < 9 * $hd_ratio; $i++) {
for($k=0 ; $k < 5 * $hd_ratio; $k++) {
if(!isset($volume_points[$i][0][$k])) $volume_points[$i][0][$k] = new Point(['x'=> $i, 'y' => 0 + 8 * $hd_ratio, 'z' => $k]);
if(!isset($volume_points[$i][12 * $hd_ratio][$k])) $volume_points[$i][12 * $hd_ratio][$k] = new Point(['x' => $i, 'y' => 12 * $hd_ratio + 8 * $hd_ratio, 'z' => $k]);
}
}
for($i = 0; $i < 8 * $hd_ratio; $i++) {
for($j=0 ; $j < 12 * $hd_ratio; $j++) {
$this->polygons['torso']['back'][] = new Polygon([$volume_points[$i][$j][0], $volume_points[$i + 1][$j][0], $volume_points[$i + 1][$j + 1][0], $volume_points[$i][$j + 1][0]], imagecolorat($img_png, (40 * $hd_ratio - 1) - $i, 20 * $hd_ratio + $j));
$this->polygons['torso']['front'][] = new Polygon([$volume_points[$i][$j][4 * $hd_ratio], $volume_points[$i + 1][$j][4 * $hd_ratio], $volume_points[$i + 1][$j + 1][4 * $hd_ratio], $volume_points[$i][$j + 1][4 * $hd_ratio]], imagecolorat($img_png, 20 * $hd_ratio + $i, 20 * $hd_ratio + $j));
}
}
for($j = 0; $j < 12 * $hd_ratio; $j++) {
for($k=0 ; $k < 4 * $hd_ratio; $k++) {
$this->polygons['torso']['right'][] = new Polygon([$volume_points[0][$j][$k], $volume_points[0][$j][$k + 1], $volume_points[0][$j + 1][$k + 1], $volume_points[0][$j + 1][$k]], imagecolorat($img_png, 16 * $hd_ratio + $k, 20 * $hd_ratio + $j));
$this->polygons['torso']['left'][] = new Polygon([$volume_points[8 * $hd_ratio][$j][$k], $volume_points[8 * $hd_ratio][$j][$k + 1], $volume_points[8 * $hd_ratio][$j + 1][$k + 1], $volume_points[8 * $hd_ratio][$j + 1][$k]], imagecolorat($img_png, (32 * $hd_ratio - 1) - $k, 20 * $hd_ratio + $j));
}
}
for($i = 0; $i < 8 * $hd_ratio; $i++) {
for($k=0 ; $k < 4 * $hd_ratio; $k++) {
$this->polygons['torso']['top'][] = new Polygon([$volume_points[$i][0][$k], $volume_points[$i + 1][0][$k], $volume_points[$i + 1][0][$k + 1], $volume_points[$i][0][$k + 1]], imagecolorat($img_png, 20 * $hd_ratio + $i, 16 * $hd_ratio + $k));
$this->polygons['torso']['bottom'][] = new Polygon([$volume_points[$i][12 * $hd_ratio][$k], $volume_points[$i + 1][12 * $hd_ratio][$k], $volume_points[$i + 1][12 * $hd_ratio][$k + 1], $volume_points[$i][12 * $hd_ratio][$k + 1]], imagecolorat($img_png, 28 * $hd_ratio + $i, (20 * $hd_ratio - 1) - $k));
}
}
$volume_points = [];
for($i = 0; $i < 9 * $hd_ratio; $i++) {
for($j=0 ; $j < 13 * $hd_ratio; $j++) {
if(!isset($volume_points[$i][$j][0])) $volume_points[$i][$j][0] = new Point(['x'=> $i - 4 * $hd_ratio, 'y' => $j + 8 * $hd_ratio, 'z' => 0 ]);
if(!isset($volume_points[$i][$j][4 * $hd_ratio])) $volume_points[$i][$j][4 * $hd_ratio] = new Point(['x' => $i - 4 * $hd_ratio, 'y' => $j + 8 * $hd_ratio, 'z' => 4 * $hd_ratio]);
}
}
for($j = 0; $j < 13 * $hd_ratio; $j++) {
for($k=0 ; $k < 5 * $hd_ratio; $k++) {
if(!isset($volume_points[0][$j][$k])) $volume_points[0][$j][$k] = new Point(['x'=> 0 - 4 * $hd_ratio, 'y' => $j + 8 * $hd_ratio, 'z' => $k]);
if(!isset($volume_points[8 * $hd_ratio][$j][$k])) $volume_points[4 * $hd_ratio][$j][$k] = new Point(['x' => 4 * $hd_ratio - 4 * $hd_ratio, 'y' => $j + 8 * $hd_ratio, 'z' => $k]);
}
}
for($i = 0; $i < 9 * $hd_ratio; $i++) {
for($k=0 ; $k < 5 * $hd_ratio; $k++) {
if(!isset($volume_points[$i][0][$k])) $volume_points[$i][0][$k] = new Point(['x'=> $i - 4 * $hd_ratio, 'y' => 0 + 8 * $hd_ratio, 'z' => $k]);
if(!isset($volume_points[$i][12 * $hd_ratio][$k])) $volume_points[$i][12 * $hd_ratio][$k] = new Point(['x' => $i - 4 * $hd_ratio, 'y' => 12 * $hd_ratio + 8 * $hd_ratio, 'z' => $k]);
}
}
for($i = 0; $i < 4 * $hd_ratio; $i++) {
for($j=0 ; $j < 12 * $hd_ratio; $j++) {
$this->polygons['rightArm']['back'][] = new Polygon([$volume_points[$i][$j][0], $volume_points[$i + 1][$j][0], $volume_points[$i + 1][$j + 1][0], $volume_points[$i][$j + 1][0]], imagecolorat($img_png, (56 * $hd_ratio - 1) - $i, 20 * $hd_ratio + $j));
$this->polygons['rightArm']['front'][] = new Polygon([$volume_points[$i][$j][4 * $hd_ratio], $volume_points[$i + 1][$j][4 * $hd_ratio], $volume_points[$i + 1][$j + 1][4 * $hd_ratio], $volume_points[$i][$j + 1][4 * $hd_ratio]], imagecolorat($img_png, 44 * $hd_ratio + $i, 20 * $hd_ratio + $j));
}
}
for($j = 0; $j < 12 * $hd_ratio; $j++) {
for($k=0 ; $k < 4 * $hd_ratio; $k++) {
$this->polygons['rightArm']['right'][] = new Polygon([$volume_points[0][$j][$k], $volume_points[0][$j][$k + 1], $volume_points[0][$j + 1][$k + 1], $volume_points[0][$j + 1][$k]], imagecolorat($img_png, 40 * $hd_ratio + $k, 20 * $hd_ratio + $j));
$this->polygons['rightArm']['left'][] = new Polygon([$volume_points[4 * $hd_ratio][$j][$k], $volume_points[4 * $hd_ratio][$j][$k + 1], $volume_points[4 * $hd_ratio][$j + 1][$k + 1], $volume_points[4 * $hd_ratio][$j + 1][$k]], imagecolorat($img_png, (52 * $hd_ratio - 1) - $k, 20 * $hd_ratio + $j));
}
}
for($i = 0; $i < 4 * $hd_ratio; $i++) {
for($k=0 ; $k < 4 * $hd_ratio; $k++) {
$this->polygons['rightArm']['top'][] = new Polygon([$volume_points[$i][0][$k], $volume_points[$i + 1][0][$k], $volume_points[$i + 1][0][$k + 1], $volume_points[$i][0][$k + 1]], imagecolorat($img_png, 44 * $hd_ratio + $i, 16 * $hd_ratio + $k));
$this->polygons['rightArm']['bottom'][] = new Polygon([$volume_points[$i][12 * $hd_ratio][$k], $volume_points[$i + 1][12 * $hd_ratio][$k], $volume_points[$i + 1][12 * $hd_ratio][$k + 1], $volume_points[$i][12 * $hd_ratio][$k + 1]], imagecolorat($img_png, 48 * $hd_ratio + $i, 16 * $hd_ratio + $k));
}
}
$volume_points = [];
for($i = 0; $i < 9 * $hd_ratio; $i++) {
for($j=0 ; $j < 13 * $hd_ratio; $j++) {
if(!isset($volume_points[$i][$j][0])) $volume_points[$i][$j][0] = new Point(['x'=> $i + 8 * $hd_ratio, 'y' => $j + 8 * $hd_ratio, 'z' => 0]);
if(!isset($volume_points[$i][$j][4 * $hd_ratio])) $volume_points[$i][$j][4 * $hd_ratio] = new Point(['x' => $i + 8 * $hd_ratio, 'y' => $j + 8 * $hd_ratio, 'z' => 4 * $hd_ratio]);
}
}
for($j = 0; $j < 13 * $hd_ratio; $j++) {
for($k=0 ; $k < 5 * $hd_ratio; $k++) {
if(!isset($volume_points[0][$j][$k])) $volume_points[0][$j][$k] = new Point(['x'=> 0 + 8 * $hd_ratio, 'y' => $j + 8 * $hd_ratio, 'z' => $k]);
if(!isset($volume_points[8 * $hd_ratio][$j][$k])) $volume_points[4 * $hd_ratio][$j][$k] = new Point(['x' => 4 * $hd_ratio + 8 * $hd_ratio, 'y' => $j + 8 * $hd_ratio, 'z' => $k]);
}
}
for($i = 0; $i < 9 * $hd_ratio; $i++) {
for($k=0 ; $k < 5 * $hd_ratio; $k++) {
if(!isset($volume_points[$i][0][$k])) $volume_points[$i][0][$k] = new Point(['x'=> $i + 8 * $hd_ratio, 'y' => 0 + 8 * $hd_ratio, 'z' => $k]);
if(!isset($volume_points[$i][12 * $hd_ratio][$k])) $volume_points[$i][12 * $hd_ratio][$k] = new Point(['x' => $i + 8 * $hd_ratio, 'y' => 12 * $hd_ratio + 8 * $hd_ratio, 'z' => $k]);
}
}
for($i = 0; $i < 4 * $hd_ratio; $i++) {
for($j=0 ; $j < 12 * $hd_ratio; $j++) {
if($this->isNewSkinType) {
$color1 = imagecolorat($img_png, 47 * $hd_ratio - $i, 52 * $hd_ratio + $j);
$color2 = imagecolorat($img_png, 36 * $hd_ratio + $i, 52 * $hd_ratio + $j);
} else {
$color1 = imagecolorat($img_png, (56 * $hd_ratio - 1) - ((4 * $hd_ratio - 1) - $i), 20 * $hd_ratio + $j);
$color2 = imagecolorat($img_png, 44 * $hd_ratio + ((4 * $hd_ratio - 1) - $i), 20 * $hd_ratio + $j);
}
$this->polygons['leftArm']['back'][] = new Polygon([$volume_points[$i][$j][0], $volume_points[$i + 1][$j][0], $volume_points[$i + 1][$j + 1][0], $volume_points[$i][$j + 1][0]], $color1);
$this->polygons['leftArm']['front'][] = new Polygon([$volume_points[$i][$j][4 * $hd_ratio], $volume_points[$i + 1][$j][4 * $hd_ratio], $volume_points[$i + 1][$j + 1][4 * $hd_ratio], $volume_points[$i][$j + 1][4 * $hd_ratio]], $color2);
}
}
for($j = 0; $j < 12 * $hd_ratio; $j++) {
for($k=0 ; $k < 4 * $hd_ratio; $k++) {
if($this->isNewSkinType) {
$color1 = imagecolorat($img_png, 32 * $hd_ratio + $k, 52 * $hd_ratio + $j);
$color2 = imagecolorat($img_png, 43 * $hd_ratio - $k, 52 * $hd_ratio + $j);
} else {
$color1 = imagecolorat($img_png, 40 * $hd_ratio + ((4 * $hd_ratio - 1) - $k), 20 * $hd_ratio + $j);
$color2 = imagecolorat($img_png, (52 * $hd_ratio - 1) - ((4 * $hd_ratio - 1) - $k), 20 * $hd_ratio + $j);
}
$this->polygons['leftArm']['right'][] = new Polygon([$volume_points[0][$j][$k], $volume_points[0][$j][$k + 1], $volume_points[0][$j + 1][$k + 1], $volume_points[0][$j + 1][$k]], $color1);
$this->polygons['leftArm']['left'][] = new Polygon([$volume_points[4 * $hd_ratio][$j][$k], $volume_points[4 * $hd_ratio][$j][$k + 1], $volume_points[4 * $hd_ratio][$j + 1][$k + 1], $volume_points[4 * $hd_ratio][$j + 1][$k]], $color2);
}
}
for($i = 0; $i < 4 * $hd_ratio; $i++) {
for($k=0 ; $k < 4 * $hd_ratio; $k++) {
if($this->isNewSkinType) {
$color1 = imagecolorat($img_png, 36 * $hd_ratio + $i, 48 * $hd_ratio + $k);
$color2 = imagecolorat($img_png, 40 * $hd_ratio + $i, 48 * $hd_ratio + $k);
} else {
$color1 = imagecolorat($img_png, 44 * $hd_ratio + ((4 * $hd_ratio - 1) - $i), 16 * $hd_ratio + $k);
$color2 = imagecolorat($img_png, 48 * $hd_ratio + ((4 * $hd_ratio - 1) - $i), (20 * $hd_ratio - 1) - $k);
}
$this->polygons['leftArm']['top'][] = new Polygon([$volume_points[$i][0][$k], $volume_points[$i + 1][0][$k], $volume_points[$i + 1][0][$k + 1], $volume_points[$i][0][$k + 1]], $color1);
$this->polygons['leftArm']['bottom'][] = new Polygon([$volume_points[$i][12 * $hd_ratio][$k], $volume_points[$i + 1][12 * $hd_ratio][$k], $volume_points[$i + 1][12 * $hd_ratio][$k + 1], $volume_points[$i][12 * $hd_ratio][$k + 1]], $color2);
}
}
$volume_points = [];
for($i = 0; $i < 9 * $hd_ratio; $i++) {
for($j=0 ; $j < 13 * $hd_ratio; $j++) {
if(!isset($volume_points[$i][$j][0])) $volume_points[$i][$j][0] = new Point(['x'=> $i, 'y' => $j + 20 * $hd_ratio, 'z' => 0]);
if(!isset($volume_points[$i][$j][4 * $hd_ratio])) $volume_points[$i][$j][4 * $hd_ratio] = new Point(['x' => $i, 'y' => $j + 20 * $hd_ratio, 'z' => 4 * $hd_ratio]);
}
}
for($j = 0; $j < 13 * $hd_ratio; $j++) {
for($k=0 ; $k < 5 * $hd_ratio; $k++) {
if(!isset($volume_points[0][$j][$k])) $volume_points[0][$j][$k] = new Point(['x'=> 0, 'y' => $j + 20 * $hd_ratio, 'z' => $k]);
if(!isset($volume_points[8 * $hd_ratio][$j][$k])) $volume_points[4 * $hd_ratio][$j][$k] = new Point(['x' => 4 * $hd_ratio, 'y' => $j + 20 * $hd_ratio, 'z' => $k]);
}
}
for($i = 0; $i < 9 * $hd_ratio; $i++) {
for($k=0 ; $k < 5 * $hd_ratio; $k++) {
if(!isset($volume_points[$i][0][$k])) $volume_points[$i][0][$k] = new Point(['x'=> $i, 'y' => 0 + 20 * $hd_ratio, 'z' => $k]);
if(!isset($volume_points[$i][12 * $hd_ratio][$k])) $volume_points[$i][12 * $hd_ratio][$k] = new Point(['x' => $i, 'y' => 12 * $hd_ratio + 20 * $hd_ratio, 'z' => $k]);
}
}
for($i = 0; $i < 4 * $hd_ratio; $i++) {
for($j=0 ; $j < 12 * $hd_ratio; $j++) {
$this->polygons['rightLeg']['back'][] = new Polygon([$volume_points[$i][$j][0], $volume_points[$i + 1][$j][0], $volume_points[$i + 1][$j + 1][0], $volume_points[$i][$j + 1][0]], imagecolorat($img_png, (16 * $hd_ratio - 1) - $i, 20 * $hd_ratio + $j));
$this->polygons['rightLeg']['front'][] = new Polygon([$volume_points[$i][$j][4 * $hd_ratio], $volume_points[$i + 1][$j][4 * $hd_ratio], $volume_points[$i + 1][$j + 1][4 * $hd_ratio], $volume_points[$i][$j + 1][4 * $hd_ratio]], imagecolorat($img_png, 4 * $hd_ratio + $i, 20 * $hd_ratio + $j));
}
}
for($j = 0; $j < 12 * $hd_ratio; $j++) {
for($k=0 ; $k < 4 * $hd_ratio; $k++) {
$this->polygons['rightLeg']['right'][] = new Polygon([$volume_points[0][$j][$k], $volume_points[0][$j][$k + 1], $volume_points[0][$j + 1][$k + 1], $volume_points[0][$j + 1][$k]], imagecolorat($img_png, 0 + $k, 20 * $hd_ratio + $j));
$this->polygons['rightLeg']['left'][] = new Polygon([$volume_points[4 * $hd_ratio][$j][$k], $volume_points[4 * $hd_ratio][$j][$k + 1], $volume_points[4 * $hd_ratio][$j + 1][$k + 1], $volume_points[4 * $hd_ratio][$j + 1][$k]], imagecolorat($img_png, (12 * $hd_ratio - 1) - $k, 20 * $hd_ratio + $j));
}
}
for($i = 0; $i < 4 * $hd_ratio; $i++) {
for($k=0 ; $k < 4 * $hd_ratio; $k++) {
$this->polygons['rightLeg']['top'][] = new Polygon([$volume_points[$i][0][$k], $volume_points[$i + 1][0][$k], $volume_points[$i + 1][0][$k + 1], $volume_points[$i][0][$k + 1]], imagecolorat($img_png, 4 * $hd_ratio + $i, 16 * $hd_ratio + $k));
$this->polygons['rightLeg']['bottom'][] = new Polygon([$volume_points[$i][12 * $hd_ratio][$k], $volume_points[$i + 1][12 * $hd_ratio][$k], $volume_points[$i + 1][12 * $hd_ratio][$k + 1], $volume_points[$i][12 * $hd_ratio][$k + 1]], imagecolorat($img_png, 8 * $hd_ratio + $i, 16 * $hd_ratio + $k));
}
}
$volume_points = [];
for($i = 0; $i < 9 * $hd_ratio; $i++) {
for($j=0 ; $j < 13 * $hd_ratio; $j++) {
if(!isset($volume_points[$i][$j][0])) $volume_points[$i][$j][0] = new Point(['x'=> $i + 4 * $hd_ratio, 'y' => $j + 20 * $hd_ratio, 'z' => 0]);
if(!isset($volume_points[$i][$j][4 * $hd_ratio])) $volume_points[$i][$j][4 * $hd_ratio] = new Point(['x' => $i + 4 * $hd_ratio, 'y' => $j + 20 * $hd_ratio, 'z' => 4 * $hd_ratio]);
}
}
for($j = 0; $j < 13 * $hd_ratio; $j++) {
for($k=0 ; $k < 5 * $hd_ratio; $k++) {
if(!isset($volume_points[0][$j][$k])) $volume_points[0][$j][$k] = new Point(['x'=> 0 + 4 * $hd_ratio, 'y' => $j + 20 * $hd_ratio, 'z' => $k]);
if(!isset($volume_points[8 * $hd_ratio][$j][$k])) $volume_points[4 * $hd_ratio][$j][$k] = new Point(['x' => 4 * $hd_ratio + 4 * $hd_ratio, 'y' => $j + 20 * $hd_ratio, 'z' => $k]);
}
}
for($i = 0; $i < 9 * $hd_ratio; $i++) {
for($k=0 ; $k < 5 * $hd_ratio; $k++) {
if(!isset($volume_points[$i][0][$k])) $volume_points[$i][0][$k] = new Point(['x'=> $i + 4 * $hd_ratio, 'y' => 0 + 20 * $hd_ratio, 'z' => $k]);
if(!isset($volume_points[$i][12 * $hd_ratio][$k])) $volume_points[$i][12 * $hd_ratio][$k] = new Point(['x' => $i + 4 * $hd_ratio, 'y' => 12 * $hd_ratio + 20 * $hd_ratio, 'z' => $k]);
}
}
for($i = 0; $i < 4 * $hd_ratio; $i++) {
for($j=0 ; $j < 12 * $hd_ratio; $j++) {
if($this->isNewSkinType) {
$color1 = imagecolorat($img_png, 31 * $hd_ratio - $i, 52 * $hd_ratio + $j);
$color2 = imagecolorat($img_png, 20 * $hd_ratio + $i, 52 * $hd_ratio + $j);
} else {
$color1 = imagecolorat($img_png, (16 * $hd_ratio - 1) - ((4 * $hd_ratio - 1) - $i), 20 * $hd_ratio + $j);
$color2 = imagecolorat($img_png, 4 * $hd_ratio + ((4 * $hd_ratio - 1) - $i), 20 * $hd_ratio + $j);
}
$this->polygons['leftLeg']['back'][] = new Polygon([$volume_points[$i][$j][0], $volume_points[$i + 1][$j][0], $volume_points[$i + 1][$j + 1][0], $volume_points[$i][$j + 1][0]], $color1);
$this->polygons['leftLeg']['front'][] = new Polygon([$volume_points[$i][$j][4 * $hd_ratio], $volume_points[$i + 1][$j][4 * $hd_ratio], $volume_points[$i + 1][$j + 1][4 * $hd_ratio], $volume_points[$i][$j + 1][4 * $hd_ratio]], $color2);
}
}
for($j = 0; $j < 12 * $hd_ratio; $j++) {
for($k=0 ; $k < 4 * $hd_ratio; $k++) {
if($this->isNewSkinType) {
$color1 = imagecolorat($img_png, 16 * $hd_ratio + $k, 52 * $hd_ratio + $j);
$color2 = imagecolorat($img_png, 27 * $hd_ratio - $k, 52 * $hd_ratio + $j);
} else {
$color1 = imagecolorat($img_png, 0 + ((4 * $hd_ratio - 1) - $k), 20 * $hd_ratio + $j);
$color2 = imagecolorat($img_png, (12 * $hd_ratio - 1) - ((4 * $hd_ratio - 1) - $k), 20 * $hd_ratio + $j);
}
$this->polygons['leftLeg']['right'][] = new Polygon([$volume_points[0][$j][$k], $volume_points[0][$j][$k + 1], $volume_points[0][$j + 1][$k + 1], $volume_points[0][$j + 1][$k]], $color1);
$this->polygons['leftLeg']['left'][] = new Polygon([$volume_points[4 * $hd_ratio][$j][$k], $volume_points[4 * $hd_ratio][$j][$k + 1], $volume_points[4 * $hd_ratio][$j + 1][$k + 1], $volume_points[4 * $hd_ratio][$j + 1][$k]], $color2);
}
}
for($i = 0; $i < 4 * $hd_ratio; $i++) {
for($k=0 ; $k < 4 * $hd_ratio; $k++) {
if($this->isNewSkinType) {
$color1 = imagecolorat($img_png, 20 * $hd_ratio + $i, 48 * $hd_ratio + $k);
$color2 = imagecolorat($img_png, 24 * $hd_ratio + $i, 48 * $hd_ratio + $k);
} else {
$color1 = imagecolorat($img_png, 4 * $hd_ratio + ((4 * $hd_ratio - 1) - $i), 16 * $hd_ratio + $k);
$color2 = imagecolorat($img_png, 8 * $hd_ratio + ((4 * $hd_ratio - 1) - $i), (20 * $hd_ratio - 1) - $k);
}
$this->polygons['leftLeg']['top'][] = new Polygon([$volume_points[$i][0][$k], $volume_points[$i + 1][0][$k], $volume_points[$i + 1][0][$k + 1], $volume_points[$i][0][$k + 1]], $color1);
$this->polygons['leftLeg']['bottom'][] = new Polygon([$volume_points[$i][12 * $hd_ratio][$k], $volume_points[$i + 1][12 * $hd_ratio][$k], $volume_points[$i + 1][12 * $hd_ratio][$k + 1], $volume_points[$i][12 * $hd_ratio][$k + 1]], $color2);
}
}
}
}
private function memberRotation() {
foreach($this->polygons['head'] as $face) {
foreach($face as $poly) {
$poly->preProject(4, 8, 2, $this->members_angles['head']['cos_alpha'], $this->members_angles['head']['sin_alpha'], $this->members_angles['head']['cos_omega'], $this->members_angles['head']['sin_omega']);
}
}
if($this->display_hair) {
foreach($this->polygons['helmet'] as $face) {
foreach($face as $poly) {
$poly->preProject(4, 8, 2, $this->members_angles['head']['cos_alpha'], $this->members_angles['head']['sin_alpha'], $this->members_angles['head']['cos_omega'], $this->members_angles['head']['sin_omega']);
}
}
}
if(!$this->head_only) {
foreach($this->polygons['rightArm'] as $face) {
foreach($face as $poly) {
$poly->preProject(-2, 8, 2, $this->members_angles['rightArm']['cos_alpha'], $this->members_angles['rightArm']['sin_alpha'], $this->members_angles['rightArm']['cos_omega'], $this->members_angles['rightArm']['sin_omega']);
}
}
foreach($this->polygons['leftArm'] as $face) {
foreach($face as $poly) {
$poly->preProject(10, 8, 2, $this->members_angles['leftArm']['cos_alpha'], $this->members_angles['leftArm']['sin_alpha'], $this->members_angles['leftArm']['cos_omega'], $this->members_angles['leftArm']['sin_omega']);
}
}
foreach($this->polygons['rightLeg'] as $face) {
foreach($face as $poly) {
$poly->preProject(2, 20, ($this->members_angles['rightLeg']['sin_alpha'] < 0 ? 0 : 4), $this->members_angles['rightLeg']['cos_alpha'], $this->members_angles['rightLeg']['sin_alpha'], $this->members_angles['rightLeg']['cos_omega'], $this->members_angles['rightLeg']['sin_omega']);
}
}
foreach($this->polygons['leftLeg'] as $face) {
foreach($face as $poly) {
$poly->preProject(6, 20, ($this->members_angles['leftLeg']['sin_alpha'] < 0 ? 0 : 4), $this->members_angles['leftLeg']['cos_alpha'], $this->members_angles['leftLeg']['sin_alpha'], $this->members_angles['leftLeg']['cos_omega'], $this->members_angles['leftLeg']['sin_omega']);
}
}
}
}
private function createProjectionPlan() {
foreach($this->polygons as $piece) {
foreach($piece as $face) {
foreach($face as $poly) {
if(!$poly->isProjected()) $poly->project();
}
}
}
}
private function displayImage() {
global $minX,$maxX,$minY,$maxY;
$aasm = 3;
$width = $maxX - $minX;
$height = $maxY - $minY;
$ratio = $this->ratio;
$ratio = $ratio * $aasm;
$srcWidth = $ratio * $width;
$srcHeight = $ratio * $height;
$realWidth = $srcWidth / $aasm;
$realHeight = $srcHeight / $aasm;
$image = img::createEmptyCanvas($srcWidth, $srcHeight);
$display_order = $this->getDisplayOrder();
foreach($display_order as $pieces) {
foreach($pieces as $piece => $faces) {
foreach($faces as $face) {
foreach($this->polygons[$piece][$face] as $poly) {
$poly->addPngPolygon($image, $minX, $minY, $ratio);
}
}
}
}
$destImage = img::createEmptyCanvas($realWidth, $realHeight);
imagecopyresampled($destImage, $image, 0, 0, 0, 0, $realWidth, $realHeight, $srcWidth, $srcHeight);
return $destImage;
}
private function getDisplayOrder() {
$display_order = [];
if(in_array('top', $this->front_faces)) {
if(in_array('right', $this->front_faces)) {
$display_order[] = ['leftLeg' => $this->back_faces];
$display_order[] = ['leftLeg' => $this->visible_faces['leftLeg']['front']];
$display_order[] = ['rightLeg' => $this->back_faces];
$display_order[] = ['rightLeg' => $this->visible_faces['rightLeg']['front']];
$display_order[] = ['leftArm' => $this->back_faces];
$display_order[] = ['leftArm' => $this->visible_faces['leftArm']['front']];
$display_order[] = ['torso' => $this->back_faces];
$display_order[] = ['torso' => $this->visible_faces['torso']['front']];
$display_order[] = ['rightArm' => $this->back_faces];
$display_order[] = ['rightArm' => $this->visible_faces['rightArm']['front']];
}
else {
$display_order[] = ['rightLeg' => $this->back_faces];
$display_order[] = ['rightLeg' => $this->visible_faces['rightLeg']['front']];
$display_order[] = ['leftLeg' => $this->back_faces];
$display_order[] = ['leftLeg' => $this->visible_faces['leftLeg']['front']];
$display_order[] = ['rightArm' => $this->back_faces];
$display_order[] = ['rightArm' => $this->visible_faces['rightArm']['front']];
$display_order[] = ['torso' => $this->back_faces];
$display_order[] = ['torso' => $this->visible_faces['torso']['front']];
$display_order[] = ['leftArm' => $this->back_faces];
$display_order[] = ['leftArm' => $this->visible_faces['leftArm']['front']];
}
$display_order[] = ['helmet' => $this->back_faces];
$display_order[] = ['head' => $this->back_faces];
$display_order[] = ['head' => $this->visible_faces['head']['front']];
$display_order[] = ['helmet' => $this->visible_faces['head']['front']];
}
else {
$display_order[] = ['helmet' => $this->back_faces];
$display_order[] = ['head' => $this->back_faces];
$display_order[] = ['head' => $this->visible_faces['head']['front']];
$display_order[] = ['helmet' => $this->visible_faces['head']['front']];
if(in_array('right', $this->front_faces)) {
$display_order[] = ['leftArm' => $this->back_faces];
$display_order[] = ['leftArm' => $this->visible_faces['leftArm']['front']];
$display_order[] = ['torso' => $this->back_faces];
$display_order[] = ['torso' => $this->visible_faces['torso']['front']];
$display_order[] = ['rightArm' => $this->back_faces];
$display_order[] = ['rightArm' => $this->visible_faces['rightArm']['front']];
$display_order[] = ['leftLeg' => $this->back_faces];
$display_order[] = ['leftLeg' => $this->visible_faces['leftLeg']['front']];
$display_order[] = ['rightLeg' => $this->back_faces];
$display_order[] = ['rightLeg' => $this->visible_faces['rightLeg']['front']];
}
else {
$display_order[] = ['rightArm' => $this->back_faces];
$display_order[] = ['rightArm' => $this->visible_faces['rightArm']['front']];
$display_order[] = ['torso' => $this->back_faces];
$display_order[] = ['torso' => $this->visible_faces['torso']['front']];
$display_order[] = ['leftArm' => $this->back_faces];
$display_order[] = ['leftArm' => $this->visible_faces['leftArm']['front']];
$display_order[] = ['rightLeg' => $this->back_faces];
$display_order[] = ['rightLeg' => $this->visible_faces['rightLeg']['front']];
$display_order[] = ['leftLeg' => $this->back_faces];
$display_order[] = ['leftLeg' => $this->visible_faces['leftLeg']['front']];
}
}
return $display_order;
}
}
class img {
private function __construct(){}
public static function createEmptyCanvas($w, $h) {
$dst = imagecreatetruecolor($w, $h);
imagesavealpha($dst, 1);
$trans_colour = imagecolorallocatealpha($dst, 255, 255, 255, 127);
imagefill($dst, 0, 0, $trans_colour);
return $dst;
}
public static function convertToTrueColor($img) {
if(imageistruecolor($img)) return $img;
$dst = img::createEmptyCanvas(imagesx($img), imagesy($img));
imagecopy($dst, $img, 0, 0, 0, 0, imagesx($img), imagesy($img));
return $dst;
}
}
class Point {
private $_originCoord;
private $_destCoord = [];
private $_isProjected = 0;
private $_isPreProjected = 0;
public function __construct($originCoord) {
if(is_array($originCoord) && count($originCoord) == 3) $this->_originCoord = ['x' => (isset($originCoord['x']) ? $originCoord['x'] : 0), 'y' => (isset($originCoord['y']) ? $originCoord['y'] : 0), 'z' => (isset($originCoord['z']) ? $originCoord['z'] :0)];
else $this->_originCoord = ['x' => 0, 'y' => 0, 'z' => 0];
}
public function project() {
global $cos_alpha,$sin_alpha,$cos_omega,$sin_omega,$minX,$maxX,$minY,$maxY;
$x = $this->_originCoord['x'];
$y = $this->_originCoord['y'];
$z = $this->_originCoord['z'];
$this->_destCoord['x'] = $x * $cos_omega + $z * $sin_omega;
$this->_destCoord['y'] = $x * $sin_alpha * $sin_omega + $y * $cos_alpha - $z * $sin_alpha * $cos_omega;
$this->_destCoord['z'] = -$x * $cos_alpha * $sin_omega + $y * $sin_alpha + $z * $cos_alpha * $cos_omega;
$this->_isProjected = 1;
$minX = min($minX, $this->_destCoord['x']);
$maxX = max($maxX, $this->_destCoord['x']);
$minY = min($minY, $this->_destCoord['y']);
$maxY = max($maxY, $this->_destCoord['y']);
}
public function preProject($dx, $dy, $dz, $cos_alpha, $sin_alpha, $cos_omega, $sin_omega) {
if(!$this->_isPreProjected) {
$x = $this->_originCoord['x'] - $dx;
$y = $this->_originCoord['y'] - $dy;
$z = $this->_originCoord['z'] - $dz;
$this->_originCoord['x'] = $x * $cos_omega + $z * $sin_omega + $dx;
$this->_originCoord['y'] = $x * $sin_alpha * $sin_omega + $y * $cos_alpha - $z * $sin_alpha * $cos_omega + $dy;
$this->_originCoord['z'] = -$x * $cos_alpha * $sin_omega + $y * $sin_alpha + $z * $cos_alpha * $cos_omega + $dz;
$this->_isPreProjected = 1;
}
}
public function getOriginCoord() {
return $this->_originCoord;
}
public function getDestCoord() {
return $this->_destCoord;
}
public function getDepth() {
if(!$this->_isProjected) $this->project();
return $this->_destCoord['z'];
}
public function isProjected() {
return $this->_isProjected;
}
}
class Polygon {
private $_dots;
private $_colour;
private $_isProjected = 0;
private $_face = 'w';
private $_faceDepth = 0;
public function __construct($dots, $colour) {
$this->_dots = $dots;
$this->_colour = $colour;
$coord_0 = $dots[0]->getOriginCoord();
$coord_1 = $dots[1]->getOriginCoord();
$coord_2 = $dots[2]->getOriginCoord();
if($coord_0['x'] == $coord_1['x'] && $coord_1['x'] == $coord_2['x']) {
$this->_face = 'x';
$this->_faceDepth = $coord_0['x'];
}
else if($coord_0['y'] == $coord_1['y'] && $coord_1['y'] == $coord_2['y']) {
$this->_face = 'y';
$this->_faceDepth = $coord_0['y'];
}
else if($coord_0['z'] == $coord_1['z'] && $coord_1['z'] == $coord_2['z']) {
$this->_face = 'z';
$this->_faceDepth = $coord_0['z'];
}
}
public function addPngPolygon($image, $minX, $minY, $ratio) {
$points_2d = [];
$nb_points = 0;
$r = ($this->_colour >> 16) & 0xFF;
$g = ($this->_colour >> 8) & 0xFF;
$b = $this->_colour & 0xFF;
$vR = (127 - (($this->_colour & 0x7F000000) >> 24)) / 127;
if($vR == 0) return;
$same_plan_x = 1;
$same_plan_y = 1;
foreach($this->_dots as $dot) {
$coord = $dot->getDestCoord();
if(!isset($coord_x)) $coord_x = $coord['x'];
if(!isset($coord_y)) $coord_y = $coord['y'];
if($coord_x != $coord['x']) $same_plan_x = 0;
if($coord_y != $coord['y']) $same_plan_y = 0;
$points_2d[] = ($coord['x'] - $minX) * $ratio;
$points_2d[] = ($coord['y'] - $minY) * $ratio;
$nb_points++;
}
if(!($same_plan_x || $same_plan_y)) {
$colour = imagecolorallocate($image, $r, $g, $b);
imagefilledpolygon($image, $points_2d, $nb_points, $colour);
}
}
public function isProjected() {
return $this->_isProjected;
}
public function project() {
foreach($this->_dots as $dot) {
if(!$dot->isProjected()) $dot->project();
}
$this->_isProjected = 1;
}
public function preProject($dx, $dy, $dz, $cos_alpha, $sin_alpha, $cos_omega, $sin_omega) {
foreach($this->_dots as $dot) {
$dot->preProject($dx, $dy, $dz, $cos_alpha, $sin_alpha, $cos_omega, $sin_omega);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment