parallax視差腳本
在環(huán)境的每個(gè)層上都有一個(gè)parallaxlayer的腳本控制視差效果。內(nèi)容很簡(jiǎn)單:
void Start()
{
cameraTransform = Camera.main.transform;
startCameraPos = cameraTransform.position;
startPos = transform.position;
}
private void LateUpdate()
{
var position = startPos;
if (horizontalOnly)
position.x += multiplier * (cameraTransform.position.x - startCameraPos.x);
else
position += multiplier * (cameraTransform.position - startCameraPos);
transform.position = position;
}
原理是記下相機(jī)的初始位置,然后物體的位移等于相機(jī)位移乘以一個(gè)系數(shù)。越近移動(dòng)就越小。遠(yuǎn)景的系數(shù)為正,中景是沒有這個(gè)腳本的。近景的系數(shù)為負(fù)數(shù)贬养。從左側(cè)的圖中可以看到圖像的移動(dòng)情況。
Sprite Shape
參考:https://docs.unity3d.com/Packages/com.unity.2d.spriteshape@2.1/manual/index.html
Sprite Shape 是創(chuàng)建2D場(chǎng)景的強(qiáng)大工具琴庵。由兩部分構(gòu)成:profile和controller误算。Profile中包含要用的sprite和角度等設(shè)定,在Controller中可以編輯曲線迷殿。
對(duì)于要使用的Sprite
- Texture類型為Sprite
- 模式為Single(經(jīng)實(shí)驗(yàn)儿礼,multiple也行,在其中選擇你需要的那個(gè)就行了)
- Mesh Type為Full Rectangle
Sprite Shape工作流
Create Sprite Shapes in following the steps:
- Create a Sprite Shape Profile from the main menu (menu: Assets > Create > Sprite Shape Profile). Select from the two available options:
- Create Angle Ranges and assign Sprites in the Sprite Shape Profile.
- Drag the Sprite Shape Profile into the Scene to automatically generate a Sprite ShapeGameObject based on that Profile.
- You can create a Sprite Shape GameObject without a Profile from the main menu (menu: GameObject > 2D Object > Sprite Shape). Then select a Sprite Shape Profile in the Sprite Shape Controller's Profile settings. The same Profile can be used by multiple Sprite Shapes.
- Edit the outline of the Sprite Shape with the Sprite Shape Controller component settings.
- Enable Physics2D interactions for your Sprite Shapes by attaching a Collider component.
創(chuàng)建Profile時(shí)庆寺,Open和Close隨便創(chuàng)建一個(gè)就行蚊夫,區(qū)別在于角度不同。創(chuàng)建后設(shè)置一下填充的Texture和邊界的Sprite懦尝,然后拖入場(chǎng)景知纷,在它的Controller中編輯Spline就可以了。
花草
Background中的Near物體花草都使用了Sprite Shape陵霉。
其中琅轧,花作為草的子物體,還附著了一個(gè)ConformingSpline的腳本踊挠,用來在父對(duì)象(也就是草地)更新的時(shí)候(檢查hashCode是否發(fā)生改變)鹰晨,更新自身的Spline。具體原理就是找到Controller的Spline止毕,生成序列化對(duì)象后更新其m_spline屬性。
最后漠趁,注意花和草使用的材質(zhì)中修改了RenderQueue扁凛。關(guān)于這個(gè)的用途,是渲染的順序闯传,但并不是說順序越靠后的最后渲染谨朝。關(guān)于什么物體在上什么在下還是由Z-Order,也就是物體離攝像機(jī)的遠(yuǎn)近決定的,只有在Shader決定禁用z-order的情況下字币,RenderQueue才會(huì)決定哪個(gè)物體渲染在上则披。參見:https://forum.unity.com/threads/how-does-renderqueue-work.98369/
AutoMask_Lit
看一下某些草和柱子使用的這個(gè)shader。
在顏色方面洗出,它添加了白平衡節(jié)點(diǎn)士复,用前兩個(gè)參數(shù)可以調(diào)節(jié)色溫和色彩。在mask方面翩活,它首先用一個(gè)0飽和度獲得灰度圖像阱洪,與Alpha相乘得到圖像的非透明區(qū)域。然后用Mask Strength控制mask的力度菠镇,用Mask Contrast控制mask的對(duì)比度冗荸。
目前還沒有找到Sprite Lit的文檔,git上顯示為404. @_@利耍。但是這個(gè)mask應(yīng)該可以用來實(shí)現(xiàn)光照在上面時(shí)候的光斑蚌本。
看下圖:
當(dāng)調(diào)整mask 的強(qiáng)度的時(shí)候,結(jié)合上后期處理的效果隘梨,就會(huì)產(chǎn)生極強(qiáng)的光斑效果: