mirror of
https://github.com/hanzoai/ProjectTinySamples.git
synced 2026-09-24 00:01:34 +00:00
Simplify smoke spawning and prefab references (#4)
This commit is contained in:
@@ -293,6 +293,8 @@ MonoBehaviour:
|
||||
y: 0
|
||||
z: -1.1
|
||||
SpawnTimer: 0
|
||||
SmokePrefab: {fileID: 5312453364359420, guid: 9e1ada267c91de44f980009b1bf1ef69,
|
||||
type: 3}
|
||||
--- !u!114 &8120410053859619024
|
||||
MonoBehaviour:
|
||||
m_ObjectHideFlags: 0
|
||||
@@ -309,6 +311,7 @@ MonoBehaviour:
|
||||
MaxSpeed: 600
|
||||
RotationSpeed: 90
|
||||
IsEngineDestroyed: 0
|
||||
PlayCrashAudio: 0
|
||||
--- !u!114 &8120410053859619027
|
||||
MonoBehaviour:
|
||||
m_ObjectHideFlags: 0
|
||||
@@ -385,6 +388,11 @@ PrefabInstance:
|
||||
m_Modification:
|
||||
m_TransformParent: {fileID: 8120410055477623919}
|
||||
m_Modifications:
|
||||
- target: {fileID: -4583913637259000950, guid: 57880d34d3cbd604cafaf0971dfc3e94,
|
||||
type: 3}
|
||||
propertyPath: m_Materials.Array.data[0]
|
||||
value:
|
||||
objectReference: {fileID: 2100000, guid: ebfc03a52e432b84fb2bd38ffa98f082, type: 2}
|
||||
- target: {fileID: -4216859302048453862, guid: 57880d34d3cbd604cafaf0971dfc3e94,
|
||||
type: 3}
|
||||
propertyPath: m_LocalPosition.x
|
||||
@@ -445,21 +453,16 @@ PrefabInstance:
|
||||
propertyPath: m_Name
|
||||
value: Kart
|
||||
objectReference: {fileID: 0}
|
||||
- target: {fileID: 574165032808307723, guid: 57880d34d3cbd604cafaf0971dfc3e94,
|
||||
type: 3}
|
||||
propertyPath: m_Materials.Array.data[0]
|
||||
value:
|
||||
objectReference: {fileID: 2100000, guid: ebfc03a52e432b84fb2bd38ffa98f082, type: 2}
|
||||
- target: {fileID: -4583913637259000950, guid: 57880d34d3cbd604cafaf0971dfc3e94,
|
||||
type: 3}
|
||||
propertyPath: m_Materials.Array.data[0]
|
||||
value:
|
||||
objectReference: {fileID: 2100000, guid: ebfc03a52e432b84fb2bd38ffa98f082, type: 2}
|
||||
- target: {fileID: -167423591363347547, guid: 57880d34d3cbd604cafaf0971dfc3e94,
|
||||
type: 3}
|
||||
propertyPath: m_Materials.Array.data[0]
|
||||
value:
|
||||
objectReference: {fileID: 2100000, guid: ebfc03a52e432b84fb2bd38ffa98f082, type: 2}
|
||||
- target: {fileID: 574165032808307723, guid: 57880d34d3cbd604cafaf0971dfc3e94,
|
||||
type: 3}
|
||||
propertyPath: m_Materials.Array.data[0]
|
||||
value:
|
||||
objectReference: {fileID: 2100000, guid: ebfc03a52e432b84fb2bd38ffa98f082, type: 2}
|
||||
- target: {fileID: 1831472566175286466, guid: 57880d34d3cbd604cafaf0971dfc3e94,
|
||||
type: 3}
|
||||
propertyPath: m_Materials.Array.data[0]
|
||||
|
||||
@@ -214,6 +214,8 @@ MonoBehaviour:
|
||||
y: 0
|
||||
z: -1.1
|
||||
SpawnTimer: 0
|
||||
SmokePrefab: {fileID: 5312453364359420, guid: 9e1ada267c91de44f980009b1bf1ef69,
|
||||
type: 3}
|
||||
--- !u!114 &7076360675072105140
|
||||
MonoBehaviour:
|
||||
m_ObjectHideFlags: 0
|
||||
|
||||
@@ -11,6 +11,7 @@ GameObject:
|
||||
- component: {fileID: 3911805622781072111}
|
||||
- component: {fileID: 5409527673522415833}
|
||||
- component: {fileID: 1742340728806773792}
|
||||
- component: {fileID: 3413346865391405944}
|
||||
m_Layer: 0
|
||||
m_Name: Smoke
|
||||
m_TagString: Untagged
|
||||
@@ -79,3 +80,19 @@ MeshRenderer:
|
||||
m_SortingLayerID: 0
|
||||
m_SortingLayer: 0
|
||||
m_SortingOrder: 0
|
||||
--- !u!114 &3413346865391405944
|
||||
MonoBehaviour:
|
||||
m_ObjectHideFlags: 0
|
||||
m_CorrespondingSourceObject: {fileID: 0}
|
||||
m_PrefabInstance: {fileID: 0}
|
||||
m_PrefabAsset: {fileID: 0}
|
||||
m_GameObject: {fileID: 5312453364359420}
|
||||
m_Enabled: 1
|
||||
m_EditorHideFlags: 0
|
||||
m_Script: {fileID: 11500000, guid: 6243cb2825db0b440b64d8056a0b1887, type: 3}
|
||||
m_Name:
|
||||
m_EditorClassIdentifier:
|
||||
Timer: 0
|
||||
Duration: 0
|
||||
BaseScale: 1
|
||||
Speed: 2
|
||||
|
||||
@@ -11,6 +11,7 @@ GameObject:
|
||||
- component: {fileID: 819923213100895471}
|
||||
- component: {fileID: 8519672974594733785}
|
||||
- component: {fileID: 2668237665861437984}
|
||||
- component: {fileID: 6220947221143206356}
|
||||
m_Layer: 0
|
||||
m_Name: SmokeDestroyed
|
||||
m_TagString: Untagged
|
||||
@@ -79,3 +80,19 @@ MeshRenderer:
|
||||
m_SortingLayerID: 0
|
||||
m_SortingLayer: 0
|
||||
m_SortingOrder: 0
|
||||
--- !u!114 &6220947221143206356
|
||||
MonoBehaviour:
|
||||
m_ObjectHideFlags: 0
|
||||
m_CorrespondingSourceObject: {fileID: 0}
|
||||
m_PrefabInstance: {fileID: 0}
|
||||
m_PrefabAsset: {fileID: 0}
|
||||
m_GameObject: {fileID: 4412307218195496700}
|
||||
m_Enabled: 1
|
||||
m_EditorHideFlags: 0
|
||||
m_Script: {fileID: 11500000, guid: 6243cb2825db0b440b64d8056a0b1887, type: 3}
|
||||
m_Name:
|
||||
m_EditorClassIdentifier:
|
||||
Timer: 0
|
||||
Duration: 0
|
||||
BaseScale: 0
|
||||
Speed: 2
|
||||
|
||||
@@ -99,7 +99,7 @@ LightmapSettings:
|
||||
m_LightProbeSampleCountMultiplier: 4
|
||||
m_LightingDataAsset: {fileID: 112000004, guid: c907e8caaa7a4164c8916ca1eef4eb49,
|
||||
type: 2}
|
||||
m_UseShadowmask: 1
|
||||
m_UseShadowmask: 0
|
||||
--- !u!196 &4
|
||||
NavMeshSettings:
|
||||
serializedVersion: 2
|
||||
@@ -6255,11 +6255,6 @@ MonoBehaviour:
|
||||
- {fileID: 2100000, guid: d0d855e6ab6a08a4dae118f529e3fd51, type: 2}
|
||||
- {fileID: 2100000, guid: c4e1afc14b9c6a640b6de2362617abfd, type: 2}
|
||||
- {fileID: 2100000, guid: 297a972aa05aa8d46885c7c79e8f4b6d, type: 2}
|
||||
CarSmokeMesh: {fileID: 289301614986012343, guid: 587988ab696006b4bbbbef7ff82d68d8,
|
||||
type: 3}
|
||||
CarSmokeMaterial: {fileID: 2100000, guid: f6950a7daf50901489f4d2e6827964d4, type: 2}
|
||||
CarDestroyedSmokeMaterial: {fileID: 2100000, guid: b7f612b4d0ae1a3418f7773d15e744d7,
|
||||
type: 2}
|
||||
--- !u!4 &755579060
|
||||
Transform:
|
||||
m_ObjectHideFlags: 0
|
||||
@@ -9974,12 +9969,12 @@ MonoBehaviour:
|
||||
m_GameObject: {fileID: 1116771990}
|
||||
m_Enabled: 1
|
||||
m_EditorHideFlags: 0
|
||||
m_Script: {fileID: 11500000, guid: f102f7f58f224264e8dd364de8132eea, type: 3}
|
||||
m_Script: {fileID: 11500000, guid: fedd7fc26f6a6464dbad38d9e1bd2942, type: 3}
|
||||
m_Name:
|
||||
m_EditorClassIdentifier:
|
||||
CarSmokePrefabMeshRenderer: {fileID: 5312453364359420, guid: 9e1ada267c91de44f980009b1bf1ef69,
|
||||
carSmokePrefab: {fileID: 5312453364359420, guid: 9e1ada267c91de44f980009b1bf1ef69,
|
||||
type: 3}
|
||||
CarSmokeDestroyedPrefabMeshRenderer: {fileID: 4412307218195496700, guid: de33c846abbce2844ad96c35431e3705,
|
||||
carSmokeDestroyedPrefab: {fileID: 4412307218195496700, guid: de33c846abbce2844ad96c35431e3705,
|
||||
type: 3}
|
||||
--- !u!4 &1116771992
|
||||
Transform:
|
||||
|
||||
@@ -1,27 +0,0 @@
|
||||
using TinyRacing.Systems.Hybrid;
|
||||
using Unity.Entities;
|
||||
using UnityEngine;
|
||||
using System.Collections.Generic;
|
||||
using TinyRacing;
|
||||
|
||||
[DisallowMultipleComponent]
|
||||
public class PrefabReferencesAuthoring : MonoBehaviour, IConvertGameObjectToEntity, IDeclareReferencedPrefabs
|
||||
{
|
||||
public GameObject CarSmokePrefabMeshRenderer;
|
||||
public GameObject CarSmokeDestroyedPrefabMeshRenderer;
|
||||
|
||||
public void Convert(Entity entity, EntityManager dstManager, GameObjectConversionSystem conversionSystem)
|
||||
{
|
||||
dstManager.AddComponentData(entity, new PrefabReferences()
|
||||
{
|
||||
carSmokePrefab = conversionSystem.GetPrimaryEntity(CarSmokePrefabMeshRenderer),
|
||||
carSmokeDestroyedPrefab = conversionSystem.GetPrimaryEntity(CarSmokeDestroyedPrefabMeshRenderer)
|
||||
});
|
||||
}
|
||||
|
||||
public void DeclareReferencedPrefabs(List<GameObject> gameObjects)
|
||||
{
|
||||
gameObjects.Add(CarSmokePrefabMeshRenderer);
|
||||
gameObjects.Add(CarSmokeDestroyedPrefabMeshRenderer);
|
||||
}
|
||||
}
|
||||
@@ -1,11 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: f102f7f58f224264e8dd364de8132eea
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
+1
-7
@@ -7,19 +7,13 @@ public class MaterialReferencesAuthoring : MonoBehaviour, IConvertGameObjectToEn
|
||||
{
|
||||
public Material Empty;
|
||||
public Material[] Numbers;
|
||||
public Mesh CarSmokeMesh;
|
||||
public Material CarSmokeMaterial;
|
||||
public Material CarDestroyedSmokeMaterial;
|
||||
|
||||
public void Convert(Entity entity, EntityManager dstManager, GameObjectConversionSystem conversionSystem)
|
||||
{
|
||||
dstManager.AddSharedComponentData(entity, new MaterialReferences()
|
||||
{
|
||||
Empty = Empty,
|
||||
Numbers = Numbers,
|
||||
CarSmokeMesh = CarSmokeMesh,
|
||||
CarSmokeMaterial = CarSmokeMaterial,
|
||||
CarDestroyedSmokeMaterial = CarDestroyedSmokeMaterial
|
||||
Numbers = Numbers
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -46,9 +46,6 @@ namespace TinyRacing.Systems.Hybrid
|
||||
{
|
||||
public Material Empty;
|
||||
public Material[] Numbers;
|
||||
public Mesh CarSmokeMesh;
|
||||
public Material CarSmokeMaterial;
|
||||
public Material CarDestroyedSmokeMaterial;
|
||||
|
||||
public bool Equals(MaterialReferences other)
|
||||
{
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace TinyRacing
|
||||
{
|
||||
[GenerateAuthoringComponent]
|
||||
public struct PrefabReferences : IComponentData
|
||||
{
|
||||
public Entity carSmokePrefab;
|
||||
|
||||
@@ -9,5 +9,6 @@ namespace TinyRacing
|
||||
public float SpawnInterval;
|
||||
public float3 SpawnOffset;
|
||||
public float SpawnTimer;
|
||||
public Entity SmokePrefab;
|
||||
}
|
||||
}
|
||||
@@ -11,24 +11,34 @@ namespace TinyRacing.Systems
|
||||
/// </summary>
|
||||
public class BumpCar : JobComponentSystem
|
||||
{
|
||||
private Entity m_CarDestroyedSmoke;
|
||||
|
||||
protected override void OnCreate()
|
||||
{
|
||||
base.OnCreate();
|
||||
RequireSingletonForUpdate<PlayerTag>();
|
||||
}
|
||||
|
||||
protected override void OnStartRunning()
|
||||
{
|
||||
base.OnStartRunning();
|
||||
m_CarDestroyedSmoke = GetSingleton<PrefabReferences>().carSmokeDestroyedPrefab;
|
||||
}
|
||||
|
||||
protected override JobHandle OnUpdate(JobHandle inputDeps)
|
||||
{
|
||||
var playerCarEntity = GetSingletonEntity<PlayerTag>();
|
||||
var playerCarPosition = EntityManager.GetComponentData<Translation>(playerCarEntity).Value;
|
||||
var destroyedSmoke = m_CarDestroyedSmoke;
|
||||
|
||||
return Entities.WithNone<PlayerTag>().ForEach((ref Car car, in Translation translation) =>
|
||||
return Entities.WithNone<PlayerTag>().ForEach((ref Car car, ref SmokeSpawner smoke, in Translation translation) =>
|
||||
{
|
||||
var distanceSq = math.distancesq(translation.Value, playerCarPosition);
|
||||
if (distanceSq < 1f && !car.IsEngineDestroyed)
|
||||
{
|
||||
car.IsEngineDestroyed = true;
|
||||
car.PlayCrashAudio = true;
|
||||
smoke.SmokePrefab = destroyedSmoke;
|
||||
}
|
||||
}).Schedule(inputDeps);
|
||||
}
|
||||
|
||||
@@ -11,22 +11,12 @@ namespace TinyRacing.Systems
|
||||
public class SpawnSmoke : ComponentSystem
|
||||
{
|
||||
private Random Random;
|
||||
private PrefabReferences PrefabReferences;
|
||||
private Entity PrefabReferencesEntity;
|
||||
|
||||
protected override void OnCreate()
|
||||
{
|
||||
base.OnCreate();
|
||||
Random = new Random();
|
||||
Random.InitState();
|
||||
RequireSingletonForUpdate<PrefabReferences>();
|
||||
}
|
||||
|
||||
protected override void OnStartRunning()
|
||||
{
|
||||
base.OnStartRunning();
|
||||
PrefabReferencesEntity = GetSingletonEntity<PrefabReferences>();
|
||||
PrefabReferences = EntityManager.GetComponentData<PrefabReferences>(PrefabReferencesEntity);
|
||||
}
|
||||
|
||||
protected override void OnUpdate()
|
||||
@@ -35,45 +25,34 @@ namespace TinyRacing.Systems
|
||||
Entities.ForEach((ref SmokeSpawner smokeSpawner, ref Car car, ref Translation translation,
|
||||
ref LocalToWorld localToWorld) =>
|
||||
{
|
||||
if (smokeSpawner.SmokePrefab == Entity.Null)
|
||||
return;
|
||||
|
||||
smokeSpawner.SpawnTimer += deltaTime;
|
||||
if (smokeSpawner.SpawnTimer > smokeSpawner.SpawnInterval)
|
||||
if (smokeSpawner.SpawnTimer < smokeSpawner.SpawnInterval)
|
||||
return;
|
||||
|
||||
smokeSpawner.SpawnTimer = 0f;
|
||||
var scale = car.IsEngineDestroyed ? Random.NextFloat(2f, 3.5f) : Random.NextFloat(0.5f, 1.2f);
|
||||
var duration = car.IsEngineDestroyed ? 2f : 0.45f;
|
||||
var startAngle = Random.NextFloat(-180f, 180f);
|
||||
var smokeEntity = EntityManager.Instantiate(smokeSpawner.SmokePrefab);
|
||||
|
||||
EntityManager.SetComponentData(smokeEntity,
|
||||
new Smoke {Duration = duration, BaseScale = scale, Speed = 2f});
|
||||
EntityManager.SetComponentData(smokeEntity, new Translation
|
||||
{
|
||||
smokeSpawner.SpawnTimer = 0f;
|
||||
var scale = car.IsEngineDestroyed ? Random.NextFloat(2f, 3.5f) : Random.NextFloat(0.5f, 1.2f);
|
||||
var duration = car.IsEngineDestroyed ? 2f : 0.45f;
|
||||
var startAngle = Random.NextFloat(-180f, 180f);
|
||||
Entity smokeEntity;
|
||||
|
||||
if (car.IsEngineDestroyed)
|
||||
smokeEntity = EntityManager.Instantiate(PrefabReferences.carSmokeDestroyedPrefab);
|
||||
else
|
||||
smokeEntity = EntityManager.Instantiate(PrefabReferences.carSmokePrefab);
|
||||
|
||||
PostUpdateCommands.AddComponent(smokeEntity,
|
||||
new Smoke {Duration = duration, BaseScale = scale, Speed = 2f});
|
||||
PostUpdateCommands.AddComponent(smokeEntity, new Translation
|
||||
{
|
||||
Value =
|
||||
translation.Value +
|
||||
localToWorld.Right * smokeSpawner.SpawnOffset.x +
|
||||
localToWorld.Up * smokeSpawner.SpawnOffset.y +
|
||||
localToWorld.Forward * smokeSpawner.SpawnOffset.z
|
||||
});
|
||||
PostUpdateCommands.AddComponent(smokeEntity, new LocalToWorld());
|
||||
PostUpdateCommands.AddComponent(smokeEntity,
|
||||
new Rotation
|
||||
{Value = math.mul(quaternion.identity, quaternion.RotateZ(math.radians(startAngle)))});
|
||||
PostUpdateCommands.AddComponent(smokeEntity, new Scale {Value = scale});
|
||||
#if UNITY_DOTSPLAYER
|
||||
MeshRenderer meshRenderer = EntityManager.GetComponentData<MeshRenderer>(smokeEntity);
|
||||
LitMaterial litMaterial = EntityManager.GetComponentData<LitMaterial>(meshRenderer.material);
|
||||
|
||||
PostUpdateCommands.AddComponent(smokeEntity, litMaterial);
|
||||
meshRenderer.material = smokeEntity;
|
||||
PostUpdateCommands.AddComponent(smokeEntity, meshRenderer);
|
||||
PostUpdateCommands.AddComponent(smokeEntity, new DynamicMaterial());
|
||||
#endif
|
||||
}
|
||||
Value =
|
||||
translation.Value +
|
||||
localToWorld.Right * smokeSpawner.SpawnOffset.x +
|
||||
localToWorld.Up * smokeSpawner.SpawnOffset.y +
|
||||
localToWorld.Forward * smokeSpawner.SpawnOffset.z
|
||||
});
|
||||
EntityManager.SetComponentData(smokeEntity,
|
||||
new Rotation
|
||||
{Value = math.mul(quaternion.identity, quaternion.RotateZ(math.radians(startAngle)))});
|
||||
// scale isn't present by default during transform conversion
|
||||
EntityManager.AddComponentData(smokeEntity, new Scale {Value = scale});
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,25 +18,18 @@ namespace TinyRacing.Systems
|
||||
var deltaTime = Time.DeltaTime;
|
||||
var ecb = ecbSystem.CreateCommandBuffer().ToConcurrent();
|
||||
|
||||
var jobHandle = Entities.ForEach((Entity entity, int entityInQueryIndex, ref Smoke smoke,
|
||||
ref Translation translation, ref Scale scale,
|
||||
ref MeshRenderer renderer
|
||||
#if UNITY_DOTSPLAYER
|
||||
, ref LitMaterial litMaterial
|
||||
#endif
|
||||
) =>
|
||||
var jobHandle = Entities.ForEach((Entity entity, int nativeThreadIndex,
|
||||
ref Smoke smoke, ref Translation translation, ref Scale scale) =>
|
||||
{
|
||||
smoke.Timer += deltaTime;
|
||||
if (smoke.Timer > smoke.Duration)
|
||||
{
|
||||
ecb.DestroyEntity(nativeThreadIndex, entity);
|
||||
return;
|
||||
}
|
||||
|
||||
translation.Value += new float3(0f, smoke.Speed, 0f) * deltaTime;
|
||||
scale.Value = smoke.BaseScale * smoke.Timer / smoke.Duration;
|
||||
#if UNITY_DOTSPLAYER
|
||||
litMaterial.transparent = true;
|
||||
var delta = smoke.Timer / smoke.Duration;
|
||||
// Inverse quadratic ease-in for smoke transparency
|
||||
litMaterial.constOpacity = 1 - delta * delta;
|
||||
#endif
|
||||
if (smoke.Timer > smoke.Duration)
|
||||
ecb.DestroyEntity(entityInQueryIndex, entity);
|
||||
}).Schedule(inputDeps);
|
||||
|
||||
ecbSystem.AddJobHandleForProducer(jobHandle);
|
||||
|
||||
Reference in New Issue
Block a user