/// <summary> /// Returns prefab index /// </summary> /// <returns></returns> private void _GenerateNewPatternPrefab(int i_prefablIndex) { // NativeArray <Blocks.PatternPrefab.RequestAddPrefabBufferElement> a_compositesInPatternPrefab = new NativeArray <Blocks.PatternPrefab.RequestAddPrefabBufferElement> ( i_compositesCountPerPatternGroup, Allocator.Temp ) ; Debug.Log("Need get composite prefab index"); // entityManager.AddComponent ( requestAddNewPrefabEntity, ) ; // store temp data Blocks.Pattern.CompositeInPatternPrefabComponent compositeInPatternPrefab = new Blocks.Pattern.CompositeInPatternPrefabComponent(); //int i_maxCount = i_initialIndex * i_currentPrefabsCount + i_compositesCountPerPatternGroup ; int i_patternOffsetIndex = i_prefablIndex * i_compositesCountPerPatternGroup; for (int z = 0; z < i_compositesCountInDepthPerPatternGroup; z++) { int i_zOffsetIndex = i_patternOffsetIndex + z * i_compositesCountInColumnPerPatternGroup * i_compositesCountInRowPerPatternGroup; for (int x = 0; x < i_compositesCountInRowPerPatternGroup; x++) { int i_xOffsetIndex = i_zOffsetIndex + x * i_compositesCountInColumnPerPatternGroup; for (int y = 0; y < i_compositesCountInColumnPerPatternGroup; y++) { compositeInPatternPrefab.f3_position = new float3( x * f_compositeScale, y * f_compositeScale, z * f_compositeScale ); compositeInPatternPrefab.f3_scale = new float3(1, 1, 1) * f_compositeScale; /* * compositeInPatternPrefab.f3_position = new float3 ( * random.NextFloat ( -0.5f, 0.5f ), * random.NextFloat ( -0.5f, 0.5f ), * random.NextFloat ( -0.5f, 0.5f ) * ) ; */ compositeInPatternPrefab.i_compositePrefabIndex = UnityEngine.Random.Range(1, 5); // generate random composites // compositeInPatternPrefab.i_compositePrefabIndex = random.NextInt ( 1, 3 ) ; // assign composite prefab index (0 is ignored) // compositeInPatternPrefab.i_compositePrefabIndex = random.NextInt ( 0, 5 ) ; // assign composite prefab index a_requestAddComposites2PatternPrefab [i_xOffsetIndex + y] = compositeInPatternPrefab; } } } // When scale of any axis is 0 no mesh is generated // For each axis with scale greater than 1, there is offset applied, // assuming pivot of mesh, is at the position of this composite. _GreedyScalling(i_prefablIndex); }
static public Blocks.Pattern.CompositeInPatternPrefabComponent _GetCompositeFromPatternPrefab(int i_index) { Blocks.Pattern.CompositeInPatternPrefabComponent compositeInPatternPrefab = a_patternPrefabs [i_index]; return(compositeInPatternPrefab); }
static private void _GreedyScallingYAxis(int i_prefablIndex) { // Blocks.Pattern.CompositeInPatternPrefabComponent compositeInPatternPrefab ; int i_lastDifferentCompositeTypeIndex = -1; int i_lastDifferentCompositeTypeId = -1; int i_initialOffsetIndex = i_prefablIndex * i_compositesCountPerPatternGroup; int i_maxCount = i_initialOffsetIndex + i_compositesCountPerPatternGroup; for (int i = i_initialOffsetIndex * i_currentPrefabsCount; i < i_maxCount; i++) { Blocks.Pattern.CompositeInPatternPrefabComponent compositeInPatternPrefab = a_requestAddComposites2PatternPrefab [i]; bool isXaxisChanged = false; if (i_lastDifferentCompositeTypeIndex >= 0) // make sure index is none negative { isXaxisChanged = math.abs(compositeInPatternPrefab.f3_position.x - a_requestAddComposites2PatternPrefab [i_lastDifferentCompositeTypeIndex].f3_position.x) > 0.001f; // check for floating point error } bool isZaxisChanged = false; if (i_lastDifferentCompositeTypeIndex >= 0) // make sure index is none negative { isZaxisChanged = math.abs(compositeInPatternPrefab.f3_position.z - a_requestAddComposites2PatternPrefab [i_lastDifferentCompositeTypeIndex].f3_position.z) > 0.001f; // check for floating point error } if (isXaxisChanged || isZaxisChanged || i_lastDifferentCompositeTypeId != compositeInPatternPrefab.i_compositePrefabIndex) { // composite type has changed from previous one i_lastDifferentCompositeTypeIndex = i; i_lastDifferentCompositeTypeId = compositeInPatternPrefab.i_compositePrefabIndex; // compositeInPatternPrefab.f3_scale = new float3 ( 1,1,1 ) * f_compositeScale ; } else { Blocks.Pattern.CompositeInPatternPrefabComponent compositeInPatternPrefabMatch = a_requestAddComposites2PatternPrefab [i_lastDifferentCompositeTypeIndex]; // composite type is the same as previous one // grow scale in the traversing direction compositeInPatternPrefabMatch.f3_scale += new float3(0, 1, 0) * f_compositeScale; compositeInPatternPrefabMatch.f3_position += new float3(0, f_compositeScale * 0.5f, 0); // first consecutive composite index, with mathching type a_requestAddComposites2PatternPrefab [i_lastDifferentCompositeTypeIndex] = compositeInPatternPrefabMatch; // This composite is same type as previous composite. // Previous composite mesh will be scaled, to overlap this composite. // hence mesh is not required. compositeInPatternPrefab.i_compositePrefabIndex = -1; // } a_requestAddComposites2PatternPrefab [i] = compositeInPatternPrefab; } // for }
static private void _GreedyScallingXAxis(int i_prefablIndex) { int i_lastDifferentCompositeTypeIndex = -1; int i_lastDifferentCompositeTypeId = -1; int i_initialOffsetIndex = i_prefablIndex * i_compositesCountPerPatternGroup; // int i_maxCount = i_initialOffsetIndex + i_compositesCountPerPatternGroup ; // for ( int x = i_initialOffsetIndex * i_currentPrefabsCount; x < i_maxCount; x ++ ) for (int y = 0; y < i_compositesCountInColumnPerPatternGroup; y++) { int i_yOffsetIndex = i_initialOffsetIndex + y; // * i_compositesCountInColumnPerPatternGroup ; for (int x = 0; x < i_compositesCountInRowPerPatternGroup; x++) { int i_index = i_yOffsetIndex + x * i_compositesCountInColumnPerPatternGroup; Blocks.Pattern.CompositeInPatternPrefabComponent compositeInPatternPrefab = a_requestAddComposites2PatternPrefab [i_index]; bool isYaxisChanged = false; if (i_lastDifferentCompositeTypeIndex >= 0) // make sure index is none negative { isYaxisChanged = math.abs(compositeInPatternPrefab.f3_position.y - a_requestAddComposites2PatternPrefab [i_lastDifferentCompositeTypeIndex].f3_position.y) > 0.001f; // check for floating point error } bool isZaxisChanged = false; if (i_lastDifferentCompositeTypeIndex >= 0) // make sure index is none negative { isZaxisChanged = math.abs(compositeInPatternPrefab.f3_position.z - a_requestAddComposites2PatternPrefab [i_lastDifferentCompositeTypeIndex].f3_position.z) > 0.001f; // check for floating point error } if (isYaxisChanged || isZaxisChanged || compositeInPatternPrefab.i_compositePrefabIndex < 0 || //compositeInPatternPrefab.i_compositePrefabIndex >= 0 && i_lastDifferentCompositeTypeId != compositeInPatternPrefab.i_compositePrefabIndex ) { i_lastDifferentCompositeTypeIndex = i_index; i_lastDifferentCompositeTypeId = compositeInPatternPrefab.i_compositePrefabIndex; } else { Blocks.Pattern.CompositeInPatternPrefabComponent compositeInPatternPrefabMatch = a_requestAddComposites2PatternPrefab [i_lastDifferentCompositeTypeIndex]; float3 f3_scaleDiffAbs = math.abs(compositeInPatternPrefab.f3_scale - compositeInPatternPrefabMatch.f3_scale); // bool isScaleMatch = f3_scaleDiffAbs.x < 0.001f && f3_scaleDiffAbs.y < 0.001f && f3_scaleDiffAbs.z < 0.001f ; // float prcision error check bool isScaleMatch = f3_scaleDiffAbs.y < 0.001f; // float prcision error check, from previous axis iteration if (isScaleMatch) // is scale matching of chekced composites? { // composite type is the same as previous one // grow scale in the traversing direction compositeInPatternPrefabMatch.f3_scale += new float3(1, 0, 0) * f_compositeScale; compositeInPatternPrefabMatch.f3_position += new float3(f_compositeScale * 0.5f, 0, 0); // first consecutive composite index, with mathching type a_requestAddComposites2PatternPrefab [i_lastDifferentCompositeTypeIndex] = compositeInPatternPrefabMatch; // This composite is same type as previous composite. // Previous composite mesh will be scaled, to overlap this composite. // hence mesh is not required. compositeInPatternPrefab.i_compositePrefabIndex = -1; // } else { // is diferent i_lastDifferentCompositeTypeIndex = i_index; i_lastDifferentCompositeTypeId = compositeInPatternPrefab.i_compositePrefabIndex; } } // for a_requestAddComposites2PatternPrefab [i_index] = compositeInPatternPrefab; } // for } // for }
/// <summary> /// Assigns composite patter, to selected entity /// </summary> /// <param name="entityWithPatern"></param> static private EntityCommandBuffer _AssignComposites2Pattern(EntityCommandBuffer commandBuffer, RequestPatternSetupData requestPatternSetupData, int i_patternGroupIndex, EntityArray a_spareCompositeEntities) { //Entity assignComposites2PatternEntity = assignComposite2PatternData.a_entities [i_entityWithPaternIndex] ; //CompositeComponent compositeComponent = assignComposite2PatternData.a_compositeEntityRelatives [i_entityWithPaternIndex] ; Blocks.PatternComponent pattern = requestPatternSetupData.a_compositesInPattern [i_patternGroupIndex]; BufferArray <Common.BufferElements.EntityBuffer> a_patternsStore = requestPatternSetupData.a_entityBuffer; int i_patternIndex = pattern.i_patternIndex; int i_patternOffsetIndex = i_patternIndex * Pattern.AddPatternPrefabSystem.i_compositesCountPerPatternGroup; Entity requestPatternSetupEntity = requestPatternSetupData.a_entities [i_patternGroupIndex]; // clear store for each pattern group entity a_patternsStore [i_patternGroupIndex].Clear(); int i_spareEntitiesOffsetIndex = i_patternGroupIndex * Pattern.AddPatternPrefabSystem.i_compositesCountPerPatternGroup; // int i_patterGroupOffsetIndex = i_componentsPatternIndex * i_compositesCountPerPatternGroup ; // assign composite entity to entity with pattern for (int i_spareEntityIndex = 0; i_spareEntityIndex < Pattern.AddPatternPrefabSystem.i_compositesCountPerPatternGroup; i_spareEntityIndex++) { // get element from patern prefab to copy into group int i_compositeInPrefabIndex = i_patternOffsetIndex + i_spareEntityIndex; Blocks.Pattern.CompositeInPatternPrefabComponent compositeInPatternPrefab = Pattern.AddPatternPrefabSystem.a_patternPrefabs [i_compositeInPrefabIndex]; // This composite is different type as previous composite. // This composite mesh will be scaled, to overlap next composite, if the type is the same. // Hence next mesh may be not required, hwen type is < 0 if (compositeInPatternPrefab.i_compositePrefabIndex >= 0) { // Assign relative references to composite Blocks.CompositeComponent composite = new CompositeComponent() { blockEntity = pattern.blockEntity, // assign grand parent entity to composite patternEntity = requestPatternSetupEntity, // assign parent pattern group entity to composite i_inPrefabIndex = i_compositeInPrefabIndex // used prefab }; Entity spareCompositeEntity = a_spareCompositeEntities [i_spareEntitiesOffsetIndex + i_spareEntityIndex]; Common.BufferElements.EntityBuffer spareEntityBuffer = new Common.BufferElements.EntityBuffer() { entity = spareCompositeEntity }; // expand buffer array if is too small a_patternsStore [i_patternGroupIndex].Add(spareEntityBuffer); commandBuffer.SetComponent(spareCompositeEntity, composite); MeshInstanceRenderer renderer; switch (compositeInPatternPrefab.i_compositePrefabIndex) { case 1: renderer = Bootstrap.octreeCenter02; break; case 2: renderer = Bootstrap.octreeCenter03; break; case 3: renderer = Bootstrap.octreeCenter04; break; case 4: renderer = Bootstrap.octreeCenter05; break; case 5: renderer = Bootstrap.octreeCenter06; break; case 6: renderer = Bootstrap.octreeCenter07; break; default: renderer = Bootstrap.octreeCenter01; break; } commandBuffer.SetSharedComponent(spareCompositeEntity, renderer); Position position = new Position() { Value = compositeInPatternPrefab.f3_position }; commandBuffer.SetComponent(spareCompositeEntity, position); Scale scale = new Scale() { Value = compositeInPatternPrefab.f3_scale * pattern.f_baseScale }; commandBuffer.SetComponent(spareCompositeEntity, scale); // Store composite back in //Pattern.AddPatternPrefabSystem.a_patternPrefabs [ i_compositeInPrefabIndex ] = compositeInPatternPrefab ; // Composite entity has been assigned // Now is ready for rendering, or other processing commandBuffer.RemoveComponent <Common.Components.IsNotAssignedTag> (spareCompositeEntity); } else { // Iteration reached composite index in the prefab store, which should be ignored. // Any later index for this prefab should also be ignored, as expanded mesh took its place. break; } } return(commandBuffer); }
// public float3 f3_moveAbout ; // Blocks.MovePatternComonent public void Execute() // for IJob // public void Execute ( int i ) // for IJobParallelFor { // f3_moveAbout += new float3 ( random.NextFloat ( -0.01f, 0.01f ) ,0,0 ) ; // iterate through patterns group, to move its composites for (int i = 0; i < movePatternData.Length; i++) { PatternComponent patternGroup = movePatternData.a_compositePatternComponent [i]; //int i_ComponentsPatternIndex = compositePatternComponent.i_patternIndex ; patternGroup.f_baseScale = 3; // test temp movePatternData.a_compositePatternComponent [i] = patternGroup; MovePattern movePattern = movePatternData.a_movePatterns [i]; //int i_entityBufferCount = movePatternData.a_compositeEntities [i].Length ; //movePattern.f3_position = f3_moveAbout + new float3 (1,0,0) * i * patternGroup.f_baseScale + new float3 (5,0,0) ; // * 0.001f; movePattern.f3_position = f3_moveAbout + patternGroup.f3_localPosition * patternGroup.f_baseScale + new float3(5, 0, 0); // * 0.001f; movePatternData.a_movePatterns [i] = movePattern; // update float f_distance = math.lengthSquared(movePattern.f3_position); Entity paternEntity = movePatternData.a_entities [i]; // move composite Position position = new Position() { Value = movePattern.f3_position }; //position.Value = blockCompositeBufferElement.f3_position + movePattern.f3_position ; // commandBuffer.SetComponent ( paternEntity, position ) ; // commandBuffer.AddComponent ( paternEntity, new Blocks.Pattern.RequestPatternSetupTag () ) ; int i_entityBufferCount = movePatternData.a_compositeEntities [i].Length; if (i_entityBufferCount > 0) { // iterate through pattern's group composites for (int i_bufferIndex = 0; i_bufferIndex < i_entityBufferCount; i_bufferIndex++) { Common.BufferElements.EntityBuffer entityBuffer = movePatternData.a_compositeEntities [i][i_bufferIndex]; Entity compositeEntity = entityBuffer.entity; // get composite data Blocks.CompositeComponent compositeComponent = a_compositeComponents [compositeEntity]; Blocks.Pattern.CompositeInPatternPrefabComponent blockCompositeBufferElement = Pattern.AddPatternPrefabSystem._GetCompositeFromPatternPrefab(compositeComponent.i_inPrefabIndex); // move composite // Position position = new Position () ; position.Value = blockCompositeBufferElement.f3_position * patternGroup.f_baseScale + movePattern.f3_position; commandBuffer.SetComponent(compositeEntity, position); Scale scale = new Scale() { Value = blockCompositeBufferElement.f3_scale * patternGroup.f_baseScale }; commandBuffer.SetComponent(compositeEntity, scale); if (f_distance > 10) { } else { // Entity paternEntity = movePatternData.a_entities [i] ; // commandBuffer.AddComponent ( paternEntity, new Blocks.Pattern.RequestPatternReleaseTag () ) ; //Test02.AddBlockSystem._AddBlockRequestViaCustomBufferWithEntity ( commandBuffer, paternEntity, movePattern.f3_position, new float3 (1,1,1), float3.zero, new Entity (), float4.zero ) ; } } // for } else { Entity compositeEntity = patternGroup.blockEntity; // get composite data Blocks.CompositeComponent compositeComponent = a_compositeComponents [compositeEntity]; Blocks.Pattern.CompositeInPatternPrefabComponent blockCompositeBufferElement = Pattern.AddPatternPrefabSystem._GetCompositeFromPatternPrefab(compositeComponent.i_inPrefabIndex); // move composite // Position position = new Position () ; position.Value = blockCompositeBufferElement.f3_position * patternGroup.f_baseScale + movePattern.f3_position; commandBuffer.SetComponent(compositeEntity, position); //Scale scale = new Scale () { Value = blockCompositeBufferElement.f3_scale * patternGroup.f_baseScale * 3 } ; //commandBuffer.SetComponent ( compositeEntity, scale ) ; } } // for }
// public float3 f3_moveAbout ; // Blocks.MovePatternComonent public void Execute() // for IJob // public void Execute ( int i ) // for IJobParallelFor { // Iterate through patterns groups, to move its composites for (int i = 0; i < movePatternData.Length; i++) { PatternComponent patternGroup = movePatternData.a_compositePatternComponent [i]; // int i_componentsPatternIndex = patternGroup.i_patternIndex ; patternGroup.f_baseScale = 1f; // test temp MovePattern movePattern = movePatternData.a_movePatterns [i]; int i_entityBufferCount = movePatternData.a_compositeEntities [i].Length; // position offset test //movePattern.f3_position = f3_moveAbout + patternGroup.f_localPosition * patternGroup.f_baseScale * 3; // * 0.001f; // movePattern.f3_position = f3_moveAbout + patternGroup.f_localPosition * patternGroup.f_baseScale + new float3 (5,0,0) ; // * 0.001f; movePattern.f3_position = f3_moveAbout + patternGroup.f3_localPosition * 3 + new float3(5, 0, 0); // * 0.001f; movePatternData.a_movePatterns [i] = movePattern; // update float f_distance = math.lengthSquared(movePattern.f3_position); // if ( f_distance < 8 ) // { // iterate through pattern's group composites for (int i_bufferIndex = 0; i_bufferIndex < i_entityBufferCount; i_bufferIndex++) { Common.BufferElements.EntityBuffer entityBuffer = movePatternData.a_compositeEntities [i][i_bufferIndex]; Entity compositeEntity = entityBuffer.entity; if (compositeEntity.Index != 0) { // get composite data Blocks.CompositeComponent compositeComponent = a_compositeComponents [compositeEntity]; Blocks.Pattern.CompositeInPatternPrefabComponent blockCompositeBufferElement = Pattern.AddPatternPrefabSystem._GetCompositeFromPatternPrefab(compositeComponent.i_inPrefabIndex); // move composite Position position = new Position(); position.Value = blockCompositeBufferElement.f3_position * patternGroup.f_baseScale + movePattern.f3_position; //position.Value = blockCompositeBufferElement.f3_position * 3 + movePattern.f3_position ; // movePattern.f3_position = f3_moveAbout + patternGroup.f_localPosition * patternGroup.f_baseScale + new float3 (5,0,0) ; // * 0.001f; commandBuffer.SetComponent(compositeEntity, position); Scale scale = new Scale() { Value = blockCompositeBufferElement.f3_scale * patternGroup.f_baseScale }; commandBuffer.SetComponent(compositeEntity, scale); } } // for // } // else // { /* * Entity paternEntity = movePatternData.a_entities [i] ; * commandBuffer.AddComponent ( paternEntity, new Blocks.Pattern.RequestPatternReleaseTag () ) ; * * // Test02.AddBlockSystem._AddBlockRequestViaCustomBufferWithEntity ( commandBuffer, paternEntity, movePattern.f3_position, new float3 (1,1,1), float3.zero, new Entity (), float4.zero ) ; * commandBuffer.AddComponent ( paternEntity, new Common.Components.Lod05Tag () ) ; // test only * commandBuffer.AddComponent ( paternEntity, new Blocks.Pattern.RequestPatternSetupTag () ) ; */ //patternGroup.f_baseScale = 3 ; //movePatternData.a_compositePatternComponent [i] = patternGroup ; // } } // for }