Skip to content

Commit

Permalink
add automation-element component
Browse files Browse the repository at this point in the history
remove static chars
  • Loading branch information
Algorush committed Feb 6, 2024
1 parent 9e67252 commit b75d6f5
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 42 deletions.
4 changes: 2 additions & 2 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@
<!-- uncomment the line below to load all possible asset categories -->
<!-- <street-assets categories="sidewalk-props people people-rigged vehicles vehicles-rigged buildings intersection-props segment-textures segment-colors lane-separator stencils vehicles-transit dividers sky grounds"></street-assets> -->
<!-- a reduced set of assets for non-animated streetmix streets without intersections -->
<street-assets categories="loud-bicycle sidewalk-props people vehicles vehicles-rigged buildings segment-textures segment-colors lane-separator stencils vehicles-transit dividers sky grounds"></street-assets>
<street-assets categories="loud-bicycle sidewalk-props people-rigged vehicles vehicles-rigged buildings segment-textures segment-colors lane-separator stencils vehicles-transit dividers sky grounds"></street-assets>
</a-assets>

<a-entity id="street-container" data-layer-name="3D Street Layers" data-layer-show-children>
Expand Down Expand Up @@ -137,7 +137,7 @@
sceneEl.components.inspector.openInspector();
document.querySelector('.viewer-header-wrapper').style.display = 'none';
}

AFRAME.registerComponent('timed-inspector', {
init: function() {
setTimeout( function () {
Expand Down
58 changes: 18 additions & 40 deletions src/aframe-streetmix-parsers.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ function getZPositions (start, end, step) {
function createSidewalkClonedVariants (segmentWidthInMeters, density, elevationPosY = 0, streetLength, direction = 'random', animated = false) {
const xValueRange = [-(0.37 * segmentWidthInMeters), (0.37 * segmentWidthInMeters)];
const zValueRange = getZPositions((-0.5 * streetLength), (0.5 * streetLength), 1.5);

const densityFactors = {
empty: 0,
sparse: 0.03,
Expand All @@ -207,14 +208,15 @@ function createSidewalkClonedVariants (segmentWidthInMeters, density, elevationP
dividerParentEl.setAttribute('position', {y: elevationPosY});
// Randomly generate avatars
for (let i = 0; i < totalPedestrianNumber; i++) {
const variantName = (animated === true) ? 'a_char' + String(getRandomIntInclusive(1, 8)) : 'char' + String(getRandomIntInclusive(1, 16));
const variantName = 'a_char' + String(getRandomIntInclusive(1, 8));
const xVal = getRandomArbitrary(xValueRange[0], xValueRange[1]);
const zVal = zValueRange.pop();
const yVal = 0;
// y = 0.2 for sidewalk elevation
const placedObjectEl = document.createElement('a-entity');
let animationDirection = 'inbound';
placedObjectEl.setAttribute('mixin', variantName);
placedObjectEl.setAttribute('position', { x: xVal, y: yVal, z: zVal });
// Roughly 50% of traffic will be incoming
if (Math.random() < 0.5 && direction === 'random') {
placedObjectEl.setAttribute('rotation', '0 180 0');
Expand All @@ -223,8 +225,14 @@ function createSidewalkClonedVariants (segmentWidthInMeters, density, elevationP
placedObjectEl.setAttribute('rotation', '0 0 0');
}

if (animated) {
addLinearStreetAnimation(placedObjectEl, 1.4, streetLength, xVal, yVal, zVal, animationDirection);
if (animated) {
placedObjectEl.setAttribute('automation-element', {
zPos: zVal,
direction: animationDirection,
speed: 1.4,
streetLength: streetLength,
mixer: true
});
}
dividerParentEl.append(placedObjectEl);
}
Expand Down Expand Up @@ -314,37 +322,6 @@ function createBusElement (variantList, length, showVehicles) {
return busParentEl;
}

function addLinearStreetAnimation (reusableObjectEl, speed, streetLength, xPos, yVal = 0, zPos, direction) {
const totalStreetDuration = (streetLength / speed) * 1000; // time in milliseconds
const halfStreet = (direction === 'outbound')
? -streetLength / 2
: streetLength / 2;
const startingDistanceToTravel = Math.abs(halfStreet - zPos);
const startingDuration = (startingDistanceToTravel / speed) * 1000;

const animationAttrs_1 = {
property: 'position',
easing: 'linear',
loop: 'false',
from: { x: xPos, y: yVal, z: zPos },
to: { z: halfStreet },
dur: startingDuration
};
const animationAttrs_2 = {
property: 'position',
easing: 'linear',
loop: 'true',
from: { x: xPos, y: yVal, z: -halfStreet },
to: { x: xPos, y: yVal, z: halfStreet },
delay: startingDuration,
dur: totalStreetDuration
};
reusableObjectEl.setAttribute('animation__1', animationAttrs_1);
reusableObjectEl.setAttribute('animation__2', animationAttrs_2);

return reusableObjectEl;
}

function createDriveLaneElement (variantList, segmentWidthInMeters, streetLength, animated = false, showVehicles = true, count = 1, carStep = undefined) {
if (!showVehicles) {
return;
Expand Down Expand Up @@ -374,11 +351,7 @@ function createDriveLaneElement (variantList, segmentWidthInMeters, streetLength
} else {
rotationY = rotationVariants[lineVariant];
}
/*
if (carType === 'pedestrian') {
return createSidewalkClonedVariants(segmentWidthInMeters, 'normal', streetLength, lineVariant, animated);
}
*/

const driveLaneParentEl = document.createElement('a-entity');

if (variantList.length == 1) {
Expand Down Expand Up @@ -431,7 +404,12 @@ function createDriveLaneElement (variantList, segmentWidthInMeters, streetLength
reusableObjectEl.setAttribute('wheel',
{ speed: speed, wheelDiameter: params['wheelDiameter'] }
);
addLinearStreetAnimation(reusableObjectEl, speed, streetLength, 0, 0, positionZ, direction);
reusableObjectEl.setAttribute('automation-element', {
zPos: positionZ,
direction: direction,
speed: speed,
streetLength: streetLength
});
}
driveLaneParentEl.append(reusableObjectEl);
return reusableObjectEl;
Expand Down
79 changes: 79 additions & 0 deletions src/components/automation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/* global AFRAME */

/*
The animation-element component controls all animation of the elements
*/
AFRAME.registerComponent('automation-element', {
schema: {
// initial z position of element
zPos: { type: 'number', default: 0 },
direction: { type: 'string', default: 'outbound', oneOf: ['outbound', 'inbound'] },
enabled: { type: 'boolean', default: true },
speed: { type: 'number', default: 1000 },
streetLength: { type: 'number', default: 60 }
},
init: function () {
const el = this.el;
this.addLinearAnimation();
},
addLinearAnimation: function () {
const el = this.el;
const streetLength = this.data.streetLength;
const speed = this.data.speed;
const direction = this.data.direction;
const zPos = el.object3D.position.z;

const totalStreetDuration = (streetLength / speed) * 1000; // time in milliseconds

if (direction === 'outbound') {
halfStreet = -streetLength / 2;
el.setAttribute('rotation', {y: 180});
} else {
halfStreet = streetLength / 2;
el.setAttribute('rotation', {y: 0});
}
const startingDistanceToTravel = Math.abs(halfStreet - zPos);
const startingDuration = (startingDistanceToTravel / speed) * 1000;

// animation params to move an object from its current position to the end of street
// in a specified direction
const animationAttrs_1 = {
property: 'object3D.position.z',
easing: 'linear',
loop: 'false',
from: zPos,
to: halfStreet,
dur: startingDuration
};
// Animation parameters for the next animation cycle.
// They can be changed when changing position of the object in the editor
const animationAttrs_2 = {
property: 'object3D.position.z',
autoplay: false,
easing: 'linear',
loop: 'true',
from: -halfStreet,
to: halfStreet,
dur: totalStreetDuration,
startEvents: 'animationcomplete__1'
//startEvents: 'startAnim2'
};
el.setAttribute('animation__1', animationAttrs_1);
el.setAttribute('animation__2', animationAttrs_2);
},
animationCompleteEvent: function (evt) {
const elem = evt.target;
//this.el.parentEl.emit('addInBuffer', {uuid: elem.uuid});
},
update: function (oldData) {
// If `oldData` is empty, then this means we're in the initialization process.
// No need to update.
if (Object.keys(oldData).length === 0) { return; }

const changedData = AFRAME.utils.diff(this.data, oldData);

if (Object.keys(changedData).length > 0) {
this.addLinearAnimation();
}
}
});
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ require('./assets.js');
require('./components/notify.js');
require('./components/create-from-json');
require('./components/screentock.js');
require('./components/automation.js');
require('aframe-atlas-uvs-component');

AFRAME.registerComponent('street', {
Expand Down

0 comments on commit b75d6f5

Please sign in to comment.