-
Notifications
You must be signed in to change notification settings - Fork 3
/
ViroActivity.java
executable file
·1519 lines (1265 loc) · 58.7 KB
/
ViroActivity.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// Copyright (c) 2017-present, ViroMedia, Inc.
// All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package com.viromedia.renderertest;
import android.content.Context;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.Point;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Display;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.Toast;
import com.viro.core.ARAnchor;
import com.viro.core.ARHitTestListener;
import com.viro.core.ARImageTarget;
import com.viro.core.ARPointCloud;
import com.viro.core.BoundingBox;
import com.viro.core.CameraImageListener;
import com.viro.core.CameraIntrinsics;
import com.viro.core.CameraListener;
import com.viro.core.ClickListener;
import com.viro.core.DragListener;
import com.viro.core.HoverListener;
import com.viro.core.Quaternion;
import com.viro.core.RendererConfiguration;
import com.viro.core.ViroMediaRecorder;
import com.viro.core.ViroViewScene;
import com.viro.core.internal.ARDeclarativeNode;
import com.viro.core.internal.ARDeclarativePlane;
import com.viro.core.ARHitTestResult;
import com.viro.core.ARNode;
import com.viro.core.ARPlaneAnchor;
import com.viro.core.ARScene;
import com.viro.core.AmbientLight;
import com.viro.core.Animation;
import com.viro.core.AnimationTimingFunction;
import com.viro.core.AnimationTransaction;
import com.viro.core.AsyncObject3DListener;
import com.viro.core.Box;
import com.viro.core.Camera;
import com.viro.core.Controller;
import com.viro.core.DirectionalLight;
import com.viro.core.EventDelegate;
import com.viro.core.internal.Image;
import com.viro.core.internal.ImageTracker;
import com.viro.core.Material;
import com.viro.core.Node;
import com.viro.core.Object3D;
import com.viro.core.OmniLight;
import com.viro.core.internal.ImageTrackerOutput;
import com.viro.core.ParticleEmitter;
import com.viro.core.Polyline;
import com.viro.core.Scene;
import com.viro.core.Sound;
import com.viro.core.SoundData;
import com.viro.core.Sphere;
import com.viro.core.Spotlight;
import com.viro.core.Surface;
import com.viro.core.Text;
import com.viro.core.Texture;
import com.viro.core.Texture.Format;
import com.viro.core.Vector;
import com.viro.core.VideoTexture;
import com.viro.core.ViroContext;
import com.viro.core.ViroView;
import com.viro.core.ViroViewARCore;
import com.viro.core.ViroViewGVR;
import com.viro.core.ViroViewOVR;
import com.viro.core.ClickState;
import com.viro.core.PinchState;
import com.viro.core.RotateState;
import com.viro.core.SwipeState;
import com.viro.core.TouchState;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class ViroActivity extends AppCompatActivity {
private static final String TAG = ViroActivity.class.getSimpleName();
private static int SOUND_COUNT = 0;
private final Map<String, Sound> mSoundMap = new HashMap<>();
private ARDeclarativeNode.Delegate mARNodeDelegate;
private ViroView mViroView = null;
private Handler mHandler;
@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mHandler = new Handler(getMainLooper());
RendererConfiguration config = new RendererConfiguration();
config.setShadowsEnabled(true);
config.setBloomEnabled(true);
config.setHDREnabled(true);
config.setPBREnabled(true);
mViroView = new ViroViewOVR(this, new ViroViewOVR.StartupListener() {
@Override
public void onSuccess() {
onRendererStart();
}
@Override
public void onFailure(ViroViewOVR.StartupError error, String errorMessage) {
onRendererFailed(error.toString(), errorMessage);
}
}, config);
setContentView(mViroView);
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if (mViroView instanceof ViroViewARCore) {
ViroViewARCore arView = (ViroViewARCore) mViroView;
Display display = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
arView.setCameraRotation(display.getRotation());
}
}
@Override
protected void onStart(){
super.onStart();
if (mViroView != null){
mViroView.onActivityStarted(this);
}
}
@Override
protected void onResume(){
super.onResume();
if (mViroView != null){
mViroView.onActivityResumed(this);
}
}
@Override
protected void onPause(){
super.onPause();
if (mViroView != null){
mViroView.onActivityPaused(this);
}
}
@Override
protected void onStop(){
super.onStop();
if (mViroView != null){
mViroView.onActivityStopped(this);
}
}
@Override
protected void onDestroy(){
super.onDestroy();
if (mViroView != null){
mViroView.onActivityDestroyed(this);
}
}
private void onRendererStart() {
mViroView.setVRModeEnabled(true);
mViroView.setDebugHUDEnabled(true);
initializeVrScene();
// Uncomment to run a test screenshot through the OpenCV target tracking code
//testFindInScreenshot();
}
private void onRendererFailed(String error, String errorMessage) {
Log.e("ViroActivity", "onRendererFailed [error: " + error + "], message [" + errorMessage + "]");
Toast.makeText(this, errorMessage, Toast.LENGTH_LONG).show();
}
private void initializeVrScene() {
// Creation of SceneControllerJni within scene navigator
final Scene scene = new Scene();
//scene.setBackgroundCubeWithColor(Color.WHITE);
final Node rootNode = scene.getRootNode();
List<Node> nodes = new ArrayList<>();
nodes = testBox(getApplicationContext());
final Bitmap background = getBitmapFromAssets("360_westlake.jpg");
final Texture backgroundTexture = new Texture(background, Texture.Format.RGBA8, true, true);
scene.setBackgroundTexture(backgroundTexture);
//testBackgroundVideo(scene);
//nodes.add(testLine(this));
//addNormalSound("http://www.kozco.com/tech/32.mp3");
//addNormalSound("http://www.bensound.com/royalty-free-music?download=dubstep");
//addSoundField("file:///android_asset/thelin.wav");
//addSpatialSound("http://www.kozco.com/tech/32.mp3");
//ByteBuffer hdrImage = getByteBufferFromAssets("wooden.vhd");
//Texture background = new Texture(hdrImage, null);
//scene.setBackgroundTexture(background);
//final SoundData data = new SoundData("http://www.kozco.com/tech/32.mp3", false);
//addSpatialSound(data);
//addNormalSound(data);
//mHandler.postDelayed(new Runnable() {
// @Override
// public void run() {
// setSoundRoom(scene, mViroView.getViroContext());
// }
// }, 5000);
//setSoundRoom(scene, mViroView.getViroContext());
for (final Node node : nodes) {
rootNode.addChildNode(node);
}
testSceneLighting(rootNode);
// Updating the scene.
mViroView.setScene(scene);
final Controller nativeController = mViroView.getController();
nativeController.setReticleStickyDepth(true);
mViroView.setCameraListener(new CameraListener() {
@Override
public void onTransformUpdate(Vector position, Vector r, Vector forward, Vector up) {
Vector l = new Vector(Math.toDegrees(r.x), Math.toDegrees(r.y),Math.toDegrees(r.z));
}
});
for (int i = 0; i < 20; i ++) {
VideoTexture mVideoTexture = new VideoTexture(mViroView.getViroContext(),
Uri.parse("file:///android_asset/stereoVid360.mp4"), null, Texture.StereoMode.TOP_BOTTOM);
mVideoTexture.setVolume(1);
mVideoTexture.setMuted(false);
mVideoTexture.setLoop(true);
mVideoTexture.play();
mVideoTexture.dispose();
}
}
/*
Used to initialize the AR Scene, should also change the mVrView to the AR one...
*/
private void initializeArScene() {
final ARScene scene = new ARScene();
final Node rootNode = scene.getRootNode();
((ViroViewARCore)mViroView).setCameraAutoFocusEnabled(true);
final List<Node> nodes = new ArrayList<>();
//nodes.addAll(testBox(this));
//testBackgroundImage(scene);
testAugmentedImageDatabase(scene);
// Add a box in front of the user
/*
Box box = new Box(.2f, .2f, .2f);
box.setMaterials(Arrays.asList(new Material()));
Node boxNode = new Node();
boxNode.setGeometry(box);
boxNode.setPosition(new Vector(0, 0, -1));
nodes.add(boxNode);
boxNode.setDragListener(new DragListener() {
@Override
public void onDrag(int source, Node node, Vector worldLocation, Vector localLocation) {
}
});
boxNode.setDragType(Node.DragType.FIXED_TO_PLANE);
boxNode.setDragPlanePoint(new Vector(0,-1,0));
boxNode.setDragPlaneNormal(new Vector(0,1,0));
boxNode.setDragMaxDistance(5);
*/
//nodes.addAll(testImperativePlane(scene));
//testARHitTest(scene, 0, 5);
//testAddArbitraryAnchors(scene, 0, 5);
//testHostCloudAnchors(scene, 0, 2);
//testResolveCloudAnchor(scene, "ua-ceb1cbf2825bf3545b4ef91650fbeff2");
//nodes.addAll(testARImageTarget(scene));
testCameraImageCapture(scene);
for (final Node node : nodes) {
rootNode.addChildNode(node);
}
testSceneLighting(rootNode);
// Updating the scene.
mViroView.setScene(scene);
//testVideoRecording();
//scene.displayPointCloud(true);
}
private void testAugmentedImageDatabase(final ARScene scene) {
try {
Uri databaseUrl = Uri.parse("file:///android_asset/mi_posters.imgdb");
scene.loadARImageDatabase(databaseUrl, new ARScene.LoadARImageDatabaseListener() {
@Override
public void onSuccess() {
}
@Override
public void onFailure(String error) {
}
});
} catch(Exception e) {
// do nothing.
}
scene.setListener(new ARScene.Listener() {
@Override
public void onTrackingInitialized() {
}
@Override
public void onTrackingUpdated(ARScene.TrackingState state, ARScene.TrackingStateReason reason) {
}
@Override
public void onAmbientLightUpdate(float intensity, Vector color) {
}
@Override
public void onAnchorFound(ARAnchor anchor, ARNode arNode) {
if (anchor.getType() == ARAnchor.Type.IMAGE) {
Log.d("ViroActivity", "found image: " + anchor.getAnchorId());
}
}
@Override
public void onAnchorUpdated(ARAnchor anchor, ARNode arNode) {
}
@Override
public void onAnchorRemoved(ARAnchor anchor, ARNode arNode) {
}
});
(new Handler()).postDelayed(new Runnable() {
@Override
public void run() {
Log.d("ViroActivity"," posting delayed!");
scene.unloadARImageDatabase();
}
}, 10000);
Bitmap bitmap = null;
try {
InputStream istr = getAssets().open("boba.png");
bitmap = BitmapFactory.decodeStream(istr);
} catch (IOException e) {
// handle exception
}
ARImageTarget target = new ARImageTarget(bitmap, ARImageTarget.Orientation.Up, .1f);
scene.addARImageTarget(target);
}
/*
This function runs the same test as VROTrackingHelper's findInScreenshot on iOS
*/
private void testFindInScreenshot() {
final Bitmap targetImage = getBitmapFromAssets("ben.jpg");
final Bitmap screenshot = getBitmapFromAssets("screenshot.png");
if (targetImage != null && screenshot != null) {
// width of a US bill is 6.14 inches ~= .156 meters
final ARImageTarget arImageTarget = new ARImageTarget(targetImage, ARImageTarget.Orientation.Up, .156f);
final ImageTracker tracker = new ImageTracker(arImageTarget);
ImageTrackerOutput output = tracker.findTarget(screenshot);
if (output.found()) {
for (float f : output.corners()) {
Log.i("ImageTracker", "corners are: " + f);
}
float[] position = output.position();
Log.i("ImageTracker", "position is: " + position[0] + ", " + position[1] + ", " + position[2]);
float[] rotation = output.rotation();
Log.i("ImageTracker", "rotation is: " + rotation[0] + ", " + rotation[1] + ", " + rotation[2]);
}
}
}
private ByteBuffer getByteBufferFromAssets(final String assetName) {
final InputStream istr;
final byte[] buffer = new byte[1024];
final ByteArrayOutputStream outStream = new ByteArrayOutputStream();
try {
istr = getAssets().open(assetName);
while (true) {
final int read = istr.read(buffer);
if (read == -1) {
break;
}
outStream.write(buffer, 0, read);
}
outStream.flush();
} catch (final IOException e) {
e.printStackTrace();
}
final ByteBuffer direct = ByteBuffer.allocateDirect(outStream.size());
direct.put(outStream.toByteArray());
direct.rewind();
return direct;
}
private Bitmap getBitmapFromAssets(final String assetName) {
final InputStream istr;
Bitmap bitmap = null;
try {
istr = getAssets().open(assetName);
bitmap = BitmapFactory.decodeStream(istr);
} catch (final IOException e) {
throw new IllegalArgumentException("Loading bitmap failed!", e);
}
return bitmap;
}
private List<Node> testText() {
final Node node = new Node();
// Create text
final Text text = new Text(mViroView.getViroContext(),
"Test Text Here", "Roboto", 25, Color.WHITE, 1f,
1f, Text.HorizontalAlignment.LEFT, Text.VerticalAlignment.TOP, Text.LineBreakMode.WORD_WRAP, Text.ClipMode.NONE, 0);
final float[] position = {0, -0.5f, -0.5f};
node.setPosition(new Vector(position));
node.setGeometry(text);
final Node pointOfView = new Node();
final Camera camera = new Camera();
camera.setPosition(new Vector(0, 0, 3));
pointOfView.setCamera(camera);
mViroView.setPointOfView(pointOfView);
return Arrays.asList(node);
}
private void testSceneLighting(final Node node) {
final float[] lightDirection = {0, -1, 0};
final AmbientLight ambientLightJni = new AmbientLight(Color.WHITE, 300.0f);
node.addLight(ambientLightJni);
final DirectionalLight directional = new DirectionalLight(Color.BLUE, 1000.0f, new Vector(lightDirection));
directional.setCastsShadow(true);
directional.setShadowOrthographicSize(10);
node.addLight(directional);
final float[] omniLightPosition = {1, 0, 0};
final OmniLight omni = new OmniLight(Color.WHITE, 1000.0f, 1, 10, new Vector(omniLightPosition));
node.addLight(omni);
}
private List<Node> testSurfaceVideo(final Context context) {
final Node node = new Node();
final Surface surface = new Surface(4, 4, 0, 0, 1, 1);
final float[] position = {0, 0, -3};
node.setPosition(new Vector(position));
final VideoTexture videoTexture = new VideoTexture(mViroView.getViroContext(), Uri.parse("https://s3.amazonaws.com/viro.video/Climber2Top.mp4"));
final Material material = new Material();
material.setDiffuseTexture(videoTexture);
surface.setMaterials(Arrays.asList(material));
videoTexture.setVolume(0.1f);
videoTexture.setLoop(true);
videoTexture.play();
node.setGeometry(surface);
return Arrays.asList(node);
}
private List<Node> testSphereVideo(final Context context) {
final Node node = new Node();
final Sphere sphere = new Sphere(2, 20, 20, false);
final VideoTexture videoTexture = new VideoTexture(mViroView.getViroContext(),
Uri.parse("https://s3.amazonaws.com/viro.video/Climber2Top.mp4"));
final Material material = new Material();
material.setDiffuseTexture(videoTexture);
sphere.setMaterials(Arrays.asList(material));
videoTexture.setVolume(0.1f);
videoTexture.setLoop(false);
videoTexture.play();
node.setGeometry(sphere);
return Arrays.asList(node);
}
private void testBackgroundVideo(final Scene scene) {
final VideoTexture videoTexture = new VideoTexture(mViroView.getViroContext(),
Uri.parse("https://s3.amazonaws.com/viro.video/Climber2Top.mp4"));
scene.setBackgroundTexture(videoTexture);
videoTexture.setVolume(0.1f);
videoTexture.setLoop(false);
videoTexture.play();
}
static class TestCameraImageListener implements CameraImageListener {
@Override
public void onCameraImageUpdated(ByteBuffer buffer, int width, int height, CameraIntrinsics intrinsics) {
Log.i("Viro", "Received image with buffer size " + buffer.capacity() + ", limit " + buffer.limit() + ", remaining " + buffer.remaining());
Log.i("Viro", "Focal Length" + intrinsics.getFocalLength()[0] + ", " + intrinsics.getFocalLength()[1] + "; Principal Point " + intrinsics.getPrincipalPoint()[0] + ", " + intrinsics.getPrincipalPoint()[1]);
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
bitmap.copyPixelsFromBuffer(buffer);
//ViroViewARCore.setBitmapOnTrackingImageView(bitmap);
}
}
private void testCameraImageCapture(final ARScene scene) {
((ViroViewARCore) mViroView).setCameraImageListener(mViroView.getViroContext(), new TestCameraImageListener());
//mHandler.postDelayed(new Runnable() {
// @Override
// public void run() {
// ((ViroViewARCore) mViroView).setCameraImageListener(mViroView.getViroContext(), null);
// }
//}, 5000);
}
private void testBackgroundImage(final Scene scene) {
final Image imageJni = new Image("boba.png", Format.RGBA8);
final Texture videoTexture = new Texture(imageJni, true, false);
scene.setBackgroundTexture(videoTexture);
final float[] rotation = {90, 0, 0};
scene.setBackgroundRotation(new Vector(rotation));
}
private void testSkyBoxImage(final Scene scene) {
final Format format = Format.RGBA8;
final Image pximageJni = new Image("px.png", format);
final Image nximageJni = new Image("nx.png", format);
final Image pyimageJni = new Image("py.png", format);
final Image nyimageJni = new Image("ny.png", format);
final Image pzimageJni = new Image("pz.png", format);
final Image nzimageJni = new Image("nz.png", format);
final Texture cubeTexture = new Texture(pximageJni, nximageJni, pyimageJni, nyimageJni,
pzimageJni, nzimageJni);
scene.setBackgroundCubeTexture(cubeTexture);
}
private List<Node> testParticles() {
final Bitmap bobaBitmap = getBitmapFromAssets("boba.png");
final Bitmap specBitmap = getBitmapFromAssets("specular.png");
final Texture bobaTexture = new Texture(bobaBitmap, Format.RGBA8, true, true);
final Node particleNode = new Node();
particleNode.setPosition(new Vector(0, -10, -15));
final Material material = new Material();
material.setDiffuseTexture(bobaTexture);
material.setDiffuseColor(Color.BLUE);
material.setSpecularTexture(bobaTexture);
material.setLightingModel(Material.LightingModel.LAMBERT);
final Surface surface = new Surface(1, 1);
surface.setMaterials(Arrays.asList(material));
final ParticleEmitter particleEmitter = new ParticleEmitter(mViroView.getViroContext(), surface);
final Vector sizeMinStart = new Vector(2, 2, 2);
final Vector sizeMaxStart = new Vector(5, 5, 5);
final ParticleEmitter.ParticleModifierVector modifier = new ParticleEmitter.ParticleModifierVector(sizeMinStart, sizeMaxStart);
modifier.addInterval(1000, new Vector(0, 0, 0));
particleEmitter.setScaleModifier(modifier);
particleNode.setParticleEmitter(particleEmitter);
particleEmitter.run();
return Arrays.asList(particleNode);
}
private ByteBuffer getRBGAFromBitmap(Bitmap bitmap) {
int width = bitmap.getWidth();
int height = bitmap.getHeight();
int componentsPerPixel = 4;
int totalPixels = width * height;
int totalBytes = totalPixels * componentsPerPixel;
byte[] rgbValues = new byte[totalBytes];
int[] argbPixels = new int[totalPixels];
bitmap.getPixels(argbPixels, 0, width, 0, 0, width, height);
for (int i = 0; i < totalPixels; i++) {
int argbPixel = argbPixels[i];
int red = Color.red(argbPixel);
int green = Color.green(argbPixel);
int blue = Color.blue(argbPixel);
int alpha = Color.alpha(argbPixel);
rgbValues[i * componentsPerPixel + 0] = (byte) red;
rgbValues[i * componentsPerPixel + 1] = (byte) green;
rgbValues[i * componentsPerPixel + 2] = (byte) blue;
rgbValues[i * componentsPerPixel + 3] = (byte) alpha;
}
ByteBuffer buffer = ByteBuffer.allocateDirect(rgbValues.length);
buffer.put(rgbValues);
buffer.flip();
return buffer;
}
private List<Node> testBox(final Context context) {
final Node pointOfView = new Node();
final Camera camera = new Camera();
camera.setPosition(new Vector(0, 0, 2f));
// pointOfView.setCamera(camera);
pointOfView.setPosition(new Vector(0, 0, 2));
pointOfView.setRotation(new Vector(0, Math.toRadians(90), 0));
mViroView.setPointOfView(pointOfView);
final Node node1 = new Node();
final Node node2 = new Node();
final Node node3 = new Node();
final Text textJni = new Text(mViroView.getViroContext(), "Test text 1 2 3", "Roboto", 24,
Color.WHITE, 10, 4, Text.HorizontalAlignment.CENTER, Text.VerticalAlignment.CENTER, Text.LineBreakMode.NONE,
Text.ClipMode.CLIP_TO_BOUNDS, 1);
Material m = new Material();
m.setCullMode(Material.CullMode.NONE);
textJni.setMaterials(Arrays.asList(m));
final float[] position = {0, -0.5f, -0.5f};
node3.setPosition(new Vector(position));
node3.setGeometry(textJni);
Node n = new Node();
n.addChildNode(node3);
n.setEventDelegate(getGenericDelegate("Text"));
final Bitmap bobaBitmap = getBitmapFromAssets("boba.png");
final Bitmap specBitmap = getBitmapFromAssets("specular.png");
//final Texture bobaTexture = new Texture(bobaBitmap, TextureFormat.RGBA8, true, true);
final Texture bobaTexture = new Texture(getRBGAFromBitmap(bobaBitmap), bobaBitmap.getWidth(), bobaBitmap.getHeight(), Format.RGBA8, Format.RGBA8,
true, false, null);
final Texture specTexture = new Texture(specBitmap, Format.RGBA8, true, true);
final Material material = new Material();
material.setDiffuseTexture(bobaTexture);
material.setSpecularTexture(specTexture);
material.setLightingModel(Material.LightingModel.PHYSICALLY_BASED);
//material.setBloomThreshold(0.2f);
// Creation of ViroBox to the right and billboarded
final Box boxGeometry = new Box(0.5f, 0.5f, 0.5f);
node1.setGeometry(boxGeometry);
final Vector boxPosition = new Vector(-2, 0, 1);
node1.setPosition(boxPosition);
boxGeometry.setMaterials(Arrays.asList(material));
node1.setEventDelegate(getGenericDelegate("Box"));
final Box boxGeometry2 = new Box(0.5f, 0.5f, 0.5f);
node2.setGeometry(boxGeometry2);
final Vector boxPosition2 = new Vector(1, 0, -2.5);
node2.setPosition(boxPosition2);
boxGeometry2.setMaterials(Arrays.asList(material));
node2.setEventDelegate(getGenericDelegate("node2"));
final Node node4 = new Node();
final Box boxGeometry4 = new Box(0.5f, 0.5f, 0.5f);
node4.setGeometry(boxGeometry4);
final Vector boxPosition4 = new Vector(0, 0, -4);
node4.setPosition(boxPosition4);
boxGeometry4.setMaterials(Arrays.asList(material));
node4.setEventDelegate(getGenericDelegate("node4"));
final Node node5 = new Node();
final Box boxGeometry5 = new Box(0.5f, 0.5f, 0.5f);
node5.setGeometry(boxGeometry5);
final Vector boxPosition5 = new Vector(1, 0, -15);
node5.setPosition(boxPosition5);
boxGeometry2.setMaterials(Arrays.asList(material));
node5.setEventDelegate(getGenericDelegate("node5"));
final Node node6 = new Node();
final Box boxGeometry6 = new Box(0.5f, 0.5f, 0.5f);
node6.setGeometry(boxGeometry6);
final Vector boxPosition6 = new Vector(2, 0, -27);
node6.setPosition(boxPosition6);
boxGeometry6.setMaterials(Arrays.asList(material));
node6.setEventDelegate(getGenericDelegate("node6"));
// Light for the box
Spotlight light = new Spotlight();
light.setDirection(new Vector(0, 0, -1));
light.setPosition(new Vector(0, 0, 5));
light.setIntensity(1500);
node1.addLight(light);
AnimationTransaction.begin();
AnimationTransaction.setAnimationDelay(1000);
AnimationTransaction.setAnimationDuration(5000);
AnimationTransaction.setTimingFunction(AnimationTimingFunction.Bounce);
AnimationTransaction.setListener(new AnimationTransaction.Listener() {
@Override
public void onFinish(final AnimationTransaction transaction) {
Log.i("Viro", "Animation finished");
}
});
node2.setPosition(new Vector(-2, 2.5f, -3));
final AnimationTransaction transaction = AnimationTransaction.commit();
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
BoundingBox box = node1.getBoundingBox();
Log.d("Viro", "bounding box for [2,4,2] box is: " + box.minX + ", " + box.maxX + ", " + box.minY
+ ", " + box.maxY + ", " + box.minZ + ", " + box.maxZ);
}
}, 1000);
return Arrays.asList(pointOfView, node1, node2, node4, node5, node6, n);
}
private List<Node> test3dObjectLoading(final Context context) {
final Node node1 = new Node();
// Creation of ObjectJni to the right
final Object3D objectJni = new Object3D();
objectJni.loadModel(mViroView.getViroContext(),
Uri.parse("file:///android_asset/object_star_anim.vrx"),
Object3D.Type.FBX, new AsyncObject3DListener() {
@Override
public void onObject3DLoaded(final Object3D object, final Object3D.Type type) {
object.setPosition(new Vector(0, 0, -3));
object.setScale(new Vector(0.4f, 0.4f, 0.4f));
final Animation animation = object.getAnimation("02_spin");
animation.setDelay(5000);
animation.setLoop(true);
animation.play();
}
@Override
public void onObject3DFailed(final String error) {
}
});
node1.addChildNode(objectJni);
return Arrays.asList(node1);
}
private List<Node> testImageSurface(final Context context) {
final Node node = new Node();
final Image bobaImage = new Image("boba.png", Format.RGBA8);
final Texture bobaTexture = new Texture(bobaImage, true, true);
final Material material = new Material();
final Surface surface = new Surface(1, 1, 0, 0, 1, 1);
surface.setMaterial(material);
surface.setImageTexture(bobaTexture);
node.setGeometry(surface);
final float[] position = {0, 0, -2};
node.setPosition(new Vector(position));
return Arrays.asList(node);
}
private List<Node> testStereoImageSurface(final Context context) {
final float[] position1 = {-1f, 1f, -3.3f};
final Node imageNode1 = getStereoImage(position1, "stereo1.jpg");
final float[] position2 = {0, 1f, -3.3f};
final Node imageNode2 = getStereoImage(position2, "stereo2.jpg");
return Arrays.asList(imageNode1,
imageNode2);
}
private Node getStereoImage(final float[] pos, final String img) {
final Node node = new Node();
final Image bobaImage = new Image(img, Format.RGBA8);
final Texture bobaTexture = new Texture(bobaImage, true, true, "LeftRight");
final Material material = new Material();
final Surface surface = new Surface(1, 1, 0, 0, 1, 1);
surface.setMaterial(material);
surface.setImageTexture(bobaTexture);
node.setGeometry(surface);
node.setPosition(new Vector(pos));
return node;
}
private void testStereoBackgroundImage(final Scene scene) {
final Image imageJni = new Image("stereo3601.jpg", Format.RGBA8);
final Texture videoTexture = new Texture(imageJni, true, false, "TopBottom");
scene.setBackgroundTexture(videoTexture);
final float[] rotation = {0, 0, 0};
scene.setBackgroundRotation(new Vector(rotation));
}
private List<Node> testStereoSurfaceVideo(final Context context) {
final Node node = new Node();
final Surface surface = new Surface(4, 4, 0, 0, 1, 1);
final float[] position = {0, 0, -5};
node.setPosition(new Vector(position));
final VideoTexture videoTexture = new VideoTexture(mViroView.getViroContext(),
Uri.parse("file:///android_asset/stereoVid.mp4"), null, Texture.StereoMode.LEFT_RIGHT);
videoTexture.setVolume(0.1f);
videoTexture.setLoop(true);
videoTexture.play();
node.setGeometry(surface);
return Arrays.asList(node);
}
private void testStereoBackgroundVideo(final Scene scene) {
final VideoTexture.PlaybackListener delegate = new VideoTexture.PlaybackListener() {
@Override
public void onVideoBufferStart(final VideoTexture video) {
}
@Override
public void onVideoBufferEnd(final VideoTexture video) {
}
@Override
public void onVideoFinish(final VideoTexture video) {
}
@Override
public void onVideoFailed(final String error) {
}
@Override
public void onReady(final VideoTexture video) {
scene.setBackgroundTexture(video);
video.setVolume(0.1f);
video.setLoop(false);
video.play();
}
@Override
public void onVideoUpdatedTime(final VideoTexture video, final float seconds, final float duration) {
Log.e(TAG,"onVideoUpdatedTime for Background within ViroActivity:" + seconds);
}
};
final VideoTexture videoTexture = new VideoTexture(mViroView.getViroContext(), Uri.parse("file:///android_asset/stereoVid360.mp4"),
delegate, Texture.StereoMode.TOP_BOTTOM);
}
private List<Node> testARPlane(final ARScene arScene) {
final ARDeclarativePlane arPlane = new ARDeclarativePlane(0, 0, ARPlaneAnchor.Alignment.HORIZONTAL);
final Node node = new Node();
final Surface surface = new Surface(.5f, .5f, 0, 0, 1, 1);
final float[] rotation = { (float) -Math.PI / 2, 0, 0};
node.setRotation(new Vector(rotation));
mARNodeDelegate = new ARDeclarativeNode.Delegate() {
@Override
public void onAnchorFound(final ARAnchor anchor) {
Log.i("ViroActivity", "onAnchorFound");
}
@Override
public void onAnchorUpdated(final ARAnchor anchor) {
final ARPlaneAnchor planeAnchor = (ARPlaneAnchor) anchor;
Log.i("ViroActivity", "onAnchorUpdated width " + planeAnchor.getExtent().x + ", " + planeAnchor.getExtent().z);
surface.setWidth(planeAnchor.getExtent().x);
surface.setHeight(planeAnchor.getExtent().z);
}
@Override
public void onAnchorRemoved() {
Log.i("ViroActivity", "onAnchorRemoved");
}
};
arPlane.setDelegate(mARNodeDelegate);
node.setGeometry(surface);
arPlane.addChildNode(node);
arScene.addARDeclarativeNode(arPlane);
final ArrayList<Node> list = new ArrayList<>();
list.add(arPlane);
return list;
}
private Object3D loadObjectNode(final int bitmask, final float scale, final Vector position) {
final Object3D objectNode = new Object3D();
objectNode.loadModel(mViroView.getViroContext(),
Uri.parse("file:///android_asset/object_star_anim.vrx"),
Object3D.Type.FBX, new AsyncObject3DListener() {
@Override
public void onObject3DLoaded(final Object3D object, final Object3D.Type type) {
object.setPosition(position);
object.setScale(new Vector(scale, scale, scale));
object.setLightReceivingBitMask(bitmask);
object.setShadowCastingBitMask(bitmask);
final Animation animation = object.getAnimation("02_spin");
//animation.setDelay(5000);
animation.setLoop(true);
animation.play();
}
@Override
public void onObject3DFailed(final String error) {
}
});
return objectNode;
}
private List<ARNode> anchoredNodes = new ArrayList<ARNode>();
private void testARHitTest(final ARScene scene, final int count, final int total) {
// Once count hits total, clear all the anchors we've found
if (count >= total) {
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
Log.i("Viro", "Detaching all nodes");
for (ARNode node : anchoredNodes) {
node.detach();
}
anchoredNodes.clear();
}}, 2000);
return;
}
final ViroViewARCore view = (ViroViewARCore) mViroView;
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
Point point = new Point((int) (view.getWidth() / 2.0), (int) (view.getHeight() / 2.0f));
Log.i("Viro", "Performing AR hit test at point " + point.x + ", " + point.y);
view.performARHitTest(point,
new ARHitTestListener() {
@Override
public void onHitTestFinished(ARHitTestResult[] results) {
Log.i("Viro", "Hit test complete with " + results.length + " results");
for (ARHitTestResult result : results) {
if (result.getType() == ARHitTestResult.Type.FEATURE_POINT) {
Log.i("Viro", " Hit feature point, creating little star");
ARNode node = result.createAnchoredNode();
node.addChildNode(loadObjectNode(1, .1f, new Vector(0, 0, 0)));
anchoredNodes.add(node);
} else if (result.getType() == ARHitTestResult.Type.PLANE) {
Log.i("Viro", " Hit plane, creating big star");
ARNode node = result.createAnchoredNode();
node.addChildNode(loadObjectNode(1, .2f, new Vector(0, 0, 0)));