Files
ploxion-nexus3d/app.blade.php

133 lines
12 KiB
PHP

{{-- PL0XI0N nexus3d — le Nexus dans un VRAI moteur 3D (RS-7 : « un moteur graphique
meilleur » → WebGL/three.js, pas d'Unity, ~600 Ko self-hosté). MODÈLE OBSERVATEUR
(RS-7) : on OBSERVE le monde, les capteurs servent UNIQUEMENT à regarder autour (pas
à se déplacer) ; le déplacement = à pied (réel) ou via commandes. Vues : 🧊 3D (orbite,
grande FOV, regarder autour) · 🔝 dessus · ▦ face · 🟰 une seule couche / tout.
Terrain voxel seedé (xerboxion) + arbres. three.js same-origin (/vendor/three.min.js).
Contrat jOSApps. --}}
<style>
.n3{height:100%;position:relative;background:#0a1018;overflow:hidden;touch-action:none}
.n3 canvas{position:absolute;inset:0;width:100%;height:100%;display:block}
.n3__bar{position:absolute;top:8px;left:50%;transform:translateX(-50%);z-index:4;display:flex;gap:5px;flex-wrap:wrap;justify-content:center;max-width:96%}
.n3__b{background:rgba(8,14,22,.72);border:1px solid rgba(95,168,255,.4);color:#bfeaff;border-radius:9px;padding:8px 11px;cursor:pointer;font:inherit;font-size:.74rem;white-space:nowrap}
.n3__b.on{background:#1f6f9f;color:#eafcff;font-weight:700;border:0}
.n3__lay{position:absolute;left:10px;bottom:14px;z-index:4;display:none;align-items:center;gap:6px;background:rgba(8,14,22,.72);border:1px solid rgba(95,168,255,.3);border-radius:9px;padding:6px 9px;color:#bfeaff;font-size:.7rem}
.n3__lay.on{display:flex}
.n3__hint{position:absolute;bottom:14px;right:12px;z-index:3;font-size:.62rem;color:rgba(223,251,233,.5);pointer-events:none;text-align:right;line-height:1.5}
.n3__loading{position:absolute;inset:0;display:flex;align-items:center;justify-content:center;color:#7fe9ff;font-family:ui-monospace,monospace;font-size:.8rem;z-index:5}
</style>
<script>
(function(){
'use strict';
var ID='nexus3d', SEEDNAME='xerboxion';
function javaHash(s){ var h=0; for(var i=0;i<s.length;i++){ h=(Math.imul(31,h)+s.charCodeAt(i))|0; } return h; }
function loadThree(cb){ if(window.THREE) return cb(window.THREE);
var s=document.querySelector('script[data-three]'); if(s){ s.addEventListener('load',function(){cb(window.THREE);}); s.addEventListener('error',function(){cb(null);}); return; }
s=document.createElement('script'); s.src='/vendor/three.min.js'; s.setAttribute('data-three','1');
s.onload=function(){ cb(window.THREE); }; s.onerror=function(){ cb(null); }; document.head.appendChild(s); }
function mount(container){
var alive=true, raf=0, renderer=null, api={};
container.innerHTML='<div class="n3"><div class="n3__loading" data-load>chargement du moteur 3D…</div>'
+'<div class="n3__bar" style="display:none" data-bar>'
+'<button class="n3__b on" data-view="3d">🧊 3D</button>'
+'<button class="n3__b" data-view="top">🔝 dessus</button>'
+'<button class="n3__b" data-view="front">▦ face</button>'
+'<button class="n3__b" data-layer>🟰 couche</button>'
+'<button class="n3__b" data-sensors>📱 capteurs</button>'
+'</div>'
+'<div class="n3__lay" data-laybox>couche <input type="range" min="0" max="8" value="3" data-layval style="width:90px"> <b data-laynum>3</b></div>'
+'<div class="n3__hint" data-hint style="display:none">observateur · glisse / 📱 = regarder autour<br>déplacement : à pied ou commandes</div>'
+'</div>';
var root=container.querySelector('.n3');
loadThree(function(THREE){ if(!alive) return;
if(!THREE){ root.querySelector('[data-load]').textContent='moteur 3D indisponible (three.js non chargé)'; return; }
root.querySelector('[data-load]').style.display='none';
root.querySelector('[data-bar]').style.display='flex';
root.querySelector('[data-hint]').style.display='block';
build3D(THREE);
});
function build3D(THREE){
var N=28, MAXH=8, seed=(javaHash(SEEDNAME)>>>0), CENTER=new THREE.Vector3(0, MAXH*0.4, 0);
function rand2(x,y,s){ var n=Math.imul(x,374761393)+Math.imul(y,668265263)+Math.imul(s,982451653); n=Math.imul(n^(n>>>13),1274126177); n=n^(n>>>16); return (n>>>0)/4294967295; }
function sm(t){ return t*t*(3-2*t); }
function vn(x,y,s){ var xi=Math.floor(x),yi=Math.floor(y),xf=x-xi,yf=y-yi,a=rand2(xi,yi,s),b=rand2(xi+1,yi,s),c=rand2(xi,yi+1,s),d=rand2(xi+1,yi+1,s),u=sm(xf),v=sm(yf); return (a*(1-u)+b*u)*(1-v)+(c*(1-u)+d*u)*v; }
var H=[]; for(var i=0;i<N;i++){ H[i]=[]; for(var j=0;j<N;j++){ var f=0.18, n=vn(i*f,j*f,seed)*0.6+vn(i*f*2.3,j*f*2.3,seed+7)*0.3+vn(i*f*4.7,j*f*4.7,seed+13)*0.1; H[i][j]=Math.max(0,Math.min(MAXH,Math.round(n*MAXH))); } }
function colFor(h){ if(h<=0)return 0x3a6b8c; if(h===1)return 0xddd3a6; if(h<=3)return 0x6fae5f; if(h<=MAXH-1)return 0x8a8a82; return 0xe8eef0; }
var scene=new THREE.Scene(); scene.background=new THREE.Color(0x0e2138); scene.fog=new THREE.Fog(0x0e2138, 26, 70);
scene.add(new THREE.HemisphereLight(0xbfd8ff, 0x20303a, 1.0));
var sun=new THREE.DirectionalLight(0xfff0d0, 0.55); sun.position.set(14,34,10); scene.add(sun);
// --- terrain : InstancedMesh reconstruit selon le mode (tout / une seule couche) ---
var geo=new THREE.BoxGeometry(1,1,1), mat=new THREE.MeshLambertMaterial(), maxInst=N*N;
var inst=new THREE.InstancedMesh(geo, mat, maxInst); inst.count=0; scene.add(inst);
var dummy=new THREE.Object3D(), col=new THREE.Color();
function buildTerrain(layerY){ var k=0;
for(var x=0;x<N;x++)for(var z=0;z<N;z++){ var h=H[x][z];
if(layerY==null){ dummy.position.set(x-N/2, (h+1)/2-0.5, z-N/2); dummy.scale.set(1,h+1,1); dummy.updateMatrix(); inst.setMatrixAt(k,dummy.matrix); col.setHex(colFor(h)); inst.setColorAt(k,col); k++; }
else if(h>=layerY){ dummy.position.set(x-N/2, layerY, z-N/2); dummy.scale.set(1,1,1); dummy.updateMatrix(); inst.setMatrixAt(k,dummy.matrix); col.setHex(colFor(layerY)); inst.setColorAt(k,col); k++; }
}
inst.count=k; inst.instanceMatrix.needsUpdate=true; if(inst.instanceColor) inst.instanceColor.needsUpdate=true;
}
buildTerrain(null);
// --- arbres (RS-7 : « il manque les arbres ») ---
var treeGrp=new THREE.Group(); scene.add(treeGrp);
var trunkG=new THREE.BoxGeometry(0.3,1.4,0.3), trunkM=new THREE.MeshLambertMaterial({color:0x6e5634});
var leafG=new THREE.BoxGeometry(1.7,1.7,1.7), leafM=new THREE.MeshLambertMaterial({color:0x3f8f3f});
var tr=0; for(var t=0;t<240&&tr<24;t++){ var tx=Math.floor(rand2(t,7,seed)*N), tz=Math.floor(rand2(t,13,seed)*N), th=H[tx]&&H[tx][tz]; if(th>=2&&th<=MAXH-1){ var bx=tx-N/2, bz=tz-N/2, by=th+1; var tk=new THREE.Mesh(trunkG,trunkM); tk.position.set(bx,by+0.7,bz); treeGrp.add(tk); var lf=new THREE.Mesh(leafG,leafM); lf.position.set(bx,by+1.95,bz); treeGrp.add(lf); tr++; } }
// --- caméras : 3D (orbite, grande FOV) + dessus (ortho) + face (ortho) ---
var W=function(){return root.clientWidth;}, Hh=function(){return root.clientHeight;};
var persp=new THREE.PerspectiveCamera(85, W()/Math.max(1,Hh()), 0.1, 300);
var ortho=new THREE.OrthographicCamera(-1,1,1,-1,-200,300);
var mode='3d', az=0.7, el=0.6, r=N*1.15, layerY=null, sensors=false, baseYaw=null, cam=persp;
function setOrtho(which){ var s=N*0.62, asp=W()/Math.max(1,Hh()); ortho.left=-s*asp; ortho.right=s*asp; ortho.top=s; ortho.bottom=-s; ortho.updateProjectionMatrix();
if(which==='top'){ ortho.position.set(0,N*1.4,0.001); } else { ortho.position.set(0,MAXH*0.5,N*1.4); }
ortho.lookAt(CENTER); }
function placeOrbit(){ var ce=Math.max(-1.4,Math.min(1.4,el)); persp.position.set(CENTER.x+r*Math.cos(ce)*Math.sin(az), CENTER.y+r*Math.sin(ce), CENTER.z+r*Math.cos(ce)*Math.cos(az)); persp.lookAt(CENTER); }
function apply(){ if(mode==='3d'){ cam=persp; placeOrbit(); } else { cam=ortho; setOrtho(mode); } }
placeOrbit();
// --- contrôles : regarder autour SEULEMENT (observateur, pas de translation par capteur) ---
var drag=null, cv=renderer? null : null;
renderer=new THREE.WebGLRenderer({antialias:true}); renderer.setPixelRatio(Math.min(2,window.devicePixelRatio||1)); renderer.setSize(W(),Hh()); root.appendChild(renderer.domElement); cv=renderer.domElement;
cv.addEventListener('pointerdown',function(e){ drag={x:e.clientX,y:e.clientY,az:az,el:el}; try{cv.setPointerCapture(e.pointerId);}catch(x){} });
cv.addEventListener('pointermove',function(e){ if(!drag||mode!=='3d')return; az=drag.az-(e.clientX-drag.x)*0.006; el=Math.max(-1.4,Math.min(1.4,drag.el+(e.clientY-drag.y)*0.006)); placeOrbit(); });
cv.addEventListener('pointerup',function(){ drag=null; });
cv.addEventListener('wheel',function(e){ if(mode!=='3d')return; e.preventDefault(); r=Math.max(6,Math.min(N*2.2, r+(e.deltaY>0?2:-2))); placeOrbit(); },{passive:false});
// gyro = regarder autour (orientation seule)
function onOrient(e){ if(!sensors||mode!=='3d')return; var y=(typeof e.webkitCompassHeading==='number')?e.webkitCompassHeading:(e.alpha!=null?(360-e.alpha):null); if(y!=null){ if(baseYaw==null)baseYaw=y; az=((y-baseYaw)*Math.PI/180); } if(e.beta!=null){ el=Math.max(-1.4,Math.min(1.4,(e.beta-45)*Math.PI/180*-1)); } placeOrbit(); }
api.onOrient=onOrient;
// --- UI ---
function setView(m){ mode=m; root.querySelectorAll('[data-view]').forEach(function(b){ b.classList.toggle('on', b.getAttribute('data-view')===m); }); apply(); }
root.querySelectorAll('[data-view]').forEach(function(b){ b.addEventListener('click',function(){ setView(b.getAttribute('data-view')); }); });
var laybox=root.querySelector('[data-laybox]'), layBtn=root.querySelector('[data-layer]');
layBtn.addEventListener('click',function(){ if(layerY==null){ layerY=+root.querySelector('[data-layval]').value; laybox.classList.add('on'); layBtn.classList.add('on'); } else { layerY=null; laybox.classList.remove('on'); layBtn.classList.remove('on'); } buildTerrain(layerY); });
root.querySelector('[data-layval]').addEventListener('input',function(e){ root.querySelector('[data-laynum]').textContent=e.target.value; if(layerY!=null){ layerY=+e.target.value; buildTerrain(layerY); } });
root.querySelector('[data-sensors]').addEventListener('click',function(){ var b=this;
if(sensors){ sensors=false; baseYaw=null; b.classList.remove('on'); window.removeEventListener('deviceorientation',onOrient,true); return; }
function go(){ sensors=true; baseYaw=null; b.classList.add('on'); window.addEventListener('deviceorientation',onOrient,true); if(mode!=='3d') setView('3d'); }
if(window.DeviceOrientationEvent && typeof DeviceOrientationEvent.requestPermission==='function'){ DeviceOrientationEvent.requestPermission().then(function(s){ if(s==='granted')go(); }).catch(function(){}); }
else if(window.DeviceOrientationEvent){ go(); }
});
function resize(){ if(!alive||!renderer)return; var w=W(),h=Hh(); persp.aspect=w/Math.max(1,h); persp.updateProjectionMatrix(); if(mode!=='3d') setOrtho(mode); renderer.setSize(w,h); }
try{ api.ro=new ResizeObserver(resize); api.ro.observe(root); }catch(e){ window.addEventListener('resize',resize); }
function loop(){ if(!alive)return; renderer.render(scene,cam); raf=requestAnimationFrame(loop); } loop();
}
return { unmount:function(){ alive=false; if(raf)cancelAnimationFrame(raf);
try{ if(api.onOrient) window.removeEventListener('deviceorientation',api.onOrient,true); if(api.ro)api.ro.disconnect(); }catch(e){}
try{ if(renderer){ renderer.dispose(); renderer.forceContextLoss&&renderer.forceContextLoss(); } }catch(e){}
container.innerHTML=''; } };
}
window.jOSApps=window.jOSApps||{};
window.jOSApps[ID]={manifest:{id:ID,name:'Nexus 3D (WebGL)',icon:'🟩'},mount:mount};
})();
</script>