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 * Establish if a given texture will affect the transparency of the material ( true by default )
330 * @method setTextureAffectsTransparency
332 * @param {integer} index The index of the texture in the array of textures
333 * @param {string} affectsTransparency True if the texture affects transparency, false otherwise
335 void MaterialApi::SetTextureAffectsTransparency( const v8::FunctionCallbackInfo< v8::Value >& args )
337 v8::Isolate* isolate = args.GetIsolate();
338 v8::HandleScope handleScope( isolate );
340 Material material = GetMaterial( isolate, args );
343 int index = V8Utils::GetIntegerParameter( PARAMETER_0, found, isolate, args, 0 /* default */);
346 DALI_SCRIPT_EXCEPTION( isolate, "invalid index parameter" );
351 bool affectsTransparency = V8Utils::GetBooleanParameter( PARAMETER_1, found, isolate, args );
354 DALI_SCRIPT_EXCEPTION( isolate, "invalid affectsTransparency parameter" );
358 material.SetTextureAffectsTransparency(index, affectsTransparency);
363 * Retrive the index of a texture given its uniform name
364 * @method getTextureIndex
366 * @param {string} uniformName The uniform name
367 * @return {integer} The index in the array of textures or -1 if the texture is not found
369 void MaterialApi::GetTextureIndex( const v8::FunctionCallbackInfo< v8::Value >& args )
371 v8::Isolate* isolate = args.GetIsolate();
372 v8::HandleScope handleScope( isolate );
374 Material material = GetMaterial( isolate, args );
377 std::string uniformName = V8Utils::GetStringParameter( PARAMETER_0, found, isolate, args );
380 DALI_SCRIPT_EXCEPTION( isolate, "invalid uniform name parameter" );
384 args.GetReturnValue().Set( v8::Integer::New( isolate, material.GetTextureIndex(uniformName) ) );
389 * Retrieve the number of textures used by the material
391 * @method getNumberOfTextures
393 * @return {integer} The number of textures
395 void MaterialApi::GetNumberOfTextures( const v8::FunctionCallbackInfo< v8::Value >& args )
397 v8::Isolate* isolate = args.GetIsolate();
398 v8::HandleScope handleScope( isolate );
400 Material material = GetMaterial( isolate, args );
402 args.GetReturnValue().Set( v8::Integer::New( isolate, material.GetNumberOfTextures() ) );
406 * Set the culling mode for this material
408 * @method setFaceCullingMode
410 * @param {integer} cullingMode The culling mode for this material
412 * // face culling mode is one of the following
413 * dali.MATERIAL_NONE // None of the faces should be culled
414 * dali.MATERIAL_CULL_BACK // Cull back face, back face should never be shown
415 * dali.MATERIAL_CULL_FRONT // Cull front face, back face should never be shown
416 * 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
418 * material.setFaceCullingMode( dali.MATERIAL_NONE );
420 void MaterialApi::SetFaceCullingMode( const v8::FunctionCallbackInfo< v8::Value >& args )
422 v8::Isolate* isolate = args.GetIsolate();
423 v8::HandleScope handleScope( isolate );
425 Material material = GetMaterial( isolate, args );
428 int mode = V8Utils::GetIntegerParameter( PARAMETER_0, found, isolate, args, 0);
431 DALI_SCRIPT_EXCEPTION( isolate, "invalid cullingMode parameter" );
435 material.SetFaceCullingMode( static_cast<Material::FaceCullingMode>(mode) );
440 * Set the blending mode.
442 * If blending is disabled (BLENDING_OFF) fade in and fade out animations do not work.
445 * @method setBlendMode
446 * @param { integer } mode The blending mode.
448 * // blend mode is one of the following
449 * dali.BLENDING_OFF // Blending is disabled.
450 * dali.BLENDING_AUTO // Blending is enabled if there is alpha channel.
451 * dali.BLENDING_ON // Blending is enabled.
453 * material.setBlendMode( dali.BLENDING_AUTO );
455 void MaterialApi::SetBlendMode( const v8::FunctionCallbackInfo< v8::Value >& args )
457 v8::Isolate* isolate = args.GetIsolate();
458 v8::HandleScope handleScope( isolate );
460 Material material = GetMaterial( isolate, args );
463 int mode = V8Utils::GetIntegerParameter( PARAMETER_0, found, isolate, args, 0 );
466 DALI_SCRIPT_EXCEPTION( isolate, "invalid blendMode parameter" );
470 material.SetBlendMode( static_cast<Dali::BlendingMode::Type>( mode ) );
475 * Retrieves the blending mode.
478 * @method getBlendMode
479 * @return { integer } blendMode
480 * @example returns one of the following:
482 * dali.BLENDING_OFF // Blending is disabled.
483 * dali.BLENDING_AUTO // Blending is enabled if there is alpha channel.
484 * dali.BLENDING_ON // Blending is enabled.
486 void MaterialApi::GetBlendMode( const v8::FunctionCallbackInfo<v8::Value>& args )
488 v8::Isolate* isolate = args.GetIsolate();
489 v8::HandleScope handleScope( isolate );
491 Material material = GetMaterial( isolate, args );
493 args.GetReturnValue().Set( v8::Integer::New( isolate, material.GetBlendMode() ) );
497 * Specify the pixel arithmetic used when the actor is blended.
500 * @method setBlendFunc
501 * @param {integer} srcFactorRgb Source Blending RGB
502 * @param {integer} destFactorRgb Destination Blending RGB
503 * @param {integer} srcFactorAlpha Source Blending Alpha
504 * @param {integer} destFactorAlpha Destinatino Blending Alpha
506 * //blending constants
507 * dali.BLEND_FACTOR_ZERO
508 * dali.BLEND_FACTOR_ONE
509 * dali.BLEND_FACTOR_SRC_COLOR
510 * dali.BLEND_FACTOR_ONE_MINUS_SRC_COLOR
511 * dali.BLEND_FACTOR_SRC_ALPHA
512 * dali.BLEND_FACTOR_ONE_MINUS_SRC_ALPHA
513 * dali.BLEND_FACTOR_DST_ALPHA
514 * dali.BLEND_FACTOR_ONE_MINUS_DST_ALPHA
515 * dali.BLEND_FACTOR_DST_COLOR
516 * dali.BLEND_FACTOR_ONE_MINUS_DST_COLOR
517 * dali.BLEND_FACTOR_SRC_ALPHA_SATURATE
518 * dali.BLEND_FACTOR_CONSTANT_COLOR
519 * dali.BLEND_FACTOR_ONE_MINUS_CONSTANT_COLOR
520 * dali.BLEND_FACTOR_CONSTANT_ALPHA
521 * dali.BLEND_FACTOR_ONE_MINUS_CONSTANT_ALPHA
523 * material.setBlendFunc( dali.BLEND_FACTOR_CONSTANT_COLOR, dali.BLEND_FACTOR_ONE_MINUS_CONSTANT_COLOR,
524 * dali.BLEND_FACTOR_CONSTANT_ALPHA, dali.BLEND_FACTOR_ONE_MINUS_CONSTANT_ALPHA );
526 void MaterialApi::SetBlendFunc( const v8::FunctionCallbackInfo< v8::Value >& args )
528 v8::Isolate* isolate = args.GetIsolate();
529 v8::HandleScope handleScope( isolate );
531 Material material = GetMaterial( isolate, args );
534 bool foundAllParams(false);
535 V8Utils::ReadIntegerArguments( foundAllParams, ¶ms[0], 4, args, 0 );
538 material.SetBlendFunc( static_cast< Dali::BlendingFactor::Type>(params[0]),
539 static_cast< Dali::BlendingFactor::Type>(params[1]),
540 static_cast< Dali::BlendingFactor::Type>(params[2]),
541 static_cast< Dali::BlendingFactor::Type>(params[3]) );
545 DALI_SCRIPT_EXCEPTION( isolate, "invalid blendFunc parameter");
550 * Query the pixel arithmetic used when the actor is blended.
553 * @method getBlendFunc
554 * @return {Object} Blend properties
555 * @example Blend properties object has 4 fields
557 * blendProperties.sourceRgb // source rgb enum
558 * blendProperties.destinationRgb // destination rgb enum
559 * blendProperties.sourceAlpha source // alpha enum
560 * blendProperties.destinationAlpha // destination alpha enum
562 void MaterialApi::GetBlendFunc( const v8::FunctionCallbackInfo< v8::Value >& args )
564 // Pass by reference doesn't work in Javascript
565 // For now just return a vector 4...
567 BlendingFactor::Type srcFactorRgb, destFactorRgb, srcFactorAlpha, destFactorAlpha;
568 v8::Isolate* isolate = args.GetIsolate();
569 v8::HandleScope handleScope( isolate );
571 Material material = GetMaterial( isolate, args );
573 material.GetBlendFunc( srcFactorRgb, destFactorRgb, srcFactorAlpha, destFactorAlpha );
575 v8::Local<v8::Object> blendProperties = v8::Object::New( isolate );
577 blendProperties->Set( v8::String::NewFromUtf8( isolate, "sourceRgb" ), v8::Integer::New( isolate, srcFactorRgb) );
578 blendProperties->Set( v8::String::NewFromUtf8( isolate, "destinationRgb" ), v8::Integer::New( isolate, destFactorRgb ) );
579 blendProperties->Set( v8::String::NewFromUtf8( isolate, "sourceAlpha" ), v8::Integer::New( isolate, srcFactorAlpha ) );
580 blendProperties->Set( v8::String::NewFromUtf8( isolate, "destinationAlpha" ), v8::Integer::New( isolate, destFactorAlpha ) );
582 args.GetReturnValue().Set( blendProperties );
586 * Specify the equation used when the actor is blended.
589 * @method setBlendEquation
590 * @param { integer } equationRgb The equation used for combining red, green, and blue components.
591 * @param { integer } equationAlpha The equation used for combining the alpha component.
593 * // blend equation is one of the following
594 * dali.BLEND_EQUATION_ADD
595 * dali.BLEND_EQUATION_SUBTRACT
596 * dali.BLEND_EQUATION_REVERSE_SUBTRACT
598 * material.setBlendEquation( dali.BLEND_EQUATION_ADD, dali.BLEND_EQUATION_REVERSE_SUBTRACT );
600 void MaterialApi::SetBlendEquation( const v8::FunctionCallbackInfo< v8::Value >& args )
602 v8::Isolate* isolate = args.GetIsolate();
603 v8::HandleScope handleScope( isolate );
605 Material material = GetMaterial( isolate, args );
608 bool foundAllParams(false);
609 V8Utils::ReadIntegerArguments( foundAllParams, ¶ms[0], 2, args, 0 );
612 material.SetBlendEquation( static_cast< BlendingEquation::Type>(params[0]), static_cast< BlendingEquation::Type>(params[1]) );
616 DALI_SCRIPT_EXCEPTION( isolate, "invalid BlendEquation parameter");
621 * Query the equation used when the actor is blended.
624 * @method getBlendEquation
625 * @return {Object} Blend equations
626 * @example Blend equations object has 2 fields
628 * blendEquations.equationRgb // equation used for combining rgb components
629 * blendEquations.equationAlpha // equation used for combining alpha components
631 void MaterialApi::GetBlendEquation( const v8::FunctionCallbackInfo< v8::Value >& args )
633 // Pass by reference doesn't work in Javascript
634 // For now just return a vector 2...
636 BlendingEquation::Type equationRgb, equationAlpha;
637 v8::Isolate* isolate = args.GetIsolate();
638 v8::HandleScope handleScope( isolate );
640 Material material = GetMaterial( isolate, args );
642 material.GetBlendEquation( equationRgb, equationAlpha );
644 v8::Local<v8::Object> blendEquations = v8::Object::New( isolate );
646 blendEquations->Set( v8::String::NewFromUtf8( isolate, "equationRgb" ), v8::Integer::New( isolate, equationRgb) );
647 blendEquations->Set( v8::String::NewFromUtf8( isolate, "equationAlpha" ), v8::Integer::New( isolate, equationAlpha ) );
649 args.GetReturnValue().Set( blendEquations );
652 } // namespace V8Plugin