summaryrefslogtreecommitdiffstats
path: root/examples/canvas3d/canvas3d/threejs/cellphone/qml/cellphone/cellphone.js
blob: 79b80503ce5ab6c0cf4f8d18769193ec4a4b5e7f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the QtCanvas3D module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:BSD$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** BSD License Usage
** Alternatively, you may use this file under the terms of the BSD license
** as follows:
**
** "Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are
** met:
**   * Redistributions of source code must retain the above copyright
**     notice, this list of conditions and the following disclaimer.
**   * Redistributions in binary form must reproduce the above copyright
**     notice, this list of conditions and the following disclaimer in
**     the documentation and/or other materials provided with the
**     distribution.
**   * Neither the name of The Qt Company Ltd nor the names of its
**     contributors may be used to endorse or promote products derived
**     from this software without specific prior written permission.
**
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
**
** $QT_END_LICENSE$
**
****************************************************************************/

Qt.include("three.js")

var camera, scene, renderer, cubeCamera;
var caseMesh, frontMesh, iconMesh;
var caseColor = new THREE.Color("#000000");
var meshesReady = false;
var canvasTextureProvider = null;
var zeroVector = new THREE.Vector3(0, 0, 0);
var cameraLight;
var sphereImage, iconImage;
var caseMeshFile, frontMeshFile, iconMeshFile;

function initializeGL(canvas, textureSource) {
    scene = new THREE.Scene();

    cubeCamera = new THREE.CubeCamera(5, 100, 512);
    cubeCamera.renderTarget.texture.minFilter = THREE.LinearMipMapLinearFilter;
    scene.add(cubeCamera);

    // Background sphere
    var sphere = new THREE.Mesh(
                new THREE.SphereGeometry(40, 16, 16),
                new THREE.MeshBasicMaterial());
    sphere.side = THREE.BackSide;
    sphere.scale.x = -1;
    scene.add(sphere);
    var textureLoader = new THREE.TextureLoader();
    if (sphereImage) {
        var sphereTexture = textureLoader.load(sphereImage, updateEnvMap);
        sphereTexture.mapping = THREE.UVMapping;
        sphereTexture.minFilter = THREE.LinearFilter;
        sphere.material.map = sphereTexture;
    } else {
        sphere.material.color = new THREE.Color("black");
    }

    camera = new THREE.PerspectiveCamera( 75, canvas.width / canvas.height, 0.001, 1000 );

    var light = new THREE.AmbientLight( 0x666666 );
    scene.add( light );

    cameraLight = new THREE.DirectionalLight( 0xffffff, 1 );
    cameraLight.position.y = 1.5;
    scene.add( cameraLight );

    var caseMaterial = new THREE.MeshPhongMaterial({ envMap: cubeCamera.renderTarget,
                                                   combine: THREE.MultiplyOperation,
                                                   reflectivity: 0.35,
                                                   specular: "#555555" });
    //! [0]
    var frontTexture = new THREE.QtQuickItemTexture( textureSource );
    //! [0]
    //! [1]
    var frontMaterial = new THREE.MeshPhongMaterial( { map: frontTexture } );
    //! [1]

    var iconMaterial = new THREE.MeshPhongMaterial( { transparent:true } );
    if (iconImage) {
        var iconTexture = textureLoader.load(iconImage);
        iconTexture.minFilter = THREE.LinearFilter;
        iconMaterial.map = iconTexture;
    } else {
        iconMaterial.opacity = 0;
    }

    renderer = new THREE.Canvas3DRenderer(
                { canvas: canvas, antialias: true, devicePixelRatio: canvas.devicePixelRatio });
    renderer.setSize( canvas.width, canvas.height );

    // The cellphone meshes were created using a third party tool (Blender).
    // They were exported from Blender as Wavefront OBJ files and then converted into JSON format
    // using the OBJ -> JSON conversion script provided by three.js (convert_obj_three.py).
    var loader = new THREE.JSONLoader();
    loader.load( caseMeshFile, function ( geometry, materials ) {
        geometry.computeVertexNormals();
        var bufferGeometry = new THREE.BufferGeometry();
        bufferGeometry.fromGeometry(geometry);
        caseMesh = new THREE.Mesh( bufferGeometry, caseMaterial );
        if (iconMesh && frontMesh)
            meshesReady = true;
        caseMesh.material.color = caseColor;
        updateEnvMap();
        scene.add( caseMesh );
    } );
    loader.load( frontMeshFile, function ( geometry, materials ) {
        var bufferGeometry = new THREE.BufferGeometry();
        bufferGeometry.fromGeometry(geometry);
        frontMesh = new THREE.Mesh( bufferGeometry, frontMaterial );
        if (iconMesh && caseMesh)
            meshesReady = true;
        scene.add( frontMesh );
    } );
    loader.load( iconMeshFile, function ( geometry, materials ) {
        var bufferGeometry = new THREE.BufferGeometry();
        bufferGeometry.fromGeometry(geometry);
        iconMesh = new THREE.Mesh( bufferGeometry, iconMaterial );
        if (caseMesh && frontMesh)
            meshesReady = true;
        scene.add( iconMesh );
    } );
}

function updateEnvMap() {
    if (caseMesh) {
        // Take a snapshot of the scene using cube camera to create environment map for case
        caseMesh.material.envMap = cubeCamera.renderTarget;
        cubeCamera.updateCubeMap(renderer, scene);
        caseMesh.material.needsUpdate = true;
    }
}

function setSphereTexture(image) {
    sphereImage = image;
}

function setIconTexture(image) {
    iconImage = image;
}

function setMeshFiles(caseFile, frontFile, iconFile) {
    caseMeshFile = caseFile;
    frontMeshFile = frontFile;
    iconMeshFile = iconFile;
}

function setCaseColor(color) {
    caseColor.set(color);
    if (caseMesh)
        caseMesh.material.color = caseColor;
}

function resizeGL(canvas) {
    camera.aspect = canvas.width / canvas.height;
    camera.updateProjectionMatrix();

    renderer.setPixelRatio(canvas.devicePixelRatio);
    renderer.setSize( canvas.width, canvas.height );
}

function degToRad(degrees) {
    return degrees * Math.PI / 180;
}

function paintGL(canvas) {
    if (meshesReady) {
        var cameraRad = degToRad(canvas.cameraAngle);
        var lightRad = cameraRad - 0.8;
        caseMesh.rotation.x = degToRad(canvas.xRotAnim);
        caseMesh.rotation.y = degToRad(canvas.yRotAnim);
        caseMesh.rotation.z = cameraRad + degToRad(canvas.zRotAnim);
        frontMesh.rotation.set(caseMesh.rotation.x, caseMesh.rotation.y, caseMesh.rotation.z);
        iconMesh.rotation.set(caseMesh.rotation.x, caseMesh.rotation.y, caseMesh.rotation.z);
        camera.position.x = canvas.distance * Math.sin(cameraRad);
        camera.position.z = canvas.distance * Math.cos(cameraRad);
        cameraLight.position.x = (canvas.distance + 2) * Math.sin(lightRad);
        cameraLight.position.z = (canvas.distance + 2) * Math.cos(lightRad);
        camera.lookAt(zeroVector);
    }

    renderer.render( scene, camera );

}