本篇介紹自定義及矢量圖實例。首先繪制一個三尖角星體,分別由“前面”,“後面”和三個“側面”組成。 自定義多邊形
代碼:
<?php
require_once('Image/3D.php');
$image = new Image_3D();
$image->setColor(new Image_3D_Color(255, 255, 255));
//創建光源
$light1 = $image->createLight('light',array(-50, -50, -50));
$light1->setColor(new Image_3D_Color(100, 250, 100));
$light2 = $image->createLight('light',array(50, -50, 0));
$light2->setColor(new Image_3D_Color(100, 100, 250));
$light3 = $image->createLight('light',array(50, 50, 0));
$light3->setColor(new Image_3D_Color(50, 0, 100));
//創建多邊形數組
$polygons = array();
//前面點坐標
$polygons[] = array(
array(0, -120, 0), array(-18, -12, 0),
array(-86, 48, 0), array(0, 18, 0),
array(86, 48, 0), array(18, -12, 0)
);
//後面點坐標
$polygons[] = array(
array(0, -120, 60), array(-18, -12, 60),
array(-86, 48, 60), array(0, 18, 60),
array(86, 48, 60), array(18, -12, 60)
);
//三側面點坐標
$polygons[] = array(
array(0, -120, 0), array(-18, -12, 0),
array(-86, 48, 0), array(-86, 48, 60),
array(-18, -12, 60),array(0, -120, 60)
);
$polygons[] = array(
array(-86, 48, 0), array(0, 18, 0),
array(86, 48, 0), array(86, 48, 60),
array(0, 18, 60), array(-86, 48, 60)
);
$polygons[] = array(
array(86, 48, 0), array(18, -12, 0),
array(0, -120, 0), array(0, -120, 60),
array(18, -12, 60),array(86, 48, 60)
);
//繪制3D圖像
foreach ($polygons as $poly) {
$points = array();
foreach ($poly as $set) {
$points[] = new Image_3D_Point($set[0], $set[1], $set[2]);
}
$p = $image->createObject('polygon', $points);
$p->setColor(new Image_3D_Color(255, 255, 255));
}
$image->transform($image->createMatrix('Rotation', array(-10, -25, -15)));
$image->createRenderer('perspectively');
$image->createDriver('gd');
$image->render(300, 300, 'anim.png');
echo '<img src="anim.png">';
?>
效果圖: