-
-
Notifications
You must be signed in to change notification settings - Fork 361
Expand file tree
/
Copy pathdev_tile.html
More file actions
214 lines (142 loc) · 5.1 KB
/
Copy pathdev_tile.html
File metadata and controls
214 lines (142 loc) · 5.1 KB
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
213
214
<!DOCTYPE html>
<html lang="en">
<head>
<title>OpenPublica</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<link rel="icon" href="favicon.ico" />
</head>
<style>
*{ padding:0; margin: 0; -o-user-select:none; -ms-user-select:none; -khtml-user-select:none; -webkit-user-select:none; -moz-user-select: none;}
html { width:100%; height:100%; }
body { background:#8397ac; font:10px sans-serif; width:100%; height:100%; color:#FFFFFF; overflow: hidden; }
#debug{ }
#hub{ position:absolute; top:0; left:0; height:100%; width:100%; pointer-events:none; display:block; text-align:center;}
</style>
<script type="module">
import * as THREE from './build/three.module.min.js'
import * as UIL from './build/uil.module.js'
import { MapGenerator } from './src/micro/map/MapGenerator.js';
import { OrbitControls } from './src/jsm/controls/OrbitControls.js';
import { GroundShader } from './src/city3d/GroundShader.js';
let camera, scene, renderer, orbitControls;
let mesh;
init();
animate();
function randInt( low, high ) {return low + Math.floor( Math.random() * ( high - low + 1 ) ) }
function init() {
camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 0.1, 100 );
camera.position.z = 5;
camera.position.y = 5;
scene = new THREE.Scene();
const textureTile = new THREE.TextureLoader().load( 'assets/textures/tiles.png' );
textureTile.colorSpace = THREE.SRGBColorSpace;
const textureNorm = new THREE.TextureLoader().load( 'assets/textures/tiles_n.png' );
const geometry = new THREE.PlaneGeometry( 16, 16, 7, 7 );
geometry.rotateX( -Math.PI * 0.5 );
geometry.setAttribute( 'color', new THREE.BufferAttribute( new Float32Array(8 * 8 * 4), 4 ) );
const position = geometry.attributes.position;
console.log( position )
const colors = geometry.attributes.color;
const ar = colors.array
let n
for (let i = 0; i < colors.count; i++) {
n = i*4
ar[n] = randInt(1,30)
ar[n+1] = randInt(1,30)
ar[n+2] = randInt(1,30)
ar[n+3] = 1//randInt(1,30)
//colors.setXYZW( i * 4 , randInt(1,30), randInt(1,30), randInt(1,30), randInt(1,30) );
}
colors.needsUpdate = true;
console.log( colors )
const material = new GroundShader( { map:textureTile, normalMap:textureNorm } );
mesh = new THREE.Mesh( geometry, material );
scene.add( mesh );
const dirLight = new THREE.DirectionalLight( 0xffffff, 5 );
dirLight.position.set( -5, 10, 6 );
scene.add( dirLight );
renderer = new THREE.WebGLRenderer( { antialias: true } );
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( window.innerWidth, window.innerHeight );
document.body.appendChild( renderer.domElement );
orbitControls = new OrbitControls( camera, renderer.domElement );
orbitControls.target.set( 0, 0, 0 );
orbitControls.enablePan = false;
orbitControls.update();
//
window.addEventListener( 'resize', onWindowResize );
}
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize( window.innerWidth, window.innerHeight );
}
function animate() {
requestAnimationFrame( animate );
mesh.rotation.y += 0.0005;
//mesh.rotation.y += 0.01;
renderer.render( scene, camera );
}
/*let map
let canvas = null
const mapGen = new MapGenerator()
const MAP_SIZES = {
SMALL: [64, 64],
MEDIUM: [128, 128],
LARGE: [192, 192],
};
const setting = {
size:'MEDIUM'
}
const ui = new UIL.Gui({w:320})
ui.add( 'title', { name:'MAP Gen', h:22, align:'center' })
ui.add( setting, 'size', { type:'button', values: ['SMALL', 'MEDIUM', 'LARGE'], selectable:true, unselectable:true, selectmulty:true, p:0, h:30 })
ui.add('button', { name:'GENERATE', h:30, callback:gen })
function gen(){
const size = MAP_SIZES[setting.size];
map = mapGen.construct( size[0], size[1], true );
makePreview(map)
}
function makePreview ( map ){
if(canvas) document.body.removeChild(canvas)
const w = map.width
const h = map.height
canvas = document.createElement("canvas")
canvas.width = w
canvas.height = h
canvas.style.cssText = 'position:absolute; left:10px; top:10px;transform: scale(8);'
const ctx = canvas.getContext('2d')
ctx.fillStyle = "blue";
ctx.fillRect(0, 0, w, h);
//const monImageData = ctx.createImageData(w, h)
const imageData = ctx.getImageData(0, 0, w, h);
const data = imageData.data;
const pp = map.tilesData
let n, c
for (var i = 0; i < pp.length; i ++) {
n = i*4;
c = pp[i]
if(c<2){
data[n] = 255-(c*4); // rouge
data[n + 1] = 128-(c*4); // vert
data[n + 2] = 0; // bleu
}
else if(c<21){
data[n] = 0; // rouge
data[n + 1] = 128-(c*4); // vert
data[n + 2] = 255-(c*4); // bleu
}else {
data[n] = c*2; // rouge
data[n + 1] = 128+(c*2); // vert
data[n + 2] = c*2; // bleu
}
data[n + 4] = 255
}
ctx.putImageData(imageData, 0, 0);
document.body.appendChild(canvas)
}*/
</script>
<div id="container"></div>
<div id="hub"></div>
</body></html>