2 * Copyright (c) 2016 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 <dali-toolkit/internal/controls/scrollable/bouncing-effect-actor.h>
22 #include <dali/public-api/math/vector3.h>
23 #include <dali/public-api/object/property-map.h>
24 #include <dali/public-api/rendering/geometry.h>
25 #include <dali/public-api/rendering/property-buffer.h>
26 #include <dali/public-api/rendering/renderer.h>
27 #include <dali/public-api/rendering/shader.h>
28 #include <dali/public-api/rendering/texture-set.h>
41 // Bouncing effect is presented by stacked three layers with same color and opacity
42 const float LAYER_HEIGHTS[5] =
51 #define MAKE_SHADER(A)#A
53 // Modify the vertex position according to the bounce coefficient;
54 const char* MESH_VERTEX_SHADER = MAKE_SHADER(
55 attribute mediump vec3 aPosition1;\n
56 attribute mediump vec3 aPosition2;\n
57 uniform mediump mat4 uMvpMatrix;\n
58 uniform mediump vec3 uSize;
59 uniform mediump float uBounceCoefficient;\n
63 gl_Position = uMvpMatrix * vec4(mix( aPosition1, aPosition2, abs(uBounceCoefficient) )*uSize, 1.0);\n
67 // use the actor color to paint every layer
68 const char* MESH_FRAGMENT_SHADER = MAKE_SHADER(
69 uniform lowp vec4 uColor;\n
72 gl_FragColor = uColor;\n
78 Actor CreateBouncingEffectActor( Property::Index& bouncePropertyIndex )
80 // Create the bouncing mesh geometry
86 // 4 vertices 2 triangles per layer. The depth interval between each layer is 0.01
87 VertexPosition vertexData[20] = {
89 { Vector3( -0.5f, -0.5f, 0.f ), Vector3( -0.5f, -0.5f, 0.f ) },
90 { Vector3( 0.5f, -0.5f, 0.f ), Vector3( 0.5f, -0.5f, 0.f ) },
91 { Vector3( -0.5f, -0.5f, 0.f ), Vector3( -0.5f, -0.5f + LAYER_HEIGHTS[0], 0.f ) },
92 { Vector3( 0.5f, -0.5f, 0.f ), Vector3( 0.5f, -0.5f+ LAYER_HEIGHTS[0], 0.f ) },
94 { Vector3( -0.5f, -0.5f, 0.01f ), Vector3( -0.5f, -0.5f, 0.01f ) },
95 { Vector3( 0.5f, -0.5f, 0.01f ), Vector3( 0.5f, -0.5f, 0.01f ) },
96 { Vector3( -0.5f, -0.5f, 0.01f ), Vector3( -0.5f, -0.5f + LAYER_HEIGHTS[1], 0.01f ) },
97 { Vector3( 0.5f, -0.5f, 0.01f ), Vector3( 0.5f, -0.5f+ LAYER_HEIGHTS[1], 0.01f ) },
99 { Vector3( -0.5f, -0.5f, 0.02f ), Vector3( -0.5f, -0.5f, 0.02f ) },
100 { Vector3( 0.5f, -0.5f, 0.02f ), Vector3( 0.5f, -0.5f, 0.02f ) },
101 { Vector3( -0.5f, -0.5f, 0.02f ), Vector3( -0.5f, -0.5f + LAYER_HEIGHTS[2], 0.02f ) },
102 { Vector3( 0.5f, -0.5f, 0.02f ), Vector3( 0.5f, -0.5f+ LAYER_HEIGHTS[2], 0.02f ) },
104 { Vector3( -0.5f, -0.5f, 0.03f ), Vector3( -0.5f, -0.5f, 0.03f ) },
105 { Vector3( 0.5f, -0.5f, 0.03f ), Vector3( 0.5f, -0.5f, 0.03f ) },
106 { Vector3( -0.5f, -0.5f, 0.03f ), Vector3( -0.5f, -0.5f + LAYER_HEIGHTS[3], 0.03f ) },
107 { Vector3( 0.5f, -0.5f, 0.03f ), Vector3( 0.5f, -0.5f+ LAYER_HEIGHTS[3], 0.03f ) },
109 { Vector3( -0.5f, -0.5f, 0.04f ), Vector3( -0.5f, -0.5f, 0.04f ) },
110 { Vector3( 0.5f, -0.5f, 0.04f ), Vector3( 0.5f, -0.5f, 0.04f ) },
111 { Vector3( -0.5f, -0.5f, 0.04f ), Vector3( -0.5f, -0.5f + LAYER_HEIGHTS[4], 0.04f ) },
112 { Vector3( 0.5f, -0.5f, 0.04f ), Vector3( 0.5f, -0.5f+ LAYER_HEIGHTS[4], 0.04f ) }
114 Property::Map vertexFormat;
115 vertexFormat["aPosition1"] = Property::VECTOR3;
116 vertexFormat["aPosition2"] = Property::VECTOR3;
117 PropertyBuffer vertices = PropertyBuffer::New( vertexFormat );
118 vertices.SetData( vertexData, 20u );
120 unsigned short indexData[30] = { 0,3,1,0,2,3,4,7,5,4,6,7,8,11,9,8,10,11,12,15,13,12,14,15,16,19,17,16,18,19};
122 Geometry meshGeometry = Geometry::New();
123 meshGeometry.AddVertexBuffer( vertices );
124 meshGeometry.SetIndexBuffer( indexData, sizeof(indexData)/sizeof(indexData[0]) );
127 Shader shader = Shader::New( MESH_VERTEX_SHADER, MESH_FRAGMENT_SHADER );
130 Renderer renderer = Renderer::New( meshGeometry, shader );
133 Actor meshActor= Actor::New();
134 meshActor.AddRenderer( renderer );
137 bouncePropertyIndex = meshActor.RegisterProperty("uBounceCoefficient", 0.f);
142 } // namespace Internal
144 } // namespace Toolkit