-
Notifications
You must be signed in to change notification settings - Fork 1
/
realitykit_terrainshader_surfaceshader_geometrymodifier.html
778 lines (592 loc) · 35 KB
/
realitykit_terrainshader_surfaceshader_geometrymodifier.html
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
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<!-- Favicon -->
<!--
<link rel="shortcut icon" href="./assets/favicon/favicon.ico" type="image/x-icon" />
-->
<link rel="shortcut icon" href="./photo.ico" type="image/x-icon" />
<!-- Map CSS -->
<link rel="stylesheet" href="https://api.mapbox.com/mapbox-gl-js/v0.53.0/mapbox-gl.css" />
<!-- Libs CSS -->
<link rel="stylesheet" href="./assets/css/libs.bundle.css" />
<!-- Theme CSS -->
<link rel="stylesheet" href="./assets/css/theme.bundle.css" />
<!-- Title -->
<title>Create an AR Terrain with RealityKit Surface Shader and Geometry Modifier</title>
<meta content="Create an AR Terrain with RealityKit Surface Shader and Geometry Modifier"
name="description">
<meta content="RealityKit, Surface Shader, Geometry Modifier, AR Terrain, Terrain Shader"
name="keywords">
</head>
<body>
<!-- NAVBAR -->
<nav class="navbar navbar-expand-lg navbar-dark">
<div class="container-fluid">
<!-- Brand -->
<a class="navbar-brand" href="./index.html">
<!--
<img src="./assets/img/brand.svg" class="navbar-brand-img" alt="...">
<img src="./images/sigmanode.png" class="navbar-brand-img" />
-->
Photo Reality Cut
</a>
<!-- Toggler -->
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarCollapse" aria-controls="navbarCollapse" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<!-- Collapse -->
<div class="collapse navbar-collapse" id="navbarCollapse">
<!-- Toggler -->
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarCollapse" aria-controls="navbarCollapse" aria-expanded="false" aria-label="Toggle navigation">
<i class="fe fe-x"></i>
</button>
<!-- Navigation -->
<ul class="navbar-nav ms-auto">
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" id="navbarPages" data-bs-toggle="dropdown" href="#" aria-haspopup="true" aria-expanded="false">
Photo
</a>
<div class="dropdown-menu dropdown-menu-lg" aria-labelledby="navbarPages">
<div class="row gx-0">
<div class="col-6">
<div class="row gx-0">
<div class="col-12 col-lg-6">
<!-- Heading -->
<h6 class="dropdown-header">
Photo
</h6>
<a class="dropdown-item" href="./photocurves.html">
Photo Curves
</a>
<a class="dropdown-item" href="./photoshaders.html">
Photo Shaders
</a>
<a class="dropdown-item mb-5" href="./photodraw.html">
Photo Draw
</a>
</div>
</div>
</div>
<div class="col-6">
<div class="row gx-0">
<div class="col-12 col-lg-6">
<h6 class="dropdown-header">
Digital Compositing
</h6>
<a class="dropdown-item mb-5" href="./digitalcompositing.html">
100 Days of Photo Editing
</a>
</div>
</div>
</div>
</div>
</div> <!-- / .row -->
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" id="navbarPages" data-bs-toggle="dropdown" href="#" aria-haspopup="true" aria-expanded="false">
Video
</a>
<div class="dropdown-menu dropdown-menu-lg" aria-labelledby="navbarPages">
<div class="row gx-0">
<div class="col-6">
<div class="row gx-0">
<div class="col-12 col-lg-6">
<h6 class="dropdown-header">
Video Editing
</h6>
<a class="dropdown-item" href="./videoeditorsdk.html">
Video Editor
</a>
<a class="dropdown-item" href="./spatialvideos.html">
Spatial Videos
</a>
<a class="dropdown-item" href="./spatialcatvideos.html">
Spatial Cat Videos
</a>
<a class="dropdown-item mb-5" href="./spatialanimalvideos.html">
Spatial Animal Videos
</a>
</div>
</div>
</div>
<div class="col-6">
<div class="row gx-0">
<div class="col-12 col-lg-6">
<!-- Heading -->
<h6 class="dropdown-header">
Artificial Intelligence
</h6>
<a class="dropdown-item" href="./stablediffusioncatanimator.html">
Stable Diffusion Cat Animator
</a>
</div>
</div>
</div>
</div>
</div> <!-- / .row -->
</li>
<!--
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" id="navbarDocumentation" data-bs-toggle="dropdown" href="#" aria-haspopup="true" aria-expanded="false">
Video
</a>
<div class="dropdown-menu dropdown-menu-md" aria-labelledby="navbarDocumentation">
<div class="list-group list-group-flush">
<a class="list-group-item" href="./videoeditorsdk.html">
<div class="icon icon-sm text-primary">
<svg width="24" height="24" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><g fill="none" fill-rule="evenodd"><path d="M0 0h24v24H0z"></path><path fill="#335EEA" opacity=".3" d="M6 4h12l2 2.5H4z"></path><path d="M5 6h14a2 2 0 012 2v9a2 2 0 01-2 2h-3c-1 0-1.5-2-4-2s-2.5 2-4 2H5a2 2 0 01-2-2V8a2 2 0 012-2zm3 8a2 2 0 100-4 2 2 0 000 4zm8 0a2 2 0 100-4 2 2 0 000 4z" fill="#335EEA"></path></g></svg>
</div>
<div class="ms-4">
<h6 class="fw-bold text-uppercase text-primary mb-0">
Video Editor
</h6>
<p class="fs-sm text-gray-700 mb-0">
Trim, Cut, Composite, and Add
</p>
</div>
</a>
</div>
</div>
</li>
-->
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" id="navbarDocumentation" data-bs-toggle="dropdown" href="#" aria-haspopup="true" aria-expanded="false">
Augmented Reality
</a>
<div class="dropdown-menu dropdown-menu-lg" aria-labelledby="navbarPages">
<div class="row gx-0">
<div class="col-6">
<div class="row gx-0">
<div class="col-12 col-lg-6">
<h6 class="dropdown-header">
Filmmaking AR Props
</h6>
<a class="dropdown-item mb-5" href="./publicdomainusdz.html">
Public Domain USDZ
</a>
<a class="dropdown-item mb-5" href="./spatialcats.html">
Spatial Cats USDZ
</a>
</div>
</div>
</div>
<div class="col-6">
<div class="row gx-0">
<div class="col-12 col-lg-6">
<!-- Heading -->
<h6 class="dropdown-header">
Augmented Reality
</h6>
<a class="dropdown-item" href="./realitykit_terrainshader_surfaceshader_geometrymodifier.html">
RealityKit Terrain Shader
</a>
</div>
</div>
</div>
<!--
<div class="dropdown-menu dropdown-menu-md" aria-labelledby="navbarDocumentation">
<div class="list-group list-group-flush">
<a class="list-group-item" href="./realitykit_terrainshader_surfaceshader_geometrymodifier.html">
<div class="icon icon-sm text-primary">
<svg width="24" height="24" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><g fill="none" fill-rule="evenodd"><path d="M0 0h24v24H0z"></path><path fill="#335EEA" opacity=".3" d="M6 4h12l2 2.5H4z"></path><path d="M5 6h14a2 2 0 012 2v9a2 2 0 01-2 2h-3c-1 0-1.5-2-4-2s-2.5 2-4 2H5a2 2 0 01-2-2V8a2 2 0 012-2zm3 8a2 2 0 100-4 2 2 0 000 4zm8 0a2 2 0 100-4 2 2 0 000 4z" fill="#335EEA"></path></g></svg>
</div>
<div class="ms-4">
<h6 class="fw-bold text-uppercase text-primary mb-0">
Spatial Computing
</h6>
<p class="fs-sm text-gray-700 mb-0">
RealityKit Terrain Shader
</p>
</div>
</a>
</div>
</div>
-->
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" id="navbarDocumentation" data-bs-toggle="dropdown" href="#" aria-haspopup="true" aria-expanded="false">
Filters & Effects
</a>
<div class="dropdown-menu dropdown-menu-md" aria-labelledby="navbarDocumentation">
<div class="list-group list-group-flush">
<a class="list-group-item" href="./photofilters.html">
<div class="icon icon-sm text-primary">
<svg width="24" height="24" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><g fill="none" fill-rule="evenodd"><path d="M0 0h24v24H0z"/><rect fill="#335EEA" x="4" y="4" width="7" height="7" rx="1.5"/><path d="M5.5 13h4a1.5 1.5 0 011.5 1.5v4A1.5 1.5 0 019.5 20h-4A1.5 1.5 0 014 18.5v-4A1.5 1.5 0 015.5 13zm9-9h4A1.5 1.5 0 0120 5.5v4a1.5 1.5 0 01-1.5 1.5h-4A1.5 1.5 0 0113 9.5v-4A1.5 1.5 0 0114.5 4zm0 9h4a1.5 1.5 0 011.5 1.5v4a1.5 1.5 0 01-1.5 1.5h-4a1.5 1.5 0 01-1.5-1.5v-4a1.5 1.5 0 011.5-1.5z" fill="#335EEA" opacity=".3"/></g></svg> </div>
<!-- Content -->
<div class="ms-4">
<!-- Heading -->
<h6 class="fw-bold text-uppercase text-primary mb-0">
Photo Filters
</h6>
<!-- Text -->
<p class="fs-sm text-gray-700 mb-0">
Full list of Filters
</p>
</div>
</a>
<a class="list-group-item" href="./photoeffects.html">
<!-- Icon -->
<div class="icon icon-sm text-primary">
<svg width="24" height="24" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><g fill="none" fill-rule="evenodd"><path d="M0 0h24v24H0z"></path><path d="M11 10.417V4a1 1 0 012 0v6.317l5.385-3.109a1 1 0 011 1.732l-5.47 3.16 5.385 3.108a1 1 0 01-1 1.732L13 13.88V20a1 1 0 01-2 0v-6.218L5.529 16.94a1 1 0 11-1-1.732L9.914 12.1 4.444 8.94a1 1 0 011-1.732L11 10.417z" fill="#335EEA" opacity=".3"></path><path d="M7.858 9.78a1 1 0 01-.666 1.348l-1.931.517a1 1 0 01-.518-1.932l1.932-.517a1.01 1.01 0 01.128-.026 1.01 1.01 0 01-.042-.123l-.518-1.932a1 1 0 111.932-.517l.518 1.931a1 1 0 01-.835 1.25zm9.168 5.293c.016.04.03.08.041.123l.518 1.931a1 1 0 01-1.932.518l-.517-1.932a1 1 0 01.834-1.25 1 1 0 01.666-1.348l1.931-.517a1 1 0 11.518 1.931l-1.932.518a1.01 1.01 0 01-.127.026zM15.97 9.78a1 1 0 01-.834-1.25l.517-1.932a1 1 0 011.932.517l-.518 1.932a1.01 1.01 0 01-.041.123c.042.006.085.014.127.026l1.932.517a1 1 0 01-.518 1.932l-1.931-.518a1 1 0 01-.666-1.347zm-9.167 5.293a1.01 1.01 0 01-.128-.026l-1.932-.518a1 1 0 11.518-1.931l1.932.517a1 1 0 01.665 1.348 1 1 0 01.834 1.25l-.517 1.932a1 1 0 11-1.932-.518l.518-1.931a1.01 1.01 0 01.042-.123zm5.111-7.635a1 1 0 01-1.5.098L9 6.12a1 1 0 111.414-1.414l1.414 1.414c.031.031.06.064.086.098A1.01 1.01 0 0112 6.12l1.414-1.414a1 1 0 111.414 1.414l-1.414 1.415a1 1 0 01-1.5-.098zm0 10.586a1.01 1.01 0 01-.086.097l-1.414 1.415A1 1 0 019 18.12l1.414-1.414a1 1 0 011.5.098 1 1 0 011.5-.098l1.414 1.414a1 1 0 01-1.414 1.415L12 18.12a1.01 1.01 0 01-.086-.097z" fill="#335EEA" opacity=".3"></path></g></svg>
</div>
<div class="ms-4">
<!-- Heading -->
<h6 class="fw-bold text-uppercase text-primary mb-0">
Photo Effects
</h6>
<!-- Text -->
<p class="fs-sm text-gray-700 mb-0">
Full list of Shader Effects
</p>
</div>
</a>
</div>
</div>
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" id="navbarPages" data-bs-toggle="dropdown" href="#" aria-haspopup="true" aria-expanded="false">
Pipeline
</a>
<div class="dropdown-menu dropdown-menu-lg" aria-labelledby="navbarPages">
<div class="row gx-0">
<div class="col-6">
<div class="row gx-0">
<div class="col-12 col-lg-6">
<!--
<h6 class="dropdown-header">
Digital Compositing
</h6>
<a class="dropdown-item mb-5" href="./digitalcompositing.html">
Open ^Graphics Pipeline
</a>
-->
<h6 class="dropdown-header">
Open ^Graphics Pipeline
</h6>
<a class="dropdown-item mb-5" href="./Reduce-Cognitive-Overload-In-Graphics-Software.pdf">
Reduce Cognitive Overload (PDF)
</a>
<h6 class="dropdown-header">
Download
</h6>
<!-- List -->
<a class="dropdown-item" href="./download.html">
xrOS, MacOS, or iOS
</a>
</div>
</div>
</div>
</div>
</div> <!-- / .row -->
</li>
</ul>
<a class="navbar-btn btn btn-sm btn-info lift ms-auto" href="./gallery.html">
Gallery
</a>
</div>
</div>
</nav>
<section class="mt-n11 pt-12 pb-8 pt-md-14 pb-md-11 bg-black bg-pattern-2">
<div class="container text-white">
<div class="row justify-content-center">
<div class="col-12 col-md-10 col-lg-9 col-xl-8">
<h1 class="display-4">
RealityKit Terrain Shader
</h1>
<p>
This project shows you how to use Fractal Brownian Motion (fBM) to generate an Augmented Reality (AR) Terrain using iOS RealityKit Custom Materials. RealityKit Custom Material includes a surface shader and geometry modifier, also known as fragment and vertex shaders in OpenGL.
</p>
<figure class="figure mb-7">
<img class="figure-img img-fluid rounded lift lift-lg" src="ar/realitykitshaders/realitykit_terrain_shader.gif" alt="RealityKit Terrain Shader">
</figure>
<!--
<h3 class="fw-bold">
Quick Summary
</h3>
<p>
In short it involves building a Mesh for the Terrain, generating a height map at each uv position of the mesh by offsetting the height-map in the x and y direction, and finally elevating the plane vertically randomly at various vertices to produce an uneven surface. The random elevation levels generated for this terrain is produced directly using a Fractal Brownian motion (fBM) function, fBMheightmapx, and a geometryModifier.
</p>
<p>
The surface shader assign colors to the heightmap according to different coloring schemes. In this case it uses a red-brownish color schem for the terrain. The set of colors (palette) used can make the generated effect change drammatically. e.g using a yellow to red spectrum can produces a fire-like effect, using a blue and white palette will produces cloud-like effect </p>
<p>
-->
<p>
<a href="augmentedreality/RealityKitTerrainShader.zip" class="btn btn-info lift" rel="nofollow">
RealityKitTerrainShader.zip<i class="fe fe-arrow-right ms-2"></i>
</a>
</p>
<p> </p>
<h2 class="fw-bold">
Fractal Brownian Motion (fBM)
</h2>
<p>
fBM (Fractal Brownian Motion) is used extensively in computer graphics to model natural phenomenons and textures such as terrain , coastline, fire, smoke, clouds, water flow, water waves , rapids, dinosaur skin, wrinkles etc. It is quite amazing to note how such a simple random function can be used in so many areas to emulate the various forms of nature.
</p>
<p>
This project shows how you can use the fBM function to very simply generate a terrain using iOS RealityKit custom materials (which includes the surface shader and geometry modifier, also known as fragment and vertex shaders in OpenGL)
</p>
<h3 class="fw-bold">
Parameters
</h3>
<p>
The form of fBM function we are using has mainly the parameters
</p>
<ul class="list-unstyled mb-7">
<li class="d-flex">
<div class="badge badge-rounded-circle bg-success-soft mt-1 me-4">
<i class="fe fe-check"></i>
</div>
<p>
uv - texture coordinates (a value of [0,0] to [1,1], representing the four corners of an image we will like to generate)
</p>
</li>
<li class="d-flex">
<div class="badge badge-rounded-circle bg-success-soft mt-1 me-4">
<i class="fe fe-check"></i>
</div>
</p>
time - in seconds (this value will animate the fBM generate image with respect to time)
</p>
</li>
</ul>
<h3 class="fw-bold">
Properties of the fBM
</h3>
<p>
A fBM can return a single value (one dimensional value) or multiple values (e.g a 3 dimensional vector). You can view a fBM as a random number generator that depends on the current pixel position of the image. However, it is not completely random because the function varies quite smoothly with respect to its neighbours.
</p>
<p>
So if the user pass in uv-coordinate values of (0.10,0.10) and (0.11,0.10) the returned value should be quite near to each other. For a 3 dimensional vector , the value of each component will be quite similar to its neighbour’s.
</p>
<p>
Apart from varying smoothly with respect to space (i.e position or uv-coordinates), the fBM also varies quite smoothly with respect to time. So a returned value at time = 0.10 sec will be quite similar to a value at time = 0.11 sec. This gives it the property of having a smooth animation w.r.t. time.
</p>
<p>
Furthermore, the continuity of many implementations fBM is not restricted to just continuity of values, but also the continuity of its gradient as well as the continuity of (radius of) curvature. This make the fBM function very ‘smooth’ and thus very suitable for artistic renderings.
</p>
<p>
A fBM has fractal like properties. If you zoom in to small area, you will be able to notice similar patterns when the image has not been zoomed in.
</p>
<h2 class="fw-bold">
Generating the Terrain
</h2>
<p>
This article is mainly about using fBM to generate a terrain with RealityKit 2. The language is in Swift and Metal.
</p>
<ol>
<li>
<p>
The steps involved is to first generate a 2D plane with 64x64 cells.
</p>
<p>
<div style="border:1px solid gray">
<pre style="overflow:auto; word-wrap:normal; white-space:pre;font-family: monospace;font-size:12px;margin-left:10px;margin-right:10px">
let plane = buildMesh(numCells: [64, 64], cellSize: 0.01)
</pre>
</div>
</p>
</li>
<li>
<p>
Assign the plane with a Custom Material. This custom material will automatically associate a Surface Shader function and a Geometry Modifier function to the plane
</p>
<p>
<div style="border:1px solid gray">
<pre style="overflow:auto; word-wrap:normal; white-space:pre;font-family: monospace;font-size:12px;margin-left:10px;margin-right:10px">
let geometryModifier = CustomMaterial.GeometryModifier(named: "geometryModifier",
in: library)
let surfaceShader = CustomMaterial.SurfaceShader(named: "surfaceShader",
in: library)
let customMaterials = try! CustomMaterial(from: baseMaterial,
surfaceShader : surfaceShader,
geometryModifier: geometryModifier)
let plane = buildMesh(numCells: [64, 64], cellSize: 0.01)
landEntity = ModelEntity(mesh: plane, materials : [customMaterials])
</pre>
</div>
</p>
</li>
<li>
<p>
The geometryModifier and surfaceShader is currently empty functions written in the Metal language. Running the app shows an empty plane.
</p>
<figure class="figure mb-7">
<img class="figure-img img-fluid rounded lift lift-lg" src="ar/realitykitshaders/terrrainplane.png" alt="Terrain Plane">
</figure>
</li>
<li>
<p>
Add normals to the plane using the computeNormal function. At this time, we will like to introduce the fBMheightmapx function. This is the main fBM function used for generating a height map for the terrain. In normal situations, we will simply render this height map and we will get a terrain. However, for RealityKit surface shader, it is easier to add details (roughness) to the terrain using normals. This is achieved by computing normals of the height map with the computeNormal function in the surfaceShader function.
</p>
<p>
<div style="border:1px solid gray">
<pre style="overflow:auto; word-wrap:normal; white-space:pre;font-family: monospace;font-size:12px;margin-left:10px;margin-right:10px">
//fBMheightmapx - generates a height map at each position (uv position) of the shader.
//This height-field has a normal at each uv coordinate position pointing away perpendicularly from the surface.
//The normals is computed by offsetting the height-map a little in the x-direction and a little in the y-direction to compute the difference.
//The normals computed thus does not necessary has magnitude / length of 1 (and need to be normalised to length 1).
float3 normal = computeNormal(uvscale,time*timefactor/divfactor ,warpType,animVelocity,angle,screensize);
normal = normalize(normal);
params.surface().set_normal(normal); //set tangent space normal
</pre>
</div>
</p>
<p>
With the normals set, we now run the app and we get something that look like below.
</p>
<figure class="figure mb-7">
<img class="figure-img img-fluid rounded lift lift-lg" src="ar/realitykitshaders/terrain_surfaceshader.png" alt="Terrain Surface Shader">
</figure>
</li>
<li>
<p>
We now elevate the plane vertically at the various vertices of each cell randomly to produce a an uneven surface. The random values (elevation levels) generated for this terrain is produced directly using the fBM function we mentioned earlier (fBMheightmapx). To elevate the plane, we have to use a geometry modifier (also known as vertex shader) to modify the position of each of these vertices. To do so, we define a geometryModifier function with the following code.
</p>
<p>
<div style="border:1px solid gray">
<pre style="overflow:auto; word-wrap:normal; white-space:pre;font-family: monospace;font-size:12px;margin-left:10px;margin-right:10px">
float4 val =
fBMheightmapx(uvscale,time ,warpType,animVelocity,angle);
//here , we are preparing the values for a displacement map
//so that only the y-value (height) is modified, but not the x and z value
//the value 0.063 is chosen by trial and error
float3 offset = float3(0.0,0.063*val.r,0.0);
//this function applies the fBM generated offset to the plane to make it uneven
params.geometry().set_model_position_offset(offset);
</pre>
</div>
</p>
<p>
We now get something that looks like below. The terrain is almost complete.
</p>
<figure class="figure mb-7">
<img class="figure-img img-fluid rounded lift lift-lg" src="ar/realitykitshaders/terrain_geometrymodifier.png" alt="Terrain Surface Shader">
</figure>
</li>
<li>
<p>
For the final step, wee proceed to color the terrain. The fBM function we mentioned earlier can do many things, apart from producing a height map (and its associated normals), we can also use it to color the terrain by mapping each height value with a color.
</p>
<p>
For our case, we use a modified extended version of the fBM function named fBMFlow. This function applies a color palette to the fBM generated height-map and retrieve the color at the current uv position. This color is then used to colorize the terrain.
</p>
<p>
<div style="border:1px solid gray">
<pre style="overflow:auto; word-wrap:normal; white-space:pre;font-family: monospace;font-size:12px;margin-left:10px;margin-right:10px">
float4 val = fBMFlow(uv, float2(2048,2048), float3(0,0,0), time*timefactor);
half3 color1 = half3(halfr,halfg,halfb);
params.surface().set_base_color(color1);
</pre>
</div>
</p>
<p>
With this done, we will get the terrain as shown below. (The image has been enhanced a little for better visibility).
</p>
<figure class="figure mb-7">
<img class="figure-img img-fluid rounded lift lift-lg" src="ar/realitykitshaders/terrain_shader.png" alt="Terrain Shader">
</figure>
</li>
<p>
<a href="https://github.com/Misfits-Rebels-Outcasts/RealityKit-Terrain-Shader" class="btn btn-info lift" rel="nofollow">
Github Project <i class="fe fe-arrow-right ms-2"></i>
</a>
</p>
</ol>
</div>
</div> <!-- / .row -->
</div> <!-- / .container -->
</section>
<!--
<section class="mt-n11 pt-12 pb-8 pt-md-14 pb-md-11 bg-black bg-pattern-2">
<section class="bg-gradient-dark-black py-8 py-md-11">
<div class="container">
<section class="pt-6 pt-md-8">
-->
<!--
<section class="pt-6 pt-md-8">
-->
<!-- BORDER -->
<div class="bg-black">
<div class="container border-top border-gray-900-50"></div>
</div>
<!-- FOOTER -->
<footer class="py-8 py-md-11 bg-black">
<div class="container">
<div class="row">
<div class="col-12 col-md-4 col-lg-3">
<!--
<img src="./assets/img/brand.svg" alt="..." class="footer-brand img-fluid mb-2">
<p class="text-gray-700 mb-2">
A better way to build.
</p>
-->
<!--
<ul class="list-unstyled list-inline list-social mb-6 mb-md-0">
<li class="list-inline-item list-social-item me-3">
<a href="#!" class="text-decoration-none">
<img src="./assets/img/icons/social/instagram.svg" class="list-social-icon" alt="...">
</a>
</li>
<li class="list-inline-item list-social-item me-3">
<a href="#!" class="text-decoration-none">
<img src="./assets/img/icons/social/facebook.svg" class="list-social-icon" alt="...">
</a>
</li>
<li class="list-inline-item list-social-item me-3">
<a href="#!" class="text-decoration-none">
<img src="./assets/img/icons/social/twitter.svg" class="list-social-icon" alt="...">
</a>
</li>
<li class="list-inline-item list-social-item">
<a href="#!" class="text-decoration-none">
<img src="./assets/img/icons/social/pinterest.svg" class="list-social-icon" alt="...">
</a>
</li>
</ul>
-->
</div>
<div class="col-6 col-md-4 col-lg-2">
</div>
<div class="col-6 col-md-4 col-lg-2">
</div>
<div class="col-6 col-md-4 offset-md-4 col-lg-2 offset-lg-0">
</div>
<div class="col-6 col-md-4 col-lg-2">
<!--
<h6 class="fw-bold text-uppercase text-gray-700">
Legal
</h6>
<ul class="list-unstyled text-muted mb-0">
<li class="mb-3">
<a href="#!" class="text-reset">
Documentation
</a>
</li>
<li class="mb-3">
<a href="#!" class="text-reset">
Changelog
</a>
</li>
<li>
<a href="#!" class="text-reset">
Pagebuilder
</a>
</li>
</ul>
-->
</div>
</div> <!-- / .row -->
</div> <!-- / .container -->
</footer>
<!-- JAVASCRIPT -->
<!-- Map JS -->
<script src='https://api.mapbox.com/mapbox-gl-js/v0.53.0/mapbox-gl.js'></script>
<!-- Vendor JS -->
<script src="./assets/js/vendor.bundle.js"></script>
<!-- Theme JS -->
<script src="./assets/js/theme.bundle.js"></script>
</body>
</html>