Hello I am new to three.js and I’ve been trying to do this chunk of code it reads automatically the lines ignore certain value now when on each line i read the points and update the z position of the vertice but even after vertice needs update it won’t can somebody help? here is my code:
function displayContents() {
var geometry = null;
if(reader.readyState==4) {
data = reader.responseText.toString();
var width = 0;height=0;var ignoreValue=0;var arrayToPush = [];
var lines = data.toString().split('\n');
for(var line = 0; line < lines.length; line++){
switch(line) {
case 0 : width = lines[line].split(" ")[1];break;
case 1 : height = lines[line].split(" ")[1];
geometry = new THREE.PlaneGeometry(width, height,width-1,height-1);
break;
case 2 : break;
case 3 : break;
case 4 : break;
case 5 : ignoreValue = parseInt(lines[line].split(" ")[1]);break;
default:
if (geometry == null){
geometry = new THREE.PlaneGeometry(width, height,width-1,height-1);
}
var internalArray = lines[line].split(" ");
//console.log(ignoreValue);
for(var j=0;j < internalArray.length;j++){
if (internalArray[j].toString().trim().length != 0){
//if (parseInt(internalArray[j]) != ignoreValue){
if (parseFloat(internalArray[j]) != ignoreValue){
geometry.vertices[j].setZ(parseFloat(internalArray[j]));
}else{
geometry.vertices[j].setZ(0);
}
}
// }
}
break;
}
}
geometry.verticesNeedUpdate = true;
var material = new THREE.MeshPhongMaterial({
color: 0xdddddd,
wireframe: true
});
var plane = new THREE.Mesh(geometry, material);
scene.add(plane);
}
}
Apologies this is a commented one
var width = window.innerWidth,
height = window.innerHeight;
var scene = new THREE.Scene();
var light = new THREE.AmbientLight(0x404040); // soft white light
scene.add(light);
var camera = new THREE.PerspectiveCamera(40, width / height, 20, 1000);
camera.position.z = 100;
camera.position.x = 200;
camera.position.y = 200;
var renderer = new THREE.WebGLRenderer();
renderer.setSize(width, height);
var data = "";
function displayContents() {
var geometry = null;
if (reader.readyState == 4) {
data = reader.responseText.toString();
var width = 0;
height = 0;
var ignoreValue = 0;
var arrayToPush = [];
var lines = data.toString().split('\n');
for (var line = 0; line < lines.length; line++) {
switch (line) {
case 0:
width = lines[line].split(" ")[1];
break;
case 1:
height = lines[line].split(" ")[1];
geometry = new THREE.PlaneGeometry(width, height, width - 1, height - 1);
break;
case 2:
break;
case 3:
break;
case 4:
break;
case 5:
ignoreValue = parseInt(lines[line].split(" ")[1]);
break;
default: //if geometry not yet set create new plane geometry
if (geometry == null) {
geometry = new THREE.PlaneGeometry(width, height, width - 1, height - 1);
}
var internalArray = lines[line].split(" ");
//looping through each line points and ignore the ones that has the ignored values (set as 0)
for (var j = 0; j < internalArray.length; j++) {
if (internalArray[j].toString().trim().length != 0) {
if (parseFloat(internalArray[j]) != ignoreValue) {
geometry.vertices[j].setZ(parseFloat(internalArray[j]));
} else {
geometry.vertices[j].setZ(0);
}
}
}
break;
}
//if reached the end of the loop update the verdices and create the mesh
if (line == (lines.length - 1)) {
geometry.verticesNeedUpdate = true;
var material = new THREE.MeshPhongMaterial({
color: 0xdddddd,
wireframe: true
});
var plane = new THREE.Mesh(geometry, material);
scene.add(plane);
}
}
}
}
/* reading the file */
var reader = new XMLHttpRequest() || new ActiveXObject('MSXML2.XMLHTTP');
reader.open('get', 'auEF_5m_Heights_v1.1_Clip_Convert.asc', true);
reader.onreadystatechange = displayContents;
reader.send(null);
var controls = new THREE.TrackballControls(camera);
/* animate function to render the scene the file */
var animate = function() {
controls.update();
requestAnimationFrame(animate);
renderer.render(scene, camera);
};
/* check if webgl is detected */
if (Detector.webgl) {
// Initiate function or other initializations here
document.getElementById('webgl1').appendChild(renderer.domElement);
animate();
} else {
var warning = Detector.getWebGLErrorMessage();
document.getElementById('container').appendChild(warning);
}