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() ) );
371 } // namespace V8Plugin