Skip to content

Update index.html #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
122 changes: 66 additions & 56 deletions index.html
Original file line number Diff line number Diff line change
@@ -1,67 +1,77 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<meta name="viewport" Content ="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no" />
<head>
<meta charset="utf-8" />
<meta name="viewport" Content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no" />
<title>WebMC</title>
<link id="favicon" rel="icon" href="#" type="image/x-icon" />
<link rel="stylesheet" type="text/css" href="./mc.css" />
<script src="./src/main.js" type="module"></script>
<style id="mcpage-preload">
mcpage-preload {
position: absolute; top: 0;
width: 100vw;
text-align: center;
height: 100vh;
overflow: hidden;
background: #1D1F21;
transition: opacity 1s ease;
z-index: 100000;
}
mcpage-preload p {
padding-top: calc(40vh + 1em);
margin-top: 0;
animation: loading-blink 1.3s infinite ease-out;
}
@keyframes loading-blink {
50% { color: transparent; }
}
mcpage-preload img {
position: fixed;
bottom: 60%; left: 50%;
transform: translateX(-50%);
}
mcpage-preload slot > ul, ::slotted(ul) {
list-style: none;
margin: 20px;
padding: 0;
white-space: nowrap;
text-overflow: ellipsis;
overflow: hidden;
line-height: 1.2em;
}
mcpage-preload .left {
text-align: start;
direction: rtl;
width: 50%;
float: left;
}
mcpage-preload .right {
text-align: start;
width: 50%;
float: right;
}
mcpage-preload {
position: absolute; top: 0;
width: 100vw;
text-align: center;
height: 100vh;
overflow: hidden;
background: #1D1F21;
transition: opacity 1s ease;
z-index: 100000;
}
mcpage-preload p {
padding-top: calc(40vh + 1em);
margin-top: 0;
animation: loading-blink 1.3s infinite ease-out;
}
@keyframes loading-blink {
50% { color: transparent; }
}
mcpage-preload img {
position: fixed;
bottom: 60%; left: 50%;
transform: translateX(-50%);
}
mcpage-preload slot > ul, ::slotted(ul) {
list-style: none;
margin: 20px;
padding: 0;
white-space: nowrap;
text-overflow: ellipsis;
overflow: hidden;
line-height: 1.2em;
}
mcpage-preload .left {
text-align: start;
direction: rtl;
width: 50%;
float: left;
}
mcpage-preload .right {
text-align: start;
width: 50%;
float: right;
}
</style>
</head>
<body>
</head>
<body>
<mcpage-preload>
<img src="./texture/jumpingBlock.gif" />
<p>Loading...</p>
<div><progress value="0" max="0"></progress>&emsp;<span class="loadingCount">0</span> / <span class="loadedCount">0</span></div>
<div class="left"><slot name="loading"><ul></ul></slot></div>
<div class="right"><slot name="loaded"><ul></ul></slot></div>
<ul slot="loading"></ul>
<ul slot="loaded"></ul>
<img src="./texture/jumpingBlock.gif" />
<p>Loading...</p>
<div>
<progress value="0" max="0"></progress>&emsp;<span class="loadingCount">0</span> / <span class="loadedCount">0</span>
</div>
<div class="left">
<slot name="loading">
<ul></ul>
</slot>
</div>
<div class="right">
<slot name="loaded">
<ul></ul>
</slot>
</div>
<ul slot="loading"></ul>
<ul slot="loaded"></ul>
</mcpage-preload>
</body>
</body>
</html>
22 changes: 13 additions & 9 deletions src/Entity/PlayerLocalController.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ class PlayerLocalController extends EntityController {
pm.addEventListener("pause=>play", this["pause=>play"]);
pm.addEventListener("play=>pause", this["play=>pause"]);
};
// 转换成键盘消息 复用键盘的处理逻辑
setMoveBtns(moveBtns = null) {
if (this.moveBtns) {
for (let btn of "up,left,down,right,jump,upleft,upright,flyup,flydown,fly,sneak".split(",")) {
Expand Down Expand Up @@ -133,6 +134,17 @@ class PlayerLocalController extends EntityController {
if (type in this) this[type](event);
this.dispatchEvent(type, event);
};
getHitting(bType = Block.renderType.NORMAL) {
let entity = this.entity,
world = entity.world,
start = entity.getEyePosition(),
end = entity.getDirection(20);
vec3.add(start, end, end);
return world.rayTraceBlock(start, end, (x, y, z) => {
let b = world.getBlock(x, y, z);
return b && b.name !== "air" && b.renderType === bType;
});
};
_setEntityPitchAndYaw(movementX, movementY) {
let i = this.mousemoveSensitivity * (Math.PI / 180);
// movementX left- right+ movementY up- down+
Expand All @@ -159,15 +171,7 @@ class PlayerLocalController extends EntityController {
if (e.button === 0) this.mouseRightBtnDown = true;
if (e.button === 2) this.mouseLeftBtnDown = true;
const destroyOrPlaceBlock = () => {
let entity = this.entity,
world = entity.world,
start = entity.getEyePosition(),
end = entity.getDirection(20);
vec3.add(start, end, end);
let hit = world.rayTraceBlock(start, end, (x, y, z) => {
let b = world.getBlock(x, y, z);
return b && b.name !== "air";
});
const world = this.entity.world, hit = this.getHitting();
if (hit === null || hit.axis === "") return;
let pos = hit.blockPos;
if (this.mouseLeftBtnDown) {
Expand Down
23 changes: 14 additions & 9 deletions src/Renderer/HighlightSelectedBlock.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import { Block } from "../World/Block.js";
import { vec3, mat4 } from "../utils/gmath.js";
import * as glsl from "./glsl.js";

class HighlightSelectedBlock {
constructor(world, renderer = world.renderer) {
this.world = world;
this.renderer = renderer;
this.mvp = mat4.identity();
const {ctx} = renderer;
this.setRenderer(renderer);
};
#calcMesh() {
const {renderer} = this, {ctx} = this.renderer;
this.meshs = new Map();
for (let renderType of Object.values(Block.renderType)) {
let isFluid = renderType === Block.renderType.FLUID;
Expand Down Expand Up @@ -42,16 +45,18 @@ class HighlightSelectedBlock {
});
}
};
setRenderer(renderer = null) {
if (!renderer) return;
// to free buffer
if (this.renderer) this.dispose();
this.renderer = renderer;
renderer.createProgram("selector", glsl.selector.vert, glsl.selector.frag);
this.#calcMesh();
};
draw() {
const {world} = this, {mainPlayer} = world;
if (mainPlayer.camera === null) return;
let start = mainPlayer.getEyePosition(),
end = mainPlayer.getDirection(20);
vec3.add(start, end, end);
let hit = world.rayTraceBlock(start, end, (x, y, z) => {
let b = world.getBlock(x, y, z);
return b && b.name !== "air";
});
const hit = mainPlayer.controller.getHitting?.() ?? null;
if (hit === null) return;

const {renderer} = this, {ctx} = renderer;
Expand Down
9 changes: 8 additions & 1 deletion src/Renderer/WorldChunkModule.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// 计算顶点,材质坐标,下标
// 负责计算顶点,材质坐标,以及区块的绘制工作
import { Block } from "../World/Block.js";
import {
Chunk,
Expand All @@ -7,6 +7,7 @@ import {
CHUNK_Z_SIZE as Z_SIZE,
} from "../World/Chunk.js";
import { manhattanDis } from "../utils/gmath.js";
import * as glsl from "./glsl.js";

const rxyz2int = Chunk.getLinearBlockIndex;

Expand Down Expand Up @@ -58,6 +59,12 @@ class ChunksModule {
setRenderer(renderer = null) {
if (!renderer) return;
this.renderer = renderer;
if (renderer.isWebGL2)
renderer.createProgram("showBlock", glsl.showBlock_webgl2.vert, glsl.showBlock_webgl2.frag)
.use().bindTex("blockTex", renderer.createTextureArray(Block.defaultBlockTextureImg));
else
renderer.createProgram("showBlock", glsl.showBlock.vert, glsl.showBlock.frag)
.use().bindTex("blockTex", renderer.createTexture(Block.defaultBlockTextureImg));
this.updateMeshs();
};
buildChunkModule(chunkKey) {
Expand Down
7 changes: 0 additions & 7 deletions src/Renderer/WorldRenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,6 @@ class WorldRenderer extends Render {
ctx.frontFace(ctx.CCW);
this.mainCamera = new Camera(this.aspectRatio, { fovy: 60, pitch: -90 * Math.PI / 180, position: [0, 20, 0] });
this.addCamera(this.mainCamera);
if (this.isWebGL2)
this.createProgram("showBlock", glsl.showBlock_webgl2.vert, glsl.showBlock_webgl2.frag)
.use().bindTex("blockTex", this.createTextureArray(Block.defaultBlockTextureImg));
else
this.createProgram("showBlock", glsl.showBlock.vert, glsl.showBlock.frag)
.use().bindTex("blockTex", this.createTexture(Block.defaultBlockTextureImg));
this.createProgram("selector", glsl.selector.vert, glsl.selector.frag);
if (world !== null) this.setWorld(world);
};
setWorld(world) {
Expand Down
8 changes: 1 addition & 7 deletions src/World/World.js
Original file line number Diff line number Diff line change
Expand Up @@ -234,13 +234,7 @@ class World extends EventDispatcher {

const {mainPlayer} = this;

let start = mainPlayer.getEyePosition(),
end = mainPlayer.getDirection(20);
vec3.add(start, end, end);
let hit = this.rayTraceBlock(start, end, (x, y, z) => {
let b = this.getBlock(x, y, z);
return b && b.name !== "air";
});
const hit = mainPlayer.controller.getHitting?.() ?? null;
let block = hit? this.getBlock(...hit.blockPos): null;
let longID = hit? this.getTile(...hit.blockPos): null;
let chunk = this.getChunkByBlockXYZ(...[...mainPlayer.position].map(n => n < 0? n - 1: n));
Expand Down