public async Task <List <GRingSurfaceDetail> > ProvideSurfaceDetail() { var stainResourceWithCoords = await _stainTerrainServiceProxy.RetriveResource(_inGamePosition); var uniformsPack = new UniformsPack(); uniformsPack.SetTexture("_PaletteTex", stainResourceWithCoords.Resource.TerrainPaletteTexture); uniformsPack.SetTexture("_PaletteIndexTex", stainResourceWithCoords.Resource.PaletteIndexTexture); uniformsPack.SetTexture("_ControlTex", stainResourceWithCoords.Resource.ControlTexture); uniformsPack.SetUniform("_TerrainStainUv", stainResourceWithCoords.Coords.ToVector4()); uniformsPack.SetUniform("_TerrainTextureSize", stainResourceWithCoords.Resource.TerrainTextureSize); uniformsPack.SetUniform("_PaletteMaxIndex", stainResourceWithCoords.Resource.PaletteMaxIndex); return(new List <GRingSurfaceDetail>() { new GRingSurfaceDetail() { UniformsWithKeywords = new UniformsWithKeywords() { Keywords = new ShaderKeywordSet(), Uniforms = uniformsPack }, ShaderName = "Custom/Terrain/Ring1" } }); }
public Grass2Aspect GenerateAspect(UnplantedGrassInstance unplantedInstance, Vector2 flatPosition) { var rotationInRad = unplantedInstance.FlatRotationInRadians; MyTransformTriplet transformTriplet = new MyTransformTriplet( new Vector3(0, 0, 0), new Vector3(0, rotationInRad, 0), unplantedInstance.Size); UniformsPack uniforms = new UniformsPack(); uniforms.SetUniform("_InitialBendingValue", unplantedInstance.InitialBendingValue); uniforms.SetUniform("_PlantBendingStiffness", unplantedInstance.InitialBendingStiffness); uniforms.SetUniform("_PlantDirection", new Vector4(Mathf.Sin(rotationInRad), Mathf.Cos(rotationInRad), 0, 0)); //todo! uniforms.SetUniform("_Color", unplantedInstance.Color); return(new Grass2Aspect() { Entities = new List <Grass2Entity>() { new Grass2Entity() { DeltaTransformTriplet = transformTriplet, Uniforms = uniforms, FlatRotation = rotationInRad } }, FlatPos = flatPosition }); }
public override void OnInspectorGUI() { DrawDefaultInspector(); var terrainLoader = (TerrainLoaderGameObject)target; _seedValue = EditorGUILayout.Slider("Seed value", _seedValue, 0.0f, 10.0f, new GUILayoutOption[] {}); _heightMultiplier = EditorGUILayout.Slider("_heightMultiplier", _heightMultiplier, 0.0f, 10.0f, new GUILayoutOption[] {}); var uniformsPack = new UniformsPack(); uniformsPack.SetUniform("_Seed", _seedValue); uniformsPack.SetUniform("_HeightMultiplier", _heightMultiplier); if (GUILayout.Button("RegenerateTextureObject")) { terrainLoader.Ring1Tree.RegenerateTextureShowingObject(uniformsPack); } if (GUILayout.Button("RegenerateTerrainObject")) { terrainLoader.Ring1Tree.RegenerateTerrainShowingObject(uniformsPack); } if (GUILayout.Button("UpdateLod")) { terrainLoader.UpdateHeightmap(); } }
public void Start(ComputeShaderContainerGameObject computeShaderContainer) { var commonExecutor = new CommonExecutorUTProxy(); var shaderExecutorObject = new UnityThreadComputeShaderExecutorObject(); var updater = new DesignBodySpotUpdater(new DesignBodySpotChangeCalculator(computeShaderContainer, shaderExecutorObject, commonExecutor, HeightDenormalizer.Identity)); _designBodySpotUpdaterProxy = new DesignBodySpotUpdaterProxy(updater); updater.SetChangesListener(new LambdaSpotPositionChangesListener(null, dict => { foreach (var pair in dict) { _grassGroupsPlanter.GrassGroupSpotChanged(pair.Key, pair.Value); } })); _designBodySpotUpdaterProxy.StartThreading(() => { }); var meshGenerator = new GrassMeshGenerator(); var mesh = meshGenerator.GetGrassBladeMesh(1); var instancingMaterial = new Material(Shader.Find("Custom/Vegetation/Grass.Instanced")); instancingMaterial.enableInstancing = true; var commonUniforms = new UniformsPack(); commonUniforms.SetUniform("_BendingStrength", 0.0f); commonUniforms.SetUniform("_WindDirection", Vector4.one); var instancingContainer = new GpuInstancingVegetationSubjectContainer( new GpuInstancerCommonData(mesh, instancingMaterial, commonUniforms), new GpuInstancingUniformsArrayTemplate(new List <GpuInstancingUniformTemplate>() { new GpuInstancingUniformTemplate("_Color", GpuInstancingUniformType.Vector4), new GpuInstancingUniformTemplate("_InitialBendingValue", GpuInstancingUniformType.Float), new GpuInstancingUniformTemplate("_PlantBendingStiffness", GpuInstancingUniformType.Float), new GpuInstancingUniformTemplate("_PlantDirection", GpuInstancingUniformType.Vector4), new GpuInstancingUniformTemplate("_RandSeed", GpuInstancingUniformType.Float), }) ); _globalGpuInstancingContainer = new GlobalGpuInstancingContainer(); var bucketId = _globalGpuInstancingContainer.CreateBucket(instancingContainer); GrassGroupsContainer grassGroupsContainer = new GrassGroupsContainer(_globalGpuInstancingContainer, bucketId); IGrassPositionResolver grassPositionResolver = new SimpleRandomSamplerPositionResolver(); GrassDetailInstancer grassDetailInstancer = new GrassDetailInstancer(); _grassGroupsPlanter = new GrassGroupsPlanter( grassDetailInstancer, grassPositionResolver, grassGroupsContainer, _designBodySpotUpdaterProxy, new LegacyGrass2BladeAspectsGenerator(), GrassDebugUtils.TemplatesDictionary, Repositioner.Identity); }
public GRingWeldingDetail ProvideWeldingDetails(TerrainDetailElementOutput heightmapDetail, int heightmapOffset) { if (!_weldingPack.WeldingEnabled) { return(new GRingWeldingDetail() { Modifier = new Ring1GroundPieceModifier(), UniformsPack = new UniformsPack() }); } if (_weldingPack == null) { Debug.LogError("WARNING. Welding pack not set!!. Only for testing!!"); return(new GRingWeldingDetail() { Modifier = new Ring1GroundPieceModifier(), UniformsPack = new UniformsPack() }); } var groundPieceModifier = new Ring1GroundPieceModifier(); _weldingTerrainIndex = _weldingPack.RegisterTerrain(new WeldingRegistrationTerrain() { Texture = heightmapDetail.TokenizedElement.DetailElement.Texture, DetailGlobalArea = heightmapDetail.TokenizedElement.DetailElement.DetailArea, Resolution = heightmapDetail.TokenizedElement.DetailElement.Resolution, TerrainLod = heightmapOffset, UvCoordsPositions2D = heightmapDetail.UvBase }, (t) => groundPieceModifier.ModifyMaterial((material) => { t.SetToMaterial(material); })); var pack = new UniformsPack(); pack.SetTexture("_WeldTexture", _weldingPack.WeldTexture.Texture); var emptyUvs = new TerrainWeldUvs(); pack.SetUniform("_LeftWeldTextureUvRange", emptyUvs.LeftUv); pack.SetUniform("_RightWeldTextureUvRange", emptyUvs.RightUv); pack.SetUniform("_TopWeldTextureUvRange", emptyUvs.TopUv); pack.SetUniform("_BottomWeldTextureUvRange", emptyUvs.BottomUv); return(new GRingWeldingDetail() { UniformsPack = pack, Modifier = groundPieceModifier, WeldingIndex = _weldingTerrainIndex }); }
public async Task <TextureWithSize> ExtractMipmapAsync(TextureWithSize inputTexture, RenderTextureFormat format, int mipmapLevelToExtract) { var pack = new UniformsPack(); pack.SetTexture("_InputTexture", inputTexture.Texture); pack.SetUniform("_MipmapLevelToExtract", mipmapLevelToExtract); var outSize = ComputeMipmappedOutSize(inputTexture.Size, mipmapLevelToExtract); var newTexture = await _textureRenderer.AddOrder(new TextureRenderingTemplate() { CanMultistep = true, Coords = new MyRectangle(0, 0, 1, 1), CreateTexture2D = false, OutTextureInfo = new ConventionalTextureInfo(outSize.X, outSize.Y, TextureFormat.ARGB32, true), RenderTextureFormat = format, RenderTextureMipMaps = true, ShaderName = "Custom/Tool/ExtractMipmap", UniformPack = pack, }); return(new TextureWithSize() { Texture = newTexture, Size = outSize }); }
private static UniformsPack CreatePackWithPointerUniform(EPropElevationPointer elevationId) { var uniformsPack = new UniformsPack(); uniformsPack.SetUniform("_Pointer", CastUtils.BitwiseCastUIntToFloat(elevationId.Value)); return(uniformsPack); }
public Grass2Aspect GenerateAspect(UnplantedGrassInstance unplantedInstance, Vector2 flatPosition) { var rotationInRad = unplantedInstance.FlatRotationInRadians; MyTransformTriplet transformTriplet = new MyTransformTriplet( new Vector3(0, 0, 0), new Vector3(Mathf.Deg2Rad * unplantedInstance.InitialBendingValue * 20, rotationInRad, 0), //TODO i use properties from Grass2 as inputs to EVegetation unplantedInstance.Size); UniformsPack uniforms = new UniformsPack(); uniforms.SetUniform("_Color", unplantedInstance.Color); return(new Grass2Aspect() { Entities = new List <Grass2Entity>() { new Grass2Entity() { DeltaTransformTriplet = transformTriplet, Uniforms = uniforms, FlatRotation = rotationInRad } }, FlatPos = flatPosition }); }
public async Task <TextureWithCoords> ApplyFeatureAsync(TextureWithCoords inputTexture, TerrainCardinalResolution resolution, bool CanMultistep = false) { if (!TaskUtils.GetGlobalMultithreading()) { Preconditions.Assert(inputTexture.Texture.width == inputTexture.Texture.height, "Only square inputTextures are supported"); } UniformsPack pack = new UniformsPack(); await _commonExecutor.AddAction(() => inputTexture.Texture.filterMode = FilterMode.Point); pack.SetTexture("_SourceTexture", inputTexture.Texture); pack.SetUniform("_InputGlobalCoords", inputTexture.Coords.ToVector4()); pack.SetUniform("_QuantingResolution", inputTexture.TextureSize.X - 1); var configuration = _configurations[resolution]; pack.SetUniform("_DetailResolutionMultiplier", configuration.DetailResolutionMultiplier); pack.SetUniform("_NoiseStrengthMultiplier", configuration.NoiseStrengthMultiplier); var renderCoords = new MyRectangle(0, 0, 1, 1); var outTextureSize = inputTexture.TextureSize; ConventionalTextureInfo outTextureInfo = new ConventionalTextureInfo(outTextureSize.X, outTextureSize.Y, TextureFormat.ARGB32, true); TextureRenderingTemplate template = new TextureRenderingTemplate() { CanMultistep = false, Coords = renderCoords, OutTextureInfo = outTextureInfo, RenderTextureFormat = RenderTextureFormat.RFloat, ShaderName = "Custom/TerrainCreation/NoiseAddingPlain", UniformPack = pack, CreateTexture2D = false }; return(new TextureWithCoords(sizedTexture: new TextureWithSize() { Texture = await _rendererProxy.AddOrder(template), Size = inputTexture.TextureSize }, coords: inputTexture.Coords)); }
private async Task <UniformsPack> CreateUniformsPack(Ring1Node ring1Node) { UniformsPack pack = new UniformsPack(); var terrainOutput = await _terrainShapeDb.Query(new TerrainDescriptionQuery() { QueryArea = Ring1Node.Ring1Position, RequestedElementDetails = new List <TerrainDescriptionQueryElementDetail>() { new TerrainDescriptionQueryElementDetail() { //PixelsPerMeter = 10, //todo Resolution = TerrainCardinalResolution.FromRing1NodeLodLevel(Ring1Node.QuadLodLevel), Type = TerrainDescriptionElementTypeEnum.HEIGHT_ARRAY }, new TerrainDescriptionQueryElementDetail() { //PixelsPerMeter = 10, //todo Resolution = TerrainCardinalResolution.FromRing1NodeLodLevel(Ring1Node.QuadLodLevel), Type = TerrainDescriptionElementTypeEnum.TESSALATION_REQ_ARRAY }, } }); var uvPosition = _coordsCalculator.CalculateUvPosition(Ring1Node.Ring1Position); //_submapTextures.UvPosition; var heightmapTexture = terrainOutput.GetElementOfType(TerrainDescriptionElementTypeEnum.HEIGHT_ARRAY); var tessalationTexture = terrainOutput.GetElementOfType(TerrainDescriptionElementTypeEnum.TESSALATION_REQ_ARRAY); pack.SetUniform("_TerrainTextureUvPositions", new Vector4(uvPosition.X, uvPosition.Y, uvPosition.Width, uvPosition.Height)); pack.SetTexture("_HeightmapTex", heightmapTexture.TokenizedElement.DetailElement.Texture.Texture); pack.SetTexture("_TessalationTex", tessalationTexture.TokenizedElement.DetailElement.Texture.Texture); pack.SetTexture("_LodTexture", _visibilityTexture); pack.SetUniform("_MaxHeight", 100); pack.SetUniform("_LodTextureUvOffset", _coordsCalculator.CalculateTextureUvLodOffset(ring1Node.Ring1Position)); pack.SetUniform("_BaseTrianglesCount", _coordsCalculator.CalculateGameObjectSize(ring1Node.Ring1Position).X - 1); return(pack); }
public Grass2Aspect GenerateAspect(UnplantedGrassInstance unplantedInstance, Vector2 flatPosition) { var entityRotationInRad = unplantedInstance.FlatRotationInRadians; var usedBillboardIndex = _billboardClan.QueryRandom((int)(entityRotationInRad * 543.21)); //todo seed var entities = new List <Grass2Entity>(); int billboardsCount = 3; for (int i = 0; i < billboardsCount; i++) { var additionalRotation = ((Mathf.PI) / (billboardsCount)) * i; var finalRotationInRad = additionalRotation + entityRotationInRad; MyTransformTriplet transformTriplet = new MyTransformTriplet( new Vector3(0, 0, 0), new Vector3(0, finalRotationInRad, 0), unplantedInstance.Size); UniformsPack uniforms = new UniformsPack(); uniforms.SetUniform("_InitialBendingValue", unplantedInstance.InitialBendingValue); uniforms.SetUniform("_PlantBendingStiffness", unplantedInstance.InitialBendingStiffness); uniforms.SetUniform("_PlantDirection", new Vector4(Mathf.Sin(finalRotationInRad), Mathf.Cos(finalRotationInRad), finalRotationInRad, 0)); //todo! uniforms.SetUniform("_Color", unplantedInstance.Color); uniforms.SetUniform("_ArrayTextureIndex", usedBillboardIndex); var newEntity = new Grass2Entity() { DeltaTransformTriplet = transformTriplet, Uniforms = uniforms, FlatRotation = finalRotationInRad }; entities.Add(newEntity); } return(new Grass2Aspect() { Entities = entities, FlatPos = flatPosition }); }
private async Task <GRingTerrainId> CreateTerrainAsync() { var triplet = _tripletProvider.ProvideTriplet(); var mesh = await _meshGeneratorUtProxy.AddOrder(() => PlaneGenerator.CreateFlatPlaneMesh(65, 65)); var creationTemplatesList = new List <Ring1GroundPieceCreationTemplate>(); int layerIndex = 0; UniformsPack pack = new UniformsPack(); pack.SetUniform("_LodLevel", _flatLod.ScalarValue); pack.SetUniform("_NodeId", LastId); var groundShapeDetails = await _groundShapeProvider.ProvideGroundTextureDetail(); _groundShapeToken = groundShapeDetails.GroundShapeToken; pack.MergeWith(groundShapeDetails.Uniforms); creationTemplatesList.Add( new Ring1GroundPieceCreationTemplate() { Name = $"TerrainElementLayer l:{layerIndex} fl:{_flatLod.ScalarValue}, X:{LastId++}", ParentGameObject = _parentObject, PieceMesh = mesh, TransformTriplet = triplet, ShaderName = "Custom/Terrain/DebugTerrainedLod", //ShaderName = "Custom/Terrain/Ring0", ShaderKeywordSet = groundShapeDetails.ShaderKeywordSet, Uniforms = pack, Modifier = new Ring1GroundPieceModifier() }); var toReturn = new GRingTerrainId() { ElementsIds = creationTemplatesList.Select(c => _orderGrabber.AddCreationOrder(c)).ToList() }; return(toReturn); }
public async Task <Texture2D> GenerateBillboardImageAsync(int bladesCount, float seed) { UniformsPack uniforms = new UniformsPack(); uniforms.SetUniform("_Seed", seed); uniforms.SetUniform("_BladesCount", bladesCount); return((await _textureRenderer.AddOrder(new TextureRenderingTemplate() { CanMultistep = false, Coords = new MyRectangle(0, 0, 1, 1), CreateTexture2D = true, OutTextureInfo = new ConventionalTextureInfo(_configuration.BillboardSize.X, _configuration.BillboardSize.Y, TextureFormat.ARGB32, true), Keywords = new ShaderKeywordSet(), RenderTextureFormat = RenderTextureFormat.ARGB32, RenderTextureMipMaps = false, ShaderName = "Custom/Precomputation/GrassBushBillboardGenerator", UniformPack = uniforms })) as Texture2D); }
public Task PlaceSegmentAsync(Texture segmentTexture, PlacementDetails placementDetails) { var segmentPlacement0 = CalculateSegmentPlacement(placementDetails.ModuledPositionInGrid); UniformsPack uniforms = new UniformsPack(); uniforms.SetTexture("_SegmentSurfaceTexture", segmentTexture); uniforms.SetUniform("_SegmentCoords", segmentPlacement0.Uvs.ToVector4()); uniforms.SetUniform("_TextureArraySliceIndex", _ceilArraySliceIndex); return(_renderer.AddOrder(new TextureRenderingTemplate() { CanMultistep = false, CreateTexture2D = false, RenderTextureToModify = _floorTextureArray, ShaderName = "Custom/ESurface/SegmentPlacer", UniformPack = uniforms, RenderingRectangle = segmentPlacement0.Pixels, RenderTargetSize = _floorTextureSize, RenderTextureArraySlice = _ceilArraySliceIndex })); }
public async Task <TextureWithCoords> ApplyFeatureAsync(TextureWithCoords texture, TerrainCardinalResolution resolution, bool canMultistep) { var configuration = _configurations[resolution]; var detailedHeightmapArray = await TaskUtils .RunInThreadPool( () => { var creator = new DiamondSquareCreator(_randomProviderGenerator.GetRandom()); var initialArray = creator.CreateDiamondSquareNoiseArray( texture.TextureSize, configuration.DiamondSquareWorkingArrayLength); return(initialArray); }); var detailedTexture = await _commonExecutor .AddAction(() => HeightmapUtils.CreateTextureFromHeightmap(detailedHeightmapArray)); UniformsPack pack = new UniformsPack(); pack.SetTexture("_Texture1", texture.Texture); pack.SetTexture("_Texture2", detailedTexture); pack.SetUniform("_Texture2Weight", configuration.DiamondSquareWeight); var renderCoords = new MyRectangle(0, 0, 1, 1); var outTextureSize = texture.TextureSize; ConventionalTextureInfo outTextureInfo = new ConventionalTextureInfo(outTextureSize.X, outTextureSize.Y, TextureFormat.ARGB32, true); TextureRenderingTemplate template = new TextureRenderingTemplate() { CanMultistep = canMultistep, Coords = renderCoords, OutTextureInfo = outTextureInfo, RenderTextureFormat = RenderTextureFormat.RFloat, ShaderName = "Custom/TerrainCreation/DiamondSquareTextureAddingPlain", UniformPack = pack, CreateTexture2D = false }; var renderedTexture = await _rendererProxy.AddOrder(template); await _commonExecutor.AddAction(() => GameObject.Destroy(detailedTexture)); return(new TextureWithCoords(sizedTexture: new TextureWithSize() { Texture = renderedTexture, Size = texture.TextureSize }, coords: texture.Coords)); }
public IntensityFieldFigure Generate(RandomFieldNature nature, float seed, MyRectangle segmentCoords) //todo use nature { UniformsPack uniforms = new UniformsPack(); uniforms.SetUniform("_Seed", seed); uniforms.SetUniform("_Coords", new Vector4(segmentCoords.X, segmentCoords.Y, segmentCoords.Width, segmentCoords.Height)); Vector2 imageSize = new Vector2( (int)Mathf.Round(_configuration.PixelsPerUnit.x * segmentCoords.Width), (int)Mathf.Round(_configuration.PixelsPerUnit.y * segmentCoords.Height) ); RenderTextureInfo renderTextureInfo = new RenderTextureInfo( (int)imageSize.x, (int)imageSize.y, RenderTextureFormat.ARGB32 ); ConventionalTextureInfo outTextureInfo = new ConventionalTextureInfo( (int)imageSize.x, (int)imageSize.y, TextureFormat.ARGB32 ); Texture2D outTexture = _textureRenderer.RenderTexture(_shaderName, _inputBlankTexture, uniforms, renderTextureInfo, outTextureInfo); var toReturn = CopyToRandomFieldFigure(outTexture); if (DebugLastGeneratedTexture != null) { GameObject.Destroy(DebugLastGeneratedTexture); } DebugLastGeneratedTexture = outTexture; //GameObject.Destroy(outTexture); return(toReturn); }
private async Task <UniformsPack> CreateUniformsPack( List <TerrainDescriptionQueryElementDetail> queryElementDetails, UniformsPack uniformsPack, StainTerrainResourceWithCoords stainResourceWithCoords) { var terrainDetailAreaPosition = _coordsCalculator.CalculateGlobalObjectPosition(Ring1Node.Ring1Position); var terrainOutput = await _terrainShapeDb.Query(new TerrainDescriptionQuery() { QueryArea = terrainDetailAreaPosition, RequestedElementDetails = queryElementDetails }); var heightmapTexture = terrainOutput.GetElementOfType(TerrainDescriptionElementTypeEnum.HEIGHT_ARRAY); uniformsPack.SetTexture("_HeightmapTex", heightmapTexture.TokenizedElement.DetailElement.Texture.Texture); uniformsPack.SetUniform("_HeightmapUv", heightmapTexture.UvBase.ToVector4()); if (terrainOutput.HasElementOfType(TerrainDescriptionElementTypeEnum.NORMAL_ARRAY)) { var normalAsTexture = terrainOutput.GetElementOfType(TerrainDescriptionElementTypeEnum.NORMAL_ARRAY); uniformsPack.SetTexture("_NormalmapTex", normalAsTexture.TokenizedElement.DetailElement.Texture.Texture); uniformsPack.SetUniform("_NormalmapUv", normalAsTexture.UvBase.ToVector4()); } uniformsPack.SetTexture("_PaletteTex", stainResourceWithCoords.Resource.TerrainPaletteTexture); uniformsPack.SetTexture("_PaletteIndexTex", stainResourceWithCoords.Resource.PaletteIndexTexture); uniformsPack.SetTexture("_ControlTex", stainResourceWithCoords.Resource.ControlTexture); uniformsPack.SetUniform("_TerrainStainUv", stainResourceWithCoords.Coords.ToVector4()); uniformsPack.SetUniform("_TerrainTextureSize", stainResourceWithCoords.Resource.TerrainTextureSize); uniformsPack.SetUniform("_PaletteMaxIndex", stainResourceWithCoords.Resource.PaletteMaxIndex); return(uniformsPack); }
public async Task <GRingMeshDetail> ProvideMeshDetailsAsync() { var mesh = await _meshGenerator.AddOrder( () => PlaneGenerator.CreateFlatPlaneMesh(_gameObjectMeshResolution.X, _gameObjectMeshResolution.Y)); var pack = new UniformsPack(); pack.SetUniform("_HeightmapLodOffset", _heightmapLodOffset); return(new GRingMeshDetail() { Mesh = mesh, Uniforms = pack, HeightmapLod = _heightmapLodOffset }); }
public DebugFlatTerrainShapeDb(UTTextureRendererProxy textureRenderer) { var size = new IntVector2(241, 241); var tex = new Texture2D(size.X, size.Y, TextureFormat.ARGB32, true, true); for (int x = 0; x < size.X; x++) { for (int y = 0; y < size.Y; y++) { tex.SetPixel(x, y, new Color(0, 0, 0, 0)); } } tex.Apply(); _blankTexture = new TextureWithSize() { Texture = tex, Size = size }; var pack = new UniformsPack(); pack.SetTexture("_HeightmapTex", tex); pack.SetUniform("_HeightMultiplier", 80); ConventionalTextureInfo outTextureInfo = new ConventionalTextureInfo(size.X, size.Y, TextureFormat.ARGB32, true); var renderCoords = new MyRectangle(0, 0, 1, 1); TextureRenderingTemplate template = new TextureRenderingTemplate() { CanMultistep = false, Coords = renderCoords, OutTextureInfo = outTextureInfo, RenderTextureFormat = RenderTextureFormat.ARGB32, ShaderName = "Custom/Terrain/NormalmapGenerator", UniformPack = pack, CreateTexture2D = false }; var outNormalTex = textureRenderer.AddOrder(template).Result; _normalTexture = new TextureWithSize() { Size = size, Texture = outNormalTex }; }
public Texture2D WrapTexture(Texture2D inputTexture) { UniformsPack uniforms = new UniformsPack(); uniforms.SetUniform("_Margin", _configuration.Margin); uniforms.SetTexture("_InputTex", inputTexture); var outSize = (new Vector2(inputTexture.width, inputTexture.height) / (1 + 2 * _configuration.Margin)).ToIntVector2(); outSize = new IntVector2(Mathf.ClosestPowerOfTwo(outSize.X), Mathf.ClosestPowerOfTwo(outSize.Y)); return((Texture2D)_textureRenderer.AddOrder(new TextureRenderingTemplate() { CanMultistep = false, Coords = new MyRectangle(0, 0, 1, 1), CreateTexture2D = true, OutTextureInfo = new ConventionalTextureInfo(outSize.X, outSize.Y, inputTexture.format, false), UniformPack = uniforms, ShaderName = "Custom/NPR/ArtMapWrapping" }).Result); //todo async }
public async Task <TextureWithSize> GenerateNormalDetailElementAsync(MyRectangle requestedArea, TerrainCardinalResolution resolution, RequiredCornersMergeStatus cornersMergeStatus) { var baseHeightTextureOutput = await _baseTerrainDetailProvider.RetriveTerrainDetailAsync( TerrainDescriptionElementTypeEnum.HEIGHT_ARRAY, requestedArea, resolution, cornersMergeStatus); var baseHeightTexture = baseHeightTextureOutput.TokenizedElement.DetailElement; IntVector2 outTextureSize = TerrainShapeUtils.RetriveTextureSize(requestedArea, resolution); UniformsPack pack = new UniformsPack(); float heightMultiplier = 0; if (resolution == TerrainCardinalResolution.MIN_RESOLUTION) { heightMultiplier = 1.25f; } else if (resolution == TerrainCardinalResolution.MID_RESOLUTION) { heightMultiplier = 10f; } else { heightMultiplier = 80f; } pack.SetTexture("_HeightmapTex", baseHeightTexture.Texture.Texture); //pack.SetUniform("_HeightMultiplier", heightMultiplier); pack.SetUniform("_HeightMultiplier", 1); pack.SetUniform("_GlobalCoords", requestedArea.ToVector4()); ConventionalTextureInfo outTextureInfo = new ConventionalTextureInfo(outTextureSize.X, outTextureSize.Y, TextureFormat.ARGB32, true); var renderCoords = TerrainShapeUtils.ComputeUvOfSubElement(requestedArea, baseHeightTexture.DetailArea); TextureRenderingTemplate template = new TextureRenderingTemplate() { CanMultistep = false, Coords = renderCoords, OutTextureInfo = outTextureInfo, RenderTextureFormat = RenderTextureFormat.ARGB32, ShaderName = "Custom/Terrain/NormalmapGenerator", UniformPack = pack, CreateTexture2D = false }; var outTex = await _rendererProxy.AddOrder(template); await _commonExecutor.AddAction(() => outTex.filterMode = FilterMode.Trilinear); await _baseTerrainDetailProvider.RemoveTerrainDetailAsync(baseHeightTextureOutput.TokenizedElement.Token); //SavingFileManager.SaveTextureToPngFile(@"C:\temp\norm1.png", outTex as Texture2D); return(new TextureWithSize() { Size = new IntVector2(outTextureSize.X, outTextureSize.Y), Texture = outTex }); }
private GroundPieceCreationDetails CalculateQueryDetails(int quadLodLevel) { TerrainCardinalResolution detailResolution = TerrainCardinalResolution.MIN_RESOLUTION; int meshLodPower = 0; bool directNormalCalculation = false; if (quadLodLevel == 1) { detailResolution = TerrainCardinalResolution.MIN_RESOLUTION; meshLodPower = 3; } else if (quadLodLevel == 2) { detailResolution = TerrainCardinalResolution.MIN_RESOLUTION; meshLodPower = 3; } else if (quadLodLevel == 3) { detailResolution = TerrainCardinalResolution.MIN_RESOLUTION; meshLodPower = 2; } else if (quadLodLevel == 4) { detailResolution = TerrainCardinalResolution.MID_RESOLUTION; meshLodPower = 2; } else if (quadLodLevel == 5) { detailResolution = TerrainCardinalResolution.MID_RESOLUTION; meshLodPower = 1; } else if (quadLodLevel == 6) { detailResolution = TerrainCardinalResolution.MID_RESOLUTION; meshLodPower = 0; directNormalCalculation = true; } else if (quadLodLevel == 7) { detailResolution = TerrainCardinalResolution.MAX_RESOLUTION; meshLodPower = 0; directNormalCalculation = true; } //else if (lodLevel == 8) //{ // detailResolution = TerrainCardinalResolution.MAX_RESOLUTION; // meshLodPower = 0; //} else { Preconditions.Fail("Unsupported lod level: " + quadLodLevel); } int baseMeshResolution = 240; int finalMeshResolution = Mathf.RoundToInt(baseMeshResolution / Mathf.Pow(2, meshLodPower)); IntVector2 terrainMeshResolution = new IntVector2(finalMeshResolution + 1, finalMeshResolution + 1); var queryElementDetails = new List <TerrainDescriptionQueryElementDetail>() { new TerrainDescriptionQueryElementDetail() { Resolution = detailResolution, Type = TerrainDescriptionElementTypeEnum.HEIGHT_ARRAY }, }; if (!directNormalCalculation) { queryElementDetails.Add( new TerrainDescriptionQueryElementDetail() { Resolution = detailResolution, Type = TerrainDescriptionElementTypeEnum.NORMAL_ARRAY } ); } var pack = new UniformsPack(); pack.SetUniform("_LodLevel", quadLodLevel); pack.SetUniform("_HeightmapLodOffset", meshLodPower); List <string> keywords = new List <string>(); if (directNormalCalculation) { keywords.Add("DYNAMIC_NORMAL_GENERATION"); } return(new GroundPieceCreationDetails() { KeywordSet = new ShaderKeywordSet(keywords), QueryElementDetails = queryElementDetails, Pack = pack, TerrainMeshResolution = terrainMeshResolution }); }
private GrassGroupsPlanter CreateBushGroupsPlanter(OtherThreadExecutingLocation otherThreadExecutingLocation) { var meshGenerator = new GrassMeshGenerator(); var mesh = meshGenerator.GetGrassBillboardMesh(0, 1); var instancingMaterial = new Material(Shader.Find("Custom/Vegetation/GrassBushBillboard.Instanced")); instancingMaterial.enableInstancing = true; /// CLAN var billboardsFileManger = new Grass2BillboardClanFilesManager(); var clan = billboardsFileManger.Load(_veConfiguration.Grass2BillboardsPath, new IntVector2(256, 256)); var singleToDuo = new Grass2BakingBillboardClanGenerator( _initializationFields.Retrive <ComputeShaderContainerGameObject>(), _initializationFields.Retrive <UnityThreadComputeShaderExecutorObject>()); var bakedClan = singleToDuo.GenerateBakedAsync(clan).Result; /// var commonUniforms = new UniformsPack(); commonUniforms.SetUniform("_BendingStrength", 0.6f); commonUniforms.SetUniform("_WindDirection", Vector4.one); commonUniforms.SetTexture("_DetailTex", bakedClan.DetailTextureArray); commonUniforms.SetTexture("_BladeSeedTex", bakedClan.BladeSeedTextureArray); var instancingContainer = new GpuInstancingVegetationSubjectContainer( new GpuInstancerCommonData(mesh, instancingMaterial, commonUniforms), new GpuInstancingUniformsArrayTemplate(new List <GpuInstancingUniformTemplate>() { new GpuInstancingUniformTemplate("_Color", GpuInstancingUniformType.Vector4), new GpuInstancingUniformTemplate("_InitialBendingValue", GpuInstancingUniformType.Float), new GpuInstancingUniformTemplate("_PlantBendingStiffness", GpuInstancingUniformType.Float), new GpuInstancingUniformTemplate("_PlantDirection", GpuInstancingUniformType.Vector4), new GpuInstancingUniformTemplate("_RandSeed", GpuInstancingUniformType.Float), new GpuInstancingUniformTemplate("_ArrayTextureIndex", GpuInstancingUniformType.Float), new GpuInstancingUniformTemplate("_Pointer", GpuInstancingUniformType.Float), }) ); var globalGpuInstancingContainer = _initializationFields.Retrive <GlobalGpuInstancingContainer>(); var bucketId = globalGpuInstancingContainer.CreateBucket(instancingContainer); GrassGroupsContainer grassGroupsContainer = new GrassGroupsContainer(globalGpuInstancingContainer, bucketId); IGrassPositionResolver grassPositionResolver = new PoissonDiskSamplerPositionResolver(_veConfiguration.BushExclusionRadiusRange); GrassDetailInstancer grassDetailInstancer = new GrassDetailInstancer(); DesignBodySpotUpdaterProxy designBodySpotUpdaterProxy = _initializationFields.Retrive <DesignBodySpotUpdaterProxy>(); var mediatorSpotUpdater = new ListenerCenteredMediatorDesignBodyChangesUpdater(designBodySpotUpdaterProxy); var grassGroupsPlanter = new GrassGroupsPlanter( grassDetailInstancer, grassPositionResolver, grassGroupsContainer, mediatorSpotUpdater, new Grass2BushAspectsGenerator(bakedClan), //todo! _veConfiguration.BushTemplatesConfiguration, _veConfiguration.VegetationRepositioner); mediatorSpotUpdater.SetTargetChangesListener(new LambdaSpotPositionChangesListener(null, dict => { otherThreadExecutingLocation.Execute(() => { foreach (var pair in dict) { grassGroupsPlanter.GrassGroupSpotChanged(pair.Key, pair.Value); } return(TaskUtils.EmptyCompleted()); }); })); var rootMediator = _initializationFields.Retrive <RootMediatorSpotPositionsUpdater>(); rootMediator.AddListener(mediatorSpotUpdater); return(grassGroupsPlanter); }
public async Task PlaceSegmentAsync(Texture segmentTexture, PlacementDetails placementDetails) { var segmentPlacement0 = CalculateSegmentPlacement(placementDetails.ModuledPositionInGrid); UniformsPack uniforms0 = new UniformsPack(); uniforms0.SetTexture("_SegmentHeightTexture", segmentTexture); uniforms0.SetUniform("_SegmentCoords", segmentPlacement0.Uvs.ToVector4()); await _renderer.AddOrder(new TextureRenderingTemplate() { CanMultistep = false, CreateTexture2D = false, RenderTextureToModify = _heightMap, ShaderName = "Custom/ETerrain/SegmentPlacer", UniformPack = uniforms0, RenderingRectangle = segmentPlacement0.Pixels, RenderTargetSize = _floorTextureSize, RenderTextureArraySlice = _heightMapSliceIndex }); if (_modifyCorners) { var modifiedCornerBuffer = await _commonExecutor.AddAction(() => { var tex = new RenderTexture(_modifiedCornerBufferSize.X, _modifiedCornerBufferSize.Y, 0, _heightMap.format); tex.Create(); return(tex); }); foreach (var cornerModification in placementDetails.CornersToModify) { var cornerMask = _cornerMaskInformations[cornerModification.Corner]; var segmentPlacement1 = CalculateSegmentPlacement(cornerModification.ModuledPositionOfSegment); var uniforms1 = new UniformsPack(); uniforms1.SetTexture("_HeightMap", _heightMap); uniforms1.SetUniform("_WeldingAreaCoords", RectangleUtils.CalculateSubPosition(segmentPlacement1.Uvs, cornerMask.SegmentSubPositionUv) .ToVector4()); uniforms1.SetUniform("_MarginSize", _interSegmentMarginSize); uniforms1.SetUniform("_CornerToWeld", cornerMask.CornerToWeldVector); uniforms1.SetUniform("_PixelSizeInUv", 1f / _floorTextureSize.X); uniforms1.SetUniform("_HeightMapSliceIndex", _heightMapSliceIndex); await _renderer.AddOrder(new TextureRenderingTemplate() { CanMultistep = false, CreateTexture2D = false, RenderTextureToModify = modifiedCornerBuffer, ShaderName = "Custom/ETerrain/GenerateNewCorner", UniformPack = uniforms1, RenderingRectangle = new IntRectangle(0, 0, segmentPlacement1.Pixels.Width, segmentPlacement1.Pixels.Height), RenderTargetSize = new IntVector2(segmentPlacement1.Pixels.Width, segmentPlacement1.Pixels.Height) }); var uniforms2 = new UniformsPack(); uniforms2.SetTexture("_ModifiedCornerBuffer", modifiedCornerBuffer); await _renderer.AddOrder(new TextureRenderingTemplate() { CanMultistep = false, CreateTexture2D = false, RenderTextureToModify = _heightMap, ShaderName = "Custom/ETerrain/CornerPlacer", UniformPack = uniforms2, RenderingRectangle = RectangleUtils.CalculateSubPosition(segmentPlacement1.Pixels.ToFloatRectangle(), cornerMask.SegmentSubPositionUv).ToIntRectange(), RenderTargetSize = _floorTextureSize, RenderTextureArraySlice = _heightMapSliceIndex }); } await _commonExecutor.AddAction(() => GameObject.Destroy(modifiedCornerBuffer)); } }
private async Task MergeDetailObject(Dictionary <TerrainDetailCorner, TerrainDetailElement> sourceTextures, TextureWithSize sourceTexture, TextureWithSize outTexture, TerrainDetailCorner activeCorner, TextureWithSize scratchTexture) { var uniforms = new UniformsPack(); var cornersPresent = new Vector4(); var cornersMerged = new Vector4(); int activeCornerIndex = 0; var i = 0; foreach (var corner in TerrainDetailCorner.OrderedDirections) { if (corner == activeCorner) { activeCornerIndex = i; } if (sourceTextures.ContainsKey(corner)) { cornersPresent[i] = 10; if (sourceTextures[corner].CornersMergeStatus == CornersMergeStatus.MERGED) { cornersMerged[i] = 10; } uniforms.SetTexture("_Corner" + GetCornerTexUniformName(corner) + "Tex", sourceTextures[corner].Texture.Texture); } i++; } uniforms.SetUniform("_ActiveCornerIndex", activeCornerIndex); uniforms.SetUniform("_CornersMerged", cornersMerged); uniforms.SetTexture("_ScratchTex", scratchTexture.Texture); uniforms.SetUniform("_MergeMargin", _configuration.MergeMarginSize); // todo await _renderer.AddOrder(new TextureRenderingTemplate() { CanMultistep = false, CreateTexture2D = false, RenderTextureToModify = scratchTexture.Texture as RenderTexture, // todo ShaderName = "Custom/TerrainDetailMerger/MergeIntoScratch", UniformPack = uniforms, RenderingRectangle = new IntRectangle(0, 0, scratchTexture.Size.X, scratchTexture.Size.Y), RenderTargetSize = new IntVector2(scratchTexture.Size.X, scratchTexture.Size.Y), }); IntRectangle renderingRectangle; if (activeCorner == TerrainDetailCorner.BottomLeft) { renderingRectangle = new IntRectangle(0, 0, 121, 121); } else if (activeCorner == TerrainDetailCorner.BottomRight) { renderingRectangle = new IntRectangle(120, 0, 121, 121); } else if (activeCorner == TerrainDetailCorner.TopLeft) { renderingRectangle = new IntRectangle(0, 120, 121, 121); } else if (activeCorner == TerrainDetailCorner.TopRight) { renderingRectangle = new IntRectangle(120, 120, 121, 121); } else { Preconditions.Fail("Unsupported activeCorner " + activeCorner); renderingRectangle = new IntRectangle(0, 0, 1, 1); } await _renderer.AddOrder(new TextureRenderingTemplate() { CanMultistep = false, CreateTexture2D = false, RenderTextureToModify = (RenderTexture)outTexture.Texture, ShaderName = "Custom/TerrainDetailMerger/ScratchToActive", UniformPack = uniforms, RenderingRectangle = renderingRectangle, RenderTargetSize = new IntVector2(241, 241), }); }
public async Task <GRingGroundShapeDetail> ProvideGroundTextureDetail() { int flatLodLevel = _flatLod.ScalarValue; if (!_configuration.SettingPerFlatLod.ContainsKey(flatLodLevel)) { Preconditions.Fail("Unsupported lod level: " + flatLodLevel); } var setting = _configuration.SettingPerFlatLod[flatLodLevel]; TerrainCardinalResolution detailResolution = setting.DetailResolution; bool directNormalCalculation = setting.DirectNormalComputation; var queryElementDetails = new List <TerrainDescriptionQueryElementDetail>() { new TerrainDescriptionQueryElementDetail() { Resolution = detailResolution, Type = TerrainDescriptionElementTypeEnum.HEIGHT_ARRAY }, }; //if (!directNormalCalculation) //we have to do it every time , as we need normals for spot updating! { queryElementDetails.Add( new TerrainDescriptionQueryElementDetail() { Resolution = detailResolution, Type = TerrainDescriptionElementTypeEnum.NORMAL_ARRAY } ); } var uniformsPack = new UniformsPack(); uniformsPack.SetUniform("_LodLevel", flatLodLevel); var terrainOutput = await _terrainShapeDb.Query(new TerrainDescriptionQuery() { QueryArea = _terrainDetailAreaPosition, RequestedElementDetails = queryElementDetails }); var heightmapTexture = terrainOutput.GetElementOfType(TerrainDescriptionElementTypeEnum.HEIGHT_ARRAY); uniformsPack.SetTexture("_HeightmapTex", heightmapTexture.TokenizedElement.DetailElement.Texture.Texture); uniformsPack.SetUniform("_HeightmapUv", heightmapTexture.UvBase.ToVector4()); var normalAsTexture = terrainOutput.GetElementOfType(TerrainDescriptionElementTypeEnum.NORMAL_ARRAY); if (!directNormalCalculation) { uniformsPack.SetTexture("_NormalmapTex", normalAsTexture.TokenizedElement.DetailElement.Texture.Texture); uniformsPack.SetUniform("_NormalmapUv", normalAsTexture.UvBase.ToVector4()); } List <string> keywords = new List <string>(); if (directNormalCalculation) { keywords.Add("DYNAMIC_NORMAL_GENERATION"); } IGroundShapeToken token = null; if (_spotUpdater != null) //todo remove when tests over! { token = _spotUpdater.AddArea(new GroundShapeInfo() { TextureGlobalPosition = _terrainDetailAreaPosition, TextureCoords = new MyRectangle(heightmapTexture.UvBase), HeightmapResolution = detailResolution, }, new UpdatedTerrainTextures() { HeightTexture = heightmapTexture.TokenizedElement.DetailElement.Texture.Texture, NormalTexture = normalAsTexture.TokenizedElement.DetailElement.Texture.Texture, TextureGlobalPosition = heightmapTexture.TokenizedElement.DetailElement.DetailArea, TextureCoords = new MyRectangle(heightmapTexture.UvBase), }); } else { Debug.Log("T333 WARNING. SPOT UPDATER NOT SET. SHOUDL BE USED ONLY IN TESTING"); token = new DummyGroundShapeToken(); } return(new GRingGroundShapeDetail() { ShaderKeywordSet = new ShaderKeywordSet(keywords), Uniforms = uniformsPack, GroundShapeToken = token, HeightDetailOutput = heightmapTexture }); }
public DebugSlopedTerrainShapeDb(UTTextureRendererProxy textureRenderer) { var size = new IntVector2(241, 241); var tex = new Texture2D(size.X, size.Y, TextureFormat.ARGB32, true, true); var encodedHeightTex = new Texture2D(size.X, size.Y, TextureFormat.ARGB32, true, true); for (int x = 0; x < size.X; x++) { for (int y = 0; y < size.Y; y++) { tex.SetPixel(x, y, new Color(0, 0, 0, 0)); //var distanceToCenter = 1 - (Vector2.Distance(new Vector2(120, 120), new Vector2(x, y)) / // Mathf.Sqrt(120 * 120)) / 2; //encodedHeightTex.SetPixel(x, y, HeightColorTransform.EncodeHeight(distanceToCenter / 100)); //var distanceToCenter = Mathf.Clamp01( // Mathf.Min( // Mathf.Abs(y - 100) / 50.0f, // Mathf.Abs(x - 100) / 50.0f) // ) / 300; //encodedHeightTex.SetPixel(x, y, HeightColorTransform.EncodeHeight(distanceToCenter)); var heightInUnits = HeightDenormalizer.Default.Normalize(5); var encodedHeight = HeightColorTransform.EncodeHeight(heightInUnits); encodedHeightTex.SetPixel(x, y, encodedHeight); } } tex.Apply(); encodedHeightTex.Apply(); _blankTexture = new TextureWithSize() { Texture = tex, Size = size }; var transformator = new TerrainTextureFormatTransformator(new CommonExecutorUTProxy()); var encodedHeightTexture = new TextureWithSize() { Texture = encodedHeightTex, Size = new IntVector2(241, 241) }; var plainTex = transformator.EncodedHeightTextureToPlain(encodedHeightTexture); _heightTexture = new TextureWithSize() { Size = new IntVector2(241, 241), Texture = plainTex }; var pack = new UniformsPack(); pack.SetTexture("_HeightmapTex", plainTex); pack.SetUniform("_HeightMultiplier", 80); ConventionalTextureInfo outTextureInfo = new ConventionalTextureInfo(size.X, size.Y, TextureFormat.ARGB32, true); var renderCoords = new MyRectangle(0, 0, 1, 1); TextureRenderingTemplate template = new TextureRenderingTemplate() { CanMultistep = false, Coords = renderCoords, OutTextureInfo = outTextureInfo, RenderTextureFormat = RenderTextureFormat.ARGB32, ShaderName = "Custom/Terrain/NormalmapGenerator", UniformPack = pack, CreateTexture2D = false }; var outNormalTex = textureRenderer.AddOrder(template).Result; _normalTexture = new TextureWithSize() { Size = size, Texture = outNormalTex }; }
public void Start(ComputeShaderContainerGameObject computeShaderContainer) { var commonExecutor = new CommonExecutorUTProxy(); var shaderExecutorObject = new UnityThreadComputeShaderExecutorObject(); var updater = new DesignBodySpotUpdater(new DesignBodySpotChangeCalculator(computeShaderContainer, shaderExecutorObject, commonExecutor, HeightDenormalizer.Identity)); _designBodySpotUpdaterProxy = new DesignBodySpotUpdaterProxy(updater); updater.SetChangesListener(new LambdaSpotPositionChangesListener(null, dict => { foreach (var pair in dict) { _grassGroupsPlanter.GrassGroupSpotChanged(pair.Key, pair.Value); } })); _designBodySpotUpdaterProxy.StartThreading(() => { }); var meshGenerator = new GrassMeshGenerator(); var mesh = meshGenerator.GetGrassBillboardMesh(0, 1); var instancingMaterial = new Material(Shader.Find("Custom/Vegetation/GrassBushBillboard.Instanced")); instancingMaterial.enableInstancing = true; /// CLAN var billboardsFileManger = new Grass2BillboardClanFilesManager(); var clan = billboardsFileManger.Load(@"C:\inz\billboards\", new IntVector2(256, 256)); var singleToDuo = new Grass2BakingBillboardClanGenerator(computeShaderContainer, shaderExecutorObject); var bakedClan = singleToDuo.GenerateBakedAsync(clan).Result; /// var commonUniforms = new UniformsPack(); commonUniforms.SetUniform("_BendingStrength", 0.0f); commonUniforms.SetUniform("_WindDirection", Vector4.one); commonUniforms.SetTexture("_DetailTex", bakedClan.DetailTextureArray); commonUniforms.SetTexture("_BladeSeedTex", bakedClan.BladeSeedTextureArray); var instancingContainer = new GpuInstancingVegetationSubjectContainer( new GpuInstancerCommonData(mesh, instancingMaterial, commonUniforms), new GpuInstancingUniformsArrayTemplate(new List <GpuInstancingUniformTemplate>() { new GpuInstancingUniformTemplate("_Color", GpuInstancingUniformType.Vector4), new GpuInstancingUniformTemplate("_InitialBendingValue", GpuInstancingUniformType.Float), new GpuInstancingUniformTemplate("_PlantBendingStiffness", GpuInstancingUniformType.Float), new GpuInstancingUniformTemplate("_PlantDirection", GpuInstancingUniformType.Vector4), new GpuInstancingUniformTemplate("_RandSeed", GpuInstancingUniformType.Float), new GpuInstancingUniformTemplate("_ArrayTextureIndex", GpuInstancingUniformType.Float), }) ); _globalGpuInstancingContainer = new GlobalGpuInstancingContainer(); var bucketId = _globalGpuInstancingContainer.CreateBucket(instancingContainer); GrassGroupsContainer grassGroupsContainer = new GrassGroupsContainer(_globalGpuInstancingContainer, bucketId); IGrassPositionResolver grassPositionResolver = new PoissonDiskSamplerPositionResolver(new MyRange(1.5f * 0.4f * 10, 10 * 2 * 1.3f)); //IGrassPositionResolver grassPositionResolver = new SimpleRandomSamplerPositionResolver(); GrassDetailInstancer grassDetailInstancer = new GrassDetailInstancer(); _grassGroupsPlanter = new GrassGroupsPlanter( grassDetailInstancer, grassPositionResolver, grassGroupsContainer, _designBodySpotUpdaterProxy, new Grass2BushAspectsGenerator(bakedClan), //todo! GrassDebugUtils.BushTemplates, Repositioner.Identity); }
public void Start() { var meshGenerator = new GrassMeshGenerator(); var mesh = meshGenerator.GetGrassBladeMesh(1); var material = new Material(Shader.Find("Custom/Vegetation/Grass")); material.SetFloat("_BendingStrength", 0.2f); material.SetFloat("_InitialBendingValue", 0.4f); material.SetFloat("_PlantBendingStiffness", 0.3f); material.SetVector("_WindDirection", Vector4.one); material.SetVector("_PlantDirection", Vector4.one); material.SetColor("_Color", Color.green); material.SetFloat("_RandSeed", 44.4f); var go = GameObject.CreatePrimitive(PrimitiveType.Capsule); go.GetComponent <MeshFilter>().mesh = mesh; go.GetComponent <MeshRenderer>().material = material; var instancingMaterial = new Material(Shader.Find("Custom/Vegetation/Grass.Instanced")); instancingMaterial.enableInstancing = true; _globalGpuInstancingContainer = new GlobalGpuInstancingContainer(); var commonUniforms = new UniformsPack(); commonUniforms.SetUniform("_BendingStrength", 0.8f); commonUniforms.SetUniform("_WindDirection", Vector4.one); var instancingContainer = new GpuInstancingVegetationSubjectContainer( new GpuInstancerCommonData(mesh, instancingMaterial, commonUniforms), new GpuInstancingUniformsArrayTemplate(new List <GpuInstancingUniformTemplate>() { new GpuInstancingUniformTemplate("_Color", GpuInstancingUniformType.Vector4), new GpuInstancingUniformTemplate("_InitialBendingValue", GpuInstancingUniformType.Float), new GpuInstancingUniformTemplate("_PlantBendingStiffness", GpuInstancingUniformType.Float), new GpuInstancingUniformTemplate("_PlantDirection", GpuInstancingUniformType.Vector4), new GpuInstancingUniformTemplate("_RandSeed", GpuInstancingUniformType.Float), }) ); var bucketId = _globalGpuInstancingContainer.CreateBucket(instancingContainer); var uniforms = new UniformsPack(); uniforms.SetUniform("_InitialBendingValue", 0.4f); uniforms.SetUniform("_PlantBendingStiffness", 0.3f); uniforms.SetUniform("_PlantDirection", Vector4.one); uniforms.SetUniform("_Color", Color.green); uniforms.SetUniform("_RandSeed", 44.4f); var msw = new MyStopWatch(); msw.StartSegment("AddingClassic"); for (int x = 0; x < 300; x++) { for (int y = 0; y < 300; y++) { var myTriplet = new MyTransformTriplet(new Vector3(x, 0, y), Vector3.zero, Vector3.one); _globalGpuInstancingContainer.AddInstance(bucketId, myTriplet.ToLocalToWorldMatrix(), uniforms); } } _globalGpuInstancingContainer.StartThread(); _globalGpuInstancingContainer.FinishUpdateBatch(); Debug.Log("It took: " + msw.CollectResults()); }
private GrassGroupsPlanter CreateGrassGroupsPlanter(OtherThreadExecutingLocation otherThreadExecutingLocation) { var meshGenerator = new GrassMeshGenerator(); var mesh = meshGenerator.GetGrassBladeMesh(1); MeshGenerationUtils.SetYBoundsToInfinity(mesh); var instancingMaterial = _veConfiguration.ReferencedAssets.GrassMaterial; if (_materialCommonPack != null) { _materialCommonPack.SetToMaterial(instancingMaterial); } var commonUniforms = new UniformsPack(); commonUniforms.SetUniform("_BendingStrength", 0.6f); commonUniforms.SetUniform("_WindDirection", Vector4.one); var gpuInstancerCommonData = new GpuInstancerCommonData(mesh, instancingMaterial, commonUniforms); gpuInstancerCommonData.CastShadows = _veConfiguration.GrassCastShadows; var instancingContainer = new GpuInstancingVegetationSubjectContainer( gpuInstancerCommonData, new GpuInstancingUniformsArrayTemplate(new List <GpuInstancingUniformTemplate>() { new GpuInstancingUniformTemplate("_Color", GpuInstancingUniformType.Vector4), new GpuInstancingUniformTemplate("_InitialBendingValue", GpuInstancingUniformType.Float), new GpuInstancingUniformTemplate("_PlantBendingStiffness", GpuInstancingUniformType.Float), new GpuInstancingUniformTemplate("_PlantDirection", GpuInstancingUniformType.Vector4), new GpuInstancingUniformTemplate("_RandSeed", GpuInstancingUniformType.Float), new GpuInstancingUniformTemplate("_Pointer", GpuInstancingUniformType.Float), }) ); var globalGpuInstancingContainer = _initializationFields.Retrive <GlobalGpuInstancingContainer>(); var bucketId = globalGpuInstancingContainer.CreateBucket(instancingContainer); var grassGroupsContainer = new GrassGroupsContainer(globalGpuInstancingContainer, bucketId); var grassPositionResolver = new SimpleRandomSamplerPositionResolver(); var grassDetailInstancer = new GrassDetailInstancer(); var designBodySpotUpdaterProxy = _initializationFields.Retrive <DesignBodySpotUpdaterProxy>(); var mediatorSpotUpdater = new ListenerCenteredMediatorDesignBodyChangesUpdater(designBodySpotUpdaterProxy); IGrass2AspectsGenerator grassAspectGenerator; if (_veConfiguration.Mode == VegetationMode.Legacy) { grassAspectGenerator = new LegacyGrass2BladeAspectsGenerator(); } else { grassAspectGenerator = new EVegetationGrass2BladeAspectsGenerator(); } var grassGroupsPlanter = new GrassGroupsPlanter( grassDetailInstancer, grassPositionResolver, grassGroupsContainer, mediatorSpotUpdater, grassAspectGenerator, _veConfiguration.GrassTemplates, _veConfiguration.VegetationRepositioner); mediatorSpotUpdater.SetTargetChangesListener(new LambdaSpotPositionChangesListener(null, dict => { otherThreadExecutingLocation.Execute(() => { foreach (var pair in dict) { grassGroupsPlanter.GrassGroupSpotChanged(pair.Key, pair.Value); } return(TaskUtils.EmptyCompleted()); }); })); var rootMediator = _initializationFields.Retrive <RootMediatorSpotPositionsUpdater>(); rootMediator.AddListener(mediatorSpotUpdater); return(grassGroupsPlanter); }