/* * Copyright 2019, kaj dijkstra, * Author, Kaj Dijkstra. * All rights reserved. * */ import scene from "./scene.js"; import shader from "./shader.js"; import mesh from "./mesh.js"; import material from "./material.js"; import sampler2D from "./sampler2D.js"; import entity from "./entity.js"; import {matrix3, matrix4} from "./math.js"; import boundingBox from "./boundingBox.js"; /** * Player object **/ class assimp { constructor() { this.baseUrl = ""; this.modelPath = 'media/models/'; this.meshes = []; this.scene = new scene(); this.shader = new shader(); this.filesToLoad = []; this.name = "new"; this.models = []; this.animations; this.skeleton; this.lightNames = ["PhotometricLight", "FPoint"]; this.shadingModelIDS = []; this.bindMaterial("$ColladaAutoName$_108", 10.0); this.bindMaterial("Box002", 1.0); this.json; this.materialCache; } stringStartsWith( string, compare ) { if (string.substring(0, compare.length) == compare) return true; else return false; } nameIsLight( name ) { for(var c = 0; c 1) { urlParts[urlParts.length-1] = part_two + ".png"; } else { } url = urlParts.join("_"); //remove slash urlParts = url.split("/"); return urlParts[ urlParts.length - 1 ]; } else { return false; } } /** * update object **/ load( jsonFilename, scene ) { console.log("load file", this.modelPath + jsonFilename); var filenameParts = jsonFilename.split("."); this.name = filenameParts[0]; this.scene = scene; this.currentModelFile = jsonFilename; if( typeof kepler !== "undefined" && kepler && kepler.resources ) { kepler.resources.updateFilePreloaderProgress( this.name, 0, 0, false ); } var data = this.loadTextFileSynchronous( this.baseUrl + this.modelPath + jsonFilename ); this.processFiles(data); console.log("filesToLoad", jsonFilename, this); } get_filesize(url, callback) { var xhr = new XMLHttpRequest(); xhr.open("HEAD", url, true); // Notice "HEAD" instead of "GET", // to get only the header xhr.onreadystatechange = function(ev) { if (this.readyState == this.DONE) { console.log('evevev', ev.target.responseURL); var url = ev.target.responseURL; var size = parseInt(ev.target.getResponseHeader("Content-Length")); //kepler.loader.add(url, size ); } }; xhr.engine = kepler; xhr.send(); } /** * load url * @param {string} url */ loadTextFileSynchronous(url, callback) { /* this.get_filesize( url ); var fileContent = kepler.resources.getFile(url) if(fileContent) { return fileContent.data; } */ var request; if (window.XMLHttpRequest) { request = new XMLHttpRequest(); if (request.overrideMimeType) { request.overrideMimeType('text/plain'); } } else if (window.ActiveXObject) { request = new ActiveXObject('MSXML2.XMLHTTP.3.0'); } else { throw 'XMLHttpRequest is disabled'; } request.onprogress = callback = function (evt) { if( typeof kepler !== "undefined" && kepler && kepler.resources && evt && evt.target && evt.target.assimp ) { var label = evt.target.assimp.name; kepler.resources.updateFilePreloaderProgress( label, evt.loaded, evt.total, false ); } }; request.onload = function (evt) { if (request.readyState === request.DONE) { if (request.status === 200) { //console.log(request.response); //console.log(request.responseText); this.assimp.filesToLoad.push({data: request.responseText, filename: request.responseURL }); //'this.assimp.filesToLoad.push(evt.responseText); if( typeof kepler !== "undefined" && kepler && kepler.resources && this.assimp ) { var label = this.assimp.name; var totalBytes = 0; if( request.responseText ) { totalBytes = request.responseText.length; } kepler.resources.updateFilePreloaderProgress( label, totalBytes, totalBytes, true ); } } } }; request.onreadystatechange = function (evt) { console.log("finish", evt.target.readyState); if(evt.target.readyState == 4) { } }; request.open('GET', url, false); request.engine = this.engine; request.assimp = this; request.send(); var data = request.responseText; return data; } processFiles(json) { //var json = this.filesToLoad[0]; this.json = JSON.parse( json ); this.json.parsedMeshes = new Array(); var materials = this.json.materials; var meshes = this.json.meshes; var rootnode = this.json.rootnode; var children = rootnode.children; this.animations = this.json.animations; var materialCache = new Array(); // get materials for(var c = 0; c < materials.length; c++) { var Material = materials[c]; var properties = Material.properties; var name = this.getPropertyByName(properties, '?mat.name', 0).value; var diffuseAdress = this.serializeUrl( this.getPropertyByName(properties, '$tex.file', 1).value, "diff"); var normalAdress = this.serializeUrl( this.getPropertyByName(properties, '$tex.file', 1).value, "ddn"); var roughAdress = this.serializeUrl( this.getPropertyByName(properties, '$tex.file', 1).value, "rough"); var depthAdress = this.serializeUrl( this.getPropertyByName(properties, '$tex.file', 1).value, "depth"); var diffuseColor = this.getPropertyByName(properties, "$clr.diffuse", 0).value; var opacity = this.getPropertyByName(properties, "$mat.opacity", 0).value; var roughness = this.getPropertyByName(properties, "$mat.shinpercent", 0).value; console.log('diffuseAdress', diffuseAdress); var normalTexture = kepler.resources.getTexture("0_floorTiles_ddn.png"); var normalSampler = new sampler2D(); normalSampler.addTexture(normalTexture); var diffuseTexture = kepler.resources.getTexture("0_floorTiles_diff.png"); var diffuseSampler = new sampler2D(); diffuseSampler.addTexture(diffuseTexture); var currentMaterial = new material( ); //if(diffuseAdress) //currentMaterial.addTexture(diffuseSampler); //currentMaterial.addNormal(normalSampler); if(!diffuseColor) { currentMaterial.diffuseColor = [1,1,1]; //currentMaterial.diffuseColor = [Math.random(), Math.random(), Math.random()]; } else { currentMaterial.diffuseColor = diffuseColor; } //currentMaterial.diffuseColor = [Math.random(), Math.random(), Math.random()]; if(kepler.resources.getTexture( this.name + "/" + diffuseAdress )){ //currentMaterial.addTexture(diffuseSampler); //currentMaterial.addNormal(normalSampler); } else { } //instanceMaterial.addRoughness(roughnessSampler); //instanceMaterial.shadingModelID = material.id; currentMaterial.roughness = 0.0; currentMaterial.alpha = 1; //currentMaterial.create(); currentMaterial.roughness = .01; currentMaterial.reflectance = .01; materialCache[c] = currentMaterial; } // get meshes for(var mesh_id = 0; mesh_id < meshes.length; mesh_id++) { this.json.parsedMeshes[mesh_id] = []; var meshInfo = meshes[mesh_id]; this.parseMesh( meshInfo, mesh_id ); } this.addModel( this.json ); //kepler.resources.loadNextTexture(); this.buildModels(materialCache); } parseMesh( meshInfo, mesh_id ) { /* if(meshInfo.faces.length > 60000) { var cutFaces = meshInfo.faces.splice(60000, meshInfo.faces.length); var newMesh = JSON.parse(JSON.stringify( meshInfo )); newMesh.faces = cutFaces; newMesh.name = "sliced mesh"; this.parseMesh( newMesh, mesh_id ); } */ //facesOriginal = //console.log("meshInfo.faces", meshInfo.faces.length); //flatten var indices = []; var vertices = []; var normals = []; for(var i = 0; i max ? arr[len] : max; } return max; } function getMin(arr) { let len = arr.length; let min = Infinity; while (len--) { min = arr[len] < min ? arr[len] : min; } return min; } //console.log(getMax(indices)); var currentMesh = new mesh(); if( meshInfo.texturecoords ){ console.log( meshInfo, "Meshinfo" ); var sliced_vertices = meshInfo.vertices;//.splice(minVert, maxVert);//.slice(min, max); var sliced_normals = meshInfo.normals;//splice(minVert, maxVert); var sliced_tangents = meshInfo.tangents; var sliced_uv = meshInfo.texturecoords[0];//.slice(minUV, maxUV); currentMesh.createMeshFromArrays( indices, sliced_vertices, sliced_normals, sliced_uv, sliced_tangents, meshInfo.bitangents ); currentMesh.material = meshInfo.materialindex; } this.json.parsedMeshes[mesh_id].push(currentMesh); } parseNode( currentAssimpJson, assimpNode, parentEntity, scene, materialCache ) { var currentEntity = new entity(this.engine); currentEntity.name = assimpNode.name; currentEntity.transform.world = matrix4.fromArray(assimpNode.transformation); currentEntity.transform.local = matrix4.fromArray(assimpNode.transformation); if(this.nameIsLight(currentEntity.name)) { currentEntity.type = "PointLight"; scene.addEntity( currentEntity ); } else { if(assimpNode.meshes) { currentEntity.type = "Actor"; } else { currentEntity.type = "transform"; scene.addEntity( currentEntity ); } } if(assimpNode.meshes) { var meshID = assimpNode.meshes[0]; var meshes = currentAssimpJson.parsedMeshes[meshID]; for(var c = 0;c