using System.Collections.Generic;
using UnityEngine;
public class Example : MonoBehaviour
{
public List<GameObject> prefabs = new List<GameObject>();
private void Update()
{
Spawn();
}
private void Spawn()
{
var prefab = prefabs[Random.Range(0, prefabs.Count)];
Instantiate(prefab);
}
}
using UnityEngine;
public class Example : MonoBehaviour
{
public Vector3 fromPosition = Vector3.zero;
public Vector3 toPosition = Vector3.one;
public float speed = 1;
private float progress;
private void Update()
{
progress += Time.deltaTime*speed;
transform.position = Vector3.Lerp(fromPosition, toPosition, progress);
}
}
Shader "Projector/Multiply"
{
Properties
{
_ShadowTex("Cookie", 2D) = "gray" {}
_FalloffTex("FallOff", 2D) = "white" {}
// Добавляем свойство
_MaskTex("Mask", 2D) = "white" {}
}
Subshader
{
Tags {"Queue"="Transparent"}
Pass
{
ZWrite Off
ColorMask RGB
Blend DstColor Zero
Offset -1, -1
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma multi_compile_fog
#include "UnityCG.cginc"
struct v2f
{
float4 uvShadow : TEXCOORD0;
float4 uvFalloff : TEXCOORD1;
UNITY_FOG_COORDS(2)
float4 pos : SV_POSITION;
};
float4x4 unity_Projector;
float4x4 unity_ProjectorClip;
v2f vert (float4 vertex : POSITION)
{
v2f o;
o.pos = UnityObjectToClipPos(vertex);
o.uvShadow = mul(unity_Projector, vertex);
o.uvFalloff = mul(unity_ProjectorClip, vertex);
UNITY_TRANSFER_FOG(o,o.pos);
return o;
}
sampler2D _ShadowTex;
sampler2D _FalloffTex;
// Добавляем переменную, чтобы использовать текстуру внутри прохода
sampler2D _MaskTex;
fixed4 frag (v2f i) : SV_Target
{
fixed4 texS = tex2Dproj(_ShadowTex, UNITY_PROJ_COORD(i.uvShadow));
texS.a = 1.0 - texS.a;
fixed4 texF = tex2Dproj(_FalloffTex, UNITY_PROJ_COORD(i.uvFalloff));
fixed4 res = lerp(fixed4(1, 1, 1, 0), texS, texF.a);
// Сэмплируем текстуру
fixed4 mask = tex2Dproj(_MaskTex, UNITY_PROJ_COORD(i.uvShadow));
// Применяем маску
res *= mask;
UNITY_APPLY_FOG_COLOR(i.fogCoord, res, fixed4(1, 1, 1, 1));
return res;
}
ENDCG
}
}
}
fixed4 frag (v2f i) : SV_Target
{
float2 uvShadow = UNITY_PROJ_COORD(i.uvShadow);
float2 uvFalloff = UNITY_PROJ_COORD(i.uvFalloff);
return fixed4(uvShadow, 0.0, 1.0);
//return fixed4(uvFalloff, 0.0, 1.0);
}
/// <summary>
/// Knapsack problem solver for items with equal value
/// </summary>
/// <typeparam name="T">Item identificator</typeparam>
/// <param name="set">
/// Set of items where key <typeparamref name="T"/> is item identificator and value is item weight</param>
/// <param name="capacity">Maximum weight</param>
/// <param name="knapsack">Pre-filled knapsack</param>
/// <returns>
/// Filled knapsack where values are number of items of type key.
/// Tends to overload knapsack: fills remainder with one smallest item.</returns>
/// <remarks>
/// https://en.wikipedia.org/wiki/Knapsack_problem
/// </remarks>
public static Dictionary<T, int> Knapsack<T>(Dictionary<T, float> set, float capacity,
Dictionary<T, int> knapsack = null)
{
var keys = new List<T>(set.Keys);
// Sort keys by their weights in descending order
keys.Sort((a, b) => -set[a].CompareTo(set[b]));
if (knapsack == null)
{
knapsack = new Dictionary<T, int>();
foreach (var key in keys)
{
knapsack[key] = 0;
}
}
return Knapsack(set, keys, capacity, knapsack, 0);
}
private static Dictionary<T, int> Knapsack<T>(Dictionary<T, float> set, List<T> keys, float remainder,
Dictionary<T, int> knapsack, int startIndex)
{
T smallestKey = keys[keys.Count - 1];
if (remainder < set[smallestKey])
{
knapsack[smallestKey] = 1;
return knapsack;
}
// Cycle through items and try to put them in knapsack
for (var i = startIndex; i < keys.Count; i++)
{
T key = keys[i];
float weight = set[key];
// Larger items won't fit, smaller items will fill as much space as they can
knapsack[key] += (int) (remainder/weight);
remainder %= weight;
}
if (remainder > 0)
{
// Throw out largest item and try again
for (var i = 0; i < keys.Count; i++)
{
T key = keys[i];
if (knapsack[key] != 0)
{
// Already tried every combination, return as is
if (key.Equals(smallestKey))
{
return knapsack;
}
knapsack[key]--;
remainder += set[key];
startIndex = i + 1;
break;
}
}
knapsack = Knapsack(set, keys, remainder, knapsack, startIndex);
}
return knapsack;
}