public int GetTex(string guid)
        {
            int index;

            if (guidToIndex.TryGetValue(guid, out index))
            {
                usageCount[index]++;
            }
            else
            {
                if (indexPool.Length <= 0)
                {
                    throw new Exception("Texture Pool out of Range!!");
                }
                index = indexPool[indexPool.Length - 1];
                indexPool.RemoveLast();
                usageCount[index] = 1;
                guidToIndex.Add(guid, index);
                msb.Clear();
                msb.Add(ClusterMatResources.texturePath);
                msb.Add(guid);
                msb.Add(".mtex");
                streamer.AddStreamingCommand(msb.str, rt, index, streamingIndex);
                //TODO
                //Streaming Load Texture
            }

            return(index);
        }
 private void LoadThreadUpdate()
 {
     while (initialized)
     {
         while (true)
         {
             LoadCommand commd;
             lock (allCommands)
             {
                 if (allCommands.Count <= 0)
                 {
                     break;
                 }
                 commd = allCommands[0];
                 allCommands.RemoveAt(0);
             }
             msb.Clear();
             msb.Add(commd.paths.Ptr(), commd.paths.Length);
             commd.paths.Dispose();
             using (FileStream reader = new FileStream(msb.str, FileMode.Open, FileAccess.Read))
             {
                 reader.Read(loadBytes, 0, Mathf.Min(loadBytes.Length, (int)reader.Length));
             }
             targetUpdateTex  = commd.targetTex;
             currentLoadSlice = 0;
             targetIndex      = commd.targetIndex;
             updateIndex      = commd.formatIndex;
             //TODO
             mainThreadSyncEvent.WaitOne();
         }
         commandEmptyEvent.WaitOne();
     }
 }
        public static void WriteGuide(string path, string saveName, NativeList <int2> blendTargets)
        {
            string direct = path + "/" + saveName + "/Guide.bytes";

            if (!Directory.Exists(direct))
            {
                Directory.CreateDirectory(direct);
            }
            MStringBuilder msb = new MStringBuilder(10 * blendTargets.Length);

            foreach (var i in blendTargets)
            {
                msb.Add(i.x.ToString());
                msb.Add('_');
                msb.Add(i.y.ToString());
                msb.Add("\r\n");
            }
            File.WriteAllText(direct, msb.str);
        }
        public static void WriteTexture(string path, string saveName, Texture rt, int2 blendTarget)
        {
            string direct = path + "/" + saveName;

            if (!Directory.Exists(direct))
            {
                Directory.CreateDirectory(direct);
            }
            ComputeBuffer buffer = new ComputeBuffer(rt.width * rt.height, sizeof(float4));
            ComputeShader cs     = Resources.Load <ComputeShader>("ReadRTData");

            cs.SetBuffer(0, "_TextureDatas", buffer);
            cs.SetTexture(0, "_TargetTexture", rt);
            cs.SetInt("_Width", rt.width);
            cs.SetInt("_Height", rt.height);
            cs.Dispatch(0, rt.width / 8, rt.height / 8, 1);


            MStringBuilder msb = new MStringBuilder(direct.Length + 25);

            msb.Add(direct);
            msb.Add('/');
            msb.Add(blendTarget.x.ToString());
            msb.Add('_');
            msb.Add(blendTarget.y.ToString());
            msb.Add(".bytes");
            Color[] cols = new Color[buffer.count];
            buffer.GetData(cols);
            byte[] bytes = new byte[cols.Length];
            for (int i = 0; i < cols.Length; ++i)
            {
                bytes[i] = (byte)(cols[i].r * 255.99999);
            }
            File.WriteAllBytes(msb.str, bytes);
        }
Exemple #5
0
    public override void OnInspectorGUI()
    {
        base.OnInspectorGUI();
        if (foldOut = EditorGUILayout.Foldout(foldOut, "Layer Culling Distance: "))
        {
            for (int i = 0; i < 32; ++i)
            {
                string layerName = LayerMask.LayerToName(i);

                if (!string.IsNullOrEmpty(layerName))
                {
                    msb.Combine("   ", layerName);
                    msb.Add(": ");
                    target.layerCullDistance[i] = EditorGUILayout.FloatField(msb.str, target.layerCullDistance[i]);
                }
            }
        }
    }
        private static void PackHeightmap(Texture2D[,] allHeightmap, int textureSize, int lodLevel, string path, string terrainName)
        {
            ComputeShader  shader     = Resources.Load <ComputeShader>("MipmapCompute");
            ComputeBuffer  readBuffer = new ComputeBuffer(textureSize * textureSize, sizeof(float));
            MStringBuilder sb         = new MStringBuilder(path.Length + terrainName.Length + 15);

            sb.Add(path);
            if (path[path.Length - 1] != '/')
            {
                sb.Add("/");
            }
            sb.Add(terrainName);
            path += terrainName;
            if (Directory.Exists(sb.str))
            {
                Directory.Delete(sb.str);
            }
            Directory.CreateDirectory(sb.str);
            int pathLength = sb.str.Length;

            for (int i = 0; i < lodLevel; ++i)
            {
                sb.Resize(pathLength);
                sb.Add("/LOD" + i.ToString());
                Directory.CreateDirectory(sb.str);
            }
            sb.Resize(pathLength);
            sb.Add("/LOD0");
            for (int x = 0; x < allHeightmap.GetLength(0); ++x)
            {
                for (int y = 0; y < allHeightmap.GetLength(1); ++y)
                {
                    Texture2D tex = allHeightmap[x, y];
                    if (tex.width != textureSize ||
                        tex.height != textureSize)
                    {
                        readBuffer.Dispose();
                        Resources.UnloadAsset(shader);
                        throw new System.Exception("Texture " + tex.name + " setting is not right!(Width, Height, isReadable)");
                    }
                }
            }
            shader.SetBuffer(1, "_OutputBuffer", readBuffer);
            float[] result = new float[textureSize * textureSize];
            void SaveTexture(StreamWriter writer, Texture tex)
            {
                shader.SetTexture(1, ShaderIDs._MainTex, tex);
                int kernelSize = Mathf.CeilToInt(textureSize / 8f);

                shader.Dispatch(1, kernelSize, kernelSize, 1);
                readBuffer.GetData(result);
                char[] chrArray = new char[result.Length * sizeof(half)];
                half * arrPtr   = (half *)chrArray.Ptr();

                for (int i = 0; i < result.Length; ++i)
                {
                    arrPtr[i] = (half)result[i];
                }
                writer.Write(chrArray);
            }

            using (StreamWriter writer = new StreamWriter(sb.str))
            {
                for (int x = 0; x < allHeightmap.GetLength(0); ++x)
                {
                    for (int y = 0; y < allHeightmap.GetLength(1); ++y)
                    {
                        Texture2D tex = allHeightmap[x, y];
                        SaveTexture(writer, tex);
                    }
                }
            }
            readBuffer.Dispose();
            Resources.UnloadAsset(shader);
        }
Exemple #7
0
 private void Awake()
 {
     if (!msb.isCreated)
     {
         msb = new MStringBuilder(30);
     }
     msb.Clear();
     msb.Add("Assets/BinaryData/Irradiance/");
     msb.Add(probeName);
     msb.Add(".mpipe");
     if (!System.IO.File.Exists(msb.str))
     {
         Debug.LogError("Probe: " + probeName + "read Error! ");
         Destroy(gameObject);
         return;
     }
     else
     {
         using (System.IO.FileStream fs = new System.IO.FileStream(msb.str, System.IO.FileMode.Open, System.IO.FileAccess.Read))
         {
             byte[] arr = GetByteArray(fs.Length);
             fs.Read(arr, 0, (int)fs.Length);
             int3 *res = (int3 *)arr.Ptr();
             resolution = *res;
             if (resolution.x * resolution.y * resolution.z * sizeof(float3x3) != fs.Length - sizeof(int3))
             {
                 Debug.LogError("Data size incorrect!");
                 Destroy(gameObject);
                 return;
             }
             NativeArray <float3x3> allDatas = new NativeArray <float3x3>(resolution.x * resolution.y * resolution.z, Allocator.Temp);
             UnsafeUtility.MemCpy(allDatas.GetUnsafePtr(), res + 1, sizeof(float3x3) * allDatas.Length);
             RenderTextureDescriptor desc = new RenderTextureDescriptor
             {
                 colorFormat       = RenderTextureFormat.ARGBHalf,
                 dimension         = TextureDimension.Tex3D,
                 enableRandomWrite = true,
                 width             = resolution.x,
                 height            = resolution.y,
                 volumeDepth       = resolution.z,
                 msaaSamples       = 1
             };
             src0             = new RenderTexture(desc);
             src1             = new RenderTexture(desc);
             desc.colorFormat = RenderTextureFormat.RHalf;
             src2             = new RenderTexture(desc);
             shBuffer         = new ComputeBuffer(allDatas.Length, sizeof(float3x3));
             shBuffer.SetData(allDatas);
             ComputeShader shader = resources.shaders.occlusionProbeCalculate;
             int3 *        arrPtr = (int3 *)resolutionArray.Ptr();
             *arrPtr = resolution;
             shader.SetBuffer(2, "_SHBuffer", shBuffer);
             shader.SetTexture(2, "_Src0", src0);
             shader.SetTexture(2, "_Src1", src1);
             shader.SetTexture(2, "_Src2", src2);
             shader.SetInts("_Resolution", resolutionArray);
             shader.Dispatch(2, Mathf.CeilToInt(resolution.x / 4f), Mathf.CeilToInt(resolution.y / 4f), Mathf.CeilToInt(resolution.z / 4f));
             shBuffer.Dispose();
             allDatas.Dispose();
         }
     }
 }
 public void GetString(MStringBuilder msb)
 {
     msb.Clear();
     msb.Add((char *)ptr, CHAR_LENGTH);
 }