Update following the changes of blending&culling options
[platform/core/uifw/dali-toolkit.git] / plugins / dali-script-v8 / src / rendering / material-api.cpp
1 /*
2  * Copyright (c) 2015 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18
19 // CLASS HEADER
20 #include "material-api.h"
21
22 // EXTERNAL INCLUDES
23 #include <dali/public-api/object/type-registry.h>
24
25 // INTERNAL INCLUDES
26 #include <v8-utils.h>
27 #include <rendering/material-wrapper.h>
28 #include <rendering/shader-wrapper.h>
29 #include <rendering/shader-api.h>
30 #include <rendering/sampler-wrapper.h>
31 #include <rendering/sampler-api.h>
32 #include <image/image-wrapper.h>
33
34 namespace Dali
35 {
36
37 namespace V8Plugin
38 {
39
40 /**
41  * ## Material API
42  *
43  * Material is a handle to an object that specifies the visual properties of the renderer.
44  *
45  * @class Material
46  * @extends Handle
47  */
48
49 Material MaterialApi::GetMaterial( v8::Isolate* isolate, const v8::FunctionCallbackInfo< v8::Value >& args )
50 {
51   v8::HandleScope handleScope( isolate );
52
53   v8::Local<v8::Object> object = args.This();
54   v8::Local<v8::External> field = v8::Local<v8::External>::Cast( object->GetInternalField(0) );
55   void* ptr = field->Value();
56
57   MaterialWrapper* wrapper = static_cast< MaterialWrapper *>(ptr);
58   return wrapper->GetMaterial();
59 }
60
61 Material MaterialApi::GetMaterialFromParams( int paramIndex,
62                                                bool& found,
63                                                v8::Isolate* isolate,
64                                                const v8::FunctionCallbackInfo< v8::Value >& args )
65 {
66   found = false;
67
68   v8::HandleScope handleScope( isolate );
69   BaseWrappedObject* wrappedObject = V8Utils::GetWrappedDaliObjectParameter( paramIndex, BaseWrappedObject::MATERIAL, isolate, args );
70   if( wrappedObject )
71   {
72     found = true;
73     MaterialWrapper* wrapper = static_cast< MaterialWrapper *>(wrappedObject);
74     return wrapper->GetMaterial();
75   }
76   else
77   {
78     return Material();
79   }
80 }
81
82 /**
83  * Create a new material object.
84  *
85  * @constructor
86  * @method Material
87  * @for Material
88  * @param {Object} shader The shader used by the material
89  * @return {Object} Material
90  */
91 Material MaterialApi::New( const v8::FunctionCallbackInfo< v8::Value >& args )
92 {
93   v8::Isolate* isolate = args.GetIsolate();
94   v8::HandleScope handleScope( isolate );
95
96   bool found( false );
97   Shader shader = ShaderApi::GetShaderFromParams( 0, found, isolate, args );
98   if( found )
99   {
100     return Material::New( shader );
101   }
102   else
103   {
104     DALI_SCRIPT_EXCEPTION( isolate, "missing shader from param 0" );
105     return Material();
106   }
107 }
108
109 /**
110  * Set the Shader used by this material
111  *
112  * @method setShader
113  * @for Material
114  * @param {Object} shader Handle to the shader
115  */
116 void MaterialApi::SetShader( const v8::FunctionCallbackInfo< v8::Value >& args )
117 {
118   v8::Isolate* isolate = args.GetIsolate();
119   v8::HandleScope handleScope( isolate );
120
121   Material material = GetMaterial( isolate, args );
122
123   bool found( false );
124   Shader shader = ShaderApi::GetShaderFromParams( 0, found, isolate, args );
125   if( !found )
126   {
127     DALI_SCRIPT_EXCEPTION( isolate, "invalid shader parameter" );
128   }
129   else
130   {
131     return material.SetShader( shader );
132   }
133 }
134
135 /**
136  * Get the Shader used by this material
137  *
138  * @method getShader
139  * @for Material
140  * @return {Object} Shader
141  */
142 void MaterialApi::GetShader( const v8::FunctionCallbackInfo< v8::Value >& args )
143 {
144   v8::Isolate* isolate = args.GetIsolate();
145   v8::HandleScope handleScope( isolate );
146
147   Material material = GetMaterial( isolate, args );
148   Shader shader = material.GetShader();
149
150   // Wrap the shader
151   v8::Local<v8::Object> localObject = ShaderWrapper::WrapShader( isolate, shader );
152   args.GetReturnValue().Set( localObject );
153 }
154
155 /**
156  * Add a new texture to be used by the material
157  *
158  * @method addTexture
159  * @for Material
160  * @param {Object} image The image used by this sampler
161  * @param {String} uniformName The string with the name of the uniform
162  * @param {Object} sampler The sampler to add to this material
163  * @return {integer} The index of the texture in the array of textures or -1 if texture can not be added
164  */
165 void MaterialApi::AddTexture( const v8::FunctionCallbackInfo< v8::Value >& args )
166 {
167   v8::Isolate* isolate = args.GetIsolate();
168   v8::HandleScope handleScope( isolate );
169
170   Material material = GetMaterial( isolate, args );
171
172   bool found( false );
173   Image image = V8Utils::GetImageParameter( PARAMETER_0, found, isolate, args );
174   if( !found )
175   {
176     DALI_SCRIPT_EXCEPTION( isolate, "missing image from param 0" );
177     return;
178   }
179
180   found = false;
181   std::string uniformName = V8Utils::GetStringParameter( PARAMETER_1, found, isolate, args );
182   if( !found )
183   {
184     DALI_SCRIPT_EXCEPTION( isolate, "missing uniform name from param 1" );
185     return;
186   }
187
188   found = false;
189   Sampler sampler = SamplerApi::GetSamplerFromParams( PARAMETER_2, found, isolate, args );
190   if( !found )
191   {
192     args.GetReturnValue().Set( v8::Integer::New( isolate, material.AddTexture( image, uniformName ) ) );
193   }
194   else
195   {
196     args.GetReturnValue().Set( v8::Integer::New( isolate, material.AddTexture( image, uniformName, sampler ) ) );
197   }
198 }
199
200 /**
201  * Removes a texture from the material
202  *
203  * @method removeTexture
204  * @for Material
205  * @param {integer} index The index of the texture in the array of textures
206  */
207 void MaterialApi::RemoveTexture( const v8::FunctionCallbackInfo< v8::Value >& args )
208 {
209   v8::Isolate* isolate = args.GetIsolate();
210   v8::HandleScope handleScope( isolate );
211
212   Material material = GetMaterial( isolate, args );
213
214   bool found( false );
215   int index = V8Utils::GetIntegerParameter( PARAMETER_0, found, isolate, args, 0 /* default */);
216   if( !found )
217   {
218     DALI_SCRIPT_EXCEPTION( isolate, "invalid index parameter" );
219   }
220   else
221   {
222     material.RemoveTexture( index );
223   }
224 }
225
226 /**
227  * Sets the image to be used by a given texture
228  * @method setTextureImage
229  * @for Material
230  * @param {integer} index The index of the texture in the array of textures
231  * @param {Object} image The image used by this sampler
232  */
233 void MaterialApi::SetTextureImage( const v8::FunctionCallbackInfo< v8::Value >& args )
234 {
235   v8::Isolate* isolate = args.GetIsolate();
236   v8::HandleScope handleScope( isolate );
237
238   Material material = GetMaterial( isolate, args );
239
240   bool found( false );
241   int index = V8Utils::GetIntegerParameter( PARAMETER_0, found, isolate, args, 0 /* default */);
242   if( !found )
243   {
244     DALI_SCRIPT_EXCEPTION( isolate, "invalid index parameter" );
245     return;
246   }
247
248   found = false;
249   Image image = V8Utils::GetImageParameter( PARAMETER_1, found, isolate, args );
250   if( !found )
251   {
252     DALI_SCRIPT_EXCEPTION( isolate, "missing image from param 1" );
253   }
254   else
255   {
256     material.SetTextureImage(index, image);
257   }
258 }
259
260 /**
261  * Set the sampler used by a given texture
262  * @method setTextureSampler
263  * @for Material
264  * @param {integer} index The index of the texture in the array of textures
265  * @param {Object} sampler The new sampler
266  */
267 void MaterialApi::SetTextureSampler( const v8::FunctionCallbackInfo< v8::Value >& args )
268 {
269   v8::Isolate* isolate = args.GetIsolate();
270   v8::HandleScope handleScope( isolate );
271
272   Material material = GetMaterial( isolate, args );
273
274   bool found( false );
275   int index = V8Utils::GetIntegerParameter( PARAMETER_0, found, isolate, args, 0 /* default */);
276   if( !found )
277   {
278     DALI_SCRIPT_EXCEPTION( isolate, "invalid index parameter" );
279     return;
280   }
281
282   found = false;
283   Sampler sampler = SamplerApi::GetSamplerFromParams( PARAMETER_1, found, isolate, args );
284   if( !found )
285   {
286     DALI_SCRIPT_EXCEPTION( isolate, "missing sampler from param 1" );
287   }
288   else
289   {
290     material.SetTextureSampler(index, sampler);
291   }
292 }
293
294 /**
295  * Set the uniform name of a given texture
296  * @method setTextureUniformName
297  * @for Material
298  * @param {integer} index The index of the texture in the array of textures
299  * @param {string} uniformName The new uniform name
300  */
301 void MaterialApi::SetTextureUniformName( const v8::FunctionCallbackInfo< v8::Value >& args )
302 {
303   v8::Isolate* isolate = args.GetIsolate();
304   v8::HandleScope handleScope( isolate );
305
306   Material material = GetMaterial( isolate, args );
307
308   bool found( false );
309   int index = V8Utils::GetIntegerParameter( PARAMETER_0, found, isolate, args, 0 /* default */);
310   if( !found )
311   {
312     DALI_SCRIPT_EXCEPTION( isolate, "invalid index parameter" );
313     return;
314   }
315
316   found = false;
317   std::string uniformName = V8Utils::GetStringParameter( PARAMETER_1, found, isolate, args );
318   if( !found )
319   {
320     DALI_SCRIPT_EXCEPTION( isolate, "invalid uniform name parameter" );
321   }
322   else
323   {
324     material.SetTextureUniformName(index, uniformName);
325   }
326 }
327
328 /**
329  * Retrive the index of a texture given its uniform name
330  * @method getTextureIndex
331  * @for Material
332  * @param {string} uniformName The uniform name
333  * @return {integer} The index in the array of textures or -1 if the texture is not found
334  */
335 void MaterialApi::GetTextureIndex( const v8::FunctionCallbackInfo< v8::Value >& args )
336 {
337   v8::Isolate* isolate = args.GetIsolate();
338   v8::HandleScope handleScope( isolate );
339
340   Material material = GetMaterial( isolate, args );
341
342   bool found( false );
343   std::string uniformName = V8Utils::GetStringParameter( PARAMETER_0, found, isolate, args );
344   if( !found )
345   {
346     DALI_SCRIPT_EXCEPTION( isolate, "invalid uniform name parameter" );
347   }
348   else
349   {
350     args.GetReturnValue().Set( v8::Integer::New( isolate, material.GetTextureIndex(uniformName) ) );
351   }
352 }
353
354 /**
355  * Retrieve the number of textures used by the material
356  *
357  * @method getNumberOfTextures
358  * @for Material
359  * @return {integer} The number of textures
360  */
361 void MaterialApi::GetNumberOfTextures( const v8::FunctionCallbackInfo< v8::Value >& args )
362 {
363   v8::Isolate* isolate = args.GetIsolate();
364   v8::HandleScope handleScope( isolate );
365
366   Material material = GetMaterial( isolate, args );
367
368   args.GetReturnValue().Set( v8::Integer::New( isolate, material.GetNumberOfTextures() ) );
369 }
370
371 } // namespace V8Plugin
372
373 } // namespace Dali