I attempted to combine AngularJS and three.js, but unfortunately, the geometries are not appearing in the render

(I found this example online, but it's not working as expected). I am attempting to incorporate AngularJS for implementing an MVC architecture for three.js files. Despite not encountering any errors during debugging, I am unable to see the rendered geometry (cube). Here is a snippet of my JS file...

var newApp = angular.module('new', ['ngRoute','newServices']);

newApp.config(['$routeProvider', '$locationProvider', function ($routeProvider,     $locationProvider) {
$routeProvider.
    when('/', {templateUrl:"views/main.html", controller:'mainCtrl'});
$locationProvider.html5Mode(false);
}]);


newApp.controller('mainCtrl', ['$scope', 'renderFactory', function ($scope, renderFactory) {
$scope.text = 'Hello';

init();
function init() {
    renderFactory.createCamera();
    renderFactory.createCube();
    renderFactory.setup();
    renderFactory.paint();

}

} ]);

My factory file...

var xrotation;
var yrotation;
var zrotation;
var WIDTH = 800;
var HEIGHT = 600;
var ASPECT = WIDTH / HEIGHT;
var renderer = new THREE.WebGLRenderer();
var scene = new THREE.Scene();
var camera;
var cube;

var newServices = angular.module('newServices', []);
newServices.factory('renderFactory', function () {
return {


    createCube: function () {
        // define cube variables
        var length = 50;
        var segments = 16;

        // create cube material
        var sphereMaterial = new THREE.MeshLambertMaterial({ color: 0xFF0000 });

        // create a new mesh with cube geometry -
        cube = new THREE.Mesh(new THREE.CubeGeometry(15, 15, 15), sphereMaterial);

        //Set Cube Rotation
        //cube.rotation.x += 0.2;
        //cube.rotation.y += 0.3;
        //cube.rotation.z += 0.1;

        scene.add(cube);

    },

    createCamera: function () {
        // set camera attributes
        var VIEW_ANGLE = 40;
        var NEAR = 0.1;
        var FAR = 10000;

        // create a WebGL camera
        camera = new THREE.PerspectiveCamera(VIEW_ANGLE,
                 ASPECT,
                 NEAR,
                 FAR);

        // the camera starts at 0,0,0 so move it back
        //camera.position.z = 250;

        // add the camera to the scene
        scene.add(camera);

    },

    paint: function () {
        // render the scene
        renderer.render(scene, camera);
    },



    setup: function () {
        // initialize the renderer
        renderer.setSize(WIDTH, HEIGHT);
        document.getElementById('container1').appendChild(renderer.domElement);

    }

};
});

What did I miss or where did I go wrong???

Answer №1

My knowledge of THREE surpasses that of Angular, and I have a different perspective on wrapping a factory in a service. As a factory on its own, without the service wrapper, it functions perfectly fine with a simple adjustment. The change you made by commenting out "camera.position.z = 250;" was putting the camera in the same location as the cube, resulting in the camera being surrounded by the transparent inside of the cube.

I decided to separate the factory from the service and uncommented the camera position setting. Additionally, I removed the router element as the focus is on Angular/Three, not routers.

Below is the updated app section:

var newApp = angular.module('new', []);

newApp.controller('mainCtrl', ['$scope', 'renderFactory', function ($scope, renderFactory) {
    init();

    function init() {
        renderFactory.createCamera();
        renderFactory.createCube();
        renderFactory.setup();
        renderFactory.paint();
    }

}]);

I made changes in your factory by removing it from the service wrapper and uncommenting the camera position:

newApp.factory('renderFactory', function renderFactory() {
    var xrotation;
    var yrotation;
    var zrotation;
    var WIDTH = 800;
    var HEIGHT = 600;
    var ASPECT = WIDTH / HEIGHT;
    var renderer = new THREE.WebGLRenderer();
    var scene = new THREE.Scene();
    var camera;
    var cube;

    return {
        createCube: function () {
        // set up the cube vars
        var length = 50;
        var segments = 16;

        // create the cube's material
        var sphereMaterial = new THREE.MeshLambertMaterial({
            color: 0xFF0000
        });

        // create a new mesh with cube geometry -
        cube = new THREE.Mesh(new THREE.BoxGeometry(15, 15, 15), sphereMaterial);

        //Set Cube Rotation
        cube.rotation.x += 0.2;
        cube.rotation.y += 0.3;
        cube.rotation.z += 0.1;

        scene.add(new THREE.AmbientLight(0xFF0000));

        scene.add(cube);

    },
    createCamera: function () {
        // set some camera attributes
        var VIEW_ANGLE = 40;
        var NEAR = 0.1;
        var FAR = 10000;

        // create a WebGL camera
        camera = new THREE.PerspectiveCamera(VIEW_ANGLE,
        ASPECT,
        NEAR,
        FAR);

        // the camera starts at 0,0,0 so pull it back
        camera.position.z = 250;

        // and the camera
        scene.add(camera);

    },
    paint: function () {
        // draw!
        renderer.render(scene, camera);
    },
    setup: function () {
        // start the renderer
        renderer.setSize(WIDTH, HEIGHT);
     document.getElementById('container1').appendChild(renderer.domElement);
    }
};

});

The html is as simple as:

<body ng-app="new">
    <div id="container1" ng-controller="mainCtrl"></div>
</body>

I also uncommented the cube rotations for a more dynamic look.

Here's a Fiddle - http://jsfiddle.net/kkelly/ccfyL82k/

Answer №2

After creating a directive that is functioning properly, I attached it. However, in your code, the line that contains

 document.getElementById('container1').appendChild(renderer.domElement);

should be replaced with

    container = angular.element('#container1');                                         
    container.append(renderer.domElement);

This modification is necessary because the original code will not work on linking or with Angular due to the 'appendchild' being undefined.

 ` elem[0].appendChild(renderer.domElement);

You can also use a linking function like the directive code provided below

 ` (function() {
    'use strict';
    angular
    .module('vr-module')
   .directive(
    "vr",vr);

    /* @ngInject */
    function vr() {
        return {
            restrict: "E",
            templateUrl:'app/vr-module/vr.html',
            templateAs:'vr',
            controller:'VrPageController',
            link: function (scope, elem, attr) {

                // declaring variables to hold the objects of the game
                  var scene, camera, fieldOfView, aspectRatio, nearPlane , farPlane,
                      renderer, container, HEIGHT, WIDTH, mouseX, mouseY , sea, Sky, cloud, Clouds, SkyBox;

                      var Colors = {
                        red:0xf25346,
                        white:0xd8d0d1,
                        brown:0x59332e,
                        pink:0xF5986E,
                        brownDark:0x23190f,
                        blue:0x68c3c0,
                      };

                                     // More code here...

                  // End of directive code

            }
        }
    }

}());

(function() {
  'use strict';
  angular
  .module('vr-module')
  .directive(
"vr",vr);

// More code here...

}());

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Encountering issues with app functionality following update to Ionic RC4 version

Since upgrading from Ionic rc3 to rc4, I've been facing difficulties running my app smoothly. The app compiles without any errors when I use ionic-app-scripts build --prod, but when I try to run it on my iPhone, all I get is a blank screen and an err ...

Jest does not recognize AnimationEvent as a defined element

I am currently facing an issue while attempting to simulate an animationEvent for a test within my Angular application. The error message I receive is: ReferenceError: AnimationEvent is not defined. Given that this feature is experimental, it seems like ...

Tips for inserting a hyperlink into a Chakra UI toast

Exploring the integration of Chakra UI within my Next.js project has led me to a curious question: Can Chakra UI toasts accommodate links and styled text? If so, what is the process for implementing these features? ...

Using Buttons to Filter Data in React

My goal is to implement button functionality that filters data from a JSON file. Upon clicking a button, the state should be updated with the filtered JSON data and display the list with the updated information. Currently, I have four buttons for filteri ...

When selecting a different file after initially choosing one, the Javascript file upload event will return e.target as null

Currently, I have implemented file uploading using <input>. However, when attempting to change the file after already selecting one, the website crashes and states that event.target is null. <Button label="Upload S3 File"> <input ...

When utilizing the ng-strict-di directive in AngularJS 1.3 with the ngbp framework, the application fails to load properly on the Chrome browser

I am currently developing a web application with AngularJS using the ngbp framework, previously known as ng-boilerplate. The default version of AngularJS in this framework is 1.2, but we are considering switching to 1.3 due to some appealing features it of ...

Changing the color of an open Bootstrap 4 accordion panel when the page loads

I am looking to emphasize the panel that the user has opened. If a user clicks the button in the card header, the background turns red, which is working smoothly as demonstrated in the code snippet. In certain scenarios, the first panel is open by default ...

How to hide offcanvas navigation bar with one click in Bootstrap 5

There was an issue with a Bootstrap 5 project where the offcanvas menu would remain open after clicking on an anchor link. Is there a way to automatically close the offcanvas menu after clicking on an anchor link? <nav class="navbar fixed-top py ...

Button activates SVG animation

Currently, I am working on an animation for a button using SVG. The animation utilizes the classes .is-loading and .is-success. However, when I click on the button, the animation does not execute as expected. I'm having trouble identifying the error w ...

Learn how to easily modify a value in a CVS file by simply clicking on the data point on a highcharts graph

Is there a way to update my CSV file by clicking on a graph? I want to be able to create a graph from data in the same CSV file and then, when I click on a point, set that point to zero on the y-axis and update the corresponding value in the CSV file to 0. ...

Error message in Node.js: Unable to establish connection to 127.0.0.1 on port 21 due to E

I am currently developing a simple application using node js, and I have encountered the following issue: Error: connect ECONNREFUSED 127.0.0.1:21 at Object exports._errnoException (util.js:1034:11) at exports _exceptionWithHostPort (util.js:1057: ...

JavaScript Flash player for iPad

As many of us know, the iPad does not support Flash. However, I am curious if the iPad sends any specific error messages that can be captured using Javascript. I realize one method is to detect the iPad from the user agent string, but I am interested in w ...

Looking for a way to detect a click event on the header using AngularJS?

I'm currently utilizing an angular ui grid and looking to enhance its functionality. When I click the header icon V, I want to trigger an alert instead of displaying the text 'hello'. Is it possible to show an alert upon clicking the icon? ...

When a directive is passed a string with HTML tags, the tags are not displayed/rendered as expected

When it comes to passing a string with HTML content to my Angular directive, I've encountered an issue where the rendered output treats the HTML as text. While researching possible solutions, most of them seem to rely on using ng-bind-html directive, ...

Is there any trouble with this controller method?

Every time I use routers without controllers, they work perfectly fine. But the moment I add controllers to them, my routers stop working completely. Can someone please shed some light on what might be causing this issue with my router functions? LOGIN CO ...

What is the process for inserting HTML content into the body of an iframe?

Is there a way to insert HTML content into the body of an iframe instead of using the src attribute to call a page's URL? I am looking for a code that is compatible with all browsers and works perfectly. ...

Exploring package.json: Uncovering the Installed Components of a Module

I am working on a project with the following structure: __tests__ example src .bablerc .eslintignore .eslintrd .gitignore package.json package-lock.json README.md The package.json file has the following parameters: { "name": "", "version": "0.0.1", ...

Exploring textboxes with jQuery loops

Is there a way to clear all these textboxes by iterating through them using Jquery? <div class="col-md-4" id="RegexInsert"> <h4>New Regex Pattern</h4> <form role="form"> <div class="form-group"> &l ...

Issue with border-color in CSS on Chrome tables

Here is a table style I have defined for selected elements: tr[selected] { background: #FFF; border-color: #000000; color: #000000; } To apply this style, I use JavaScript: $this.unbind().change(function () { element = $(this).parent ...

Set the minimum date for the jQuery datepicker

Having trouble with jQuery datepickers for selecting from and to dates? Currently, the code restricts the selection in the second datepicker to dates after the one selected in the first datepicker. However, what if you need to set the to datepicker to allo ...