Needle Engine

Changes between version 3.2.6-alpha and 3.2.7-alpha
Files changed (4) hide show
  1. src/engine/codegen/register_types.js +2 -2
  2. src/engine-components/Component.ts +13 -0
  3. src/engine/engine_physics.ts +20 -1
  4. src/engine/extensions/NEEDLE_lighting_settings.ts +15 -3
src/engine/codegen/register_types.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import { TypeStore } from "./../engine_typestore"
2
-
2
+
3
3
  // Import types
4
4
  import { __Ignore } from "../../engine-components/codegen/components";
5
5
  import { AlignmentConstraint } from "../../engine-components/AlignmentConstraint";
@@ -187,7 +187,7 @@
187
187
  import { XRGrabRendering } from "../../engine-components/webxr/WebXRGrabRendering";
188
188
  import { XRRig } from "../../engine-components/webxr/WebXRRig";
189
189
  import { XRState } from "../../engine-components/XRFlag";
190
-
190
+
191
191
  // Register types
192
192
  TypeStore.add("__Ignore", __Ignore);
193
193
  TypeStore.add("AlignmentConstraint", AlignmentConstraint);
src/engine-components/Component.ts CHANGED
@@ -9,6 +9,7 @@
9
9
  import { findByGuid, destroy, InstantiateOptions, instantiate, HideFlags, foreachComponent, markAsInstancedRendered, isActiveInHierarchy, isActiveSelf, isUsingInstancing, setActive, isDestroyed } from "../engine/engine_gameobject";
10
10
 
11
11
  import { Euler, Object3D, Quaternion, Scene, Vector3 } from "three";
12
+ import { showBalloonWarning, isDevEnvironment } from "../engine/debug";
12
13
 
13
14
  // export interface ISerializationCallbackReceiver {
14
15
  // onBeforeSerialize?(): object | void;
@@ -470,6 +471,12 @@
470
471
 
471
472
  /** @internal */
472
473
  __internalEnable(): boolean {
474
+ if (this.__destroyed) {
475
+ if (isDevEnvironment()) {
476
+ console.warn("[Needle Engine Dev] Trying to enable destroyed component");
477
+ }
478
+ return false;
479
+ }
473
480
  // Don't change enable before awake
474
481
  // But a user can change enable during awake
475
482
  if (!this.__didAwake) return false;
@@ -511,6 +518,12 @@
511
518
  return typeof this.__isEnabled === "boolean" ? this.__isEnabled : true; // if it has no enabled field it is always enabled
512
519
  }
513
520
  set enabled(val: boolean) {
521
+ if (this.__destroyed) {
522
+ if (isDevEnvironment()) {
523
+ console.warn(`[Needle Engine Dev] Trying to ${val ? "enable" : "disable"} destroyed component`);
524
+ }
525
+ return;
526
+ }
514
527
  // when called from animationclip we receive numbers
515
528
  // due to interpolation they can be anything between 0 and 1
516
529
  if (typeof val === "number") {
src/engine/engine_physics.ts CHANGED
@@ -1082,7 +1082,7 @@
1082
1082
  // if one is a trigger we dont get collisions but want to raise the trigger events
1083
1083
  if (self.isTrigger || other.isTrigger) {
1084
1084
  foreachComponent(self.gameObject, (c: IComponent) => {
1085
- if (c.onTriggerEnter) {
1085
+ if (c.onTriggerEnter && !c.destroyed) {
1086
1086
  c.onTriggerEnter(other);
1087
1087
  }
1088
1088
  this.activeTriggers.push({ collider: self, component: c, otherCollider: other });
@@ -1093,6 +1093,7 @@
1093
1093
  // TODO: we dont respect the flip value here!
1094
1094
  this.world.contactPair(selfBody, otherBody, (manifold, _flipped) => {
1095
1095
  foreachComponent(object, (c: IComponent) => {
1096
+ if(c.destroyed) return;
1096
1097
  const hasDeclaredEventMethod = c.onCollisionEnter || c.onCollisionStay || c.onCollisionExit;
1097
1098
  if (hasDeclaredEventMethod || debugCollisions) {
1098
1099
  if (!collision) {
@@ -1137,6 +1138,7 @@
1137
1138
  private onHandleCollisionStay() {
1138
1139
  for (const active of this.activeCollisionsStay) {
1139
1140
  const c = active.component;
1141
+ if(c.destroyed) continue;
1140
1142
  if (c.activeAndEnabled && c.onCollisionStay) {
1141
1143
  const arg = active.collision;
1142
1144
  c.onCollisionStay(arg);
@@ -1144,6 +1146,7 @@
1144
1146
  }
1145
1147
  for (const active of this.activeTriggers) {
1146
1148
  const c = active.component;
1149
+ if(c.destroyed) continue;
1147
1150
  if (c.activeAndEnabled && c.onTriggerStay) {
1148
1151
  const arg = active.otherCollider;
1149
1152
  c.onTriggerStay(arg);
@@ -1152,9 +1155,15 @@
1152
1155
  }
1153
1156
 
1154
1157
  private onCollisionEnded(self: ICollider, other: ICollider) {
1158
+ if(self.destroyed || other.destroyed) return;
1155
1159
  for (let i = 0; i < this.activeCollisions.length; i++) {
1156
1160
  const active = this.activeCollisions[i];
1157
1161
  const collider = active.collider;
1162
+ if(collider.destroyed) {
1163
+ this.activeCollisions.splice(i, 1);
1164
+ i--;
1165
+ continue;
1166
+ }
1158
1167
  if (collider === self && active.collision.collider === other) {
1159
1168
  const c = active.component;
1160
1169
  this.activeCollisions.splice(i, 1);
@@ -1168,6 +1177,11 @@
1168
1177
  for (let i = 0; i < this.activeCollisionsStay.length; i++) {
1169
1178
  const active = this.activeCollisionsStay[i];
1170
1179
  const collider = active.collider;
1180
+ if(collider.destroyed) {
1181
+ this.activeCollisionsStay.splice(i, 1);
1182
+ i--;
1183
+ continue;
1184
+ }
1171
1185
  if (collider === self && active.collision.collider === other) {
1172
1186
  const c = active.component;
1173
1187
  this.activeCollisionsStay.splice(i, 1);
@@ -1181,6 +1195,11 @@
1181
1195
  for (let i = 0; i < this.activeTriggers.length; i++) {
1182
1196
  const active = this.activeTriggers[i];
1183
1197
  const collider = active.collider;
1198
+ if(collider.destroyed) {
1199
+ this.activeTriggers.splice(i, 1);
1200
+ i--;
1201
+ continue;
1202
+ }
1184
1203
  if (collider === self && active.otherCollider === other) {
1185
1204
  const c = active.component;
1186
1205
  this.activeTriggers.splice(i, 1);
src/engine/extensions/NEEDLE_lighting_settings.ts CHANGED
@@ -8,6 +8,7 @@
8
8
  import { Context } from "../engine_setup";
9
9
  import { LightProbe } from "three";
10
10
  import { ContextEvent, ContextRegistry } from "../engine_context_registry";
11
+ import { Mathf } from "../engine_math";
11
12
 
12
13
  export const EXTENSION_NAME = "NEEDLE_lighting_settings";
13
14
  const debug = getParam("debugenvlight");
@@ -122,13 +123,22 @@
122
123
  this.context.sceneLighting.internalUnregisterSceneLightSettings(this);
123
124
  }
124
125
 
126
+ private calculateIntensityFactor(col:Color){
127
+ const intensity = Math.max(col.r, col.g, col.b);// * 0.2126 + col.g * 0.7152 + col.b * 0.0722;
128
+ const factor = 2.2 * Mathf.lerp(0, 1.33, intensity); // scale based on intensity
129
+ return factor;
130
+ }
131
+
125
132
  onEnable(): void {
126
133
  if (debug) console.warn("💡🟡 >>> Enable lighting", this.sourceId, this);
127
134
 
128
135
  if (this.ambientMode == AmbientMode.Flat) {
129
136
  if (this.ambientLight && !this._ambientLightObj) {
130
- this._ambientLightObj = new AmbientLight(this.ambientLight, this.ambientIntensity);
131
- if (debug) console.log("Created ambient light", this.sourceId, this._ambientLightObj)
137
+ // TODO: currently ambient intensity is always exported as 1? The exported values are not correct in threejs
138
+ // the following calculation is a workaround to get the correct intensity
139
+ const factor = this.calculateIntensityFactor(this.ambientLight);
140
+ this._ambientLightObj = new AmbientLight(this.ambientLight, this.ambientIntensity * factor);
141
+ if (debug) console.log("Created ambient light", this.sourceId, this._ambientLightObj, this.ambientIntensity, factor)
132
142
  }
133
143
  if (this._ambientLightObj) {
134
144
  this.gameObject.add(this._ambientLightObj)
@@ -139,8 +149,10 @@
139
149
  if (this.ambientTrilight) {
140
150
  const ground = this.ambientTrilight[0];
141
151
  const sky = this.ambientTrilight[this.ambientTrilight.length - 1];
142
- this._hemisphereLightObj = new HemisphereLight(sky, ground, this.ambientIntensity);
152
+ const factor = this.calculateIntensityFactor(sky);
153
+ this._hemisphereLightObj = new HemisphereLight(sky, ground, this.ambientIntensity * factor);
143
154
  this.gameObject.add(this._hemisphereLightObj)
155
+ if (debug) console.log("Created hemisphere ambient light", this.sourceId, this._hemisphereLightObj, this.ambientIntensity, factor)
144
156
  }
145
157
  }
146
158
  else {