|
1
|
|
|
<?php |
|
2
|
|
|
/** @formatter:off |
|
3
|
|
|
* ****************************************************************** |
|
4
|
|
|
* Created by Marko Kungla on Jun 25, 2016 - 7:51:42 PM |
|
5
|
|
|
* Contact [email protected] |
|
6
|
|
|
* @copyright 2016 Marko Kungla - https://github.com/mkungla |
|
7
|
|
|
* @license The MIT License (MIT) |
|
8
|
|
|
* |
|
9
|
|
|
* @category AframeVR |
|
10
|
|
|
* @package aframe-php |
|
11
|
|
|
* |
|
12
|
|
|
* Lang PHP (php version >= 7) |
|
13
|
|
|
* Encoding UTF-8 |
|
14
|
|
|
* File MaterialComponent.php |
|
15
|
|
|
* Code format PSR-2 and 12 |
|
16
|
|
|
* @link https://github.com/mkungla/aframe-php |
|
17
|
|
|
^ @issues https://github.com/mkungla/aframe-php/issues |
|
18
|
|
|
* ******************************************************************** |
|
19
|
|
|
* Contributors: |
|
20
|
|
|
* @author Marko Kungla <[email protected]> |
|
21
|
|
|
* ******************************************************************** |
|
22
|
|
|
* Comments: |
|
23
|
|
|
* @formatter:on */ |
|
24
|
|
|
namespace AframeVR\Core\Components\Material; |
|
25
|
|
|
|
|
26
|
|
|
use \AframeVR\Interfaces\Core\Components\Material\MaterialInterface; |
|
27
|
|
|
use \AframeVR\Core\Helpers\ComponentAbstract; |
|
28
|
|
|
use \AframeVR\Core\Exceptions\BadShaderCallException; |
|
29
|
|
|
use \AframeVR\Interfaces\ShaderInterface; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* AframeVR\Core\Components\Material |
|
33
|
|
|
* |
|
34
|
|
|
* The material component defines the appearance of an entity. |
|
35
|
|
|
* The built-in shaders allow us to define properties such as color, |
|
36
|
|
|
* opacity, or textures. Custom shaders can be registered to extend |
|
37
|
|
|
* the material component to allow for a wide range of visual effects. |
|
38
|
|
|
* The geometry component can be defined alongside to provide a shape alongside |
|
39
|
|
|
* the appearance to create a complete mesh. The material component is coupled to shaders. |
|
40
|
|
|
* Some of the built-in shading models will provide properties like color or texture to the material component. |
|
41
|
|
|
*/ |
|
42
|
|
|
class MaterialComponent extends ComponentAbstract implements MaterialInterface |
|
43
|
|
|
{ |
|
44
|
|
|
|
|
45
|
|
|
private $shaderObj; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Initialize Component |
|
49
|
|
|
* |
|
50
|
|
|
* {@inheritdoc} |
|
51
|
|
|
* |
|
52
|
|
|
* @return bool |
|
53
|
|
|
*/ |
|
54
|
35 |
|
public function initializeComponent(): bool |
|
55
|
|
|
{ |
|
56
|
35 |
|
$this->setDomAttributeName('material'); |
|
57
|
35 |
|
return true; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Return DOM attribute contents |
|
62
|
|
|
* |
|
63
|
|
|
* @return string |
|
64
|
|
|
*/ |
|
65
|
3 |
|
public function getDomAttributeString(): string |
|
66
|
|
|
{ |
|
67
|
3 |
|
$this->prepareShader(); |
|
68
|
3 |
|
$material_attrs = $this->getDOMAttributesArray(); |
|
69
|
3 |
|
$format = implode(': %s; ', array_keys($material_attrs)) . ': %s;'; |
|
70
|
3 |
|
return vsprintf($format, array_values($material_attrs)); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Material Shader |
|
75
|
|
|
* |
|
76
|
|
|
* {@inheritdoc} |
|
77
|
|
|
* |
|
78
|
|
|
* @param string $shader |
|
79
|
|
|
* @throws BadShaderCallException |
|
80
|
|
|
* @return object|null |
|
81
|
|
|
*/ |
|
82
|
16 |
|
public function shader(string $shader = 'standard') |
|
83
|
|
|
{ |
|
84
|
16 |
|
$this->dom_attributes['shader'] = $shader; |
|
85
|
|
|
|
|
86
|
16 |
|
if ($this->shaderObj instanceof ShaderInterface) |
|
87
|
3 |
|
return $this->shaderObj; |
|
88
|
|
|
|
|
89
|
16 |
|
$shader = sprintf('\AframeVR\Core\Shaders\%s', ucfirst($shader)); |
|
90
|
16 |
|
if (class_exists($shader)) { |
|
91
|
15 |
|
$this->shaderObj = new $shader(); |
|
92
|
|
|
} else { |
|
93
|
1 |
|
throw new BadShaderCallException($shader); |
|
94
|
|
|
} |
|
95
|
15 |
|
return $this->shaderObj ?? null; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Prepare Shader attributes |
|
100
|
|
|
* |
|
101
|
|
|
* @return void |
|
102
|
|
|
*/ |
|
103
|
3 |
|
private function prepareShader() |
|
104
|
|
|
{ |
|
105
|
3 |
|
if (! empty($this->shaderObj)) { |
|
106
|
3 |
|
$this->shaderObj->removeDefaultDOMAttributes(); |
|
107
|
3 |
|
foreach ($this->shaderObj as $key => $val) |
|
108
|
3 |
|
$this->dom_attributes[$key] = $val; |
|
109
|
|
|
} |
|
110
|
3 |
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|