Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by bodhizafa · Nov 12, 2020 at 04:33 PM · dots

script created ECS Entity renders black

I'm trying to wrap my head around how ECS entities work in code, and I'm having trouble creating one that works. My goal is to be able to create an ECS entity in code without going through a GameObject.

I'm using the URP and unity 2020.1.12f1. I made a normal GameObject and attached the following code to it (and set the Material to a basic blue material). I do get a sphere in my scene, but it renders as black (as if the shader is broken). When I create a normal GameObject sphere, apply the associated material for it and look at it in the Entity Debugger, it has a bunch more components (AmbientProbeTag, BuiltinMatrialPropertyUnity_, etc).

I'm assuming it won't render properly cause some or all of these are missing, but I'm not sure how to figure out which of them I need, and what to set them to. Ideally I'd like a workflow that lets me go from a material (or more generally, an ECS system and a set of behaviors, in this case "URP" and "render correctly") to the set of components that I need to set up for it to work.

code:

     using System.Collections.Generic;
     using UnityEngine;
     using Unity.Entities;
     using Unity.Transforms;
     using Unity.Rendering;
     using Unity.Mathematics;
     public class ECSSpawner : MonoBehaviour
     {
         [SerializeField] private Material unitMaterial;
         void Start()
         {
             var em = World.DefaultGameObjectInjectionWorld.EntityManager;
             var mesh = Resources.GetBuiltinResource<Mesh>("New-Sphere.fbx");
     
             var arch = em.CreateArchetype(
                 typeof(Translation),
                 typeof(Rotation),
                 typeof(RenderMesh),
                 typeof(RenderBounds),
                 typeof(LocalToWorld)
             );
             var ent = em.CreateEntity(arch);
             em.AddComponentData(ent, new Translation {Value = new float3(0, 1, 0)});
             em.AddSharedComponentData(ent, new RenderMesh { mesh = mesh, material = unitMaterial});
             print("Created an entity");
         }
     }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by bodhizafa · Nov 12, 2020 at 11:51 PM

The code that works (at least in this instance) is as follows. I got this by reading MeshRendererConversion.cs in the hybrid renderer. Unfortunately it doesn't look like this logic from there (and the bunch of other stuff it does that I'll probably need at some point) is exposed publicly in a way that doesn't require a GameObject.

 public class ECSSpawner : MonoBehaviour
 {
     [SerializeField] private Material unitMaterial;
     void Start () {
         var mesh = Resources.GetBuiltinResource<Mesh>("New-Sphere.fbx");
 
         var world = World.DefaultGameObjectInjectionWorld;
         var em = world.EntityManager;
         var archetype = em.CreateArchetype(
             typeof(RenderMesh),
             typeof(RenderBounds),
             typeof(WorldRenderBounds),
             typeof(LocalToWorld),
             typeof(AmbientProbeTag), // Seems to work without this
             typeof(PerInstanceCullingTag), // works without this
             typeof(BuiltinMaterialPropertyUnity_RenderingLayer), // works without this
             typeof(BuiltinMaterialPropertyUnity_LightData),
             typeof(WorldToLocal_Tag)
         );
         float3 position = new float3(0, 1, 0);
 
         var entity = em.CreateEntity(archetype);
         em.SetComponentData(entity, new LocalToWorld {
             Value = float4x4.TRS(
                 translation:    position,
                 rotation:       quaternion.identity,
                 scale:          Vector3.one
             )
         });
         em.SetSharedComponentData(entity, new RenderMesh {
             mesh = mesh,
             material = unitMaterial,
             subMesh = 0,
             layer = 0,
             castShadows = UnityEngine.Rendering.ShadowCastingMode.On,
             receiveShadows = true
         });
         em.SetComponentData(entity, new RenderBounds { Value = mesh.bounds.ToAABB() });
         em.SetComponentData(entity, new BuiltinMaterialPropertyUnity_RenderingLayer { Value = new uint4(1, 0, 0, 0) });
         em.SetComponentData(entity, new BuiltinMaterialPropertyUnity_LightData { Value = new float4(0, 0, 1, 0) });
     }
 }
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Goularou · Jan 24 at 10:08 PM 0
Share

THANKS A LOT to @bodhizafa for his very useful & detailed answer, working!

avatar image
2

Answer by andrew-lukasik · Jan 24 at 10:53 PM

Since this answer received a ping I'll let myself post a simple and universal (render pipeline-wise) answer:

 using UnityEngine;
 using UnityEngine.Rendering;
 using Unity.Entities;
 using Unity.Rendering;
 using Unity.Transforms;
 using Unity.Mathematics;
 public class LetsCreateARenderMeshEntity : MonoBehaviour
 {
     [SerializeField] Mesh _mesh = null;
     [SerializeField] Material _material = null;
     void Start ()
     {
         var entityManager = World.DefaultGameObjectInjectionWorld.EntityManager;
 
         Entity prefab = entityManager.CreateEntity( typeof(Prefab) );// prefab is invisible & inactive
         RenderMeshUtility.AddComponents( prefab , entityManager , new RenderMeshDescription(
             mesh:_mesh , material:_material , shadowCastingMode:ShadowCastingMode.On , receiveShadows:true
         ) );
 
         float3 position = new float3( 1f , 2f , 3f );
         quaternion rotation = quaternion.Euler( 0f , 0f , 0f );
         float3 scale = Vector3.one;
 
         Entity instance = entityManager.Instantiate( prefab );
         entityManager.SetComponentData( instance , new LocalToWorld{
             Value = float4x4.TRS( position , rotation , scale )
         } );
     }
 }


Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by adrian2256 · Jan 09, 2021 at 05:14 PM

Seems like you need to set the "Baked Global Illumination" off, in the Lightning tab. I think they haven't added it yet to DOTS.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Goularou · Jan 24 at 10:08 PM 0
Share

This appears unrelated to Baked Global Illu$$anonymous$$ation.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

141 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Label dots in between numbers (currency formatting) 2 Answers

How to determine which side of an object the player collided with? 2 Answers

Acessing static variable from Entities.ForEach 1 Answer

DOTS Grid-based generation question. 1 Answer

Where is com.unity.animation? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges