-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo_2.php
75 lines (58 loc) · 1.97 KB
/
demo_2.php
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
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>WebGL Demo with three.js</title>
<link rel="stylesheet" href="./styles/style.css" type="text/css">
</head>
<body>
<h1>WebGL Demo with three.js</h1>
<canvas id="glcanvas" width="640" height="480"></canvas>
<p>The same cube using Three.js framework which is handling the render part. (From the book)</p>
<?php include("_navigation.php"); ?>
</body>
<script type="module">
import * as THREE from './scripts/three.js/three.module.js';
function startGL() {
const canvas = document.querySelector('#glcanvas');
const renderer = new THREE.WebGLRenderer({canvas});
// Camera
const fov = 56;
const aspect = 4/3; // the canvas default
const near = 0.1;
const far = 5;
const camera = new THREE.PerspectiveCamera(fov, aspect, near, far);
camera.position.z = 2;
// Scene
const scene = new THREE.Scene();
// Box Geometry
const boxWidth = 1;
const boxHeight = 1;
const boxDepth = 1;
const geometry = new THREE.BoxGeometry(boxWidth, boxHeight, boxDepth);
// Box Material
const material = new THREE.MeshPhongMaterial({color: 0xffffff});
const texture = new THREE.TextureLoader().load("img/cubetexture.png");
material.map = texture;
// Box Mesh construction
const cube = new THREE.Mesh(geometry, material);
// Box added to scene
scene.add(cube);
// Light
const color = 0xFFFFFF;
const intensity = 1;
const light = new THREE.DirectionalLight(color, intensity);
light.position.set(-1, 2, 4);
scene.add(light);
function render(time) {
time *= 0.001; // convert time to seconds
cube.rotation.x = time;
cube.rotation.y = time;
renderer.render(scene, camera);
requestAnimationFrame(render);
}
requestAnimationFrame(render);
}
startGL();
</script>
</html>