Creating a hexagonal grid pattern with the use of texture

I have conducted several experiments in the past to determine the most effective way to create large-scale hexagon grids.

I attempted drawing the hexagons using THREE.Line and THREE.LineSegments. While this method was successful for small grids, the performance took a significant hit when trying to render grids with more than 1000 cells.

As a solution, I came up with the idea of applying a texture (which contains a hexagon grid pattern) to a simple plane. By utilizing the Texture's repeat() function, I was able to specify how many repeats would be performed vertically and horizontally.

https://i.sstatic.net/oukWQ.png

The overall performance improved drastically; however, I observed that the texture only aligned perfectly with the grid cells around the center of the plane.

https://i.sstatic.net/oFJau.png

The dots visible in the grid above represent THREE.Points, which are used to display all corners of each hexagon. This aids me in verifying if the texture matches the grid cells accurately or not.

https://i.sstatic.net/r73J2.png

Upon further inspection, I noticed that as I moved away from the center, the offset between the actual hexagon and its corresponding representation in the texture grew larger.

I believe there could be a more sophisticated approach to pre-calculate the size in order to eliminate any discrepancies between these two grids.

This serves as an example of how the grid appears and how it is generated...

window.onload = function() {
  var renderer, camera, scene, controls, all_loaded = false, controls, tiles = {};

  var imgDataURI = "data:image/jpeg;base64,iVBORw0KGgoAAAANSUhEUgAAAGgAAABaCAYAAABUrhMHAAAFhklEQVR4Xu2dT2gc...

<script src="https://ajax.googleapis.com/ajax/libs/threejs/r76/three.min.js"></script>
<script src="https://dl.dropboxusercontent.com/u/3587259/Code/Threejs/OrbitControls.js"></script>

Answer №1

Roundoff errors are the root cause of the issue you're encountering. Typically, the code that determines the placement of a hexagon does so based on a starting reference point. You can use the code from Calculate 6 vertices of randomely generated hexagon to see how the calculation is done and I can show you the console output as well.

Since this calculation happens dynamically, rounding errors occur, resulting in the browser having to instruct where to render it. The actual drawing of lines is then handed off to the graphics card which ultimately decides which pixel(s) will be used to draw the lines.

var canvas=document.getElementById("canvas");
var ctx=canvas.getContext("2d");

// Draw the original hexagon
ctx.beginPath();
ctx.moveTo(0,0);
...
...
etc...
body{ background-color: ivy; padding:10px; }
canvas{border:1px solid red;}
<h4>Function to draw hexagon with specified radius and left-midpoint.</h4>
<canvas id="canvas" width=300 height=300></canvas>

Sometimes, the hexagons may align perfectly with a full pixel from the texture image, and other times they may not due to the shifting caused by rounding. To address this issue in three.js, ensure that the hexagons you draw match up precisely with the coordinate points on your texture map. This requires creating an offset map for all points on the texture, finding the nearest texture map hexagon, and adjusting accordingly.

Pseudo code:

 center = hexagon.pos();
 ...
 etc...

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Customizing the label styles within MUI's Chip component

Incorporating React with MUI's library has been a seamless experience for me. One of the key MUI components I have integrated is the Chip Within this particular Chip, there lies the label attribute, offering the option to showcase a text f ...

Is it possible to create Firebase real-time database triggers in files other than index.js?

Is it possible to split a large index.js file into multiple files in order to better organize the code? Specifically, can Firebase triggers be written in separate JavaScript files? If so, could you please provide guidance on how to do this properly? child. ...

Problematic redirect following jQuery AJAX request

I am facing an issue with my code where the page is redirected to the index page of my website after the AJAX method completes, but the redirected page appears empty with just a background. function login(){ var uname = document.getElementById("UserNa ...

Map on leaflet with popup next to key

I am facing a scenario where I have a map with a unique legend that is styled either as an SVG or a PNG. The legend is always positioned in the bottom left corner but can be quite large, as users have the option to toggle it on and off. Additionally, the ...

ng-if is executed prior to the specified time

This unique Soundcloud player was created using Plangular, a directive that utilizes AngularJS and the Soundcloud API. Due to certain restrictions with third party apps not being able to stream some Soundcloud tracks, I have implemented ng-if="track" and n ...

"Standard" approach for a Date instance

During my time in the Node REPL environment, the following output was displayed: > console.log(new Date()) 2023-08-15T09:21:45.762Z undefined > console.log(new Date().toString()) Sun Aug 15 2023 09:21:50 GMT+0000 (Coordinated Universal Time) undefine ...

Creating a customized component using unique styles in Material UI version 1

Currently, I am facing a challenge in styling my Card component with a width of 75 pixels while using Redux and Material UI V1. Despite following the example provided by Material UI on custom styling through withStyles and connect, I have not been able to ...

Error: The function clickHandler has been referenced before it was declared or it has already been declared

Since I started using JSLint, I have encountered the common issues of "used before defined" and "is already defined." While I found solutions for some problems, I am currently stuck. Here is a snippet of my code: var foo; foo = addEventListener("click" ...

Retrieve TypeScript object after successful login with Firebase

I'm struggling with the code snippet below: login = (email: string, senha: string): { nome: string, genero: string, foto: string;} => { this.fireAuth.signInWithEmailAndPassword(email, senha).then(res => { firebase.database().ref(&ap ...

Verify if a checkbox is selected upon loading the page

Hey there, I have a jQuery change function set up to turn an input text box grey and read-only when a user selects a checkbox. However, I'm interested in adding a check on page load to see if the checkbox is already selected. Any suggestions for enhan ...

Retrieving the identifier from a value within a Symfony controller

One thing I am looking to achieve is to retrieve an Id based on a folder number. Both the Id and folder number (which is unique) are located in the same controller. I input the folder number from a text box and then need to direct the user to a /Id page. ...

Exploring object-level "this" behavior in jQuery

If I were to implement the following code snippet: const CustomObject = function() { this.showAlert = function() { alert("Hello World!"); } } CustomObject.prototype.bindToElement = function(element) { const self = this; $(element).bind(' ...

Mapping an object in ReactJS: The ultimate guide

When I fetch user information from an API, the data (object) that I receive looks something like this: { "id":"1111", "name":"abcd", "xyz":[ { "a":"a", "b":"b", "c":"c" ...

Unable to initiate the server in a new window due to the absence of a specified terminal application

When incorporating an external library into my React Native project, the installation process is usually smooth. However, when I try to integrate the library into my entire project and run 'react-native run-android' command, it shows an error. I ...

In Javascript, when trying to call Firebase database child(), it may sometimes result in the return value being "

Here is the current setup: Firebase Database: setores: { -KkBgUmX6BEeVyrudlfK: { id: '-KkBgUmX6BEeVyrudlfK', nome: 'test' } -KkDYxfwka8YM6uFOWpH: { id: '-KkDYxfwka8YM6uFOWpH', nome ...

Can Node.js endpoints effectively handle the garbage collection of new class instances?

Just diving into node.js I'm currently dealing with a lengthy and messy function that constructs a CYPHER query for Neo4j. I am considering transforming it into a class, complete with methods, along with a corresponding mocha spec. The expected usag ...

Whenever I change the value of a textbox using plain JavaScript, the text becomes hidden from view

I'm struggling to understand why this function is making the textbox value invisible. I attempted changing the hidden field to a visible field, but that didn't solve the issue. Would appreciate some guidance. Here's the complete page code, p ...

VueJS - repeating input fields for file uploads

I need help removing duplicate items from an array in JavaScript, but when I try to delete one, it always deletes the last occurrence! https://i.sstatic.net/NeJRJ.jpg let app = new Vue({ el: '#app', data: { items: [] }, methods: { ...

Creating the correct JSON structureHere is how you can format JSON

I have a snippet of javascript code that I'm utilizing with casperjs to iterate through links and retrieve data in json format. Below is the code snippet: casper.each(links, function (self, link) { this.thenOpen(link, function () { // obtain w ...

I need help with a process to extract information from a database and convert it into an object specifically for my situation

Currently, I am utilizing the postgres row_to_json function to retrieve data that has been stored using JSON.stringify(). However, upon retrieval and attempting to parse it with JSON.parse(), an error message stating "unexpected token ," is returned. The ...