2 * Copyright (c) 2018 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.
18 #include <dali-toolkit/internal/controls/scene/gltf-loader.h>
19 #include <dali-toolkit/internal/controls/scene/gltf-shader.h>
23 #include <dali/integration-api/debug.h>
24 #include <dali/devel-api/adaptor-framework/image-loading.h>
25 #include <dali/devel-api/adaptor-framework/file-loader.h>
28 #include <dali-toolkit/devel-api/image-loader/texture-manager.h>
30 using namespace Dali::Toolkit::Internal::GLTF;
41 GltfLoader::GltfLoader()
47 GltfLoader::~GltfLoader()
51 bool GltfLoader::LoadScene( const std::string& filePath, Internal::Scene& scene )
53 // Extracting directory path from full path to load resources.
54 if( std::string::npos != filePath.rfind('/') )
56 mPath = filePath.substr( 0, filePath.rfind('/') ) + "/";
59 if( !ParseGltf( filePath ) )
61 DALI_LOG_ERROR( "Fail to parse json file\n" );
65 mRoot = mParser.GetRoot();
76 if( !CreateScene( scene ) )
83 bool GltfLoader::ParseGltf( const std::string& filePath )
85 std::ifstream fileStream( filePath.c_str() );
86 std::string fileBuffer( ( std::istreambuf_iterator<char>( fileStream ) ),
87 ( std::istreambuf_iterator<char>() ) );
88 mParser = Dali::Toolkit::JsonParser::New();
90 return mParser.Parse( fileBuffer );
93 bool GltfLoader::LoadAssets()
95 if( LoadBinaryData( mRoot ) && // pass a reference
96 LoadTextureArray( mRoot ) && // pass a reference
97 LoadMaterialSetArray( mRoot ) && // pass a reference
98 LoadMeshArray( mRoot ) // pass a reference
106 bool GltfLoader::LoadBinaryData( const TreeNode* root )
108 const TreeNode* buffersNode = root->GetChild( "buffers" );
113 for( TreeNode::ConstIterator bufferIter = ( *buffersNode ).CBegin(); bufferIter != ( *buffersNode ).CEnd(); ++bufferIter )
115 LoadBuffer( ( *bufferIter ).second );
118 const TreeNode* bufferViewsNode = root->GetChild( "bufferViews" );
119 if( !bufferViewsNode )
123 for( TreeNode::ConstIterator bufferViewIter = ( *bufferViewsNode ).CBegin(); bufferViewIter != ( *bufferViewsNode ).CEnd(); ++bufferViewIter )
125 LoadBufferView( ( *bufferViewIter ).second );
128 const TreeNode* accessorsNode = root->GetChild( "accessors" );
133 for( TreeNode::ConstIterator accesorIter = ( *accessorsNode ).CBegin(); accesorIter != ( *accessorsNode ).CEnd(); ++accesorIter )
135 LoadAccessor( ( *accesorIter ).second );
141 bool GltfLoader::LoadBuffer( const TreeNode& buffer )
143 BufferInfo bufferInfo;
145 const TreeNode* uriNode = buffer.GetChild( "uri" );
148 ReadString( uriNode, bufferInfo.uri );
151 const TreeNode* byteLengthNode = buffer.GetChild( "byteLength" );
154 ReadInt( byteLengthNode, bufferInfo.byteLength );
155 if( bufferInfo.byteLength < 0 )
161 const TreeNode* nameNode = buffer.GetChild( "name" );
164 ReadString( nameNode, bufferInfo.name );
167 mBufferArray.push_back( bufferInfo );
172 bool GltfLoader::LoadBufferView( const TreeNode& buffer )
174 BufferViewInfo bufferViewInfo;
176 const TreeNode* bufferNode = buffer.GetChild( "buffer" );
179 ReadInt( bufferNode, bufferViewInfo.buffer );
180 if( bufferViewInfo.buffer < 0 )
186 const TreeNode* byteOffsetNode = buffer.GetChild( "byteOffset" );
189 ReadInt( byteOffsetNode, bufferViewInfo.byteOffset );
192 const TreeNode* byteLengthNode = buffer.GetChild( "byteLength" );
195 ReadInt( byteLengthNode, bufferViewInfo.byteLength );
196 if( bufferViewInfo.byteLength < 0 )
202 const TreeNode* byteStrideNode = buffer.GetChild( "byteStride" );
205 ReadInt( byteStrideNode, bufferViewInfo.byteStride );
208 const TreeNode* targetNode = buffer.GetChild( "target" );
211 ReadInt( targetNode, bufferViewInfo.target );
214 const TreeNode* nameNode = buffer.GetChild( "name" );
217 ReadString( nameNode, bufferViewInfo.name );
220 mBufferViewArray.push_back( bufferViewInfo );
225 bool GltfLoader::LoadAccessor( const TreeNode& buffer )
227 AccessorInfo accessorInfo;
229 const TreeNode* bufferViewNode = buffer.GetChild( "bufferView" );
232 ReadInt( bufferViewNode, accessorInfo.bufferView );
235 const TreeNode* byteOffsetNode = buffer.GetChild( "byteOffset" );
238 ReadInt( byteOffsetNode, accessorInfo.byteOffset );
241 const TreeNode* componentTypeNode = buffer.GetChild( "componentType" );
242 if( componentTypeNode )
244 ReadInt( componentTypeNode, accessorInfo.componentType );
245 if( accessorInfo.componentType < 0 )
251 const TreeNode* normalizedNode = buffer.GetChild( "normalized" );
254 ReadBool( normalizedNode, accessorInfo.normalized );
257 const TreeNode* countNode = buffer.GetChild( "count" );
260 ReadInt( countNode, accessorInfo.count );
261 if( accessorInfo.count < 0 )
267 const TreeNode* typeNode = buffer.GetChild( "type" );
270 ReadString( typeNode, accessorInfo.type );
271 if( accessorInfo.type == "" )
277 const TreeNode* maxNode = buffer.GetChild( "max" );
280 ReadInt( maxNode, accessorInfo.max );
283 const TreeNode* minNode = buffer.GetChild( "min" );
286 ReadInt( minNode, accessorInfo.min );
289 const TreeNode* nameNode = buffer.GetChild( "name" );
292 ReadString( nameNode, accessorInfo.name );
295 mAccessorArray.push_back( accessorInfo );
300 bool GltfLoader::LoadTextureArray( const TreeNode* root )
302 const TreeNode* imagesNode = root->GetChild( "images" );
305 for( TreeNode::ConstIterator imageIter = imagesNode->CBegin(); imageIter != imagesNode->CEnd(); ++imageIter )
307 std::string imageUrl;
308 const TreeNode* uriNode = ( &( ( *imageIter ).second ) )->GetChild( "uri" );
312 ReadString( uriNode, uri );
313 imageUrl = mPath + uri;
316 mSourceArray.push_back( LoadTexture( imageUrl.c_str(), true ) );
320 const TreeNode* samplersNode = root->GetChild( "samplers" );
323 for( TreeNode::ConstIterator samplerIter = samplersNode->CBegin(); samplerIter != samplersNode->CEnd(); ++samplerIter )
325 mSamplerArray.push_back( LoadSampler( ( ( *samplerIter ).second ) ) );
329 const TreeNode* texturesNode = root->GetChild( "textures" );
332 for( TreeNode::ConstIterator textureIter = texturesNode->CBegin(); textureIter != texturesNode->CEnd(); ++textureIter )
334 const TreeNode* TextureNode = &( ( *textureIter ).second );
337 const TreeNode* sourceNode = TextureNode->GetChild( "source" );
340 ReadInt( sourceNode, texture.sourceIdx );
343 const TreeNode* samplerNode = TextureNode->GetChild( "sampler" );
346 ReadInt( samplerNode, texture.samplerIdx );
349 mTextureArray.push_back( texture );
355 Texture GltfLoader::LoadTexture( const char* imageUrl, bool generateMipmaps )
358 Devel::PixelBuffer pixelBuffer = LoadImageFromFile( imageUrl );
361 texture = Texture::New( TextureType::TEXTURE_2D, pixelBuffer.GetPixelFormat(), pixelBuffer.GetWidth(), pixelBuffer.GetHeight() );
362 PixelData pixelData = Devel::PixelBuffer::Convert( pixelBuffer );
363 texture.Upload( pixelData );
365 if( generateMipmaps )
367 texture.GenerateMipmaps();
374 Sampler GltfLoader::LoadSampler( const TreeNode& samplerNode )
376 Sampler sampler = Sampler::New();
378 FilterMode::Type minFilter = FilterMode::DEFAULT;
379 FilterMode::Type magFilter = FilterMode::DEFAULT;
380 const TreeNode* magFilterNode = samplerNode.GetChild( "magFilter" );
383 int magFilter_integer = 0;
384 ReadInt( magFilterNode, magFilter_integer );
385 magFilter = GetFilterMode( magFilter_integer );
388 const TreeNode* minFilterNode = samplerNode.GetChild( "minFilter" );
391 int minFilter_integer = 0;
392 ReadInt( minFilterNode, minFilter_integer );
393 minFilter = GetFilterMode( minFilter_integer );
396 WrapMode::Type wrapR = WrapMode::REPEAT;
397 WrapMode::Type wrapS = WrapMode::REPEAT;
398 WrapMode::Type wrapT = WrapMode::REPEAT;
399 const TreeNode* wrapNode = samplerNode.GetChild( "wrapS" );
402 wrapS = GetWrapMode( wrapNode->GetInteger() );
405 wrapNode = samplerNode.GetChild( "wrapT" );
408 wrapT = GetWrapMode( wrapNode->GetInteger() );
411 sampler.SetFilterMode( minFilter, magFilter );
412 sampler.SetWrapMode( wrapR, wrapS, wrapT );
417 FilterMode::Type GltfLoader::GetFilterMode( int mode )
419 FilterMode::Type retValue = FilterMode::DEFAULT;
421 * glTF 2.0 Specification
425 * 9984 : NEAREST_MIPMAP_NEAREST
426 * 9985 : LINEAR_MIPMAP_NEAREST
427 * 9986 : NEAREST_MIPMAP_LINEAR
428 * 9987 : LINEAR_MIPMAP_LINEAR
434 retValue = FilterMode::NEAREST;
439 retValue = FilterMode::LINEAR;
444 retValue = FilterMode::NEAREST_MIPMAP_NEAREST;
449 retValue = FilterMode::LINEAR_MIPMAP_NEAREST;
454 retValue = FilterMode::NEAREST_MIPMAP_LINEAR;
459 retValue = FilterMode::LINEAR_MIPMAP_LINEAR;
467 WrapMode::Type GltfLoader::GetWrapMode( int mode )
469 WrapMode::Type retValue = WrapMode::REPEAT;
471 * glTF 2.0 Specification
473 * 33071 : CLAMP_TO_EDGE
474 * 33648 : MIRRORED_REPEAT
481 retValue = WrapMode::CLAMP_TO_EDGE;
486 retValue = WrapMode::MIRRORED_REPEAT;
491 retValue = WrapMode::REPEAT;
499 bool GltfLoader::LoadMaterialSetArray( const TreeNode* root )
501 const TreeNode* materialsNode = root->GetChild( "materials" );
507 for( TreeNode::ConstIterator materialIter = materialsNode->CBegin(); materialIter != materialsNode->CEnd(); ++materialIter )
509 GLTF::MaterialInfo materialInfo;
510 LoadPbrMetallicRoughness( ( ( *materialIter ).second ), materialInfo );
512 const TreeNode* materialNode = &( ( *materialIter ).second );
513 const TreeNode* tempNode = materialNode->GetChild( "name" );
516 ReadString( tempNode, materialInfo.name );
519 materialInfo.alphaMode = "OPAQUE";
520 tempNode = materialNode->GetChild( "alphaMode" );
523 ReadString( tempNode, materialInfo.alphaMode );
526 materialInfo.alphaCutoff = 1.0;
527 tempNode = materialNode->GetChild( "alphaCutoff" );
530 ReadFloat( tempNode, materialInfo.alphaCutoff );
533 materialInfo.doubleSided = false;
534 tempNode = materialNode->GetChild( "doubleSided" );
537 ReadBool( tempNode, materialInfo.doubleSided );
541 tempNode = materialNode->GetChild( "emissiveFactor" );
542 if( tempNode && ReadVector( tempNode, floatVec, 3 ) )
544 materialInfo.emissiveFactor = Vector3( floatVec[0], floatVec[1], floatVec[2] );
547 const TreeNode* texture = materialNode->GetChild( "normalTexture" );
550 tempNode = texture->GetChild( "index" );
553 materialInfo.normalTexture.index = tempNode->GetInteger();
556 tempNode = texture->GetChild( "texCoord" );
559 materialInfo.normalTexture.texCoord = tempNode->GetInteger();
562 materialInfo.normalTexture.value = 1.0;
563 tempNode = texture->GetChild( "scale" );
566 ReadFloat( tempNode, materialInfo.normalTexture.value );
570 texture = materialNode->GetChild( "occlusionTexture" );
573 tempNode = texture->GetChild( "index" );
576 materialInfo.occlusionTexture.index = tempNode->GetInteger();
579 tempNode = texture->GetChild( "texCoord" );
582 materialInfo.occlusionTexture.texCoord = tempNode->GetInteger();
586 tempNode = texture->GetChild( "strength" );
589 ReadFloat( tempNode, materialInfo.occlusionTexture.value );
593 texture = materialNode->GetChild( "emissiveTexture" );
596 tempNode = texture->GetChild( "index" );
599 materialInfo.emissiveTexture.index = tempNode->GetInteger();
602 tempNode = texture->GetChild( "texCoord" );
605 materialInfo.emissiveTexture.texCoord = tempNode->GetInteger();
608 mMaterialArray.push_back( materialInfo );
613 bool GltfLoader::LoadPbrMetallicRoughness( const TreeNode& material, MaterialInfo& materialInfo )
616 const TreeNode* pbrMetallicRoughnessNode = material.GetChild( "pbrMetallicRoughness" );
617 if( !pbrMetallicRoughnessNode )
622 const TreeNode* tempNode;
623 tempNode = pbrMetallicRoughnessNode->GetChild( "metallicFactor" );
626 ReadFloat( tempNode, materialInfo.metallicFactor );
629 tempNode = pbrMetallicRoughnessNode->GetChild( "roughnessFactor" );
632 ReadFloat( tempNode, materialInfo.roughnessFactor );
635 tempNode = pbrMetallicRoughnessNode->GetChild( "baseColorFactor" );
636 if( tempNode && ReadVector( tempNode, floatVec, 4 ) )
638 materialInfo.baseColorFactor = Vector4( floatVec[0], floatVec[1], floatVec[2], floatVec[3] );
641 const TreeNode* baseColorTextureNode = pbrMetallicRoughnessNode->GetChild( "baseColorTexture" );
642 if( baseColorTextureNode )
644 tempNode = baseColorTextureNode->GetChild( "index" );
647 materialInfo.baseColorTexture.index = tempNode->GetInteger();
650 tempNode = baseColorTextureNode->GetChild( "texCoord" );
653 materialInfo.baseColorTexture.texCoord = tempNode->GetInteger();
657 const TreeNode* metallicRoughnessTextureNode = pbrMetallicRoughnessNode->GetChild( "metallicRoughnessTexture" );
658 if( metallicRoughnessTextureNode )
660 tempNode = metallicRoughnessTextureNode->GetChild( "index" );
663 materialInfo.metallicRoughnessTexture.index = tempNode->GetInteger();
666 tempNode = metallicRoughnessTextureNode->GetChild( "texCoord" );
669 materialInfo.metallicRoughnessTexture.texCoord = tempNode->GetInteger();
676 bool GltfLoader::LoadMeshArray( const TreeNode* root )
678 const TreeNode* meshesNode = root->GetChild( "meshes" );
684 for( TreeNode::ConstIterator meshIter = ( *meshesNode ).CBegin(); meshIter != ( *meshesNode ).CEnd(); ++meshIter )
687 const TreeNode* nameNode = ( &( *meshIter ).second )->GetChild( "name" );
690 ReadString( nameNode, meshInfo.name );
692 meshInfo.geometry = Geometry::New();
694 //Need to add weights for Morph targets.
695 LoadPrimitive( ( *meshIter ).second, meshInfo );
696 SetGeometry( meshInfo );
697 mMeshArray.push_back( meshInfo );
703 bool GltfLoader::LoadPrimitive( const TreeNode& mesh, MeshInfo& meshInfo )
705 const TreeNode* primitivesNode = mesh.GetChild( "primitives" );
706 if( !primitivesNode )
711 for( TreeNode::ConstIterator primitiveIter = ( *primitivesNode ).CBegin(); primitiveIter != ( *primitivesNode ).CEnd(); ++primitiveIter )
713 const TreeNode* primitiveNode = ( &( *primitiveIter ).second );
714 const TreeNode* tempNode;
716 tempNode = primitiveNode->GetChild( "indices" );
719 meshInfo.indicesIdx = tempNode->GetInteger();
722 tempNode = primitiveNode->GetChild( "material" );
725 meshInfo.materialsIdx = tempNode->GetInteger();
728 tempNode = primitiveNode->GetChild( "mode" );
731 meshInfo.mode = tempNode->GetInteger();
734 LoadAttribute( primitiveNode, meshInfo );
740 bool GltfLoader::LoadAttribute( const TreeNode* primitive, MeshInfo& meshInfo )
742 const TreeNode* attrbuteNode = primitive->GetChild( "attributes" );
748 const TreeNode* tempNode;
749 tempNode = attrbuteNode->GetChild( "POSITION" );
752 meshInfo.attribute.POSITION = tempNode->GetInteger();
755 tempNode = attrbuteNode->GetChild( "NORMAL" );
758 meshInfo.attribute.NORMAL = tempNode->GetInteger();
761 tempNode = attrbuteNode->GetChild( "TANGENT" );
764 meshInfo.attribute.TANGENT = tempNode->GetInteger();
768 meshInfo.attribute.TEXCOORD.clear();
769 tempNode = attrbuteNode->GetChild( "TEXCOORD_" + std::to_string( index ) );
772 int value = tempNode->GetInteger();
773 meshInfo.attribute.TEXCOORD.push_back( value );
774 tempNode = attrbuteNode->GetChild( "TEXCOORD_" + std::to_string( ++index ) );
778 meshInfo.attribute.COLOR.clear();
779 tempNode = attrbuteNode->GetChild( "COLOR_" + std::to_string( index ) );
782 int value = tempNode->GetInteger();
783 meshInfo.attribute.COLOR.push_back( value );
784 tempNode = attrbuteNode->GetChild( "COLOR" + std::to_string( ++index ) );
790 bool GltfLoader::SetGeometry( MeshInfo& meshInfo )
792 int indicesIdx = meshInfo.indicesIdx;
794 if( meshInfo.mode != 0 )
796 meshInfo.geometry.SetType( ( Dali::Geometry::Type )meshInfo.mode );
799 if( indicesIdx >= 0 )
801 SetIndexBuffersData( meshInfo, indicesIdx );
804 SetVertexBufferData( meshInfo, meshInfo.attribute.POSITION, "aPosition", Property::VECTOR3 );
805 SetAttributeBufferData<Vector3>( meshInfo, meshInfo.attribute.NORMAL, "aNormal", Property::VECTOR3 );
806 SetAttributeBufferData<Vector4>( meshInfo, meshInfo.attribute.TANGENT, "aTangent", Property::VECTOR4 );
808 for( unsigned int i = 0; i < meshInfo.attribute.TEXCOORD.size(); ++i )
810 int accessorIdx = meshInfo.attribute.TEXCOORD[i];
811 std::ostringstream texCoordString;
812 texCoordString << "aTexCoord" << i;
813 SetAttributeBufferData<Vector2>( meshInfo, accessorIdx, texCoordString.str(), Property::VECTOR2 );
816 for( unsigned int i = 0; i < meshInfo.attribute.COLOR.size(); ++i )
818 int accessorIdx = meshInfo.attribute.COLOR[i];
819 if( accessorIdx < 0 )
824 if( mAccessorArray[accessorIdx].type == "VEC3" )
826 Dali::Vector<Vector3> inputBufferData;
827 LoadDataFromAccessor( accessorIdx, inputBufferData );
829 Dali::Vector<Vector4> bufferData;
830 bufferData.Resize( inputBufferData.Size() );
831 for( unsigned int i = 0; i<inputBufferData.Size(); ++i )
833 bufferData[i].x = inputBufferData[i].x;
834 bufferData[i].y = inputBufferData[i].y;
835 bufferData[i].z = inputBufferData[i].z;
836 bufferData[i].w = 1.0;
838 PropertyBuffer propertyBuffer = CreatePropertyBuffer<Vector4>( bufferData, "aVertexColor", Property::VECTOR4 );
839 meshInfo.geometry.AddVertexBuffer( propertyBuffer );
841 else if( mAccessorArray[accessorIdx].type == "VEC4" )
843 SetAttributeBufferData<Vector4>( meshInfo, accessorIdx, "aVertexColor", Property::VECTOR4 );
849 void GltfLoader::SetMeshInfoAndCanonize( MeshInfo& meshInfo, Dali::Vector<Dali::Vector3> &vertexBufferData )
851 Vector3 pointMin( std::numeric_limits<float>::max(), std::numeric_limits<float>::max(), std::numeric_limits<float>::max() );
852 Vector3 pointMax( std::numeric_limits<float>::min(), std::numeric_limits<float>::min(), std::numeric_limits<float>::min() );
853 unsigned int vertexSize = vertexBufferData.Size();
854 for( unsigned int i = 0; i<vertexSize; ++i )
856 pointMin.x = std::min( vertexBufferData[i].x, pointMin.x );
857 pointMin.y = std::min( vertexBufferData[i].y, pointMin.y );
858 pointMin.z = std::min( vertexBufferData[i].z, pointMin.z );
860 pointMax.x = std::max( vertexBufferData[i].x, pointMax.x );
861 pointMax.y = std::max( vertexBufferData[i].y, pointMax.y );
862 pointMax.z = std::max( vertexBufferData[i].z, pointMax.z );
864 meshInfo.size = pointMax - pointMin;
865 meshInfo.pivot.x = ( -pointMin.x ) / ( pointMax.x - pointMin.x );
866 meshInfo.pivot.y = ( -pointMin.y ) / ( pointMax.y - pointMin.y );
867 meshInfo.pivot.z = ( -pointMin.z ) / ( pointMax.z - pointMin.z );
869 Vector3 center = meshInfo.size * 0.5 + pointMin;
870 for( unsigned int i = 0; i < vertexSize; ++i )
872 vertexBufferData[i] = vertexBufferData[i] - center;
873 vertexBufferData[i].x = vertexBufferData[i].x / meshInfo.size.x;
874 vertexBufferData[i].y = vertexBufferData[i].y / meshInfo.size.y;
875 vertexBufferData[i].z = vertexBufferData[i].z / meshInfo.size.z;
879 bool GltfLoader::CreateScene( Internal::Scene& scene )
881 scene.SetDefaultCamera( Dali::Camera::LOOK_AT_TARGET, 0.01, Vector3::ZERO );
884 if( !LoadSceneNodes( scene ) )
889 if( !LoadAnimation( scene ) )
897 void GltfLoader::LoadCamera( Scene& scene )
899 const TreeNode* camerasNode = mRoot->GetChild( "cameras" );
905 for( TreeNode::ConstIterator cameraIter = ( *camerasNode ).CBegin(); cameraIter != ( *camerasNode ).CEnd(); ++cameraIter )
907 const TreeNode* tempNode = ( &( *cameraIter ).second )->GetChild( "name" );
908 CameraInfo cameraInfo;
911 ReadString( tempNode, cameraInfo.name );
914 tempNode = ( &( *cameraIter ).second )->GetChild( "type" );
917 ReadString( tempNode, cameraInfo.type );
920 CameraActor cameraActor = CameraActor::New();
921 cameraActor.SetParentOrigin( ParentOrigin::CENTER );
922 cameraActor.SetAnchorPoint( AnchorPoint::CENTER );
924 if( cameraInfo.type == "orthographic" )
926 LoadOrthoGraphic( ( *cameraIter ).second, cameraInfo );
927 float xMag_2 = cameraInfo.orthographic.xmag / 2.0;
928 float yMag_2 = cameraInfo.orthographic.ymag / 2.0;
929 cameraActor.SetOrthographicProjection( -xMag_2, xMag_2, yMag_2, -yMag_2,
930 cameraInfo.orthographic.znear, cameraInfo.orthographic.zfar );
932 else if( cameraInfo.type == "perspective" )
934 if( !LoadPerspective( ( *cameraIter ).second, cameraInfo ) )
938 cameraActor.SetProjectionMode( Dali::Camera::PERSPECTIVE_PROJECTION );
939 cameraActor.SetFieldOfView( cameraInfo.perspective.yfov );
940 cameraActor.SetNearClippingPlane( cameraInfo.perspective.znear );
942 if( cameraInfo.perspective.zfar > 0.0 )
944 cameraActor.SetFarClippingPlane( cameraInfo.perspective.zfar );
946 if( cameraInfo.perspective.aspectRatio > 0.0 )
948 cameraActor.SetAspectRatio( cameraInfo.perspective.aspectRatio );
952 scene.AddCamera( cameraActor );
956 bool GltfLoader::LoadOrthoGraphic( const TreeNode& camera, CameraInfo& cameraInfo )
958 const TreeNode* orthographicNode = camera.GetChild( "orthographic" );
959 if( !orthographicNode )
964 const TreeNode* tempNode;
965 tempNode = orthographicNode->GetChild( "xmag" );
968 ReadFloat( tempNode, cameraInfo.orthographic.xmag );
971 tempNode = orthographicNode->GetChild( "ymag" );
974 ReadFloat( tempNode, cameraInfo.orthographic.ymag );
977 tempNode = orthographicNode->GetChild( "zfar" );
980 ReadFloat( tempNode, cameraInfo.orthographic.zfar );
983 tempNode = orthographicNode->GetChild( "znear" );
986 ReadFloat( tempNode, cameraInfo.orthographic.znear );
992 bool GltfLoader::LoadPerspective( const TreeNode& camera, CameraInfo& cameraInfo )
994 const TreeNode* perspectiveNode = camera.GetChild( "perspective" );
995 if( !perspectiveNode )
1000 const TreeNode* tempNode;
1001 tempNode = perspectiveNode->GetChild( "aspectRatio" );
1004 ReadFloat( tempNode, cameraInfo.perspective.aspectRatio );
1007 tempNode = perspectiveNode->GetChild( "yfov" );
1010 ReadFloat( tempNode, cameraInfo.perspective.yfov );
1013 tempNode = perspectiveNode->GetChild( "zfar" );
1016 ReadFloat( tempNode, cameraInfo.perspective.zfar );
1019 tempNode = perspectiveNode->GetChild( "znear" );
1022 ReadFloat( tempNode, cameraInfo.perspective.znear );
1028 bool GltfLoader::LoadSceneNodes( Scene& scene )
1030 const TreeNode* sceneNode = mRoot->GetChild( "scene" );
1034 sceneNum = sceneNode->GetInteger();
1037 const TreeNode* scenesNode = mRoot->GetChild( "scenes" );
1038 if( !( scenesNode && ( mNodes = mRoot->GetChild( "nodes" ) ) ) )
1043 const TreeNode* tempNode = Tidx( scenesNode, sceneNum );
1049 tempNode = tempNode->GetChild( "nodes" );
1055 for( TreeNode::ConstIterator nodeIter = ( *tempNode ).CBegin(); nodeIter != ( *tempNode ).CEnd(); ++nodeIter )
1057 Actor actor = AddNode( scene, ( ( *nodeIter ).second ).GetInteger() );
1058 actor.SetParentOrigin( ParentOrigin::CENTER );
1059 scene.GetRoot().Add( actor );
1065 Actor GltfLoader::AddNode( Scene& scene, int index )
1067 const TreeNode* node = Tidx( mNodes, index );
1068 Actor actor = Actor::New();
1069 Vector3 actorSize( Vector3::ONE );
1071 Vector3 translation = Vector3( 0.0, 0.0, 0.0 );
1072 Vector3 scale = Vector3( 1.0, 1.0, 1.0 );
1073 Quaternion orientation( Vector4( 0.0, 0.0, 0.0, 1.0 ) );
1075 Vector3 anchorPoint = AnchorPoint::CENTER;
1077 const TreeNode* tempNode = NULL;
1078 if( ( tempNode = node->GetChild( "translation" ) ) )
1080 float floatVec[3] = { 0.0, 0.0, 0.0 };
1081 if( tempNode && ReadVector( tempNode, floatVec, 3 ) )
1083 translation = Vector3( floatVec[0], floatVec[1], floatVec[2] );
1087 if( ( tempNode = node->GetChild( "scale" ) ) )
1089 float floatVec[3] = { 1.0, 1.0, 1.0 };
1090 if( tempNode && ReadVector( tempNode, floatVec, 3 ) )
1092 scale = Vector3( floatVec[0], floatVec[1], floatVec[2] );
1096 if( ( tempNode = node->GetChild( "rotation" ) ) )
1098 float floatVec[4] = { 0.0, 0.0, 0.0, 1.0 };
1099 if( tempNode && ReadVector( tempNode, floatVec, 4 ) )
1101 orientation = Quaternion( Vector4( floatVec[0], floatVec[1], floatVec[2], floatVec[3] ) );
1105 if( ( tempNode = node->GetChild( "matrix" ) ) )
1107 float floatVec[16] = { 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0 };
1108 if( tempNode && ReadVector( tempNode, floatVec, 16 ) )
1110 Matrix nodeMatrix = Matrix( floatVec );
1111 nodeMatrix.GetTransformComponents( translation, orientation, scale );
1115 if( ( tempNode = node->GetChild( "mesh" ) ) )
1117 MeshInfo meshInfo = mMeshArray[tempNode->GetInteger()];
1118 GLTF::MaterialInfo materialInfo = mMaterialArray[meshInfo.materialsIdx];
1119 bool isMaterial = ( meshInfo.materialsIdx >= 0 );
1121 TextureSet textureSet;
1122 textureSet = TextureSet::New();
1125 int shaderTypeIndex = 0;
1126 int maxMipmapLevel = 0;
1127 bool isBaseColorTexture = false;
1128 bool isMetallicRoughnessTexture = false;
1129 bool isNormalTexture = false;
1130 bool isOcclusionTexture = false;
1131 bool isEmissiveTexture = false;
1133 std::string VERTEX_SHADER, FRAGMENT_SHADER;
1134 VERTEX_SHADER = GLES_VERSION_300;
1135 VERTEX_SHADER += PHYSICALLY_BASED_VERTEX_SHADER;
1136 FRAGMENT_SHADER = GLES_VERSION_300;
1138 bool useIBL = ( scene.GetLightType() >= Toolkit::Scene::LightType::IMAGE_BASED_LIGHT );
1141 if( SetTextureAndSampler( textureSet, materialInfo.baseColorTexture.index, FRAGMENT_SHADER, DEFINE_BASECOLOR_TEXTURE, addIdx ) )
1143 shaderTypeIndex += static_cast<int>( ShaderType::BASECOLOR_SHADER );
1144 isBaseColorTexture = true;
1146 if( SetTextureAndSampler( textureSet, materialInfo.metallicRoughnessTexture.index, FRAGMENT_SHADER, DEFINE_METALLICROUGHNESS_TEXTURE, addIdx ) )
1148 shaderTypeIndex += static_cast<int>( ShaderType::METALLICROUGHNESS_SHADER );
1149 isMetallicRoughnessTexture = true;
1151 if( SetTextureAndSampler( textureSet, materialInfo.normalTexture.index, FRAGMENT_SHADER, DEFINE_NORMAL_TEXTURE, addIdx ) )
1153 shaderTypeIndex += static_cast<int>( ShaderType::NORMAL_SHADER );
1154 isNormalTexture = true;
1156 if( SetTextureAndSampler( textureSet, materialInfo.occlusionTexture.index, FRAGMENT_SHADER, DEFINE_OCCLUSION_TEXTURE, addIdx ) )
1158 shaderTypeIndex += static_cast<int>( ShaderType::OCCLUSION_SHADER );
1159 isOcclusionTexture = true;
1161 if( SetTextureAndSampler( textureSet, materialInfo.emissiveTexture.index, FRAGMENT_SHADER, DEFINE_EMIT_TEXTURE, addIdx ) )
1163 shaderTypeIndex += static_cast<int>( ShaderType::EMIT_SHADER );
1164 isEmissiveTexture = true;
1169 shaderTypeIndex += static_cast<int>( ShaderType::IBL_SHADER );
1170 FRAGMENT_SHADER += DEFINE_IBL_TEXTURE;
1172 Sampler sampler = Sampler::New();
1173 sampler.SetFilterMode( FilterMode::DEFAULT, FilterMode::DEFAULT );
1174 sampler.SetWrapMode( WrapMode::REPEAT, WrapMode::REPEAT, WrapMode::REPEAT );
1176 textureSet.SetTexture( addIdx, scene.GetBRDFTexture() );
1177 textureSet.SetSampler( addIdx++, sampler );
1178 Sampler samplerIBL = Sampler::New();
1179 samplerIBL.SetFilterMode( FilterMode::LINEAR_MIPMAP_LINEAR, FilterMode::LINEAR );
1180 samplerIBL.SetWrapMode( WrapMode::CLAMP_TO_EDGE, WrapMode::CLAMP_TO_EDGE, WrapMode::CLAMP_TO_EDGE );
1181 textureSet.SetTexture( addIdx, scene.GetDiffuseTexture() );
1182 textureSet.SetSampler( addIdx++, samplerIBL );
1183 Texture specularTexture = scene.GetSpecularTexture();
1184 textureSet.SetTexture( addIdx, specularTexture );
1185 textureSet.SetSampler( addIdx++, samplerIBL );
1187 int textureSize = std::min( specularTexture.GetWidth(), specularTexture.GetHeight() );
1189 while( textureSize >= 1 )
1197 FRAGMENT_SHADER += PHYSICALLY_BASED_FRAGMENT_SHADER;
1198 if( !mShaderCache[shaderTypeIndex] )
1200 mShaderCache[shaderTypeIndex] = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER );
1201 scene.AddShader( mShaderCache[shaderTypeIndex] );
1203 Shader shader = mShaderCache[shaderTypeIndex];
1205 Renderer renderer = Renderer::New( meshInfo.geometry, shader );
1206 renderer.SetProperty( Renderer::Property::DEPTH_WRITE_MODE, DepthWriteMode::ON );
1207 renderer.SetProperty( Renderer::Property::DEPTH_TEST_MODE, DepthTestMode::ON );
1208 renderer.SetTextures( textureSet );
1210 anchorPoint = meshInfo.pivot;
1211 actor.SetAnchorPoint( anchorPoint );
1213 actor.SetSize( Vector3( meshInfo.size.x, meshInfo.size.y, meshInfo.size.z ) );
1214 actor.AddRenderer( renderer );
1216 actor.SetScale( scale );
1217 actor.RotateBy( orientation );
1218 actor.SetPosition( translation );
1220 shader.RegisterProperty( "uLightType", ( scene.GetLightType() & ~Toolkit::Scene::LightType::IMAGE_BASED_LIGHT ) );
1221 shader.RegisterProperty( "uLightVector", scene.GetLightVector() );
1222 shader.RegisterProperty( "uLightColor", scene.GetLightColor() );
1224 actor.RegisterProperty( "uIsColor", meshInfo.attribute.COLOR.size() > 0 );
1227 actor.RegisterProperty( "uBaseColorFactor", materialInfo.baseColorFactor );
1228 actor.RegisterProperty( "uMetallicRoughnessFactors", Vector2( materialInfo.metallicFactor, materialInfo.roughnessFactor ) );
1230 if( materialInfo.alphaMode == "OPAQUE" )
1232 actor.RegisterProperty( "alphaMode", 0 );
1234 else if( materialInfo.alphaMode == "MASK" )
1236 actor.RegisterProperty( "alphaMode", 1 );
1240 actor.RegisterProperty( "alphaMode", 2 );
1242 actor.RegisterProperty( "alphaCutoff", materialInfo.alphaCutoff );
1244 if( isBaseColorTexture )
1246 actor.RegisterProperty( "uBaseColorTexCoordIndex", materialInfo.baseColorTexture.texCoord );
1248 if( isMetallicRoughnessTexture )
1250 actor.RegisterProperty( "uMetallicRoughnessTexCoordIndex", materialInfo.metallicRoughnessTexture.texCoord );
1252 if( isNormalTexture )
1254 actor.RegisterProperty( "uNormalScale", materialInfo.normalTexture.value );
1255 actor.RegisterProperty( "uNormalTexCoordIndex", materialInfo.normalTexture.texCoord );
1257 if( isOcclusionTexture )
1259 actor.RegisterProperty( "uOcclusionTexCoordIndex", materialInfo.occlusionTexture.texCoord );
1260 actor.RegisterProperty( "uOcclusionStrength", materialInfo.occlusionTexture.value );
1262 if( isEmissiveTexture )
1264 actor.RegisterProperty( "uEmissiveTexCoordIndex", materialInfo.emissiveTexture.texCoord );
1265 actor.RegisterProperty( "uEmissiveFactor", materialInfo.emissiveFactor );
1269 if( isMaterial && useIBL )
1271 actor.RegisterProperty( "uScaleIBLAmbient", scene.GetIBLScaleFactor() );
1272 actor.RegisterProperty( "uMipmapLevel", static_cast<float>( maxMipmapLevel ) );
1277 actor.SetAnchorPoint( AnchorPoint::CENTER );
1278 actor.SetPosition( translation );
1279 actor.RotateBy( orientation );
1280 actor.SetSize( actorSize );
1283 tempNode = node->GetChild( "camera" );
1286 int cameraNum = tempNode->GetInteger();
1287 actor.Add( scene.GetCamera( cameraNum ) );
1290 tempNode = node->GetChild( "name" );
1293 std::string nameString;
1294 ReadString( tempNode, nameString );
1295 actor.SetName( nameString );
1298 SetActorCache( actor, index );
1299 if( ( tempNode = node->GetChild( "children" ) ) )
1301 for( TreeNode::ConstIterator childIter = ( *tempNode ).CBegin(); childIter != ( *tempNode ).CEnd(); ++childIter )
1303 Actor childActor = AddNode( scene, ( ( *childIter ).second ).GetInteger() );
1304 childActor.SetParentOrigin( anchorPoint );
1305 actor.Add( childActor );
1312 void GltfLoader::SetActorCache( Actor& actor, const int index )
1314 if( mActorCache.size() < static_cast<unsigned int>( index + 1 ) )
1316 mActorCache.resize( index + 1 );
1318 mActorCache[index] = actor;
1321 bool GltfLoader::SetTextureAndSampler( TextureSet& textureSet, int textureIdx, std::string& toShader, std::string shader, int& addIdx )
1323 if( textureIdx >= 0 )
1326 TextureInfo textureInfo = mTextureArray[textureIdx];
1327 if( textureInfo.sourceIdx >= 0 )
1329 textureSet.SetTexture( addIdx, mSourceArray[textureInfo.sourceIdx] );
1331 if( textureInfo.samplerIdx >= 0 )
1333 textureSet.SetSampler( addIdx, mSamplerArray[textureInfo.samplerIdx] );
1337 Sampler sampler = Sampler::New();
1338 sampler.SetFilterMode( FilterMode::DEFAULT, FilterMode::DEFAULT );
1339 sampler.SetWrapMode( WrapMode::REPEAT, WrapMode::REPEAT, WrapMode::REPEAT );
1340 textureSet.SetSampler( addIdx, sampler );
1348 bool GltfLoader::LoadAnimation( Scene& scene )
1350 const TreeNode* animationsNode = mRoot->GetChild( "animations" );
1351 if( !animationsNode )
1356 for( TreeNode::ConstIterator animationIter = ( *animationsNode ).CBegin(); animationIter != ( *animationsNode ).CEnd(); ++animationIter )
1358 const TreeNode* nameNode = ( &( *animationIter ).second )->GetChild( "name" );
1359 AnimationInfo animationInfo;
1362 ReadString( nameNode, animationInfo.name );
1365 Property::Index propIndex = Property::INVALID_INDEX;
1366 LoadAnimationChannels( ( *animationIter ).second, animationInfo );
1367 if( animationInfo.channelArray.size() == 0 )
1372 LoadAnimationSamplers( ( *animationIter ).second, animationInfo );
1374 int channelNum = animationInfo.channelArray.size();
1375 for( int i = 0; i < channelNum; i++ )
1377 AnimationChannelInfo currentChannel = animationInfo.channelArray[i];
1379 if( currentChannel.path == "rotation" )
1381 propIndex = Dali::Actor::Property::ORIENTATION;
1383 else if( currentChannel.path == "translation" )
1385 propIndex = Dali::Actor::Property::POSITION;
1387 else if( currentChannel.path == "scale" )
1389 propIndex = Dali::Actor::Property::SCALE;
1392 float duration = 0.0f;
1393 KeyFrames keyframes = KeyFrames::New();
1394 if( propIndex == Dali::Actor::Property::ORIENTATION )
1396 duration = LoadKeyFrames<Vector4>( animationInfo.samplerArray[currentChannel.sampler], propIndex, keyframes );
1400 duration = LoadKeyFrames<Vector3>( animationInfo.samplerArray[currentChannel.sampler], propIndex, keyframes );
1403 Animation animation = Animation::New( duration );
1404 Animation::Interpolation interpolation = Animation::Interpolation::Linear;
1405 if( animationInfo.samplerArray[currentChannel.sampler].interpolation == "CUBICSPLINE" )
1407 interpolation = Animation::Interpolation::Cubic;
1409 if( animationInfo.samplerArray[currentChannel.sampler].interpolation == "STEP" )
1413 animation.AnimateBetween( Property( mActorCache[currentChannel.targetNode], propIndex ), keyframes, interpolation );
1415 animation.SetLooping( false );
1416 scene.AddAnimation( animation );
1423 bool GltfLoader::LoadAnimationChannels( const TreeNode& animation, AnimationInfo& animationInfo )
1425 const TreeNode* channelsNode = animation.GetChild( "channels" );
1431 for( TreeNode::ConstIterator channelIter = ( *channelsNode ).CBegin(); channelIter != ( *channelsNode ).CEnd(); ++channelIter )
1433 AnimationChannelInfo animationChannelInfo;
1434 const TreeNode* channelNode = ( &( *channelIter ).second );
1435 const TreeNode* samplerNode = channelNode->GetChild( "sampler" );
1438 animationChannelInfo.sampler = samplerNode->GetInteger();
1441 const TreeNode* targetNode = channelNode->GetChild( "target" );
1444 const TreeNode* tempNode = targetNode->GetChild( "node" );
1447 animationChannelInfo.targetNode = tempNode->GetInteger();
1454 tempNode = targetNode->GetChild( "path" );
1457 ReadString( tempNode, animationChannelInfo.path );
1461 animationInfo.channelArray.push_back( animationChannelInfo );
1466 bool GltfLoader::LoadAnimationSamplers( const TreeNode& animation, AnimationInfo& animationInfo )
1468 const TreeNode* samplersNode = animation.GetChild( "samplers" );
1474 for( TreeNode::ConstIterator sampler = ( *samplersNode ).CBegin(); sampler != ( *samplersNode ).CEnd(); ++sampler )
1476 AnimationSamplerInfo animationSamplerInfo;
1477 const TreeNode* samplerNode = ( &( *sampler ).second );
1478 const TreeNode* tempNode = samplerNode->GetChild( "input" );
1481 animationSamplerInfo.input = tempNode->GetInteger();
1484 tempNode = samplerNode->GetChild( "output" );
1487 animationSamplerInfo.output = tempNode->GetInteger();
1490 tempNode = samplerNode->GetChild( "interpolation" );
1493 ReadString( tempNode, animationSamplerInfo.interpolation );
1496 animationInfo.samplerArray.push_back( animationSamplerInfo );
1502 // Template functions
1503 template <typename T>
1504 bool GltfLoader::ReadBinFile( Vector<T> &dataBuffer, std::string url, int offset, int count )
1506 dataBuffer.Resize( count );
1507 FILE* fp = fopen( url.c_str(), "rb" );
1512 fseek( fp, offset, SEEK_SET );
1513 ssize_t result = fread( &dataBuffer[0], sizeof( T ), count, fp );
1516 return ( result >= 0 );
1519 template <typename T>
1520 float GltfLoader::IntToFloat( T element, bool normalize )
1524 return static_cast<float>( element );
1527 if( std::is_same<T, char>::value )
1529 return std::max( static_cast<float>( element ) / 127.0, -1.0 );
1531 if( std::is_same<T, unsigned char>::value )
1533 return static_cast<float>( element ) / 255.0;
1535 if( std::is_same<T, short>::value )
1537 return std::max( static_cast<float>( element ) / 32767.0, -1.0 );
1539 if( std::is_same<T, unsigned short>::value )
1541 return static_cast<float>( element ) / 65535.0;
1546 template <typename Td, typename Ts>
1547 void GltfLoader::FitBuffer( Dali::Vector<Td>& bufferDestiny, Dali::Vector<Ts>& bufferSource, int bufferSize, int elementNumOfByteStride, bool normalize )
1549 bufferDestiny.Resize( bufferSize );
1550 int count = bufferSource.Size() / elementNumOfByteStride;
1551 for( int i = 0; i<count; ++i )
1553 bufferDestiny[i] = static_cast<Td>( bufferSource[i * elementNumOfByteStride] );
1557 template <typename T>
1558 void GltfLoader::FitBuffer( Dali::Vector<Vector2>& bufferDestiny, Dali::Vector<T>& bufferSource, int bufferSize, int elementNumOfByteStride, bool normalize )
1560 bufferDestiny.Resize( bufferSize );
1561 int count = bufferSource.Size() / elementNumOfByteStride;
1562 for( int i = 0; i<count; ++i )
1564 bufferDestiny[i].x = IntToFloat( bufferSource[i * elementNumOfByteStride], normalize );
1565 bufferDestiny[i].y = IntToFloat( bufferSource[i * elementNumOfByteStride + 1], normalize );
1569 template <typename T>
1570 void GltfLoader::FitBuffer( Dali::Vector<Vector3>& bufferDestiny, Dali::Vector<T>& bufferSource, int bufferSize, int elementNumOfByteStride, bool normalize )
1572 bufferDestiny.Resize( bufferSize );
1573 int count = bufferSource.Size() / elementNumOfByteStride;
1574 for( int i = 0; i<count; ++i )
1576 bufferDestiny[i].x = IntToFloat( bufferSource[i * elementNumOfByteStride], normalize );
1577 bufferDestiny[i].y = IntToFloat( bufferSource[i * elementNumOfByteStride + 1], normalize );
1578 bufferDestiny[i].z = IntToFloat( bufferSource[i * elementNumOfByteStride + 2], normalize );
1582 template <typename T>
1583 void GltfLoader::FitBuffer( Dali::Vector<Vector4>& bufferDestiny, Dali::Vector<T>& bufferSource, int bufferSize, int elementNumOfByteStride, bool normalize )
1585 bufferDestiny.Resize( bufferSize );
1586 int count = bufferSource.Size() / elementNumOfByteStride;
1587 for( int i = 0; i<count; ++i )
1589 bufferDestiny[i].x = IntToFloat( bufferSource[i * elementNumOfByteStride], normalize );
1590 bufferDestiny[i].y = IntToFloat( bufferSource[i * elementNumOfByteStride + 1], normalize );
1591 bufferDestiny[i].z = IntToFloat( bufferSource[i * elementNumOfByteStride + 2], normalize );
1592 bufferDestiny[i].w = IntToFloat( bufferSource[i * elementNumOfByteStride + 3], normalize );
1596 template <typename T>
1597 void GltfLoader::LoadDataFromAccessor( int accessorIdx, Dali::Vector<T>& bufferData )
1599 AccessorInfo accessor = mAccessorArray[accessorIdx];
1600 BufferViewInfo bufferView = mBufferViewArray[accessor.bufferView];
1601 BufferInfo buffer = mBufferArray[bufferView.buffer];
1602 std::string load_uri = buffer.uri;
1604 // In the glTF 2.0 Specification, 5121 is UNSIGNED BYTE, 5123 is UNSIGNED SHORT
1605 int elementByteSize = ( accessor.componentType <= 5121 ) ? 1 :
1606 ( ( accessor.componentType <= 5123 ) ? 2 : 4 );
1608 if( accessor.type == "VEC2" )
1612 else if( accessor.type == "VEC3" )
1616 else if( accessor.type == "VEC4" || accessor.type == "MAT2" )
1620 else if( accessor.type == "MAT3" )
1624 else if( accessor.type == "MAT4" )
1632 int elementNumOfByteStride = elementNum;
1633 if( bufferView.byteStride > 0 )
1635 elementNumOfByteStride = bufferView.byteStride / elementByteSize;
1639 * glTF 2.0 Specification
1642 * 5121 : UNSIGNED_BYTE
1644 * 5123 : UNSIGNED_SHORT
1645 * 5125 : UNSIGNED_INT
1648 if( accessor.componentType == 5120 )
1650 Dali::Vector<char> inputBufferData;
1651 ReadBinFile<char>( inputBufferData, mPath + load_uri, bufferView.byteOffset + accessor.byteOffset, elementNumOfByteStride * accessor.count );
1652 FitBuffer( bufferData, inputBufferData, accessor.count, elementNumOfByteStride, accessor.normalized );
1654 else if( accessor.componentType == 5121 )
1656 Dali::Vector<unsigned char> inputBufferData;
1657 ReadBinFile<unsigned char>( inputBufferData, mPath + load_uri, bufferView.byteOffset + accessor.byteOffset, elementNumOfByteStride * accessor.count );
1658 FitBuffer( bufferData, inputBufferData, accessor.count, elementNumOfByteStride, accessor.normalized );
1660 else if( accessor.componentType == 5122 )
1662 Dali::Vector<short> inputBufferData;
1663 ReadBinFile<short>( inputBufferData, mPath + load_uri, bufferView.byteOffset + accessor.byteOffset, elementNumOfByteStride * accessor.count );
1664 FitBuffer( bufferData, inputBufferData, accessor.count, elementNumOfByteStride, accessor.normalized );
1666 else if( accessor.componentType == 5123 )
1668 Dali::Vector<unsigned short> inputBufferData;
1669 ReadBinFile<unsigned short>( inputBufferData, mPath + load_uri, bufferView.byteOffset + accessor.byteOffset, elementNumOfByteStride * accessor.count );
1670 FitBuffer( bufferData, inputBufferData, accessor.count, elementNumOfByteStride, accessor.normalized );
1672 else if( accessor.componentType == 5125 )
1674 Dali::Vector<unsigned int> inputBufferData;
1675 ReadBinFile<unsigned int>( inputBufferData, mPath + load_uri, bufferView.byteOffset + accessor.byteOffset, elementNumOfByteStride * accessor.count );
1676 FitBuffer( bufferData, inputBufferData, accessor.count, elementNumOfByteStride, accessor.normalized );
1678 else if( accessor.componentType == 5126 )
1680 Dali::Vector<float> inputBufferData;
1681 ReadBinFile<float>( inputBufferData, mPath + load_uri, bufferView.byteOffset + accessor.byteOffset, elementNumOfByteStride * accessor.count );
1682 FitBuffer( bufferData, inputBufferData, accessor.count, elementNumOfByteStride, accessor.normalized );
1686 template <typename T>
1687 PropertyBuffer GltfLoader::CreatePropertyBuffer( Vector<T> bufferData, std::string map, int type )
1689 Property::Map positionMap;
1690 positionMap[map] = type;
1692 PropertyBuffer propertyBuffer = PropertyBuffer::New( positionMap );
1693 propertyBuffer.SetData( bufferData.Begin(), bufferData.Count() );
1694 return propertyBuffer;
1698 void GltfLoader::SetVertexBufferData( MeshInfo& meshInfo, int accessorIdx, std::string map, int type )
1700 if( accessorIdx >= 0 )
1702 Dali::Vector<Vector3> bufferData;
1703 LoadDataFromAccessor( accessorIdx, bufferData );
1704 SetMeshInfoAndCanonize( meshInfo, bufferData );
1706 PropertyBuffer propertyBuffer = CreatePropertyBuffer<Vector3>( bufferData, map, type );
1707 meshInfo.geometry.AddVertexBuffer( propertyBuffer );
1711 template <typename T>
1712 void GltfLoader::SetAttributeBufferData( MeshInfo& meshInfo, int accessorIdx, std::string map, int type )
1714 if( accessorIdx >= 0 )
1716 Dali::Vector<T> bufferData;
1717 LoadDataFromAccessor( accessorIdx, bufferData );
1719 PropertyBuffer propertyBuffer = CreatePropertyBuffer<T>( bufferData, map, type );
1720 meshInfo.geometry.AddVertexBuffer( propertyBuffer );
1724 void GltfLoader::SetIndexBuffersData( MeshInfo& meshInfo, int indexIdx )
1726 Dali::Vector<unsigned short> indexBufferData;
1727 LoadDataFromAccessor( indexIdx, indexBufferData );
1728 meshInfo.geometry.SetIndexBuffer( &indexBufferData[0], indexBufferData.Size() );
1731 template<typename T>
1732 float GltfLoader::LoadKeyFrames( const AnimationSamplerInfo& currentSampler, const Property::Index propIndex, KeyFrames& keyframes )
1734 Dali::Vector<float> inputBufferData;
1735 Dali::Vector<T> outputBufferData;
1737 LoadDataFromAccessor<float>( currentSampler.input, inputBufferData );
1738 LoadDataFromAccessor<T>( currentSampler.output, outputBufferData );
1740 int keyframeNum = inputBufferData.Size();
1741 float lengthAnimation = inputBufferData[keyframeNum - 1];
1742 for( int i = 0; i < keyframeNum; i++ )
1744 if( propIndex == Dali::Actor::Property::ORIENTATION )
1746 Vector4 vectorOrientation( outputBufferData[i] );
1747 float vW = vectorOrientation.w;
1748 vW = ( vW < 0.0f ) ? std::max( vW, -1.0f ) : std::min( vW, 1.0f );
1749 vectorOrientation.w = vW;
1750 keyframes.Add( inputBufferData[i] / lengthAnimation, Quaternion( Vector4( vectorOrientation ) ) );
1752 else if( propIndex == Dali::Actor::Property::POSITION )
1754 keyframes.Add( inputBufferData[i] / lengthAnimation, Vector3( outputBufferData[i] ) );
1756 else if( propIndex == Dali::Actor::Property::SCALE )
1758 keyframes.Add( inputBufferData[i] / lengthAnimation, Vector3( outputBufferData[i] ) );
1761 return lengthAnimation;
1764 // Utility functions
1765 const TreeNode* GltfLoader::Tidx( const TreeNode *node, int index )
1768 for( TreeNode::ConstIterator it = ( *node ).CBegin(); it != ( *node ).CEnd(); ++it, ++i )
1772 return &( ( *it ).second );
1778 bool GltfLoader::ReadBool( const TreeNode* node, bool& num )
1784 bool returnValue = false;
1786 if( node->GetType() == TreeNode::BOOLEAN )
1788 num = node->GetBoolean();
1795 bool GltfLoader::ReadInt( const TreeNode* node, int& num )
1801 bool returnValue = false;
1802 if( node->GetType() == TreeNode::INTEGER )
1804 num = node->GetInteger();
1807 else if( node->GetType() == TreeNode::FLOAT )
1809 num = node->GetFloat();
1816 bool GltfLoader::ReadFloat( const TreeNode* node, float& num )
1822 bool returnValue = false;
1824 if( node->GetType() == TreeNode::FLOAT )
1826 num = node->GetFloat();
1829 else if( node->GetType() == TreeNode::INTEGER )
1832 ReadInt( node, tempNum );
1833 num = static_cast<float>( tempNum );
1840 bool GltfLoader::ReadVector( const TreeNode* node, float* num, unsigned int size )
1846 bool returnValue = false;
1848 if( ( node->Size() >= size ) && ( node->GetType() == TreeNode::ARRAY ) )
1850 unsigned int offset = 0u;
1851 for( TreeNode::ConstIterator it = node->CBegin(); offset < size; ++it, ++offset )
1853 const TreeNode& coord = ( *it ).second;
1854 if( !ReadFloat( &coord, *( num + offset ) ) )
1865 bool GltfLoader::ReadString( const TreeNode* node, std::string& strValue )
1871 bool returnValue = false;
1872 if( node->GetType() == TreeNode::STRING )
1874 strValue = node->GetString();
1880 }//namespace Internal
1882 }//namespace Toolkit