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/dali.h>
20 #include <dali/public-api/rendering/renderer.h>
21 #include <dali-toolkit/dali-toolkit.h>
28 #include "shared/view.h"
29 #include "shared/utility.h"
35 const char * const APPLICATION_TITLE( "Refraction Effect" );
36 const char * const TOOLBAR_IMAGE( DEMO_IMAGE_DIR "top-bar.png" );
37 const char * const CHANGE_TEXTURE_ICON( DEMO_IMAGE_DIR "icon-change.png" );
38 const char * const CHANGE_TEXTURE_ICON_SELECTED( DEMO_IMAGE_DIR "icon-change-selected.png" );
39 const char * const CHANGE_MESH_ICON( DEMO_IMAGE_DIR "icon-replace.png" );
40 const char * const CHANGE_MESH_ICON_SELECTED( DEMO_IMAGE_DIR "icon-replace-selected.png" );
42 const char* MESH_FILES[] =
44 DEMO_MODEL_DIR "surface_pattern_v01.obj",
45 DEMO_MODEL_DIR "surface_pattern_v02.obj"
47 const unsigned int NUM_MESH_FILES( sizeof( MESH_FILES ) / sizeof( MESH_FILES[0] ) );
49 const char* TEXTURE_IMAGES[]=
51 DEMO_IMAGE_DIR "background-1.jpg",
52 DEMO_IMAGE_DIR "background-2.jpg",
53 DEMO_IMAGE_DIR "background-3.jpg",
54 DEMO_IMAGE_DIR "background-4.jpg"
56 const unsigned int NUM_TEXTURE_IMAGES( sizeof( TEXTURE_IMAGES ) / sizeof( TEXTURE_IMAGES[0] ) );
58 struct LightOffsetConstraint
60 LightOffsetConstraint( float radius )
65 void operator()( Vector2& current, const PropertyInputContainer& inputs )
67 float spinAngle = inputs[0]->GetFloat();
68 current.x = cos( spinAngle );
69 current.y = sin( spinAngle );
78 * structure of the vertex in the mesh
89 Vertex( const Vector3& position, const Vector3& normal, const Vector2& textureCoord )
90 : position( position ), normal( normal ), textureCoord( textureCoord )
94 /************************************************************************************************
95 *** The shader source is used when the MeshActor is not touched***
96 ************************************************************************************************/
97 const char* VERTEX_SHADER_FLAT = DALI_COMPOSE_SHADER(
98 attribute mediump vec3 aPosition;\n
99 attribute mediump vec3 aNormal;\n
100 attribute highp vec2 aTexCoord;\n
101 uniform mediump mat4 uMvpMatrix;\n
102 varying mediump vec2 vTexCoord;\n
105 gl_Position = uMvpMatrix * vec4( aPosition.xy, 0.0, 1.0 );\n
106 vTexCoord = aTexCoord.xy;\n
110 const char* FRAGMENT_SHADER_FLAT = DALI_COMPOSE_SHADER(
111 uniform lowp vec4 uColor;\n
112 uniform sampler2D sTexture;\n
113 varying mediump vec2 vTexCoord;\n
116 gl_FragColor = texture2D( sTexture, vTexCoord ) * uColor;\n
120 /************************************************************
121 ** Custom refraction effect shader***************************
122 ************************************************************/
123 const char* VERTEX_SHADER_REFRACTION = DALI_COMPOSE_SHADER(
124 attribute mediump vec3 aPosition;\n
125 attribute mediump vec3 aNormal;\n
126 attribute highp vec2 aTexCoord;\n
127 uniform mediump mat4 uMvpMatrix;\n
128 varying mediump vec4 vVertex;\n
129 varying mediump vec3 vNormal;\n
130 varying mediump vec2 vTexCoord;\n
131 varying mediump vec2 vTextureOffset;\n
134 gl_Position = uMvpMatrix * vec4( aPosition.xy, 0.0, 1.0 );\n
135 vTexCoord = aTexCoord.xy;\n
138 vVertex = vec4( aPosition, 1.0 );\n
139 float length = max(0.01, length(aNormal.xy)) * 40.0;\n
140 vTextureOffset = aNormal.xy / length;\n
144 const char* FRAGMENT_SHADER_REFRACTION = DALI_COMPOSE_SHADER(
145 precision mediump float;\n
146 uniform mediump float uEffectStrength;\n
147 uniform mediump vec3 uLightPosition;\n
148 uniform mediump vec2 uLightXYOffset;\n
149 uniform mediump vec2 uLightSpinOffset;\n
150 uniform mediump float uLightIntensity;\n
151 uniform lowp vec4 uColor;\n
152 uniform sampler2D sTexture;\n
153 varying mediump vec4 vVertex;\n
154 varying mediump vec3 vNormal;\n
155 varying mediump vec2 vTexCoord;\n
156 varying mediump vec2 vTextureOffset;\n
158 vec3 rgb2hsl(vec3 rgb)\n
160 float epsilon = 1.0e-10;\n
161 vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0);\n
162 vec4 P = mix(vec4(rgb.bg, K.wz), vec4(rgb.gb, K.xy), step(rgb.b, rgb.g));\n
163 vec4 Q = mix(vec4(P.xyw, rgb.r), vec4(rgb.r, P.yzx), step(P.x, rgb.r));\n
167 float chroma = Q.x - min(Q.w, Q.y);\n
168 float hue = abs(Q.z + (Q.w-Q.y) / (6.0*chroma+epsilon));\n
170 float lightness = value - chroma*0.5;\n
171 return vec3( hue, chroma/max( 1.0-abs(lightness*2.0-1.0), 1.0e-1 ), lightness );\n
174 vec3 hsl2rgb( vec3 hsl )\n
177 vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0);\n
178 vec3 p = abs(fract(hsl.xxx + K.xyz) * 6.0 - K.www);\n
179 vec3 RGB = clamp(p - K.xxx, 0.0, 1.0);\n
181 float chroma = ( 1.0 - abs( hsl.z*2.0-1.0 ) ) * hsl.y;\n
182 return ( RGB - 0.5 ) * chroma + hsl.z;\n
187 vec3 normal = normalize( vNormal);\n
189 vec3 lightPosition = uLightPosition + vec3(uLightXYOffset+uLightSpinOffset, 0.0);\n
190 mediump vec3 vecToLight = normalize( (lightPosition - vVertex.xyz) * 0.01 );\n
191 mediump float spotEffect = pow( max(0.05, vecToLight.z ) - 0.05, 8.0);\n
193 spotEffect = spotEffect * uEffectStrength;\n
194 mediump float lightDiffuse = ( ( dot( vecToLight, normal )-0.75 ) *uLightIntensity ) * spotEffect;\n
196 lowp vec4 color = texture2D( sTexture, vTexCoord + vTextureOffset * spotEffect );\n
197 vec3 lightedColor = hsl2rgb( rgb2hsl(color.rgb) + vec3(0.0,0.0,lightDiffuse) );\n
199 gl_FragColor = vec4( lightedColor, color.a ) * uColor;\n
206 /*************************************************/
207 /*Demo using RefractionEffect*****************/
208 /*************************************************/
209 class RefractionEffectExample : public ConnectionTracker
212 RefractionEffectExample( Application &application )
213 : mApplication( application ),
214 mCurrentTextureId( 1 ),
217 // Connect to the Application's Init signal
218 application.InitSignal().Connect(this, &RefractionEffectExample::Create);
221 ~RefractionEffectExample()
227 // The Init signal is received once (only) during the Application lifetime
228 void Create(Application& application)
230 Stage stage = Stage::GetCurrent();
231 Vector2 stageSize = stage.GetSize();
233 stage.KeyEventSignal().Connect(this, &RefractionEffectExample::OnKeyEvent);
235 // Creates a default view with a default tool bar.
236 // The view is added to the stage.
237 Toolkit::ToolBar toolBar;
238 Toolkit::Control view;
239 mContent = DemoHelper::CreateView( application,
246 // Add a button to change background. (right of toolbar)
247 mChangeTextureButton = Toolkit::PushButton::New();
248 mChangeTextureButton.SetUnselectedImage( CHANGE_TEXTURE_ICON );
249 mChangeTextureButton.SetSelectedImage( CHANGE_TEXTURE_ICON_SELECTED );
250 mChangeTextureButton.ClickedSignal().Connect( this, &RefractionEffectExample::OnChangeTexture );
251 toolBar.AddControl( mChangeTextureButton,
252 DemoHelper::DEFAULT_VIEW_STYLE.mToolBarButtonPercentage,
253 Toolkit::Alignment::HorizontalRight,
254 DemoHelper::DEFAULT_MODE_SWITCH_PADDING );
255 // Add a button to change mesh pattern. ( left of bar )
256 mChangeMeshButton = Toolkit::PushButton::New();
257 mChangeMeshButton.SetUnselectedImage( CHANGE_MESH_ICON );
258 mChangeMeshButton.SetSelectedImage( CHANGE_MESH_ICON_SELECTED );
259 mChangeMeshButton.ClickedSignal().Connect( this, &RefractionEffectExample::OnChangeMesh );
260 toolBar.AddControl( mChangeMeshButton,
261 DemoHelper::DEFAULT_VIEW_STYLE.mToolBarButtonPercentage,
262 Toolkit::Alignment::HorizontalLeft,
263 DemoHelper::DEFAULT_MODE_SWITCH_PADDING );
267 // shader used when the screen is not touched, render a flat surface
268 mShaderFlat = Shader::New( VERTEX_SHADER_FLAT, FRAGMENT_SHADER_FLAT );
269 mGeometry = CreateGeometry( MESH_FILES[mCurrentMeshId] );
271 Texture texture = DemoHelper::LoadStageFillingTexture( TEXTURE_IMAGES[mCurrentTextureId] );
272 mTextureSet = TextureSet::New();
273 mTextureSet.SetTexture( 0u, texture );
275 mRenderer = Renderer::New( mGeometry, mShaderFlat );
276 mRenderer.SetTextures( mTextureSet );
278 mMeshActor = Actor::New();
279 mMeshActor.AddRenderer( mRenderer );
280 mMeshActor.SetSize( stageSize );
281 mMeshActor.SetParentOrigin(ParentOrigin::CENTER);
282 mContent.Add( mMeshActor );
284 // Connect the callback to the touch signal on the mesh actor
285 mContent.TouchSignal().Connect( this, &RefractionEffectExample::OnTouch );
287 // shader used when the finger is touching the screen. render refraction effect
288 mShaderRefraction = Shader::New( VERTEX_SHADER_REFRACTION, FRAGMENT_SHADER_REFRACTION );
291 mLightXYOffsetIndex = mMeshActor.RegisterProperty( "uLightXYOffset", Vector2::ZERO );
293 mLightIntensityIndex = mMeshActor.RegisterProperty( "uLightIntensity", 2.5f );
295 mEffectStrengthIndex = mMeshActor.RegisterProperty( "uEffectStrength", 0.f );
297 Vector3 lightPosition( -stageSize.x*0.5f, -stageSize.y*0.5f, stageSize.x*0.5f ); // top_left
298 mMeshActor.RegisterProperty( "uLightPosition", lightPosition );
300 Property::Index lightSpinOffsetIndex = mMeshActor.RegisterProperty( "uLightSpinOffset", Vector2::ZERO );
302 mSpinAngleIndex = mMeshActor.RegisterProperty("uSpinAngle", 0.f );
303 Constraint constraint = Constraint::New<Vector2>( mMeshActor, lightSpinOffsetIndex, LightOffsetConstraint(stageSize.x*0.1f) );
304 constraint.AddSource( LocalSource(mSpinAngleIndex) );
307 // the animation which spin the light around the finger touch position
308 mLightAnimation = Animation::New(2.f);
309 mLightAnimation.AnimateTo( Property( mMeshActor, mSpinAngleIndex ), Math::PI*2.f );
310 mLightAnimation.SetLooping( true );
311 mLightAnimation.Pause();
314 void SetLightXYOffset( const Vector2& offset )
316 mMeshActor.SetProperty( mLightXYOffsetIndex, offset );
320 * Create a mesh actor with different geometry to replace the current one
322 bool OnChangeMesh( Toolkit::Button button )
324 mCurrentMeshId = ( mCurrentMeshId + 1 ) % NUM_MESH_FILES;
325 mGeometry = CreateGeometry( MESH_FILES[mCurrentMeshId] );
326 mRenderer.SetGeometry( mGeometry );
331 bool OnChangeTexture( Toolkit::Button button )
333 mCurrentTextureId = ( mCurrentTextureId + 1 ) % NUM_TEXTURE_IMAGES;
334 Texture texture = DemoHelper::LoadStageFillingTexture( TEXTURE_IMAGES[mCurrentTextureId] );
335 mTextureSet.SetTexture( 0u, texture );
339 bool OnTouch( Actor actor, const TouchData& event )
341 switch( event.GetState( 0 ) )
343 case PointState::DOWN:
345 mRenderer.SetShader( mShaderRefraction );
347 SetLightXYOffset( event.GetScreenPosition( 0 ) );
349 mLightAnimation.Play();
351 if( mStrenghAnimation )
353 mStrenghAnimation.Clear();
356 mStrenghAnimation= Animation::New(0.5f);
357 mStrenghAnimation.AnimateTo( Property( mMeshActor, mEffectStrengthIndex ), 1.f );
358 mStrenghAnimation.Play();
362 case PointState::MOTION:
364 // make the light position following the finger movement
365 SetLightXYOffset( event.GetScreenPosition( 0 ) );
369 case PointState::LEAVE:
370 case PointState::INTERRUPTED:
372 mLightAnimation.Pause();
374 if( mStrenghAnimation )
376 mStrenghAnimation.Clear();
378 mStrenghAnimation = Animation::New(0.5f);
379 mStrenghAnimation.AnimateTo( Property( mMeshActor, mEffectStrengthIndex ), 0.f );
380 mStrenghAnimation.FinishedSignal().Connect( this, &RefractionEffectExample::OnTouchFinished );
381 mStrenghAnimation.Play();
384 case PointState::STATIONARY:
393 void OnTouchFinished( Animation& source )
395 mRenderer.SetShader( mShaderFlat );
396 SetLightXYOffset( Vector2::ZERO );
399 Geometry CreateGeometry(const std::string& objFileName)
401 std::vector<Vector3> vertexPositions;
402 Vector<unsigned int> faceIndices;
403 Vector<float> boundingBox;
404 // read the vertice and faces from the .obj file, and record the bounding box
405 ReadObjFile( objFileName, boundingBox, vertexPositions, faceIndices );
407 std::vector<Vector2> textureCoordinates;
408 // align the mesh, scale it to fit the screen size, and calculate the texture coordinate for each vertex
409 ShapeResizeAndTexureCoordinateCalculation( boundingBox, vertexPositions, textureCoordinates );
411 // re-organize the mesh, the vertices are duplicated, each vertex only belongs to one triangle.
412 // Without sharing vertex between triangle, so we can manipulate the texture offset on each triangle conveniently.
413 std::vector<Vertex> vertices;
415 std::size_t size = faceIndices.Size();
416 vertices.reserve( size );
418 for( std::size_t i=0; i<size; i=i+3 )
420 Vector3 edge1 = vertexPositions[ faceIndices[i+2] ] - vertexPositions[ faceIndices[i] ];
421 Vector3 edge2 = vertexPositions[ faceIndices[i+1] ] - vertexPositions[ faceIndices[i] ];
422 Vector3 normal = edge1.Cross(edge2);
425 // make sure all the faces are front-facing
428 vertices.push_back( Vertex( vertexPositions[ faceIndices[i] ], normal, textureCoordinates[ faceIndices[i] ] ) );
429 vertices.push_back( Vertex( vertexPositions[ faceIndices[i+1] ], normal, textureCoordinates[ faceIndices[i+1] ] ) );
430 vertices.push_back( Vertex( vertexPositions[ faceIndices[i+2] ], normal, textureCoordinates[ faceIndices[i+2] ] ) );
435 vertices.push_back( Vertex( vertexPositions[ faceIndices[i] ], normal, textureCoordinates[ faceIndices[i] ] ) );
436 vertices.push_back( Vertex( vertexPositions[ faceIndices[i+2] ], normal, textureCoordinates[ faceIndices[i+2] ] ) );
437 vertices.push_back( Vertex( vertexPositions[ faceIndices[i+1] ], normal, textureCoordinates[ faceIndices[i+1] ] ) );
441 Property::Map vertexFormat;
442 vertexFormat["aPosition"] = Property::VECTOR3;
443 vertexFormat["aNormal"] = Property::VECTOR3;
444 vertexFormat["aTexCoord"] = Property::VECTOR2;
445 PropertyBuffer surfaceVertices = PropertyBuffer::New( vertexFormat );
446 surfaceVertices.SetData( &vertices[0], vertices.size() );
448 Geometry surface = Geometry::New();
449 surface.AddVertexBuffer( surfaceVertices );
454 void ReadObjFile( const std::string& objFileName,
455 Vector<float>& boundingBox,
456 std::vector<Vector3>& vertexPositions,
457 Vector<unsigned int>& faceIndices)
459 std::ifstream ifs( objFileName.c_str(), std::ios::in );
461 boundingBox.Resize( 6 );
462 boundingBox[0]=boundingBox[2]=boundingBox[4] = std::numeric_limits<float>::max();
463 boundingBox[1]=boundingBox[3]=boundingBox[5] = -std::numeric_limits<float>::max();
466 while( std::getline( ifs, line ) )
468 if( line[0] == 'v' && std::isspace(line[1])) // vertex
470 std::istringstream iss(line.substr(2), std::istringstream::in);
473 while( iss >> vertex[i++] && i < 3);
474 if( vertex.x < boundingBox[0] ) boundingBox[0] = vertex.x;
475 if( vertex.x > boundingBox[1] ) boundingBox[1] = vertex.x;
476 if( vertex.y < boundingBox[2] ) boundingBox[2] = vertex.y;
477 if( vertex.y > boundingBox[3] ) boundingBox[3] = vertex.y;
478 if( vertex.z < boundingBox[4] ) boundingBox[4] = vertex.z;
479 if( vertex.z > boundingBox[5] ) boundingBox[5] = vertex.z;
480 vertexPositions.push_back( vertex );
482 else if( line[0] == 'f' ) //face
484 unsigned int numOfInt = 3;
487 std::size_t found = line.find('/');
488 if( found == std::string::npos )
496 std::istringstream iss(line.substr(2), std::istringstream::in);
497 unsigned int indices[ numOfInt ];
499 while( iss >> indices[i++] && i < numOfInt);
500 unsigned int step = (i+1) / 3;
501 faceIndices.PushBack( indices[0]-1 );
502 faceIndices.PushBack( indices[step]-1 );
503 faceIndices.PushBack( indices[2*step]-1 );
510 void ShapeResizeAndTexureCoordinateCalculation( const Vector<float>& boundingBox,
511 std::vector<Vector3>& vertexPositions,
512 std::vector<Vector2>& textureCoordinates)
514 Vector3 bBoxSize( boundingBox[1] - boundingBox[0], boundingBox[3] - boundingBox[2], boundingBox[5] - boundingBox[4]);
515 Vector3 bBoxMinCorner( boundingBox[0], boundingBox[2], boundingBox[4] );
517 Vector2 stageSize = Stage::GetCurrent().GetSize();
518 Vector3 scale( stageSize.x / bBoxSize.x, stageSize.y / bBoxSize.y, 1.f );
519 scale.z = (scale.x + scale.y)/2.f;
521 textureCoordinates.reserve(vertexPositions.size());
523 for( std::vector<Vector3>::iterator iter = vertexPositions.begin(); iter != vertexPositions.end(); iter++ )
525 Vector3 newPosition( (*iter) - bBoxMinCorner ) ;
527 textureCoordinates.push_back( Vector2( newPosition.x / bBoxSize.x, newPosition.y / bBoxSize.y ) );
529 newPosition -= bBoxSize * 0.5f;
530 (*iter) = newPosition * scale;
535 * Main key event handler
537 void OnKeyEvent(const KeyEvent& event)
539 if(event.state == KeyEvent::Down)
541 if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
550 Application& mApplication;
552 TextureSet mTextureSet;
558 Shader mShaderRefraction;
560 Animation mLightAnimation;
561 Animation mStrenghAnimation;
563 Property::Index mLightXYOffsetIndex;
564 Property::Index mSpinAngleIndex;
565 Property::Index mLightIntensityIndex;
566 Property::Index mEffectStrengthIndex;
568 Toolkit::PushButton mChangeTextureButton;
569 Toolkit::PushButton mChangeMeshButton;
570 unsigned int mCurrentTextureId;
571 unsigned int mCurrentMeshId;
574 /*****************************************************************************/
577 RunTest(Application& app)
579 RefractionEffectExample theApp(app);
583 /*****************************************************************************/
585 int DALI_EXPORT_API main(int argc, char **argv)
587 Application app = Application::New(&argc, &argv, DEMO_THEME_PATH);