private void Start() { // カーネルIdの取得 kernelInitialize = _computeShader.FindKernel("Initialize"); kernelAddWave = _computeShader.FindKernel("AddWave"); kernelUpdate = _computeShader.FindKernel("Update"); kernelDraw = _computeShader.FindKernel("Draw"); // 波の高さを格納するテクスチャの作成 _waveTexture = new RenderTexture(256, 256, 0, RenderTextureFormat.RG32); _waveTexture.wrapMode = TextureWrapMode.Clamp; _waveTexture.enableRandomWrite = true; _waveTexture.Create(); // レンダリング用のテクスチャの作成 _drawTexture = new RenderTexture(256, 256, 0, RenderTextureFormat.ARGB32); _drawTexture.enableRandomWrite = true; _drawTexture.Create(); // スレッド数の取得 uint threadSizeX, threadSizeY, threadSizeZ; _computeShader.GetKernelThreadGroupSizes(kernelInitialize, out threadSizeX, out threadSizeY, out threadSizeZ); threadSizeInitialize = new ThreadSize(threadSizeX, threadSizeY, threadSizeZ); _computeShader.GetKernelThreadGroupSizes(kernelUpdate, out threadSizeX, out threadSizeY, out threadSizeZ); threadSizeUpdate = new ThreadSize(threadSizeX, threadSizeY, threadSizeZ); _computeShader.GetKernelThreadGroupSizes(kernelDraw, out threadSizeX, out threadSizeY, out threadSizeZ); threadSizeDraw = new ThreadSize(threadSizeX, threadSizeY, threadSizeZ); // 波の高さの初期化 _computeShader.SetTexture(kernelInitialize, "waveTexture", _waveTexture); _computeShader.Dispatch(kernelInitialize, Mathf.CeilToInt(_waveTexture.width / threadSizeInitialize.x), Mathf.CeilToInt(_waveTexture.height / threadSizeInitialize.y), 1); }
// Use this for initialization void Start() { //Initialized RenderTexture _renderTexture = new RenderTexture(512, 512, 0, RenderTextureFormat.ARGB32); _renderTexture.enableRandomWrite = true; _renderTexture.Create(); //ComputeShader settings _kernelIndex_PanelColor = _computeShaderTextureTest.FindKernel("PanelColor"); uint threadSizeX; uint threadSizeY; uint threadSizeZ; _computeShaderTextureTest.GetKernelThreadGroupSizes (_kernelIndex_PanelColor, out threadSizeX, out threadSizeY, out threadSizeZ); _kernelThreadSize_PanelColor = new ThreadSize(threadSizeX, threadSizeY, threadSizeZ); //Output Setting _computeShaderTextureTest.SetTexture (_kernelIndex_PanelColor, "Result", _renderTexture); _renderer = _plane.GetComponent <Renderer>(); }
private void LifeGameStart() { // カーネルIdの取得 kernelInitialize = computeShader.FindKernel("Initialize"); kernelUpdate = computeShader.FindKernel("Update"); kernelDraw = computeShader.FindKernel("Draw"); // 状態を格納するテクスチャの作成 stateTexture = new RenderTexture(512, 512, 0, RenderTextureFormat.RInt); stateTexture.wrapMode = TextureWrapMode.Repeat; stateTexture.enableRandomWrite = true; stateTexture.Create(); // レンダリング用のテクスチャの取得 drawTexture = new RenderTexture(512, 512, 0, RenderTextureFormat.ARGB32); drawTexture.filterMode = FilterMode.Point; drawTexture.enableRandomWrite = true; drawTexture.Create(); // スレッド数の取得 uint threadSizeX, threadSizeY, threadSizeZ; computeShader.GetKernelThreadGroupSizes(kernelInitialize, out threadSizeX, out threadSizeY, out threadSizeZ); threadSizeInitialize = new ThreadSize(threadSizeX, threadSizeY, threadSizeZ); computeShader.GetKernelThreadGroupSizes(kernelUpdate, out threadSizeX, out threadSizeY, out threadSizeZ); threadSizeUpdate = new ThreadSize(threadSizeX, threadSizeY, threadSizeZ); computeShader.GetKernelThreadGroupSizes(kernelDraw, out threadSizeX, out threadSizeY, out threadSizeZ); threadSizeDraw = new ThreadSize(threadSizeX, threadSizeY, threadSizeZ); // LifeGameの状態の初期化 computeShader.SetFloat("keikaTime", Time.time); computeShader.SetTexture(kernelInitialize, "stateTexture", stateTexture); computeShader.Dispatch(kernelInitialize, Mathf.CeilToInt(stateTexture.width / threadSizeInitialize.x), Mathf.CeilToInt(stateTexture.height / threadSizeInitialize.y), 1); }
private void Update() { var vertBuf = this.VertsFacade == null ? null : this.VertsFacade.Get(); var targetsBuf = this.TargetsFacade == null ? null : this.TargetsFacade.Get(); if (vertBuf == null || targetsBuf == null) { return; // TODO; log something? } // Update our UnitSize to match the number of verts in our vertBuf // (when multiplied with ThreadSize, which should match the values in the shader) if (_count != vertBuf.count) { this._count = (uint)vertBuf.count; Vector2Int resolution = new ThreadSize(this._count, (uint)ThreadSize.x, (uint)ThreadSize.y).Resolution; uint vertcount = (uint)resolution.x * (uint)resolution.y; this.UnitSize = new ThreadSize(vertcount, (uint)ThreadSize.x, (uint)ThreadSize.y).UnitSize; #if UNITY_EDITOR // Update info in Unity editor for debugging... this.VectorCount = (int)vertcount; #endif } this.Shader.SetBuffer(Kernel, ShaderProps.vertBuffer, this.VertsFacade.Get()); this.Shader.SetBuffer(Kernel, ShaderProps.targetsBuffer, this.TargetsFacade.Get()); this.Shader.SetInt(ShaderProps.TargetsCount, targetsBuf.count); this.Shader.SetFloat(ShaderProps.EaseFactor, this.EaseFactor); this.Shader.SetInt(ShaderProps.ResolutionX, this.ThreadSize.x * this.UnitSize.x); this.Shader.SetBool(ShaderProps.AnimateAll, this.AnimateAll); this.Shader.Dispatch(Kernel, UnitSize.x, UnitSize.y, 1); }
override public void Apply(ComputeBuffer forces_buf, ComputeBuffer positions_buf) { if (OriginTransform != null) { this.Origin = OriginTransform.position; } // Update our UnitSize to match the number of verts in our vertBuf // (when multiplied with ThreadSize, which should match the values in the shader) if (count_ != forces_buf.count) { this.count_ = (uint)forces_buf.count; Vector2Int resolution = new ThreadSize(this.count_, (uint)ThreadSize.x, (uint)ThreadSize.y).Resolution; uint count = (uint)resolution.x * (uint)resolution.y; this.UnitSize = new ThreadSize(count, (uint)ThreadSize.x, (uint)ThreadSize.y).UnitSize; #if UNITY_EDITOR // Update info in Unity editor for debugging... this.ForcesCount = (int)count; this.Res = resolution; this.UnitRes = this.UnitSize; #endif } this.Shader.SetBuffer(Kernel, ShaderProps.positions_buf, positions_buf); this.Shader.SetBuffer(Kernel, ShaderProps.forces_buf, forces_buf); this.Shader.SetInt(ShaderProps.PositionsCount, positions_buf.count); this.Shader.SetFloat(ShaderProps.Strength, this.Strength); this.Shader.SetFloat(ShaderProps.ZeroDist, this.ZeroDist); this.Shader.SetInt(ShaderProps.ResolutionX, this.ThreadSize.x * this.UnitSize.x); this.Shader.SetBool(ShaderProps.Additive, this.Additive); this.Shader.SetVector(ShaderProps.Origin, this.Origin); this.Shader.Dispatch(Kernel, UnitSize.x, UnitSize.y, 1); }
void Start() { // Reserve render textures int reso = (int)Mathf.Pow(2, m_resolution); m_renderTextureX = new RenderTexture(reso, reso, 0, RenderTextureFormat.ARGB32); m_renderTextureY = new RenderTexture(reso, reso, 0, RenderTextureFormat.ARGB32); m_renderTextureX.enableRandomWrite = true; m_renderTextureY.enableRandomWrite = true; m_renderTextureX.Create(); m_renderTextureY.Create(); // Read kernel indices and thread sizes m_kernelIdxX = m_computeShader.FindKernel("KernelFuncX"); m_kernelIdxY = m_computeShader.FindKernel("KernelFuncY"); uint threadSizeX, threadSizeY, threadSizeZ; m_computeShader.GetKernelThreadGroupSizes(m_kernelIdxX, out threadSizeX, out threadSizeY, out threadSizeZ); m_kernelThreadSizeX = new ThreadSize(threadSizeX, threadSizeY, threadSizeZ); m_computeShader.GetKernelThreadGroupSizes(m_kernelIdxY, out threadSizeX, out threadSizeY, out threadSizeZ); m_kernelThreadSizeY = new ThreadSize(threadSizeX, threadSizeY, threadSizeZ); m_computeShader.SetTexture(m_kernelIdxX, "textureBuffer", m_renderTextureX); m_computeShader.SetTexture(m_kernelIdxY, "textureBuffer", m_renderTextureY); }
void Start() { // enableRandomWrite を有効にして、 // 書き込み可能な状態のテクスチャを生成することが重要です。 this.renderTexture_A = new RenderTexture(512, 512, 0, RenderTextureFormat.ARGB32); this.renderTexture_B = new RenderTexture(512, 512, 0, RenderTextureFormat.ARGB32); this.renderTexture_C = new RenderTexture(512, 512, 0, RenderTextureFormat.ARGB32); this.renderTexture_A.enableRandomWrite = true; this.renderTexture_B.enableRandomWrite = true; this.renderTexture_C.enableRandomWrite = true; this.renderTexture_A.Create(); this.renderTexture_B.Create(); this.renderTexture_C.Create(); // カーネルのインデックスと、スレッドのサイズを保存します。 this.kernelIndex_KernelFunction_A = this.computeShader.FindKernel("KernelFunction_A"); this.kernelIndex_KernelFunction_B = this.computeShader.FindKernel("KernelFunction_B"); this.kernelIndex_KernelFunction_C = this.computeShader.FindKernel("KernelFunction_C"); uint threadSizeX, threadSizeY, threadSizeZ; this.computeShader.GetKernelThreadGroupSizes (this.kernelIndex_KernelFunction_A, out threadSizeX, out threadSizeY, out threadSizeZ); this.kernelThreadSize_KernelFunction_A = new ThreadSize(threadSizeX, threadSizeY, threadSizeZ); this.computeShader.GetKernelThreadGroupSizes (this.kernelIndex_KernelFunction_B, out threadSizeX, out threadSizeY, out threadSizeZ); this.kernelThreadSize_KernelFunction_B = new ThreadSize(threadSizeX, threadSizeY, threadSizeZ); this.computeShader.GetKernelThreadGroupSizes (this.kernelIndex_KernelFunction_C, out threadSizeX, out threadSizeY, out threadSizeZ); this.kernelThreadSize_KernelFunction_C = new ThreadSize(threadSizeX, threadSizeY, threadSizeZ); // ComputeShader で計算した結果を保存するためのバッファとして、ここではテクスチャを設定します。 this.computeShader.SetTexture (this.kernelIndex_KernelFunction_A, "textureBuffer", this.renderTexture_A); this.computeShader.SetTexture (this.kernelIndex_KernelFunction_B, "textureBuffer", this.renderTexture_B); this.computeShader.SetTexture (this.kernelIndex_KernelFunction_C, "textureBuffer", this.renderTexture_C); }
override public void Apply(ComputeBuffer forces_buf, ComputeBuffer positions_buf) { if (this.Texture == null) { return; } this.Texture.filterMode = FilterMode.Bilinear; // Update our UnitSize to match the number of verts in our vertBuf // (when multiplied with ThreadSize, which should match the values in the shader) if (count_ != forces_buf.count) { this.count_ = (uint)forces_buf.count; Vector2Int resolution = new ThreadSize(this.count_, (uint)ThreadSize.x, (uint)ThreadSize.y).Resolution; uint count = (uint)resolution.x * (uint)resolution.y; this.UnitSize = new ThreadSize(count, (uint)ThreadSize.x, (uint)ThreadSize.y).UnitSize; #if UNITY_EDITOR // Update info in Unity editor for debugging... this.DebugInfo.Count = (int)count; //this.Res = resolution; //this.UnitRes = this.UnitSize; #endif } this.Shader.SetBuffer(Kernel, ShaderProps.positions_buf, positions_buf); this.Shader.SetBuffer(Kernel, ShaderProps.forces_buf, forces_buf); this.Shader.SetInt(ShaderProps.PositionsCount, positions_buf.count); this.Shader.SetInt(ShaderProps.ResolutionX, this.ThreadSize.x * this.UnitSize.x); this.Shader.SetBool(ShaderProps.Additive, this.Additive); this.Shader.SetVector(ShaderProps.TexCoordOrigin, this.TexCoordOrigin); this.Shader.SetVector(ShaderProps.TexCoordFactorX, this.TexCoordFactorX); this.Shader.SetVector(ShaderProps.TexCoordFactorY, this.TexCoordFactorX); this.Shader.SetVector(ShaderProps.TexCoordFactorZ, this.TexCoordFactorX); this.Shader.SetVector(ShaderProps.MinForceR, this.MinForceR); this.Shader.SetVector(ShaderProps.MinForceG, this.MinForceG); this.Shader.SetVector(ShaderProps.MinForceB, this.MinForceB); this.Shader.SetVector(ShaderProps.MaxForceR, this.MaxForceR); this.Shader.SetVector(ShaderProps.MaxForceG, this.MaxForceG); this.Shader.SetVector(ShaderProps.MaxForceB, this.MaxForceB); this.Shader.SetTexture(Kernel, ShaderProps.Texture, this.Texture); this.Shader.Dispatch(Kernel, UnitSize.x, UnitSize.y, 1); }
void Start() { this.renderTexture_A = new RenderTexture(512, 512, 0, RenderTextureFormat.ARGB32); //textureへの書き込みを有効にする this.renderTexture_A.enableRandomWrite = true; renderTexture_A.Create(); this.kernelIndex_KernelFunction_A = this.computeShader.FindKernel("KernelFunction_A"); uint threadSizeX, threadSizeY, threadSizeZ; //threadSizeの取得 this.computeShader.GetKernelThreadGroupSizes(this.kernelIndex_KernelFunction_A , out threadSizeX, out threadSizeY, out threadSizeZ); this.kernelThreadSize_KrnelFunction_A = new ThreadSize(threadSizeX, threadSizeY, threadSizeZ); this.computeShader.SetTexture(this.kernelIndex_KernelFunction_A, "textureBuffer", this.renderTexture_A); }
override public void Apply(ComputeBuffer forces_buf, ComputeBuffer positions_buf) { var targets_buf = (this.TargetsFacade == null ? null : this.TargetsFacade.GetValid()); if (targets_buf == null) { return; } // Update our UnitSize to match the number of verts in our vertBuf // (when multiplied with ThreadSize, which should match the values in the shader) if (count_ != forces_buf.count) { this.count_ = (uint)forces_buf.count; Vector2Int resolution = new ThreadSize(this.count_, (uint)ThreadSize.x, (uint)ThreadSize.y).Resolution; uint count = (uint)resolution.x * (uint)resolution.y; this.UnitSize = new ThreadSize(count, (uint)ThreadSize.x, (uint)ThreadSize.y).UnitSize; #if UNITY_EDITOR // Update info in Unity editor for debugging... this.Count = (int)count; this.Res = resolution; this.UnitRes = this.UnitSize; #endif } this.Shader.SetBuffer(Kernel, ShaderProps.positions_buf, positions_buf); this.Shader.SetBuffer(Kernel, ShaderProps.forces_buf, forces_buf); this.Shader.SetBuffer(Kernel, ShaderProps.targets_buf, targets_buf); this.Shader.SetMatrix(ShaderProps.TargetsToWorldMatrix, this.TargetsParent != null ? this.TargetsParent.localToWorldMatrix : Matrix4x4.identity); this.Shader.SetInt(ShaderProps.PositionsCount, positions_buf.count); this.Shader.SetFloat(ShaderProps.Strength, this.Strength); this.Shader.SetFloat(ShaderProps.LinearFalloffRange, this.LinearFalloffRange); this.Shader.SetFloat(ShaderProps.CubicFalloffRange, this.CubicFalloffRange); this.Shader.SetFloat(ShaderProps.MinDistance, this.MinDistance); this.Shader.SetInt(ShaderProps.ResolutionX, this.ThreadSize.x * this.UnitSize.x); this.Shader.SetBool(ShaderProps.Additive, this.Additive); this.Shader.Dispatch(Kernel, UnitSize.x, UnitSize.y, 1); }
// Use this for initialization void Start() { int width = size; int height = size; if (inputTexture) { width = inputTexture.width; height = inputTexture.height; } if (targetMaterial == null) { targetMaterial = gameObject.GetComponent <Renderer>().material; } // renderTextureの生成 this.renderTextureIn = new RenderTexture(width, height, 0, RenderTextureFormat.ARGB32); this.renderTextureOut = new RenderTexture(width, height, 0, RenderTextureFormat.ARGB32); this.renderTextureIn.enableRandomWrite = true; this.renderTextureOut.enableRandomWrite = true; this.renderTextureIn.Create(); this.renderTextureOut.Create(); // kernel indexの取得 this.kernelIndex_GenerateRandomTexture = this.computeShader.FindKernel("GenerateRandomTexture"); this.kernelIndex_LifeGame = this.computeShader.FindKernel("LifeGame"); this.kernelIndex_FlipBuffer = this.computeShader.FindKernel("FlipBuffer"); // スレッドサイズの取得 uint threadSizeX, threadSizeY, threadSizeZ; this.computeShader.GetKernelThreadGroupSizes (this.kernelIndex_GenerateRandomTexture, out threadSizeX, out threadSizeY, out threadSizeZ); Debug.Log("x,y,y : " + threadSizeX + "," + threadSizeY + "," + threadSizeZ); this.kernelThreadSize_GenerateRandomTexture = new ThreadSize(threadSizeX, threadSizeY, threadSizeZ); this.computeShader.GetKernelThreadGroupSizes (this.kernelIndex_LifeGame, out threadSizeX, out threadSizeY, out threadSizeZ); this.kernelThreadSize_LifeGame = new ThreadSize(threadSizeX, threadSizeY, threadSizeZ); this.computeShader.GetKernelThreadGroupSizes (this.kernelIndex_FlipBuffer, out threadSizeX, out threadSizeY, out threadSizeZ); this.kernelThreadSize_FlipBuffer = new ThreadSize(threadSizeX, threadSizeY, threadSizeZ); // kernelへtexture bufferを設定 this.computeShader.SetTexture (this.kernelIndex_GenerateRandomTexture, "textureBufferIn", this.renderTextureIn); this.computeShader.SetTexture (this.kernelIndex_LifeGame, "textureBufferIn", this.renderTextureIn); this.computeShader.SetTexture (this.kernelIndex_LifeGame, "textureBufferOut", this.renderTextureOut); this.computeShader.SetTexture (this.kernelIndex_FlipBuffer, "textureBufferIn", this.renderTextureIn); this.computeShader.SetTexture (this.kernelIndex_FlipBuffer, "textureBufferOut", this.renderTextureOut); // 初期設定 this.computeShader.SetInt("Seed", (int)Time.time); this.computeShader.SetFloat("Density", density); this.computeShader.SetFloats("aliveColor", new float[] { aliveColor.r, aliveColor.g, aliveColor.b, aliveColor.a }); this.computeShader.SetFloats("dieColor", new float[] { dieColor.r, dieColor.g, dieColor.b, dieColor.a }); if (inputTexture) { //メインテクスチャのコピー Graphics.Blit(inputTexture, this.renderTextureIn); } else { this.computeShader.Dispatch(this.kernelIndex_GenerateRandomTexture, this.renderTextureIn.width / this.kernelThreadSize_GenerateRandomTexture.x, this.renderTextureIn.height / this.kernelThreadSize_GenerateRandomTexture.y, this.kernelThreadSize_GenerateRandomTexture.z); } targetMaterial.mainTexture = this.renderTextureIn; }
void initilizeCubemap() { //создаем числа в случайном массиве this.init(125); this.renderTexture_A = new RenderTexture(1024, 1024, 0, RenderTextureFormat.ARGB32); this.renderTexture_A.enableRandomWrite = true; this.renderTexture_A.Create(); this.kernelIndex_KernelFunction_A = this.computeShader.FindKernel("KernelFunction_A"); uint threadSizeX, threadSizeY, threadSizeZ; this.computeShader.GetKernelThreadGroupSizes (this.kernelIndex_KernelFunction_A, out threadSizeX, out threadSizeY, out threadSizeZ); this.kernelThreadSize_KernelFunction_A = new ThreadSize(threadSizeX, threadSizeY, threadSizeZ); this.computeShader.SetTexture (this.kernelIndex_KernelFunction_A, "textureBuffer", this.renderTexture_A); //ставлю массив с случайной датой this.computeShader.SetBuffer(this.kernelIndex_KernelFunction_A, "randData", randDataBuffer); //this.computeShader.SetInts("randData", randData); this.heights = new ComputeBuffer(1024 * 1024 * 6, sizeof(float)); this.heightsData = new float[1024 * 1024 * 6]; this.computeShader.SetBuffer(this.kernelIndex_KernelFunction_A, "heights", this.heights); this.map = new Cubemap(1024, UnityEngine.Experimental.Rendering.GraphicsFormat.R8G8B8A8_SRGB, 0); this.computeShader.Dispatch(this.kernelIndex_KernelFunction_A, this.renderTexture_A.width / this.kernelThreadSize_KernelFunction_A.x, this.renderTexture_A.height / this.kernelThreadSize_KernelFunction_A.y, this.kernelThreadSize_KernelFunction_A.z); this.heights.GetData(this.heightsData); for (int x = 0; x < 1024; x++) { for (int y = 0; y < 1024; y++) { int index = x + y * 1024; float val = this.heightsData[index]; this.map.SetPixel(CubemapFace.NegativeX, x, y, new Color(val, val, val, 1)); index += 1024 * 1024; val = this.heightsData[index]; this.map.SetPixel(CubemapFace.NegativeY, x, y, new Color(val, val, val, 1)); index += 1024 * 1024; val = this.heightsData[index]; this.map.SetPixel(CubemapFace.NegativeZ, x, y, new Color(val, val, val, 1)); index += 1024 * 1024; val = this.heightsData[index]; this.map.SetPixel(CubemapFace.PositiveX, x, y, new Color(val, val, val, 1)); index += 1024 * 1024; val = this.heightsData[index]; this.map.SetPixel(CubemapFace.PositiveY, x, y, new Color(val, val, val, 1)); index += 1024 * 1024; val = this.heightsData[index]; this.map.SetPixel(CubemapFace.PositiveZ, x, y, new Color(val, val, val, 1)); } } this.map.Apply(); }