gpt4 book ai didi

javascript - ThreeJs + AngularJs + OBJLoader 不渲染任何内容,也不显示错误

转载 作者:行者123 更新时间:2023-11-28 04:35:50 26 4
gpt4 key购买 nike

我想将我的基本脚本(运行良好)移至 AngularJs这是我的 Controller 和基本的 html。当我执行此操作时,它不会显示任何错误,只是得到空白页面。在内部,元素获得了信息,但我看不到任何东西。我觉得我错过了一些东西,但我看不到它。

1-当启动 Angular 项目时,我执行 iniciar() ($scope.iniciar())

2-我尝试区分 ThreeJs 的基本对象

3-当一切正常时,我渲染所有内容,但不显示对象(香蕉)

谢谢!

/************ Controller *************/

'use strict';

/**
* @ngdoc function
* @name myApp.controller:CanvasCtrl
* @description
* # CanvasCtrl
* Controller of the myApp
*/

angular.module('myApp')
.controller('CanvasCtrl', function ($rootScope,$scope,config) {

$scope.renderer = null;
$scope.canvas = null;
$scope.camera = null;
$scope.scene = null;
$scope.directionalLight = null;
$scope.objeto = null;
$scope.manager = null;
$scope.loader = null;

$scope.iniciar = function() {

/* SCENE */
$scope.scene = new THREE.Scene();

/* CANVAS */
$scope.renderer = new THREE.WebGLRenderer({ alpha: true });
$scope.canvas = angular.element('#myCanvas');
$scope.renderer.setSize($scope.canvas.width(), 700);


/* CAMARA */
$scope.camera = new THREE.PerspectiveCamera(50, $scope.canvas.width() / 700, 0.1, 10000);
$scope.camera.position.set(0, 0, 500);
$scope.scene.add($scope.camera);

/* DIRECTIONAL LIGHT */
$scope.directionalLight = new THREE.DirectionalLight(0xffffff, 0.8);
$scope.directionalLight.position.set(0, 0, 350);
$scope.directionalLight.lookAt(new THREE.Vector3(0, 0, 0));
$scope.scene.add($scope.directionalLight);

/* Load OBJ */
$scope.loadOBJ();
};


$scope.loadOBJ = function() {

/* LOADING MANAGER */
$scope.manager = new THREE.LoadingManager();
$scope.loader = new THREE.OBJLoader($scope.manager);

/* LOAD OBJ */
$scope.loader.load('http://mamboleoo.be/learnThree/demos/banana.obj', function(object) {
$scope.objeto = object;
$scope.objeto.rotation.x = Math.PI / 2;
$scope.objeto.position.y = -200;
$scope.objeto.position.z = 50;
object.traverse(function(child) {
if (child instanceof THREE.Mesh) {
child.material.color = new THREE.Color(0X00FF00);
child.geometry.computeVertexNormals();
}
});
/* Add object to Scene */
$scope.scene.add($scope.objeto);
$scope.render();

});
};


$scope.render = function() {
requestAnimationFrame($scope.render);
$scope.objeto.rotation.z += 0.01;
$scope.renderer.render($scope.scene, $scope.camera);
};


});

/************HTML****************/

<!doctype html>
<html>
<head>
<some angular's js>
<some css's>

</head>
<body ng-app="myApp" ng-controller="CanvasCtrl" data-spy="scroll" data-target=".navbar" data-offset="60">
<div ng-init="iniciar()">
<canvas class="col-md-12" id="myCanvas"></canvas>
</div>


<script src="http://cdnjs.cloudflare.com/ajax/libs/three.js/r79/three.min.js"></script>
<script src="http://mamboleoo.be/learnThree/demos/OBJLoader.js"></script>
<script src="scripts/controllers/canvasCtrl.js"></script>

</body>
</html>

最佳答案

这段代码会对你有所帮助。

angular.module('myApp', [])
.controller('CanvasCtrl', function($rootScope, $scope) {
$scope.renderer = null;
// $scope.canvas = null;
$scope.camera = null;
$scope.scene = null;
$scope.directionalLight = null;
$scope.objeto = null;
$scope.manager = null;
$scope.loader = null;
$scope.iniciar = function() {

/* SCENE */
$scope.scene = new THREE.Scene();

/* CANVAS */
$scope.renderer = new THREE.WebGLRenderer({ alpha: true });
$scope.canvas = document.getElementById('myCanvas');
$scope.renderer.setSize($scope.canvas.width, 700);

document.body.appendChild($scope.renderer.domElement);
/* CAMARA */
$scope.camera = new THREE.PerspectiveCamera(50, $scope.canvas.width / 700, 0.1, 10000);
$scope.camera.position.set(0, 0, 500);
$scope.scene.add($scope.camera);

/* DIRECTIONAL LIGHT */
$scope.directionalLight = new THREE.DirectionalLight(0xffffff, 0.8);
$scope.directionalLight.position.set(0, 0, 350);
$scope.directionalLight.lookAt(new THREE.Vector3(0, 0, 0));
$scope.scene.add($scope.directionalLight);

/* Load OBJ */
$scope.loadOBJ();
};
$scope.loadOBJ = function() {

/* LOADING MANAGER */
$scope.manager = new THREE.LoadingManager();
$scope.loader = new THREE.OBJLoader($scope.manager);

/* LOAD OBJ */
$scope.loader.load('http://mamboleoo.be/learnThree/demos/banana.obj', function(object) {
$scope.objeto = object;
$scope.objeto.rotation.x = Math.PI / 2;
$scope.objeto.position.y = -200;
$scope.objeto.position.z = 50;
object.traverse(function(child) {
if (child instanceof THREE.Mesh) {
child.material.color = new THREE.Color(0X00FF00);
child.geometry.computeVertexNormals();
}
});
/* Add object to Scene */
$scope.scene.add($scope.objeto);
$scope.render();


});
};
$scope.render = function() {
requestAnimationFrame($scope.render);
$scope.objeto.rotation.z += 0.01;
$scope.renderer.render($scope.scene, $scope.camera);
};
});

关于javascript - ThreeJs + AngularJs + OBJLoader 不渲染任何内容,也不显示错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44219962/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com