Esempio n. 1
0
        void ComputeVoxel(Kernel kernel, float dt)
        {
            voxelControl.SetBuffer(kernel.Index, kVoxelBufferKey, data.Buffer);

            voxelControl.SetVector(kStartKey, bounds.min);
            voxelControl.SetVector(kEndKey, bounds.max);
            voxelControl.SetVector(kSizeKey, bounds.size);

            voxelControl.SetFloat(kUnitLengthKey, data.UnitLength);
            voxelControl.SetFloat(kInvUnitLengthKey, 1f / data.UnitLength);
            voxelControl.SetFloat(kHalfUnitLengthKey, data.UnitLength * 0.5f);

            voxelControl.SetInt(kWidthKey, data.Width);
            voxelControl.SetInt(kHeightKey, data.Height);
            voxelControl.SetInt(kDepthKey, data.Depth);

            voxelControl.SetVector(kTimeKey, GetTime(Time.timeSinceLevelLoad));
            voxelControl.SetFloat(kDTKey, dt);

            voxelControl.SetFloat(kThrottleKey, throttle);

            GPUHelper.Dispatch2D(voxelControl, kernel, data.Width, data.Height);
        }
        public override void OnUpdateBounds(Bounds bounds)
        {
            base.OnUpdateBounds(bounds);

            depth = Create(resolution, resolution, 24, RenderTextureFormat.RFloat, TextureWrapMode.Repeat);

            var cam = GetComponent <Camera>();

            cam.targetTexture = depth;
            // cam.RenderWithShader(replacementShader, "RenderType");
            cam.SetReplacementShader(replacementShader, "RenderType");
            cam.Render();

            cam.enabled = false;

            Material mat;

            mat  = new Material(maskShader);
            mask = new RenderTexture(depth.width, depth.height, 0);
            mask.Create();
            Graphics.Blit(depth, mask, mat);
            Destroy(mat);

            signedDistanceField = new RenderTexture(depth.width, depth.height, 0);
            signedDistanceField.enableRandomWrite = true;
            signedDistanceField.Create();
            var kernel = signedDistanceFieldGen.FindKernel("Generate");

            signedDistanceFieldGen.SetTexture(kernel, "_Input", mask);
            signedDistanceFieldGen.SetTexture(kernel, "_Field", signedDistanceField);
            signedDistanceFieldGen.SetFloat("_Spread", spread);
            GPUHelper.Dispatch2D(signedDistanceFieldGen, kernel, signedDistanceField.width, signedDistanceField.height);

            onCaptureDepth.Invoke(depth);
            onCaptureSDF.Invoke(signedDistanceField);
        }