Files
cocos4/playground/simple.html
T
2019-01-14 17:11:03 +08:00

76 lines
2.1 KiB
HTML

<!DOCTYPE html>
<html lang="en">
<head>
<title>spec-scroll-view</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" width="256" height="256">
<img name="testImg" src="cubetexture.png" />
</canvas>
<script src="../bin/cocos-3d.dev.js"></script>
<script>
function createTexture() {
const imgElms = cc.game.canvas.getElementsByTagName('img');
const textureAsset = new cc.Texture2D();
textureAsset.image = new cc.ImageAsset(imgElms[0]);
return textureAsset;
}
function onStart() {
const $body = document.body;
const bcr = $body.getBoundingClientRect();
cc.view.setCanvasSize(bcr.width, bcr.height);
window.addEventListener('resize', () => {
const bcr = $body.getBoundingClientRect();
cc.view.setCanvasSize(bcr.width, bcr.height);
});
const root = cc.director.root;
const scene = new cc.Scene();
const cameraNode = new cc.Node();
cameraNode.parent = scene;
const camComp = cameraNode.addComponent(cc.CameraComponent);
const mainWindow = root.mainWindow;
const material = new cc.Material();
material.effectName = 'test'; // parsed effect file is embedded in cocos/3d/builtin/effects.js
material.setProperty('u_color', cc.color('#ff0000'));
material.setProperty('u_sampler', createTexture());
material.passes[0].update();
const modelNode = new cc.Node();
modelNode.parent = scene;
const modelCom = modelNode.addComponent('cc.ModelComponent');
modelCom.material = material;
modelCom.mesh = cc.utils.createMesh(cc.game._gfxDevice, cc.primitives.box());
modelNode.setRotationFromEuler(45, 45, 45);
cc.director.runSceneImmediate(scene);
}
cc.game.run({ id: 'canvas' }, onStart);
</script>
</body>
</html>