NGraphics.cs 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using FairyGUI.Utils;
  5. using Object = UnityEngine.Object;
  6. namespace FairyGUI
  7. {
  8. /// <summary>
  9. ///
  10. /// </summary>
  11. public class NGraphics : IMeshFactory
  12. {
  13. /// <summary>
  14. ///
  15. /// </summary>
  16. public GameObject gameObject { get; private set; }
  17. /// <summary>
  18. ///
  19. /// </summary>
  20. public MeshFilter meshFilter { get; private set; }
  21. /// <summary>
  22. ///
  23. /// </summary>
  24. public MeshRenderer meshRenderer { get; private set; }
  25. /// <summary>
  26. ///
  27. /// </summary>
  28. public Mesh mesh { get; private set; }
  29. /// <summary>
  30. ///
  31. /// </summary>
  32. public BlendMode blendMode;
  33. /// <summary>
  34. /// 不参与剪裁
  35. /// </summary>
  36. public bool dontClip;
  37. /// <summary>
  38. /// 当Mesh更新时触发
  39. /// </summary>
  40. public event Action meshModifier;
  41. NTexture _texture;
  42. string _shader;
  43. Material _material;
  44. int _customMatarial; //0-none, 1-common, 2-support internal mask, 128-owns material
  45. MaterialManager _manager;
  46. string[] _shaderKeywords;
  47. int _materialFlags;
  48. IMeshFactory _meshFactory;
  49. float _alpha;
  50. Color _color;
  51. bool _meshDirty;
  52. Rect _contentRect;
  53. FlipType _flip;
  54. public class VertexMatrix
  55. {
  56. public Vector3 cameraPos;
  57. public Matrix4x4 matrix;
  58. }
  59. VertexMatrix _vertexMatrix;
  60. bool hasAlphaBackup;
  61. List<byte> _alphaBackup; //透明度改变需要通过修改顶点颜色实现,但顶点颜色本身可能就带有透明度,所以这里要有一个备份
  62. internal int _maskFlag;
  63. StencilEraser _stencilEraser;
  64. #if !UNITY_5_6_OR_NEWER
  65. Color32[] _colors;
  66. #endif
  67. MaterialPropertyBlock _propertyBlock;
  68. bool _blockUpdated;
  69. /// <summary>
  70. ///
  71. /// </summary>
  72. /// <param name="gameObject"></param>
  73. public NGraphics(GameObject gameObject)
  74. {
  75. this.gameObject = gameObject;
  76. _alpha = 1f;
  77. _shader = ShaderConfig.imageShader;
  78. _color = Color.white;
  79. _meshFactory = this;
  80. meshFilter = gameObject.AddComponent<MeshFilter>();
  81. meshRenderer = gameObject.AddComponent<MeshRenderer>();
  82. meshRenderer.shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.Off;
  83. meshRenderer.reflectionProbeUsage = UnityEngine.Rendering.ReflectionProbeUsage.Off;
  84. meshRenderer.receiveShadows = false;
  85. mesh = new Mesh();
  86. mesh.name = gameObject.name;
  87. mesh.MarkDynamic();
  88. meshFilter.mesh = mesh;
  89. meshFilter.hideFlags = DisplayObject.hideFlags;
  90. meshRenderer.hideFlags = DisplayObject.hideFlags;
  91. mesh.hideFlags = DisplayObject.hideFlags;
  92. Stats.LatestGraphicsCreation++;
  93. }
  94. /// <summary>
  95. ///
  96. /// </summary>
  97. public IMeshFactory meshFactory
  98. {
  99. get { return _meshFactory; }
  100. set
  101. {
  102. if (_meshFactory != value)
  103. {
  104. _meshFactory = value;
  105. _meshDirty = true;
  106. }
  107. }
  108. }
  109. /// <summary>
  110. ///
  111. /// </summary>
  112. /// <typeparam name="T"></typeparam>
  113. /// <returns></returns>
  114. public T GetMeshFactory<T>() where T : IMeshFactory, new()
  115. {
  116. if (!(_meshFactory is T))
  117. {
  118. _meshFactory = new T();
  119. _meshDirty = true;
  120. }
  121. return (T)_meshFactory;
  122. }
  123. /// <summary>
  124. ///
  125. /// </summary>
  126. public Rect contentRect
  127. {
  128. get { return _contentRect; }
  129. set
  130. {
  131. _contentRect = value;
  132. _meshDirty = true;
  133. }
  134. }
  135. /// <summary>
  136. ///
  137. /// </summary>
  138. public FlipType flip
  139. {
  140. get { return _flip; }
  141. set
  142. {
  143. if (_flip != value)
  144. {
  145. _flip = value;
  146. _meshDirty = true;
  147. }
  148. }
  149. }
  150. /// <summary>
  151. ///
  152. /// </summary>
  153. public NTexture texture
  154. {
  155. get { return _texture; }
  156. set
  157. {
  158. if (_texture != value)
  159. {
  160. if (value != null)
  161. value.AddRef();
  162. if (_texture != null)
  163. _texture.ReleaseRef();
  164. _texture = value;
  165. if (_customMatarial != 0 && _material != null)
  166. _material.mainTexture = _texture != null ? _texture.nativeTexture : null;
  167. _meshDirty = true;
  168. UpdateManager();
  169. }
  170. }
  171. }
  172. /// <summary>
  173. ///
  174. /// </summary>
  175. public string shader
  176. {
  177. get { return _shader; }
  178. set
  179. {
  180. _shader = value;
  181. UpdateManager();
  182. }
  183. }
  184. /// <summary>
  185. ///
  186. /// </summary>
  187. /// <param name="shader"></param>
  188. /// <param name="texture"></param>
  189. public void SetShaderAndTexture(string shader, NTexture texture)
  190. {
  191. _shader = shader;
  192. if (_texture != texture)
  193. this.texture = texture;
  194. else
  195. UpdateManager();
  196. }
  197. /// <summary>
  198. ///
  199. /// </summary>
  200. public Material material
  201. {
  202. get
  203. {
  204. if (_customMatarial == 0 && _material == null && _manager != null)
  205. _material = _manager.GetMaterial(_materialFlags, blendMode, 0);
  206. return _material;
  207. }
  208. set
  209. {
  210. if ((_customMatarial & 128) != 0 && _material != null)
  211. Object.DestroyImmediate(_material);
  212. _material = value;
  213. if (_material != null)
  214. {
  215. _customMatarial = 1;
  216. if (_material.HasProperty(ShaderConfig.ID_Stencil) || _material.HasProperty(ShaderConfig.ID_ClipBox))
  217. _customMatarial |= 2;
  218. meshRenderer.sharedMaterial = _material;
  219. if (_texture != null)
  220. _material.mainTexture = _texture.nativeTexture;
  221. }
  222. else
  223. {
  224. _customMatarial = 0;
  225. meshRenderer.sharedMaterial = null;
  226. }
  227. }
  228. }
  229. /// <summary>
  230. /// Same as material property except that ownership is transferred to this object.
  231. /// </summary>
  232. /// <param name="material"></param>
  233. public void SetMaterial(Material material)
  234. {
  235. this.material = material;
  236. _customMatarial |= 128;
  237. }
  238. /// <summary>
  239. ///
  240. /// </summary>
  241. public string[] materialKeywords
  242. {
  243. get { return _shaderKeywords; }
  244. set
  245. {
  246. _shaderKeywords = value;
  247. UpdateMaterialFlags();
  248. }
  249. }
  250. /// <summary>
  251. ///
  252. /// </summary>
  253. /// <param name="keyword"></param>
  254. /// <param name="enabled"></param>
  255. public void ToggleKeyword(string keyword, bool enabled)
  256. {
  257. if (enabled)
  258. {
  259. if (_shaderKeywords == null)
  260. {
  261. _shaderKeywords = new string[] { keyword };
  262. UpdateMaterialFlags();
  263. }
  264. else if (Array.IndexOf(_shaderKeywords, keyword) == -1)
  265. {
  266. Array.Resize(ref _shaderKeywords, _shaderKeywords.Length + 1);
  267. _shaderKeywords[_shaderKeywords.Length - 1] = keyword;
  268. UpdateMaterialFlags();
  269. }
  270. }
  271. else
  272. {
  273. if (_shaderKeywords != null)
  274. {
  275. int i = Array.IndexOf(_shaderKeywords, keyword);
  276. if (i != -1)
  277. {
  278. _shaderKeywords[i] = null;
  279. UpdateMaterialFlags();
  280. }
  281. }
  282. }
  283. }
  284. void UpdateManager()
  285. {
  286. if (_texture != null)
  287. _manager = _texture.GetMaterialManager(_shader);
  288. else
  289. _manager = null;
  290. UpdateMaterialFlags();
  291. }
  292. void UpdateMaterialFlags()
  293. {
  294. if (_customMatarial != 0)
  295. {
  296. if (material != null)
  297. material.shaderKeywords = _shaderKeywords;
  298. }
  299. else if (_shaderKeywords != null && _manager != null)
  300. _materialFlags = _manager.GetFlagsByKeywords(_shaderKeywords);
  301. else
  302. _materialFlags = 0;
  303. }
  304. /// <summary>
  305. ///
  306. /// </summary>
  307. public bool enabled
  308. {
  309. get { return meshRenderer.enabled; }
  310. set { meshRenderer.enabled = value; }
  311. }
  312. /// <summary>
  313. ///
  314. /// </summary>
  315. public int sortingOrder
  316. {
  317. get { return meshRenderer.sortingOrder; }
  318. set { meshRenderer.sortingOrder = value; }
  319. }
  320. /// <summary>
  321. ///
  322. /// </summary>
  323. /// <param name="value"></param>
  324. internal void _SetStencilEraserOrder(int value)
  325. {
  326. _stencilEraser.meshRenderer.sortingOrder = value;
  327. }
  328. /// <summary>
  329. ///
  330. /// </summary>
  331. /// <param name="value"></param>
  332. public Color color
  333. {
  334. get { return _color; }
  335. set { _color = value; }
  336. }
  337. /// <summary>
  338. ///
  339. /// </summary>
  340. public void Tint()
  341. {
  342. if (_meshDirty)
  343. return;
  344. int vertCount = mesh.vertexCount;
  345. if (vertCount == 0)
  346. return;
  347. #if !UNITY_5_6_OR_NEWER
  348. Color32[] colors = _colors;
  349. if (colors == null)
  350. colors = mesh.colors32;
  351. #else
  352. VertexBuffer vb = VertexBuffer.Begin();
  353. mesh.GetColors(vb.colors);
  354. List<Color32> colors = vb.colors;
  355. #endif
  356. for (int i = 0; i < vertCount; i++)
  357. {
  358. Color32 col = _color;
  359. col.a = (byte)(_alpha * (hasAlphaBackup ? _alphaBackup[i] : (byte)255));
  360. colors[i] = col;
  361. }
  362. #if !UNITY_5_6_OR_NEWER
  363. mesh.colors32 = colors;
  364. #else
  365. mesh.SetColors(vb.colors);
  366. vb.End();
  367. #endif
  368. }
  369. void ChangeAlpha(float value)
  370. {
  371. _alpha = value;
  372. int vertCount = mesh.vertexCount;
  373. if (vertCount == 0)
  374. return;
  375. #if !UNITY_5_6_OR_NEWER
  376. Color32[] colors = _colors;
  377. if (colors == null)
  378. colors = mesh.colors32;
  379. #else
  380. VertexBuffer vb = VertexBuffer.Begin();
  381. mesh.GetColors(vb.colors);
  382. List<Color32> colors = vb.colors;
  383. #endif
  384. for (int i = 0; i < vertCount; i++)
  385. {
  386. Color32 col = colors[i];
  387. col.a = (byte)(_alpha * (hasAlphaBackup ? _alphaBackup[i] : (byte)255));
  388. colors[i] = col;
  389. }
  390. #if !UNITY_5_6_OR_NEWER
  391. mesh.colors32 = colors;
  392. #else
  393. mesh.SetColors(vb.colors);
  394. vb.End();
  395. #endif
  396. }
  397. /// <summary>
  398. ///
  399. /// </summary>
  400. public VertexMatrix vertexMatrix
  401. {
  402. get { return _vertexMatrix; }
  403. set
  404. {
  405. _vertexMatrix = value;
  406. _meshDirty = true;
  407. }
  408. }
  409. /// <summary>
  410. ///
  411. /// </summary>
  412. /// <returns></returns>
  413. public MaterialPropertyBlock materialPropertyBlock
  414. {
  415. get
  416. {
  417. if (_propertyBlock == null)
  418. _propertyBlock = new MaterialPropertyBlock();
  419. _blockUpdated = true;
  420. return _propertyBlock;
  421. }
  422. }
  423. /// <summary>
  424. ///
  425. /// </summary>
  426. public void SetMeshDirty()
  427. {
  428. _meshDirty = true;
  429. }
  430. /// <summary>
  431. ///
  432. /// </summary>
  433. /// <returns></returns>
  434. public bool UpdateMesh()
  435. {
  436. if (_meshDirty)
  437. {
  438. UpdateMeshNow();
  439. return true;
  440. }
  441. else
  442. return false;
  443. }
  444. /// <summary>
  445. ///
  446. /// </summary>
  447. public void Dispose()
  448. {
  449. if (mesh != null)
  450. {
  451. if (Application.isPlaying)
  452. Object.Destroy(mesh);
  453. else
  454. Object.DestroyImmediate(mesh);
  455. mesh = null;
  456. }
  457. if ((_customMatarial & 128) != 0 && _material != null)
  458. Object.DestroyImmediate(_material);
  459. if (_texture != null)
  460. {
  461. _texture.ReleaseRef();
  462. _texture = null;
  463. }
  464. _manager = null;
  465. _material = null;
  466. meshRenderer = null;
  467. meshFilter = null;
  468. _stencilEraser = null;
  469. meshModifier = null;
  470. }
  471. /// <summary>
  472. ///
  473. /// </summary>
  474. /// <param name="context"></param>
  475. /// <param name="alpha"></param>
  476. /// <param name="grayed"></param>
  477. public void Update(UpdateContext context, float alpha, bool grayed)
  478. {
  479. Stats.GraphicsCount++;
  480. if (_meshDirty)
  481. {
  482. _alpha = alpha;
  483. UpdateMeshNow();
  484. }
  485. else if (_alpha != alpha)
  486. ChangeAlpha(alpha);
  487. if (_propertyBlock != null && _blockUpdated)
  488. {
  489. meshRenderer.SetPropertyBlock(_propertyBlock);
  490. _blockUpdated = false;
  491. }
  492. if (_customMatarial != 0)
  493. {
  494. if ((_customMatarial & 2) != 0 && _material != null)
  495. context.ApplyClippingProperties(_material, false);
  496. }
  497. else
  498. {
  499. if (_manager != null)
  500. {
  501. if (_maskFlag == 1)
  502. {
  503. _material = _manager.GetMaterial((int)MaterialFlags.AlphaMask | _materialFlags, BlendMode.Normal, context.clipInfo.clipId);
  504. context.ApplyAlphaMaskProperties(_material, false);
  505. }
  506. else
  507. {
  508. int matFlags = _materialFlags;
  509. if (grayed)
  510. matFlags |= (int)MaterialFlags.Grayed;
  511. if (context.clipped)
  512. {
  513. if (context.stencilReferenceValue > 0)
  514. matFlags |= (int)MaterialFlags.StencilTest;
  515. if (context.rectMaskDepth > 0)
  516. {
  517. if (context.clipInfo.soft)
  518. matFlags |= (int)MaterialFlags.SoftClipped;
  519. else
  520. matFlags |= (int)MaterialFlags.Clipped;
  521. }
  522. _material = _manager.GetMaterial(matFlags, blendMode, context.clipInfo.clipId);
  523. if (_manager.firstMaterialInFrame)
  524. context.ApplyClippingProperties(_material, true);
  525. }
  526. else
  527. _material = _manager.GetMaterial(matFlags, blendMode, 0);
  528. }
  529. }
  530. else
  531. _material = null;
  532. if (!Material.ReferenceEquals(_material, meshRenderer.sharedMaterial))
  533. meshRenderer.sharedMaterial = _material;
  534. }
  535. if (_maskFlag != 0)
  536. {
  537. if (_maskFlag == 1)
  538. _maskFlag = 2;
  539. else
  540. {
  541. if (_stencilEraser != null)
  542. _stencilEraser.enabled = false;
  543. _maskFlag = 0;
  544. }
  545. }
  546. }
  547. internal void _PreUpdateMask(UpdateContext context, uint maskId)
  548. {
  549. //_maskFlag: 0-new mask, 1-active mask, 2-mask complete
  550. if (_maskFlag == 0)
  551. {
  552. if (_stencilEraser == null)
  553. {
  554. _stencilEraser = new StencilEraser(gameObject.transform);
  555. _stencilEraser.meshFilter.mesh = mesh;
  556. }
  557. else
  558. _stencilEraser.enabled = true;
  559. }
  560. _maskFlag = 1;
  561. if (_manager != null)
  562. {
  563. //这里使用maskId而不是clipInfo.clipId,是因为遮罩有两个用途,一个是写入遮罩,一个是擦除,两个不能用同一个材质
  564. Material mat = _manager.GetMaterial((int)MaterialFlags.AlphaMask | _materialFlags, BlendMode.Normal, maskId);
  565. if (!Material.ReferenceEquals(mat, _stencilEraser.meshRenderer.sharedMaterial))
  566. _stencilEraser.meshRenderer.sharedMaterial = mat;
  567. context.ApplyAlphaMaskProperties(mat, true);
  568. }
  569. }
  570. void UpdateMeshNow()
  571. {
  572. _meshDirty = false;
  573. if (_texture == null || _meshFactory == null)
  574. {
  575. if (mesh.vertexCount > 0)
  576. {
  577. mesh.Clear();
  578. if (meshModifier != null)
  579. meshModifier();
  580. }
  581. return;
  582. }
  583. VertexBuffer vb = VertexBuffer.Begin();
  584. vb.contentRect = _contentRect;
  585. vb.uvRect = _texture.uvRect;
  586. if (_texture != null)
  587. vb.textureSize = new Vector2(_texture.width, _texture.height);
  588. else
  589. vb.textureSize = new Vector2(0, 0);
  590. if (_flip != FlipType.None)
  591. {
  592. if (_flip == FlipType.Horizontal || _flip == FlipType.Both)
  593. {
  594. float tmp = vb.uvRect.xMin;
  595. vb.uvRect.xMin = vb.uvRect.xMax;
  596. vb.uvRect.xMax = tmp;
  597. }
  598. if (_flip == FlipType.Vertical || _flip == FlipType.Both)
  599. {
  600. float tmp = vb.uvRect.yMin;
  601. vb.uvRect.yMin = vb.uvRect.yMax;
  602. vb.uvRect.yMax = tmp;
  603. }
  604. }
  605. vb.vertexColor = _color;
  606. _meshFactory.OnPopulateMesh(vb);
  607. int vertCount = vb.currentVertCount;
  608. if (vertCount == 0)
  609. {
  610. if (mesh.vertexCount > 0)
  611. {
  612. mesh.Clear();
  613. if (meshModifier != null)
  614. meshModifier();
  615. }
  616. vb.End();
  617. return;
  618. }
  619. if (_texture.rotated)
  620. {
  621. float xMin = _texture.uvRect.xMin;
  622. float yMin = _texture.uvRect.yMin;
  623. float yMax = _texture.uvRect.yMax;
  624. float tmp;
  625. for (int i = 0; i < vertCount; i++)
  626. {
  627. Vector2 vec = vb.uvs[i];
  628. tmp = vec.y;
  629. vec.y = yMin + vec.x - xMin;
  630. vec.x = xMin + yMax - tmp;
  631. vb.uvs[i] = vec;
  632. }
  633. }
  634. hasAlphaBackup = vb._alphaInVertexColor;
  635. if (hasAlphaBackup)
  636. {
  637. if (_alphaBackup == null)
  638. _alphaBackup = new List<byte>();
  639. else
  640. _alphaBackup.Clear();
  641. for (int i = 0; i < vertCount; i++)
  642. {
  643. Color32 col = vb.colors[i];
  644. _alphaBackup.Add(col.a);
  645. col.a = (byte)(col.a * _alpha);
  646. vb.colors[i] = col;
  647. }
  648. }
  649. else if (_alpha != 1)
  650. {
  651. for (int i = 0; i < vertCount; i++)
  652. {
  653. Color32 col = vb.colors[i];
  654. col.a = (byte)(col.a * _alpha);
  655. vb.colors[i] = col;
  656. }
  657. }
  658. if (_vertexMatrix != null)
  659. {
  660. Vector3 camPos = _vertexMatrix.cameraPos;
  661. Vector3 center = new Vector3(camPos.x, camPos.y, 0);
  662. center -= _vertexMatrix.matrix.MultiplyPoint(center);
  663. for (int i = 0; i < vertCount; i++)
  664. {
  665. Vector3 pt = vb.vertices[i];
  666. pt = _vertexMatrix.matrix.MultiplyPoint(pt);
  667. pt += center;
  668. Vector3 vec = pt - camPos;
  669. float lambda = -camPos.z / vec.z;
  670. pt.x = camPos.x + lambda * vec.x;
  671. pt.y = camPos.y + lambda * vec.y;
  672. pt.z = 0;
  673. vb.vertices[i] = pt;
  674. }
  675. }
  676. mesh.Clear();
  677. #if UNITY_5_2 || UNITY_5_3_OR_NEWER
  678. mesh.SetVertices(vb.vertices);
  679. if (vb._isArbitraryQuad)
  680. mesh.SetUVs(0, vb.FixUVForArbitraryQuad());
  681. else
  682. mesh.SetUVs(0, vb.uvs);
  683. mesh.SetColors(vb.colors);
  684. mesh.SetTriangles(vb.triangles, 0);
  685. if (vb.uvs2.Count == vb.uvs.Count)
  686. mesh.SetUVs(1, vb.uvs2);
  687. #if !UNITY_5_6_OR_NEWER
  688. _colors = null;
  689. #endif
  690. #else
  691. Vector3[] vertices = new Vector3[vertCount];
  692. Vector2[] uv = new Vector2[vertCount];
  693. _colors = new Color32[vertCount];
  694. int[] triangles = new int[vb.triangles.Count];
  695. vb.vertices.CopyTo(vertices);
  696. vb.uvs.CopyTo(uv);
  697. vb.colors.CopyTo(_colors);
  698. vb.triangles.CopyTo(triangles);
  699. mesh.vertices = vertices;
  700. mesh.uv = uv;
  701. mesh.triangles = triangles;
  702. mesh.colors32 = _colors;
  703. if(vb.uvs2.Count==uv.Length)
  704. {
  705. uv = new Vector2[vertCount];
  706. vb.uvs2.CopyTo(uv);
  707. mesh.uv2 = uv;
  708. }
  709. #endif
  710. vb.End();
  711. if (meshModifier != null)
  712. meshModifier();
  713. }
  714. public void OnPopulateMesh(VertexBuffer vb)
  715. {
  716. Rect rect = texture.GetDrawRect(vb.contentRect);
  717. vb.AddQuad(rect, vb.vertexColor, vb.uvRect);
  718. vb.AddTriangles();
  719. vb._isArbitraryQuad = _vertexMatrix != null;
  720. }
  721. class StencilEraser
  722. {
  723. public GameObject gameObject;
  724. public MeshFilter meshFilter;
  725. public MeshRenderer meshRenderer;
  726. public StencilEraser(Transform parent)
  727. {
  728. gameObject = new GameObject("StencilEraser");
  729. gameObject.transform.SetParent(parent, false);
  730. meshFilter = gameObject.AddComponent<MeshFilter>();
  731. meshRenderer = gameObject.AddComponent<MeshRenderer>();
  732. meshRenderer.shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.Off;
  733. meshRenderer.reflectionProbeUsage = UnityEngine.Rendering.ReflectionProbeUsage.Off;
  734. meshRenderer.receiveShadows = false;
  735. gameObject.layer = parent.gameObject.layer;
  736. gameObject.hideFlags = parent.gameObject.hideFlags;
  737. meshFilter.hideFlags = parent.gameObject.hideFlags;
  738. meshRenderer.hideFlags = parent.gameObject.hideFlags;
  739. }
  740. public bool enabled
  741. {
  742. get { return meshRenderer.enabled; }
  743. set { meshRenderer.enabled = value; }
  744. }
  745. }
  746. }
  747. }