Jump to content

[SOLVED] attachControl doesn't work in the imported scene from blender


8Observer8
 Share

Recommended Posts

Hello,

How do you think why the attachControl doesn't work in this code?

/// <reference path="../babylon.d.ts" />

namespace LOADING
{
    export class LoadScene
    {
		/*
		* Public members
		*/
        public camera: BABYLON.ArcRotateCamera;
        public light: BABYLON.PointLight;

		/*
		* Private members
		*/
        private _canvas: HTMLCanvasElement;
        private _engine: BABYLON.Engine;
        private _scene: BABYLON.Scene;

        constructor(canvas: HTMLCanvasElement)
        {
            // Engine
            this._engine = new BABYLON.Engine(canvas);

            // Scene
            this._scene = new BABYLON.Scene(this._engine);

            // Camera
            this.camera = new BABYLON.ArcRotateCamera("camera", 0, 0, 30, BABYLON.Vector3.Zero(), this._scene);
            this.camera.attachControl(canvas, true);

            // Light
            this.light = new BABYLON.PointLight("light", new BABYLON.Vector3(20, 20, 20), this._scene);
            this.light.diffuse = new BABYLON.Color3(0, 1, 0);
            this.light.specular = new BABYLON.Color3(1, 0, 1);
            this.light.intensity = 1.0;

			/**
			* Load an exported scene
			* This static method contains 6 parameters
			* 1: the directory of the scene file
			* 2: the scene file name
			* 3: the Babylon.js engine
			* 4: a success callback, providing the new scene created by the loader
			* 5: progress callback, empty as default (can be null)
			* 6: error callback, providing the new scene created by the loader 
			*/
            BABYLON.SceneLoader.Load("./", "awesome_scene.babylon", this._engine,
                (scene: BABYLON.Scene) =>
                { // Success callblack
                    this._scene = scene;

                    // We can now access the scene.meshes array etc.
                    // Decal the meshes to 10 units on X
                    for (var i = 0; i < this._scene.meshes.length; i++)
                    {
                        console.log(this._scene.meshes[i].name);
                        this._scene.meshes[i].position.addInPlace(new BABYLON.Vector3(10, 0, 0));
                    }

                    // Just append the same scene 
                    this._appendScene();

                }, () =>
                { // Progress callback
                    // Do something with your web page :)
                }, (scene: BABYLON.Scene) =>
                { // Error callback

                });

        }

        /*
        * Use graphs.The new parent of the light is the camera.
        * Then, the light's position will be the same as the camera
        */
        public setCameraParentOfLight(): void
        {
            this.light.position = BABYLON.Vector3.Zero();
            this.light.parent = this.camera;
        }

		/**
		* Runs the engine render loop
		*/
        public runRenderLoop(): void
        {
            this._engine.runRenderLoop(() =>
            {
                this._scene.render();
            });
        }

		/**
		* This method appends new scene with the already existing scene
		* Here, we are appending the same scene at its original position
		*/
        private _appendScene(): void
        {
            BABYLON.SceneLoader.Append("./", "awesome_scene.babylon", this._scene, (scene: BABYLON.Scene) =>
            {
                // Do something you want
            }, () =>
                {
                    // Progress
                }, (scene: BABYLON.Scene) =>
                {
                    // Error
                });
        }

		/**
		* Import the skull mesh (available in the Babylon.js examples)
		* This methods imports meshes and only meshes.
		*/
        private _importMesh(): void
        {
            BABYLON.SceneLoader.ImportMesh("", "./", "skull.babylon", this._scene,
                (meshes, particles, skeletons) =>
                { // Success callback
                    // Here, meshes contains only one mesh: the skull (meshes[0])
                    // Particles array is empty
                    // skeletons array is empty

                }, () =>
                { // Progress callback

                }, (scene: BABYLON.Scene, e: any) =>
                {
                    // Do something
                    console.log(e.message);
                });
        }
    }

    window.onload = () =>
    {
        let canvas = <HTMLCanvasElement>document.getElementById("renderCanvas");
        let scene = new LoadScene(canvas);
        scene.runRenderLoop();
        scene.setCameraParentOfLight();
    }
}

 

Link to comment
Share on other sites

Hiya 8o8... good to see you again.  Yep, scene.cameras and scene.lights are where non-mesh scene items live.  Cameras and lights are "nodes", but they are not mesh.

Good job with self-solution!

Fun experiment of the day:  When a MESH is the parent of another mesh, the child "inherits" all .position, .rotation, and .scaling values... from the parent.  But what about when the parent is a non-mesh node?  What values does the child mesh inherit... when its parent is a camera or light?  A fun and interesting experiment!  :)

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...