コード例 #1
0
 private void OnEnable()
 {
     albedoRT = new RenderTexture(targetSize.x, targetSize.y, 0, RenderTextureFormat.ARGBFloat, RenderTextureReadWrite.Linear);
     normalRT = new RenderTexture(targetSize.x, targetSize.y, 0, RenderTextureFormat.RGFloat, RenderTextureReadWrite.Linear);
     smoRT    = new RenderTexture(albedoRT.descriptor);
     whiteTex = new Texture2D(1, 1, TextureFormat.ARGB32, false, true);
     whiteTex.SetPixel(0, 0, Color.white);
     whiteTex.Apply();
     normalTex = new Texture2D(1, 1, TextureFormat.ARGB32, false, true);
     normalTex.SetPixel(0, 0, new Color(0.5f, 0.5f, 1, 1));
     normalTex.Apply();
     albedoRT.Create();
     normalRT.Create();
     smoRT.Create();
     Graphics.SetRenderTarget(new RenderBuffer[] { albedoRT.colorBuffer, normalRT.colorBuffer, smoRT.colorBuffer }, albedoRT.depthBuffer);
     GL.Clear(true, true, new Color(0, 0, 0, 0));
     rand         = new Random((uint)System.Guid.NewGuid().GetHashCode());
     randomMatrix = new float4x4(rand.NextFloat4(), rand.NextFloat4(), rand.NextFloat4(), rand.NextFloat4());
     testBuffer   = new CommandBuffer();
 }
コード例 #2
0
 public void UpdateRender(CommandBuffer buffer)
 {
     buffer.SetRenderTarget(colorBuffers, albedoRT.depthBuffer);
     buffer.ClearRenderTarget(true, true, new Color(0, 0, 0, 0));
     foreach (var i in data.allSettings)
     {
         buffer.SetGlobalTexture("_BlendAlbedo", i.startAlbedo ? i.startAlbedo : whiteTex);
         buffer.SetGlobalTexture("_BlendNormal", i.startNormal ? i.startNormal : normalTex);
         buffer.SetGlobalTexture("_BlendSMO", i.startSMO ? i.startNormal : normalTex);
         buffer.SetGlobalTexture("_BlendMask", i.maskTex ? i.maskTex : whiteTex);
         buffer.SetGlobalVector("_BlendScaleOffset", i.scaleOffset);
         Random   rand       = new Random(i.voronoiRandomSeed);
         float4x4 randMatrix = float4x4(rand.NextFloat4(), rand.NextFloat4(), rand.NextFloat4(), rand.NextFloat4());
         buffer.SetGlobalMatrix("_RandomSeedMatrix", randMatrix);
         buffer.SetGlobalInt("_UseVoronoi", i.useVoronoiSample ? 1 : 0);
         buffer.DrawMesh(GraphicsUtility.mesh, Matrix4x4.identity, blendTexMat, 0, 0);
     }
     testMat.SetTexture("_MainTex", albedoRT);
     testMat.SetTexture("_BumpMap", normalRT);
     testMat.SetTexture("_SpecularMap", smoRT);
 }
コード例 #3
0
    private void InitializeEntity(ref Unity.Mathematics.Random random, EntityManager manager, Entity entity)
    {
        var       moves = manager.GetBuffer <DanceMove>(entity);
        DanceMove move  = default;

        for (uint i = 0; i < danceLoopLength; ++i)
        {
            var values = random.NextFloat4() * 10f - 5f;
            move.Duration = values.w + 5f;
            move.Velocity = values.xyz;
            random.state  = random.NextUInt();
            moves.Add(move);
        }
        manager.SetComponentData(entity, new Velocity {
            Value = (random.NextFloat3() - 0.5f) * 8f
        });
    }
コード例 #4
0
 void UpdateMatrix()
 {
     randomMatrix = new float4x4(rand.NextFloat4(), rand.NextFloat4(), rand.NextFloat4(), rand.NextFloat4());
 }
コード例 #5
0
        IEnumerator AsyncLoader()
        {
            textureShader.SetTexture(1, ShaderIDs._VirtualMainTex, albedoTex);
            textureShader.SetTexture(1, ShaderIDs._VirtualBumpMap, normalTex);
            textureShader.SetTexture(1, ShaderIDs._VirtualSMO, smTex);
            //     textureShader.SetTexture(1, ShaderIDs._MaskTex, heightTex);
            textureShader.SetTexture(2, ShaderIDs._VirtualMainTex, albedoTex);
            textureShader.SetTexture(2, ShaderIDs._VirtualBumpMap, normalTex);
            textureShader.SetTexture(2, ShaderIDs._VirtualSMO, smTex);
            //    textureShader.SetTexture(2, ShaderIDs._MaskTex, heightTex);
            for (int i = 0; i < terrainData.textures.Length; ++i)
            {
                PBRTexture texs = terrainData.textures[i];
                AsyncOperationHandle <Texture> albedoLoader = texs.albedoOccTex.LoadAssetAsync <Texture>();
                AsyncOperationHandle <Texture> normalLoader = texs.normalTex.LoadAssetAsync <Texture>();
                AsyncOperationHandle <Texture> smLoader     = texs.SMTex.LoadAssetAsync <Texture>();
                yield return(albedoLoader);

                yield return(normalLoader);

                yield return(smLoader);

                const int disp = COLOR_RESOLUTION / 8;
                textureShader.SetInt(ShaderIDs._OffsetIndex, i);
                if (albedoLoader.Result && normalLoader.Result && smLoader.Result)
                {
                    textureShader.SetTexture(1, ShaderIDs._TerrainMainTexArray, albedoLoader.Result);
                    textureShader.SetTexture(1, ShaderIDs._TerrainBumpMapArray, normalLoader.Result);
                    textureShader.SetTexture(1, ShaderIDs._TerrainSMTexArray, smLoader.Result);
                    textureShader.Dispatch(1, disp, disp, 1);
                }
                else
                {
                    textureShader.Dispatch(2, disp, disp, 1);
                }
                texs.albedoOccTex.ReleaseAsset();
                texs.normalTex.ReleaseAsset();
                texs.SMTex.ReleaseAsset();
            }
            albedoTex.GenerateMips();
            smTex.GenerateMips();
            normalTex.GenerateMips();
            //   heightTex.GenerateMips();
            CommandBuffer buffer = RenderPipeline.BeforeFrameBuffer;

            Unity.Mathematics.Random r = new Unity.Mathematics.Random((uint)System.Guid.NewGuid().GetHashCode());
            buffer.SetComputeTextureParam(textureShader, 6, ShaderIDs._DestTex, randomTileRT);
            buffer.SetComputeVectorParam(textureShader, ShaderIDs._TextureSize, float4(float2(1.0 / 256), 1, 1));
            buffer.SetComputeVectorParam(textureShader, ShaderIDs._RandomSeed, r.NextFloat4() + float4(0, 0, 0.5f, 0.5f));
            buffer.DispatchCompute(textureShader, 6, 8, 8, 1);
            while (enabled)
            {
                buffer = RenderPipeline.BeforeFrameBuffer;
                buffer.SetComputeTextureParam(textureShader, 0, ShaderIDs._NoiseTillingTexture, randomTileRT);
                buffer.SetComputeBufferParam(textureShader, 0, ShaderIDs._MaterialBuffer, materialBuffer);
                buffer.SetComputeTextureParam(textureShader, 0, ShaderIDs._VirtualMainTex, vt.GetTexture(0));
                buffer.SetComputeTextureParam(textureShader, 0, ShaderIDs._VirtualBumpMap, vt.GetTexture(1));
                buffer.SetComputeTextureParam(textureShader, 0, ShaderIDs._VirtualSMO, vt.GetTexture(2));
                //  buffer.SetComputeTextureParam(textureShader, 0, ShaderIDs._HeightMap, heightTex);
                buffer.SetComputeTextureParam(textureShader, 0, ShaderIDs._MainTex, albedoTex);
                buffer.SetComputeTextureParam(textureShader, 0, ShaderIDs._BumpMap, normalTex);
                buffer.SetComputeTextureParam(textureShader, 0, ShaderIDs._SMMap, smTex);
                buffer.SetComputeTextureParam(textureShader, 0, ShaderIDs._NoiseTexture, terrainData.noiseTex);
                buffer.SetComputeIntParam(textureShader, ShaderIDs._HeightResolution, MASK_RESOLUTION);
                int rtID = maskVT.GetTextureFormat(0).rtPropertyID;
                buffer.SetComputeTextureParam(textureShader, 0, rtID, maskVT.GetTexture(0));
                buffer.SetComputeTextureParam(textureShader, 0, maskVT.indexTexID, maskVT.indexTex);
                buffer.SetComputeTextureParam(textureShader, 4, ShaderIDs._VirtualBumpMap, vt.GetTexture(1));
                buffer.SetComputeTextureParam(textureShader, 3, ShaderIDs._IndexTexture, maskVT.indexTex);
                buffer.SetComputeTextureParam(textureShader, 3, ShaderIDs._VirtualHeightmap, maskVT.GetTexture(1));
                buffer.SetComputeTextureParam(textureShader, 3, ShaderIDs._DestTex, heightloadingCacheRT);
                buffer.SetComputeTextureParam(textureShader, 4, ShaderIDs._SourceTex, heightloadingCacheRT);
                buffer.SetComputeTextureParam(textureShader, 7, ShaderIDs._IndexTexture, maskVT.indexTex);
                buffer.SetComputeTextureParam(textureShader, 7, ShaderIDs._VirtualHeightmap, maskVT.GetTexture(1));
                buffer.SetComputeTextureParam(textureShader, 7, vt.GetTextureFormat(3).rtPropertyID, vt.GetTexture(3));

                buffer.SetComputeIntParam(textureShader, ShaderIDs._ColorResolution, COLOR_RESOLUTION);
                buffer.SetGlobalVector(ShaderIDs._HeightScaleOffset, (float4)double4(terrainData.heightScale, terrainData.heightOffset, 1, 1));
                TerrainLoadData loadData;
                MaskLoadCommand maskCommand;
                int             targetElement;
                while (maskLoadList.TryDequeue(out maskCommand))
                {
                    if (maskCommand.load)
                    {
                        int maskEle;
                        if (maskVT.LoadNewTexture(maskCommand.pos, 1, out maskEle))
                        {
                            VirtualTextureLoader.MaskBuffer maskLoadBuffer   = maskLoader.ScheduleLoadingJob(maskCommand.pos);
                            VirtualTextureLoader.MaskBuffer heightLoadBuffer = heightLoader.ScheduleLoadingJob(maskCommand.pos);
                            loadingThread.Schedule();
                            const int frameSepar = (MASK_RESOLUTION / 2048) * (MASK_RESOLUTION / 2048);
                            yield return(maskLoader.ReadToTexture(maskVT.GetTexture(0), maskEle, maskLoadBuffer, frameSepar));

                            yield return(heightLoader.ReadToTexture(maskVT.GetTexture(1), maskEle, heightLoadBuffer, frameSepar * 2));
                        }
                        else
                        {
                            Debug.LogError("No Enough Mask Position!");
                        }
                    }
                    else
                    {
                        maskVT.UnloadTexture(maskCommand.pos);
                    }
                }

                int initializedListLength = initializeLoadList.Length;
                if (initializedListLength > 0)
                {
                    NativeArray <int> allTextureElements = new NativeArray <int>(initializedListLength, Allocator.Persistent);

                    for (int i = 0; i < initializedListLength; ++i)
                    {
                        if (initializeLoadList.TryDequeue(out loadData))
                        {
                            switch (loadData.ope)
                            {
                            case TerrainLoadData.Operator.Load:
                                bool elementAva = vt.LoadNewTexture(loadData.startIndex, loadData.size, out targetElement);

                                if (elementAva)
                                {
                                    LoadTexture(loadData.startIndex, loadData.size, loadData.rootPos, loadData.maskScaleOffset, targetElement, RenderPipeline.BeforeFrameBuffer);
                                }

                                break;
                            }
                            initializeLoadList.Add(loadData);
                        }
                    }
                    int elementCount = 0;
                    while (initializeLoadList.TryDequeue(out loadData))
                    {
                        switch (loadData.ope)
                        {
                        case TerrainLoadData.Operator.Load:
                            targetElement = allTextureElements[elementCount];
                            elementCount++;
                            if (targetElement >= 0)
                            {
                                if (loadData.targetDecalLayer != 0)
                                {
                                    DrawDecal(loadData.startIndex, loadData.size, targetElement, loadData.targetDecalLayer, loadData.rootPos);
                                    yield return(null);
                                }
                            }
                            break;
                        }
                    }
                    allTextureElements.Dispose();
                }


                if (vt.LeftedTextureElement < leastVirtualTextureLefted)
                {
                    leastVirtualTextureLefted = vt.LeftedTextureElement;
                    Debug.Log(leastVirtualTextureLefted);
                }
                if (loadDataList.TryDequeue(out loadData))
                {
                    switch (loadData.ope)
                    {
                    case TerrainLoadData.Operator.Update:
                        targetElement = vt.GetChunkIndex(loadData.startIndex);
                        if (targetElement >= 0)
                        {
                            LoadTexture(loadData.startIndex, loadData.size, loadData.rootPos, loadData.maskScaleOffset, targetElement, RenderPipeline.BeforeFrameBuffer);
                            DrawDecal(loadData.startIndex, loadData.size, targetElement, loadData.targetDecalLayer, loadData.rootPos);
                        }
                        break;

                    case TerrainLoadData.Operator.Load:

                        bool elementAva = vt.LoadNewTexture(loadData.startIndex, loadData.size, out targetElement);

                        if (elementAva)
                        {
                            LoadTexture(loadData.startIndex, loadData.size, loadData.rootPos, loadData.maskScaleOffset, targetElement, RenderPipeline.BeforeFrameBuffer);
                            DrawDecal(loadData.startIndex, loadData.size, targetElement, loadData.targetDecalLayer, loadData.rootPos);
                        }
                        else
                        {
                            Debug.LogError("Virtual Texture No Capacity in loading  " + textureCapacity);
                        }
                        yield return(null);

                        break;

                    case TerrainLoadData.Operator.Separate:
                        int  subSize        = loadData.size / 2;
                        int2 leftDownIndex  = loadData.startIndex;
                        int2 leftUpIndex    = loadData.startIndex + int2(0, subSize);
                        int2 rightDownIndex = loadData.startIndex + int2(subSize, 0);
                        int2 rightUpIndex   = loadData.startIndex + subSize;
                        if (vt.LeftedTextureElement >= 3)
                        {
                            vt.LoadNewTextureChunks(loadData.startIndex, subSize, 2, vtContainer);
                            float  subScale             = loadData.maskScaleOffset.x;
                            float2 leftUpOffset         = float2(loadData.maskScaleOffset.yz + float2(0, subScale));
                            float2 rightDownOffset      = float2(loadData.maskScaleOffset.yz + float2(subScale, 0));
                            float2 rightUpOffset        = float2(loadData.maskScaleOffset.yz + subScale);
                            float3 leftDownScaleOffset  = loadData.maskScaleOffset;
                            float3 leftUpScaleOffset    = float3(subScale, leftUpOffset);
                            float3 rightDownScaleOffset = float3(subScale, rightDownOffset);
                            float3 rightUpScaleOffset   = float3(subScale, rightUpOffset);
                            LoadTexture(leftDownIndex, subSize, loadData.rootPos, leftDownScaleOffset, vtContainer[0], RenderPipeline.BeforeFrameBuffer);
                            LoadTexture(leftUpIndex, subSize, loadData.rootPos, leftUpScaleOffset, vtContainer[2], RenderPipeline.BeforeFrameBuffer);
                            LoadTexture(rightDownIndex, subSize, loadData.rootPos, rightDownScaleOffset, vtContainer[1], RenderPipeline.BeforeFrameBuffer);
                            LoadTexture(rightUpIndex, subSize, loadData.rootPos, rightUpScaleOffset, vtContainer[3], RenderPipeline.BeforeFrameBuffer);
                            DrawDecal(leftDownIndex, subSize, vtContainer[0], loadData.targetDecalLayer, loadData.rootPos);
                            DrawDecal(leftUpIndex, subSize, vtContainer[2], loadData.targetDecalLayer, loadData.rootPos);
                            DrawDecal(rightDownIndex, subSize, vtContainer[1], loadData.targetDecalLayer, loadData.rootPos);
                            DrawDecal(rightUpIndex, subSize, vtContainer[3], loadData.targetDecalLayer, loadData.rootPos);
                        }
                        else
                        {
                            Debug.LogError("Virtual Texture No Capacity  in separate   " + textureCapacity + "  " + vt.LeftedTextureElement);
                        }
                        yield return(null);

                        break;

                    case TerrainLoadData.Operator.Unload:
                        vt.UnloadTexture(loadData.startIndex);
                        break;

                    case TerrainLoadData.Operator.Combine:
                        subSize        = loadData.size / 2;
                        leftDownIndex  = loadData.startIndex;
                        leftUpIndex    = loadData.startIndex + int2(0, subSize);
                        rightDownIndex = loadData.startIndex + int2(subSize, 0);
                        rightUpIndex   = loadData.startIndex + subSize;
                        targetElement  = vt.CombineQuadTextures(leftDownIndex, rightDownIndex, leftUpIndex, rightUpIndex, leftDownIndex, loadData.size, RenderPipeline.BeforeFrameBuffer);
                        GenerateMips(targetElement, RenderPipeline.BeforeFrameBuffer);
                        yield return(null);

                        break;
                    }
                }
                else
                {
                    yield return(null);
                }
            }
        }