Free PHP Library for 3D Rendering Complex Scenes
Open Source PHP 3D Rendering Library allows Software Developers to generate & manipulate 3D graphics, OBJ File parsing and applying various textures & materials
Web development has come a long way, constantly evolving to deliver immersive and visually stunning experiences. Traditionally, rendering 3D graphics was limited to specialized software or platforms, requiring extensive knowledge of languages such as C++ or JavaScript. However, advancements in web technologies have made it possible to integrate 3D rendering into web applications using PHP, a language known for its simplicity and flexibility. The 3D PHP Render library is a powerful tool that enables developers to create and manipulate 3D objects directly in PHP.
The 3D PHP Render library is a robust and versatile tool that enables software developers to generate and manipulate 3D graphics effortlessly. It offers a wide range of features, including rendering complex scenes and 3D models, creating animations, handling lighting and shadows, and applying various textures & materials. This library bridges the gap between PHP and 3D graphics, allowing software developers to leverage their existing PHP skills to create immersive visual experiences. By leveraging the capabilities of this library, developers can bring their web applications to life with visually stunning 3D elements.
One of the standout advantages of using a 3D PHP render library is its simplicity. Software Developers familiar with PHP can quickly grasp the library's concepts and start building 3D scenes without having to learn complex new languages or frameworks. The 3D PHP render library seamlessly integrates with existing PHP frameworks and libraries. It provides a straightforward API and can be easily incorporated into PHP-based web applications. With its ease of use, flexibility, and performance, this library is poised to transform how we experience the web, bringing us closer to a future where 3D graphics are a standard feature of online interactions.
Getting Started with PHP Render
The easiest way to install PHP Render stable release is using Composer. Please use the following command for a smooth installation.
Install PHP Render via Composer
$composer install php-render
You can download the compiled shared library from Github repository.
Work with 3D Model via PHP API
The open source PHP Render library allows software developers to handling various tasks related to 3D models inside PHP applications. The library makes it easy to create dynamic & interactive 3D content and can define keyframes and transitions to animate objects, cameras, and lights. Software developers can easily generate and manipulate various geometric shapes, including polygons, spheres, cubes, and more. They can modify vertices, apply transformations, and combine shapes to create complex models. The following example shows how software developers can load a model from an OBJ file using PHP code.
How to Load Model from an OBJ File using PHP API?
define('EXAMPLE_DIR', __DIR__);
require __DIR__ . '/../example_base.php';
use PHPR\Context;
use PHPR\Shader\Shader;
use PHPR\Mesh\{ObjParser, Vertex};
use PHPR\Mesh\Vertex\{VertexPNU};
use PHPR\Mesh\VertexArray;
use PHPR\Math\{Vec3, Vec4, Mat4, Angle};
/**
* Simpel cube shader
*/
class CubeShader extends Shader
{
public Mat4 $mvp;
/**
* Vertex shader like thing
*/
public function vertex(Vertex $vertex, array &$out) : Vec4
{
$out['color'] = $vertex->normal;
return $this->mvp->multiplyVec3($vertex->position);
}
/**
* Fragment shader like thing
*/
public function fragment(array &$in, array &$out)
{
$out['color'] = $in['color']->normalize()->toColorInt();
// $out['color'] = 0xFFFFFF;
}
}
/**
* Build a model view projection
*/
$projection = Mat4::perspective(Angle::degreesToRadians(-45.0), EXAMPLE_RENDER_ASPECT_RATIO, 0.5, 10);
$view = (new Mat4())->inverse();
$model = (new Mat4)->translate(new Vec3(0.1, -0.3, -2.5));
$model->rotateX(0.4);
$model->rotateY(-0.6);
//$model->rotateY(-0.05);
/**
* Create shader object
*/
$shader = new CubeShader;
$shader->mvp = $model->multiply($view->multiply($projection, true), true);
/**
* Build the context
*/
$context = create_exmaple_context();
$context->bindShader($shader);
$context->enableDepthTesting();
// $context->setDrawMode(Context::DRAW_MODE_LINES);
/**
* Define the model
*/
$model = ObjParser::loadFromDisk(EXAMPLE_DIR . '/suv.obj');
/**
* draw the model
*/
$context->drawVertexArray($model);
render_example_context($context);
render_example_context_depth($context);
3D Scenes Rendering via PHP API
The open source PHP Render library makes it easy for software developers to load and render various types of 3D scenes inside their PHP applications. It provides a streamlined API for rendering 3D scenes, complete with camera controls, lighting, and object manipulation. Developers can define objects, position them within the scene, and specify their appearance and behavior. Moreover, software developers can generate and manipulate various geometric shapes, including polygons, spheres, cubes, and more.
Better Product Visualization
The free 3D PHP Render library empowers software developers to add visually stunning 3D elements to their web applications effortlessly. E-commerce websites can utilize the 3D PHP Render library to offer interactive product views, allowing customers to rotate, zoom, and examine products from different angles. This immersive experience enhances product understanding and improves customer satisfaction. The following example demonstrates the interpolation of the barycentric coordinates inside of a drawn triangle using PHP API.
Perform Interpolation of Barycentric Coordinates inside of a Drawn Triangle via PHP?
use PHPR\Shader\TriangleTestShader;
use PHPR\Mesh\Vertex;
use PHPR\Math\Vec3;
$context = create_exmaple_context();
$context->bindShader(new TriangleTestShader);
// $context->setDrawMode(\PHPR\Context::DRAW_MODE_LINES);
/**
* Vertex subclass with color attribute
*/
class ExampleVertex extends Vertex
{
public static function c(float $x, float $y, float $z, float $r, float $g, float $b)
{
$v = ExampleVertex::cP($x, $y, $z);
$v->color = new Vec3($r, $g, $b);
return $v;
}
public Vec3 $color;
}
/**
* Draw the tirangle
*/
$context->drawTriangle(
// | position | color
ExampleVertex::c(-0.8, -0.8, 0.0, 1.0, 0.0, 0.0),
ExampleVertex::c( 0.8, -0.8, 0.0, 0.0, 1.0, 0.0),
ExampleVertex::c( 0.0, 0.8, 0.0, 0.0, 0.0, 1.0),
);