本篇介绍自定义及矢量图实例。首先绘制一个三尖角星体,分别由“前面”,“后面”和三个“侧面”组成。 自定义多边形
代码:
<?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">';
?>
效果图: