1. Products
  2.   3D
  3.   PHP
  4.   PHP Render
 
  

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

What is PHP Render?

Web development has advanced significantly, continuously changing to produce visually stunning and immersive experiences. Rendering 3D graphics was formerly restricted to specialist systems or software that required a deep understanding of languages like JavaScript or C++. However, thanks to developments in web technology, PHP—a language renowned for its ease of use and adaptability—can now be used to include 3D rendering into web applications. Developers may generate and work with 3D objects directly in PHP with the help of the robust 3D PHP Render library. Programmers may easily create and work with 3D visuals with the help of the powerful and adaptable 3D PHP Render library. Among its many advantages are the ability to generate intricate scenes and 3D models, create animations, manage lighting, and shadows, and applying various textures & materials.

The ease of use of a 3D PHP render library is one of its most notable benefits. Without having to learn complicated new languages or frameworks, software developers who are already familiar with PHP may rapidly understand the library's fundamentals and begin creating 3D sceneries. Existing PHP frameworks and libraries are simply integrated with the 3D PHP render library. It offers a simple API and is simple to integrate into web applications that use PHP. By bridging the gap between PHP and 3D graphics, this library enables software developers to create rich visual experiences using their existing PHP expertise. Developers can use this library's features to add visually appealing 3D elements to their online apps, making them come to life. 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.

Previous Next

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.

How to 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),
);