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>
33 #include <dali-toolkit/internal/controls/control/control-data-impl.h>
47 // Texture indices are constants.
50 DIFFUSE_TEXTURE_INDEX,
56 * @brief Loads a texture from a file.
57 * @param[in] imageUrl The URL of the file
58 * @return A texture if loading succeeds, an empty handle otherwise
60 Texture LoadTexture( const char* imageUrl )
63 Devel::PixelBuffer pixelBuffer = LoadImageFromFile( imageUrl );
66 texture = Texture::New( TextureType::TEXTURE_2D, pixelBuffer.GetPixelFormat(), pixelBuffer.GetWidth(), pixelBuffer.GetHeight() );
67 PixelData pixelData = Devel::PixelBuffer::Convert( pixelBuffer );
68 texture.Upload( pixelData );
69 texture.GenerateMipmaps();
78 return Toolkit::Model3dView::New();
81 // Setup properties, signals and actions using the type-registry.
82 DALI_TYPE_REGISTRATION_BEGIN( Toolkit::Model3dView, Toolkit::Control, Create );
84 DALI_PROPERTY_REGISTRATION( Toolkit, Model3dView, "geometryUrl", STRING, GEOMETRY_URL)
85 DALI_PROPERTY_REGISTRATION( Toolkit, Model3dView, "materialUrl", STRING, MATERIAL_URL)
86 DALI_PROPERTY_REGISTRATION( Toolkit, Model3dView, "imagesUrl", STRING, IMAGES_URL)
87 DALI_PROPERTY_REGISTRATION( Toolkit, Model3dView, "illuminationType", INTEGER, ILLUMINATION_TYPE)
88 DALI_PROPERTY_REGISTRATION( Toolkit, Model3dView, "texture0Url", STRING, TEXTURE0_URL)
89 DALI_PROPERTY_REGISTRATION( Toolkit, Model3dView, "texture1Url", STRING, TEXTURE1_URL)
90 DALI_PROPERTY_REGISTRATION( Toolkit, Model3dView, "texture2Url", STRING, TEXTURE2_URL)
92 DALI_ANIMATABLE_PROPERTY_REGISTRATION( Toolkit, Model3dView, "lightPosition", VECTOR3, LIGHT_POSITION)
94 DALI_TYPE_REGISTRATION_END()
97 #define MAKE_SHADER(A)#A
99 // Diffuse illumination shader
101 const char* SIMPLE_VERTEX_SHADER = MAKE_SHADER(
102 attribute highp vec3 aPosition;\n
103 attribute highp vec2 aTexCoord;\n
104 attribute highp vec3 aNormal;\n
105 varying mediump vec3 vIllumination;\n
106 uniform mediump vec3 uSize;\n
107 uniform mediump mat4 uMvpMatrix;\n
108 uniform mediump mat4 uModelView;\n
109 uniform mediump mat3 uNormalMatrix;
110 uniform mediump mat4 uObjectMatrix;\n
111 uniform mediump vec3 uLightPosition;\n
115 vec4 vertexPosition = vec4(aPosition*min(uSize.x, uSize.y), 1.0);\n
116 vertexPosition = uObjectMatrix * vertexPosition;\n
117 vertexPosition = uMvpMatrix * vertexPosition;\n
119 //Illumination in Model-View space - Transform attributes and uniforms\n
120 vec4 vertPos = uModelView * vec4(aPosition.xyz, 1.0);\n
121 vec3 normal = uNormalMatrix * aNormal;\n
122 vec4 lightPos = uModelView * vec4(uLightPosition, 1.0);\n
123 vec3 vecToLight = normalize( lightPos.xyz - vertPos.xyz );\n
125 float lightDiffuse = max( dot( vecToLight, normal ), 0.0 );\n
126 vIllumination = vec3(lightDiffuse * 0.5 + 0.5);\n
128 gl_Position = vertexPosition;\n
132 const char* SIMPLE_FRAGMENT_SHADER = MAKE_SHADER(
133 precision mediump float;\n
134 varying mediump vec3 vIllumination;\n
135 uniform lowp vec4 uColor;\n
139 gl_FragColor = vec4( vIllumination.rgb * uColor.rgb, uColor.a);\n
143 // Diffuse and specular illumination shader with albedo texture
145 const char* VERTEX_SHADER = MAKE_SHADER(
146 attribute highp vec3 aPosition;\n
147 attribute highp vec2 aTexCoord;\n
148 attribute highp vec3 aNormal;\n
149 varying mediump vec2 vTexCoord;\n
150 varying mediump vec3 vIllumination;\n
151 varying mediump float vSpecular;\n
152 uniform mediump vec3 uSize;\n
153 uniform mediump mat4 uMvpMatrix;\n
154 uniform mediump mat4 uModelView;
155 uniform mediump mat3 uNormalMatrix;
156 uniform mediump mat4 uObjectMatrix;\n
157 uniform mediump vec3 uLightPosition;\n
161 vec4 vertexPosition = vec4(aPosition*min(uSize.x, uSize.y), 1.0);\n
162 vertexPosition = uObjectMatrix * vertexPosition;\n
163 vertexPosition = uMvpMatrix * vertexPosition;\n
165 //Illumination in Model-View space - Transform attributes and uniforms\n
166 vec4 vertPos = uModelView * vec4(aPosition.xyz, 1.0);\n
167 vec4 lightPos = uModelView * vec4(uLightPosition, 1.0);\n
168 vec3 normal = normalize(uNormalMatrix * aNormal);\n
170 vec3 vecToLight = normalize( lightPos.xyz - vertPos.xyz );\n
171 vec3 viewDir = normalize(-vertPos.xyz);
173 vec3 halfVector = normalize(viewDir + vecToLight);
175 float lightDiffuse = dot( vecToLight, normal );\n
176 lightDiffuse = max(0.0,lightDiffuse);\n
177 vIllumination = vec3(lightDiffuse * 0.5 + 0.5);\n
179 vec3 reflectDir = reflect(-vecToLight, normal);
180 vSpecular = pow( max(dot(reflectDir, viewDir), 0.0), 4.0 );
182 vTexCoord = aTexCoord;\n
183 gl_Position = vertexPosition;\n
187 const char* FRAGMENT_SHADER = MAKE_SHADER(
188 precision mediump float;\n
189 varying mediump vec2 vTexCoord;\n
190 varying mediump vec3 vIllumination;\n
191 varying mediump float vSpecular;\n
192 uniform sampler2D sDiffuse;\n
193 uniform lowp vec4 uColor;\n
197 vec4 texture = texture2D( sDiffuse, vTexCoord );\n
198 gl_FragColor = vec4( vIllumination.rgb * texture.rgb * uColor.rgb + vSpecular * 0.3, texture.a * uColor.a);\n
202 // Diffuse and specular illumination shader with albedo texture, normal map and gloss map shader
204 const char* NRMMAP_VERTEX_SHADER = MAKE_SHADER(
205 attribute highp vec3 aPosition;\n
206 attribute highp vec2 aTexCoord;\n
207 attribute highp vec3 aNormal;\n
208 attribute highp vec3 aTangent;\n
209 attribute highp vec3 aBiNormal;\n
210 varying mediump vec2 vTexCoord;\n
211 varying mediump vec3 vLightDirection;\n
212 varying mediump vec3 vHalfVector;\n
213 uniform mediump vec3 uSize;\n
214 uniform mediump mat4 uMvpMatrix;\n
215 uniform mediump mat4 uModelView;
216 uniform mediump mat3 uNormalMatrix;
217 uniform mediump mat4 uObjectMatrix;\n
218 uniform mediump vec3 uLightPosition;\n
222 vec4 vertexPosition = vec4(aPosition*min(uSize.x, uSize.y), 1.0);\n
223 vertexPosition = uObjectMatrix * vertexPosition;\n
224 vertexPosition = uMvpMatrix * vertexPosition;\n
226 vec4 vertPos = uModelView * vec4(aPosition.xyz, 1.0);\n
227 vec4 lightPos = uModelView * vec4(uLightPosition, 1.0);\n
229 vec3 tangent = normalize(uNormalMatrix * aTangent);
230 vec3 binormal = normalize(uNormalMatrix * aBiNormal);
231 vec3 normal = normalize(uNormalMatrix * aNormal);
233 vec3 vecToLight = normalize( lightPos.xyz - vertPos.xyz );\n
234 vLightDirection.x = dot(vecToLight, tangent);
235 vLightDirection.y = dot(vecToLight, binormal);
236 vLightDirection.z = dot(vecToLight, normal);
238 vec3 viewDir = normalize(-vertPos.xyz);
239 vec3 halfVector = normalize(viewDir + vecToLight);
240 vHalfVector.x = dot(halfVector, tangent);
241 vHalfVector.y = dot(halfVector, binormal);
242 vHalfVector.z = dot(halfVector, normal);
244 vTexCoord = aTexCoord;\n
245 gl_Position = vertexPosition;\n
249 const char* NRMMAP_FRAGMENT_SHADER = MAKE_SHADER(
250 precision mediump float;\n
251 varying mediump vec2 vTexCoord;\n
252 varying mediump vec3 vLightDirection;\n
253 varying mediump vec3 vHalfVector;\n
254 uniform sampler2D sDiffuse;\n
255 uniform sampler2D sNormal;\n
256 uniform sampler2D sGloss;\n
257 uniform lowp vec4 uColor;\n
261 vec4 texture = texture2D( sDiffuse, vTexCoord );\n
262 vec3 normal = normalize( texture2D( sNormal, vTexCoord ).xyz * 2.0 - 1.0 );\n
263 vec4 glossMap = texture2D( sGloss, vTexCoord );\n
265 float lightDiffuse = max( 0.0, dot( normal, normalize(vLightDirection) ) );\n
266 lightDiffuse = lightDiffuse * 0.5 + 0.5;\n
268 float shininess = pow (max (dot (normalize( vHalfVector ), normal), 0.0), 16.0) ;
270 gl_FragColor = vec4( texture.rgb * uColor.rgb * lightDiffuse + shininess * glossMap.rgb, texture.a * uColor.a);\n
275 } // anonymous namespace
277 using namespace Dali;
279 Model3dView::Model3dView()
280 : Control( ControlBehaviour( CONTROL_BEHAVIOUR_DEFAULT ) )
282 mIlluminationType = Toolkit::Model3dView::DIFFUSE_WITH_NORMAL_MAP;
284 mCameraFOV = Math::PI_OVER_180 * 45.f;
286 mControlSize = Vector2(100.,100.);
289 Model3dView::~Model3dView()
293 Toolkit::Model3dView Model3dView::New()
295 Model3dView* impl = new Model3dView();
297 Dali::Toolkit::Model3dView handle = Dali::Toolkit::Model3dView( *impl );
299 // Second-phase init of the implementation
300 // This can only be done after the CustomActor connection has been made...
306 void Model3dView::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
308 Toolkit::Model3dView model3dView = Toolkit::Model3dView::DownCast( Dali::BaseHandle( object ) );
312 Model3dView& impl( GetImpl( model3dView ) );
315 case Toolkit::Model3dView::Property::GEOMETRY_URL:
317 if( value.Get(impl.mObjUrl) )
320 impl.CreateGeometry();
324 case Toolkit::Model3dView::Property::MATERIAL_URL:
326 if( value.Get(impl.mTextureSetUrl) )
329 impl.CreateMaterial();
334 case Toolkit::Model3dView::Property::IMAGES_URL:
336 if( value.Get(impl.mImagesUrl) )
342 case Toolkit::Model3dView::Property::ILLUMINATION_TYPE:
344 int illuminationType;
345 if( value.Get(illuminationType) )
347 impl.mIlluminationType = Toolkit::Model3dView::IlluminationType(illuminationType);
348 impl.CreateGeometry();
349 impl.CreateMaterial();
354 case Toolkit::Model3dView::Property::TEXTURE0_URL:
356 value.Get(impl.mTexture0Url);
359 case Toolkit::Model3dView::Property::TEXTURE1_URL:
361 value.Get(impl.mTexture1Url);
364 case Toolkit::Model3dView::Property::TEXTURE2_URL:
366 value.Get(impl.mTexture2Url);
373 Property::Value Model3dView::GetProperty( BaseObject* object, Property::Index index )
375 Property::Value value;
377 Toolkit::Model3dView model3dView = Toolkit::Model3dView::DownCast( Dali::BaseHandle( object ) );
381 Model3dView& impl( GetImpl( model3dView ) );
384 case Toolkit::Model3dView::Property::GEOMETRY_URL:
386 value = impl.mObjUrl;
389 case Toolkit::Model3dView::Property::MATERIAL_URL:
391 value = impl.mTextureSetUrl;
394 case Toolkit::Model3dView::Property::IMAGES_URL:
396 value = impl.mImagesUrl;
399 case Toolkit::Model3dView::Property::ILLUMINATION_TYPE:
401 value = int(impl.mIlluminationType);
404 case Toolkit::Model3dView::Property::TEXTURE0_URL:
406 value = impl.mTexture0Url;
409 case Toolkit::Model3dView::Property::TEXTURE1_URL:
411 value = impl.mTexture1Url;
414 case Toolkit::Model3dView::Property::TEXTURE2_URL:
416 value = impl.mTexture2Url;
425 /////////////////////////////////////////////////////////////
428 void Model3dView::OnSceneConnection( int depth )
430 CustomActor self = Self();
431 self.AddRenderer( mRenderer );
433 if( mObjLoader.IsSceneLoaded() )
435 mMesh = mObjLoader.CreateGeometry( GetShaderProperties( mIlluminationType ), true );
440 mRenderer.SetGeometry( mMesh );
442 //create constraint for lightPosition Property with uLightPosition in the shader
443 Vector3 lightPosition( 0, 0, 0 );
444 Dali::Property::Index lightProperty = mShader.RegisterProperty( "uLightPosition", lightPosition );
445 Constraint constraint = Constraint::New<Vector3>( mShader, lightProperty, EqualToConstraint() );
446 constraint.AddSource( Source( self, Toolkit::Model3dView::Property::LIGHT_POSITION ) );
450 Control::OnSceneConnection( depth );
453 ///////////////////////////////////////////////////////////
458 void Model3dView::OnInitialize()
460 //Create empty versions of the geometry and material so we always have a Renderer
461 Geometry mesh = Geometry::New();
462 Shader shader = Shader::New( SIMPLE_VERTEX_SHADER, SIMPLE_FRAGMENT_SHADER );
463 mRenderer = Renderer::New( mesh, shader );
465 DevelControl::SetAccessibilityConstructor( Self(), []( Dali::Actor actor ) {
466 return std::unique_ptr< Dali::Accessibility::Accessible >(
467 new Control::Impl::AccessibleImpl( actor, Dali::Accessibility::Role::IMAGE ) );
471 void Model3dView::LoadGeometry()
473 //Load file in adaptor
474 std::streampos fileSize;
475 Dali::Vector<char> fileContent;
477 if (FileLoader::ReadFile(mObjUrl,fileSize,fileContent,FileLoader::TEXT))
479 mObjLoader.ClearArrays();
480 mObjLoader.LoadObject(fileContent.Begin(), fileSize);
482 //Get size information from the obj loaded
483 mSceneCenter = mObjLoader.GetCenter();
484 mSceneSize = mObjLoader.GetSize();
492 void Model3dView::LoadMaterial()
494 //Load file in adaptor
495 std::streampos fileSize;
496 Dali::Vector<char> fileContent;
498 if( FileLoader::ReadFile(mTextureSetUrl, fileSize, fileContent, FileLoader::TEXT) )
500 mObjLoader.LoadMaterial(fileContent.Begin(), fileSize, mTexture0Url, mTexture1Url, mTexture2Url);
508 void Model3dView::Load()
514 void Model3dView::OnRelayout( const Vector2& size, RelayoutContainer& container )
519 void Model3dView::UpdateView()
521 if( mObjLoader.IsSceneLoaded() )
523 //The object will always be centred
526 scaleMatrix.SetIdentityAndScale(Vector3(1.0, -1.0, 1.0));
528 mShader.RegisterProperty( "uObjectMatrix", scaleMatrix );
532 void Model3dView::CreateGeometry()
534 if( mObjLoader.IsSceneLoaded() )
536 mMesh = mObjLoader.CreateGeometry( GetShaderProperties( mIlluminationType ), true );
540 mRenderer.SetGeometry( mMesh );
541 mRenderer.SetProperty( Renderer::Property::DEPTH_WRITE_MODE, DepthWriteMode::ON );
542 mRenderer.SetProperty( Renderer::Property::DEPTH_TEST_MODE, DepthTestMode::ON );
547 void Model3dView::UpdateShaderUniforms()
551 //Update shader related info, uniforms, etc. for the new shader
554 Vector3 lightPosition( 0, 0, 0 );
555 Dali::Property::Index lightProperty = mShader.RegisterProperty( "uLightPosition", lightPosition );
557 CustomActor self = Self();
559 //create constraint for lightPosition Property with uLightPosition in the shader
562 Constraint constraint = Constraint::New<Vector3>( mShader, lightProperty, EqualToConstraint() );
563 constraint.AddSource( Source( self, Toolkit::Model3dView::Property::LIGHT_POSITION ) );
569 void Model3dView::CreateMaterial()
571 if( mObjLoader.IsMaterialLoaded() && (mTexture0Url != "") && mObjLoader.IsTexturePresent() )
573 if( (mTexture2Url != "") && (mTexture1Url != "") && (mIlluminationType == Toolkit::Model3dView::DIFFUSE_WITH_NORMAL_MAP) )
575 mShader = Shader::New( NRMMAP_VERTEX_SHADER, NRMMAP_FRAGMENT_SHADER );
577 else if( mIlluminationType == Toolkit::Model3dView::DIFFUSE_WITH_TEXTURE ||
578 mIlluminationType == Toolkit::Model3dView::DIFFUSE_WITH_NORMAL_MAP )
580 mShader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER );
584 mShader = Shader::New( SIMPLE_VERTEX_SHADER, SIMPLE_FRAGMENT_SHADER );
589 mShader = Shader::New( SIMPLE_VERTEX_SHADER, SIMPLE_FRAGMENT_SHADER );
592 mTextureSet = TextureSet::New();
596 mRenderer.SetTextures( mTextureSet );
597 mRenderer.SetShader( mShader );
598 mRenderer.SetProperty( Renderer::Property::FACE_CULLING_MODE, FaceCullingMode::BACK );
601 UpdateShaderUniforms();
604 void Model3dView::LoadTextures()
611 Sampler sampler = Sampler::New();
612 sampler.SetFilterMode( FilterMode::LINEAR_MIPMAP_LINEAR, FilterMode::LINEAR_MIPMAP_LINEAR );
614 // Setup diffuse texture.
615 if( !mTexture0Url.empty() && ( mIlluminationType != Toolkit::Model3dView::DIFFUSE ) )
617 std::string imageUrl = mImagesUrl + mTexture0Url;
620 Texture diffuseTexture = LoadTexture( imageUrl.c_str() );
623 mTextureSet.SetTexture( DIFFUSE_TEXTURE_INDEX, diffuseTexture );
624 mTextureSet.SetSampler( DIFFUSE_TEXTURE_INDEX, sampler );
628 if( mIlluminationType == Toolkit::Model3dView::DIFFUSE_WITH_NORMAL_MAP )
630 // Setup normal map texture.
631 if( !mTexture1Url.empty() )
633 std::string imageUrl = mImagesUrl + mTexture1Url;
636 Texture normalTexture = LoadTexture( imageUrl.c_str() );
639 mTextureSet.SetTexture( NORMAL_TEXTURE_INDEX, normalTexture );
640 mTextureSet.SetSampler( NORMAL_TEXTURE_INDEX, sampler );
643 if( !mTexture2Url.empty() )
645 // Setup gloss map texture.
646 std::string imageUrl = mImagesUrl + mTexture2Url;
649 Texture glossTexture = LoadTexture( imageUrl.c_str() );
652 mTextureSet.SetTexture( GLOSS_TEXTURE_INDEX, glossTexture );
653 mTextureSet.SetSampler( GLOSS_TEXTURE_INDEX, sampler );
659 int Model3dView::GetShaderProperties( Toolkit::Model3dView::IlluminationType illuminationType )
661 int objectProperties = 0;
663 if( illuminationType == Toolkit::Model3dView::DIFFUSE_WITH_TEXTURE ||
664 illuminationType == Toolkit::Model3dView::DIFFUSE_WITH_NORMAL_MAP )
666 objectProperties |= ObjLoader::TEXTURE_COORDINATES;
669 if( illuminationType == Toolkit::Model3dView::DIFFUSE_WITH_NORMAL_MAP )
671 objectProperties |= ObjLoader::TANGENTS | ObjLoader::BINORMALS;
674 return objectProperties;
677 } // namespace Internal
678 } // namespace Toolkit