2 * Copyright (c) 2015 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 #include "material-api.h"
23 #include <dali/public-api/object/type-registry.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>
43 * Material is a handle to an object that specifies the visual properties of the renderer.
49 Material MaterialApi::GetMaterial( v8::Isolate* isolate, const v8::FunctionCallbackInfo< v8::Value >& args )
51 v8::HandleScope handleScope( isolate );
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();
57 MaterialWrapper* wrapper = static_cast< MaterialWrapper *>(ptr);
58 return wrapper->GetMaterial();
61 Material MaterialApi::GetMaterialFromParams( int paramIndex,
64 const v8::FunctionCallbackInfo< v8::Value >& args )
68 v8::HandleScope handleScope( isolate );
69 BaseWrappedObject* wrappedObject = V8Utils::GetWrappedDaliObjectParameter( paramIndex, BaseWrappedObject::MATERIAL, isolate, args );
73 MaterialWrapper* wrapper = static_cast< MaterialWrapper *>(wrappedObject);
74 return wrapper->GetMaterial();
83 * Create a new material object.
88 * @param {Object} shader The shader used by the material
89 * @return {Object} Material
91 Material MaterialApi::New( const v8::FunctionCallbackInfo< v8::Value >& args )
93 v8::Isolate* isolate = args.GetIsolate();
94 v8::HandleScope handleScope( isolate );
97 Shader shader = ShaderApi::GetShaderFromParams( 0, found, isolate, args );
100 return Material::New( shader );
104 DALI_SCRIPT_EXCEPTION( isolate, "missing shader from param 0" );
110 * Set the Shader used by this material
114 * @param {Object} shader Handle to the shader
116 void MaterialApi::SetShader( const v8::FunctionCallbackInfo< v8::Value >& args )
118 v8::Isolate* isolate = args.GetIsolate();
119 v8::HandleScope handleScope( isolate );
121 Material material = GetMaterial( isolate, args );
124 Shader shader = ShaderApi::GetShaderFromParams( 0, found, isolate, args );
127 DALI_SCRIPT_EXCEPTION( isolate, "invalid shader parameter" );
131 return material.SetShader( shader );
136 * Get the Shader used by this material
140 * @return {Object} Shader
142 void MaterialApi::GetShader( const v8::FunctionCallbackInfo< v8::Value >& args )
144 v8::Isolate* isolate = args.GetIsolate();
145 v8::HandleScope handleScope( isolate );
147 Material material = GetMaterial( isolate, args );
148 Shader shader = material.GetShader();
151 v8::Local<v8::Object> localObject = ShaderWrapper::WrapShader( isolate, shader );
152 args.GetReturnValue().Set( localObject );
156 * Add a new texture to be used by the 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
165 void MaterialApi::AddTexture( const v8::FunctionCallbackInfo< v8::Value >& args )
167 v8::Isolate* isolate = args.GetIsolate();
168 v8::HandleScope handleScope( isolate );
170 Material material = GetMaterial( isolate, args );
173 Image image = V8Utils::GetImageParameter( PARAMETER_0, found, isolate, args );
176 DALI_SCRIPT_EXCEPTION( isolate, "missing image from param 0" );
181 std::string uniformName = V8Utils::GetStringParameter( PARAMETER_1, found, isolate, args );
184 DALI_SCRIPT_EXCEPTION( isolate, "missing uniform name from param 1" );
189 Sampler sampler = SamplerApi::GetSamplerFromParams( PARAMETER_2, found, isolate, args );
192 args.GetReturnValue().Set( v8::Integer::New( isolate, material.AddTexture( image, uniformName ) ) );
196 args.GetReturnValue().Set( v8::Integer::New( isolate, material.AddTexture( image, uniformName, sampler ) ) );
201 * Removes a texture from the material
203 * @method removeTexture
205 * @param {integer} index The index of the texture in the array of textures
207 void MaterialApi::RemoveTexture( const v8::FunctionCallbackInfo< v8::Value >& args )
209 v8::Isolate* isolate = args.GetIsolate();
210 v8::HandleScope handleScope( isolate );
212 Material material = GetMaterial( isolate, args );
215 int index = V8Utils::GetIntegerParameter( PARAMETER_0, found, isolate, args, 0 /* default */);
218 DALI_SCRIPT_EXCEPTION( isolate, "invalid index parameter" );
222 material.RemoveTexture( index );
227 * Sets the image to be used by a given texture
228 * @method setTextureImage
230 * @param {integer} index The index of the texture in the array of textures
231 * @param {Object} image The image used by this sampler
233 void MaterialApi::SetTextureImage( const v8::FunctionCallbackInfo< v8::Value >& args )
235 v8::Isolate* isolate = args.GetIsolate();
236 v8::HandleScope handleScope( isolate );
238 Material material = GetMaterial( isolate, args );
241 int index = V8Utils::GetIntegerParameter( PARAMETER_0, found, isolate, args, 0 /* default */);
244 DALI_SCRIPT_EXCEPTION( isolate, "invalid index parameter" );
249 Image image = V8Utils::GetImageParameter( PARAMETER_1, found, isolate, args );
252 DALI_SCRIPT_EXCEPTION( isolate, "missing image from param 1" );
256 material.SetTextureImage(index, image);
261 * Set the sampler used by a given texture
262 * @method setTextureSampler
264 * @param {integer} index The index of the texture in the array of textures
265 * @param {Object} sampler The new sampler
267 void MaterialApi::SetTextureSampler( const v8::FunctionCallbackInfo< v8::Value >& args )
269 v8::Isolate* isolate = args.GetIsolate();
270 v8::HandleScope handleScope( isolate );
272 Material material = GetMaterial( isolate, args );
275 int index = V8Utils::GetIntegerParameter( PARAMETER_0, found, isolate, args, 0 /* default */);
278 DALI_SCRIPT_EXCEPTION( isolate, "invalid index parameter" );
283 Sampler sampler = SamplerApi::GetSamplerFromParams( PARAMETER_1, found, isolate, args );
286 DALI_SCRIPT_EXCEPTION( isolate, "missing sampler from param 1" );
290 material.SetTextureSampler(index, sampler);
295 * Set the uniform name of a given texture
296 * @method setTextureUniformName
298 * @param {integer} index The index of the texture in the array of textures
299 * @param {string} uniformName The new uniform name
301 void MaterialApi::SetTextureUniformName( const v8::FunctionCallbackInfo< v8::Value >& args )
303 v8::Isolate* isolate = args.GetIsolate();
304 v8::HandleScope handleScope( isolate );
306 Material material = GetMaterial( isolate, args );
309 int index = V8Utils::GetIntegerParameter( PARAMETER_0, found, isolate, args, 0 /* default */);
312 DALI_SCRIPT_EXCEPTION( isolate, "invalid index parameter" );
317 std::string uniformName = V8Utils::GetStringParameter( PARAMETER_1, found, isolate, args );
320 DALI_SCRIPT_EXCEPTION( isolate, "invalid uniform name parameter" );
324 material.SetTextureUniformName(index, uniformName);
329 * Retrive the index of a texture given its uniform name
330 * @method getTextureIndex
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
335 void MaterialApi::GetTextureIndex( const v8::FunctionCallbackInfo< v8::Value >& args )
337 v8::Isolate* isolate = args.GetIsolate();
338 v8::HandleScope handleScope( isolate );
340 Material material = GetMaterial( isolate, args );
343 std::string uniformName = V8Utils::GetStringParameter( PARAMETER_0, found, isolate, args );
346 DALI_SCRIPT_EXCEPTION( isolate, "invalid uniform name parameter" );
350 args.GetReturnValue().Set( v8::Integer::New( isolate, material.GetTextureIndex(uniformName) ) );
355 * Retrieve the number of textures used by the material
357 * @method getNumberOfTextures
359 * @return {integer} The number of textures
361 void MaterialApi::GetNumberOfTextures( const v8::FunctionCallbackInfo< v8::Value >& args )
363 v8::Isolate* isolate = args.GetIsolate();
364 v8::HandleScope handleScope( isolate );
366 Material material = GetMaterial( isolate, args );
368 args.GetReturnValue().Set( v8::Integer::New( isolate, material.GetNumberOfTextures() ) );
372 * Set the culling mode for this material
374 * @method setFaceCullingMode
376 * @param {integer} cullingMode The culling mode for this material
378 * // face culling mode is one of the following
379 * dali.MATERIAL_NONE // None of the faces should be culled
380 * dali.MATERIAL_CULL_BACK // Cull back face, back face should never be shown
381 * dali.MATERIAL_CULL_FRONT // Cull front face, back face should never be shown
382 * dali.MATERIAL_CULL_BACK_AND_FRONT // Cull back and front faces, if the geometry is composed of triangles none of the faces will be shown
384 * material.setFaceCullingMode( dali.MATERIAL_NONE );
386 void MaterialApi::SetFaceCullingMode( const v8::FunctionCallbackInfo< v8::Value >& args )
388 v8::Isolate* isolate = args.GetIsolate();
389 v8::HandleScope handleScope( isolate );
391 Material material = GetMaterial( isolate, args );
394 int mode = V8Utils::GetIntegerParameter( PARAMETER_0, found, isolate, args, 0);
397 DALI_SCRIPT_EXCEPTION( isolate, "invalid cullingMode parameter" );
401 material.SetFaceCullingMode( static_cast<Material::FaceCullingMode>(mode) );
406 * Set the blending mode.
408 * If blending is disabled (BLENDING_OFF) fade in and fade out animations do not work.
411 * @method setBlendMode
412 * @param { integer } mode The blending mode.
414 * // blend mode is one of the following
415 * dali.BLENDING_OFF // Blending is disabled.
416 * dali.BLENDING_AUTO // Blending is enabled if there is alpha channel.
417 * dali.BLENDING_ON // Blending is enabled.
419 * material.setBlendMode( dali.BLENDING_AUTO );
421 void MaterialApi::SetBlendMode( const v8::FunctionCallbackInfo< v8::Value >& args )
423 v8::Isolate* isolate = args.GetIsolate();
424 v8::HandleScope handleScope( isolate );
426 Material material = GetMaterial( isolate, args );
429 int mode = V8Utils::GetIntegerParameter( PARAMETER_0, found, isolate, args, 0 );
432 DALI_SCRIPT_EXCEPTION( isolate, "invalid blendMode parameter" );
436 material.SetBlendMode( static_cast<Dali::BlendingMode::Type>( mode ) );
441 * Retrieves the blending mode.
444 * @method getBlendMode
445 * @return { integer } blendMode
446 * @example returns one of the following:
448 * dali.BLENDING_OFF // Blending is disabled.
449 * dali.BLENDING_AUTO // Blending is enabled if there is alpha channel.
450 * dali.BLENDING_ON // Blending is enabled.
452 void MaterialApi::GetBlendMode( const v8::FunctionCallbackInfo<v8::Value>& args )
454 v8::Isolate* isolate = args.GetIsolate();
455 v8::HandleScope handleScope( isolate );
457 Material material = GetMaterial( isolate, args );
459 args.GetReturnValue().Set( v8::Integer::New( isolate, material.GetBlendMode() ) );
463 * Specify the pixel arithmetic used when the actor is blended.
466 * @method setBlendFunc
467 * @param {integer} srcFactorRgb Source Blending RGB
468 * @param {integer} destFactorRgb Destination Blending RGB
469 * @param {integer} srcFactorAlpha Source Blending Alpha
470 * @param {integer} destFactorAlpha Destinatino Blending Alpha
472 * //blending constants
473 * dali.BLEND_FACTOR_ZERO
474 * dali.BLEND_FACTOR_ONE
475 * dali.BLEND_FACTOR_SRC_COLOR
476 * dali.BLEND_FACTOR_ONE_MINUS_SRC_COLOR
477 * dali.BLEND_FACTOR_SRC_ALPHA
478 * dali.BLEND_FACTOR_ONE_MINUS_SRC_ALPHA
479 * dali.BLEND_FACTOR_DST_ALPHA
480 * dali.BLEND_FACTOR_ONE_MINUS_DST_ALPHA
481 * dali.BLEND_FACTOR_DST_COLOR
482 * dali.BLEND_FACTOR_ONE_MINUS_DST_COLOR
483 * dali.BLEND_FACTOR_SRC_ALPHA_SATURATE
484 * dali.BLEND_FACTOR_CONSTANT_COLOR
485 * dali.BLEND_FACTOR_ONE_MINUS_CONSTANT_COLOR
486 * dali.BLEND_FACTOR_CONSTANT_ALPHA
487 * dali.BLEND_FACTOR_ONE_MINUS_CONSTANT_ALPHA
489 * material.setBlendFunc( dali.BLEND_FACTOR_CONSTANT_COLOR, dali.BLEND_FACTOR_ONE_MINUS_CONSTANT_COLOR,
490 * dali.BLEND_FACTOR_CONSTANT_ALPHA, dali.BLEND_FACTOR_ONE_MINUS_CONSTANT_ALPHA );
492 void MaterialApi::SetBlendFunc( const v8::FunctionCallbackInfo< v8::Value >& args )
494 v8::Isolate* isolate = args.GetIsolate();
495 v8::HandleScope handleScope( isolate );
497 Material material = GetMaterial( isolate, args );
500 bool foundAllParams(false);
501 V8Utils::ReadIntegerArguments( foundAllParams, ¶ms[0], 4, args, 0 );
504 material.SetBlendFunc( static_cast< Dali::BlendingFactor::Type>(params[0]),
505 static_cast< Dali::BlendingFactor::Type>(params[1]),
506 static_cast< Dali::BlendingFactor::Type>(params[2]),
507 static_cast< Dali::BlendingFactor::Type>(params[3]) );
511 DALI_SCRIPT_EXCEPTION( isolate, "invalid blendFunc parameter");
516 * Query the pixel arithmetic used when the actor is blended.
519 * @method getBlendFunc
520 * @return {Object} Blend properties
521 * @example Blend properties object has 4 fields
523 * blendProperties.sourceRgb // source rgb enum
524 * blendProperties.destinationRgb // destination rgb enum
525 * blendProperties.sourceAlpha source // alpha enum
526 * blendProperties.destinationAlpha // destination alpha enum
528 void MaterialApi::GetBlendFunc( const v8::FunctionCallbackInfo< v8::Value >& args )
530 // Pass by reference doesn't work in Javascript
531 // For now just return a vector 4...
533 BlendingFactor::Type srcFactorRgb, destFactorRgb, srcFactorAlpha, destFactorAlpha;
534 v8::Isolate* isolate = args.GetIsolate();
535 v8::HandleScope handleScope( isolate );
537 Material material = GetMaterial( isolate, args );
539 material.GetBlendFunc( srcFactorRgb, destFactorRgb, srcFactorAlpha, destFactorAlpha );
541 v8::Local<v8::Object> blendProperties = v8::Object::New( isolate );
543 blendProperties->Set( v8::String::NewFromUtf8( isolate, "sourceRgb" ), v8::Integer::New( isolate, srcFactorRgb) );
544 blendProperties->Set( v8::String::NewFromUtf8( isolate, "destinationRgb" ), v8::Integer::New( isolate, destFactorRgb ) );
545 blendProperties->Set( v8::String::NewFromUtf8( isolate, "sourceAlpha" ), v8::Integer::New( isolate, srcFactorAlpha ) );
546 blendProperties->Set( v8::String::NewFromUtf8( isolate, "destinationAlpha" ), v8::Integer::New( isolate, destFactorAlpha ) );
548 args.GetReturnValue().Set( blendProperties );
552 * Specify the equation used when the actor is blended.
555 * @method setBlendEquation
556 * @param { integer } equationRgb The equation used for combining red, green, and blue components.
557 * @param { integer } equationAlpha The equation used for combining the alpha component.
559 * // blend equation is one of the following
560 * dali.BLEND_EQUATION_ADD
561 * dali.BLEND_EQUATION_SUBTRACT
562 * dali.BLEND_EQUATION_REVERSE_SUBTRACT
564 * material.setBlendEquation( dali.BLEND_EQUATION_ADD, dali.BLEND_EQUATION_REVERSE_SUBTRACT );
566 void MaterialApi::SetBlendEquation( const v8::FunctionCallbackInfo< v8::Value >& args )
568 v8::Isolate* isolate = args.GetIsolate();
569 v8::HandleScope handleScope( isolate );
571 Material material = GetMaterial( isolate, args );
574 bool foundAllParams(false);
575 V8Utils::ReadIntegerArguments( foundAllParams, ¶ms[0], 2, args, 0 );
578 material.SetBlendEquation( static_cast< BlendingEquation::Type>(params[0]), static_cast< BlendingEquation::Type>(params[1]) );
582 DALI_SCRIPT_EXCEPTION( isolate, "invalid BlendEquation parameter");
587 * Query the equation used when the actor is blended.
590 * @method getBlendEquation
591 * @return {Object} Blend equations
592 * @example Blend equations object has 2 fields
594 * blendEquations.equationRgb // equation used for combining rgb components
595 * blendEquations.equationAlpha // equation used for combining alpha components
597 void MaterialApi::GetBlendEquation( const v8::FunctionCallbackInfo< v8::Value >& args )
599 // Pass by reference doesn't work in Javascript
600 // For now just return a vector 2...
602 BlendingEquation::Type equationRgb, equationAlpha;
603 v8::Isolate* isolate = args.GetIsolate();
604 v8::HandleScope handleScope( isolate );
606 Material material = GetMaterial( isolate, args );
608 material.GetBlendEquation( equationRgb, equationAlpha );
610 v8::Local<v8::Object> blendEquations = v8::Object::New( isolate );
612 blendEquations->Set( v8::String::NewFromUtf8( isolate, "equationRgb" ), v8::Integer::New( isolate, equationRgb) );
613 blendEquations->Set( v8::String::NewFromUtf8( isolate, "equationAlpha" ), v8::Integer::New( isolate, equationAlpha ) );
615 args.GetReturnValue().Set( blendEquations );
618 } // namespace V8Plugin