3D Spatial hash

Size
2,084 Kb
Views
4,048

How do I make an 3d spatial hash?

What is a 3d spatial hash? How do you make a 3d spatial hash? This script and codes were developed by Sarah Cartwright on 05 December 2022, Monday.

3D Spatial hash Previews

3D Spatial hash - Script Codes HTML Codes

<!DOCTYPE html>
<html >
<head> <meta charset="UTF-8"> <title>3D Spatial hash</title> <link rel="stylesheet" href="css/style.css">
</head>
<body> This Pen does nothing on its own - it is used in other pens. <script src="js/index.js"></script>
</body>
</html>

3D Spatial hash - Script Codes CSS Codes

body { padding: 10px; margin: 0px; background: #2e2e2e; color: #909090; font: 12px arial, sans-serif;
}

3D Spatial hash - Script Codes JS Codes

function spatialHash(cellSize){ this.cellSize = cellSize; this.bucket = []; // Yay for very sparse arrays.
}
spatialHash.prototype.hash3D = function(x, y, z){ var ix = Math.floor((x+1000)/this.cellSize), iy = Math.floor((y+1000)/this.cellSize), iz = Math.floor((z+1000)/this.cellSize); return ((ix * 73856093) ^ (iy * 19349663) ^ (iz * 83492791) ); //% 5000; //5000 is size of hash table.
}
spatialHash.prototype.add = function(x, y, z, obj){ var hash = this.hash3D(x, y, z); if(this.bucket[hash]===undefined) this.bucket[hash] = [obj]; else this.bucket[hash].push(obj);
}
spatialHash.prototype.clear = function(){ this.bucket = [];
}
spatialHash.prototype.getLocal = function(x, y, z){ var nearObjects = []; var o = this.cellSize; for(var xx=-o*2; xx<o*2+o; xx+=o) for(var yy=-o*2; yy<o*2+o; yy+=o) for(var zz=-o*2; zz<o*2+o; zz+=o){ var newObjects = this.bucket[this.hash3D(x+xx, y+yy, z+zz)]; if(newObjects !== undefined) nearObjects = nearObjects.concat(newObjects); } return nearObjects;
}
/*
console.clear();
var SH = new spatialHash(5);
for(var i=0; i<30; i+=2){ SH.add(i,2,3,"TEST"+i);
}
console.log(SH.getLocal(10,2,2));
SH.clear();
console.log(SH.getLocal(17,2,2));
*/
3D Spatial hash - Script Codes
3D Spatial hash - Script Codes
Home Page Home
Developer Sarah Cartwright
Username SarahC
Uploaded December 05, 2022
Rating 3
Size 2,084 Kb
Views 4,048
Do you need developer help for 3D Spatial hash?

Find the perfect freelance services for your business! Fiverr's mission is to change how the world works together. Fiverr connects businesses with freelancers offering digital services in 500+ categories. Find Developer!

Sarah Cartwright (SarahC) Script Codes
Create amazing SEO content with AI!

Jasper is the AI Content Generator that helps you and your team break through creative blocks to create amazing, original content 10X faster. Discover all the ways the Jasper AI Content Platform can help streamline your creative workflows. Start For Free!