大家好,又见面了,我是你们的朋友全栈君。
看完这本书《3D Game Programing for Kids》之后,发现3D游戏的学习,也可以使用javascript来写的。
先要上这个网站https://threejs.org,然后下载它的three.js源码放到一个目录,比如js。
然后放入这段代码:
代码语言:javascript复制<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8>
<title>My first three.js app</title>
<style>
body { margin: 0; }
canvas { width: 100%; height: 100% }
</style>
</head>
<body>
<script src="js/three.js"></script>
<script>
var scene = new THREE.Scene();
var camera = new THREE.PerspectiveCamera( 75, window.innerWidth/window.innerHeight, 0.1, 1000 );
var renderer = new THREE.WebGLRenderer();
renderer.setSize( window.innerWidth, window.innerHeight );
document.body.appendChild( renderer.domElement );
var geometry = new THREE.BoxGeometry( 1, 1, 1 );
var material = new THREE.MeshBasicMaterial( { color: 0x00ff00 } );
var cube = new THREE.Mesh( geometry, material );
scene.add( cube );
camera.position.z = 5;
var animate = function () {
requestAnimationFrame( animate );
cube.rotation.x = 0.1;
cube.rotation.y = 0.1;
renderer.render(scene, camera);
};
animate();
</script>
</body>
</html>
保存之后,再使用CHROME浏览器打开,就可以看到下面的画面:
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/141384.html原文链接:https://javaforall.cn