@@ -4,33 +4,90 @@ import * as THREE from "three";
4
4
import { OrbitControls } from "three/examples/jsm/controls/OrbitControls.js" ;
5
5
import { FirstPersonControls } from "./controller" ;
6
6
import { fragmentShader } from "./shader.js" ;
7
+ import { Sky } from "three/addons/objects/Sky.js" ;
7
8
8
- let model , clock , controls , shader_mat , renderer ;
9
+ import Stats from "stats.js" ;
10
+
11
+ const _VS = `
12
+ varying vec3 vWorldPosition;
13
+ void main() {
14
+ vec4 worldPosition = modelMatrix * vec4( position, 1.0 );
15
+ vWorldPosition = worldPosition.xyz;
16
+ gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
17
+ }` ;
18
+
19
+ const _FS = `
20
+ uniform vec3 topColor;
21
+ uniform vec3 bottomColor;
22
+ uniform float offset;
23
+ uniform float exponent;
24
+ varying vec3 vWorldPosition;
25
+ void main() {
26
+ float h = normalize( vWorldPosition + offset ).y;
27
+ gl_FragColor = vec4( mix( bottomColor, topColor, max( pow( max( h , 0.0), exponent ), 0.0 ) ), 1.0 );
28
+ }` ;
29
+
30
+
31
+ let model , clock , controls , shader_mat , renderer ;
32
+ let sun , sky ;
9
33
10
34
// Scene, Camera and Renderer
11
35
clock = new THREE . Clock ( ) ;
12
36
13
37
const scene = new THREE . Scene ( ) ;
14
38
scene . background = new THREE . Color ( 0xa0a0a0 ) ;
15
39
40
+ // fps counter
41
+ var stats = Stats ( ) ;
42
+ document . body . appendChild ( stats . domElement ) ;
43
+
16
44
const camera = new THREE . PerspectiveCamera (
17
45
75 ,
18
46
window . innerWidth / window . innerHeight ,
19
- 0.1 ,
20
- 1000
47
+ 0.01 ,
48
+ 100000
21
49
) ;
22
- main ( ) ;
50
+
51
+ function loadSky ( ) {
52
+
53
+ const hemiLight = new THREE . HemisphereLight ( 0xffffff , 0xfffffff , 0.6 ) ;
54
+ hemiLight . color . setHSL ( 0.6 , 1 , 0.6 ) ;
55
+ hemiLight . groundColor . setHSL ( 0.095 , 1 , 0.75 ) ;
56
+ hemiLight . position . set ( 0 , 50 , 0 ) ;
57
+ scene . add ( hemiLight ) ;
58
+
59
+ const uniforms = {
60
+ topColor : { value : new THREE . Color ( 0x0077ff ) } ,
61
+ bottomColor : { value : new THREE . Color ( 0xffffff ) } ,
62
+ offset : { value : 33 } ,
63
+ exponent : { value : 0.6 } ,
64
+ } ;
65
+ // scene.fog.color.copy(uniforms["bottomColor"].value);
66
+
67
+ const skyGeo = new THREE . SphereGeometry ( 1000 , 32 , 15 ) ;
68
+ const skyMat = new THREE . ShaderMaterial ( {
69
+ uniforms : uniforms ,
70
+ vertexShader : _VS ,
71
+ fragmentShader : _FS ,
72
+ side : THREE . BackSide ,
73
+ } ) ;
74
+
75
+ const sky = new THREE . Mesh ( skyGeo , skyMat ) ;
76
+ scene . add ( sky ) ;
77
+ }
78
+
23
79
function main ( ) {
24
- // scene.fog = new THREE.Fog( 0xa0a0a0, 1, 50 );
80
+ scene . fog = new THREE . Fog ( 0xa0a0a0 , 1 , 5000 ) ;
25
81
26
82
renderer = new THREE . WebGLRenderer ( {
27
83
canvas : document . querySelector ( ".webgl" ) ,
28
84
} ) ;
29
85
30
86
controls = new FirstPersonControls ( camera , renderer . domElement ) ;
31
- controls . movementSpeed = 10 ;
32
- controls . lookSpeed = 0.1 ;
87
+ controls . movementSpeed = 20 ;
88
+ controls . lookSpeed = 0.8 ;
33
89
controls . activeLook = false ;
90
+ controls . heightMin = - 10 ;
34
91
// controls.autoForward = true;
35
92
controls . mouseDragOn = true ;
36
93
@@ -39,14 +96,16 @@ function main() {
39
96
40
97
renderer . setPixelRatio ( window . devicePixelRatio ) ;
41
98
renderer . setSize ( window . innerWidth , window . innerHeight ) ;
99
+ render . toneMappingExposure = 0.5 ;
42
100
// camera.position.setZ(30);
43
101
44
102
renderer . render ( scene , camera ) ;
45
103
46
104
// Adding objects here
47
105
const geom = new THREE . BoxGeometry ( 5 , 5 , 5 ) ;
48
- const material = new THREE . MeshStandardMaterial ( { color : "#00ff83 " } ) ;
106
+ const material = new THREE . MeshStandardMaterial ( { color : "#ffffff " } ) ;
49
107
const box = new THREE . Mesh ( geom , material ) ;
108
+ box . castShadow = true ;
50
109
scene . add ( box ) ;
51
110
52
111
// todo
@@ -55,26 +114,29 @@ function main() {
55
114
fragmentShader : fragmentShader ( ) ,
56
115
} ) ;
57
116
const mesh = new THREE . Mesh (
58
- new THREE . PlaneGeometry ( 200 , 200 ) ,
117
+ new THREE . PlaneGeometry ( 10000 , 10000 , 256 , 256 ) ,
59
118
shader_mat ,
60
119
) ;
61
120
mesh . rotation . x = - Math . PI / 2 ;
121
+ mesh . position . set ( 0 , - 10 , 0 ) ;
62
122
mesh . receiveShadow = true ;
63
123
scene . add ( mesh ) ;
64
124
65
125
// cont planeMat = new THREE.
66
126
67
127
// light
68
- const light = new THREE . DirectionalLight ( 0xffffff , 1 , 100 ) ;
69
- light . position . set ( 0 , 10 , 30 ) ;
70
- light . castShadow = true ;
71
- scene . add ( light ) ;
128
+ // const light = new THREE.DirectionalLight(0xffffff, 1, 100);
129
+ // light.position.set(0, 10, 30);
130
+ // light.castShadow = true;
131
+ // scene.add(light);
72
132
73
- const lightHelper = new THREE . DirectionalLightHelper ( light , 1 ) ;
74
- scene . add ( lightHelper ) ;
133
+ // const lightHelper = new THREE.DirectionalLightHelper(light, 1);
134
+ // scene.add(lightHelper);
75
135
76
136
camera . position . z = 25 ;
77
137
138
+ loadSky ( ) ;
139
+
78
140
// updating on window size
79
141
// window.addEventListener("resize", onWindowResize);
80
142
}
@@ -90,15 +152,15 @@ function onWindowResize() {
90
152
function animate ( ) {
91
153
// torus.position.x += 0.1
92
154
requestAnimationFrame ( animate ) ;
155
+ stats . update ( ) ;
93
156
render ( ) ;
94
157
}
95
158
96
159
function render ( ) {
97
- // mesh.
98
160
controls . update ( clock . getDelta ( ) , clock . elapsedTime ) ;
99
- console . log ( "itime" , shader_mat . uniforms . iTimeDelta )
100
- // controls.update(0.5, 0.5);
101
161
renderer . render ( scene , camera ) ;
102
162
}
103
163
164
+ // Calls of funtion in order
165
+ main ( ) ;
104
166
animate ( ) ;
0 commit comments