Spaces:
Running
Running
Update main.js
Browse files
main.js
CHANGED
|
@@ -5,97 +5,133 @@ const input = document.getElementById("input");
|
|
| 5 |
|
| 6 |
function print(m){ log.innerHTML += m+"<br/>"; log.scrollTop=log.scrollHeight; }
|
| 7 |
|
| 8 |
-
canvas.width = window.innerWidth -
|
| 9 |
canvas.height = window.innerHeight;
|
| 10 |
|
| 11 |
-
//
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
}
|
| 16 |
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
|
|
|
| 20 |
|
| 21 |
-
|
| 22 |
-
|
| 23 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
const vert = `
|
| 25 |
attribute vec2 p;
|
| 26 |
-
void main(){ gl_Position
|
| 27 |
`;
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
gl.
|
| 32 |
-
|
| 33 |
-
return s;
|
| 34 |
}
|
| 35 |
|
| 36 |
-
const prog
|
| 37 |
-
gl.attachShader(prog,
|
| 38 |
-
gl.attachShader(prog,
|
| 39 |
gl.linkProgram(prog);
|
| 40 |
gl.useProgram(prog);
|
| 41 |
|
| 42 |
-
const buf
|
| 43 |
-
gl.bindBuffer(gl.ARRAY_BUFFER,
|
| 44 |
-
gl.bufferData(gl.ARRAY_BUFFER,
|
| 45 |
-
-1,-1, 1,-1, -1,1, 1,1
|
| 46 |
-
]), gl.STATIC_DRAW);
|
| 47 |
|
| 48 |
-
const loc
|
| 49 |
gl.enableVertexAttribArray(loc);
|
| 50 |
gl.vertexAttribPointer(loc,2,gl.FLOAT,false,0,0);
|
| 51 |
|
| 52 |
-
const uRes
|
| 53 |
-
const uTime
|
| 54 |
-
const uPow
|
| 55 |
-
const uEng
|
|
|
|
|
|
|
| 56 |
|
| 57 |
function loop(t){
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
gl.drawArrays(gl.TRIANGLE_STRIP,0,4);
|
| 63 |
requestAnimationFrame(loop);
|
| 64 |
}
|
| 65 |
loop(0);
|
| 66 |
})();
|
| 67 |
|
| 68 |
-
//
|
| 69 |
function handle(cmd){
|
| 70 |
-
const p
|
| 71 |
switch(p[0]){
|
| 72 |
case "help":
|
| 73 |
print("inject <v> | superposition <v>");
|
| 74 |
-
print("
|
|
|
|
| 75 |
break;
|
| 76 |
case "inject":
|
| 77 |
-
energy
|
| 78 |
-
print("Ψ
|
| 79 |
break;
|
| 80 |
case "superposition":
|
| 81 |
-
power
|
| 82 |
-
print("σ
|
| 83 |
break;
|
| 84 |
-
case "
|
| 85 |
-
|
| 86 |
-
print("Ω
|
| 87 |
break;
|
| 88 |
-
case "
|
| 89 |
-
if(
|
| 90 |
-
power
|
| 91 |
-
|
| 92 |
-
|
|
|
|
| 93 |
}
|
| 94 |
break;
|
|
|
|
|
|
|
|
|
|
| 95 |
}
|
| 96 |
}
|
| 97 |
|
| 98 |
-
input.addEventListener("keydown",
|
| 99 |
if(e.key==="Enter"){
|
| 100 |
print("> "+input.value);
|
| 101 |
handle(input.value.trim());
|
|
@@ -103,5 +139,5 @@ input.addEventListener("keydown", e=>{
|
|
| 103 |
}
|
| 104 |
});
|
| 105 |
|
| 106 |
-
print("CodexReality3D
|
| 107 |
print("Type `help`");
|
|
|
|
| 5 |
|
| 6 |
function print(m){ log.innerHTML += m+"<br/>"; log.scrollTop=log.scrollHeight; }
|
| 7 |
|
| 8 |
+
canvas.width = window.innerWidth - 420;
|
| 9 |
canvas.height = window.innerHeight;
|
| 10 |
|
| 11 |
+
// ===== Camera =====
|
| 12 |
+
let camPos = [0, 0, -4];
|
| 13 |
+
let yaw = 0, pitch = 0;
|
| 14 |
+
const keys = {};
|
| 15 |
+
|
| 16 |
+
window.addEventListener("keydown", e=>keys[e.key]=true);
|
| 17 |
+
window.addEventListener("keyup", e=>keys[e.key]=false);
|
| 18 |
+
canvas.addEventListener("mousemove", e=>{
|
| 19 |
+
if(e.buttons===1){
|
| 20 |
+
yaw += e.movementX * 0.002;
|
| 21 |
+
pitch += e.movementY * 0.002;
|
| 22 |
+
}
|
| 23 |
+
});
|
| 24 |
+
|
| 25 |
+
function camRot(){
|
| 26 |
+
const cy=Math.cos(yaw), sy=Math.sin(yaw);
|
| 27 |
+
const cp=Math.cos(pitch), sp=Math.sin(pitch);
|
| 28 |
+
return [
|
| 29 |
+
cy, 0, -sy,
|
| 30 |
+
sy*sp, cp, cy*sp,
|
| 31 |
+
sy*cp, -sp, cy*cp
|
| 32 |
+
];
|
| 33 |
}
|
| 34 |
|
| 35 |
+
// ===== Load Shader =====
|
| 36 |
+
async function loadShader(){
|
| 37 |
+
return await (await fetch("shader.glsl")).text();
|
| 38 |
+
}
|
| 39 |
|
| 40 |
+
let power = 8.0;
|
| 41 |
+
let energy = 1.0;
|
| 42 |
|
| 43 |
+
// Ω branch graph
|
| 44 |
+
const branches = {};
|
| 45 |
+
let currentBranch = "root";
|
| 46 |
+
|
| 47 |
+
(async ()=>{
|
| 48 |
+
const frag = await loadShader();
|
| 49 |
const vert = `
|
| 50 |
attribute vec2 p;
|
| 51 |
+
void main(){ gl_Position=vec4(p,0,1); }
|
| 52 |
`;
|
| 53 |
+
function compile(t,s){
|
| 54 |
+
const sh=gl.createShader(t);
|
| 55 |
+
gl.shaderSource(sh,s);
|
| 56 |
+
gl.compileShader(sh);
|
| 57 |
+
return sh;
|
|
|
|
| 58 |
}
|
| 59 |
|
| 60 |
+
const prog=gl.createProgram();
|
| 61 |
+
gl.attachShader(prog,compile(gl.VERTEX_SHADER,vert));
|
| 62 |
+
gl.attachShader(prog,compile(gl.FRAGMENT_SHADER,frag));
|
| 63 |
gl.linkProgram(prog);
|
| 64 |
gl.useProgram(prog);
|
| 65 |
|
| 66 |
+
const buf=gl.createBuffer();
|
| 67 |
+
gl.bindBuffer(gl.ARRAY_BUFFER,buf);
|
| 68 |
+
gl.bufferData(gl.ARRAY_BUFFER,new Float32Array([-1,-1,1,-1,-1,1,1,1]),gl.STATIC_DRAW);
|
|
|
|
|
|
|
| 69 |
|
| 70 |
+
const loc=gl.getAttribLocation(prog,"p");
|
| 71 |
gl.enableVertexAttribArray(loc);
|
| 72 |
gl.vertexAttribPointer(loc,2,gl.FLOAT,false,0,0);
|
| 73 |
|
| 74 |
+
const uRes=gl.getUniformLocation(prog,"u_res");
|
| 75 |
+
const uTime=gl.getUniformLocation(prog,"u_time");
|
| 76 |
+
const uPow=gl.getUniformLocation(prog,"u_power");
|
| 77 |
+
const uEng=gl.getUniformLocation(prog,"u_energy");
|
| 78 |
+
const uCam=gl.getUniformLocation(prog,"u_camPos");
|
| 79 |
+
const uRot=gl.getUniformLocation(prog,"u_camRot");
|
| 80 |
|
| 81 |
function loop(t){
|
| 82 |
+
if(keys["w"]) camPos[2]+=0.05;
|
| 83 |
+
if(keys["s"]) camPos[2]-=0.05;
|
| 84 |
+
if(keys["a"]) camPos[0]-=0.05;
|
| 85 |
+
if(keys["d"]) camPos[0]+=0.05;
|
| 86 |
+
|
| 87 |
+
gl.uniform2f(uRes,canvas.width,canvas.height);
|
| 88 |
+
gl.uniform1f(uTime,t*0.001);
|
| 89 |
+
gl.uniform1f(uPow,power);
|
| 90 |
+
gl.uniform1f(uEng,energy);
|
| 91 |
+
gl.uniform3fv(uCam,camPos);
|
| 92 |
+
gl.uniformMatrix3fv(uRot,false,new Float32Array(camRot()));
|
| 93 |
gl.drawArrays(gl.TRIANGLE_STRIP,0,4);
|
| 94 |
requestAnimationFrame(loop);
|
| 95 |
}
|
| 96 |
loop(0);
|
| 97 |
})();
|
| 98 |
|
| 99 |
+
// ===== CLI (CCL-v1) =====
|
| 100 |
function handle(cmd){
|
| 101 |
+
const p=cmd.split(" ");
|
| 102 |
switch(p[0]){
|
| 103 |
case "help":
|
| 104 |
print("inject <v> | superposition <v>");
|
| 105 |
+
print("branch <name> | switch <name>");
|
| 106 |
+
print("move x y z");
|
| 107 |
break;
|
| 108 |
case "inject":
|
| 109 |
+
energy=parseFloat(p[1])||energy;
|
| 110 |
+
print("Ψ="+energy);
|
| 111 |
break;
|
| 112 |
case "superposition":
|
| 113 |
+
power=parseFloat(p[1])||power;
|
| 114 |
+
print("σ="+power);
|
| 115 |
break;
|
| 116 |
+
case "branch":
|
| 117 |
+
branches[p[1]]={power,energy,cam:[...camPos]};
|
| 118 |
+
print("Ω branch created");
|
| 119 |
break;
|
| 120 |
+
case "switch":
|
| 121 |
+
if(branches[p[1]]){
|
| 122 |
+
({power,energy}=branches[p[1]]);
|
| 123 |
+
camPos=[...branches[p[1]].cam];
|
| 124 |
+
currentBranch=p[1];
|
| 125 |
+
print("Ω switched");
|
| 126 |
}
|
| 127 |
break;
|
| 128 |
+
case "move":
|
| 129 |
+
camPos=[+p[1],+p[2],+p[3]];
|
| 130 |
+
break;
|
| 131 |
}
|
| 132 |
}
|
| 133 |
|
| 134 |
+
input.addEventListener("keydown",e=>{
|
| 135 |
if(e.key==="Enter"){
|
| 136 |
print("> "+input.value);
|
| 137 |
handle(input.value.trim());
|
|
|
|
| 139 |
}
|
| 140 |
});
|
| 141 |
|
| 142 |
+
print("CodexReality3D v6 — Open World Engine");
|
| 143 |
print("Type `help`");
|