Skip to content

Commit c372159

Browse files
committed
stairsTest
1 parent 890548b commit c372159

File tree

7 files changed

+194
-43
lines changed

7 files changed

+194
-43
lines changed

Terrain.png

-90.1 KB
Binary file not shown.

assets/models/stairs.blend

661 KB
Binary file not shown.

assets/models/stairs.blend1

661 KB
Binary file not shown.

assets/models/stairso.fbx

14.8 KB
Binary file not shown.

index.html

Lines changed: 88 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -106,14 +106,14 @@ <h3>Gra zatrzymana</h3>
106106
</div>
107107
<script src="js/jq.js?10210281028012"></script>
108108
<script type="module">
109-
import * as THREE from 'https://threejsfundamentals.org/threejs/resources/threejs/r115/build/three.module.js';
109+
import * as THREE from 'https://threejs.org/build/three.module.js';
110110
import {
111111
BufferGeometryUtils
112-
} from 'https://threejsfundamentals.org/threejs/resources/threejs/r115/examples/jsm/utils/BufferGeometryUtils.js';
113-
import Stats from 'https://threejsfundamentals.org/threejs/resources/threejs/r115/examples/jsm/libs/stats.module.js';
112+
} from 'https://threejs.org/examples/jsm/utils/BufferGeometryUtils.js';
113+
import Stats from 'https://threejs.org/examples/jsm/libs/stats.module.js';
114114

115115
import {FirstPersonControls} from './js/fpc.module.js?123';
116-
import { FBXLoader } from 'https://threejsfundamentals.org/threejs/resources/threejs/r115/examples/jsm/loaders/FBXLoader.js';
116+
import { FBXLoader } from 'https://threejs.org/examples/jsm/loaders/FBXLoader.js';
117117

118118
//Renderer
119119
var canvas = document.querySelector('#c');
@@ -127,15 +127,8 @@ <h3>Gra zatrzymana</h3>
127127
// scene.fog= new THREE.Fog(new THREE.Color('lightblue'),50, 64)
128128
scene.fog = new THREE.FogExp2(0xffffff, 0.00015);
129129

130-
//clouds
131-
var loader = new FBXLoader();
132-
loader.load( 'objects/clouds.fbx', function ( object ) {
133-
object.scale.x=0.1
134-
object.scale.y=0.1
135-
object.scale.z=0.1
136-
object.position.y=100
137-
scene.add( object );
138-
} );
130+
131+
139132

140133
//Camera
141134
var camera = new THREE.PerspectiveCamera(75, 2, 0.1, 1000);
@@ -166,12 +159,40 @@ <h3>Gra zatrzymana</h3>
166159

167160

168161
//Textures
169-
var texture = new THREE.TextureLoader().load('Terrain.png');
162+
var texture = new THREE.TextureLoader().load('atla.png');
170163
texture.magFilter = THREE.NearestFilter;
171164

172165
var gameState="menu";
173166

174167

168+
//clouds
169+
var loader = new FBXLoader();
170+
loader.load( 'assets/models/clouds.fbx', function ( object ) {
171+
object.scale.x=0.1
172+
object.scale.y=0.1
173+
object.scale.z=0.1
174+
object.position.y=100
175+
scene.add( object );
176+
} );
177+
178+
var stairsGeometry;
179+
loader.load( 'assets/models/stairso.fbx', function ( object ) {
180+
stairsGeometry=object.children[0].geometry
181+
stairsGeometry.scale(10,10,10)
182+
// object.scale.set(0.01,0.01,0.01)
183+
// object.children[0].material=new THREE.MeshLambertMaterial({
184+
// map: texture,
185+
// side: 2
186+
// })
187+
// // var mesh=new THREE.Mesh(object.geometry,object.material);
188+
// object.children[0].index=null
189+
190+
191+
// scene.add(object)
192+
// object.position.set(26,26,26)
193+
});
194+
195+
175196
class Terrain {
176197
constructor(options) {
177198
this.textureAtlas = options.textureAtlas;
@@ -330,14 +351,23 @@ <h3>Gra zatrzymana</h3>
330351
var voxelGeometries = this.generateVoxelGeometry(x * cellSize + i, y * cellSize + j, z * cellSize + k);
331352
// console.log(voxelGeometries)
332353
for (var l = 0; l < voxelGeometries.length; l++) {
354+
if(voxelGeometries[l].index!=null){
355+
voxelGeometries[l]=voxelGeometries[l].toNonIndexed()
356+
}else{
357+
console.log("NON INDEXED")
358+
}
333359
geometries.push(voxelGeometries[l]);
360+
334361
}
335362
}
336363
}
337364
}
338365
// console.log(geometries)
339366
if (geometries.length != 0) {
367+
// console.log(geometries)
368+
console.log(geometries)
340369
var geometry = BufferGeometryUtils.mergeBufferGeometries(geometries);
370+
// console.log(geometry)
341371
geometry.computeBoundingSphere();
342372
} else {
343373
var geometry = null;
@@ -349,36 +379,49 @@ <h3>Gra zatrzymana</h3>
349379
var voxel = this.getVoxel(x, y, z);
350380
var matrix = new THREE.Matrix4();
351381
matrix.makeTranslation(x, y, z);
352-
if (this.blocks[voxel] != undefined) {
353-
var geometries = [];
354-
if (this.getVoxel(x - 1, y, z) == 0) {
355-
var nxGeometry = this.generateFace("nx", voxel);
356-
geometries.push(nxGeometry.applyMatrix4(matrix))
357-
}
358-
if (this.getVoxel(x + 1, y, z) == 0) {
359-
var pxGeometry = this.generateFace("px", voxel);
360-
geometries.push(pxGeometry.applyMatrix4(matrix))
361-
}
362-
if (this.getVoxel(x, y - 1, z) == 0) {
363-
var nyGeometry = this.generateFace("ny", voxel);
364-
geometries.push(nyGeometry.applyMatrix4(matrix))
365-
}
366-
if (this.getVoxel(x, y + 1, z) == 0) {
367-
var pyGeometry = this.generateFace("py", voxel);
368-
geometries.push(pyGeometry.applyMatrix4(matrix))
369-
}
370-
if (this.getVoxel(x, y, z - 1) == 0) {
371-
var nzGeometry = this.generateFace("nz", voxel);
372-
geometries.push(nzGeometry.applyMatrix4(matrix))
373-
}
374-
if (this.getVoxel(x, y, z + 1) == 0) {
375-
var pzGeometry = this.generateFace("pz", voxel);
376-
geometries.push(pzGeometry.applyMatrix4(matrix))
382+
if(voxel==4){
383+
// console.log(stairsGeometry)
384+
var geo=stairsGeometry.clone()
385+
geo=geo.applyMatrix4(matrix)
386+
geo.translate(0,-0.25,0)
387+
console.log(geo)
388+
return [geo]
389+
390+
391+
392+
}else{
393+
if (this.blocks[voxel] != undefined) {
394+
var geometries = [];
395+
if (this.getVoxel(x - 1, y, z) == 0) {
396+
var nxGeometry = this.generateFace("nx", voxel);
397+
geometries.push(nxGeometry.applyMatrix4(matrix))
398+
}
399+
if (this.getVoxel(x + 1, y, z) == 0) {
400+
var pxGeometry = this.generateFace("px", voxel);
401+
geometries.push(pxGeometry.applyMatrix4(matrix))
402+
}
403+
if (this.getVoxel(x, y - 1, z) == 0) {
404+
var nyGeometry = this.generateFace("ny", voxel);
405+
geometries.push(nyGeometry.applyMatrix4(matrix))
406+
}
407+
if (this.getVoxel(x, y + 1, z) == 0) {
408+
var pyGeometry = this.generateFace("py", voxel);
409+
geometries.push(pyGeometry.applyMatrix4(matrix))
410+
}
411+
if (this.getVoxel(x, y, z - 1) == 0) {
412+
var nzGeometry = this.generateFace("nz", voxel);
413+
geometries.push(nzGeometry.applyMatrix4(matrix))
414+
}
415+
if (this.getVoxel(x, y, z + 1) == 0) {
416+
var pzGeometry = this.generateFace("pz", voxel);
417+
geometries.push(pzGeometry.applyMatrix4(matrix))
418+
}
419+
return geometries;
420+
} else {
421+
return []
377422
}
378-
return geometries;
379-
} else {
380-
return []
381423
}
424+
382425
}
383426
generateFace(type, voxel) {
384427
var geometry = new THREE.PlaneBufferGeometry(1, 1);
@@ -546,7 +589,7 @@ <h3>Gra zatrzymana</h3>
546589
//Setup inventory bar
547590
var inv_bar = new InventoryBar({
548591
boxSize: 60,
549-
boxes: 3,
592+
boxes: 4,
550593
padding: 4,
551594
div: ".inventoryBar",
552595
activeBox: 1
@@ -724,6 +767,7 @@ <h3>Gra zatrzymana</h3>
724767
} else {
725768
//Budowanie
726769
world.setVoxel(positionQ[0], positionQ[1], positionQ[2], inv_bar.activeBox)
770+
// console.log(inv_bar.activeBox)
727771
// console.log(world.cells[world.computeCellId(...positionQ)])
728772
}
729773
}
@@ -737,6 +781,7 @@ <h3>Gra zatrzymana</h3>
737781
function animate() {
738782
stats.begin();
739783
render()
784+
// console.log(renderer.info.render.calls)
740785
stats.end();
741786
requestAnimationFrame(animate)
742787
}

js/mbg.js

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/**
2+
* Combine indexed and non-indexed BufferGeometry into a new indexed BufferGeometry. All missing uniforms are set to 0.
3+
* @param {array} geometries Array of THREE.BufferGeometry instances
4+
* @returns {BufferGeometry}
5+
*/
6+
function mergeBufferGeometries (geometries) {
7+
var indexLength = 0,
8+
verticesLength = 0,
9+
attributesInfos = {},
10+
geometriesInfos = [],
11+
geometryInfo,
12+
referenceAttributesKeys = [],
13+
attributesKeys,
14+
countVertices,
15+
i,
16+
j,
17+
k;
18+
19+
// read the geometries and attributes information, calculate indexLength and verticesLength
20+
21+
for (i = 0; i < geometries.length; i++) {
22+
attributesKeys = Object.keys(geometries[i].attributes);
23+
24+
geometryInfo = {
25+
indexed: geometries[i].index !== null,
26+
vertices: geometries[i].attributes[attributesKeys[0]].count
27+
};
28+
29+
geometriesInfos.push(geometryInfo);
30+
31+
if (geometryInfo.indexed) {
32+
indexLength += geometries[i].index.count;
33+
} else {
34+
indexLength += geometryInfo.vertices;
35+
}
36+
37+
verticesLength += geometryInfo.vertices;
38+
39+
for (j = 0; j < attributesKeys.length; j++) {
40+
if (referenceAttributesKeys.indexOf(attributesKeys[j]) === -1) {
41+
referenceAttributesKeys.push(attributesKeys[j]);
42+
43+
attributesInfos[attributesKeys[j]] = {
44+
array: null,
45+
constructor: geometries[i].attributes[attributesKeys[j]].array.constructor,
46+
itemSize: geometries[i].attributes[attributesKeys[j]].itemSize
47+
};
48+
}
49+
}
50+
}
51+
52+
// prepare the new BufferGeometry and its attributes
53+
54+
var newGeometry = new THREE.BufferGeometry(),
55+
indexArray = verticesLength > 0xFFFF ? new Uint32Array(indexLength) : new Uint16Array(indexLength);
56+
57+
for (i = 0; i < referenceAttributesKeys.length; i++) {
58+
attributesInfos[referenceAttributesKeys[i]].array = new (attributesInfos[referenceAttributesKeys[i]].constructor)(
59+
verticesLength * attributesInfos[referenceAttributesKeys[i]].itemSize
60+
);
61+
62+
newGeometry.addAttribute(referenceAttributesKeys[i], new THREE.BufferAttribute(
63+
attributesInfos[referenceAttributesKeys[i]].array,
64+
attributesInfos[referenceAttributesKeys[i]].itemSize
65+
));
66+
}
67+
68+
// copy all the data in the new BufferGeometry
69+
70+
var offsetIndices = 0,
71+
offsetVertices = 0,
72+
offsetAttribute;
73+
74+
for (i = 0; i < geometries.length; i++) {
75+
76+
geometryInfo = geometriesInfos[i];
77+
78+
if (geometryInfo.indexed) {
79+
for (j = 0; j < geometries[i].index.count; j++) {
80+
indexArray[offsetIndices + j] = offsetVertices + geometries[i].index.array[j];
81+
}
82+
83+
offsetIndices += geometries[i].index.count;
84+
} else {
85+
for (j = 0; j < geometryInfo.vertices; j++) {
86+
indexArray[offsetIndices + j] = offsetVertices + j;
87+
}
88+
89+
offsetIndices += geometryInfo.vertices;
90+
}
91+
92+
for (j = 0; j < referenceAttributesKeys.length; j++) {
93+
offsetAttribute = offsetVertices * attributesInfos[referenceAttributesKeys[j]].itemSize;
94+
95+
if (geometries[i].attributes[referenceAttributesKeys[j]]) {
96+
attributesInfos[referenceAttributesKeys[j]].array.set(geometries[i].attributes[referenceAttributesKeys[j]].array, offsetAttribute);
97+
}
98+
}
99+
100+
offsetVertices += geometryInfo.vertices;
101+
}
102+
103+
newGeometry.setIndex(new THREE.BufferAttribute(indexArray, 1));
104+
105+
return newGeometry;
106+
}

0 commit comments

Comments
 (0)