public void SetAgentPositions(ComponentDataArray <Position> positions) { for (int i = 0; i < positions.Length; i++) { xSorted[i] = new AgentPosition() { index = i, position = positions[i].Value }; } ySorted.CopyFrom(xSorted); zSorted.CopyFrom(xSorted); xSorted.Sort(new XComparer()); ySorted.Sort(new YComparer()); zSorted.Sort(new ZComparer()); for (var i = 0; i < xSorted.Length; i++) { xMap.TryAdd(xSorted[i].index, i); } for (var i = 0; i < ySorted.Length; i++) { yMap.TryAdd(ySorted[i].index, i); } for (var i = 0; i < zSorted.Length; i++) { zMap.TryAdd(zSorted[i].index, i); } }
protected override void OnUpdate() { m_DataGroupIndex = m_mapDataGroup.GetComponentDataArray <MapIndexComponent>(); m_DataGroupData = m_mapDataGroup.GetComponentDataArray <MapDataComponent>(); m_terrainPos = m_terrainGroup.GetComponentDataArray <Components.Transform.Pos>(); m_terrainIndex = m_terrainGroup.GetComponentDataArray <MapIndexComponent>(); int2 index2D = new int2 { x = 0, y = 0 }; float3 pos3D = new float3 { x = 0, y = 0, z = 0 }; for (int i = 0; i < m_DataGroupIndex.Length; i++) { m_terrainIndex[i] = m_DataGroupIndex[i]; index2D = m_DataGroupIndex[i].Value; pos3D = new float3 { x = index2D.x, y = m_DataGroupData[i].Value * 10, z = index2D.y }; m_terrainPos[i] = new Components.Transform.Pos { Value = pos3D }; } }
protected override void OnUpdate() { charaMoves = group.GetComponentDataArray <CharaMove>(); charaDashs = group.GetComponentDataArray <CharaDash>(); charaMotions = group.GetComponentDataArray <CharaMotion>(); padInputs = group.GetComponentDataArray <PadInput>(); for (int i = 0; i < charaMotions.Length; i++) { //モーションごとの入力 switch (charaMotions[i].motionType) { case EnumMotion.Idle: Friction(i); break; case EnumMotion.Walk: Walk(i); break; case EnumMotion.Dash: break; case EnumMotion.Slip: Friction(i); break; case EnumMotion.Jump: break; case EnumMotion.Fall: break; case EnumMotion.Land: Stop(i); break; case EnumMotion.Damage: break; case EnumMotion.Fly: break; case EnumMotion.Down: Friction(i); break; case EnumMotion.Dead: Stop(i); break; case EnumMotion.Action: break; default: Debug.Assert(false); break; } } }
protected override void OnUpdate() { m_data = m_mapComponentGroup.GetComponentDataArray <MapDataComponent>(); m_loc = m_mapComponentGroup.GetComponentDataArray <MapIndexComponent>(); for (int w = 0; w < 128; w++) { for (int h = 0; h < 128; h++) { int index = w * 128 + h; texture.SetPixel(m_loc[index].Value.x, m_loc[index].Value.y, new Color { r = m_data[index].Value, g = m_data[index].Value, b = m_data[index].Value, a = 1.0f }); } } texture.Apply(); material.mainTexture = texture; }
public static List <T> ComponentDataArrayToList <T>(ComponentDataArray <T> cda) where T : struct, IComponentData { NativeArray <T> na = cda.GetChunkArray(0, cda.Length); List <T> list = new List <T>(na.ToArray()); return(list); }
private void RemovedEntities() { EntityArray entities = removedSyncEntities.entities; ComponentDataArray <NetworkSyncState> networkSyncs = removedSyncEntities.networkSyncStateComponents; for (int i = 0; i < entities.Length; i++) { NetworkSyncState component = new NetworkSyncState() { actorId = networkManager.LocalPlayerID, networkId = networkManager.GetNetworkId(), }; PostUpdateCommands.RemoveComponent <NetworkSyncState>(entities[i]); for (int j = 0; j < RemoveComponentOnDestroyEntityMethods.Count; j++) { RemoveComponentOnDestroyEntityMethods[j].Invoke(this, entities[i]); } NetworkSyncEntity networkSyncEntity = new NetworkSyncEntity { ActorId = component.actorId, NetworkId = component.networkId, }; ownNetworkSendMessageUtility.RemoveEntity(networkSyncEntity); AllNetworkSendMessageUtility.RemoveEntity(networkSyncEntity); } }
protected override JobHandle OnUpdate(JobHandle inputDeps) { // Debug.LogWarning ( "Col" ) ; Bounds checkBounds = new Bounds() { center = new float3(10, 2, 10), size = new float3(1, 1, 1) * 5 // Total size of boundry }; EntityArray a_entities = group.GetEntityArray(); Entity rootNodeEntity = a_entities [0]; ComponentDataArray <RootNodeData> a_rootNodeData = group.GetComponentDataArray <RootNodeData> ( ); RootNodeData rootNodeData = a_rootNodeData [0]; BufferFromEntity <NodeBufferElement> nodeBufferElement = GetBufferFromEntity <NodeBufferElement> (); DynamicBuffer <NodeBufferElement> a_nodesBuffer = nodeBufferElement [rootNodeEntity]; Bounds maxBouds = _GetOctreeMaxBounds(rootNodeData, a_nodesBuffer); return(base.OnUpdate(inputDeps)); }
/// <summary> /// Like `EntityArray.ToArray` but for CDA. /// </summary> public static List <T> CopyToList <T>(this ComponentDataArray <T> cda, IComparer <T> sorting) where T : struct, IComponentData { var list = CopyToList <T>(cda); list.Sort(sorting); return(list); }
private void AddedComponents <T>() where T : struct, IComponentData { ComponentType componentType = ComponentType.Create <T>(); ComponentGroup group = GetComponentGroup(ComponentType.Create <NetworkSyncState>(), componentType, ComponentType.Subtractive <NetworkComponentState <T> >(), ComponentType.Create <NetworktAuthority>()); ComponentDataArray <T> components = group.GetComponentDataArray <T>(); ComponentDataArray <NetworkSyncState> networkSyncStateComponents = group.GetComponentDataArray <NetworkSyncState>(); EntityArray entities = group.GetEntityArray(); NetworkMemberInfo[] networkMemberInfos = reflectionUtility.GetNetworkMemberInfo(componentType); for (int i = 0; i < entities.Length; i++) { NetworkSyncState networkSyncState = networkSyncStateComponents[i]; ComponentDataContainer componentData = new ComponentDataContainer { ComponentTypeId = reflectionUtility.GetComponentTypeID(componentType) }; T component = components[i]; for (int j = 0; j < networkMemberInfos.Length; j++) { componentData.MemberData.Add(new MemberDataContainer { MemberId = j, Data = (networkMemberInfos[j] as NetworkMemberInfo <T>).GetValue(component), }); } ownNetworkSendMessageUtility.AddComponent(entities[i], networkSyncState.actorId, networkSyncState.networkId, componentData); AllNetworkSendMessageUtility.AddComponent(entities[i], networkSyncState.actorId, networkSyncState.networkId, componentData); int numberOfMembers = reflectionUtility.GetNumberOfMembers(componentType.GetManagedType()); networkFactory.CreateNetworkComponentData <T>(entities[i], numberOfMembers); PostUpdateCommands.AddComponent(entities[i], new NetworkComponentState <T>()); } }
private void NetworkManager_OnMasterClientChanged(int oldMasterClientId, int newMasterClientId) { if (networkManager.LocalPlayerID == oldMasterClientId) { ComponentGroup group = GetComponentGroup(ComponentType.Create <NetworkSyncState>(), ComponentType.Create <NetworkSync>(), ComponentType.Create <NetworktAuthority>()); EntityArray entities = group.GetEntityArray(); ComponentDataArray <NetworkSync> networkSync = group.GetComponentDataArray <NetworkSync>(); for (int i = 0; i < entities.Length; i++) { if (networkSync[i].authority != Authority.Client) { PostUpdateCommands.RemoveComponent <NetworktAuthority>(entities[i]); } } } if (networkManager.LocalPlayerID == newMasterClientId) { ComponentGroup group = GetComponentGroup(ComponentType.Create <NetworkSyncState>(), ComponentType.Create <NetworkSync>(), ComponentType.Subtractive <NetworktAuthority>()); ComponentDataArray <NetworkSync> networkSync = group.GetComponentDataArray <NetworkSync>(); EntityArray entities = group.GetEntityArray(); for (int i = 0; i < entities.Length; i++) { if (networkSync[i].authority != Authority.Client) { PostUpdateCommands.AddComponent(entities[i], new NetworktAuthority()); } } } }
private void NetworkManager_OnDisconnect() { ComponentGroup group = GetComponentGroup(ComponentType.Create <NetworkSyncState>(), ComponentType.Create <NetworkSync>()); ComponentDataArray <NetworkSyncState> networkSyncStateComponents = group.GetComponentDataArray <NetworkSyncState>(); ComponentDataArray <NetworkSync> networkSyncComponents = group.GetComponentDataArray <NetworkSync>(); EntityArray entities = group.GetEntityArray(); for (int i = 0; i < entities.Length; i++) { Entity entity = entities[i]; PostUpdateCommands.RemoveComponent <NetworkSyncState>(entity); if (networkSyncStateComponents[i].actorId != networkManager.LocalPlayerID && networkSyncComponents[i].authority != Authority.Scene) { PostUpdateCommands.DestroyEntity(entity); if (EntityManager.HasComponent <Transform>(entity)) { gameObjectsToDestroy.Add(EntityManager.GetComponentObject <Transform>(entity).gameObject); } } else if (EntityManager.HasComponent <NetworktAuthority>(entity)) { PostUpdateCommands.RemoveComponent <NetworktAuthority>(entity); } for (int j = 0; j < RemoveComponentOnDestroyEntityMethods.Count; j++) { RemoveComponentOnDestroyEntityMethods[j].Invoke(this, entity); } } }
public void CopyResultsTo(ref FixedArrayArray <PolygonId> agentPaths, ref ComponentDataArray <CrowdAgentNavigator> agentNavigators) { var state = m_State[0]; for (var i = 0; i < state.resultPathsCount; i++) { var index = m_AgentIndices[i]; var resultPathInfo = m_ResultRanges[i]; var resultNodes = new NativeSlice <PolygonId>(m_ResultNodes, resultPathInfo.begin, resultPathInfo.size); var agentPathBuffer = agentPaths[index]; var pathLength = math.min(resultNodes.Length, agentPathBuffer.Length); for (var j = 0; j < pathLength; j++) { agentPathBuffer[j] = resultNodes[j]; } var navigator = agentNavigators[index]; navigator.pathStart = resultPathInfo.start; navigator.pathEnd = resultPathInfo.end; navigator.pathSize = pathLength; navigator.StartMoving(); agentNavigators[index] = navigator; } }
protected override JobHandle OnUpdate(JobHandle inputDeps) { if (!initialized) { Initialize(); initialized = true; } sphereRequestsGroup = GetComponentGroup(typeof(SphereRequest)); if (sphereRequestsGroup.CalculateLength() == 0) { return(inputDeps); } ComponentDataArray <SphereRequest> sphereRequests = sphereRequestsGroup.GetComponentDataArray <SphereRequest>(); JobHandle sgj = new SphereGenerateSystem.SphereGenerateJob { requests = sphereRequests, commandBuffer = generateBarrier.CreateCommandBuffer().ToConcurrent(), }.Schedule(sphereRequests.Length, 64, inputDeps); EntityArray sphereRequestEntities = sphereRequestsGroup.GetEntityArray(); JobHandle srcj = new SphereRequestCleanupJob { entities = sphereRequestEntities, commandBuffer = cleanupBarrier.CreateCommandBuffer().ToConcurrent(), }.Schedule(sphereRequestEntities.Length, 64, sgj); return(srcj); }
private void Entities() { EntityArray entities = addedSyncEntities.entities; ComponentDataArray <NetworkSync> networkSyncs = addedSyncEntities.networkSyncComponents; ComponentDataArray <NetworkSyncState> networkSyncStates = addedSyncEntities.networkSyncStateComponents; for (int i = 0; i < entities.Length; i++) { int instanceId = networkSyncs[i].instanceId; Entity entity = entities[i]; NetworkEntityData networkEntityData = new NetworkEntityData { InstanceId = networkSyncs[i].instanceId, NetworkSyncEntity = new NetworkSyncEntity { ActorId = networkSyncStates[i].actorId, NetworkId = networkSyncStates[i].networkId, } }; for (int j = 0; j < GetComponentDataMethods.Count; j++) { if (GetComponentDataMethods[j].Invoke(this, ref entity, out ComponentDataContainer componentData)) { networkEntityData.ComponentData.Add(componentData); } } networkSendMessageUtility.AddEntity(networkEntityData); } }
protected override void OnUpdate() { m_mapDataEntities = m_mapDataGroup.GetEntityArray(); m_DataGroupIndex = m_mapDataGroup.GetComponentDataArray <MapIndexComponent>(); m_DataGroupData = m_mapDataGroup.GetComponentDataArray <MapDataComponent>(); for (int i = 0; i < 128 * 128; i++) { m_rawFloatData[m_DataGroupIndex[i].Value.x, m_DataGroupIndex[i].Value.y] = m_DataGroupData[i].Value; m_entityIndexData[m_DataGroupIndex[i].Value.x, m_DataGroupIndex[i].Value.y] = i; } for (int w = 0; w < 128; w++) { for (int h = 0; h < 128; h++) { //------ INNER LOOP ------ for (int wx = -1; wx <= 1; wx++) { for (int hx = -1; hx <= 1; hx++) { } } //------ INNER LOOP ------ } } m_mapDataGroup.Dispose(); Unity.Entities.World.Active.GetExistingManager <MetaDataSystem>().Enabled = false; }
protected override JobHandle OnUpdate(JobHandle inputDeps) { //Arrays de obstaculos activos y de sus posiciones obstaculosActivos = m_ObstaculosActivos.GetEntityArray(); ComponentDataArray <Position> posicionesObstaculos = m_ObstaculosActivos.GetComponentDataArray <Position>(); //Arrays de premios activos y de sus posiciones premiosActivos = m_PremiosActivos.GetEntityArray(); ComponentDataArray <Position> posicionesPremios = m_PremiosActivos.GetComponentDataArray <Position>(); var jobObs = new DesactivarObstaculo { Commands = _collisionBarrier.CreateCommandBuffer() }; JobHandle jO = jobObs.Schedule(this, inputDeps); var jobPrem = new DesactivarPremio { Commands = _collisionBarrier.CreateCommandBuffer() }; JobHandle jP = jobPrem.Schedule(this, jO); //"depende" del job anterior (espera a que termine) var jobPalillos = new DesactivarPalillos { Commands = _collisionBarrier.CreateCommandBuffer() }; JobHandle jPl = jobPalillos.Schedule(this, jP); //"depende" del job anterior (espera a que termine) return(jPl); }
public void Initialize(ComponentGroup group, Vector3[] vertices, Vector3[] normals, NativeCounter.Concurrent counter) { _thicknesses = group.GetComponentDataArray <Thickness>(); _lines = group.GetComponentDataArray <Line>(); _vertices = UnsafeUtility.AddressOf(ref vertices[0]); _normals = UnsafeUtility.AddressOf(ref normals[0]); _counter = counter; }
/// <summary> /// Like `EntityArray.ToArray` but for CDA. /// </summary> public static List <T> CopyToList <T>(this ComponentDataArray <T> cda) where T : struct, IComponentData { using (var na = new NativeArray <T>(cda.Length, Allocator.Temp)) { cda.CopyTo(na); List <T> list = new List <T>(na); return(list); } }
public AgentJob(int count) { timeDelta = Time.deltaTime; cluster = new Cluster(count); positions = new ComponentDataArray <Position>(); rotations = new ComponentDataArray <Rotation>(); agents = new ComponentDataArray <AgentData>(); agentData = default(SharedAgentData); }
protected override void OnUpdate() { if (flag) { return; } flag = true; ComponentGroup planetGroup = GetComponentGroup(typeof(Planet), typeof(PlanetNoise)); ComponentGroup dataGroup = GetComponentGroup(typeof(PlanetSharedData)); ComponentDataArray <Planet> planetArray = planetGroup.GetComponentDataArray <Planet>(); ComponentDataArray <PlanetNoise> noiseArray = planetGroup.GetComponentDataArray <PlanetNoise>(); SharedComponentDataArray <PlanetSharedData> dataArray = dataGroup.GetSharedComponentDataArray <PlanetSharedData>(); GameObject prefab = dataArray[0].nodePrefab; for (int i = 0; i < planetArray.Length; ++i) { Planet planet = planetArray[i]; PlanetNoise noise = noiseArray[i]; HyperDistance r = planet.radius; for (int n = 0; n < 20; ++n) { Entity nodeEntity = EntityManager.Instantiate(prefab); TerrainNode node = EntityManager.GetComponentData <TerrainNode>(nodeEntity); node.level = 0; node.planetData = planet; node.noiseData = noise; node.built = 0; node.divided = 0; node.hyperDistant = 1; int idx = n * 3; node.corner1 = icoVerts[idx]; node.corner2 = icoVerts[idx + 1]; node.corner3 = icoVerts[idx + 2]; EntityManager.SetComponentData(nodeEntity, node); HyperPosition pos = math.normalize(node.corner1 + node.corner2 + node.corner3) * r; PrecisePosition prspos = new PrecisePosition { pos = pos.prs }; EntityManager.SetComponentData(nodeEntity, prspos); OctantPosition octpos = new OctantPosition { pos = pos.oct }; EntityManager.SetComponentData(nodeEntity, octpos); } } }
public static void DebugConnection(ComponentDataArray <Position> pos, ComponentDataArray <HitResult> hitresult) { for (int i = 0; i < pos.Length; i++) { int frontid = hitresult[i].FrontEntityId; if (frontid != 0) { Debug.DrawLine(pos[i].Value, pos[frontid].Value); } } }
public static void CopyFrom <T>(this ComputeBuffer dst, ref ComponentDataArray <T> src, int length) where T : struct, IComponentData { NativeArray <T> chunkArray; for (int copiedCount = 0; copiedCount < length; copiedCount += chunkArray.Length) { // Allocator.InvalidであるからDisposeしてはならぬ。 chunkArray = src.GetChunkArray(copiedCount, length - copiedCount); dst.SetData(chunkArray, 0, copiedCount, chunkArray.Length); } }
private AABB GetBoundingBox(ComponentDataArray <AABB> AABB) { AABB result = AABB[0]; for (int i = 0; i < _capacity; i++) { result.Min = math.min(result.Min, AABB[i].Min); result.Max = math.max(result.Max, AABB[i].Max); } return(result); }
public SetTargetJob(float dt, float time, EntityCommandBuffer buffer, NativeQueue <DamageInfo> .Concurrent sendDamage, NativeQueue <PathfindingInfo> .Concurrent needsPathfinding, EntityArray entities, ComponentDataArray <NavAgent> agents, ComponentDataArray <AnimatedState> animations, ComponentDataArray <Position> positions, ComponentDataArray <Unit> units) { this.dt = dt; this.animations = animations; this.entities = entities; this.agents = agents; this.positions = positions; this.units = units; this.sendDamage = sendDamage; this.needsPathfinding = needsPathfinding; this.buffer = buffer; this.time = time; }
//----------------------------------------------------------------------------- public unsafe static void CopyMatrices(ComponentDataArray <TransformMatrix> transforms, int beginIndex, int length, Matrix4x4[] outMatrices) { fixed(Matrix4x4 *matricesPtr = outMatrices) { Assert.AreEqual(sizeof(Matrix4x4), sizeof(TransformMatrix)); var matricesSlice = NativeSliceUnsafeUtility.ConvertExistingDataToNativeSlice <TransformMatrix>(matricesPtr, sizeof(Matrix4x4), length); #if ENABLE_UNITY_COLLECTIONS_CHECKS NativeSliceUnsafeUtility.SetAtomicSafetyHandle(ref matricesSlice, AtomicSafetyHandle.GetTempUnsafePtrSliceHandle()); #endif transforms.CopyTo(matricesSlice, beginIndex); } }
public static unsafe void CopyFromUnsafe <T>(this ComputeBuffer dst, ref ComponentDataArray <T> src, int length) where T : struct, IComponentData { var stride = dst.stride; var ptr = dst.GetNativeBufferPtr(); NativeArray <T> chunkArray; for (int copiedCount = 0, chunkLength = 0; copiedCount < length; copiedCount += chunkLength) { chunkArray = src.GetChunkArray(copiedCount, length - copiedCount); chunkLength = chunkArray.Length; UnsafeUtility.MemCpy(ptr.ToPointer(), NativeArrayUnsafeUtility.GetUnsafeBufferPointerWithoutChecks(chunkArray), chunkLength * stride); ptr += chunkLength * stride; } }
public JobCalculateMovement(ComponentDataArray <CharacterControllerState> states, ComponentDataArray <StVelocity> velocities, ComponentDataArray <DefStGroundRunSettings> settings, ComponentDataArray <DefStRunInput> inputses, NativeArray <quaternion> rotations, float deltaTime) { States = states; Velocities = velocities; Settings = settings; Inputs = inputses; Rotations = rotations; DeltaTime = deltaTime; }
public unsafe static void CopyMatrices(ComponentDataArray <EntityInstanceRendererTransform> transforms, int beginIndex, int length, Matrix4x4[] outMatrices) { // @TODO: This is only unsafe because the Unity DrawInstances API takes a Matrix4x4[] instead of NativeArray. /// And we also want the code to be really fast. fixed(Matrix4x4 *matricesPtr = outMatrices) { UnityEngine.Assertions.Assert.AreEqual(sizeof(Matrix4x4), sizeof(EntityInstanceRendererTransform)); var matricesSlice = Unity.Collections.LowLevel.Unsafe.NativeSliceUnsafeUtility.ConvertExistingDataToNativeSlice <EntityInstanceRendererTransform>(matricesPtr, sizeof(Matrix4x4), length); #if ENABLE_UNITY_COLLECTIONS_CHECKS Unity.Collections.LowLevel.Unsafe.NativeSliceUnsafeUtility.SetAtomicSafetyHandle(ref matricesSlice, AtomicSafetyHandle.GetTempUnsafePtrSliceHandle()); #endif transforms.CopyTo(matricesSlice, beginIndex); } }
public void Initialize( ComponentGroup group, UnityEngine.Vector3 [] vertices, UnityEngine.Vector3 [] normals, NativeCounter.Concurrent counter ) { Particles = group.GetComponentDataArray <Particle>(); Positions = group.GetComponentDataArray <Position>(); Triangles = group.GetComponentDataArray <Triangle>(); Variants = group.GetSharedComponentDataArray <SimpleParticle>(); Vertices = UnsafeUtility.AddressOf(ref vertices[0]); Normals = UnsafeUtility.AddressOf(ref normals[0]); Counter = counter; }
void Render(int i, ref ComponentDataArray <Position> positionDataArray, ref ComponentDataArray <Heading> headingDataArray) { var length = positionDataArray.Length; if (length == 0) { return; } var sprite = sharedComponents[i]; var index = sharedIndices[i]; if (!buffers.TryGetValue(index, out var buffer)) { buffers.Add(index, buffer = new ComputeBuffer(length, PositionHeading.Stride)); sprite.material.SetBuffer(ShaderProperty_PositionBuffer, buffer); } if (buffer.count < length) { buffer.Release(); buffers.Add(index, buffer = new ComputeBuffer(length, PositionHeading.Stride)); sprite.material.SetBuffer(ShaderProperty_PositionBuffer, buffer); } //メッシュの頂点数 args[0] = sprite.mesh.GetIndexCount(0); // 何体描画するか args[1] = (uint)length; argsList[i].SetData(args, 0, 0, 2); // 時代はSpan<T>、ただしUnityの場合はNativeSlice<T> // Allocator.Persistentにしてクラスフィールドにしたほうがいいのかどうなのかいまいちわからない。 using (var position = new NativeArray <Position>(length, Allocator.Temp, NativeArrayOptions.UninitializedMemory)) using (var heading = new NativeArray <Heading>(length, Allocator.Temp, NativeArrayOptions.UninitializedMemory)) { var positionHeading = new NativeArray <PositionHeading>(length, Allocator.Temp, NativeArrayOptions.UninitializedMemory); positionDataArray.CopyTo(position); for (int j = 0; j < positionHeading.Length; j++) { positionHeading[j] = new PositionHeading { Position = position[j].Value, HeadingX = heading[j].Value.x }; } buffer.SetData(positionHeading, 0, 0, length); positionHeading.Dispose(); } // 影を描画しないしさせない鉄の意志。 Graphics.DrawMeshInstancedIndirect(sprite.mesh, 0, sprite.material, Bounds, argsList[i], 0, null, ShadowCastingMode.Off, false, 0, Camera, LightProbeUsage.Off, null); }