编辑:
我制作了一个 WebGL 指令的完整示例,它使用带绑定的three.js 来调整对象的大小或更改它的材质类型。此外,窗口大小调整和鼠标移动等事件:
是的,这很有可能。除了菜单、排行榜等之外,您还可以将 canvas 包装到指令中。
- 控制器将设置游戏状态
- 将此状态、从服务器加载的数据以及您可能拥有的任何其他数据传递给指令
- 最后在指令链接函数中初始化画布
我制作了这个小应用程序来帮助我完成一个学校项目:http://callmethey.herokuapp.com/polygons。这是我使用的指令(画布部分使用了三个.js):
app.directive('polygon', function() {
return {
restrict: 'A',
scope: {
vertices: '=polygon',
color: '=color'
},
link: function(scope, element, attrs)
{
var camera, scene, renderer;
var polygon;
var targetRotation = 0;
var targetYRotation = 0, targetXRotation = 0;
var targetYRotationOnMouseDown = 0, targetXRotationOnMouseDown = 0;
var mouseX = 0, mouseY = 0;
var mouseXOnMouseDown = 0, mouseYOnMouseDown = 0;
var width = $(element).width();
var height = 200;
var widthHalfX = width/2;
var widthHalfY = height/2;
init();
function init() {
// Setup scene
camera = new THREE.PerspectiveCamera( 70, width / height, 1, 1000 );
camera.position.x = 0;
camera.position.y = 0;
camera.position.z = 300;
scene = new THREE.Scene();
// Build Polygon
var geometry = new THREE.Geometry();
angular.forEach(scope.vertices, function (v) {
geometry.vertices.push( new THREE.Vector3( v.x, v.y, v.z ) );
});
geometry.faces.push( new THREE.Face3(0, 1, 2 ));
THREE.GeometryUtils.center( geometry );
// Push polygon to scene
var material = new THREE.MeshBasicMaterial( { color: cols[scope.color], side: THREE.DoubleSide } );
polygon = new THREE.Mesh( geometry, material );
scene.add(polygon);
renderer = new THREE.WebGLRenderer();
renderer.setSize( width, height );
}
// ..... rest of the code truncated for readability
};
});