Unity Community


Page 9 of 47 FirstFirst ... 789101119 ... LastLast
Results 161 to 180 of 939

Thread: Decal System


  1. Posts
    2,871
    @rbarbosa: I have the impression that you are understanding the way the Decal System works very well. Without knowing more about the project and without having tested the performance of the Decal System on mobiles, it is impossible for me to give you a clear answer.
    Computing the decal in the editor would be the optimal case from a performance point of view, but that certainly doesn't work in all cases. So if you want to compute them at runtime, the performance heavily depends on the number of vertices/edges/triangles the ground has onto which you perform the projection.
    It is out of question that the computation of a decal is expensive, but with a simple ground geometry it may even work at runtime. But I can't promise anything.

    Very vague answer, I am aware of that. In the case that you are testing it, it would be awesome if you could share your experience!


  2. Posts
    37
    @Dantus...thanks for the quick reply. I wish I knew more about my project too.

    I don't actually have a project in mind...I'm just considering possibilities. I have downloaded the Decal system...and I will spend some time testing and playing around with it over the weekend...I will absolutely share my experience and right up a review for the product.

    Thanks again for the response!


  3. Location
    Germany | Hamburg
    Posts
    1,916
    hi rbarbosa,

    having a look at your special case i think a projected texture limited to a special plane would be the best solution.
    as projectors usually are very heavy to render it is absolutely necessary to limit it to this special plane which gets updated by the object’s position and the ground’s surface.
    as buildings should not be placed on rather uneven terrain i guess the plane does not have to be an accurate decal following excactly the terrain’s shape.
    the angry roots demo does so for the player’s ground shadow. just have a look at it!

    lars
    Last edited by larsbertram1; 09-12-2012 at 03:54 PM.
    Lux - open source physically based shader framework: http://forum.unity3d.com/threads/235027

    Advanced terrain shaders V2 [free]:http://u3d.as/content/forst/advanced...shaders-v2/2Bo
    Advanced mobile foliage shaders [free]: http://u3d.as/content/forst/ats-mobile-foliage/2XM
    ATS Colormap terrain shader [free]: http://u3d.as/content/forst/ats-colo...ain-shader/3fr
    ATS Snow Suite [free]: http://u3d.as/content/forst/ats-snow-suite/3fq


  4. Posts
    37
    @Lars...you make a great point. For the most part, the land around an RTS structure is normally made level before plotting the building itself. So adding a small plane around the base of the structure (or even a cube...less vertices than the standard Unity plane mesh) that has the appropriate material/texture would work and perhaps be less expensive than using a decal projection.

    Nonetheless...this is mostly just for my experimentation to play around with the Decal System.

    Thanks for the advice, however!


  5. Posts
    79
    hi!
    just implemented your system into my game : )

    the problem is that i get very short but noticeable "stops" or "delays" when creating the decal at runtime.
    my world mesh is quite highres. ~50k tris objects.

    is there a way to compute the decal over several frames?
    or any way?

    thank you!


  6. Posts
    2,871
    Quote Originally Posted by Lizzard4000 View Post
    hi!
    just implemented your system into my game : )

    the problem is that i get very short but noticeable "stops" or "delays" when creating the decal at runtime.
    my world mesh is quite highres. ~50k tris objects.

    is there a way to compute the decal over several frames?
    or any way?

    thank you!
    50k... 50k!

    Let me quickly explain you what happens internally. You have a decals mesh and a certain projector according to which the cutting will be made. All the mesh data is added to the decals mesh and after that the 6 cuts take place, before the decals mesh data is passed to as DS_Decals game object that internally passes it to the actual mesh renderer.
    My performance tests showed that the expensive steps are the copy of the mesh data into the decals mesh and the cutting. To my own surprise it did not make sense to split the 6 cuts, because some tend to be very expensive (often just one and sometimes two) while the others are nearly "free". For that reason I didn't provide such a split over several frames functionality. On the other hand my tests were only made with 10k vertices .

    I sincerely hope that you don't need this for a mobile platform . In that case there may be alternatives. Instead of using the 50k objects, you may have some other meshes or at least the data of them that are not actually displayed, but could be used for the decals. In that case you may use fewer data for the decals computation, which would significantly improve the performance!
    Usually not the whole mesh data is needed, but only the vertices, normals and triangles. It is possible to pass only that data to the decals mesh!

    I can't imagine that it is practically possible to work with 50k object at the moment.

    Hope this helps.


  7. Posts
    79
    hi! thx for the answer!

    i had to find, and found a different solution(no decal system, different approach) . so i cant use your system.

    but i must say. for 50k it was very fast! if my game wasnt that fast, i would not have noticed it!
    i will definitely come back to your system if i need a decal system in the future!

  8. DHA DHA is offline

    Posts
    2
    Hi, Dantus. I've been trying to implement your system into my game and encountered this problem: I'm trying to create decals at runtime and attach them to moving objects. As I've seen on your vid that I need to make the projector a child of that object so that they move together, but I can't access to the transform of the newly created l_DecalProjector ( as in your Bullet example ). Am I missing something here?


  9. Posts
    2,871
    Quote Originally Posted by DHA View Post
    Hi, Dantus. I've been trying to implement your system into my game and encountered this problem: I'm trying to create decals at runtime and attach them to moving objects. As I've seen on your vid that I need to make the projector a child of that object so that they move together, but I can't access to the transform of the newly created l_DecalProjector ( as in your Bullet example ). Am I missing something here?
    DecalProjectors have no transform, but you can access the position directly. In that case that this doesn't help you, could you post your code here?

  10. DHA DHA is offline

    Posts
    2
    Thanks for your reply, Dantus. I solved my problem by using DS_DecalProjector instead.


  11. Posts
    2,871
    Quote Originally Posted by DHA View Post
    Thanks for your reply, Dantus. I solved my problem by using DS_DecalProjector instead.
    Great! Just for your information, it should also work with DecalProjector even if it has no transform.
    Last edited by Dantus; 09-20-2012 at 10:55 AM.


  12. Posts
    1,321
    Does this currently work with Unity 4? I'm getting this error when it tries to create a decal:

    (m_Channels[shaderChannelIndex].dimension != 0) == (((m_CurrentChannels & (1 << shaderChannelIndex)) != 0))
    UnityEngine.Mesh:set_normals(Vector3[])
    Edelweiss.DecalSystem.Decals:ApplyToDecalsMeshRend erer(DecalsMeshRenderer, DecalsMesh)
    Edelweiss.DecalSystem.Decals:UpdateDecalsMeshes(De calsMesh)

    I saw someone else asked about this error here: http://answers.unity3d.com/questions...-currentc.html

    Also, in the thread for ex2D, someone mentioned getting the error for their product in Unity 4 as well. It seems that Unity 4 made some change to the way that UV's and normals are set? Or it might just be a weird bug that they introduced, since the error message is pretty incomprehensible.


  13. Posts
    2,871
    Quote Originally Posted by makeshiftwings View Post
    Does this currently work with Unity 4? I'm getting this error when it tries to create a decal:

    (m_Channels[shaderChannelIndex].dimension != 0) == (((m_CurrentChannels & (1 << shaderChannelIndex)) != 0))
    UnityEngine.Mesh:set_normals(Vector3[])
    Edelweiss.DecalSystem.Decals:ApplyToDecalsMeshRend erer(DecalsMeshRenderer, DecalsMesh)
    Edelweiss.DecalSystem.Decals:UpdateDecalsMeshes(De calsMesh)

    I saw someone else asked about this error here: http://answers.unity3d.com/questions...-currentc.html

    Also, in the thread for ex2D, someone mentioned getting the error for their product in Unity 4 as well. It seems that Unity 4 made some change to the way that UV's and normals are set? Or it might just be a weird bug that they introduced, since the error message is pretty incomprehensible.
    You are absolutely right, it doesn't work properly in Unity 4 yet. I haven't decided yet how to go on. There are some changes in the pipeline and I haven't decided, if I want to make a Unity 4 update with or without the changes.
    But it is out of question that an compatible version is going to be available!


  14. Posts
    2
    Hi, I've started to use the decal system and it's quite amazing.
    I've been trying to modify the example to make it work with moving object but I was unable to do so correctly.

    I've attached a ds_decal gameObject as a child to the moving object and used the bulletExampleCS script with some slight modification to make it work with the moving object child ds_decal.

    When the object up vector is the same as the world up vector, the decal system works fine, but if the object up vector point to any other direction then decals are created only on some faces and not others.

    How can I make this works?


  15. Posts
    2,871
    Quote Originally Posted by ZuruSonaKitsune View Post
    Hi, I've started to use the decal system and it's quite amazing.
    I've been trying to modify the example to make it work with moving object but I was unable to do so correctly.

    I've attached a ds_decal gameObject as a child to the moving object and used the bulletExampleCS script with some slight modification to make it work with the moving object child ds_decal.

    When the object up vector is the same as the world up vector, the decal system works fine, but if the object up vector point to any other direction then decals are created only on some faces and not others.

    How can I make this works?
    It would be awesome if you could post your code here, such that I can reproduce your issue and see what is going wrong. It is nearly impossible to help just with a description.


  16. Posts
    2
    bullet code :

    Code:  
    1. {
    2.     Ray ray = new Ray(positionAtCreation, directionAtCreation);
    3.  
    4.     if (Other.collider.Raycast (ray, out hit, 100.0f))
    5.     {
    6.          RealTimePerObjectDecalCreation decal = Other.gameObject.GetComponent("RealTimePerObjectDecalCreation") as RealTimePerObjectDecalCreation;
    7.            
    8.         if (decal != null)
    9.         {
    10.             decal.CreatePerObjectDecal(Other, hit.point, hit.normal);
    11.             Destroy(gameObject);
    12.          } 
    13.     }
    14. }

    now the Decal script derived from the BulletExampleCS script from the example.
    The script is attached to the moving game Object and the decalPrefab is set manually.
    Code:  
    1.     public GameObject decalsPrefab;
    2.  
    3.     private void Start () {
    4.         m_Decals = decalsPrefab.GetComponentInChildren <DS_Decals> ();
    5.        
    6.         if (m_Decals == null) {
    7.             Debug.LogError ("The 'decalsPrefab' does not contain a 'DS_Decals' instance!");
    8.         } else {
    9.            
    10.                 // Create the decals mesh (intermediate mesh data) for our decals instance.
    11.                 // Further we need a decals mesh cutter instance and the world to decals matrix.
    12.             m_DecalsMesh = new DecalsMesh (m_Decals);
    13.             m_DecalsMeshCutter = new DecalsMeshCutter ();
    14.             m_WorldToDecalsMatrix = m_Decals.CachedTransform.worldToLocalMatrix;
    15.         }
    16.     }
    17.  
    18.     public void CreatePerObjectDecal( Collision collisionInfo,Vector3 contactPoint, Vector3 surfaceNormal)
    19.     {
    20.         // Make sure there are not too many projectors.
    21.         if (m_DecalProjectors.Count >= 50)
    22.         {   
    23.                 // If there are more than 50 projectors, we remove the first one from
    24.                 // our list and certainly from the decals mesh (the intermediate mesh
    25.                 // format). All the mesh data that belongs to this projector will
    26.                 // be removed.
    27.             DecalProjector l_DecalProjector = m_DecalProjectors [0];
    28.             m_DecalProjectors.RemoveAt (0);
    29.             m_DecalsMesh.RemoveProjector (l_DecalProjector);
    30.         }
    31.        
    32.         // Calculate the position and rotation for the new decal projector.
    33.         Vector3 l_ProjectorPosition = contactPoint + (decalProjectorOffset * surfaceNormal.normalized);
    34.         Vector3 l_ForwardDirection = Vector3.up;
    35.         Vector3 l_UpDirection = surfaceNormal;
    36.         Quaternion l_ProjectorRotation = Quaternion.LookRotation (l_ForwardDirection, l_UpDirection);
    37.        
    38.         // We hit a collider. Next we have to find the mesh that belongs to the collider.
    39.         // That step depends on how you set up your mesh filters and collider relative to
    40.         // each other in the game objects. It is important to have a consistent way in order
    41.         // to have a simpler implementation.
    42.        
    43.        
    44.         MeshCollider l_MeshCollider = collisionInfo.collider.GetComponent <MeshCollider> ();
    45.         MeshFilter l_MeshFilter = collisionInfo.collider.GetComponent <MeshFilter> ();
    46.  
    47.        
    48.         if (l_MeshCollider != null || l_MeshFilter != null) {
    49.             Mesh l_Mesh = null;
    50.             if (l_MeshCollider != null) {
    51.                
    52.                     // Mesh collider was hit. Just use the mesh data from that one.
    53.                 l_Mesh = l_MeshCollider.sharedMesh;
    54.             } else if (l_MeshFilter != null) {
    55.                
    56.                     // Otherwise take the data from the shared mesh.
    57.                 l_Mesh = l_MeshFilter.sharedMesh;
    58.             }
    59.            
    60.             if (l_Mesh != null) {
    61.                
    62.                     // Create the decal projector.
    63.                 DecalProjector l_DecalProjector = new DecalProjector (l_ProjectorPosition, l_ProjectorRotation, decalProjectorScale, cullingAngle, meshOffset, m_UVRectangleIndex, m_UVRectangleIndex);
    64.  
    65.                     // Add the projector to our list and the decals mesh, such that both are
    66.                     // synchronized. All the mesh data that is now added to the decals mesh
    67.                     // will belong to this projector.
    68.                 m_DecalProjectors.Add (l_DecalProjector);
    69.                 m_DecalsMesh.AddProjector (l_DecalProjector);
    70.                
    71.                     // Get the required matrices.
    72.                 Matrix4x4 l_WorldToMeshMatrix = collisionInfo.collider.renderer.transform.worldToLocalMatrix;
    73.                 Matrix4x4 l_MeshToWorldMatrix = collisionInfo.collider.renderer.transform.localToWorldMatrix;
    74.                
    75.                     // Add the mesh data to the decals mesh, cut and offset it before we pass it
    76.                     // to the decals instance to be displayed.
    77.                 m_DecalsMesh.Add (l_Mesh, l_WorldToMeshMatrix, l_MeshToWorldMatrix);                       
    78.                 m_DecalsMeshCutter.CutDecalsPlanes (m_DecalsMesh);
    79.                 m_DecalsMesh.OffsetActiveProjectorVertices ();
    80.                 m_Decals.UpdateDecalsMeshes (m_DecalsMesh);
    81.                
    82.                     // For the next hit, use a new uv rectangle. Usually, you would select the uv rectangle
    83.                     // based on the surface you have hit.
    84.                 NextUVRectangleIndex ();
    85.             }
    86.         }
    87.     }


    The rest of the code is exactly the same as the original file.


  17. Posts
    2,871
    I had a closer look at the issue. I didn't have the time to analyze your code yet, but quickly made my own implementation. The first script is like the usual bullet example, but there is a if to find out, whether we are dealing with a dynamic object. In that case we call the method from the second script. The first script is a replacement for the bullet example script, while the secondary one is supposed to be placed in the dynamic object. I assume that the dynamic object has a rigidbody, but you can certainly change that.
    I am not entirely sure if there are culling angle issues. I had to increase it to 180 in order to get better results, but I couldn't have a close enough look yet in order to find out if it is working correctly.

    Hope this helps

    Code:  
    1. //
    2. // Author:
    3. //   Andreas Suter (andy@edelweissinteractive.com)
    4. //
    5. // Copyright (C) 2012 Edelweiss Interactive (http://edelweissinteractive.com)
    6. //
    7.  
    8. #pragma strict
    9.  
    10. import System.Collections.Generic;
    11. import Edelweiss.DecalSystem;
    12.  
    13. public class BulletExampleDynamicJS extends MonoBehaviour {
    14.  
    15.         // The prefab which contains the DS_Decals script with already set material and
    16.         // uv rectangles.
    17.     var decalsPrefab : GameObject;
    18.    
    19.         // The reference to the instantiated prefab's DS_Decals instance.
    20.     private var m_Decals : DS_Decals;
    21.     private var m_WorldToDecalsMatrix : Matrix4x4;
    22.  
    23.         // All the projectors that were created at runtime.
    24.     private var m_DecalProjectors : List.<DecalProjector> = List.<DecalProjector> ();
    25.    
    26.         // Intermediate mesh data. Mesh data is added to that one for a specific projector
    27.         // in order to perform the cutting.
    28.     private var m_DecalsMesh : DecalsMesh;
    29.     private var m_DecalsMeshCutter : DecalsMeshCutter;
    30.    
    31.         // The raycast hits a collider at a certain position. This value indicated how far we need to
    32.         // go back from that hit point along the ray of the raycast to place the new decal projector. Set
    33.         // this value to 0.0f to see why this is needed.
    34.     var decalProjectorOffset : float = 0.5;
    35.    
    36.         // The size of new decal projectors.
    37.     var decalScale : Vector3 = Vector3 (0.2, 2.0, 0.2);
    38.     var cullingAngle : float = 90.0;
    39.     var meshOffset : float = 0.001;
    40.    
    41.         // We iterate through all the defined uv rectangles. This one indices which index we are using at
    42.         // the moment.
    43.     private var m_UVRectangleIndex : int = 0;
    44.  
    45.         // Move on to the next uv rectangle index.
    46.     private function NextUVRectangleIndex () {
    47.         m_UVRectangleIndex = m_UVRectangleIndex + 1;
    48.         if (m_UVRectangleIndex >= m_Decals.uvRectangles.Length) {
    49.             m_UVRectangleIndex = 0;
    50.         }
    51.     }
    52.    
    53.     function Start () {
    54.    
    55.             // Instantiate the prefab and get its decals instance.
    56.         var l_Instance = UnityEngine.Object.Instantiate (decalsPrefab);
    57.         m_Decals = l_Instance.GetComponentInChildren.<DS_Decals> ();
    58.        
    59.         if (m_Decals == null) {
    60.             Debug.LogError ("The 'decalsPrefab' does not contain a 'DS_Decals' instance!");
    61.         } else {
    62.        
    63.                 // Create the decals mesh (intermediate mesh data) for our decals instance.
    64.                 // Further we need a decals mesh cutter instance and the world to decals matrix.
    65.             m_DecalsMesh = new DecalsMesh (m_Decals);
    66.             m_DecalsMeshCutter = new DecalsMeshCutter ();
    67.             m_WorldToDecalsMatrix = m_Decals.CachedTransform.worldToLocalMatrix;
    68.         }
    69.     }
    70.  
    71.     function Update () {
    72.         if (Input.GetButtonDown ("Fire1")) {
    73.             var l_Ray = Camera.main.ViewportPointToRay (Vector3 (0.5f, 0.5f, 0.0f));
    74.             var l_RaycastHit : RaycastHit;
    75.             if (Physics.Raycast (l_Ray, l_RaycastHit, Mathf.Infinity)) {
    76.                
    77.                 if (l_RaycastHit.rigidbody == null) {
    78.                
    79.                         // Collider hit.
    80.                    
    81.                         // Make sure there are not too many projectors.
    82.                     if (m_DecalProjectors.Count >= 50) {
    83.                    
    84.                             // If there are more than 50 projectors, we remove the first one from
    85.                             // our list and certainly from the decals mesh (the intermediate mesh
    86.                             // format). All the mesh data that belongs to this projector will
    87.                             // be removed.
    88.                         var l_DecalProjectorForRemoval = m_DecalProjectors [0];
    89.                         m_DecalProjectors.RemoveAt (0);
    90.                         m_DecalsMesh.RemoveProjector (l_DecalProjectorForRemoval);
    91.                     }
    92.                    
    93.                         // Calculate the position and rotation for the new decal projector.
    94.                     var l_ProjectorPosition = l_RaycastHit.point - (decalProjectorOffset * l_Ray.direction.normalized);
    95.                     var l_ForwardDirection = Camera.main.transform.up;
    96.                     var l_UpDirection = - Camera.main.transform.forward;
    97.                     var l_ProjectorRotation = Quaternion.LookRotation (l_ForwardDirection, l_UpDirection);
    98.                    
    99.                         // Randomize the rotation.
    100.                     var l_RandomRotation = Quaternion.Euler (0.0f, Random.Range (0.0f, 360.0f), 0.0f);
    101.                     l_ProjectorRotation = l_ProjectorRotation * l_RandomRotation;
    102.                    
    103.                     var l_TerrainCollider : TerrainCollider = l_RaycastHit.collider as TerrainCollider;
    104.                     if (l_TerrainCollider != null) {
    105.                    
    106.                             // Terrain collider hit.
    107.                    
    108.                         var l_Terrain : Terrain = l_TerrainCollider.GetComponent.<Terrain> ();
    109.                         if (l_Terrain != null) {
    110.                            
    111.                                 // Create the decal projector with all the required information.
    112.                             var l_TerrainDecalProjector = DecalProjector (l_ProjectorPosition, l_ProjectorRotation, decalScale, cullingAngle, meshOffset, m_UVRectangleIndex, m_UVRectangleIndex);
    113.                            
    114.                                 // Add the projector to our list and the decals mesh, such that both are
    115.                                 // synchronized. All the mesh data that is now added to the decals mesh
    116.                                 // will belong to this projector.
    117.                             m_DecalProjectors.Add (l_TerrainDecalProjector);
    118.                             m_DecalsMesh.AddProjector (l_TerrainDecalProjector);
    119.                            
    120.                                 // The terrain data has to be converted to the decals instance's space.
    121.                             var l_TerrainToDecalsMatrix = Matrix4x4.TRS (l_Terrain.transform.position, Quaternion.identity, Vector3.one) * m_WorldToDecalsMatrix;
    122.                            
    123.                                 // Pass the terrain data with the corresponding conversion to the decals mesh.
    124.                             m_DecalsMesh.Add (l_Terrain, l_TerrainToDecalsMatrix);
    125.                        
    126.                                 // Cut the data in the decals mesh accoring to the size and position of the decal projector. Offset the
    127.                                 // vertices afterwards and pass the newly computed mesh to the decals instance, such that it becomes
    128.                                 // visible.
    129.                             m_DecalsMeshCutter.CutDecalsPlanes (m_DecalsMesh);
    130.                             m_DecalsMesh.OffsetActiveProjectorVertices ();
    131.                             m_Decals.UpdateDecalsMeshes (m_DecalsMesh);
    132.                            
    133.                                 // For the next hit, use a new uv rectangle. Usually, you would select the uv rectangle
    134.                                 // based on the surface you have hit.
    135.                             NextUVRectangleIndex ();
    136.                         } else {
    137.                             Debug.Log ("Terrain is null!");
    138.                         }
    139.                        
    140.                     } else {
    141.                        
    142.                             // We hit a collider. Next we have to find the mesh that belongs to the collider.
    143.                             // That step depends on how you set up your mesh filters and collider relative to
    144.                             // each other in the game objects. It is important to have a consistent way in order
    145.                             // to have a simpler implementation.
    146.                        
    147.                         var l_MeshCollider = l_RaycastHit.collider.GetComponent.<MeshCollider> ();
    148.                         var l_MeshFilter = l_RaycastHit.collider.GetComponent.<MeshFilter> ();
    149.                        
    150.                         if (l_MeshCollider != null || l_MeshFilter != null) {
    151.                             var l_Mesh : Mesh = null;
    152.                             if (l_MeshCollider != null) {
    153.                            
    154.                                     // Mesh collider was hit. Just use the mesh data from that one.
    155.                                 l_Mesh = l_MeshCollider.sharedMesh;
    156.                             } else if (l_MeshFilter != null) {
    157.                            
    158.                                     // Otherwise take the data from the shared mesh.
    159.                                 l_Mesh = l_MeshFilter.sharedMesh;
    160.                             }
    161.                            
    162.                             if (l_Mesh != null) {
    163.                            
    164.                                     // Create the decal projector.
    165.                                 var l_DecalProjector = DecalProjector (l_ProjectorPosition, l_ProjectorRotation, decalScale, cullingAngle, meshOffset, m_UVRectangleIndex, m_UVRectangleIndex);
    166.                                
    167.                                     // Add the projector to our list and the decals mesh, such that both are
    168.                                     // synchronized. All the mesh data that is now added to the decals mesh
    169.                                     // will belong to this projector.
    170.                                 m_DecalProjectors.Add (l_DecalProjector);
    171.                                 m_DecalsMesh.AddProjector (l_DecalProjector);
    172.                                
    173.                                     // Get the required matrices.
    174.                                 var l_WorldToMeshMatrix = l_RaycastHit.collider.renderer.transform.worldToLocalMatrix;
    175.                                 var l_MeshToWorldMatrix = l_RaycastHit.collider.renderer.transform.localToWorldMatrix;
    176.                                
    177.                                     // Add the mesh data to the decals mesh, cut and offset it before we pass it
    178.                                     // to the decals instance to be displayed.
    179.                                 m_DecalsMesh.Add (l_Mesh, l_WorldToMeshMatrix, l_MeshToWorldMatrix);                       
    180.                                 m_DecalsMeshCutter.CutDecalsPlanes (m_DecalsMesh);
    181.                                 m_DecalsMesh.OffsetActiveProjectorVertices ();
    182.                                 m_Decals.UpdateDecalsMeshes (m_DecalsMesh);
    183.                                
    184.                                 NextUVRectangleIndex ();
    185.                             }
    186.                         }
    187.                     }
    188.                    
    189.                 } else {
    190.                    
    191.                         // Dynamic object case.
    192.                     var l_BulletExampleDynamicObject = l_RaycastHit.collider.GetComponent.<BulletExampleDynamicObjectJS> ();
    193.                     l_BulletExampleDynamicObject.AddDecalProjector (l_Ray, l_RaycastHit);
    194.                 }
    195.             }
    196.         }
    197.     }
    198. }

    Code:  
    1. //
    2. // Author:
    3. //   Andreas Suter (andy@edelweissinteractive.com)
    4. //
    5. // Copyright (C) 2012 Edelweiss Interactive (http://edelweissinteractive.com)
    6. //
    7.  
    8. #pragma strict
    9.  
    10. import System.Collections.Generic;
    11. import Edelweiss.DecalSystem;
    12.  
    13. public class BulletExampleDynamicObjectJS extends MonoBehaviour {
    14.  
    15.         // The prefab which contains the DS_Decals script with already set material and
    16.         // uv rectangles.
    17.     var decalsPrefab : GameObject;
    18.    
    19.         // The reference to the instantiated prefab's DS_Decals instance.
    20.     private var m_Decals : DS_Decals;
    21.     private var m_WorldToDecalsMatrix : Matrix4x4;
    22.  
    23.         // All the projectors that were created at runtime.
    24.     private var m_DecalProjectors : List.<DecalProjector> = List.<DecalProjector> ();
    25.    
    26.         // Intermediate mesh data. Mesh data is added to that one for a specific projector
    27.         // in order to perform the cutting.
    28.     private var m_DecalsMesh : DecalsMesh;
    29.     private var m_DecalsMeshCutter : DecalsMeshCutter;
    30.    
    31.         // The raycast hits a collider at a certain position. This value indicated how far we need to
    32.         // go back from that hit point along the ray of the raycast to place the new decal projector. Set
    33.         // this value to 0.0f to see why this is needed.
    34.     var decalProjectorOffset : float = 0.5;
    35.    
    36.         // The size of new decal projectors.
    37.     var decalScale : Vector3 = Vector3 (0.2, 2.0, 0.2);
    38.     var cullingAngle : float = 180.0;
    39.     var meshOffset : float = 0.001;
    40.    
    41.         // We iterate through all the defined uv rectangles. This one indices which index we are using at
    42.         // the moment.
    43.     private var m_UVRectangleIndex : int = 0;
    44.  
    45.         // Move on to the next uv rectangle index.
    46.     private function NextUVRectangleIndex () {
    47.         m_UVRectangleIndex = m_UVRectangleIndex + 1;
    48.         if (m_UVRectangleIndex >= m_Decals.uvRectangles.Length) {
    49.             m_UVRectangleIndex = 0;
    50.         }
    51.     }
    52.    
    53.     function Start () {
    54.    
    55.             // Instantiate the prefab and get its decals instance.
    56.         var l_Instance = UnityEngine.Object.Instantiate (decalsPrefab);
    57.         m_Decals = l_Instance.GetComponentInChildren.<DS_Decals> ();
    58.        
    59.             // Make sure the decals move with this dynamic object.
    60.         m_Decals.transform.parent = transform;
    61.         m_Decals.transform.localPosition = Vector3.zero;
    62.         m_Decals.transform.localScale = Vector3.one;
    63.        
    64.         if (m_Decals == null) {
    65.             Debug.LogError ("The 'decalsPrefab' does not contain a 'DS_Decals' instance!");
    66.         } else {
    67.        
    68.                 // Create the decals mesh (intermediate mesh data) for our decals instance.
    69.                 // Further we need a decals mesh cutter instance and the world to decals matrix.
    70.             m_DecalsMesh = new DecalsMesh (m_Decals);
    71.             m_DecalsMeshCutter = new DecalsMeshCutter ();
    72.             m_WorldToDecalsMatrix = m_Decals.CachedTransform.worldToLocalMatrix;
    73.         }
    74.     }
    75.  
    76.     function AddDecalProjector (a_Ray : Ray, a_RaycastHit : RaycastHit) {
    77.             // Make sure there are not too many projectors.
    78.         if (m_DecalProjectors.Count >= 50) {
    79.        
    80.                 // If there are more than 50 projectors, we remove the first one from
    81.                 // our list and certainly from the decals mesh (the intermediate mesh
    82.                 // format). All the mesh data that belongs to this projector will
    83.                 // be removed.
    84.             var l_DecalProjectorForRemoval = m_DecalProjectors [0];
    85.             m_DecalProjectors.RemoveAt (0);
    86.             m_DecalsMesh.RemoveProjector (l_DecalProjectorForRemoval);
    87.         }
    88.        
    89.             // Calculate the position and rotation for the new decal projector.
    90.         var l_ProjectorPosition = a_RaycastHit.point - (decalProjectorOffset * a_Ray.direction.normalized);
    91.         var l_ForwardDirection = Camera.main.transform.up;
    92.         var l_UpDirection = - Camera.main.transform.forward;
    93.         var l_ProjectorRotation = Quaternion.LookRotation (l_ForwardDirection, l_UpDirection);
    94.        
    95.             // Randomize the rotation.
    96.         var l_RandomRotation = Quaternion.Euler (0.0f, Random.Range (0.0f, 360.0f), 0.0f);
    97.         l_ProjectorRotation = l_ProjectorRotation * l_RandomRotation;
    98.        
    99.        
    100.             // We hit a collider. Next we have to find the mesh that belongs to the collider.
    101.             // That step depends on how you set up your mesh filters and collider relative to
    102.             // each other in the game objects. It is important to have a consistent way in order
    103.             // to have a simpler implementation.
    104.        
    105.         var l_MeshCollider = a_RaycastHit.collider.GetComponent.<MeshCollider> ();
    106.         var l_MeshFilter = a_RaycastHit.collider.GetComponent.<MeshFilter> ();
    107.        
    108.         if (l_MeshCollider != null || l_MeshFilter != null) {
    109.             var l_Mesh : Mesh = null;
    110.             if (l_MeshCollider != null) {
    111.            
    112.                     // Mesh collider was hit. Just use the mesh data from that one.
    113.                 l_Mesh = l_MeshCollider.sharedMesh;
    114.             } else if (l_MeshFilter != null) {
    115.            
    116.                     // Otherwise take the data from the shared mesh.
    117.                 l_Mesh = l_MeshFilter.sharedMesh;
    118.             }
    119.            
    120.             if (l_Mesh != null) {
    121.            
    122.                     // Create the decal projector.
    123.                 var l_DecalProjector = DecalProjector (l_ProjectorPosition, l_ProjectorRotation, decalScale, cullingAngle, meshOffset, m_UVRectangleIndex, m_UVRectangleIndex);
    124.                
    125.                     // Add the projector to our list and the decals mesh, such that both are
    126.                     // synchronized. All the mesh data that is now added to the decals mesh
    127.                     // will belong to this projector.
    128.                 m_DecalProjectors.Add (l_DecalProjector);
    129.                 m_DecalsMesh.AddProjector (l_DecalProjector);
    130.                
    131.                     // Get the required matrices.
    132.                 var l_WorldToMeshMatrix = a_RaycastHit.collider.renderer.transform.worldToLocalMatrix;
    133.                 var l_MeshToWorldMatrix = a_RaycastHit.collider.renderer.transform.localToWorldMatrix;
    134.                
    135.                     // Add the mesh data to the decals mesh, cut and offset it before we pass it
    136.                     // to the decals instance to be displayed.
    137.                 m_DecalsMesh.Add (l_Mesh, l_WorldToMeshMatrix, l_MeshToWorldMatrix);                       
    138.                 m_DecalsMeshCutter.CutDecalsPlanes (m_DecalsMesh);
    139.                 m_DecalsMesh.OffsetActiveProjectorVertices ();
    140.                 m_Decals.UpdateDecalsMeshes (m_DecalsMesh);
    141.                
    142.                 NextUVRectangleIndex ();
    143.             }
    144.         }
    145.     }
    146. }


  18. Posts
    1

    Small issue

    Hi,
    First thanks for this excellent tool!
    Found one small issue when target object / mesh is rotated x=90, Y=180, decals are projected to wrong side of surface.
    Steps to reproduce.
    1. Create Cube (make sure it's rotated 0, 0, 0)
    2. Create "Decals", (random material, all default settings will work)
    3. Change projector so, that it projects image to cube.
    4. Rotate cube x=90, y=180
    5. Click Update All Projectors
    Decal is projected to inside.

    Thanks, i hope this helps to improve already excellent tool!
    Imre


  19. Posts
    2,871
    Quote Originally Posted by Imre View Post
    Hi,
    First thanks for this excellent tool!
    Found one small issue when target object / mesh is rotated x=90, Y=180, decals are projected to wrong side of surface.
    Steps to reproduce.
    1. Create Cube (make sure it's rotated 0, 0, 0)
    2. Create "Decals", (random material, all default settings will work)
    3. Change projector so, that it projects image to cube.
    4. Rotate cube x=90, y=180
    5. Click Update All Projectors
    Decal is projected to inside.

    Thanks, i hope this helps to improve already excellent tool!
    Imre
    Thanks for pointing that out! That one was reported just recently by mail and I was just able to resolved it. It will take one or two weeks until I can publish the next versions, because of a lack of time.

    Thanks a lot for taking the time and making it reproducible!


  20. Location
    Thailand
    Posts
    10
    Hi, just wanted to share an issue (self inflicted) that took me an age to work out, that others might fall into also.

    The system makes use of the tag MainCamera (used in the scripts too). I had more than one camera in my scene, the original Main Camera and the FPS Camera. Both were tagged with MainCamera (by default?). This caused the following issue that can be hard to sort out.

    Putting a sphere (for example) in the scene, using script such as BulletExampleCS/JS, then walking around shooting nice little holes in it. What I was getting was that I could shoot happily when facing the object or from +/- 90 from front on, but nothing from beyong these points (behind). The Decals Clone that is created on start, was not moving for shots from these angles at all. (This was maddening and hard to spot as other effects were kicking in - like sparks/blood splatter, ausio, etc, but not the decal).

    Obvious in hindsight that the fixed scene camera (which took presidence) was culling the reverse side as it saw it. Took me several hair pulling days to discover my faux pas - simply setting the tag to Untagged on the scene camera fixed it instantly. Doh!
    --------------------------------------
    Richard Lee
    CTO AngloThai Solutions
    CTO Electronik Eel Studios

Page 9 of 47 FirstFirst ... 789101119 ... LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •