|
1651 | 1651 | })();
|
1652 | 1652 | scene.add(gridMesh); */
|
1653 | 1653 |
|
1654 |
| -worker = (() => { |
1655 |
| - let cbs = []; |
1656 |
| - const worker = new Worker('mc-worker.js'); |
1657 |
| - worker.onmessage = e => { |
1658 |
| - const {data} = e; |
1659 |
| - const {error, result} = data; |
1660 |
| - cbs.shift()(error, result); |
1661 |
| - }; |
1662 |
| - worker.onerror = err => { |
1663 |
| - console.warn(err); |
1664 |
| - }; |
1665 |
| - worker.request = (req, transfers) => new Promise((accept, reject) => { |
1666 |
| - worker.postMessage(req, transfers); |
1667 |
| - |
1668 |
| - cbs.push((err, result) => { |
1669 |
| - if (!err) { |
1670 |
| - accept(result); |
1671 |
| - } else { |
1672 |
| - reject(err); |
1673 |
| - } |
1674 |
| - }); |
1675 |
| - }); |
1676 |
| - return worker; |
1677 |
| -})(); |
1678 | 1654 | const _makeMiningPlaceholderMesh = (x, z, owner) => {
|
1679 |
| - // console.log('make text mesh', token); |
1680 |
| - const mesh = _makeTextMesh('Parcel ' + x + ' : ' + z + ' owned by ' + owner, 30); |
1681 |
| - mesh.position.set(x * PARCEL_SIZE, 1, z * PARCEL_SIZE); |
1682 |
| - // mesh.token = token; |
1683 |
| - mesh.update = () => { |
1684 |
| - // XXX |
1685 |
| - }; |
1686 |
| - mesh.intersect = () => null; |
1687 |
| - mesh.destroy = () => { |
1688 |
| - // XXX |
1689 |
| - }; |
| 1655 | + const mesh = new THREE.Object3D(); |
1690 | 1656 | return mesh;
|
1691 | 1657 | };
|
1692 |
| -const _makeMiningMesh = (x, z/*, token*/) => { |
1693 |
| - const terrainVsh = ` |
1694 |
| - varying vec3 vViewPosition; |
1695 |
| - void main() { |
1696 |
| - vec4 mvPosition = modelMatrix * vec4( position.xyz, 1.0 ); |
1697 |
| - gl_Position = projectionMatrix * modelViewMatrix * vec4( position.xyz, 1.0 ); |
1698 |
| - vViewPosition = mvPosition.xyz; |
1699 |
| - } |
1700 |
| - `; |
1701 |
| - const terrainFsh = ` |
1702 |
| - uniform vec3 uSelect; |
1703 |
| - varying vec3 vViewPosition; |
1704 |
| - vec4 color = vec4(${new THREE.Color(0x9ccc65).toArray().map(n => n.toFixed(8)).join(',')}, 1.0); |
1705 |
| - vec4 color2 = vec4(${new THREE.Color(0xec407a).toArray().map(n => n.toFixed(8)).join(',')}, 1.0); |
1706 |
| - bool inRange(vec3 pos, vec3 minPos, vec3 maxPos) { |
1707 |
| - return pos.x >= minPos.x && |
1708 |
| - pos.y >= minPos.y && |
1709 |
| - pos.z >= minPos.z && |
1710 |
| - pos.x <= maxPos.x && |
1711 |
| - pos.y <= maxPos.y && |
1712 |
| - pos.z <= maxPos.z; |
1713 |
| - } |
1714 |
| - void main() { |
1715 |
| - vec3 fdx = vec3( dFdx( -vViewPosition.x ), dFdx( -vViewPosition.y ), dFdx( -vViewPosition.z ) ); |
1716 |
| - vec3 fdy = vec3( dFdy( -vViewPosition.x ), dFdy( -vViewPosition.y ), dFdy( -vViewPosition.z ) ); |
1717 |
| - vec3 normal = normalize( cross( fdx, fdy ) ); |
1718 |
| - float dotNL = saturate( dot( normal, normalize(vec3(1.0, 1.0, 1.0))) ); |
1719 |
| -
|
1720 |
| - float range = 1.01; |
1721 |
| - // float range = 2.01; |
1722 |
| - vec3 minPos = uSelect - range; |
1723 |
| - vec3 maxPos = minPos + (range*2.); |
1724 |
| - if (inRange(vViewPosition, minPos, maxPos)) { |
1725 |
| - gl_FragColor = color2; |
1726 |
| - } else { |
1727 |
| - gl_FragColor = color; |
1728 |
| - } |
1729 |
| - gl_FragColor.rgb += dotNL * 0.5; |
1730 |
| - } |
1731 |
| - `; |
1732 |
| - |
1733 |
| - const geometry = new THREE.BufferGeometry(); |
1734 |
| - const material = new THREE.ShaderMaterial({ |
1735 |
| - uniforms: { |
1736 |
| - uSelect: { |
1737 |
| - type: 'v3', |
1738 |
| - value: new THREE.Vector3(NaN, NaN, NaN), |
1739 |
| - }, |
1740 |
| - }, |
1741 |
| - vertexShader: terrainVsh, |
1742 |
| - fragmentShader: terrainFsh, |
1743 |
| - extensions: { |
1744 |
| - derivatives: true, |
1745 |
| - }, |
1746 |
| - }); |
1747 |
| - const mesh = new THREE.Mesh(geometry, material); |
1748 |
| - mesh.frustumCulled = false; |
1749 |
| - mesh.visible = false; |
1750 |
| - // mesh.token = token; |
1751 |
| - |
1752 |
| - const size = PARCEL_SIZE; |
1753 |
| - const dims = Float32Array.from([size, size, size]); |
1754 |
| - const potential = new Float32Array(size*size*size); |
1755 |
| - potential.fill(1); |
1756 |
| - for (let x = 0; x < size; x++) { |
1757 |
| - for (let y = 0; y < size; y++) { |
1758 |
| - for (let z = 0; z < size; z++) { |
1759 |
| - if ( |
1760 |
| - x === 0 || y === 0 || z === 0 || |
1761 |
| - x === (size-1) || y === (size-1) || z === (size-1) |
1762 |
| - ) { |
1763 |
| - potential[x + y*size + z*size*size] = 0; |
1764 |
| - } |
1765 |
| - } |
1766 |
| - } |
1767 |
| - } |
1768 |
| - const shift = Float32Array.from([(x-0.5)*size, 0, (z-0.5)*size]); |
1769 |
| - |
1770 |
| - mesh.x = x; |
1771 |
| - mesh.z = z; |
1772 |
| - mesh.potential = potential; |
1773 |
| - mesh.shift = shift; |
1774 |
| - mesh.contains = pos => |
1775 |
| - pos.x >= shift[0] && |
1776 |
| - pos.y >= shift[1] && |
1777 |
| - pos.z >= shift[2] && |
1778 |
| - pos.x < shift[0] + size && |
1779 |
| - pos.y < shift[1] + size && |
1780 |
| - pos.z < shift[2] + size; |
1781 |
| - mesh.getPotential = pos => { |
1782 |
| - const x = pos.x - shift[0]; |
1783 |
| - const y = pos.y - shift[1]; |
1784 |
| - const z = pos.z - shift[2]; |
1785 |
| - return potential[x + y*size*size + z*size]; |
1786 |
| - }; |
1787 |
| - mesh.mine = pos => { |
1788 |
| - const x = Math.floor(pos.x - shift[0]); |
1789 |
| - const y = Math.floor(pos.y - shift[1]); |
1790 |
| - const z = Math.floor(pos.z - shift[2]); |
1791 |
| - const factor = 2; |
1792 |
| - const max = Math.sqrt(factor*factor*3); |
1793 |
| - for (let dx = -factor; dx <= factor; dx++) { |
1794 |
| - for (let dz = -factor; dz <= factor; dz++) { |
1795 |
| - for (let dy = -factor; dy <= factor; dy++) { |
1796 |
| - const ax = x + dx; |
1797 |
| - const ay = y + dy; |
1798 |
| - const az = z + dz; |
1799 |
| - if ( |
1800 |
| - ax >= 0 && |
1801 |
| - ay >= 0 && |
1802 |
| - az >= 0 && |
1803 |
| - ax < size && |
1804 |
| - ay < size && |
1805 |
| - az < size |
1806 |
| - ) { |
1807 |
| - const index = ax + ay*size*size + az*size; |
1808 |
| - const d = (max - Math.sqrt(dx*dx + dy*dy + dz*dz)) / max * 2; |
1809 |
| - potential[index] = Math.max(potential[index] - d, 0); |
1810 |
| - } |
1811 |
| - } |
1812 |
| - } |
1813 |
| - } |
1814 |
| - worker.request({ |
1815 |
| - method: 'march', |
1816 |
| - dims, |
1817 |
| - potential, |
1818 |
| - shift, |
1819 |
| - }).then(res => { |
1820 |
| - geometry.addAttribute('position', new THREE.BufferAttribute(res.positions, 3)); |
1821 |
| - geometry.setIndex(new THREE.BufferAttribute(res.faces, 1)); |
1822 |
| - |
1823 |
| - if (potential.every(n => n <= 0)) { |
1824 |
| - const {x, z} = mesh; |
1825 |
| - fetch('https://djj7y2i5p8.execute-api.us-west-1.amazonaws.com/default/Webaverse', { |
1826 |
| - method: 'POST', |
1827 |
| - body: JSON.stringify({ |
1828 |
| - addr: window.web3.eth.defaultAccount, |
1829 |
| - x, |
1830 |
| - y: z, |
1831 |
| - }), |
1832 |
| - }) |
1833 |
| - .then(res => res.json()) |
1834 |
| - .then(o => { |
1835 |
| - const {v, r, s} = o; |
1836 |
| - const addr = window.web3.eth.defaultAccount; |
1837 |
| - console.log('mintTokenFromSignature', {addr, x, z, v, r, s}); |
1838 |
| - return user.execute({ |
1839 |
| - method: 'mintTokenFromSignature', |
1840 |
| - data: { |
1841 |
| - addr, |
1842 |
| - x, |
1843 |
| - y: z, |
1844 |
| - v, |
1845 |
| - r, |
1846 |
| - s, |
1847 |
| - }, |
1848 |
| - }); |
1849 |
| - }) |
1850 |
| - .then(() => { |
1851 |
| - console.log('mined', {x, z}); |
1852 |
| - |
1853 |
| - for (;;) { |
1854 |
| - const index = sceneMeshes.findIndex(sceneMesh => sceneMesh.token.coords.some(coord => coord[0] === x && coord[1] === z)); |
1855 |
| - if (index !== -1) { |
1856 |
| - const sceneMesh = sceneMeshes[index]; |
1857 |
| - sceneMesh.destroy(); |
1858 |
| - scene.remove(sceneMesh); |
1859 |
| - sceneMeshes.splice(index, 1); |
1860 |
| - } else { |
1861 |
| - break; |
1862 |
| - } |
1863 |
| - } |
1864 |
| - |
1865 |
| - const sceneMesh = _makeSceneObjectMesh(); |
1866 |
| - sceneMesh.token = { |
1867 |
| - id: 0, |
1868 |
| - coords: [[x, z]], |
1869 |
| - apps: [], |
1870 |
| - owner: _getUserAddress(), |
1871 |
| - }; |
1872 |
| - scene.add(sceneMesh); |
1873 |
| - sceneMeshes.push(sceneMesh); |
1874 |
| - |
1875 |
| - selectedSceneToken = null; |
1876 |
| - |
1877 |
| - lastSceneCoords[0] = NaN; |
1878 |
| - lastSceneCoords[1] = NaN; |
1879 |
| - }); |
1880 |
| - } |
1881 |
| - }); |
1882 |
| - }; |
1883 |
| - mesh.destroy = () => { |
1884 |
| - geometry.dispose(); |
1885 |
| - material.dispose(); |
1886 |
| - }; |
1887 |
| - let colliding = false; |
1888 |
| - mesh.update = () => { |
1889 |
| - if (!colliding && geometry.attributes.position) { |
1890 |
| - colliding = true; |
1891 |
| - |
1892 |
| - const controllerMesh = controllerMeshes[1]; // XXX make this work for all controllers |
1893 |
| - worker.request({ |
1894 |
| - method: 'collide', |
1895 |
| - positions: geometry.attributes.position.array, |
1896 |
| - indices: geometry.index.array, |
1897 |
| - origin: controllerMesh.ray.origin.toArray(new Float32Array(3)), |
1898 |
| - direction: controllerMesh.ray.direction.toArray(new Float32Array(3)), |
1899 |
| - }) |
1900 |
| - .then(collision => { |
1901 |
| - material.uniforms.uSelect.value.fromArray(collision); |
1902 |
| - }) |
1903 |
| - .catch(err => { |
1904 |
| - console.warn(err.stack); |
1905 |
| - }) |
1906 |
| - .finally(() => { |
1907 |
| - colliding = false; |
1908 |
| - }); |
1909 |
| - } |
1910 |
| - }; |
1911 |
| - mesh.intersect = ray => { |
1912 |
| - if (isFinite(material.uniforms.uSelect.value.x)) { |
1913 |
| - const intersectionPoint = material.uniforms.uSelect.value.clone(); |
1914 |
| - const distance = ray.origin.distanceTo(intersectionPoint); |
1915 |
| - return { |
1916 |
| - type: 'mine', |
1917 |
| - mesh, |
1918 |
| - intersectionPoint, |
1919 |
| - distance, |
1920 |
| - }; |
1921 |
| - } else { |
1922 |
| - return null; |
1923 |
| - } |
1924 |
| - }; |
1925 |
| - |
1926 |
| - worker.request({ |
1927 |
| - method: 'march', |
1928 |
| - dims, |
1929 |
| - potential, |
1930 |
| - shift, |
1931 |
| - }).then(res => { |
1932 |
| - geometry.addAttribute('position', new THREE.BufferAttribute(res.positions, 3)); |
1933 |
| - geometry.setIndex(new THREE.BufferAttribute(res.faces, 1)); |
1934 |
| - mesh.visible = true; |
1935 |
| - }); |
1936 |
| - |
| 1658 | +const _makeMiningMesh = (x, z) => { |
| 1659 | + const mesh = new THREE.Object3D(); |
1937 | 1660 | return mesh;
|
1938 | 1661 | };
|
1939 | 1662 | let data;
|
|
0 commit comments