2 * Copyright (c) 2020 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.
19 #include "model3d-view-impl.h"
22 #include <dali/public-api/object/type-registry.h>
23 #include <dali/public-api/animation/constraint.h>
24 #include <dali/public-api/animation/constraint-source.h>
25 #include <dali/public-api/animation/constraints.h>
26 #include <dali/public-api/object/type-registry-helper.h>
27 #include <dali-toolkit/public-api/controls/model3d-view/model3d-view.h>
28 #include <dali/devel-api/adaptor-framework/file-loader.h>
29 #include <dali/devel-api/adaptor-framework/image-loading.h>
32 #include <dali-toolkit/internal/controls/model3d-view/obj-loader.h>
46 // Texture indices are constants.
49 DIFFUSE_TEXTURE_INDEX,
55 * @brief Loads a texture from a file.
56 * @param[in] imageUrl The URL of the file
57 * @return A texture if loading succeeds, an empty handle otherwise
59 Texture LoadTexture( const char* imageUrl )
62 Devel::PixelBuffer pixelBuffer = LoadImageFromFile( imageUrl );
65 texture = Texture::New( TextureType::TEXTURE_2D, pixelBuffer.GetPixelFormat(), pixelBuffer.GetWidth(), pixelBuffer.GetHeight() );
66 PixelData pixelData = Devel::PixelBuffer::Convert( pixelBuffer );
67 texture.Upload( pixelData );
68 texture.GenerateMipmaps();
77 return Toolkit::Model3dView::New();
80 // Setup properties, signals and actions using the type-registry.
81 DALI_TYPE_REGISTRATION_BEGIN( Toolkit::Model3dView, Toolkit::Control, Create );
83 DALI_PROPERTY_REGISTRATION( Toolkit, Model3dView, "geometryUrl", STRING, GEOMETRY_URL)
84 DALI_PROPERTY_REGISTRATION( Toolkit, Model3dView, "materialUrl", STRING, MATERIAL_URL)
85 DALI_PROPERTY_REGISTRATION( Toolkit, Model3dView, "imagesUrl", STRING, IMAGES_URL)
86 DALI_PROPERTY_REGISTRATION( Toolkit, Model3dView, "illuminationType", INTEGER, ILLUMINATION_TYPE)
87 DALI_PROPERTY_REGISTRATION( Toolkit, Model3dView, "texture0Url", STRING, TEXTURE0_URL)
88 DALI_PROPERTY_REGISTRATION( Toolkit, Model3dView, "texture1Url", STRING, TEXTURE1_URL)
89 DALI_PROPERTY_REGISTRATION( Toolkit, Model3dView, "texture2Url", STRING, TEXTURE2_URL)
91 DALI_ANIMATABLE_PROPERTY_REGISTRATION( Toolkit, Model3dView, "lightPosition", VECTOR3, LIGHT_POSITION)
93 DALI_TYPE_REGISTRATION_END()
96 #define MAKE_SHADER(A)#A
98 // Diffuse illumination shader
100 const char* SIMPLE_VERTEX_SHADER = MAKE_SHADER(
101 attribute highp vec3 aPosition;\n
102 attribute highp vec2 aTexCoord;\n
103 attribute highp vec3 aNormal;\n
104 varying mediump vec3 vIllumination;\n
105 uniform mediump vec3 uSize;\n
106 uniform mediump mat4 uMvpMatrix;\n
107 uniform mediump mat4 uModelView;\n
108 uniform mediump mat3 uNormalMatrix;
109 uniform mediump mat4 uObjectMatrix;\n
110 uniform mediump vec3 uLightPosition;\n
114 vec4 vertexPosition = vec4(aPosition*min(uSize.x, uSize.y), 1.0);\n
115 vertexPosition = uObjectMatrix * vertexPosition;\n
116 vertexPosition = uMvpMatrix * vertexPosition;\n
118 //Illumination in Model-View space - Transform attributes and uniforms\n
119 vec4 vertPos = uModelView * vec4(aPosition.xyz, 1.0);\n
120 vec3 normal = uNormalMatrix * aNormal;\n
121 vec4 lightPos = uModelView * vec4(uLightPosition, 1.0);\n
122 vec3 vecToLight = normalize( lightPos.xyz - vertPos.xyz );\n
124 float lightDiffuse = max( dot( vecToLight, normal ), 0.0 );\n
125 vIllumination = vec3(lightDiffuse * 0.5 + 0.5);\n
127 gl_Position = vertexPosition;\n
131 const char* SIMPLE_FRAGMENT_SHADER = MAKE_SHADER(
132 precision mediump float;\n
133 varying mediump vec3 vIllumination;\n
134 uniform lowp vec4 uColor;\n
138 gl_FragColor = vec4( vIllumination.rgb * uColor.rgb, uColor.a);\n
142 // Diffuse and specular illumination shader with albedo texture
144 const char* VERTEX_SHADER = MAKE_SHADER(
145 attribute highp vec3 aPosition;\n
146 attribute highp vec2 aTexCoord;\n
147 attribute highp vec3 aNormal;\n
148 varying mediump vec2 vTexCoord;\n
149 varying mediump vec3 vIllumination;\n
150 varying mediump float vSpecular;\n
151 uniform mediump vec3 uSize;\n
152 uniform mediump mat4 uMvpMatrix;\n
153 uniform mediump mat4 uModelView;
154 uniform mediump mat3 uNormalMatrix;
155 uniform mediump mat4 uObjectMatrix;\n
156 uniform mediump vec3 uLightPosition;\n
160 vec4 vertexPosition = vec4(aPosition*min(uSize.x, uSize.y), 1.0);\n
161 vertexPosition = uObjectMatrix * vertexPosition;\n
162 vertexPosition = uMvpMatrix * vertexPosition;\n
164 //Illumination in Model-View space - Transform attributes and uniforms\n
165 vec4 vertPos = uModelView * vec4(aPosition.xyz, 1.0);\n
166 vec4 lightPos = uModelView * vec4(uLightPosition, 1.0);\n
167 vec3 normal = normalize(uNormalMatrix * aNormal);\n
169 vec3 vecToLight = normalize( lightPos.xyz - vertPos.xyz );\n
170 vec3 viewDir = normalize(-vertPos.xyz);
172 vec3 halfVector = normalize(viewDir + vecToLight);
174 float lightDiffuse = dot( vecToLight, normal );\n
175 lightDiffuse = max(0.0,lightDiffuse);\n
176 vIllumination = vec3(lightDiffuse * 0.5 + 0.5);\n
178 vec3 reflectDir = reflect(-vecToLight, normal);
179 vSpecular = pow( max(dot(reflectDir, viewDir), 0.0), 4.0 );
181 vTexCoord = aTexCoord;\n
182 gl_Position = vertexPosition;\n
186 const char* FRAGMENT_SHADER = MAKE_SHADER(
187 precision mediump float;\n
188 varying mediump vec2 vTexCoord;\n
189 varying mediump vec3 vIllumination;\n
190 varying mediump float vSpecular;\n
191 uniform sampler2D sDiffuse;\n
192 uniform lowp vec4 uColor;\n
196 vec4 texture = texture2D( sDiffuse, vTexCoord );\n
197 gl_FragColor = vec4( vIllumination.rgb * texture.rgb * uColor.rgb + vSpecular * 0.3, texture.a * uColor.a);\n
201 // Diffuse and specular illumination shader with albedo texture, normal map and gloss map shader
203 const char* NRMMAP_VERTEX_SHADER = MAKE_SHADER(
204 attribute highp vec3 aPosition;\n
205 attribute highp vec2 aTexCoord;\n
206 attribute highp vec3 aNormal;\n
207 attribute highp vec3 aTangent;\n
208 attribute highp vec3 aBiNormal;\n
209 varying mediump vec2 vTexCoord;\n
210 varying mediump vec3 vLightDirection;\n
211 varying mediump vec3 vHalfVector;\n
212 uniform mediump vec3 uSize;\n
213 uniform mediump mat4 uMvpMatrix;\n
214 uniform mediump mat4 uModelView;
215 uniform mediump mat3 uNormalMatrix;
216 uniform mediump mat4 uObjectMatrix;\n
217 uniform mediump vec3 uLightPosition;\n
221 vec4 vertexPosition = vec4(aPosition*min(uSize.x, uSize.y), 1.0);\n
222 vertexPosition = uObjectMatrix * vertexPosition;\n
223 vertexPosition = uMvpMatrix * vertexPosition;\n
225 vec4 vertPos = uModelView * vec4(aPosition.xyz, 1.0);\n
226 vec4 lightPos = uModelView * vec4(uLightPosition, 1.0);\n
228 vec3 tangent = normalize(uNormalMatrix * aTangent);
229 vec3 binormal = normalize(uNormalMatrix * aBiNormal);
230 vec3 normal = normalize(uNormalMatrix * aNormal);
232 vec3 vecToLight = normalize( lightPos.xyz - vertPos.xyz );\n
233 vLightDirection.x = dot(vecToLight, tangent);
234 vLightDirection.y = dot(vecToLight, binormal);
235 vLightDirection.z = dot(vecToLight, normal);
237 vec3 viewDir = normalize(-vertPos.xyz);
238 vec3 halfVector = normalize(viewDir + vecToLight);
239 vHalfVector.x = dot(halfVector, tangent);
240 vHalfVector.y = dot(halfVector, binormal);
241 vHalfVector.z = dot(halfVector, normal);
243 vTexCoord = aTexCoord;\n
244 gl_Position = vertexPosition;\n
248 const char* NRMMAP_FRAGMENT_SHADER = MAKE_SHADER(
249 precision mediump float;\n
250 varying mediump vec2 vTexCoord;\n
251 varying mediump vec3 vLightDirection;\n
252 varying mediump vec3 vHalfVector;\n
253 uniform sampler2D sDiffuse;\n
254 uniform sampler2D sNormal;\n
255 uniform sampler2D sGloss;\n
256 uniform lowp vec4 uColor;\n
260 vec4 texture = texture2D( sDiffuse, vTexCoord );\n
261 vec3 normal = normalize( texture2D( sNormal, vTexCoord ).xyz * 2.0 - 1.0 );\n
262 vec4 glossMap = texture2D( sGloss, vTexCoord );\n
264 float lightDiffuse = max( 0.0, dot( normal, normalize(vLightDirection) ) );\n
265 lightDiffuse = lightDiffuse * 0.5 + 0.5;\n
267 float shininess = pow (max (dot (normalize( vHalfVector ), normal), 0.0), 16.0) ;
269 gl_FragColor = vec4( texture.rgb * uColor.rgb * lightDiffuse + shininess * glossMap.rgb, texture.a * uColor.a);\n
274 } // anonymous namespace
276 using namespace Dali;
278 Model3dView::Model3dView()
279 : Control( ControlBehaviour( CONTROL_BEHAVIOUR_DEFAULT ) )
281 mIlluminationType = Toolkit::Model3dView::DIFFUSE_WITH_NORMAL_MAP;
283 mCameraFOV = Math::PI_OVER_180 * 45.f;
285 mControlSize = Vector2(100.,100.);
288 Model3dView::~Model3dView()
292 Toolkit::Model3dView Model3dView::New()
294 Model3dView* impl = new Model3dView();
296 Dali::Toolkit::Model3dView handle = Dali::Toolkit::Model3dView( *impl );
298 // Second-phase init of the implementation
299 // This can only be done after the CustomActor connection has been made...
305 void Model3dView::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
307 Toolkit::Model3dView model3dView = Toolkit::Model3dView::DownCast( Dali::BaseHandle( object ) );
311 Model3dView& impl( GetImpl( model3dView ) );
314 case Toolkit::Model3dView::Property::GEOMETRY_URL:
316 if( value.Get(impl.mObjUrl) )
319 impl.CreateGeometry();
323 case Toolkit::Model3dView::Property::MATERIAL_URL:
325 if( value.Get(impl.mTextureSetUrl) )
328 impl.CreateMaterial();
333 case Toolkit::Model3dView::Property::IMAGES_URL:
335 if( value.Get(impl.mImagesUrl) )
341 case Toolkit::Model3dView::Property::ILLUMINATION_TYPE:
343 int illuminationType;
344 if( value.Get(illuminationType) )
346 impl.mIlluminationType = Toolkit::Model3dView::IlluminationType(illuminationType);
347 impl.CreateGeometry();
348 impl.CreateMaterial();
353 case Toolkit::Model3dView::Property::TEXTURE0_URL:
355 value.Get(impl.mTexture0Url);
358 case Toolkit::Model3dView::Property::TEXTURE1_URL:
360 value.Get(impl.mTexture1Url);
363 case Toolkit::Model3dView::Property::TEXTURE2_URL:
365 value.Get(impl.mTexture2Url);
372 Property::Value Model3dView::GetProperty( BaseObject* object, Property::Index index )
374 Property::Value value;
376 Toolkit::Model3dView model3dView = Toolkit::Model3dView::DownCast( Dali::BaseHandle( object ) );
380 Model3dView& impl( GetImpl( model3dView ) );
383 case Toolkit::Model3dView::Property::GEOMETRY_URL:
385 value = impl.mObjUrl;
388 case Toolkit::Model3dView::Property::MATERIAL_URL:
390 value = impl.mTextureSetUrl;
393 case Toolkit::Model3dView::Property::IMAGES_URL:
395 value = impl.mImagesUrl;
398 case Toolkit::Model3dView::Property::ILLUMINATION_TYPE:
400 value = int(impl.mIlluminationType);
403 case Toolkit::Model3dView::Property::TEXTURE0_URL:
405 value = impl.mTexture0Url;
408 case Toolkit::Model3dView::Property::TEXTURE1_URL:
410 value = impl.mTexture1Url;
413 case Toolkit::Model3dView::Property::TEXTURE2_URL:
415 value = impl.mTexture2Url;
424 /////////////////////////////////////////////////////////////
427 void Model3dView::OnSceneConnection( int depth )
429 CustomActor self = Self();
430 self.AddRenderer( mRenderer );
432 if( mObjLoader.IsSceneLoaded() )
434 mMesh = mObjLoader.CreateGeometry( GetShaderProperties( mIlluminationType ), true );
439 mRenderer.SetGeometry( mMesh );
441 //create constraint for lightPosition Property with uLightPosition in the shader
442 Vector3 lightPosition( 0, 0, 0 );
443 Dali::Property::Index lightProperty = mShader.RegisterProperty( "uLightPosition", lightPosition );
444 Constraint constraint = Constraint::New<Vector3>( mShader, lightProperty, EqualToConstraint() );
445 constraint.AddSource( Source( self, Toolkit::Model3dView::Property::LIGHT_POSITION ) );
449 Control::OnSceneConnection( depth );
452 ///////////////////////////////////////////////////////////
457 void Model3dView::OnInitialize()
459 //Create empty versions of the geometry and material so we always have a Renderer
460 Geometry mesh = Geometry::New();
461 Shader shader = Shader::New( SIMPLE_VERTEX_SHADER, SIMPLE_FRAGMENT_SHADER );
462 mRenderer = Renderer::New( mesh, shader );
466 void Model3dView::LoadGeometry()
468 //Load file in adaptor
469 std::streampos fileSize;
470 Dali::Vector<char> fileContent;
472 if (FileLoader::ReadFile(mObjUrl,fileSize,fileContent,FileLoader::TEXT))
474 mObjLoader.ClearArrays();
475 mObjLoader.LoadObject(fileContent.Begin(), fileSize);
477 //Get size information from the obj loaded
478 mSceneCenter = mObjLoader.GetCenter();
479 mSceneSize = mObjLoader.GetSize();
487 void Model3dView::LoadMaterial()
489 //Load file in adaptor
490 std::streampos fileSize;
491 Dali::Vector<char> fileContent;
493 if( FileLoader::ReadFile(mTextureSetUrl, fileSize, fileContent, FileLoader::TEXT) )
495 mObjLoader.LoadMaterial(fileContent.Begin(), fileSize, mTexture0Url, mTexture1Url, mTexture2Url);
503 void Model3dView::Load()
509 void Model3dView::OnRelayout( const Vector2& size, RelayoutContainer& container )
514 void Model3dView::UpdateView()
516 if( mObjLoader.IsSceneLoaded() )
518 //The object will always be centred
521 scaleMatrix.SetIdentityAndScale(Vector3(1.0, -1.0, 1.0));
523 mShader.RegisterProperty( "uObjectMatrix", scaleMatrix );
527 void Model3dView::CreateGeometry()
529 if( mObjLoader.IsSceneLoaded() )
531 mMesh = mObjLoader.CreateGeometry( GetShaderProperties( mIlluminationType ), true );
535 mRenderer.SetGeometry( mMesh );
536 mRenderer.SetProperty( Renderer::Property::DEPTH_WRITE_MODE, DepthWriteMode::ON );
537 mRenderer.SetProperty( Renderer::Property::DEPTH_TEST_MODE, DepthTestMode::ON );
542 void Model3dView::UpdateShaderUniforms()
546 //Update shader related info, uniforms, etc. for the new shader
549 Vector3 lightPosition( 0, 0, 0 );
550 Dali::Property::Index lightProperty = mShader.RegisterProperty( "uLightPosition", lightPosition );
552 CustomActor self = Self();
554 //create constraint for lightPosition Property with uLightPosition in the shader
557 Constraint constraint = Constraint::New<Vector3>( mShader, lightProperty, EqualToConstraint() );
558 constraint.AddSource( Source( self, Toolkit::Model3dView::Property::LIGHT_POSITION ) );
564 void Model3dView::CreateMaterial()
566 if( mObjLoader.IsMaterialLoaded() && (mTexture0Url != "") && mObjLoader.IsTexturePresent() )
568 if( (mTexture2Url != "") && (mTexture1Url != "") && (mIlluminationType == Toolkit::Model3dView::DIFFUSE_WITH_NORMAL_MAP) )
570 mShader = Shader::New( NRMMAP_VERTEX_SHADER, NRMMAP_FRAGMENT_SHADER );
572 else if( mIlluminationType == Toolkit::Model3dView::DIFFUSE_WITH_TEXTURE ||
573 mIlluminationType == Toolkit::Model3dView::DIFFUSE_WITH_NORMAL_MAP )
575 mShader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER );
579 mShader = Shader::New( SIMPLE_VERTEX_SHADER, SIMPLE_FRAGMENT_SHADER );
584 mShader = Shader::New( SIMPLE_VERTEX_SHADER, SIMPLE_FRAGMENT_SHADER );
587 mTextureSet = TextureSet::New();
591 mRenderer.SetTextures( mTextureSet );
592 mRenderer.SetShader( mShader );
593 mRenderer.SetProperty( Renderer::Property::FACE_CULLING_MODE, FaceCullingMode::BACK );
596 UpdateShaderUniforms();
599 void Model3dView::LoadTextures()
606 Sampler sampler = Sampler::New();
607 sampler.SetFilterMode( FilterMode::LINEAR_MIPMAP_LINEAR, FilterMode::LINEAR_MIPMAP_LINEAR );
609 // Setup diffuse texture.
610 if( !mTexture0Url.empty() && ( mIlluminationType != Toolkit::Model3dView::DIFFUSE ) )
612 std::string imageUrl = mImagesUrl + mTexture0Url;
615 Texture diffuseTexture = LoadTexture( imageUrl.c_str() );
618 mTextureSet.SetTexture( DIFFUSE_TEXTURE_INDEX, diffuseTexture );
619 mTextureSet.SetSampler( DIFFUSE_TEXTURE_INDEX, sampler );
623 if( mIlluminationType == Toolkit::Model3dView::DIFFUSE_WITH_NORMAL_MAP )
625 // Setup normal map texture.
626 if( !mTexture1Url.empty() )
628 std::string imageUrl = mImagesUrl + mTexture1Url;
631 Texture normalTexture = LoadTexture( imageUrl.c_str() );
634 mTextureSet.SetTexture( NORMAL_TEXTURE_INDEX, normalTexture );
635 mTextureSet.SetSampler( NORMAL_TEXTURE_INDEX, sampler );
638 if( !mTexture2Url.empty() )
640 // Setup gloss map texture.
641 std::string imageUrl = mImagesUrl + mTexture2Url;
644 Texture glossTexture = LoadTexture( imageUrl.c_str() );
647 mTextureSet.SetTexture( GLOSS_TEXTURE_INDEX, glossTexture );
648 mTextureSet.SetSampler( GLOSS_TEXTURE_INDEX, sampler );
654 int Model3dView::GetShaderProperties( Toolkit::Model3dView::IlluminationType illuminationType )
656 int objectProperties = 0;
658 if( illuminationType == Toolkit::Model3dView::DIFFUSE_WITH_TEXTURE ||
659 illuminationType == Toolkit::Model3dView::DIFFUSE_WITH_NORMAL_MAP )
661 objectProperties |= ObjLoader::TEXTURE_COORDINATES;
664 if( illuminationType == Toolkit::Model3dView::DIFFUSE_WITH_NORMAL_MAP )
666 objectProperties |= ObjLoader::TANGENTS | ObjLoader::BINORMALS;
669 return objectProperties;
672 } // namespace Internal
673 } // namespace Toolkit