Source for file 3dsChunks.php
Documentation is available at 3dsChunks.php
const EDIT3DS = 0x3D3D; // this is the start of the editor config
const KEYF3DS = 0xB000; // this is the start of the keyframer config
//>------ sub defines of EDIT3DS
const EDIT_MATERIAL = 0xAFFF;
const EDIT_CONFIG1 = 0x0100;
const EDIT_CONFIG2 = 0x3E3D;
const EDIT_VIEW_P1 = 0x7012;
const EDIT_VIEW_P2 = 0x7011;
const EDIT_VIEW_P3 = 0x7020;
const EDIT_VIEW1 = 0x7001;
const EDIT_BACKGR = 0x1200;
const EDIT_AMBIENT = 0x2100;
const EDIT_OBJECT = 0x4000;
//>------ sub defines of EDIT_OBJECT
const OBJ_TRIMESH = 0x4100;
const OBJ_LIGHT = 0x4600;
const OBJ_CAMERA = 0x4700;
const OBJ_UNKNWN01 = 0x4010;
const OBJ_UNKNWN02 = 0x4012;
//>------ sub defines of OBJ_CAMERA
const CAM_UNKNWN01 = 0x4710;
const CAM_UNKNWN02 = 0x4720;
//>------ sub defines of OBJ_LIGHT
const LIT_UNKNWN01 = 0x465A;
//>------ sub defines of OBJ_TRIMESH
const TRI_VERTEXL = 0x4110;
const TRI_FACEL2 = 0x4111;
const TRI_FACEL1 = 0x4120;
const TRI_SMOOTH = 0x4150;
const TRI_LOCAL = 0x4160;
const TRI_VISIBLE = 0x4165;
//>>------ sub defs of KEYF3DS
const KEYF_UNKNWN01 = 0xB009;
const KEYF_UNKNWN02 = 0xB00A;
const KEYF_FRAMES = 0xB008;
const KEYF_OBJDES = 0xB002;
//>>------ these define the different color chunk types
//>>------ defines for viewport chunks
public function __construct ($type, $content) {
$this->type = (int) $type;
$this->size = strlen($content);
$this->content = $content;
public function readChunks () {
if (count($this->chunks) || ($this->size < 6 )) return false;
$string = $this->content;
$length = $this->size - 6;
while ($position <= $length) {
$type = $this->getWord (substr($string, $position, 2 ));
$chunkLength = $this->getDWord (substr($string, $position, 4 )) - 6;
$this->chunks[] = new Image_3D_Chunk ($type, substr($string, $position, $chunkLength));
$position += $chunkLength;
public function debug () {
printf("Typ: %6d (0x%04x) (%6d bytes) | Objects:%4d | Content:%6d\n", $this->type, $this->type, $this->size, count($this->chunks), strlen($this->content));
protected function getWord ($string) {
return (ord($string{1 }) << 8 ) | ord($string{0 });
protected function getDWord ($string) {
return ord($string{0 }) | (ord($string{1 }) << 8 ) | (ord($string{2 }) << 16 ) | (ord($string{3 }) << 32 );
protected function getUnsignedInt ($string) {
return (ord($string{0 }) << 8 ) | ord($string{1 });
protected function getFloat ($string) {
// Convert C-Float to PHP-Float
return (ord($string{3 }) & 128 ? -1 : 1 ) * (1 + (float) (ord($string{2 }) & 127 ) / 127 + (float) (ord($string{1 })) / 256 / 127 + (float) (ord($string{0 })) / 256 / 256 / 127 ) * pow(2. , ((((ord($string{3 }) & 127 ) << 1 ) | (ord($string{2 }) >> 7 )) - 127 ));
public function getChunks () {
public function getType () {
public function getContent () {
public function getFirstChunkByType ($type) {
foreach ($this->chunks as $chunk) if ($chunk->getType () === $type) return $chunk;
public function getChunksByType ($type) {
foreach ($this->chunks as $chunk) if ($chunk->getType () === $type) $chunks[] = $chunk;
class Image_3D_Chunk_Object extends Image_3D_Chunk {
public function __construct ($type, $content) {
parent ::__construct ($type, $content);
protected function getName () {
while ((ord($this->content{$i}) !== 0 ) && ($i < $this->size )) $this->name .= $this->content {$i++ };
$this->content = substr($this->content , $i + 1 );
public function readChunks (Image_3D_Object_3ds $k3ds) {
$subtype = $this->getWord (substr($this->content , 0 , 2 ));
$subcontent = substr($this->content , 6 );
$object = $k3ds->addObject ($this->name );
$this->chunks [] = new Image_3D_Chunk_TriMesh ($subtype, $subcontent, $object);
public function debug () {
echo 'Object: ', $this->name , "\n";
class Image_3D_Chunk_TriMesh extends Image_3D_Chunk {
public function __construct ($type, $content, $object) {
parent ::__construct ($type, $content);
protected function getPoints () {
$vertexlists = $this->getChunksByType (Image_3D_Chunk ::TRI_VERTEXL );
foreach ($vertexlists as $vertexlist) {
$points = $vertexlist->getContent ();
$count = $this->getWord (substr($points, 0 , 2 ));
for ($i = 0; $i < $count; $i++ ) {
$x = $this->getFloat (substr($points, 0 , 4 ));
$y = $this->getFloat (substr($points, 4 , 4 ));
$z = $this->getFloat (substr($points, 8 , 4 ));
$this->object->newPoint ($x, $y, $z);
$points = substr($points, 12 );
protected function getFaces () {
$facelists = $this->getChunksByType (Image_3D_Chunk ::TRI_FACEL1 );
foreach ($facelists as $facelist) {
$faces = $facelist->getContent ();
$count = $this->getWord (substr($faces, 0 , 2 ));
for ($i = 0; $i < $count; $i++ ) {
$p1 = $this->getWord (substr($faces, 0 , 2 ));
$p2 = $this->getWord (substr($faces, 2 , 2 ));
$p3 = $this->getWord (substr($faces, 4 , 2 ));
$this->object->newPolygon ($p1, $p2, $p3);
protected function getTranslations () {
$translists = $this->getChunksByType (Image_3D_Chunk ::TRI_LOCAL );
foreach ($translists as $translist) {
$trans = $translist->getContent ();
echo "Trans: " . strlen($trans), "\n";
public function debug () {
printf("Trimesh with %d (0x%04x) points - Pointsize: %.2f\n", $this->pointCount, $this->pointCount, $this->size / $this->pointCount);
// echo "New Point: $x, $y, $z -> ", count($this->_points), "\n";
// printf("ERROR: Unknown point (%d, %d, %d of %d)\n", $p1, $p2, $p3, count($this->_points) - 1);
// echo "New Polygon: $p1, $p2, $p3 -> ", count($this->_polygones), "\n";
public function debug() {
Documentation generated on Mon, 11 Mar 2019 14:38:27 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.
|