PageSourceSearch

https://www.sokkel.nl/modules/isemanticsconfigurator/webroot/configurator/js/core/camera.js

js sokkel.nl collected 2026-10-09 17:09:16 UTC 5,979 bytes, 181 lines download raw bytes

1'use strict';
2/**
3 * Isemantics. Michel Peters
4 *
5 * Redistributions of files must retain the above copyright notice.
6 * If you did not receive a copy of the license and are unable to obtain it through
7 * the web, please send a note to [email protected] so we can mail you a
8 * copy immediately.
9 *
10 * @category  Javascript
11 * @package   App
12 * @author    Michel Peters <[email protected]>
13 * @copyright 2015-2018 Isemantics
14 * @license   Proprietary licence
15 * @version   GIT: $Id$
16 * @link      https://www.isemantics.nl
17 * @since     1.0.0
18 */
19
20/**
21 * Configurator camera
22 */
23function ConfiguratorCamera(parent) {
24
25    // Alias self
26    const self = this;
27
28    // Parent object (core)
29    self.parent = parent;
30
31    // ThreeJS camera
32    self.cameraObject = null;
33
34    // ThreeJS orbit controls
35    self.controls = null;
36
37    // Renderer DOM element (to attach the camera to webGL)
38    self.domElement = null;
39
40    // Initial camera position
41    // note: saved for export, so we can revent to initial position to
42    // get consistent export images/screenshots
43    self.initialCameraPosition = {x: 60, y: 140, z: 110};
44
45    /**
46     * Initialize
47     */
48    self.initialize = function (rendererDomElement, screenRatio) {    
49        logger.debug('camera::initialize');
50
51        self.domElement = rendererDomElement;
52
53        self.cameraObject = new THREE.PerspectiveCamera(PERSPECTIVE_FOV, screenRatio, PERSPECTIVE_NEAR, PERSPECTIVE_FAR);
54        self.cameraObject.position.set(self.initialCameraPosition.x, self.initialCameraPosition.y, self.initialCameraPosition.z);
55
56        if (self.cameraObject == null) {
57            console.error('camera::initialize: Camera could not be created and stayed NULL');
58            return;
59        }
60
61        // # Camera controls
62        // Create orbit controls and bind to camera
63        self.controls = new THREE.OrbitControls(self.cameraObject, self.domElement);
64        self.controls.enableDamping = true;
65        self.controls.dampingFactor = 0.25;
66        self.controls.enableZoom = true;
67        // to disable pan
68        self.controls.enablePan = true; //false;
69        self.controls.rotateSpeed = 0.1;
70        self.controls.target = new THREE.Vector3(0, 1, 0)
71        // Limit zoom in/out
72        self.controls.minDistance = 60;
73        self.controls.maxDistance = 260;
74        // How far you can orbit vertically, upper and lower limits.
75        // Range is 0 to Math.PI radians.
76        self.controls.minPolarAngle = -10; // 0.2; // radians
77        self.controls.maxPolarAngle = Math.PI / 2; // / 2.5; // radians
78        // How far you can orbit horizontally, upper and lower limits.
79        // If set, must be a sub-interval of the interval [ - Math.PI, Math.PI ].
80        self.controls.minAzimuthAngle = -Math.PI; // radians
81        self.controls.maxAzimuthAngle = Math.PI; // radians
82        self.controls.target.set(-10, 70, -10);
83
84        self.controls.update();
85        self.controls.saveState();
86
87    }
88
89    self.reset = function () {
90        logger.debug('camera::reset');
91        self.controls.reset();
92    }
93
94    self.update = function () {   
95        //logger.debug('camera::update');     
96        self.controls.update();
97    }
98
99    // Example: after resize, the ratio must be reset
100    self.updateProjectionMatrix = function (previewSize) {
101        logger.debug('camera::updateProjectionMatrix');
102        // Calculate new camera ratio
103        self.cameraObject.aspect = previewSize.width / previewSize.height;
104        self.cameraObject.updateProjectionMatrix();
105    }
106
107    /**
108     * Get reference to the THREE camera
109     */
110    self.getCameraObject = function () {
111        //logger.debug('camera::getCameraObject');
112        return self.cameraObject;
113    }
114
115    self.getPosition = function () {
116        logger.debug('camera::getPosition');
117        return self.cameraObject != null
118            ? self.cameraObject.position
119            : null;
120    }
121
122    self.getInitialPosition = function () {
123        logger.debug('camera::getInitialPosition');
124        return self.initialCameraPosition;
125    }
126
127    self.setPositionForScreenshot = function () {
128        var initialCameraPosition = self.getInitialPosition();
129        self.cameraObject.position.set(initialCameraPosition.x, initialCameraPosition.y, self.initialCameraPosition.z);
130        var fov = self.cameraObject.fov * ( Math.PI / 180 );        
131        var modelHeight = self.parent.parent.getModel().getFullHeight();
132        var cameraPosition = new THREE.Vector3(
133          40, //50,
134          Math.abs( (modelHeight * 0.85 ) / Math.tan( fov / 2 ) ),
135          110, //110
136        );
137        
138        self.cameraObject.position.copy( cameraPosition );
139        self.cameraObject.lookAt( new THREE.Vector3( 0, modelHeight / 4, 0 ) );
140    }
141    
142    self.setPosition = function (pos) {
143        self.cameraObject.position.set(pos.x, pos.y, pos.z);
144    }
145    
146    /**
147     * Pan up
148     */
149    self.pan = function (direction) {
150        switch (direction) {
151            case 'up': self.controls.panUp(PAN_DISTANCE); break;
152            case 'down': self.controls.panUp(PAN_DISTANCE * -1); break;
153            case 'left': self.controls.panLeft(PAN_DISTANCE); break;
154            case 'right': self.controls.panLeft(PAN_DISTANCE * -1); break;
155        }
156    }
157    
158    /**
159     * Zoom in
160     */
161    self.zoom = function (direction) {
162        switch (direction) {
163            case 'in': self.controls.dollyOut(ZOOM_FACTOR); break;
164            case 'out': self.controls.dollyIn(ZOOM_FACTOR); break;
165        }
166    }
167    
168    // Return public class method mapping
169    return {
170        getCameraObject: self.getCameraObject,
171        getPosition: self.getPosition,
172        setPosition: self.setPosition,
173        setPositionForScreenshot: self.setPositionForScreenshot,
174        pan: self.pan,
175        zoom: self.zoom,
176        reset: self.reset,
177        update: self.update,
178        updateProjectionMatrix: self.updateProjectionMatrix,
179        initialize: self.initialize
180    }
181}

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.