-
Notifications
You must be signed in to change notification settings - Fork 11
/
Part 2.html
746 lines (540 loc) · 76.4 KB
/
Part 2.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
<!DOCTYPE html>
<!-- saved from url=(0112)https://web.archive.org/web/20150318011823/http://alexstv.com:80/index.php/posts/unity-voxel-block-tutorial-pt-2 -->
<html lang="en"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><script async="" src="./Part 2_files/analytics.js.descarga"></script><script src="./Part 2_files/analytics.js(1).descarga" type="text/javascript"></script>
<script type="text/javascript">window.addEventListener('DOMContentLoaded',function(){var v=archive_analytics.values;v.service='wb';v.server_name='wwwb-app57.us.archive.org';v.server_ms=333;archive_analytics.send_pageview({});});</script>
<script type="text/javascript" src="./Part 2_files/ait-client-rewrite.js.descarga" charset="utf-8"></script>
<script type="text/javascript">
WB_wombat_Init('https://web.archive.org/web', '20150318011823', 'alexstv.com:80');
</script>
<script type="text/javascript" src="./Part 2_files/wbhack.js.descarga" charset="utf-8"></script>
<script type="text/javascript">
__wbhack.init('https://web.archive.org/web');
</script>
<link rel="stylesheet" type="text/css" href="./Part 2_files/banner-styles.css">
<link rel="stylesheet" type="text/css" href="./Part 2_files/iconochive.css">
<!-- End Wayback Rewrite JS Include -->
<title>AlexStv</title>
<meta name="description" content="Code and things">
<link rel="stylesheet" href="./Part 2_files/reset.css">
<link rel="stylesheet" href="./Part 2_files/style.css">
<link rel="alternate" type="application/rss+xml" title="RSS" href="https://web.archive.org/web/20150318011823/http://alexstv.com:80/index.php/feeds/rss">
<link rel="shortcut icon" href="https://web.archive.org/web/20150318011823im_/http://alexstv.com:80/themes/AnchoredGallery/img/favicon.png">
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<script>var base = '/themes/AnchoredGallery/';</script>
<script src="./Part 2_files/zepto.js.descarga"></script>
<script src="./Part 2_files/main.js.descarga"></script>
<link href="./Part 2_files/css" rel="stylesheet" type="text/css">
<link href="./Part 2_files/css(1)" rel="stylesheet" type="text/css">
<link type="text/css" rel="Stylesheet" href="./Part 2_files/shCoreRDark.css">
<meta name="viewport" content="width=600">
<meta name="generator" content="Anchor CMS">
<meta property="og:title" content="AlexStv">
<meta property="og:type" content="website">
<meta property="og:url" content="posts/unity-voxel-block-tutorial-pt-2">
<meta property="og:image" content="/web/20150318011823im_/http://alexstv.com:80/themes/AnchoredGallery/img/og_image.gif">
<meta property="og:site_name" content="AlexStv">
<meta property="og:description" content="Code and things">
<!-- Include required JS files -->
<script type="text/javascript" src="./Part 2_files/shCore.js.descarga"></script>
<script type="text/javascript" src="./Part 2_files/shBrushCSharp.js.descarga"></script>
<script type="text/javascript" src="./Part 2_files/jquery.min.js.descarga"></script>
<!-- Include jQuery Syntax Highlighter -->
<script type="text/javascript" src="./Part 2_files/jquery.syntaxhighlighter.min.js.descarga"></script>
<!-- Initialise jQuery Syntax Highlighter -->
<script type="text/javascript">$.SyntaxHighlighter.init();</script>
<!-- Initialise jQuery Syntax Highlighter -->
<script type="text/javascript">
$.SyntaxHighlighter.init({
'alternateLines':true
});
</script>
<script type="text/javascript">
SyntaxHighlighter.all()
</script><script type="text/javascript" src="./Part 2_files/prettify.min.js.descarga" id="script-httpswebarchiveorgweb20150323181528httpbaluptongithubcomjquerysyntaxhighlighterprettifyprettifyminjs"></script><link type="text/css" rel="stylesheet" media="screen" href="./Part 2_files/prettify.min.css" id="stylesheet-httpswebarchiveorgweb20150323181528httpbaluptongithubcomjquerysyntaxhighlighterprettifyprettifymincss"><link type="text/css" rel="stylesheet" media="screen" href="./Part 2_files/style.min.css" id="stylesheet-httpswebarchiveorgweb20150323181528httpbaluptongithubcomjquerysyntaxhighlighterstylesstylemincss"><link type="text/css" rel="stylesheet" media="screen" href="./Part 2_files/theme-balupton.min.css" id="stylesheet-httpswebarchiveorgweb20150323181528httpbaluptongithubcomjquerysyntaxhighlighterstylesthemebaluptonmincss"></head>
<body class="posts home"><div id="wm-ipp" lang="en" style="display: block; direction: ltr;">
<div style="position:fixed;left:0;top:0;right:0;">
<div id="wm-ipp-inside">
<div style="position:relative;">
<div id="wm-logo" style="float:left;width:130px;padding-top:10px;">
<a href="https://web.archive.org/web/" title="Wayback Machine home page"><img src="./Part 2_files/wayback-toolbar-logo.png" alt="Wayback Machine" width="110" height="39" border="0"></a>
</div>
<div class="r" style="float:right;">
<div id="wm-btns" style="text-align:right;height:25px;">
<div id="wm-save-snapshot-success">success</div>
<div id="wm-save-snapshot-fail">fail</div>
<a href="https://web.archive.org/web/20150318011823/http://alexstv.com:80/index.php/posts/unity-voxel-block-tutorial-pt-2#" onclick="__wm.saveSnapshot('http://alexstv.com:80/index.php/posts/unity-voxel-block-tutorial-pt-2', '20150318011823')" title="Share via My Web Archive" id="wm-save-snapshot-open" style="display: none;">
<span class="iconochive-web"></span>
</a>
<a href="https://archive.org/account/login.php" title="Sign In" id="wm-sign-in" style="display: inline-block;">
<span class="iconochive-person"></span>
</a>
<span id="wm-save-snapshot-in-progress" class="iconochive-web" style="display: none;"></span>
<a href="http://faq.web.archive.org/" title="Get some help using the Wayback Machine" style="top:-6px;"><span class="iconochive-question" style="color:rgb(87,186,244);font-size:160%;"></span></a>
<a id="wm-tb-close" href="https://web.archive.org/web/20150318011823/http://alexstv.com:80/index.php/posts/unity-voxel-block-tutorial-pt-2#close" onclick="__wm.h(event);return false;" style="top:-2px;" title="Close the toolbar"><span class="iconochive-remove-circle" style="color:#888888;font-size:240%;"></span></a>
</div>
<div id="wm-share" style="text-align:right;">
<a href="https://web.archive.org/web/20150318011823/http://alexstv.com:80/index.php/posts/unity-voxel-block-tutorial-pt-2#" onclick="window.open('https://www.facebook.com/sharer/sharer.php?u=https://web.archive.org/web/20150318011823/http://alexstv.com:80/index.php/posts/unity-voxel-block-tutorial-pt-2', '', 'height=400,width=600'); return false;" title="Share on Facebook" style="margin-right:5px;" target="_blank"><span class="iconochive-facebook" style="color:#3b5998;font-size:160%;"></span></a>
<a href="https://web.archive.org/web/20150318011823/http://alexstv.com:80/index.php/posts/unity-voxel-block-tutorial-pt-2#" onclick="window.open('https://twitter.com/intent/tweet?text=https://web.archive.org/web/20150318011823/http://alexstv.com:80/index.php/posts/unity-voxel-block-tutorial-pt-2&via=internetarchive', '', 'height=400,width=600'); return false;" title="Share on Twitter" style="margin-right:5px;" target="_blank"><span class="iconochive-twitter" style="color:#1dcaff;font-size:160%;"></span></a>
</div>
</div>
<table class="c" style="">
<tbody>
<tr>
<td class="u" colspan="2">
<form target="_top" method="get" action="https://web.archive.org/web/submit" name="wmtb" id="wmtb"><input type="text" name="url" id="wmtbURL" value="http://alexstv.com:80/index.php/posts/unity-voxel-block-tutorial-pt-2" onfocus="this.focus();this.select();" autocomplete="off"><input type="hidden" name="type" value="replay"><input type="hidden" name="date" value="20150318011823"><input type="submit" value="Go"></form>
</td>
<td class="n" rowspan="2" style="width:110px;">
<table>
<tbody>
<!-- NEXT/PREV MONTH NAV AND MONTH INDICATOR -->
<tr class="m">
<td class="b" nowrap="nowrap"><a href="https://web.archive.org/web/20150214050336/http://alexstv.com:80/index.php/posts/unity-voxel-block-tutorial-pt-2" title="14 Feb 2015"><strong>Feb</strong></a></td>
<td class="c" id="displayMonthEl" title="You are here: 01:18:23 Mar 18, 2015">MAR</td>
<td class="f" nowrap="nowrap"><a href="https://web.archive.org/web/20150423074146/http://alexstv.com:80/index.php/posts/unity-voxel-block-tutorial-pt-2" title="23 Apr 2015"><strong>Apr</strong></a></td>
</tr>
<!-- NEXT/PREV CAPTURE NAV AND DAY OF MONTH INDICATOR -->
<tr class="d">
<td class="b" nowrap="nowrap"><a href="https://web.archive.org/web/20150214050336/http://alexstv.com:80/index.php/posts/unity-voxel-block-tutorial-pt-2" title="05:03:36 Feb 14, 2015"><img src="./Part 2_files/wm_tb_prv_on.png" alt="Previous capture" width="14" height="16" border="0"></a></td>
<td class="c" id="displayDayEl" style="width:34px;font-size:24px;white-space:nowrap;" title="You are here: 01:18:23 Mar 18, 2015">18</td>
<td class="f" nowrap="nowrap"><a href="https://web.archive.org/web/20150423074146/http://alexstv.com:80/index.php/posts/unity-voxel-block-tutorial-pt-2" title="07:41:46 Apr 23, 2015"><img src="./Part 2_files/wm_tb_nxt_on.png" alt="Next capture" width="14" height="16" border="0"></a></td>
</tr>
<!-- NEXT/PREV YEAR NAV AND YEAR INDICATOR -->
<tr class="y">
<td class="b" nowrap="nowrap">2014</td>
<td class="c" id="displayYearEl" title="You are here: 01:18:23 Mar 18, 2015">2015</td>
<td class="f" nowrap="nowrap"><a href="https://web.archive.org/web/20160628070550/http://alexstv.com:80/index.php/posts/unity-voxel-block-tutorial-pt-2" title="28 Jun 2016"><strong>2016</strong></a></td>
</tr>
</tbody>
</table>
</td>
</tr>
<tr>
<td class="s">
<div id="wm-nav-captures"><a class="t" href="https://web.archive.org/web/*/http://alexstv.com:80/index.php/posts/unity-voxel-block-tutorial-pt-2" title="See a list of every capture for this URL">26 captures</a><div class="r" title="Timespan for captures of this URL">31 Jan 2015 - 22 Sep 2017</div></div>
</td>
<td class="k">
<a href="https://web.archive.org/web/20150318011823/http://alexstv.com:80/index.php/posts/unity-voxel-block-tutorial-pt-2" id="wm-graph-anchor">
<div id="wm-ipp-sparkline" title="Explore captures for this URL" style="position: relative">
<canvas id="wm-sparkline-canvas" width="575" height="27" border="0"></canvas>
<div class="yt" style="display: none; width: 25px; height: 27px;"></div><div class="mt" style="display: none; width: 2px; height: 27px;"></div></div>
</a>
</td>
</tr>
</tbody>
</table>
<div style="position:absolute;bottom:0;right:2px;text-align:right;">
<a id="wm-expand" class="wm-btn wm-closed" href="https://web.archive.org/web/20150318011823/http://alexstv.com:80/index.php/posts/unity-voxel-block-tutorial-pt-2#expand" onclick="__wm.ex(event);return false;"><span id="wm-expand-icon" class="iconochive-down-solid"></span> <span style="font-size:80%">About this capture</span></a>
</div>
</div>
<div id="wm-capinfo" style="border-top:1px solid #777;display:none; overflow: hidden">
<div style="background-color:#666;color:#fff;font-weight:bold;text-align:center">COLLECTED BY</div>
<div style="padding:3px;position:relative" id="wm-collected-by-content">
<div style="display:inline-block;vertical-align:top;width:50%;">
<span class="c-logo" style="background-image:url(https://archive.org/services/img/alexacrawls);"></span>
Organization: <a style="color:#33f;" href="https://archive.org/details/alexacrawls" target="_new"><span class="wm-title">Alexa Crawls</span></a>
<div style="max-height:75px;overflow:hidden;position:relative;">
<div style="position:absolute;top:0;left:0;width:100%;height:75px;background:linear-gradient(to bottom,rgba(255,255,255,0) 0%,rgba(255,255,255,0) 90%,rgba(255,255,255,255) 100%);"></div>
Starting in 1996, <a href="http://www.alexa.com/">Alexa Internet</a> has been donating their crawl data to the Internet Archive. Flowing in every day, these data are added to the <a href="http://web.archive.org/">Wayback Machine</a> after an embargo period.
</div>
</div>
<div style="display:inline-block;vertical-align:top;width:49%;">
<span class="c-logo" style="background-image:url(https://archive.org/services/img/alexacrawls)"></span>
<div>Collection: <a style="color:#33f;" href="https://archive.org/details/alexacrawls" target="_new"><span class="wm-title">Alexa Crawls</span></a></div>
<div style="max-height:75px;overflow:hidden;position:relative;">
<div style="position:absolute;top:0;left:0;width:100%;height:75px;background:linear-gradient(to bottom,rgba(255,255,255,0) 0%,rgba(255,255,255,0) 90%,rgba(255,255,255,255) 100%);"></div>
Starting in 1996, <a href="http://www.alexa.com/">Alexa Internet</a> has been donating their crawl data to the Internet Archive. Flowing in every day, these data are added to the <a href="http://web.archive.org/">Wayback Machine</a> after an embargo period.
</div>
</div>
</div>
<div style="background-color:#666;color:#fff;font-weight:bold;text-align:center" title="Timestamps for the elements of this page">TIMESTAMPS</div>
<div>
<div id="wm-capresources" style="margin:0 5px 5px 5px;max-height:250px;overflow-y:scroll !important"></div>
<div id="wm-capresources-loading" style="text-align:left;margin:0 20px 5px 5px;display:none"><img src="./Part 2_files/loading.gif" alt="loading"></div>
</div>
</div></div></div></div><!-- BEGIN WAYBACK TOOLBAR INSERT -->
<script type="text/javascript" src="./Part 2_files/timestamp.js.descarga" charset="utf-8"></script>
<script type="text/javascript" src="./Part 2_files/graph-calc.js.descarga" charset="utf-8"></script>
<script type="text/javascript" src="./Part 2_files/auto-complete.js.descarga" charset="utf-8"></script>
<script type="text/javascript" src="./Part 2_files/toolbar.js.descarga" charset="utf-8"></script>
<style type="text/css">
body {
margin-top:0 !important;
padding-top:0 !important;
/*min-width:800px !important;*/
}
.wb-autocomplete-suggestions {
text-align: left; cursor: default; border: 1px solid #ccc; border-top: 0; background: #fff; box-shadow: -1px 1px 3px rgba(0,0,0,.1);
position: absolute; display: none; z-index: 2147483647; max-height: 254px; overflow: hidden; overflow-y: auto; box-sizing: border-box;
}
.wb-autocomplete-suggestion { position: relative; padding: 0 .6em; line-height: 23px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-size: 1.02em; color: #333; }
.wb-autocomplete-suggestion b { font-weight: bold; }
.wb-autocomplete-suggestion.selected { background: #f0f0f0; }
</style>
<script type="text/javascript">
__wm.bt(575,27,25,2,"web","http://alexstv.com:80/index.php/posts/unity-voxel-block-tutorial-pt-2","2015-03-18",1996);
</script><div class="wb-autocomplete-suggestions "></div>
<!-- END WAYBACK TOOLBAR INSERT -->
<div style="background-color:#4774A5; overflow:hidden; background-position:center; background-image:url('https://web.archive.org/web/20150318011823im_/http://4.bp.blogspot.com/-9w7pH08mSaM/UGbaDEz3kOI/AAAAAAAADeI/9XWFFsdLdwg/s1600/body.png');background-repeat:repeat;">
<article class="wrapper">
<a href="https://web.archive.org/web/20150318011823/http://alexstv.com:80/index.php/">
<header>
<titletext>AlexStv</titletext>
</header>
</a>
<div class="pagewrapper" style="width:100%">
<a href="https://web.archive.org/web/20150318011823/http://alexstv.com:80/index.php/category/general" title="General">
<div class="page" style="width:33.333333333333%">
<stamptext>General (2)</stamptext>
</div>
</a>
<a href="https://web.archive.org/web/20150318011823/http://alexstv.com:80/index.php/category/voxels" title="Voxels">
<div class="page" style="width:33.333333333333%">
<stamptext>Voxels (7)</stamptext>
</div>
</a>
<a href="https://web.archive.org/web/20150318011823/http://alexstv.com:80/index.php/category/grapple" title="Grapple">
<div class="page" style="width:33.333333333333%">
<stamptext>Grapple (3)</stamptext>
</div>
</a>
</div>
</article>
</div>
<div class="bg2">
<article class="wrapper">
<div class="post">
<div id="filler">
<div class="description" style="width:100%; padding-bottom:30px; background-color:#3C5C89;">
<h1 style="color:#fff;">Unity voxel block tutorial pt. 2</h1>
</div>
</div>
<div id="filler">
<div class="description">
<bodytext>
<p>Welcome back! Last time we made functions to set up the mesh with vertices and triangles. In this part we'll add textures and collision meshes to our blocks. We'll start with textures, first of all we need a tile sheet to use for textures. Since we have many blocks on the same mesh the mesh needs one image with all the textures.</p>
<p>I'll be using this tilesheet in the tutorial. Feel free to use it as well non-comercially.
</p></bodytext></div> </div> <div style="width:100%;float:left;"><div class="centerimg"><img src="./Part 2_files/tiles.png"></div></div><div id="filler"> <div class="description"> <bodytext>
You can use your own if you want or save this image to the project's assets folder.<p></p>
<p>Open up the import settings for this texture because we'll need to change them a bit, Set the texture type to advanced, disable all the options down to wrap mode, set that to repeat and set filter mode to point. This will stop it from filtering the texture and blurring the lines between pixels which causes bleeding between tiles where you can see the edge of the tile next to the one a block is supposed to be using. Then set the max size large enough to accommodate your texture and automatic compressed should work fine.</p>
<p>Take the chunk game object we created in the last part by putting the chunk script on an empty game object. Now add the tilesheet texture to the chunk by dragging the texture onto it (make sure it appears added in the inspector). Now the chunk will use this texture but we still need to assign uv coordinates for each face and apply them to the mesh.</p>
<p>Start with the <strong>block script</strong> where first of all we'll give it a function that can easily be overridden to change the texture.</p>
<p>We'll create a struct for tile positions first:
</p></bodytext></div> </div> <div class="codeblock"><div><div id="highlighter_921514" class="syntaxhighlighter csharp"><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td class="gutter"><div class="line number1 index0 alt2">1</div></td><td class="code"><div class="container"><div class="line number1 index0 alt2"><code class="csharp spaces"> </code><code class="csharp keyword">public</code> <code class="csharp keyword">struct</code> <code class="csharp plain">Tile { </code><code class="csharp keyword">public</code> <code class="csharp keyword">int</code> <code class="csharp plain">x; </code><code class="csharp keyword">public</code> <code class="csharp keyword">int</code> <code class="csharp plain">y;}</code></div></div></td></tr></tbody></table></div></div></div><div id="filler"> <div class="description" style="padding-top:0px"> <bodytext><p></p>
<p>And then a function to return the tile positions based on direction:</p>
<p> </p></bodytext></div> </div> <div class="codeblock"><div><div id="highlighter_161001" class="syntaxhighlighter csharp"><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td class="gutter"><div class="line number1 index0 alt2">1</div><div class="line number2 index1 alt1">2</div><div class="line number3 index2 alt2">3</div><div class="line number4 index3 alt1">4</div><div class="line number5 index4 alt2">5</div><div class="line number6 index5 alt1">6</div><div class="line number7 index6 alt2">7</div><div class="line number8 index7 alt1">8</div></td><td class="code"><div class="container"><div class="line number1 index0 alt2"><code class="csharp spaces"> </code><code class="csharp keyword">public</code> <code class="csharp keyword">virtual</code> <code class="csharp plain">Tile TexturePosition(Direction direction)</code></div><div class="line number2 index1 alt1"><code class="csharp spaces"> </code><code class="csharp plain">{</code></div><div class="line number3 index2 alt2"><code class="csharp spaces"> </code><code class="csharp plain">Tile tile = </code><code class="csharp keyword">new</code> <code class="csharp plain">Tile();</code></div><div class="line number4 index3 alt1"><code class="csharp spaces"> </code><code class="csharp plain">tile.x = 0;</code></div><div class="line number5 index4 alt2"><code class="csharp spaces"> </code><code class="csharp plain">tile.y = 0;</code></div><div class="line number6 index5 alt1"> </div><div class="line number7 index6 alt2"><code class="csharp spaces"> </code><code class="csharp keyword">return</code> <code class="csharp plain">tile;</code></div><div class="line number8 index7 alt1"><code class="csharp spaces"> </code><code class="csharp plain">}</code></div></div></td></tr></tbody></table></div></div></div><div id="filler"> <div class="description" style="padding-top:0px"> <bodytext><p></p>
<p>This function will let us use the direction of the face and return which tile to use. This function always returns 0,0 since it's the base class but later we'll create a block with more interesting texturing. First though we'll use the positions to create uv coordinates for the mesh with a new function and add a new variable:</p>
<p>variable:
</p></bodytext></div> </div> <div class="codeblock"><div><div id="highlighter_465670" class="syntaxhighlighter csharp"><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td class="gutter"><div class="line number1 index0 alt2">1</div></td><td class="code"><div class="container"><div class="line number1 index0 alt2"><code class="csharp spaces"> </code><code class="csharp keyword">const</code> <code class="csharp keyword">float</code> <code class="csharp plain">tileSize = 0.25f;</code></div></div></td></tr></tbody></table></div></div></div><div id="filler"> <div class="description" style="padding-top:0px"> <bodytext><p></p>
<p>function:
</p></bodytext></div> </div> <div class="codeblock"><div><div id="highlighter_859551" class="syntaxhighlighter csharp"><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td class="gutter"><div class="line number1 index0 alt2">1</div><div class="line number2 index1 alt1">2</div><div class="line number3 index2 alt2">3</div><div class="line number4 index3 alt1">4</div><div class="line number5 index4 alt2">5</div><div class="line number6 index5 alt1">6</div><div class="line number7 index6 alt2">7</div><div class="line number8 index7 alt1">8</div><div class="line number9 index8 alt2">9</div><div class="line number10 index9 alt1">10</div><div class="line number11 index10 alt2">11</div><div class="line number12 index11 alt1">12</div><div class="line number13 index12 alt2">13</div><div class="line number14 index13 alt1">14</div><div class="line number15 index14 alt2">15</div><div class="line number16 index15 alt1">16</div></td><td class="code"><div class="container"><div class="line number1 index0 alt2"><code class="csharp spaces"> </code><code class="csharp keyword">public</code> <code class="csharp keyword">virtual</code> <code class="csharp plain">Vector2[] FaceUVs(Direction direction)</code></div><div class="line number2 index1 alt1"><code class="csharp spaces"> </code><code class="csharp plain">{</code></div><div class="line number3 index2 alt2"><code class="csharp spaces"> </code><code class="csharp plain">Vector2[] UVs = </code><code class="csharp keyword">new</code> <code class="csharp plain">Vector2[4];</code></div><div class="line number4 index3 alt1"><code class="csharp spaces"> </code><code class="csharp plain">Tile tilePos = TexturePosition(direction);</code></div><div class="line number5 index4 alt2"> </div><div class="line number6 index5 alt1"><code class="csharp spaces"> </code><code class="csharp plain">UVs[0] = </code><code class="csharp keyword">new</code> <code class="csharp plain">Vector2(tileSize * tilePos.x + tileSize,</code></div><div class="line number7 index6 alt2"><code class="csharp spaces"> </code><code class="csharp plain">tileSize * tilePos.y);</code></div><div class="line number8 index7 alt1"><code class="csharp spaces"> </code><code class="csharp plain">UVs[1] = </code><code class="csharp keyword">new</code> <code class="csharp plain">Vector2(tileSize * tilePos.x + tileSize,</code></div><div class="line number9 index8 alt2"><code class="csharp spaces"> </code><code class="csharp plain">tileSize * tilePos.y + tileSize);</code></div><div class="line number10 index9 alt1"><code class="csharp spaces"> </code><code class="csharp plain">UVs[2] = </code><code class="csharp keyword">new</code> <code class="csharp plain">Vector2(tileSize * tilePos.x,</code></div><div class="line number11 index10 alt2"><code class="csharp spaces"> </code><code class="csharp plain">tileSize * tilePos.y + tileSize);</code></div><div class="line number12 index11 alt1"><code class="csharp spaces"> </code><code class="csharp plain">UVs[3] = </code><code class="csharp keyword">new</code> <code class="csharp plain">Vector2(tileSize * tilePos.x,</code></div><div class="line number13 index12 alt2"><code class="csharp spaces"> </code><code class="csharp plain">tileSize * tilePos.y);</code></div><div class="line number14 index13 alt1"> </div><div class="line number15 index14 alt2"><code class="csharp spaces"> </code><code class="csharp keyword">return</code> <code class="csharp plain">UVs;</code></div><div class="line number16 index15 alt1"><code class="csharp spaces"> </code><code class="csharp plain">}</code></div></div></td></tr></tbody></table></div></div></div><div id="filler"> <div class="description" style="padding-top:0px"> <bodytext><p></p>
<p>The variable tileSize is equal to 1 divided by the number of tiles per side, in the case of our example texture 1/4 (0.25). The function creates an array of vector2 and gets the tile position with the direction and populates the array based on the tile position. Every UV coordinate corresponds to a vertex and the triangle made of those vertices uses the corresponding texture coordinates.</p>
<p>Now we have to call the function for every face adding the resulting UV coordinates to the mesh data so for every FaceData function add the resulting uv array to the mesh data uv list:</p>
<p> </p></bodytext></div> </div> <div class="codeblock"><div><div id="highlighter_762672" class="syntaxhighlighter csharp"><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td class="gutter"><div class="line number1 index0 alt2">1</div><div class="line number2 index1 alt1">2</div><div class="line number3 index2 alt2">3</div><div class="line number4 index3 alt1">4</div><div class="line number5 index4 alt2">5</div><div class="line number6 index5 alt1">6</div><div class="line number7 index6 alt2">7</div><div class="line number8 index7 alt1">8</div><div class="line number9 index8 alt2">9</div><div class="line number10 index9 alt1">10</div><div class="line number11 index10 alt2">11</div><div class="line number12 index11 alt1">12</div><div class="line number13 index12 alt2">13</div><div class="line number14 index13 alt1">14</div><div class="line number15 index14 alt2">15</div></td><td class="code"><div class="container"><div class="line number1 index0 alt2"><code class="csharp spaces"> </code><code class="csharp keyword">protected</code> <code class="csharp keyword">virtual</code> <code class="csharp plain">MeshData FaceDataUp</code></div><div class="line number2 index1 alt1"><code class="csharp spaces"> </code><code class="csharp plain">(Chunk chunk, </code><code class="csharp keyword">int</code> <code class="csharp plain">x, </code><code class="csharp keyword">int</code> <code class="csharp plain">y, </code><code class="csharp keyword">int</code> <code class="csharp plain">z, MeshData meshData)</code></div><div class="line number3 index2 alt2"><code class="csharp spaces"> </code><code class="csharp plain">{</code></div><div class="line number4 index3 alt1"><code class="csharp spaces"> </code><code class="csharp plain">meshData.vertices.Add(</code><code class="csharp keyword">new</code> <code class="csharp plain">Vector3(x - 0.5f, y + 0.5f, z + 0.5f));</code></div><div class="line number5 index4 alt2"><code class="csharp spaces"> </code><code class="csharp plain">meshData.vertices.Add(</code><code class="csharp keyword">new</code> <code class="csharp plain">Vector3(x + 0.5f, y + 0.5f, z + 0.5f));</code></div><div class="line number6 index5 alt1"><code class="csharp spaces"> </code><code class="csharp plain">meshData.vertices.Add(</code><code class="csharp keyword">new</code> <code class="csharp plain">Vector3(x + 0.5f, y + 0.5f, z - 0.5f));</code></div><div class="line number7 index6 alt2"><code class="csharp spaces"> </code><code class="csharp plain">meshData.vertices.Add(</code><code class="csharp keyword">new</code> <code class="csharp plain">Vector3(x - 0.5f, y + 0.5f, z - 0.5f));</code></div><div class="line number8 index7 alt1"> </div><div class="line number9 index8 alt2"><code class="csharp spaces"> </code><code class="csharp plain">meshData.AddQuadTriangles();</code></div><div class="line number10 index9 alt1"> </div><div class="line number11 index10 alt2"><code class="csharp spaces"> </code><code class="csharp comments">//Add the following line to every FaceData function with the direction of the face</code></div><div class="line number12 index11 alt1"><code class="csharp spaces"> </code><code class="csharp plain">meshData.uv.AddRange(FaceUVs(Direction.up));</code></div><div class="line number13 index12 alt2"> </div><div class="line number14 index13 alt1"><code class="csharp spaces"> </code><code class="csharp keyword">return</code> <code class="csharp plain">meshData;</code></div><div class="line number15 index14 alt2"><code class="csharp spaces"> </code><code class="csharp plain">}</code></div></div></td></tr></tbody></table></div></div></div><div id="filler"> <div class="description" style="padding-top:0px"> <bodytext><p></p>
<p>Lastly use the uv list in the chunk to create the mesh:</p>
<p> </p></bodytext></div> </div> <div class="codeblock"><div><div id="highlighter_114992" class="syntaxhighlighter csharp"><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td class="gutter"><div class="line number1 index0 alt2">1</div><div class="line number2 index1 alt1">2</div><div class="line number3 index2 alt2">3</div><div class="line number4 index3 alt1">4</div><div class="line number5 index4 alt2">5</div><div class="line number6 index5 alt1">6</div><div class="line number7 index6 alt2">7</div><div class="line number8 index7 alt1">8</div><div class="line number9 index8 alt2">9</div><div class="line number10 index9 alt1">10</div><div class="line number11 index10 alt2">11</div></td><td class="code"><div class="container"><div class="line number1 index0 alt2"><code class="csharp spaces"> </code><code class="csharp comments">// Sends the calculated mesh information</code></div><div class="line number2 index1 alt1"><code class="csharp spaces"> </code><code class="csharp comments">// to the mesh and collision components</code></div><div class="line number3 index2 alt2"><code class="csharp spaces"> </code><code class="csharp keyword">void</code> <code class="csharp plain">RenderMesh(MeshData meshData)</code></div><div class="line number4 index3 alt1"><code class="csharp spaces"> </code><code class="csharp plain">{</code></div><div class="line number5 index4 alt2"><code class="csharp spaces"> </code><code class="csharp plain">filter.mesh.Clear();</code></div><div class="line number6 index5 alt1"><code class="csharp spaces"> </code><code class="csharp plain">filter.mesh.vertices = meshData.vertices.ToArray();</code></div><div class="line number7 index6 alt2"><code class="csharp spaces"> </code><code class="csharp plain">filter.mesh.triangles = meshData.triangles.ToArray();</code></div><div class="line number8 index7 alt1"><code class="csharp spaces"> </code><code class="csharp comments">//Add the following two lines</code></div><div class="line number9 index8 alt2"><code class="csharp spaces"> </code><code class="csharp plain">filter.mesh.uv = meshData.uv.ToArray();</code></div><div class="line number10 index9 alt1"><code class="csharp spaces"> </code><code class="csharp plain">filter.mesh.RecalculateNormals();</code></div><div class="line number11 index10 alt2"><code class="csharp spaces"> </code><code class="csharp plain">}</code></div></div></td></tr></tbody></table></div></div></div><div id="filler"> <div class="description" style="padding-top:0px"> <bodytext><p></p>
<p>Firstly just send the uvs to the mesh and then we need to recalculate the normals of our new mesh or all the lighting will be off.</p>
<p>Now we can test our code in unity, if you're using the tiles I provided the base block should show up as stone!
</p></bodytext></div> </div> <img src="./Part 2_files/voxstone.png"> <div id="filler"> <div class="description"> <bodytext><p></p>
<p>It doesn't seem super impressive but let's take a crack at another block type. Grass blocks often have a texture for the sides, top and bottom so we'll try that. <strong>Create a new script and call it BlockGrass</strong> and we'll override the TexturePositions function.</p>
<p> </p></bodytext></div> </div> <div class="codeblock"><div><div id="highlighter_293398" class="syntaxhighlighter csharp"><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td class="gutter"><div class="line number1 index0 alt2">1</div><div class="line number2 index1 alt1">2</div><div class="line number3 index2 alt2">3</div><div class="line number4 index3 alt1">4</div><div class="line number5 index4 alt2">5</div><div class="line number6 index5 alt1">6</div><div class="line number7 index6 alt2">7</div><div class="line number8 index7 alt1">8</div><div class="line number9 index8 alt2">9</div><div class="line number10 index9 alt1">10</div><div class="line number11 index10 alt2">11</div><div class="line number12 index11 alt1">12</div><div class="line number13 index12 alt2">13</div><div class="line number14 index13 alt1">14</div><div class="line number15 index14 alt2">15</div><div class="line number16 index15 alt1">16</div><div class="line number17 index16 alt2">17</div><div class="line number18 index17 alt1">18</div><div class="line number19 index18 alt2">19</div><div class="line number20 index19 alt1">20</div><div class="line number21 index20 alt2">21</div><div class="line number22 index21 alt1">22</div><div class="line number23 index22 alt2">23</div><div class="line number24 index23 alt1">24</div><div class="line number25 index24 alt2">25</div><div class="line number26 index25 alt1">26</div><div class="line number27 index26 alt2">27</div><div class="line number28 index27 alt1">28</div><div class="line number29 index28 alt2">29</div><div class="line number30 index29 alt1">30</div><div class="line number31 index30 alt2">31</div><div class="line number32 index31 alt1">32</div><div class="line number33 index32 alt2">33</div><div class="line number34 index33 alt1">34</div></td><td class="code"><div class="container"><div class="line number1 index0 alt2"><code class="csharp keyword">using</code> <code class="csharp plain">UnityEngine;</code></div><div class="line number2 index1 alt1"><code class="csharp keyword">using</code> <code class="csharp plain">System.Collections;</code></div><div class="line number3 index2 alt2"> </div><div class="line number4 index3 alt1"><code class="csharp keyword">public</code> <code class="csharp keyword">class</code> <code class="csharp plain">BlockGrass : Block</code></div><div class="line number5 index4 alt2"><code class="csharp plain">{</code></div><div class="line number6 index5 alt1"> </div><div class="line number7 index6 alt2"><code class="csharp spaces"> </code><code class="csharp keyword">public</code> <code class="csharp plain">BlockGrass()</code></div><div class="line number8 index7 alt1"><code class="csharp spaces"> </code><code class="csharp plain">: </code><code class="csharp keyword">base</code><code class="csharp plain">()</code></div><div class="line number9 index8 alt2"><code class="csharp spaces"> </code><code class="csharp plain">{</code></div><div class="line number10 index9 alt1"> </div><div class="line number11 index10 alt2"><code class="csharp spaces"> </code><code class="csharp plain">}</code></div><div class="line number12 index11 alt1"> </div><div class="line number13 index12 alt2"><code class="csharp spaces"> </code><code class="csharp keyword">public</code> <code class="csharp keyword">override</code> <code class="csharp plain">Tile TexturePosition(Direction direction)</code></div><div class="line number14 index13 alt1"><code class="csharp spaces"> </code><code class="csharp plain">{</code></div><div class="line number15 index14 alt2"><code class="csharp spaces"> </code><code class="csharp plain">Tile tile = </code><code class="csharp keyword">new</code> <code class="csharp plain">Tile();</code></div><div class="line number16 index15 alt1"> </div><div class="line number17 index16 alt2"><code class="csharp spaces"> </code><code class="csharp keyword">switch</code> <code class="csharp plain">(direction)</code></div><div class="line number18 index17 alt1"><code class="csharp spaces"> </code><code class="csharp plain">{</code></div><div class="line number19 index18 alt2"><code class="csharp spaces"> </code><code class="csharp keyword">case</code> <code class="csharp plain">Direction.up:</code></div><div class="line number20 index19 alt1"><code class="csharp spaces"> </code><code class="csharp plain">tile.x = 2;</code></div><div class="line number21 index20 alt2"><code class="csharp spaces"> </code><code class="csharp plain">tile.y = 0;</code></div><div class="line number22 index21 alt1"><code class="csharp spaces"> </code><code class="csharp keyword">return</code> <code class="csharp plain">tile;</code></div><div class="line number23 index22 alt2"><code class="csharp spaces"> </code><code class="csharp keyword">case</code> <code class="csharp plain">Direction.down:</code></div><div class="line number24 index23 alt1"><code class="csharp spaces"> </code><code class="csharp plain">tile.x = 1;</code></div><div class="line number25 index24 alt2"><code class="csharp spaces"> </code><code class="csharp plain">tile.y = 0;</code></div><div class="line number26 index25 alt1"><code class="csharp spaces"> </code><code class="csharp keyword">return</code> <code class="csharp plain">tile;</code></div><div class="line number27 index26 alt2"><code class="csharp spaces"> </code><code class="csharp plain">}</code></div><div class="line number28 index27 alt1"> </div><div class="line number29 index28 alt2"><code class="csharp spaces"> </code><code class="csharp plain">tile.x = 3;</code></div><div class="line number30 index29 alt1"><code class="csharp spaces"> </code><code class="csharp plain">tile.y = 0;</code></div><div class="line number31 index30 alt2"> </div><div class="line number32 index31 alt1"><code class="csharp spaces"> </code><code class="csharp keyword">return</code> <code class="csharp plain">tile;</code></div><div class="line number33 index32 alt2"><code class="csharp spaces"> </code><code class="csharp plain">}</code></div><div class="line number34 index33 alt1"><code class="csharp plain">}</code></div></div></td></tr></tbody></table></div></div></div><div id="filler"> <div class="description" style="padding-top:0px"> <bodytext><p></p>
<p>The up and down faces return specific tiles and the rest return another. This gives us a simple way to retexture a block for a new bock type. Lets see it in game, add this line just before update chunk in the chunk's Update function:</p>
<p> </p></bodytext></div> </div> <div class="codeblock"><div><div id="highlighter_770456" class="syntaxhighlighter csharp"><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td class="gutter"><div class="line number1 index0 alt2">1</div></td><td class="code"><div class="container"><div class="line number1 index0 alt2"><code class="csharp spaces"> </code><code class="csharp plain">blocks[4, 5, 2] = </code><code class="csharp keyword">new</code> <code class="csharp plain">BlockGrass();</code></div></div></td></tr></tbody></table></div></div></div><div id="filler"> <div class="description" style="padding-top:0px"> <bodytext><p></p>
<p>And you'll see this:</p>
<p> </p></bodytext></div> </div> <img src="./Part 2_files/voxstonegrass.png"> <div id="filler"> <div class="description"> <bodytext><p></p>
<h2>Collision mesh</h2>
<p>Now let's take a look at generating the collision mesh, the last voxel tutorial I wrote used the same mesh for rendering as for the collision mesh. This works for the standard cube voxel but a lot of games end up having more and more complex blocks where you would prefer to have a more simplified collision mesh or none at all.</p>
<p></p><div class="infobox"><infoboxtext><p>Updating the mesh the collision mesh is one of the longest factors in chunk update time, because of this it's smart to keep your collision mesh as simple as possible. It is also possible (if you don't intend to use unity physics) to script your own collisions without a mesh. Here's a good explanation of a technique you could use for your own collisions: <a href="https://web.archive.org/web/20150318011823/http://stackoverflow.com/questions/22512319/what-is-aabb-collision-detection">Stack Overflow</a></p></infoboxtext></div><p></p>
<p>Because most of the time the collision mesh will match the render mesh the first thing we'll do is make an easy way to use the same data for both. We'll do this by copying all additions to vertices and triangles in the mesh data object.</p>
<p>First add a new boolean to MeshData:
</p></bodytext></div> </div> <div class="codeblock"><div><div id="highlighter_514239" class="syntaxhighlighter csharp"><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td class="gutter"><div class="line number1 index0 alt2">1</div></td><td class="code"><div class="container"><div class="line number1 index0 alt2"><code class="csharp spaces"> </code><code class="csharp keyword">public</code> <code class="csharp keyword">bool</code> <code class="csharp plain">useRenderDataForCol;</code></div></div></td></tr></tbody></table></div></div></div><div id="filler"> <div class="description" style="padding-top:0px"> <bodytext><p></p>
<p>When this is true all triangles and vertices added to the render mesh get added to the collision mesh as well.</p>
<p>Then we'll change the existing AddQuadTriangles function:
</p></bodytext></div> </div> <div class="codeblock"><div><div id="highlighter_708756" class="syntaxhighlighter csharp"><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td class="gutter"><div class="line number1 index0 alt2">1</div><div class="line number2 index1 alt1">2</div><div class="line number3 index2 alt2">3</div><div class="line number4 index3 alt1">4</div><div class="line number5 index4 alt2">5</div><div class="line number6 index5 alt1">6</div><div class="line number7 index6 alt2">7</div><div class="line number8 index7 alt1">8</div><div class="line number9 index8 alt2">9</div><div class="line number10 index9 alt1">10</div><div class="line number11 index10 alt2">11</div><div class="line number12 index11 alt1">12</div><div class="line number13 index12 alt2">13</div><div class="line number14 index13 alt1">14</div><div class="line number15 index14 alt2">15</div><div class="line number16 index15 alt1">16</div><div class="line number17 index16 alt2">17</div><div class="line number18 index17 alt1">18</div><div class="line number19 index18 alt2">19</div></td><td class="code"><div class="container"><div class="line number1 index0 alt2"><code class="csharp spaces"> </code><code class="csharp keyword">public</code> <code class="csharp keyword">void</code> <code class="csharp plain">AddQuadTriangles()</code></div><div class="line number2 index1 alt1"><code class="csharp spaces"> </code><code class="csharp plain">{</code></div><div class="line number3 index2 alt2"><code class="csharp spaces"> </code><code class="csharp plain">triangles.Add(vertices.Count - 4);</code></div><div class="line number4 index3 alt1"><code class="csharp spaces"> </code><code class="csharp plain">triangles.Add(vertices.Count - 3);</code></div><div class="line number5 index4 alt2"><code class="csharp spaces"> </code><code class="csharp plain">triangles.Add(vertices.Count - 2);</code></div><div class="line number6 index5 alt1"><code class="csharp spaces"> </code><code class="csharp plain">triangles.Add(vertices.Count - 4);</code></div><div class="line number7 index6 alt2"><code class="csharp spaces"> </code><code class="csharp plain">triangles.Add(vertices.Count - 2);</code></div><div class="line number8 index7 alt1"><code class="csharp spaces"> </code><code class="csharp plain">triangles.Add(vertices.Count - 1);</code></div><div class="line number9 index8 alt2"> </div><div class="line number10 index9 alt1"><code class="csharp spaces"> </code><code class="csharp keyword">if</code> <code class="csharp plain">(useRenderDataForCol)</code></div><div class="line number11 index10 alt2"><code class="csharp spaces"> </code><code class="csharp plain">{</code></div><div class="line number12 index11 alt1"><code class="csharp spaces"> </code><code class="csharp plain">colTriangles.Add(colVertices.Count - 4);</code></div><div class="line number13 index12 alt2"><code class="csharp spaces"> </code><code class="csharp plain">colTriangles.Add(colVertices.Count - 3);</code></div><div class="line number14 index13 alt1"><code class="csharp spaces"> </code><code class="csharp plain">colTriangles.Add(colVertices.Count - 2);</code></div><div class="line number15 index14 alt2"><code class="csharp spaces"> </code><code class="csharp plain">colTriangles.Add(colVertices.Count - 4);</code></div><div class="line number16 index15 alt1"><code class="csharp spaces"> </code><code class="csharp plain">colTriangles.Add(colVertices.Count - 2);</code></div><div class="line number17 index16 alt2"><code class="csharp spaces"> </code><code class="csharp plain">colTriangles.Add(colVertices.Count - 1);</code></div><div class="line number18 index17 alt1"><code class="csharp spaces"> </code><code class="csharp plain">}</code></div><div class="line number19 index18 alt2"><code class="csharp spaces"> </code><code class="csharp plain">}</code></div></div></td></tr></tbody></table></div></div></div><div id="filler"> <div class="description" style="padding-top:0px"> <bodytext><p></p>
<p>This way we carry out the same function but for the collision mesh when useRenderDataForCol is true but we use colVertices.Count as the offset for the collision list.</p>
<p>Next add a function to add to MeshData:</p>
<p> </p></bodytext></div> </div> <div class="codeblock"><div><div id="highlighter_523234" class="syntaxhighlighter csharp"><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td class="gutter"><div class="line number1 index0 alt2">1</div><div class="line number2 index1 alt1">2</div><div class="line number3 index2 alt2">3</div><div class="line number4 index3 alt1">4</div><div class="line number5 index4 alt2">5</div><div class="line number6 index5 alt1">6</div><div class="line number7 index6 alt2">7</div><div class="line number8 index7 alt1">8</div><div class="line number9 index8 alt2">9</div><div class="line number10 index9 alt1">10</div></td><td class="code"><div class="container"><div class="line number1 index0 alt2"><code class="csharp spaces"> </code><code class="csharp keyword">public</code> <code class="csharp keyword">void</code> <code class="csharp plain">AddVertex(Vector3 vertex)</code></div><div class="line number2 index1 alt1"><code class="csharp spaces"> </code><code class="csharp plain">{</code></div><div class="line number3 index2 alt2"><code class="csharp spaces"> </code><code class="csharp plain">vertices.Add(vertex);</code></div><div class="line number4 index3 alt1"> </div><div class="line number5 index4 alt2"><code class="csharp spaces"> </code><code class="csharp keyword">if</code> <code class="csharp plain">(useRenderDataForCol)</code></div><div class="line number6 index5 alt1"><code class="csharp spaces"> </code><code class="csharp plain">{</code></div><div class="line number7 index6 alt2"><code class="csharp spaces"> </code><code class="csharp plain">colVertices.Add(vertex);</code></div><div class="line number8 index7 alt1"><code class="csharp spaces"> </code><code class="csharp plain">}</code></div><div class="line number9 index8 alt2"> </div><div class="line number10 index9 alt1"><code class="csharp spaces"> </code><code class="csharp plain">}</code></div></div></td></tr></tbody></table></div></div></div><div id="filler"> <div class="description" style="padding-top:0px"> <bodytext><p></p>
<p>Currently over in the block script we're adding to the vertices list directly so we'll have to change over to calling this function to add vertices. <strong>Go to the block class</strong> and change all the lines like this:</p>
<p> </p></bodytext></div> </div> <div class="codeblock"><div><div id="highlighter_531916" class="syntaxhighlighter csharp"><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td class="gutter"><div class="line number1 index0 alt2">1</div></td><td class="code"><div class="container"><div class="line number1 index0 alt2"><code class="csharp spaces"> </code><code class="csharp plain">meshData.vertices.Add(</code><code class="csharp keyword">new</code> <code class="csharp plain">Vector3(...));</code></div></div></td></tr></tbody></table></div></div></div><div id="filler"> <div class="description" style="padding-top:0px"> <bodytext><p></p>
<p>to this:</p>
<p> </p></bodytext></div> </div> <div class="codeblock"><div><div id="highlighter_778425" class="syntaxhighlighter csharp"><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td class="gutter"><div class="line number1 index0 alt2">1</div></td><td class="code"><div class="container"><div class="line number1 index0 alt2"><code class="csharp spaces"> </code><code class="csharp plain">meshData.AddVertex(</code><code class="csharp keyword">new</code> <code class="csharp plain">Vector3(...));</code></div></div></td></tr></tbody></table></div></div></div><div id="filler"> <div class="description" style="padding-top:0px"> <bodytext><p></p>
<p>This way they're added to vertices and added to colVertices if useRenderDataForCol is true.</p>
<p>One last function we need in the <strong>MeshData script</strong> adds to the triangles list. Although right now we're only using AddQuadTriangles() we might need the functionality later:</p>
<p> </p></bodytext></div> </div> <div class="codeblock"><div><div id="highlighter_805445" class="syntaxhighlighter csharp"><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td class="gutter"><div class="line number1 index0 alt2">1</div><div class="line number2 index1 alt1">2</div><div class="line number3 index2 alt2">3</div><div class="line number4 index3 alt1">4</div><div class="line number5 index4 alt2">5</div><div class="line number6 index5 alt1">6</div><div class="line number7 index6 alt2">7</div><div class="line number8 index7 alt1">8</div><div class="line number9 index8 alt2">9</div></td><td class="code"><div class="container"><div class="line number1 index0 alt2"><code class="csharp spaces"> </code><code class="csharp keyword">public</code> <code class="csharp keyword">void</code> <code class="csharp plain">AddTriangle(</code><code class="csharp keyword">int</code> <code class="csharp plain">tri)</code></div><div class="line number2 index1 alt1"><code class="csharp spaces"> </code><code class="csharp plain">{</code></div><div class="line number3 index2 alt2"><code class="csharp spaces"> </code><code class="csharp plain">triangles.Add(tri);</code></div><div class="line number4 index3 alt1"> </div><div class="line number5 index4 alt2"><code class="csharp spaces"> </code><code class="csharp keyword">if</code> <code class="csharp plain">(useRenderDataForCol)</code></div><div class="line number6 index5 alt1"><code class="csharp spaces"> </code><code class="csharp plain">{</code></div><div class="line number7 index6 alt2"><code class="csharp spaces"> </code><code class="csharp plain">colTriangles.Add(tri - (vertices.Count - colVertices.Count));</code></div><div class="line number8 index7 alt1"><code class="csharp spaces"> </code><code class="csharp plain">}</code></div><div class="line number9 index8 alt2"><code class="csharp spaces"> </code><code class="csharp plain">}</code></div></div></td></tr></tbody></table></div></div></div><div id="filler"> <div class="description" style="padding-top:0px"> <bodytext><p></p>
<p>Firstly it just adds to the triangle list, then if useRenderDataForCol is true we add it to the colTriangles list but we need to adjust the value by the difference between the count of the vertices and colVertices lists since the triangles list entries correspond to indexes in the vertices lists we have to adjust their values to match the differences in the lists.</p>
<p>Now that we have the capability to add to both lists we have to enable it by setting useRenderDataForCol to true at the start of the BlockData function <strong>in the block script</strong>:</p>
<p> </p></bodytext></div> </div> <div class="codeblock"><div><div id="highlighter_901457" class="syntaxhighlighter csharp"><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td class="gutter"><div class="line number1 index0 alt2">1</div><div class="line number2 index1 alt1">2</div><div class="line number3 index2 alt2">3</div><div class="line number4 index3 alt1">4</div></td><td class="code"><div class="container"><div class="line number1 index0 alt2"><code class="csharp spaces"> </code><code class="csharp keyword">public</code> <code class="csharp keyword">virtual</code> <code class="csharp plain">MeshData BlockData(Chunk chunk, </code><code class="csharp keyword">int</code> <code class="csharp plain">x, </code><code class="csharp keyword">int</code> <code class="csharp plain">y, </code><code class="csharp keyword">int</code> <code class="csharp plain">z, MeshData meshData){</code></div><div class="line number2 index1 alt1"> </div><div class="line number3 index2 alt2"><code class="csharp spaces"> </code><code class="csharp plain">meshData.useRenderDataForCol = </code><code class="csharp keyword">true</code><code class="csharp plain">;</code></div><div class="line number4 index3 alt1"><code class="csharp spaces"> </code><code class="csharp plain">...</code></div></div></td></tr></tbody></table></div></div></div><div id="filler"> <div class="description" style="padding-top:0px"> <bodytext><p></p>
<p></p><div class="warnbox"><infoboxtext><p>Remember to set this to true or false at the start BlockData whenever you override it or else you'll continue using the setting of the last block.</p></infoboxtext></div><p></p>
<p>That should give you valid collision mesh data in the meshData class. Now we have to apply it to out Mesh Collider component:</p>
<p> </p></bodytext></div> </div> <div class="codeblock"><div><div id="highlighter_592770" class="syntaxhighlighter csharp"><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td class="gutter"><div class="line number1 index0 alt2">1</div><div class="line number2 index1 alt1">2</div><div class="line number3 index2 alt2">3</div><div class="line number4 index3 alt1">4</div><div class="line number5 index4 alt2">5</div><div class="line number6 index5 alt1">6</div><div class="line number7 index6 alt2">7</div><div class="line number8 index7 alt1">8</div><div class="line number9 index8 alt2">9</div><div class="line number10 index9 alt1">10</div><div class="line number11 index10 alt2">11</div><div class="line number12 index11 alt1">12</div><div class="line number13 index12 alt2">13</div><div class="line number14 index13 alt1">14</div><div class="line number15 index14 alt2">15</div><div class="line number16 index15 alt1">16</div><div class="line number17 index16 alt2">17</div><div class="line number18 index17 alt1">18</div></td><td class="code"><div class="container"><div class="line number1 index0 alt2"><code class="csharp spaces"> </code><code class="csharp keyword">void</code> <code class="csharp plain">RenderMesh(MeshData meshData)</code></div><div class="line number2 index1 alt1"><code class="csharp spaces"> </code><code class="csharp plain">{</code></div><div class="line number3 index2 alt2"><code class="csharp spaces"> </code><code class="csharp plain">filter.mesh.Clear();</code></div><div class="line number4 index3 alt1"><code class="csharp spaces"> </code><code class="csharp plain">filter.mesh.vertices = meshData.vertices.ToArray();</code></div><div class="line number5 index4 alt2"><code class="csharp spaces"> </code><code class="csharp plain">filter.mesh.triangles = meshData.triangles.ToArray();</code></div><div class="line number6 index5 alt1"><code class="csharp spaces"> </code><code class="csharp plain">filter.mesh.uv = meshData.uv.ToArray();</code></div><div class="line number7 index6 alt2"> </div><div class="line number8 index7 alt1"><code class="csharp spaces"> </code><code class="csharp plain">filter.mesh.RecalculateNormals();</code></div><div class="line number9 index8 alt2"><code class="csharp spaces"> </code> </div><div class="line number10 index9 alt1"><code class="csharp spaces"> </code><code class="csharp comments">//additions:</code></div><div class="line number11 index10 alt2"><code class="csharp spaces"> </code><code class="csharp plain">coll.sharedMesh = </code><code class="csharp keyword">null</code><code class="csharp plain">;</code></div><div class="line number12 index11 alt1"><code class="csharp spaces"> </code><code class="csharp plain">Mesh mesh = </code><code class="csharp keyword">new</code> <code class="csharp plain">Mesh();</code></div><div class="line number13 index12 alt2"><code class="csharp spaces"> </code><code class="csharp plain">mesh.vertices = meshData.colVertices.ToArray();</code></div><div class="line number14 index13 alt1"><code class="csharp spaces"> </code><code class="csharp plain">mesh.triangles = meshData.colTriangles.ToArray();</code></div><div class="line number15 index14 alt2"><code class="csharp spaces"> </code><code class="csharp plain">mesh.RecalculateNormals();</code></div><div class="line number16 index15 alt1"><code class="csharp spaces"> </code> </div><div class="line number17 index16 alt2"><code class="csharp spaces"> </code><code class="csharp plain">coll.sharedMesh = mesh;</code></div><div class="line number18 index17 alt1"><code class="csharp spaces"> </code><code class="csharp plain">}</code></div></div></td></tr></tbody></table></div></div></div><div id="filler"> <div class="description" style="padding-top:0px"> <bodytext><p></p>
<p>The additions to the RenderMesh function remove the current collider mesh and then create a new mesh to apply the vertices and triangles to and then use the new mesh as the collision mesh. Running that in unity should give you a collision mesh exactly where your rendered mesh is. You can see that it's working by enabling Gizmos > MeshCollider and disabling then enabling the collider. The collider should show up as a green wireframe when the chunk is selected right on top of the blue outline that was there. You should see the green wireframe turn on and off by disabling and enabling the mesh collider component.</p>
<p> </p></bodytext></div> </div> <img src="./Part 2_files/voxcol.png"> <div id="filler"> <div class="description"> <bodytext><p></p>
<p>If your project isn't working at this point try downloading the scripts from this tutorial below and see if yours match. If you can't solve it then post below with your problem and any errors you're receiving. If you find any errors in my tutorial or have suggestions I would be grateful to hear them as well.</p>
<p><a href="https://web.archive.org/web/20150318011823/http://alexstv.com/index.php/posts/unity-voxel-block-tutorial" class="inline_button" style="background-color:#2D9AE3">Tutorial part 1</a><a href="https://web.archive.org/web/20150318011823/http://alexstv.com/downloads/voxeltut2.zip" class="inline_button" style="background-color:#2D9AE3">Download scripts so far</a>
<a href="https://web.archive.org/web/20150318011823/http://alexstv.com/index.php/posts/unity-voxel-block-tutorial-pt-3" class="inline_button" style="background-color:#2D9AE3">Tutorial part 3</a><br><br></p>
<p><a href="https://web.archive.org/web/20150318011823/https://twitter.com/stv_alex" class="inline_button" style="background-color:#77B1D1">Follow me on twitter</a><a href="https://web.archive.org/web/20150318011823/https://plus.google.com/+AlexandrosStavrinou/posts" class="inline_button" style="background-color:#D73D32">Add me on Google+</a><br><br></p>
</bodytext>
</div>
</div>
<section class="yspace"></section>
<div class="datestamp">
<stamptext>26th Dec, 2014</stamptext>
</div>
<div class="authorstamp">
<stamptext>Alex</stamptext>
</div>
<section class="yspace"></section>
</div>
<div class="post">
<div id="filler">
<bodytext>
<form id="comment" class="commentform commentbox" method="post" action="https://web.archive.org/web/20150318011823/http://alexstv.com:80/index.php/posts/unity-voxel-block-tutorial-pt-2#comment">
<p class="name">
<label for="name">Your name:</label>
<input name="name" id="name" type="text" placeholder="Your name" value=""> </p>
<p class="email">
<label for="email">Your email address:</label>
<input name="email" id="email" type="email" placeholder="Your email (won’t be published)" value=""> </p>
<p class="textarea">
<label for="text">Your comment:</label>
<textarea name="text" id="text" placeholder="Your comment"></textarea> </p>
<p class="submit">
<button class="btn" type="submit">Post Comment</button> </p>
</form>
</bodytext>
</div>
</div>
<div class="post">
<div id="filler">
<div class="description">
<bodytext>
<h1>Micaiah Stevens</h1>
</bodytext>
</div>
</div>
<div id="filler">
<div class="description">
<bodytext>
FaceDataUp doesn't match the original Part 1. You give different values. This part 2 values are incorrect, and shift the up texture to a off position.
Also I seem to not figure out why my scene my texture faces rotate and blend weirdly. </bodytext>
</div>
</div>
<section class="yspace"></section>
<div class="datestamp">
<stamptext>2 months ago</stamptext>
</div>
<div class="authorstamp">
<stamptext>Comment #7</stamptext>
</div>
<section class="yspace"></section>
</div>
<div class="post">
<div id="filler">
<div class="description">
<bodytext>
<h1>Bruno</h1>
</bodytext>
</div>
</div>
<div id="filler">
<div class="description">
<bodytext>
Hello, I wonder if I can use this design for sale? </bodytext>
</div>
</div>
<section class="yspace"></section>
<div class="datestamp">
<stamptext>2 months ago</stamptext>
</div>
<div class="authorstamp">
<stamptext>Comment #9</stamptext>
</div>
<section class="yspace"></section>
</div>
<div class="post">
<div id="filler">
<div class="description">
<bodytext>
<h1>AlexStv</h1>
</bodytext>
</div>
</div>
<div id="filler">
<div class="description">
<bodytext>
@Micaiah Stevens Ah, thank you for noticing that, I changed it. About your texture faces behaving strangely, if you're using a custom tilesheet try using the one supplied and see if the tilesheet layout might be the cause. Then if it isn't that try with the complete code from this part, the link is at the bottom of the tutorial and see where your code differs from that. If you post a screenshot of the issue I might be able to help more.
@Bruno You can use any code you make in the tutorial or learn from this tutorial in commercial projects without limitation. The textures supplied are free for non-commercial use. </bodytext>
</div>
</div>
<section class="yspace"></section>
<div class="datestamp">
<stamptext>2 months ago</stamptext>
</div>
<div class="authorstamp">
<stamptext>Comment #11</stamptext>
</div>
<section class="yspace"></section>
</div>
<div class="post">
<div id="filler">
<div class="description">
<bodytext>
<h1>Venor</h1>
</bodytext>
</div>
</div>
<div id="filler">
<div class="description">
<bodytext>
Hi alex,
how would you go about seperating the textures in different files so that they are connected to the blocks and not the chunk?
Instead of AllTextures256x256.png one would like to have:
Stone32x80 connected to the blockStone.cs
Dirt32x80 connected to blockDirt.cs
I read of subMeshes but i just dont see how to couple it with your system cleverly.
I wonder if you mind to help me out with a hint or two.
Greets mike </bodytext>
</div>
</div>
<section class="yspace"></section>
<div class="datestamp">
<stamptext>2 months ago</stamptext>
</div>
<div class="authorstamp">
<stamptext>Comment #13</stamptext>
</div>
<section class="yspace"></section>
</div>
<div class="post">
<div id="filler">
<div class="description">
<bodytext>
<h1>Jack</h1>
</bodytext>
</div>
</div>
<div id="filler">
<div class="description">
<bodytext>
@Venor
So, one block face per UV image file, say, grass.png, grassside.png grassbottom/dirt.png? </bodytext>
</div>
</div>
<section class="yspace"></section>
<div class="datestamp">
<stamptext>1 month ago</stamptext>
</div>
<div class="authorstamp">
<stamptext>Comment #36</stamptext>
</div>
<section class="yspace"></section>
</div>
<div class="post">
<div id="filler">
<div class="description">
<bodytext>
<h1>jonas</h1>
</bodytext>
</div>
</div>
<div id="filler">
<div class="description">
<bodytext>
my world wont be generated
</bodytext>
</div>
</div>
<section class="yspace"></section>
<div class="datestamp">
<stamptext>1 month ago</stamptext>
</div>
<div class="authorstamp">
<stamptext>Comment #41</stamptext>
</div>
<section class="yspace"></section>
</div>
<div class="post">
<div id="filler">
<div class="description">
<bodytext>
<h1>Jake Mathews</h1>
</bodytext>
</div>
</div>
<div id="filler">
<div class="description">
<bodytext>
If I set the "useRenderDataForCol" boolean to equal false, from my understanding, that should mean Unity will fill in the mesh collider data itself. However when I set that variable to false, it adds the collider component, but there is no data, so nothing collides with it. Is this an expected behavior? </bodytext>
</div>
</div>
<section class="yspace"></section>
<div class="datestamp">
<stamptext>4 weeks ago</stamptext>
</div>
<div class="authorstamp">
<stamptext>Comment #50</stamptext>
</div>
<section class="yspace"></section>
</div>
<div class="post">
<div id="filler">
<div class="description">
<bodytext>
<h1>Captain Hook</h1>
</bodytext>
</div>
</div>
<div id="filler">
<div class="description">
<bodytext>
Hi
First: Thank you for the tutorial. It is realy great!
I had a MemoryLeak Problem with RenderMesh() Method. I think it dont destroy the coll.sharedMesh = null. I called the method around 100000 and i get a problem with my Mem. The solution was to put "Destroy (coll.sharedMesh);". before setting it to null. But it is possible that only i had this problem because i had make some change on the code. </bodytext>
</div>
</div>
<section class="yspace"></section>
<div class="datestamp">
<stamptext>7 hours ago</stamptext>
</div>
<div class="authorstamp">
<stamptext>Comment #99</stamptext>
</div>
<section class="yspace"></section>
</div>
</article>
</div>
<div class="bg1">
<footer class="wrapper">
<footer class="footer">
<div class="fiftyfifty" style="padding:10px;"><bodytext>© 2015 AlexStv. All rights reserved.</bodytext></div>
<div class="fiftyfifty"><form id="search" action="https://web.archive.org/web/20150318011823/http://alexstv.com:80/index.php/search" method="post">
<bodytext>Search: </bodytext>
<input type="search" style="width:300px; padding:10px; border: 1px solid #e8e8e8;
-webkit-border-radius: 3px;
-moz-border-radius: 3px;
border-radius: 3px;" id="term" name="term" placeholder="Type here and hit enter to search" value=""></form></div>
</footer>
</footer>
</div>
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//web.archive.org/web/20150318011823/http://www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-58007562-1', 'auto');
ga('send', 'pageview');
</script>
<!--
playback timings (ms):
LoadShardBlock: 234.884 (3)
esindex: 0.006
captures_list: 254.664
CDXLines.iter: 14.903 (3)
PetaboxLoader3.datanode: 239.005 (4)
exclusion.robots: 0.308
exclusion.robots.policy: 0.283
RedisCDXSource: 1.504
PetaboxLoader3.resolve: 26.42
load_resource: 55.058
--></body></html>