{"id":634,"date":"2012-02-15T23:19:11","date_gmt":"2012-02-15T20:19:11","guid":{"rendered":"http:\/\/unitycoder.com\/blog\/?p=634"},"modified":"2013-08-15T22:21:47","modified_gmt":"2013-08-15T19:21:47","slug":"dummy-craft-chunk-renderer-js","status":"publish","type":"post","link":"https:\/\/unitycoder.com\/blog\/2012\/02\/15\/dummy-craft-chunk-renderer-js\/","title":{"rendered":"DummyCraft \/ Chunk Renderer (js)"},"content":{"rendered":"<p><a title=\"start webplayer demo\" href=\"http:\/\/unitycoder.com\/upload\/demos\/DummyCraft1\/\" target=\"_blank\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"638\" data-permalink=\"https:\/\/unitycoder.com\/blog\/2012\/02\/15\/dummy-craft-chunk-renderer-js\/chunk_render2\/\" data-orig-file=\"https:\/\/i0.wp.com\/unitycoder.com\/blog\/wp-content\/uploads\/2012\/02\/chunk_render2.jpg?fit=680%2C495&amp;ssl=1\" data-orig-size=\"680,495\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;}\" data-image-title=\"chunk_render2\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/unitycoder.com\/blog\/wp-content\/uploads\/2012\/02\/chunk_render2.jpg?fit=680%2C495&amp;ssl=1\" class=\"alignnone size-full wp-image-638\" title=\"chunk_render2\" alt=\"\" src=\"https:\/\/i0.wp.com\/unitycoder.com\/blog\/wp-content\/uploads\/2012\/02\/chunk_render2.jpg?resize=680%2C495\" width=\"680\" height=\"495\" srcset=\"https:\/\/i0.wp.com\/unitycoder.com\/blog\/wp-content\/uploads\/2012\/02\/chunk_render2.jpg?w=680&amp;ssl=1 680w, https:\/\/i0.wp.com\/unitycoder.com\/blog\/wp-content\/uploads\/2012\/02\/chunk_render2.jpg?resize=300%2C218&amp;ssl=1 300w\" sizes=\"auto, (max-width: 680px) 100vw, 680px\" \/><\/a><\/p>\n<p>Yay..somebody has started a minecraft-style terrain tutorial here: <a title=\"http:\/\/www.blocksters.com\/node\/57\" href=\"http:\/\/www.blocksters.com\/node\/57\" target=\"_blank\">http:\/\/www.blocksters.com\/node\/57<\/a><\/p>\n<p>Converted the ChunkRenderer to unity javascript and it worked!..Oo (see image#1)<br \/>\n(now just waiting for the next parts of the tutorial..)<\/p>\n<p>While waiting, added other sides for the planes + uv (see main image ^)<br \/>\nIts working, but its quite &#8220;heavy&#8221; since there are planes inside too..<\/p>\n<p><strong>Questions:<\/strong><br \/>\n&#8211; if I make the chunk array bigger, it will hit the 65000 vertex limit..<br \/>\n&#8211; And if I would want to update it, lets say remove one cube, do I need to re-create the whole chunk mesh again(?<br \/>\n&#8211; How could I have hundreds of these on screen..?<\/p>\n<p><strong>Notes for v1:<\/strong><br \/>\n&#8211; Can remove blocks with mouse click<br \/>\n&#8211; Whole chunk mesh is re-created everytime you remove block<br \/>\n&#8211; Using mesh collider<br \/>\n&#8211; For some reason, cannot hold button down in one place to dig deep hole..<\/p>\n<p><strong>Webplayer:<\/strong><br \/>\n<a title=\"http:\/\/unitycoder.com\/upload\/demos\/DummyCraft1\/\" href=\"http:\/\/unitycoder.com\/upload\/demos\/DummyCraft1\/\" target=\"_blank\">http:\/\/unitycoder.com\/upload\/demos\/DummyCraft1\/<\/a> (v1.0)<\/p>\n<p>D<strong>ownload webplayer source:<\/strong><br \/>\n<a title=\"Download (with UnityCoder toolbar)\" href=\"http:\/\/unitycoder.com\/download\/index2.php?link=178&amp;uid=un1t3c0d5r\" target=\"_blank\">ChunkRenderTest.unityPackage<\/a>\u00a0 (webplayer scene+sources)<\/p>\n<p><strong>Source (js):<\/strong> (Attach this script to a gameObject: with MeshRenderer &amp; MeshFilter, move the gameObject to: &#8220;0,0,0&#8221;, Add Linerenderer to camera)<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\/\/ ChunkRenderer testing in unity javascript - mgear - http:\/\/unitycoder.com\/blog\r\n\/\/ *ORIGINAL SOURCE: Rendering sorcery II by &quot;paulp&quot; &gt; http:\/\/www.blocksters.com\/node\/57\r\n#pragma strict\r\n\r\nimport System.Collections.Generic;\r\nprivate var s:int=16; \/\/ size n*n*n\r\nprivate var lineRenderer : LineRenderer;\r\n\/\/public var explofx:Transform; \/\/ disabled for now\r\nprivate var chunk:int&#x5B;,,] = new int&#x5B;s,s,s];\r\n\r\nfunction Start ()\r\n{\r\n\/\/ fill array\r\nfor (var x:int=0; x&lt; s; x++)\r\n{\r\nfor (var y:int=0; y&lt; s; y++)\r\n{\r\nfor (var z:int = 0; z &lt; s; z++)\r\n{\r\nchunk&#x5B;x,y,z] = 1; \/\/Random.Range (0,2); \/\/ 0 or 1\r\n}\r\n}\r\n}\r\ngameObject.AddComponent (&quot;MeshCollider&quot;);\r\nChunkRender(chunk);\r\n\/\/ for laser fx\r\nlineRenderer = Camera.main.transform.GetComponent(LineRenderer);\r\n}\r\n\r\nfunction ChunkRender(chunk:int&#x5B;,,])\r\n{\r\nvar vertices:List.&lt;Vector3&gt; = new List.&lt;Vector3&gt;();\r\nvar uvs:List.&lt;Vector2&gt; = new List.&lt;Vector2&gt;();\r\nvar triangles:List.&lt;int&gt; = new List.&lt;int&gt;();\r\nvar vertexIndex:int;\r\nvar top:int;\r\nvar north:int;\r\nvar east:int;\r\nvar south:int;\r\nvar west:int;\r\nvar bottom:int;\r\nfor (var x:int=0; x&lt; s; x++)\r\nfor (var y:int=0; y&lt; s; y++)\r\nfor (var z:int = 0; z &lt; s; z++)\r\n{\r\nvar block = chunk&#x5B;x, y, z];\r\n\r\nif (y==s-1){top = 0;}else{top = chunk&#x5B;x, y + 1, z];}\r\nif (y==0){bottom = 0;}else{bottom = chunk&#x5B;x, y - 1, z];}\r\nif (z==s-1){north = 0;}else{north = chunk&#x5B;x, y, z+1];}\r\nif (z==0){south = 0;}else{south = chunk&#x5B;x, y, z-1];}\r\nif (x==s-1){east = 0;}else{east = chunk&#x5B;x+1, y, z];}\r\nif (x==0){west = 0;}else{west = chunk&#x5B;x-1, y, z];}\r\n\r\n\/\/ we are checking the top face of the block, so see if the top is exposed\r\nif (block == 1 &amp;&amp; top == 0)\r\n{\r\nvertexIndex = vertices.Count;\r\nvertices.Add(new Vector3(x, y + 1, z));\r\nvertices.Add(new Vector3(x, y + 1, z+1));\r\nvertices.Add(new Vector3(x+1, y + 1, z+1));\r\nvertices.Add(new Vector3(x+1, y + 1, z));\r\n\/\/ first triangle for the block top\r\ntriangles.Add(vertexIndex);\r\ntriangles.Add(vertexIndex+1);\r\ntriangles.Add(vertexIndex+2);\r\n\/\/ second triangle for the block top\r\ntriangles.Add(vertexIndex+2);\r\ntriangles.Add(vertexIndex+3);\r\ntriangles.Add(vertexIndex);\r\n\/\/ add UV\r\nuvs.Add(Vector2 (0, 0));\r\nuvs.Add(Vector2 (0, 1));\r\nuvs.Add(Vector2 (1, 1));\r\nuvs.Add(Vector2 (1, 0));\r\n}\r\n\r\nif (block == 1 &amp;&amp; north == 0)\r\n{\r\nvertexIndex = vertices.Count;\r\nvertices.Add(new Vector3(x, y, z+1));\r\nvertices.Add(new Vector3(x+1, y, z+1));\r\nvertices.Add(new Vector3(x+1, y + 1, z+1));\r\nvertices.Add(new Vector3(x, y+1 , z+1));\r\n\/\/ first triangle for the block top\r\ntriangles.Add(vertexIndex);\r\ntriangles.Add(vertexIndex+1);\r\ntriangles.Add(vertexIndex+2);\r\n\/\/ second triangle for the block top\r\ntriangles.Add(vertexIndex+2);\r\ntriangles.Add(vertexIndex+3);\r\ntriangles.Add(vertexIndex);\r\n\/\/ add UV\r\nuvs.Add(Vector2 (0, 0));\r\nuvs.Add(Vector2 (0, 1));\r\nuvs.Add(Vector2 (1, 1));\r\nuvs.Add(Vector2 (1, 0));\r\n}\r\n\r\nif (block == 1 &amp;&amp; east == 0)\r\n{\r\nvertexIndex = vertices.Count;\r\nvertices.Add(new Vector3(x+1, y, z));\r\nvertices.Add(new Vector3(x+1, y + 1, z));\r\nvertices.Add(new Vector3(x+1, y + 1, z+1));\r\nvertices.Add(new Vector3(x+1, y , z+1));\r\n\/\/ first triangle for the block top\r\ntriangles.Add(vertexIndex);\r\ntriangles.Add(vertexIndex+1);\r\ntriangles.Add(vertexIndex+2);\r\n\/\/ second triangle for the block top\r\ntriangles.Add(vertexIndex+2);\r\ntriangles.Add(vertexIndex+3);\r\ntriangles.Add(vertexIndex);\r\n\/\/ add UV\r\nuvs.Add(Vector2 (0, 0));\r\nuvs.Add(Vector2 (0, 1));\r\nuvs.Add(Vector2 (1, 1));\r\nuvs.Add(Vector2 (1, 0));\r\n}\r\n\r\nif (block == 1 &amp;&amp; south == 0)\r\n{\r\nvertexIndex = vertices.Count;\r\nvertices.Add(new Vector3(x, y, z));\r\nvertices.Add(new Vector3(x, y + 1, z));\r\nvertices.Add(new Vector3(x+1, y + 1, z));\r\nvertices.Add(new Vector3(x+1, y , z));\r\n\/\/ first triangle for the block top\r\ntriangles.Add(vertexIndex);\r\ntriangles.Add(vertexIndex+1);\r\ntriangles.Add(vertexIndex+2);\r\n\/\/ second triangle for the block top\r\ntriangles.Add(vertexIndex+2);\r\ntriangles.Add(vertexIndex+3);\r\ntriangles.Add(vertexIndex);\r\n\/\/ add UV\r\nuvs.Add(Vector2 (0, 0));\r\nuvs.Add(Vector2 (0, 1));\r\nuvs.Add(Vector2 (1, 1));\r\nuvs.Add(Vector2 (1, 0));\r\n}\r\n\r\nif (block == 1 &amp;&amp; west == 0)\r\n{\r\nvertexIndex = vertices.Count;\r\nvertices.Add(new Vector3(x, y, z+1));\r\nvertices.Add(new Vector3(x, y + 1, z+1));\r\nvertices.Add(new Vector3(x, y + 1, z));\r\nvertices.Add(new Vector3(x, y , z));\r\n\/\/ first triangle for the block top\r\ntriangles.Add(vertexIndex);\r\ntriangles.Add(vertexIndex+1);\r\ntriangles.Add(vertexIndex+2);\r\n\/\/ second triangle for the block top\r\ntriangles.Add(vertexIndex+2);\r\ntriangles.Add(vertexIndex+3);\r\ntriangles.Add(vertexIndex);\r\n\/\/ add UV\r\nuvs.Add(Vector2 (0, 0));\r\nuvs.Add(Vector2 (0, 1));\r\nuvs.Add(Vector2 (1, 1));\r\nuvs.Add(Vector2 (1, 0));\r\n}\r\n\r\nif (block == 1 &amp;&amp; bottom == 0)\r\n{\r\nvertexIndex = vertices.Count;\r\nvertices.Add(new Vector3(x, y, z));\r\nvertices.Add(new Vector3(x+1, y, z));\r\nvertices.Add(new Vector3(x+1, y, z+1));\r\nvertices.Add(new Vector3(x, y , z+1));\r\n\/\/ first triangle for the block top\r\ntriangles.Add(vertexIndex);\r\ntriangles.Add(vertexIndex+1);\r\ntriangles.Add(vertexIndex+2);\r\n\/\/ second triangle for the block top\r\ntriangles.Add(vertexIndex+2);\r\ntriangles.Add(vertexIndex+3);\r\ntriangles.Add(vertexIndex);\r\n\/\/ add UV\r\nuvs.Add(Vector2 (0, 0));\r\nuvs.Add(Vector2 (0, 1));\r\nuvs.Add(Vector2 (1, 1));\r\nuvs.Add(Vector2 (1, 0));\r\n}\r\n}\r\n\r\n\/\/ Build the Mesh:\r\nvar mesh : Mesh = GetComponent(MeshFilter).mesh;\r\nmesh.vertices = vertices.ToArray();\r\nmesh.triangles = triangles.ToArray();\r\nmesh.uv = uvs.ToArray();\r\nmesh.RecalculateNormals();\r\n\/\/mesh.Optimize();\r\n\/\/ update mesh collider\r\nGetComponent(MeshCollider).sharedMesh = null;\r\nGetComponent(MeshCollider).sharedMesh = mesh;\r\n}\r\n\r\nfunction Update ()\r\n{\r\n\r\nif (Input.GetMouseButtonUp(0)) \/\/ button released\r\n{\r\n\/\/ linerenderer fx..reset location 1st\r\nlineRenderer.SetPosition(0, Camera.main.transform.position);\r\nlineRenderer.SetPosition(1, Camera.main.transform.position);\r\n}\r\n\r\n\/\/ remove block .. with mouse\r\nif (Input.GetMouseButton(0)) \/\/ button is held down\r\n{\r\nvar ray:Ray = Camera.main.ScreenPointToRay(Input.mousePosition);\r\nvar hit:RaycastHit;\r\nif (Physics.Raycast(ray,hit, 100))\r\n{\r\nvar hx:int = hit.point.x;\r\nvar hy:int = hit.point.y-0.2; \/\/ -0.2 is temporary fix for clicking top plane and getting too big values..\r\nvar hz:int = hit.point.z;\r\n\/\/\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0print (&quot;Hit:&quot;+hx+&quot;,&quot;+hy+&quot;,&quot;+hz);\r\n\/\/\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0Debug.DrawLine (hit.point,hit.point+Vector3(0,1,0), Color.red,1);\r\nlineRenderer.SetPosition(0, Camera.main.transform.position+Vector3(0,-1,0));\r\nlineRenderer.SetPosition(1, hit.point);\r\nchunk&#x5B;hx,hy,hz] = 0;\r\nChunkRender(chunk);\r\n\/\/ particle fx\r\n\/\/Instantiate(explofx, Vector3(hx+0.5,hy+0.5,hz+0.5), Quaternion.identity);\r\n}\r\n}\r\n}<\/pre>\n<p>&#8212;<\/p>\n<p><strong>Screenshots:<\/strong><br \/>\nimage#1 (original)<\/p>\n<p><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" title=\"ChunkRender1\" alt=\"\" src=\"https:\/\/i0.wp.com\/unitycoder.com\/blog\/wp-content\/uploads\/2012\/02\/ChunkRender1.jpg?resize=680%2C487\" width=\"680\" height=\"487\" \/><\/p>\n<p>&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Yay..somebody has started a minecraft-style terrain tutorial here: http:\/\/www.blocksters.com\/node\/57 Converted the ChunkRenderer to unity javascript and it worked!..Oo (see image#1) (now just waiting for the next parts of the tutorial..) While waiting, added other sides for the planes + uv (see main image ^) Its [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":638,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[4,3],"tags":[175,18,144,128,168,171],"class_list":["post-634","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-demos","category-unity3d","tag-chunk","tag-javascript","tag-minecraft","tag-render","tag-terrain","tag-unity"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/unitycoder.com\/blog\/wp-content\/uploads\/2012\/02\/chunk_render2.jpg?fit=680%2C495&ssl=1","jetpack_shortlink":"https:\/\/wp.me\/p1KTaT-ae","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/unitycoder.com\/blog\/wp-json\/wp\/v2\/posts\/634","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unitycoder.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unitycoder.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unitycoder.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unitycoder.com\/blog\/wp-json\/wp\/v2\/comments?post=634"}],"version-history":[{"count":16,"href":"https:\/\/unitycoder.com\/blog\/wp-json\/wp\/v2\/posts\/634\/revisions"}],"predecessor-version":[{"id":639,"href":"https:\/\/unitycoder.com\/blog\/wp-json\/wp\/v2\/posts\/634\/revisions\/639"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/unitycoder.com\/blog\/wp-json\/wp\/v2\/media\/638"}],"wp:attachment":[{"href":"https:\/\/unitycoder.com\/blog\/wp-json\/wp\/v2\/media?parent=634"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unitycoder.com\/blog\/wp-json\/wp\/v2\/categories?post=634"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unitycoder.com\/blog\/wp-json\/wp\/v2\/tags?post=634"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}