Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using UnityEngine;
- using NWH.DWP2.WaterObjects;
- using sc.modeling.river.runtime;
- namespace NWH.DWP2.WaterData
- {
- public class RiverWaterDataProvider : RaycastWaterDataProvider
- {
- [Min(0)]
- [Space]
- public float baseFlowStrength = 5f;
- private RiverModeler river;
- private int prevRiverHash;
- public override void Awake()
- {
- base.Awake();
- Physics.IgnoreLayerCollision(waterLayer, objectLayer);
- _rayDirection = -Vector3.up;
- _rayStartOffset = -_rayDirection * raycastDistance * 0.5f;
- _prevDataSize = -1;
- }
- private void Reset()
- {
- river = FindObjectOfType<RiverModeler>();
- if (river) waterLayer = river.gameObject.layer;
- }
- public override bool SupportsWaterFlowQueries()
- {
- return true;
- }
- private Vector2[] meshFlowData;
- private int[] meshTriangles;
- public override void GetWaterFlows(WaterObject waterObject, ref Vector3[] points, ref Vector3[] waterFlows)
- {
- _flow = Vector3.zero;
- bool queriesHitBackfaces = Physics.queriesHitBackfaces;
- Physics.queriesHitBackfaces = false;
- _ray.origin = waterObject.transform.position + _rayStartOffset;
- _ray.direction = _rayDirection;
- if (Physics.Raycast(_ray, out _hit, raycastDistance, _layerMask, QueryTriggerInteraction.Ignore) && _hit.collider != null)
- {
- river = _hit.collider.GetComponent<RiverModeler>();
- if (river && river.settings.uv.flowVectors)
- {
- _mesh = river.meshFilter.sharedMesh;
- //Check if the river being evaluated is the same as in last query
- int hashCode = river.GetHashCode();
- if (hashCode != prevRiverHash)
- {
- //Debug.Log($"[DWP2] Now sampling {river.gameObject.name}");
- prevRiverHash = hashCode;
- //Cache the Mesh data arrays, as accessing it directly from the mesh incurs unwanted allocations.
- meshFlowData = _mesh.uv3;
- meshTriangles = _mesh.triangles;
- }
- _vertIndex = meshTriangles[_hit.triangleIndex * 3];
- _uv4 = meshFlowData[_vertIndex];
- _vertDir.x = _uv4.x;
- _vertDir.y = 0;
- _vertDir.z = _uv4.y;
- _flow.x = _vertDir.x * baseFlowStrength;
- _flow.y = 0;
- _flow.z = _vertDir.z * baseFlowStrength;
- }
- for (int d = 0; d < waterFlows.Length; d++)
- {
- waterFlows[d] = _flow;
- }
- }
- Physics.queriesHitBackfaces = queriesHitBackfaces;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement