12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709 |
- "use strict";
- /*
- The MIT License (MIT)
- Copyright (c) 2014-2018 Nikolai Suslov and the Krestianstvo.org project contributors. (https://github.com/NikolaySuslov/livecodingspace/blob/master/LICENSE.md)
- Virtual World Framework Apache 2.0 license (https://github.com/NikolaySuslov/livecodingspace/blob/master/licenses/LICENSE_VWF.md)
- */
- // VWF & A-Frame model driver
- define(["module", "vwf/model", "vwf/utility"], function (module, model, utility) {
- var self;
- return model.load(module, {
- // == Module Definition ====================================================================
- // -- initialize ---------------------------------------------------------------------------
- initialize: function () {
- self = this;
- this.state = {
- nodes: {},
- scenes: {},
- prototypes: {},
- createLocalNode: function (nodeID, childID, childExtendsID, childImplementsIDs,
- childSource, childType, childIndex, childName, callback) {
- return {
- "parentID": nodeID,
- "ID": childID,
- "extendsID": childExtendsID,
- "implementsIDs": childImplementsIDs,
- "source": childSource,
- "type": childType,
- "name": childName,
- "prototypes": undefined,
- "aframeObj": undefined,
- "scene": undefined,
- "componentName": undefined,
- "events": {
- "clickable": false
- }
- };
- },
- isAFrameClass: function (prototypes, classID) {
- if (prototypes) {
- for (var i = 0; i < prototypes.length; i++) {
- if (prototypes[i] === classID) {
- //console.info( "prototypes[ i ]: " + prototypes[ i ] );
- return true;
- }
- }
- }
- return false;
- },
- isAFrameComponent: function (prototypes) {
- var found = false;
- if (prototypes) {
- for (var i = 0; i < prototypes.length && !found; i++) {
- found = (prototypes[i] === "proxy/aframe/node.vwf");
- }
- }
- return found;
- },
- setAFrameProperty: function (propertyName, propertyValue, aframeObject) {
- //console.log(propertyValue);
- if (propertyValue.hasOwnProperty('x')) {
- aframeObject.setAttribute(propertyName, propertyValue)
- } else
- if (Array.isArray(propertyValue)) {
- aframeObject.setAttribute(propertyName, { x: propertyValue[0], y: propertyValue[1], z: propertyValue[2] })
- } else if (typeof propertyValue === 'string') {
- propertyValue.includes(',') ? aframeObject.setAttribute(propertyName, AFRAME.utils.coordinates.parse(propertyValue.split(',').join(' '))) : aframeObject.setAttribute(propertyName, AFRAME.utils.coordinates.parse(propertyValue))
- //aframeObject.setAttribute(propertyName, AFRAME.utils.coordinates.parse(propertyValue))
- } else if (propertyValue.hasOwnProperty('0')) {
- aframeObject.setAttribute(propertyName, { x: propertyValue[0], y: propertyValue[1], z: propertyValue[2] })
- }
- },
- setFromValue: function (propertyValue) {
- var value = goog.vec.Vec3.create();
- if (propertyValue.hasOwnProperty('x')) {
- value = goog.vec.Vec3.createFromValues(propertyValue.x, propertyValue.y, propertyValue.z)
- }
- else if (Array.isArray(propertyValue) || propertyValue instanceof Float32Array) {
- value = goog.vec.Vec3.createFromArray(propertyValue);
- }
- else if (typeof propertyValue === 'string') {
- let val = propertyValue.includes(',') ? AFRAME.utils.coordinates.parse(propertyValue.split(',').join(' ')) : AFRAME.utils.coordinates.parse(propertyValue);
- value = goog.vec.Vec3.createFromValues(val.x, val.y, val.z)
- // let val = AFRAME.utils.coordinates.parse(propertyValue);
- // value = goog.vec.Vec3.createFromValues(val.x, val.y, val.z)
- } else if (propertyValue.hasOwnProperty('0')) {
- value = goog.vec.Vec3.createFromValues(propertyValue[0], propertyValue[1], propertyValue[2])
- }
- return value
- }
- };
- this.state.kernel = this.kernel.kernel.kernel;
- this.aframeDef = {
- 'A-BOX': [
- 'depth', 'height', 'segments-depth',
- 'segments-height', 'segments-width',
- 'width'],
- 'A-SPHERE': [
- 'phi-length', 'phi-start', 'radius',
- 'segments-depth',
- 'segments-height', 'segments-width',
- 'theta-length', 'theta-start'
- ],
- 'A-CYLINDER': [
- 'height', 'radius',
- 'open-ended', 'radius-bottom', 'radius-top',
- 'segments-height', 'segments-radial',
- 'theta-length', 'theta-start'
- ],
- 'A-CONE': [
- 'height',
- 'open-ended', 'radius-bottom', 'radius-top',
- 'segments-height', 'segments-radial',
- 'theta-length', 'theta-start'
- ],
- 'A-PLANE': [
- 'height', 'segments-height', 'segments-width', 'width'
- ],
- 'A-TEXT': [
- 'align', 'alpha-test', 'anchor',
- 'baseline', 'color', 'font',
- 'font-image', 'height',
- 'letter-spacing', 'line-height',
- 'opacity', 'shader',
- 'side', 'tab-size',
- 'transparent', 'value',
- 'white-space', 'width',
- 'wrap-count', 'wrap-pixels',
- 'z-offset', 'negate'
- ],
- 'A-SKY': [
- 'phi-length', 'phi-start', 'radius', 'segments-height',
- 'segments-width',
- 'theta-length', 'theta-start',
- ],
- 'A-LIGHT': [
- 'angle', 'color', 'decay', 'distance',
- 'ground-color', 'intensity', 'penumbra',
- 'type', 'target'
- ]
- }
- },
- // == Model API ============================================================================
- // -- creatingNode -------------------------------------------------------------------------
- creatingNode: function (nodeID, childID, childExtendsID, childImplementsIDs,
- childSource, childType, childIndex, childName, callback /* ( ready ) */) {
- // If the parent nodeID is 0, this node is attached directly to the root and is therefore either
- // the scene or a prototype. In either of those cases, save the uri of the new node
- var childURI = (nodeID === 0 ? childIndex : undefined);
- var appID = this.kernel.application();
- // If the node being created is a prototype, construct it and add it to the array of prototypes,
- // and then return
- var prototypeID = utility.ifPrototypeGetId(appID, this.state.prototypes, nodeID, childID);
- if (prototypeID !== undefined) {
- this.state.prototypes[prototypeID] = {
- parentID: nodeID,
- ID: childID,
- extendsID: childExtendsID,
- implementsID: childImplementsIDs,
- source: childSource,
- type: childType,
- name: childName
- };
- return;
- }
- var protos = getPrototypes(this.kernel, childExtendsID);
- //var kernel = this.kernel.kernel.kernel;
- var node;
- if (this.state.isAFrameComponent(protos)) {
- // Create the local copy of the node properties
- if (this.state.nodes[childID] === undefined) {
- this.state.nodes[childID] = this.state.createLocalNode(nodeID, childID, childExtendsID, childImplementsIDs,
- childSource, childType, childIndex, childName, callback);
- }
- node = this.state.nodes[childID];
- node.prototypes = protos;
- // if (childType == "component") {
- // if (nodeID !== undefined) {
- // node.aframeObj = setAFrameObjectComponents(node);
- // addNodeToHierarchy(node);
- // }
- // } else {
- node.aframeObj = createAFrameObject(node);
- addNodeToHierarchy(node);
- if (isAEntityDefinition(node.prototypes)) {
- //updateStoredTransform( node );
- updateStoredTransformFor(node, 'position');
- updateStoredTransformFor(node, 'rotation');
- updateStoredTransformFor(node, 'scale');
- }
- //notifyDriverOfPrototypeAndBehaviorProps();
- // }
- //addNodeToHierarchy(node);
- }
- },
- // initializingNode: function( nodeID, childID, childExtendsID, childImplementsIDs,
- // childSource, childType, childIndex, childName ) {
- // var node = this.state.nodes[childID];
- // if ( node && childType == "component" ) {
- // }
- // },
- // -- initializingProperty -----------------------------------------------------------------
- initializingProperty: function (nodeID, propertyName, propertyValue) {
- var value = undefined;
- var node = this.state.nodes[nodeID];
- if (node !== undefined) {
- value = this.settingProperty(nodeID, propertyName, propertyValue);
- }
- return value;
- },
- // -- creatingProperty ---------------------------------------------------------------------
- creatingProperty: function (nodeID, propertyName, propertyValue) {
- return this.initializingProperty(nodeID, propertyName, propertyValue);
- },
- // -- callingMethod ------------------------------------------------------------------------
- callingMethod: function (nodeID, methodName, methodParameters) {
- var node = this.state.nodes[nodeID];
- if (!node) return;
- if (node && node.aframeObj) {
- if (methodName == 'lookAt') {
- console.log('lookAt: ' + methodParameters[0]);
- let target = methodParameters[0];
- node.aframeObj.object3D.lookAt(new THREE.Vector3(target.x, target.y, target.z));
- let newRotation = node.aframeObj.getAttribute('rotation');
- self.kernel.setProperty(nodeID, "rotation", { x: 0, y: newRotation.y, z: 0 });
- }
- if (methodName == 'worldRotation') {
- var worldQuat = new THREE.Quaternion();
- node.aframeObj.object3D.getWorldQuaternion(worldQuat);
- let angle = (new THREE.Euler()).setFromQuaternion(worldQuat, 'YXZ');
- let rotation = (new THREE.Vector3(THREE.Math.radToDeg(angle.x),
- THREE.Math.radToDeg(angle.y), THREE.Math.radToDeg(angle.z)));
- return rotation
- }
- if (methodName == 'worldPosition') {
- var position = new THREE.Vector3();
- node.aframeObj.object3D.getWorldPosition(position);
- return position
- }
- }
- },
- // -- deletingNode -------------------------------------------------------------------------
- //deletingNode: function( nodeID ) {
- //},
- // -- deletingNode -------------------------------------------------------------------------
- deletingNode: function (nodeID) {
- if (nodeID) {
- var childNode = this.state.nodes[nodeID];
- if (!childNode) return;
- if (childNode !== undefined) {
- if (childNode.children) {
- for (var i = 0; i < childNode.children.length; i++) {
- this.deletingNode(childNode.children[i]);
- }
- }
- if (childNode.aframeObj !== undefined) {
- // removes and destroys object
- childNode.aframeObj.parentNode.removeChild(childNode.aframeObj);
- childNode.aframeObj.destroy();
- childNode.aframeObj = undefined;
- }
- delete this.state.nodes[nodeID];
- }
- }
- },
- // -- settingProperty ----------------------------------------------------------------------
- settingProperty: function (nodeID, propertyName, propertyValue) {
- var node = this.state.nodes[nodeID];
- var value = undefined;
- // if (node.componentName == 'line') {
- // console.log(node.aframeObj);
- // }
- if (node && node.aframeObj && utility.validObject(propertyValue)) {
- var aframeObject = node.aframeObj;
- if (isNodeDefinition(node.prototypes)) {
- // 'id' will be set to the nodeID
- value = propertyValue;
- switch (propertyName) {
- default:
- value = undefined;
- break;
- }
- }
- if (value === undefined && isAEntityDefinition(node.prototypes)) {
- value = propertyValue;
- switch (propertyName) {
- case "position":
- var position = this.state.setFromValue(propertyValue || []); //goog.vec.Vec3.createFromArray( propertyValue || [] );
- node.transform.position = goog.vec.Vec3.clone(position);
- //value = propertyValue;
- node.transform.storedPositionDirty = true;
- //setTransformsDirty( threeObject );
- //this.state.setAFrameProperty('position', propertyValue, aframeObject);
- break;
- case "rotation":
- var rotation = this.state.setFromValue(propertyValue || []); //goog.vec.Vec3.createFromArray( propertyValue || [] );
- node.transform.rotation = goog.vec.Vec3.clone(rotation);
- //value = propertyValue;
- node.transform.storedRotationDirty = true;
- //this.state.setAFrameProperty('rotation', propertyValue, aframeObject);
- break;
- case "scale":
- var scale = this.state.setFromValue(propertyValue || []); //goog.vec.Vec3.createFromArray( propertyValue || [] );
- node.transform.scale = goog.vec.Vec3.clone(scale);
- //value = propertyValue;
- node.transform.storedScaleDirty = true;
- //setTransformsDirty( threeObject );
- //this.state.setAFrameProperty('position', propertyValue, aframeObject);
- //this.state.setAFrameProperty('scale', propertyValue, aframeObject);
- break;
- case "animationTimeUpdated":
- if (node.transform) {
- node.transform.storedPositionDirty = true;
- node.transform.storedRotationDirty = true;
- node.transform.storedScaleDirty = true;
- }
- break;
- case "clickable":
- if (propertyValue) {
- aframeObject.setAttribute('class', 'intersectable');
- } else {
- aframeObject.setAttribute('class', 'nonintersectable');
- }
- node.events.clickable = propertyValue;
- break;
- case "class":
- var newClasses = [];
- if (propertyValue.includes(',')){
- newClasses = propertyValue.split(',');
- } else {
- newClasses = propertyValue.split(' ')
- }
-
- // let newClasses = propertyValue.split(','); //trim()
- aframeObject.setAttribute('class',"");
- newClasses.forEach(el=>{
- aframeObject.classList.add(el.trim());
- })
- break;
- case "ownedBy":
- aframeObject.setAttribute('ownedby', propertyValue);
- break;
- case "visible":
- aframeObject.setAttribute('visible', propertyValue);
- break;
- // case "clickable":
- // console.log("set clickable!");
- // value = propertyValue;
- // break;
- // case "clickable":
- // if (propertyValue) {
- // aframeObject.addEventListener('click', function (evt) {
- // console.log("click!");
- // vwf_view.kernel.fireEvent(node.ID, "clickEvent",evt.detail.cursorEl.id);
- // });
- // }
- // break;
- // case "look-controls-enabled":
- // aframeObject.setAttribute('look-controls', 'enabled', propertyValue);
- // break;
- case "wasd-controls":
- aframeObject.setAttribute('wasd-controls', 'enabled', propertyValue);
- break;
- default:
- value = undefined;
- break;
- }
- }
- if (value === undefined && aframeObject.nodeName == "A-ASSET-ITEM") {
- value = propertyValue;
- switch (propertyName) {
- case "itemID":
- aframeObject.setAttribute('id', propertyValue);
- break;
- case "itemSrc":
- aframeObject.setAttribute('src', propertyValue);
- break;
- default:
- value = undefined;
- break;
- }
- }
- if (value === undefined && aframeObject.nodeName == "IMG") {
- value = propertyValue;
- switch (propertyName) {
- case "itemID":
- aframeObject.setAttribute('id', propertyValue);
- break;
- case "itemSrc":
- aframeObject.setAttribute('src', propertyValue);
- break;
- // case "width":
- // aframeObject.width = propertyValue;
- // break;
- // case "height":
- // aframeObject.height = propertyValue;
- // break;
- default:
- value = undefined;
- break;
- }
- }
- if (value === undefined && aframeObject.nodeName == "AUDIO") {
- value = propertyValue;
- switch (propertyName) {
- case "itemID":
- aframeObject.setAttribute('id', propertyValue);
- break;
- case "itemSrc":
- aframeObject.setAttribute('src', propertyValue);
- break;
- default:
- value = undefined;
- break;
- }
- }
- if (value === undefined && aframeObject.nodeName == "VIDEO") {
- value = propertyValue;
- switch (propertyName) {
- case "itemID":
- aframeObject.setAttribute('id', propertyValue);
- break;
- case "itemSrc":
- aframeObject.setAttribute('src', propertyValue);
- break;
- default:
- value = undefined;
- break;
- }
- }
- if (value === undefined && aframeObject.nodeName == "A-SKY") {
- value = propertyValue;
- self.aframeDef['A-SKY'].forEach(element => {
- element == propertyName ? aframeObject.setAttribute(element, propertyValue) :
- value = undefined;
- })
- }
- if (value === undefined && aframeObject.nodeName == "A-TEXT") {
- value = propertyValue;
- //.filter(el=>el !== 'font')
- self.aframeDef['A-TEXT'].forEach(element => {
-
- element == propertyName ? aframeObject.setAttribute(element, propertyValue) :
- value = undefined;
-
- })
- // if(propertyName == 'font'){
- // console.log('Loading font...', element);
- // }
- }
- if (value === undefined && aframeObject.nodeName == "A-SCENE") {
- value = propertyValue;
- switch (propertyName) {
- case "color":
- aframeObject.setAttribute('background', { 'color': propertyValue });
- break;
- case "transparent":
- aframeObject.setAttribute('background', { 'transparent': propertyValue });
- break;
- // case "fog":
- // aframeObject.setAttribute('fog', propertyValue);
- // break;
- case "assets":
- initAssets(propertyValue)
- break;
- default:
- value = undefined;
- break;
- }
- }
- if (value === undefined && aframeObject.nodeName == "A-BOX") {
- value = propertyValue;
- self.aframeDef['A-BOX'].forEach(element => {
- element == propertyName ? aframeObject.setAttribute(element, propertyValue) :
- value = undefined;
- })
- }
- if (value === undefined && aframeObject.nodeName == "A-LIGHT") {
- value = propertyValue;
- self.aframeDef['A-LIGHT'].forEach(element => {
- element == propertyName ? aframeObject.setAttribute(element, propertyValue) :
- value = undefined;
- })
- switch (propertyName) {
- case "castShadow":
- aframeObject.setAttribute('light', 'castShadow', propertyValue);
- break;
- case "shadowCameraVisible":
- aframeObject.setAttribute('light', 'shadowCameraVisible', propertyValue);
- break;
- default:
- value = undefined;
- break;
- }
- }
- if (value === undefined && aframeObject.nodeName == "A-GLTF-MODEL") {
- value = propertyValue;
- switch (propertyName) {
- case "src":
- aframeObject.setAttribute('src', propertyValue);
- break;
- default:
- value = undefined;
- break;
- }
- }
- if (value === undefined && aframeObject.nodeName == "A-COLLADA-MODEL") {
- value = propertyValue;
- switch (propertyName) {
- case "src":
- aframeObject.setAttribute('src', propertyValue);
- break;
- default:
- value = undefined;
- break;
- }
- }
- if (value === undefined && aframeObject.nodeName == "A-OBJ-MODEL") {
- value = propertyValue;
- switch (propertyName) {
- case "src":
- aframeObject.setAttribute('src', propertyValue);
- break;
- case "mtl":
- aframeObject.setAttribute('mtl', propertyValue);
- break;
- default:
- value = undefined;
- break;
- }
- }
- if (value === undefined && aframeObject.nodeName == "A-PLANE") {
- value = propertyValue;
- self.aframeDef['A-PLANE'].forEach(element => {
- element == propertyName ? aframeObject.setAttribute(element, propertyValue) :
- value = undefined;
- })
- }
- if (value === undefined && aframeObject.nodeName == "A-SPHERE") {
- value = propertyValue;
- self.aframeDef['A-SPHERE'].forEach(element => {
- element == propertyName ? aframeObject.setAttribute(element, propertyValue) :
- value = undefined;
- })
- }
- if (value === undefined && aframeObject.nodeName == "A-CYLINDER") {
- value = propertyValue;
- self.aframeDef['A-CYLINDER'].forEach(element => {
- element == propertyName ? aframeObject.setAttribute(element, propertyValue) :
- value = undefined;
- })
- }
- if (value === undefined && aframeObject.nodeName == "A-CONE") {
- value = propertyValue;
- self.aframeDef['A-CONE'].forEach(element => {
- element == propertyName ? aframeObject.setAttribute(element, propertyValue) :
- value = undefined;
- })
- }
- if (value === undefined && aframeObject.nodeName == "A-ANIMATION") {
- value = propertyValue;
- switch (propertyName) {
- // attribute:
- // dur:
- // from:
- // to:
- // repeat:
- case "dur":
- aframeObject.setAttribute('dur', propertyValue);
- break;
- case "from":
- aframeObject.setAttribute('from', propertyValue);
- break;
- case "to":
- aframeObject.setAttribute('to', propertyValue);
- break;
- case "repeat":
- aframeObject.setAttribute('repeat', propertyValue);
- break;
- case "attribute":
- aframeObject.setAttribute('attribute', propertyValue);
- break;
- case "begin":
- aframeObject.setAttribute('begin', propertyValue);
- break;
- default:
- value = undefined;
- break;
- }
- }
- if (value === undefined && aframeObject.nodeName == "A-CAMERA") {
- value = propertyValue;
- switch (propertyName) {
- case "user-height":
- aframeObject.setAttribute('user-height', propertyValue);
- break;
- case "look-controls-enabled":
- aframeObject.setAttribute('look-controls-enabled', propertyValue);
- break;
- case "wasd-controls-enabled":
- aframeObject.setAttribute('wasd-controls-enabled', propertyValue);
- break;
- // case "active":
- // aframeObject.setAttribute('camera', 'active', propertyValue);
- // break;
- default:
- value = undefined;
- break;
- }
- }
- if (value === undefined && aframeObject.nodeName == "A-SUN-SKY") {
- value = propertyValue;
- switch (propertyName) {
- case "sunPosition":
- aframeObject.setAttribute('material', 'sunPosition', propertyValue);
- break;
- // case "active":
- // aframeObject.setAttribute('camera', 'active', propertyValue);
- // break;
- default:
- value = undefined;
- break;
- }
- }
- if (value === undefined && aframeObject.nodeName == "A-ANCHOR") {
- value = propertyValue;
- switch (propertyName) {
- case "changeMatrixMode":
- aframeObject.setAttribute('arjs-anchor', 'changeMatrixMode', propertyValue);
- break;
- case 'hit-testing-enabled':
- aframeObject.setAttribute('hit-testing-enabled', propertyValue);
- break;
- case 'preset':
- aframeObject.setAttribute('preset', propertyValue);
- break;
- case 'markerType':
- aframeObject.setAttribute('type', propertyValue);
- break;
- case 'markerValue':
- aframeObject.setAttribute('velue', propertyValue);
- break;
- default:
- value = undefined;
- break;
- }
-
- }
- }
- return value;
- },
- // -- gettingProperty ----------------------------------------------------------------------
- gettingProperty: function (nodeID, propertyName, propertyValue) {
- var node = this.state.nodes[nodeID];
- var value = undefined;
- if (node && node.aframeObj) {
- var aframeObject = node.aframeObj;
- if (isNodeDefinition(node.prototypes)) {
- switch (propertyName) {
- }
- }
- if (value === undefined && isAEntityDefinition(node.prototypes)) {
- switch (propertyName) {
- case "position":
- // var pos = aframeObject.getAttribute('position');
- // if (pos !== undefined) {
- // value = pos//[pos.x, pos.y, pos.z]//AFRAME.utils.coordinates.stringify(pos);
- // }
- if (node.transform.position) {
- if (node.transform.storedPositionDirty) {
- updateStoredTransformFor(node, 'position');
- }
- value = goog.vec.Vec3.clone(node.transform.position);
- //value = node.transform.position;
- }
- break;
- case "rotation":
- if (node.transform.rotation) {
- if (node.transform.storedRotationDirty) {
- updateStoredTransformFor(node, 'rotation');
- }
- value = goog.vec.Vec3.clone(node.transform.rotation);
- // var rot = aframeObject.getAttribute('rotation');
- // if (rot !== undefined) {
- // value = rot//AFRAME.utils.coordinates.stringify(rot);
- // }
- }
- break;
- case "scale":
- if (node.transform.scale) {
- if (node.transform.storedScaleDirty) {
- updateStoredTransformFor(node, 'scale');
- }
- value = goog.vec.Vec3.clone(node.transform.scale);
- // var scale = aframeObject.getAttribute('scale');
- // if (scale !== undefined) {
- // value = scale//AFRAME.utils.coordinates.stringify(scale);
- // }
- }
- break;
- case "clickable":
- value = node.events.clickable;
- break;
- case "class":
- value = aframeObject.classList.toString();
- //aframeObject.getAttribute('class');
- break;
- // case "look-controls-enabled":
- // var look = aframeObject.getAttribute('look-controls-enabled');
- // if (look !== null && look !== undefined) {
- // value = aframeObject.getAttribute('look-controls').enabled;
- // }
- // break;
- // case "wasd-controls":
- // var wasd = aframeObject.getAttribute('wasd-controls');
- // if (wasd !== null && wasd !== undefined) {
- // value = aframeObject.getAttribute('wasd-controls').enabled;
- // }
- // break;
- case "ownedBy":
- value = aframeObject.getAttribute('ownedby');
- break;
- case "visible":
- value = aframeObject.getAttribute('visible');
- break;
- }
- }
- if (value === undefined && aframeObject.nodeName == "A-ASSET-ITEM") {
- switch (propertyName) {
- case "itemID":
- value = aframeObject.getAttribute('id');
- break;
- case "itemSrc":
- value = aframeObject.getAttribute('src');
- break;
- }
- }
- if (value === undefined && aframeObject.nodeName == "IMG") {
- switch (propertyName) {
- case "itemID":
- value = aframeObject.getAttribute('id');
- break;
- case "itemSrc":
- value = aframeObject.getAttribute('src');
- break;
- case "width":
- value = aframeObject.width;
- break;
- case "height":
- value = aframeObject.height;
- break;
- }
- }
- if (value === undefined && aframeObject.nodeName == "AUDIO") {
- switch (propertyName) {
- case "itemID":
- value = aframeObject.getAttribute('id');
- break;
- case "itemSrc":
- value = aframeObject.getAttribute('src');
- break;
- }
- }
- if (value === undefined && aframeObject.nodeName == "VIDEO") {
- switch (propertyName) {
- case "itemID":
- value = aframeObject.getAttribute('id');
- break;
- case "itemSrc":
- value = aframeObject.getAttribute('src');
- break;
- case "videoWidth":
- value = aframeObject.videoWidth;
- break;
- case "videoHeight":
- value = aframeObject.videoHeight;
- break;
- }
- }
- if (value === undefined && aframeObject.nodeName == "A-SCENE") {
- switch (propertyName) {
- // case "fog":
- // value = aframeObject.getAttribute('fog');
- // break;
- case "assets":
- break;
- case "color":
- if (aframeObject.getAttribute('background')) {
- value = aframeObject.getAttribute('background').color;
- }
- break;
- case "transparent":
- if (aframeObject.getAttribute('background')) {
- value = aframeObject.getAttribute('background').transparent;
- }
- break;
- }
- }
- if (value === undefined && aframeObject.nodeName == "A-SKY") {
- self.aframeDef['A-SKY'].forEach(element => {
- if (element == propertyName) {
- value = aframeObject.getAttribute(element);
- }
- })
- }
- if (value === undefined && aframeObject.nodeName == "A-LIGHT") {
- self.aframeDef['A-LIGHT'].forEach(element => {
- if (element == propertyName) {
- value = aframeObject.getAttribute(element);
- }
- })
- switch (propertyName) {
- case "castShadow":
- value = aframeObject.getAttribute('light').castShadow;
- break;
- case "shadowCameraVisible":
- value = aframeObject.getAttribute('light').shadowCameraVisible;
- break;
- }
- }
- if (value === undefined && aframeObject.nodeName == "A-BOX") {
- self.aframeDef['A-BOX'].forEach(element => {
- if (element == propertyName) {
- value = aframeObject.getAttribute(element);
- }
- })
- }
- if (value === undefined && aframeObject.nodeName == "A-PLANE") {
- self.aframeDef['A-PLANE'].forEach(element => {
- if (element == propertyName) {
- value = aframeObject.getAttribute(element);
- }
- })
- }
- if (value === undefined && aframeObject.nodeName == "A-SPHERE") {
- self.aframeDef['A-SPHERE'].forEach(element => {
- if (element == propertyName) {
- value = aframeObject.getAttribute(element);
- }
- })
- }
- if (value === undefined && aframeObject.nodeName == "A-CYLINDER") {
- self.aframeDef['A-CYLINDER'].forEach(element => {
- if (element == propertyName) {
- value = aframeObject.getAttribute(element);
- }
- })
- }
- if (value === undefined && aframeObject.nodeName == "A-CONE") {
- self.aframeDef['A-CONE'].forEach(element => {
- if (element == propertyName) {
- value = aframeObject.getAttribute(element);
- }
- })
- }
- if (value === undefined && aframeObject.nodeName == "A-TEXT") {
- self.aframeDef['A-TEXT'].forEach(element => {
- if (element == propertyName) {
- value = aframeObject.getAttribute(element);
- }
- })
- }
- if (value === undefined && aframeObject.nodeName == "A-ANIMATION") {
- switch (propertyName) {
- case "attribute":
- value = aframeObject.getAttribute('attribute');
- break;
- case "dur":
- value = aframeObject.getAttribute('dur');
- break;
- case "from":
- value = aframeObject.getAttribute('from');
- break;
- case "to":
- value = aframeObject.getAttribute('to');
- break;
- case "repeat":
- value = aframeObject.getAttribute('repeat');
- break;
- case "begin":
- value = aframeObject.getAttribute('begin');
- break;
- }
- }
- if (value === undefined && aframeObject.nodeName == "A-CAMERA") {
- switch (propertyName) {
- case "user-height":
- value = aframeObject.getAttribute('user-height');
- break;
- case "look-controls-enabled":
- value = aframeObject.getAttribute('look-controls-enabled');
- break;
- case "wasd-controls-enabled":
- value = aframeObject.getAttribute('wasd-controls-enabled');
- break;
- }
- // switch (propertyName) {
- // case "active":
- // value = aframeObject.getAttribute('camera').active;
- // break;
- // }
- }
- if (value === undefined && aframeObject.nodeName == "A-SUN-SKY") {
- switch (propertyName) {
- case "sunPosition":
- value = aframeObject.getAttribute('material').sunPosition;
- break;
- }
- // switch (propertyName) {
- // case "active":
- // value = aframeObject.getAttribute('camera').active;
- // break;
- // }
- }
- if (value === undefined && aframeObject.nodeName == "A-COLLADA-MODEL") {
- switch (propertyName) {
- case "src":
- value = aframeObject.getAttribute('src');
- break;
- }
- }
- if (value === undefined && aframeObject.nodeName == "A-OBJ-MODEL") {
- switch (propertyName) {
- case "src":
- value = aframeObject.getAttribute('src');
- break;
- case "mtl":
- value = aframeObject.getAttribute('mtl');
- break;
- }
- }
- if (value === undefined && aframeObject.nodeName == "A-GLTF-MODEL") {
- switch (propertyName) {
- case "src":
- value = aframeObject.getAttribute('src');
- break;
- }
- }
- if (value === undefined && aframeObject.nodeName == "A-ANCHOR") {
- switch (propertyName) {
- case "changeMatrixMode":
- value = aframeObject.getAttribute('arjs-anchor').changeMatrixMode;
- break;
- case "hit-testing-enabled":
- value = aframeObject.getAttribute('hit-testing-enabled');
- break;
- case "preset":
- value = aframeObject.getAttribute('preset');
- break;
- case 'markerType':
- value = aframeObject.getAttribute('type');
- break;
-
- case 'markerValue':
- value = aframeObject.getAttribute('value');
- break;
- }
- }
- }
- if (value !== undefined) {
- propertyValue = value;
- }
- return value;
- }
- });
- function createAFrameObject(node, config) {
- var protos = node.prototypes;
- var aframeObj = undefined;
- if (self.state.isAFrameClass(protos, "proxy/aframe/ascene.vwf")) {
- aframeObj = document.createElement('a-scene');
- let assetsElement = document.createElement('a-assets');
- aframeObj.appendChild(assetsElement);
- aframeObj.setAttribute('scene-utils', "");
- aframeObj.setAttribute('light', 'defaultLightsEnabled', false);
- //aframeObj.setAttribute('embedded', {});
- //aframeObj.setAttribute('loading-screen', "backgroundColor: black");
- self.state.scenes[node.ID] = aframeObj;
- //TODO: move from veiw here
- //document.body.appendChild(aframeObj);
- } else if (self.state.isAFrameClass(protos, "proxy/aframe/a-asset-item.vwf")) {
- let assets = document.querySelector('a-assets');
- if (assets) {
- aframeObj = document.createElement('a-asset-item');
- aframeObj.setAttribute('id', "item-" + GUID());
- aframeObj.setAttribute('src', "");
- aframeObj.setAttribute('crossorigin', "anonymous");
- assets.appendChild(aframeObj);
- }
- } else if (self.state.isAFrameClass(protos, "proxy/aframe/a-asset-image-item.vwf")) {
- let assets = document.querySelector('a-assets');
- if (assets) {
- let elID = "item-" + GUID();
- aframeObj = document.createElement('img');
- aframeObj.setAttribute('id', elID);
- aframeObj.setAttribute('src', "");
- aframeObj.setAttribute('crossorigin', "anonymous");
- assets.appendChild(aframeObj);
- }
- } else if (self.state.isAFrameClass(protos, "proxy/aframe/a-asset-audio-item.vwf")) {
- let assets = document.querySelector('a-assets');
- if (assets) {
- aframeObj = document.createElement('audio');
- aframeObj.setAttribute('id', "item-" + GUID());
- aframeObj.setAttribute('src', "");
- aframeObj.setAttribute('crossorigin', "anonymous");
- assets.appendChild(aframeObj);
- }
- } else if (self.state.isAFrameClass(protos, "proxy/aframe/a-asset-video-item.vwf")) {
- let assets = document.querySelector('a-assets');
- if (assets) {
- aframeObj = document.createElement('video');
- aframeObj.setAttribute('id', "item-" + GUID());
- aframeObj.setAttribute('src', "");
- aframeObj.setAttribute('crossorigin', "anonymous");
- aframeObj.setAttribute('autoplay', "");
- aframeObj.setAttribute('loop', true);
- assets.appendChild(aframeObj);
- }
- } else if (self.state.isAFrameClass(protos, "proxy/aframe/asky.vwf")) {
- aframeObj = document.createElement('a-sky');
- } else if (self.state.isAFrameClass(protos, "proxy/aframe/a-arjs-anchor.vwf")) {
- aframeObj = document.createElement('a-anchor');
-
- } else if (self.state.isAFrameClass(protos, "proxy/aframe/alight.vwf")) {
- aframeObj = document.createElement('a-light');
- } else if (self.state.isAFrameClass(protos, "proxy/aframe/acursor.vwf")) {
- aframeObj = document.createElement('a-cursor');
- } else if (self.state.isAFrameClass(protos, "proxy/aframe/a-sun-sky.vwf")) {
- aframeObj = document.createElement('a-sun-sky');
- } else if (self.state.isAFrameClass(protos, "proxy/aframe/abox.vwf")) {
- aframeObj = document.createElement('a-box');
- } else if (self.state.isAFrameClass(protos, "proxy/aframe/aplane.vwf")) {
- aframeObj = document.createElement('a-plane');
- } else if (self.state.isAFrameClass(protos, "proxy/aframe/acylinder.vwf")) {
- aframeObj = document.createElement('a-cylinder');
- } else if (self.state.isAFrameClass(protos, "proxy/aframe/acone.vwf")) {
- aframeObj = document.createElement('a-cone');
- } else if (self.state.isAFrameClass(protos, "proxy/aframe/atext.vwf")) {
- aframeObj = document.createElement('a-text');
- } else if (self.state.isAFrameClass(protos, "proxy/aframe/acolladamodel.vwf")) {
- aframeObj = document.createElement('a-collada-model');
- } else if (self.state.isAFrameClass(protos, "proxy/aframe/aobjmodel.vwf")) {
- aframeObj = document.createElement('a-obj-model');
- } else if (self.state.isAFrameClass(protos, "proxy/aframe/agltfmodel.vwf")) {
- aframeObj = document.createElement('a-gltf-model');
- } else if (self.state.isAFrameClass(protos, "proxy/aframe/asphere.vwf")) {
- aframeObj = document.createElement('a-sphere');
- } else if (self.state.isAFrameClass(protos, "proxy/aframe/aanimation.vwf")) {
- aframeObj = document.createElement('a-animation');
- } else if (self.state.isAFrameClass(protos, "proxy/aframe/acamera.vwf")) {
- aframeObj = document.createElement('a-camera');
- aframeObj.setAttribute('camera', 'active', false);
- } else if (self.state.isAFrameClass(protos, "proxy/aframe/aentity.vwf")) {
- aframeObj = document.createElement('a-entity');
- }
- if (aframeObj.nodeName !== "A-ASSET-ITEM" &&
- aframeObj.nodeName !== "IMG" &&
- aframeObj.nodeName !== "AUDIO" &&
- aframeObj.nodeName !== "VIDEO"
- ) {
- aframeObj.setAttribute('id', node.ID);
- }
- return aframeObj;
- }
- function addNodeToHierarchy(node) {
- if (node.aframeObj) {
- if (self.state.nodes[node.parentID] !== undefined) {
- var parent = self.state.nodes[node.parentID];
- if (parent.aframeObj) {
- if (parent.children === undefined) {
- parent.children = [];
- }
- parent.children.push(node.ID);
- //console.info( "Adding child: " + childID + " to " + nodeID );
- if (node.aframeObj.nodeName !== "A-ASSET-ITEM" &&
- node.aframeObj.nodeName !== "IMG" &&
- node.aframeObj.nodeName !== "AUDIO" &&
- node.aframeObj.nodeName !== "VIDEO"
- ) {
- parent.aframeObj.appendChild(node.aframeObj);
- }
- }
- }
- if (node.aframeObj.nodeName !== "A-SCENE") {
- node.scene = self.state.scenes[self.kernel.application()];
- }
- }
- }
- function updateStoredTransform(node) {
- if (node && node.aframeObj) {
- // Add a local model-side transform that can stay pure even if the view changes the
- // transform on the threeObject - this already happened in creatingNode for those nodes that
- // didn't need to load a model
- node.transform = {};
- //let pos = node.aframeObj.object3D.position;
- let pos = (new THREE.Vector3()).copy(node.aframeObj.object3D.position);
- node.transform.position = goog.vec.Vec3.createFromValues(pos.x, pos.y, pos.z);
- //node.transform.position = AFRAME.utils.coordinates.stringify(node.aframeObj.object3D.position);
- node.transform.rotation = AFRAME.utils.coordinates.stringify(node.aframeObj.object3D.rotation);
- node.storedTransformDirty = false;
- }
- }
- async function initAssets(propertyValue){
- let assetsEl = document.querySelector('a-assets');
- if (!assetsEl) {
- let newAssetsEl = document.createElement('a-assets');
- aframeObject.appendChild(newAssetsEl);
- }
- var assetsElement = document.querySelector('a-assets');
- if (propertyValue) {
- let path = JSON.parse(localStorage.getItem('lcs_app')).path.public_path;
- let worldName = path.slice(1);
- let dbPath = propertyValue.split(".").join("_");
- let userDB = _LCSDB.user(_LCS_WORLD_USER.pub);
- userDB.get('worlds').get(worldName).get(dbPath).load(function(response) {
- if (response) {
- if (Object.keys(response).length > 0) {
- //console.log(JSON.parse(response));
- let assets = (typeof(response) == 'object') ? response: JSON.parse(response);
- for (var prop in assets) {
- var elm = document.createElement(assets[prop].tag);
- elm.setAttribute('id', prop);
- elm.setAttribute('src', assets[prop].src);
- assetsElement.appendChild(elm);
- }
- }
- }
- });
- // httpGetJson(propertyValue).then(function (response) {
- // console.log(JSON.parse(response));
- // let assets = JSON.parse(response);
- // for (var prop in assets) {
- // var elm = document.createElement(assets[prop].tag);
- // elm.setAttribute('id', prop);
- // elm.setAttribute('src', assets[prop].src);
- // assetsElement.appendChild(elm);
- // }
- // }).catch(function (error) {
- // console.log(error);
- // });
- }
- }
- function updateStoredTransformFor(node, propertyName) {
- if (node && node.aframeObj) {
- // Add a local model-side transform that can stay pure even if the view changes the
- // transform on the threeObject - this already happened in creatingNode for those nodes that
- // didn't need to load a model
- if (!node.transform)
- node.transform = {};
- if (propertyName == 'position') {
- let pos = (new THREE.Vector3()).copy(node.aframeObj.object3D.position);
- node.transform.position = goog.vec.Vec3.createFromValues(pos.x, pos.y, pos.z);
- node.transform.storedPositionDirty = false;
- }
- if (propertyName == 'rotation') {
- // let rot = (new THREE.Vector3()).copy(node.aframeObj.object3D.rotation);
- let rot = node.aframeObj.getAttribute('rotation');
- node.transform.rotation = goog.vec.Vec3.createFromValues(rot.x, rot.y, rot.z);
- node.transform.storedRotationDirty = false;
- }
- if (propertyName == 'scale') {
- let scale = (new THREE.Vector3()).copy(node.aframeObj.object3D.scale);
- node.transform.scale = goog.vec.Vec3.createFromValues(scale.x, scale.y, scale.z);
- node.transform.storedScaleDirty = false;
- }
- //node.transform.position = AFRAME.utils.coordinates.stringify(node.aframeObj.object3D.position);
- // node.transform.rotation = AFRAME.utils.coordinates.stringify(node.aframeObj.object3D.rotation);
- // node.storedTransformDirty = false;
- }
- }
- function getPrototypes(kernel, extendsID) {
- var prototypes = [];
- var id = extendsID;
- while (id !== undefined) {
- prototypes.push(id);
- id = kernel.prototype(id);
- }
- return prototypes;
- }
- function isNodeDefinition(prototypes) {
- var found = false;
- if (prototypes) {
- for (var i = 0; i < prototypes.length && !found; i++) {
- found = (prototypes[i] == "proxy/aframe/node.vwf");
- }
- }
- return found;
- }
- function isAEntityDefinition(prototypes) {
- var found = false;
- if (prototypes) {
- for (var i = 0; i < prototypes.length && !found; i++) {
- found = (prototypes[i] == "proxy/aframe/aentity.vwf");
- }
- }
- return found;
- }
- // Changing this function significantly from the GLGE code
- // Will search hierarchy down until encountering a matching child
- // Will look into nodes that don't match.... this might not be desirable
- function FindChildByName(obj, childName, childType, recursive) {
- var child = undefined;
- if (recursive) {
- // TODO: If the obj itself has the child name, the object will be returned by this function
- // I don't think this this desirable.
- if (nameTest.call(this, obj, childName)) {
- child = obj;
- } else if (obj.children && obj.children.length > 0) {
- for (var i = 0; i < obj.children.length && child === undefined; i++) {
- child = FindChildByName(obj.children[i], childName, childType, true);
- }
- }
- } else {
- if (obj.children) {
- for (var i = 0; i < obj.children.length && child === undefined; i++) {
- if (nameTest.call(this, obj.children[i], childName)) {
- child = obj.children[i];
- }
- }
- }
- }
- return child;
- }
- function nameTest(obj, name) {
- if (obj.name == "") {
- return (obj.parent.name + "Child" == name);
- } else {
- return (obj.name == name || obj.id == name || obj.vwfID == name);
- }
- }
- function httpGet(url) {
- return new Promise(function (resolve, reject) {
- // do the usual Http request
- let request = new XMLHttpRequest();
- request.open('GET', url);
- request.onload = function () {
- if (request.status == 200) {
- resolve(request.response);
- } else {
- reject(Error(request.statusText));
- }
- };
- request.onerror = function () {
- reject(Error('Network Error'));
- };
- request.send();
- });
- }
- async function httpGetJson(url) {
- // check if the URL looks like a JSON file and call httpGet.
- let regex = /\.(json)$/i;
- if (regex.test(url)) {
- // call the async function, wait for the result
- return await httpGet(url);
- } else {
- throw Error('Bad Url Format');
- }
- }
- function GUID() {
- var S4 = function () {
- return Math.floor(
- Math.random() * 0x10000 /* 65536 */
- ).toString(16);
- };
- return (
- S4() + S4() + "-" +
- S4() + "-" +
- S4() + "-" +
- S4() + "-" +
- S4() + S4() + S4()
- );
- }
- });
|