11< html >
2- < head >
3- < meta charset ="utf-8 "/>
4- < script src ="https://cdnjs.cloudflare.com/ajax/libs/three.js/100/three.min.js "> </ script >
5- < script >
6- // ページの読み込みを待つ
7- window . addEventListener ( 'load' , init ) ;
2+ < head >
3+ < meta charset ="utf-8 " />
4+ < script src ="https://cdnjs.cloudflare.com/ajax/libs/three.js/100/three.min.js "> </ script >
5+ < script >
6+ // ページの読み込みを待つ
7+ window . addEventListener ( 'load' , init ) ;
88
9- function init ( ) {
10- // サイズを指定
11- const width = 960 ;
12- const height = 540 ;
13- let rot = 0 ;
9+ function init ( ) {
10+ // サイズを指定
11+ const width = 960 ;
12+ const height = 540 ;
13+ let rot = 0 ;
1414
15- // レンダラーを作成
16- const renderer = new THREE . WebGLRenderer ( {
17- canvas : document . querySelector ( '#myCanvas' ) ,
18- } ) ;
19- renderer . setSize ( width , height ) ;
15+ // レンダラーを作成
16+ const renderer = new THREE . WebGLRenderer ( {
17+ canvas : document . querySelector ( '#myCanvas' )
18+ } ) ;
19+ renderer . setSize ( width , height ) ;
2020
21- // シーンを作成
22- const scene = new THREE . Scene ( ) ;
21+ // シーンを作成
22+ const scene = new THREE . Scene ( ) ;
2323
24- // カメラを作成
25- const camera = new THREE . PerspectiveCamera ( 45 , width / height ) ;
24+ // カメラを作成
25+ const camera = new THREE . PerspectiveCamera ( 45 , width / height ) ;
2626
27- // 平行光源を作成
28- const directionalLight = new THREE . DirectionalLight ( 0xFFFFFF ) ;
29- directionalLight . position . set ( 1 , 1 , 1 ) ;
30- scene . add ( directionalLight ) ;
27+ // 平行光源を作成
28+ const directionalLight = new THREE . DirectionalLight ( 0xffffff ) ;
29+ directionalLight . position . set ( 1 , 1 , 1 ) ;
30+ scene . add ( directionalLight ) ;
3131
32- // マテリアルを作成
33- const material = new THREE . MeshStandardMaterial ( {
34- map : new THREE . TextureLoader ( ) . load ( 'imgs/earthmap1k.jpg' ) ,
35- side : THREE . DoubleSide ,
36- } ) ;
32+ // マテリアルを作成
33+ const material = new THREE . MeshStandardMaterial ( {
34+ map : new THREE . TextureLoader ( ) . load ( 'imgs/earthmap1k.jpg' ) ,
35+ side : THREE . DoubleSide
36+ } ) ;
3737
38- // 球体の形状を作成します
39- const geometry = new THREE . SphereGeometry ( 300 , 30 , 30 ) ;
40- // 形状とマテリアルからメッシュを作成します
41- const earthMesh = new THREE . Mesh ( geometry , material ) ;
42- // シーンにメッシュを追加します
43- scene . add ( earthMesh ) ;
38+ // 球体の形状を作成します
39+ const geometry = new THREE . SphereGeometry ( 300 , 30 , 30 ) ;
40+ // 形状とマテリアルからメッシュを作成します
41+ const earthMesh = new THREE . Mesh ( geometry , material ) ;
42+ // シーンにメッシュを追加します
43+ scene . add ( earthMesh ) ;
4444
45- // 星屑を作成します (カメラの動きをわかりやすくするため)
46- createStarField ( ) ;
45+ // 星屑を作成します (カメラの動きをわかりやすくするため)
46+ createStarField ( ) ;
4747
48- function createStarField ( ) {
49- // 形状データを作成
50- const geometry = new THREE . Geometry ( ) ;
51- for ( let i = 0 ; i < 1000 ; i ++ ) {
52- geometry . vertices . push ( new THREE . Vector3 (
53- 3000 * ( Math . random ( ) - 0.5 ) ,
54- 3000 * ( Math . random ( ) - 0.5 ) ,
55- 3000 * ( Math . random ( ) - 0.5 ) ,
56- ) ) ;
57- }
58- // マテリアルを作成
59- const material = new THREE . PointsMaterial ( {
60- size : 10 ,
61- color : 0xFFFFFF ,
62- } ) ;
48+ function createStarField ( ) {
49+ // 形状データを作成
50+ const geometry = new THREE . Geometry ( ) ;
51+ for ( let i = 0 ; i < 1000 ; i ++ ) {
52+ geometry . vertices . push (
53+ new THREE . Vector3 (
54+ 3000 * ( Math . random ( ) - 0.5 ) ,
55+ 3000 * ( Math . random ( ) - 0.5 ) ,
56+ 3000 * ( Math . random ( ) - 0.5 )
57+ )
58+ ) ;
59+ }
60+ // マテリアルを作成
61+ const material = new THREE . PointsMaterial ( {
62+ size : 10 ,
63+ color : 0xffffff
64+ } ) ;
6365
64- // 物体を作成
65- const mesh = new THREE . Points ( geometry , material ) ;
66- scene . add ( mesh ) ;
67- }
66+ // 物体を作成
67+ const mesh = new THREE . Points ( geometry , material ) ;
68+ scene . add ( mesh ) ;
69+ }
6870
69- tick ( ) ;
71+ tick ( ) ;
7072
71- // 毎フレーム時に実行されるループイベントです
72- function tick ( ) {
73- rot += 0.5 ; // 毎フレーム角度を0.5度ずつ足していく
74- // ラジアンに変換する
75- const radian = rot * Math . PI / 180 ;
76- // 角度に応じてカメラの位置を設定
77- camera . position . x = 1000 * Math . sin ( radian ) ;
78- camera . position . z = 1000 * Math . cos ( radian ) ;
79- // 原点方向を見つめる
80- camera . lookAt ( new THREE . Vector3 ( 0 , 0 , 0 ) ) ;
73+ // 毎フレーム時に実行されるループイベントです
74+ function tick ( ) {
75+ rot += 0.5 ; // 毎フレーム角度を0.5度ずつ足していく
76+ // ラジアンに変換する
77+ const radian = ( rot * Math . PI ) / 180 ;
78+ // 角度に応じてカメラの位置を設定
79+ camera . position . x = 1000 * Math . sin ( radian ) ;
80+ camera . position . z = 1000 * Math . cos ( radian ) ;
81+ // 原点方向を見つめる
82+ camera . lookAt ( new THREE . Vector3 ( 0 , 0 , 0 ) ) ;
8183
82- // レンダリング
83- renderer . render ( scene , camera ) ;
84+ // レンダリング
85+ renderer . render ( scene , camera ) ;
8486
85- requestAnimationFrame ( tick ) ;
87+ requestAnimationFrame ( tick ) ;
88+ }
8689 }
87- }
88- </ script >
89- </ head >
90- < body >
91- < canvas id ="myCanvas "> </ canvas >
92- </ body >
93- </ html >
90+ </ script >
91+ </ head >
92+ < body >
93+ < canvas id ="myCanvas "> </ canvas >
94+ </ body >
95+ </ html >
0 commit comments