mirror of
https://github.com/cocos/cocos4.git
synced 2026-11-20 23:52:40 +08:00
171 lines
5.9 KiB
HTML
171 lines
5.9 KiB
HTML
<!DOCTYPE html>
|
|
<html lang="en">
|
|
|
|
<head>
|
|
<title>simple</title>
|
|
<meta charset="utf-8">
|
|
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
|
|
<style>
|
|
body {
|
|
background-color: #333;
|
|
margin: 0px;
|
|
overflow: hidden;
|
|
}
|
|
|
|
.fit {
|
|
position: absolute;
|
|
top: 0;
|
|
bottom: 0;
|
|
left: 0;
|
|
right: 0;
|
|
}
|
|
</style>
|
|
</head>
|
|
|
|
<body class="fit">
|
|
<canvas id="canvas" tabindex="-1" class="fit">
|
|
<img name="testImg" src="cubetexture.png" />
|
|
<!-- <img name="testImg" src="environment_0.hdr.png" />
|
|
<img name="testImg" src="environment_1.hdr.png" />
|
|
<img name="testImg" src="environment_2.hdr.png" />
|
|
<img name="testImg" src="environment_3.hdr.png" />
|
|
<img name="testImg" src="environment_4.hdr.png" />
|
|
<img name="testImg" src="environment_5.hdr.png" /> -->
|
|
</canvas>
|
|
|
|
<script src="../bin/cocos-3d.dev.js"></script>
|
|
<script src="./camera.js"></script>
|
|
<script>
|
|
const $body = document.body;
|
|
const bcr = $body.getBoundingClientRect();
|
|
const canv = document.getElementById('canvas');
|
|
canv.width = bcr.width;
|
|
canv.height = bcr.height;
|
|
|
|
window.addEventListener('resize', () => {
|
|
const bcr = $body.getBoundingClientRect();
|
|
cc.director.root.resize(bcr.width, bcr.height);
|
|
});
|
|
|
|
const imgs = canv.getElementsByTagName('img');
|
|
function createTexture(idx) {
|
|
const textureAsset = new cc.Texture2D();
|
|
textureAsset.image = new cc.ImageAsset(imgs[idx]);
|
|
return textureAsset;
|
|
}
|
|
|
|
const geomList = ['box', 'sphere', 'cylinder', 'cone', 'torus', 'capsule'];
|
|
function onStart() {
|
|
const root = cc.director.root;
|
|
const scene = new cc.Scene();
|
|
|
|
const cameraNode = new cc.Node('Camera');
|
|
cameraNode.parent = scene;
|
|
cameraNode.setPosition(1, 1, 4);
|
|
cameraNode.setRotationFromEuler(0, 30, 0);
|
|
cameraNode.addComponent(cc.CameraComponent);
|
|
cameraNode.addComponent(FirstPersonCamera);
|
|
|
|
// const skybox = scene.globals.skybox;
|
|
// const textureCube = new cc.TextureCube();
|
|
// textureCube.image = {
|
|
// right: new cc.ImageAsset(imgs[1]),
|
|
// left: new cc.ImageAsset(imgs[2]),
|
|
// top: new cc.ImageAsset(imgs[3]),
|
|
// bottom: new cc.ImageAsset(imgs[4]),
|
|
// front: new cc.ImageAsset(imgs[5]),
|
|
// back: new cc.ImageAsset(imgs[6]),
|
|
// };
|
|
// skybox.isRGBE = true;
|
|
// skybox.cubemap = textureCube;
|
|
// skybox.enabled = true;
|
|
|
|
const lightNode = new cc.Node('Light');
|
|
lightNode.parent = scene;
|
|
lightNode.setRotationFromEuler(70, 0, 0);
|
|
const lightComp = lightNode.addComponent(cc.LightComponent);
|
|
|
|
// parsed effect file is embedded in cocos/3d/builtin/effects.js
|
|
const material = new cc.Material();
|
|
material.initialize({
|
|
//effectName: 'builtin-unlit',
|
|
effectName: 'builtin-standard',
|
|
//defines: { USE_COLOR: true },
|
|
});
|
|
|
|
const len = 50;
|
|
const models = [], passes = [];
|
|
const color = cc.color();
|
|
for (let i = 0; i < len; i++) {
|
|
const modelNode = new cc.Node(`${i}`);
|
|
modelNode.parent = scene;
|
|
const modelCom = modelNode.addComponent('cc.ModelComponent');
|
|
modelCom.material = material;
|
|
modelCom.mesh = cc.utils.createMesh(cc.primitives[geomList[i % geomList.length]]());
|
|
modelNode.setPosition(0, 0, i * 2);
|
|
models.push(modelNode);
|
|
// material instance initialized here
|
|
passes.push(modelCom.material.passes[0]);
|
|
}
|
|
//const handle = passes[0].getHandle('color');
|
|
const handle = passes[0].getHandle('albedo');
|
|
|
|
/*
|
|
{
|
|
const material = new cc.Material();
|
|
material.initialize({ effectName: 'builtin-sprite' });
|
|
material.setProperty('mainTexture', createTexture(0));
|
|
const modelNode = new cc.Node('test');
|
|
modelNode.parent = scene;
|
|
const modelCom = modelNode.addComponent('cc.ModelComponent');
|
|
modelCom.material = material;
|
|
const shape = cc.primitives.quad();
|
|
shape.colors = Array(shape.uvs.length / 2).fill(0)
|
|
.map((t) => [Math.random(), Math.random(), Math.random(), 0.8])
|
|
.reduce((c, a) => a.concat(c), []);
|
|
modelCom.mesh = cc.utils.createMesh(shape);
|
|
}
|
|
*/
|
|
|
|
const material2 = new cc.Material();
|
|
material2.initialize({ effectName: 'builtin-standard' });
|
|
const modelNode2 = new cc.Node('test2');
|
|
modelNode2.parent = scene;
|
|
const modelCom2 = modelNode2.addComponent('cc.ModelComponent');
|
|
modelCom2.material = material2;
|
|
const shape2 = cc.primitives.sphere();
|
|
modelCom2.mesh = cc.utils.createMesh(shape2);
|
|
|
|
let buttonDown = false, pos = cc.v2();
|
|
canv.addEventListener('mousedown', (e) => buttonDown = (!e.button) ? true : buttonDown);
|
|
canv.addEventListener('mouseup', (e) => buttonDown = (!e.button) ? false : buttonDown);
|
|
canv.addEventListener('mousemove', (e) => (pos.x = e.x, pos.y = e.y));
|
|
|
|
cc.director.on(cc.Director.EVENT_AFTER_UPDATE, () => {
|
|
/*
|
|
if (buttonDown) {
|
|
const ray = camComp.screenPointToRay(pos.x, canv.height - pos.y);
|
|
const res = cc.director._scene._renderScene.raycast(ray);
|
|
if (res.length) res.forEach((r) => console.log(r.node.name, r.distance));
|
|
}
|
|
*/
|
|
models.forEach((m, i) => {
|
|
const dt = cc.director._deltaTime;
|
|
const t = cc.director._totalFrames / (60);
|
|
const rad = i * Math.PI * 8 / len;
|
|
const x = Math.cos(rad + t), y = Math.sin(rad + t);
|
|
let z = m._lpos.z + dt; if (z > len * 2) z = 0;
|
|
m.setPosition(x * 2, y * 2, z);
|
|
color.fromHSV((x + 1) * 0.5, 0.5, 1);
|
|
passes[i].setUniform(handle, color);
|
|
});
|
|
});
|
|
|
|
cc.director.runSceneImmediate(scene);
|
|
}
|
|
cc.game.run({ id: 'canvas' }, onStart);
|
|
</script>
|
|
</body>
|
|
|
|
</html>
|