2020-12-22 03:13:43 +00:00
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
|
|
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
|
|
|
|
using System;
|
2020-11-27 22:47:33 +00:00
|
|
|
|
using UnityEngine;
|
2021-02-26 19:01:47 +00:00
|
|
|
|
using Unity.Mathematics;
|
|
|
|
|
|
using UnityEngine.Experimental.Rendering;
|
|
|
|
|
|
using static Unity.Mathematics.math;
|
|
|
|
|
|
using static Unity.Mathematics.noise;
|
2020-11-27 22:47:33 +00:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Drives the volume render.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
//[ExecuteInEditMode]
|
|
|
|
|
|
[RequireComponent( typeof( Camera ) ), ImageEffectAllowedInSceneView]
|
|
|
|
|
|
public class VolumeSampling : MonoBehaviour
|
|
|
|
|
|
{
|
2021-02-26 19:01:47 +00:00
|
|
|
|
// public int Octaves = 4;
|
|
|
|
|
|
// public float Frequency = 1;
|
|
|
|
|
|
// public float Lacunarity = 2;
|
|
|
|
|
|
// public float Gain = .5f;
|
|
|
|
|
|
// public int NoiseTextureCount = 16;
|
|
|
|
|
|
// public int NoiseTextureSize = 32;
|
2020-11-27 22:47:33 +00:00
|
|
|
|
public Material _volMaterial;
|
|
|
|
|
|
public Transform GridTransform;
|
|
|
|
|
|
private Camera _camera;
|
2021-02-26 19:01:47 +00:00
|
|
|
|
// private Texture3D[] _noiseTextures;
|
|
|
|
|
|
// private int _currentNoiseTexture;
|
2020-11-27 22:47:33 +00:00
|
|
|
|
|
|
|
|
|
|
private void Start()
|
|
|
|
|
|
{
|
|
|
|
|
|
_camera = GetComponent<Camera>();
|
2021-02-26 19:01:47 +00:00
|
|
|
|
// _noiseTextures = new Texture3D[NoiseTextureCount];
|
|
|
|
|
|
// var samples = new Color[NoiseTextureSize * NoiseTextureSize * NoiseTextureSize];
|
|
|
|
|
|
// for (int i = 0; i < NoiseTextureCount; i++)
|
|
|
|
|
|
// {
|
|
|
|
|
|
// var iv = (float) i / NoiseTextureCount;
|
|
|
|
|
|
// var tex = new Texture3D(NoiseTextureSize,NoiseTextureSize,NoiseTextureSize, GraphicsFormat.R8_UNorm, TextureCreationFlags.None);
|
|
|
|
|
|
// tex.filterMode = FilterMode.Bilinear;
|
|
|
|
|
|
// for (int z = 0; z < NoiseTextureSize; z++)
|
|
|
|
|
|
// {
|
|
|
|
|
|
// var zf = (float) z / NoiseTextureSize;
|
|
|
|
|
|
// int zOffset = z * NoiseTextureSize * NoiseTextureSize;
|
|
|
|
|
|
// for (int y = 0; y < NoiseTextureSize; y++)
|
|
|
|
|
|
// {
|
|
|
|
|
|
// var yf = (float) y / NoiseTextureSize;
|
|
|
|
|
|
// int yOffset = y * NoiseTextureSize;
|
|
|
|
|
|
// for (int x = 0; x < NoiseTextureSize; x++)
|
|
|
|
|
|
// {
|
|
|
|
|
|
// var xf = (float) x / NoiseTextureSize;
|
|
|
|
|
|
// var index = x + yOffset + zOffset;
|
|
|
|
|
|
// float freq = Frequency, amp = 1;
|
|
|
|
|
|
// float sum = 0;
|
|
|
|
|
|
// var p = float4(xf, yf, zf, iv);
|
|
|
|
|
|
// for(int o = 0; o < Octaves; o++)
|
|
|
|
|
|
// {
|
|
|
|
|
|
// sum += (pnoise(p * freq, 1 / freq) + 1) / 2 * amp;
|
|
|
|
|
|
// freq *= Lacunarity;
|
|
|
|
|
|
// amp *= Gain;
|
|
|
|
|
|
// }
|
|
|
|
|
|
//
|
|
|
|
|
|
// samples[index] = Color.white * sum;
|
|
|
|
|
|
// }
|
|
|
|
|
|
// }
|
|
|
|
|
|
// }
|
|
|
|
|
|
// tex.SetPixels(samples);
|
|
|
|
|
|
// tex.Apply();
|
|
|
|
|
|
// _noiseTextures[i] = tex;
|
|
|
|
|
|
// }
|
2020-11-27 22:47:33 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[ImageEffectOpaque]
|
|
|
|
|
|
void OnRenderImage(RenderTexture source, RenderTexture destination)
|
|
|
|
|
|
{
|
|
|
|
|
|
// check for no shader / shader compile error
|
|
|
|
|
|
if( _volMaterial == null )
|
|
|
|
|
|
{
|
|
|
|
|
|
Graphics.Blit(source, destination);
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
2021-02-26 19:01:47 +00:00
|
|
|
|
|
|
|
|
|
|
// _currentNoiseTexture = (_currentNoiseTexture + 1) % NoiseTextureCount;
|
|
|
|
|
|
//
|
|
|
|
|
|
// _volMaterial.SetTexture("_NoiseTex", _noiseTextures[_currentNoiseTexture]);
|
2020-11-27 22:47:33 +00:00
|
|
|
|
|
|
|
|
|
|
// Shader needs this matrix to generate world space rays
|
|
|
|
|
|
_volMaterial.SetMatrix("_CamProj", (_camera.projectionMatrix * _camera.worldToCameraMatrix).inverse);
|
2020-12-01 00:16:11 +00:00
|
|
|
|
_volMaterial.SetMatrix("_CamInvProj", (_camera.projectionMatrix * _camera.worldToCameraMatrix).inverse);
|
2020-11-27 22:47:33 +00:00
|
|
|
|
|
|
|
|
|
|
// Shader needs to know the position and scale of cameras used to render input textures
|
|
|
|
|
|
if(GridTransform != null)
|
|
|
|
|
|
_volMaterial.SetVector("_GridTransform", new Vector4(GridTransform.position.x,GridTransform.position.z,GridTransform.localScale.x*4));
|
|
|
|
|
|
|
|
|
|
|
|
Graphics.Blit( source, destination, _volMaterial, 0 );
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|