2 using System.Runtime.InteropServices;
3 using Tizen.NUI.BaseComponents;
4 using System.Collections.Generic;
6 namespace Tizen.NUI.Samples
8 // Make custom view to ignore Layout features so we can use Depth information
9 public class Custom3DView : CustomView
11 // Default View Enabled SizeNegotiations, and It is Breakdown the depth values.
12 // So we need to create CustomView whitch CustomViewBehavior.DisableSizeNegotiation.
13 public Custom3DView() : base("Custom3DView", CustomViewBehaviour.DisableSizeNegotiation)
18 // copied from https://github.com/hinohie/nui-demo/blob/geotest/Mesh/Mesh.cs
19 public class DisposeTest : IExample
25 public Vec2(float xIn, float yIn)
36 public Vec3(float xIn, float yIn, float zIn)
44 struct TexturedQuadVertex
51 static readonly string VERTEX_SHADER =
52 "attribute mediump vec3 aPosition;\n" +
53 "attribute mediump vec3 aNormal;\n" +
54 "attribute mediump vec2 aTexCoord;\n" +
55 "uniform mediump mat4 uMvpMatrix;\n" +
56 "uniform mediump mat3 uNormalMatrix;\n" +
57 "uniform mediump vec3 uSize;\n" +
58 "varying mediump vec3 vNormal;\n" +
59 "varying mediump vec2 vTexCoord;\n" +
60 "varying mediump vec3 vPosition;\n" +
63 " vec4 pos = vec4(aPosition, 1.0)*vec4(uSize,1.0);\n"+
64 " gl_Position = uMvpMatrix*pos;\n" +
65 " vPosition = aPosition;\n" +
66 " vNormal = normalize(uNormalMatrix * aNormal);\n" +
67 " vTexCoord = aTexCoord;\n" +
70 static readonly string FRAGMENT_SHADER =
71 "uniform lowp vec4 uColor;\n" +
72 "uniform sampler2D sTexture;\n" +
73 "varying mediump vec3 vNormal;\n" +
74 "varying mediump vec2 vTexCoord;\n" +
75 "varying mediump vec3 vPosition;\n" +
76 "mediump vec3 uLightDir = vec3(2.0, 0.5, 1.0);\n" + // constant light dir
77 "mediump vec3 uViewDir = vec3(0.0, 0.0, 1.0);\n" + // constant view dir.
78 "mediump vec3 uAmbientColor = vec3(0.2, 0.2, 0.2);\n" +
79 "mediump vec3 uDiffuseColor = vec3(0.8, 0.8, 0.8);\n" +
80 "mediump vec3 uSpecularColor = vec3(0.5, 0.5, 0.5);\n" +
83 " mediump vec3 lightdir = normalize(uLightDir);\n" +
84 " mediump vec3 eyedir = normalize(uViewDir);\n" +
85 " mediump vec4 texColor = texture2D( sTexture, vTexCoord ) * uColor;\n" +
86 " mediump float diffuse = min(max(-dot(vNormal, lightdir) + 0.1, 0.0), 1.0);\n" +
87 " mediump vec3 reflectdir = reflect(-lightdir, vNormal);\n" +
88 " mediump float specular = pow(max(0.0, dot(reflectdir, eyedir)), 50.0);\n" +
89 " mediump vec4 color = texColor * vec4(uAmbientColor + uDiffuseColor * diffuse, 1.0) + vec4(uSpecularColor, 0.0) * specular;\n" +
90 " gl_FragColor = color;\n" +
93 // Copy from dali-toolkit/internal/visuals/primitive/primitive-visual.cpp
94 // NOTE. I add one more slices for texture coordinate
95 private global::System.IntPtr SphereVertexDataPtr()
97 TexturedQuadVertex[] vertices = new TexturedQuadVertex[SPHERE_VERTEX_NUMBER];
99 const int slices = SPHERE_SLICES;
100 const int stacks = SPHERE_STACKS;
103 int vertexIndex = 0; //Track progress through vertices.
109 vertices[vertexIndex].position = new Vec3(0.0f, 0.5f, 0.0f);
110 vertices[vertexIndex].normal = new Vec3(0.0f, 1.0f, 0.0f);
111 vertices[vertexIndex].texcoord = new Vec2(0.5f, 1.0f);
115 for (int i = 1; i < stacks; i++)
117 //Note. This vertex method is not common.
118 //We set one more vertexes for correct texture coordinate. at j == slices
119 //j==0 and j==slices have equal position, normal, but there texcoord.x is different
120 for (int j = 0; j <= slices; j++, vertexIndex++)
122 float cos_j = (float)Math.Cos(2.0f * (float)Math.PI * j / (float)slices);
123 float sin_j = (float)Math.Sin(2.0f * (float)Math.PI * j / (float)slices);
124 float cos_i = (float)Math.Cos((float)Math.PI * i / (float)stacks);
125 float sin_i = (float)Math.Sin((float)Math.PI * i / (float)stacks);
130 vertices[vertexIndex].position = new Vec3(x / 2.0f, y / 2.0f, z / 2.0f);
131 vertices[vertexIndex].normal = new Vec3(x, y, z);
132 vertices[vertexIndex].texcoord = new Vec2((float)j / (float)slices, 1.0f - (float)i / (float)stacks);
137 vertices[vertexIndex].position = new Vec3(0.0f, -0.5f, 0.0f);
138 vertices[vertexIndex].normal = new Vec3(0.0f, -1.0f, 0.0f);
139 vertices[vertexIndex].texcoord = new Vec2(0.5f, 0.0f);
143 int length = Marshal.SizeOf(vertices[0]);
144 global::System.IntPtr pA = Marshal.AllocHGlobal(length * SPHERE_VERTEX_NUMBER);
146 for (int i = 0; i < SPHERE_VERTEX_NUMBER; i++)
148 Marshal.StructureToPtr(vertices[i], pA + i * length, true);
154 private ushort[] SphereIndexData()
156 ushort[] indices = new ushort[SPHERE_INDEX_NUMBER];
157 const int slices = SPHERE_SLICES;
158 const int stacks = SPHERE_STACKS;
162 int indiceIndex = 0; //Used to keep track of progress through indices.
163 int previousCycleBeginning = 1; //Stores the index of the vertex that started the cycle of the previous stack.
164 int currentCycleBeginning = 1 + slices + 1;
166 //Top stack. Loop from index 1 to index slices, as not counting the very first vertex.
167 for (int i = 1; i <= slices; i++, indiceIndex += 3)
169 indices[indiceIndex] = 0;
170 indices[indiceIndex + 1] = (ushort)(i + 1);
171 indices[indiceIndex + 2] = (ushort)i;
174 //Middle Stacks. Want to form triangles between the top and bottom stacks, so loop up to the number of stacks - 2.
175 //Note. This index method is not common.
176 //We increase Beginning indexes slices+1 cause we add one more vertexes for correct texture coordinate.
177 for (int i = 0; i < stacks - 2; i++, previousCycleBeginning += slices + 1, currentCycleBeginning += slices + 1)
179 for (int j = 0; j < slices; j++, indiceIndex += 6)
181 indices[indiceIndex] = (ushort)(previousCycleBeginning + j);
182 indices[indiceIndex + 1] = (ushort)(previousCycleBeginning + 1 + j);
183 indices[indiceIndex + 2] = (ushort)(currentCycleBeginning + j);
184 indices[indiceIndex + 3] = (ushort)(currentCycleBeginning + j);
185 indices[indiceIndex + 4] = (ushort)(previousCycleBeginning + 1 + j);
186 indices[indiceIndex + 5] = (ushort)(currentCycleBeginning + 1 + j);
190 //Bottom stack. Loop around the last stack from the previous loop, and go up to the penultimate vertex.
191 for (int i = 0; i < slices; i++, indiceIndex += 3)
193 indices[indiceIndex] = (ushort)(previousCycleBeginning + slices + 1);
194 indices[indiceIndex + 1] = (ushort)(previousCycleBeginning + i);
195 indices[indiceIndex + 2] = (ushort)(previousCycleBeginning + i + 1);
202 const int SPHERE_SLICES = 30; // >= 3
203 const int SPHERE_STACKS = 20; // >= 1
204 const int SPHERE_VERTEX_NUMBER = (SPHERE_SLICES + 1) * (SPHERE_STACKS - 1) + 2;
205 const int SPHERE_INDEX_NUMBER = 6 * SPHERE_SLICES * (SPHERE_STACKS - 1);
208 private const int AutoDisposedObjectCount = 10;
209 private const int ManualDisposedObjectCount = 10;
213 private bool toggle = false;
214 private string resource;
215 private List<Custom3DView> views;
216 private Animation rotateAnimation;
218 public void Activate()
220 win = NUIApplication.GetDefaultWindow();
221 resource = Tizen.Applications.Application.Current.DirectoryInfo.Resource;
222 // Set layer behavior as Layer3D. without this, Rendering will be broken
223 win.GetDefaultLayer().Behavior = Layer.LayerBehavior.Layer3D;
227 Size = new Size(10, 10),
228 BackgroundColor = Color.Blue,
232 views = new List<Custom3DView>();
233 rotateAnimation = new Animation(1500); //1.5s
238 timer = new Timer(3000); //3s
239 timer.Tick += OnTimerTick;
244 private bool OnTimerTick(object source, Timer.TickEventArgs e)
259 private Geometry GenerateGeometry()
261 PropertyMap vertexFormat = new PropertyMap();
262 vertexFormat.Add("aPosition", new PropertyValue((int)PropertyType.Vector3));
263 vertexFormat.Add("aNormal", new PropertyValue((int)PropertyType.Vector3));
264 vertexFormat.Add("aTexCoord", new PropertyValue((int)PropertyType.Vector2));
265 PropertyBuffer vertexBuffer = new PropertyBuffer(vertexFormat);
267 vertexBuffer.SetData(SphereVertexDataPtr(), SPHERE_VERTEX_NUMBER);
269 ushort[] indexBuffer = SphereIndexData();
271 Geometry geometry = new Geometry();
272 geometry.AddVertexBuffer(vertexBuffer);
273 geometry.SetIndexBuffer(indexBuffer, SPHERE_INDEX_NUMBER);
274 geometry.SetType(Geometry.Type.TRIANGLES);
278 private void AddManyViews()
280 Random rand = new Random();
282 for (int i = 0; i < AutoDisposedObjectCount; i++)
285 var view = new Custom3DView()
287 Size = new Size(viewSize, viewSize, viewSize),
288 Position = new Position(
289 rand.Next(10, win.WindowSize.Width - 10),
290 rand.Next(10, win.WindowSize.Height - 10),
291 rand.Next(-3 * viewSize, 3 * viewSize)
296 PixelData pixelData = PixelBuffer.Convert(ImageLoading.LoadImageFromFile(
297 resource + "/images/PopupTest/circle.jpg",
299 FittingModeType.ScaleToFill
301 Texture texture = new Texture(
302 TextureType.TEXTURE_2D,
303 pixelData.GetPixelFormat(),
304 pixelData.GetWidth(),
305 pixelData.GetHeight()
307 texture.Upload(pixelData);
308 TextureSet textureSet = new TextureSet();
309 textureSet.SetTexture(0u, texture);
310 Renderer renderer = new Renderer(GenerateGeometry(), new Shader(VERTEX_SHADER, FRAGMENT_SHADER));
311 renderer.SetTextures(textureSet);
312 view.AddRenderer(renderer);
314 rotateAnimation.AnimateBy(view, "Orientation", new Rotation(new Radian(new Degree(360.0f)), Vector3.YAxis));
317 for (int i = 0; i < ManualDisposedObjectCount; i++)
320 var view = new Custom3DView()
322 Size = new Size(viewSize, viewSize, viewSize),
323 Position = new Position(
324 rand.Next(10, win.WindowSize.Width - 10),
325 rand.Next(10, win.WindowSize.Height - 10),
326 rand.Next(-3 * viewSize, 3 * viewSize)
332 PixelData pixelData = PixelBuffer.Convert(ImageLoading.LoadImageFromFile(
333 resource + "/images/PaletteTest/red2.jpg",
335 FittingModeType.ScaleToFill
337 Texture texture = new Texture(
338 TextureType.TEXTURE_2D,
339 pixelData.GetPixelFormat(),
340 pixelData.GetWidth(),
341 pixelData.GetHeight()
343 texture.Upload(pixelData);
344 TextureSet textureSet = new TextureSet();
345 textureSet.SetTexture(0u, texture);
346 Renderer renderer = new Renderer(GenerateGeometry(), new Shader(VERTEX_SHADER, FRAGMENT_SHADER));
347 renderer.SetTextures(textureSet);
348 view.AddRenderer(renderer);
350 rotateAnimation.AnimateBy(view, "Orientation", new Rotation(new Radian(new Degree(-360.0f)), Vector3.YAxis));
352 rotateAnimation.Looping = true;
353 rotateAnimation.Play();
355 private void RemoveAllViews()
357 uint cnt = root.ChildCount;
358 for (int i = (int)(cnt - 1); i >= 0; i--)
360 root.Remove(root.GetChildAt((uint)i));
362 foreach(var view in views)
364 var renderer = view.GetRendererAt(0);
369 rotateAnimation.Clear();
372 private void FullGC()
374 global::System.GC.Collect();
375 global::System.GC.WaitForPendingFinalizers();
376 global::System.GC.Collect();
379 public void Deactivate()
383 rotateAnimation?.Dispose();